Chapter 5 TimeState
Chapter 5 TimeState
Problems:
3. Vector Clocks
• Time triggered systems: these are systems in
which certain activities are scheduled to occur at
predefined moments in time. If such activities are to
4. Causal Ordering of Messages be coordinated over a distributed system we need a
coherent notion of time.
Solutions:
The make-program example
☞ Synchronization of physical clocks
•
When the programmer has finished changing some
source files he starts make; make examines the • Computer clocks are synchronized with one
times at which all object and source files were last another to an achievable, known, degree of
modified and decides which source files have to be accuracy ⇒ within the bounds of this accuracy we
(re)compiled. can coordinate activities on different computers
local using each computer’s local clock.
physical
• Physical clock synchronization is needed for
Computer 1 652 653 654 655 656 clock
(compiler) distributed real-time systems.
P.o created
local
physical ☞ Logical clocks
Computer 2 648 649 650 651 652 clock
(editor) • In many applications we are not interested in the
P.c created physical time at which events occur; what is
important is the relative order of events!
The make-program is such an example (slide 3).
Although P.c is modified after P.o has been
generated, because of the clock drift the time
• In such situations we don’t need synchronized
assigned to P.c is smaller.
physical clocks.
Relative ordering is based on a virtual notion of
time - logical time.
P.c will not be recompiled for the new version! • Logical time is implemented using logical clocks.
m1
occurred before the event of receiving it.
P2
☞ Ordering by Lamport is based on the happened- c d
before relation (denoted by →):
m2
• a → b, if a and b are events in the same process
and a occurred before b; P3
• a → b, if a is the event of sending a message m in a e f
process, and b is the event of the same message m
being received by another process;
• If a → b and b → c, then a → c (the relation is P1, P2, P3: processes;
transitive). a, b, c, d, e, f: events;
a → b, c → d, e → f, b → c, d → f
a → c, a → d, a → f, b → d, b → f, ...
a || e, c || e, ...
m1
3 4 • For certain applications a total ordering is needed;
P2 they consider that no two events can occur at the
c d
same time.
m2
• In order to enforce total ordering a global logical
timestamp is introduced:
1 5
P3 - the global logical timestamp of an event a oc-
e f curring at process Pi, with logical timestamp
CPi(a), is a pair (CPi(a), i), where i is an identifi-
er of process Pi;
• For the make-program example we suppose that a - we define
process running a compilation notifies, through a (CPi(a), i) < (CPj(b), j) if and only if
message, the process holding the source file about
CPi(a) < CPj(b), or CPi(a) = CPj(b) and i < j.
the event P.o created ⇒ a logical clock can be used
to correctly timestamp the files.
local (1,1) (2,1)
P1
physical a b
Computer 1 652 653 654 655 656 clock
m1
(compiler) 1 2
P.o created (3,2) (4,2)
P2
local c d
physical
Computer 2 648 650 651 652
m2
649 clock
(editor) 1 3 4 (1,3) (5,3)
P.c created P3
e f
Problems with Lamport’s Logical Clocks (cont’d) Problems with Lamport’s Logical Clocks (cont’d)
1 M2 M1 M3
4 1 3
(3,2) (4,2)
P2
c d
m2
• The value of CPi v is used to assign timestamps to [R2]: a) When a is the event of sending a message m
v
events in process Pi. from process Pi, then the timestamp tm = CPi (a)
v
v
CPi(a) is the timestamp of event a in process Pi. is included in m (CPi (a) is the vector clock value
obtained after applying rule R1).
• v
CPi v
[i], the ith entry of CPi ,corresponds to Pi’s own b) On receiving message m by process Pj, its
v is updated as follows:
vector clock CPj
logical time.
∀k ∈ {1,2,..,n}, CPj
v [k] := max(Cv [k], t [k]).
Pj m
v is used to timestamp the
• v
CPi[j], j ≠ i, is Pi’s "best guess" of the logical time at Pj. c) The new value of CPj
v
CPi [j] indicates the (logical) time of occurrence event of receiving message m by Pj (applying
of the last event at Pj which is in a happened- rule R1).
before relation to the current event at Pi.
Ch1: $50
Why a problem?
$450 $200
• Because there is no global clock (no coherent
notion of time) and no shared memory! Ch2: empty
A B
Ch1: empty
$450 $250
Ch2: empty
A B
C : consistent
A Ch1 B
NC: not consistent
500 empty 200 C
500 50 200 NC
450 50 200 C
450 empty 200 NC
500 50 250 NC
450 50 250 NC
450 empty 250 C
500 empty 250 NC
Formal Definition
Global States (cont’d)
☞ It is difficult to record channel states to ensure the • send(mkij) ∈ LSi if and only if the sending event occurred
above rule ⇒ global states are very often recorded before the local state was recorded;
without using channel states. rec(mkij) ∈ LSj if and only if the receiving event occurred
This is the case in the definition below. before the local state was recorded.
Theorem
☞ Let ek denote an event at process Pk.
A cut Ct = {c1, c2, ..., cn} is a consistent cut if and only if
A cut Ct = {c1, c2, ..., cn} is a consistent cut if and only if
no two cut events are causally related, that is:
∀Pi,∀Pj, ∃ei,∃ej such that (ei → ej) ∧ (ej → cj) ∧ ¬(ei → ci) ∀ci, ∀cj :: ¬(ci → cj) ∧ ¬(cj → ci)
• A set of concurrent cut events form a consistent cut.
• A cut is consistent if every message that was
received before a cut event was sent before the cut LS11
event at the sender process. e1 LS12
P1
c1 c6
LS11 LS12
e1
P1 LS21 LS22 LS23
c1 c6 P2
c2 e2 c4 c7
LS21 LS22 LS23
P2 c8
c2 e2 c4 c7 c3 c5
P3
LS31 LS32 LS33
c3 c5 c8
P3
LS31 LS32 LS33 {c1,c2,c3}: strongly consistent (no communication line is
crossed)
{c6,c7,c8}: consistent (communication line is crossed but
{c1,c4,c5} is not consistent: (e1→e2) ∧ (e2→c4) ∧ ¬(e1→c1) no causal relation).
{c1,c4,c5}: not consistent; c1 → c4
• Such a recorded "view" of the system is called a • The state SChij of the channel Chij consists of all
snapshot. messages that process Pi sent before recording LSi
and which have not been received by Pj when
• We assume that processes are connected through recording LSj.
one directional channels and message delivery is
FIFO. • A snapshot is started at the request of a particular
process Pi, for example, when it suspects a
• We assume that the graph of processes and deadlock because of long delay in accessing a
channels is strongly connected (there exists a path resource; Pi then records its state LSi and, before
between any two processes). sending any other message, it sends a token to
every Pj that Pi communicates with.
• The algorithm is based on the use of a special
message, snapshot token, in order to control the • When Pj receives a token from Pi, and this is the
state collection process. first time it received a token, it must record its state
before it receives the next message from Pi. After
recording its state Pj sends a token to every
process it communicates with, before sending them
any other message.
Pk LSk
Pj
cj
state of Chkj
messages sent
after LSk recorded
token from Pk
messages sent
before LSk recorded messages received
after LSj recorded • Don’t forget when you look to the picture: we
messages received assumed that message passing on a channel
state of before LSj recorded connecting two processes is FIFO.
channel Chkj
LSj
Pj
The algorithm
Distributed Systems Fö 5 - 35
Summary (cont’d)