Professional Documents
Culture Documents
Synchronization
Gian Pietro Picco
Dipartimento di Elettronica e Informazione
Politecnico di Milano, Italy
picco@elet.polimi.it
http://www.elet.polimi.it/~picco
P olitecnico
di M ilano
Synchronization in
Distributed Systems
The problem of synchronizing concurrent activities
arises also in non-distributed systems
However, distribution complicates matters:
Absence of a global physical clock
Absence of globally shared memory
Partial failures
P olitecnico
di M ilano
P olitecnico
di M ilano
Time
Clocks vs. timers
Time is a tricky issue per se:
Up to 1940, time is measured astronomically
1 second = 1/86400th of a solar day
Earth is slowing down, making measures inaccurate
P olitecnico
di M ilano
P olitecnico
di M ilano
Simple Algorithms
Cristian (1989)
The time server collects the time from all clients, averages, and
then retransmits the required adjustment
P olitecnico
di M ilano
Synchronization mechanisms
Multicast (over LAN)
Procedure-call mode (similar to Cristians)
Symmetric mode (for higher levels)
P olitecnico
di M ilano
P olitecnico
di M ilano
Some Observations
In many applications it is sufficient to agree
on a time, even if it is not accurate w.r.t. the
absolute time
What matters is often the ordering and
causality relationships of events, rather than
the timestamp itself
If two processes do not interact, it is not
necessary that their clocks be synchronized
P olitecnico
di M ilano
Logical
Time
Scalar
Clocks
L. Lamport. Time, clocks, and the ordering of events in a distributed system.
Communications of the ACM, 21(7):558-565, July 1978.
P1
B
C
P2
P3
Distributed Systems - 2004-2006 G.P. Picco
D
F
10
P olitecnico
di M ilano
Starts at zero
Increments clock upon each local event
Each message is timestamped with the senders scalar time
Upon receipt, the receivers clock is set to:
0 1
P1
P2
D
4
P3
Distributed Systems - 2004-2006 G.P. Picco
F
5
11
P olitecnico
di M ilano
Example
12
P olitecnico
di M ilano
Example:Totally Ordered
Multicast
Updates in sequence:
Customer deposits $100
bank adds 1% interest
13
P olitecnico
di M ilano
Vector Clocks
In scalar clocks, A B C(A) < C(B)
But the reverse does not necessarily hold, e.g., if A || B
14
P olitecnico
di M ilano
15
P olitecnico
di M ilano
Examples
P1
1,0,0 2,0,0
P2
P3
P1
P2
P3
2,1,0
2,2,0
0,0,1
2,2,2
1,0,0 2,0,0
0,1,0
2,2,0
0,0,1
0,0,2
0,0,3
16
P olitecnico
di M ilano
msg
2
Pk
Pi
Pj
Pk
msg
reply 5
1,0,0
2,2,0
msg
1,1,0 1,2,0
0,0,1 reply
msg
?
1,0,
P olitecnico
di M ilano
Mutual Exclusion
Required to prevent interference and ensure
consistency of resource access
Critical section problem, typical of OS
But here, no shared memory
Requirements:
Safety property: at most one process may execute in the critical
section at a time
Liveness property: all requests to enter/exit the critical section
eventually succeed
18
P olitecnico
di M ilano
Mutual Exclusion
with Scalar Clocks
To request access to a resource:
A process Pi sends a resource request message m, with
timestamp Tm, to all processes (including itself)
The request is put into a local queue ordered according to Tm
(process identifiers are used to break ties)
Upon receipt of m, a process sends an acknowledgment to Pi,
with timestamp >Tm
To release a resource
19
P olitecnico
di M ilano
Example
Acquire
P1:1
P3:1 P1
78
9 10
13
13
23
P1:1
P3:1 P2
P1:1
P3:1 P3
45
11
5 6
Request
11 12
Acquire
Ack
Release
Distributed Systems - 2004-2006 G.P. Picco
20
P olitecnico
di M ilano
21
P olitecnico
di M ilano
Comparison
Algorithm
Messages
per entry/exit
Centralized
Coordinator crash
Distributed
(Lamport)
3(n1)
2(n1)
Token ring
1 to
0 to n 1
22
P olitecnico
di M ilano
Leader Election
Many distributed algorithms require a process to act as a
coordinator (or some other special role)
E.g., for server-based mutual exclusion
Assumptions:
Nodes are distinguishable
Otherwise, no way to perform selection
Typically use the identifier: the process with the highest ID becomes
the leader
23
P olitecnico
di M ilano
P olitecnico
di M ilano
A Ring-Based Algorithm
25
P olitecnico
di M ilano
48
12
30
Distributed Systems - 2004-2006 G.P. Picco
20
26
P olitecnico
di M ilano
27
P olitecnico
di M ilano
Simplistic Algorithm
28
P olitecnico
di M ilano
Distributed Snapshot
Chandy-Lamport, 1985
12
30
In any case stop recording incoming message on channel the token arrived along
Recording messages
If a message arrives on a channel which is recording messages, record the arrival
of the message, then process the message as normal
Otherwise, just process the message as normal
29
Processing
P olitecnico
di M ilano
Token received,
state recording is stopped
30
P olitecnico
di M ilano
Processing
Not Yet
Processing
source
Received no token
Received some of the tokens
31
P olitecnico
di M ilano
Example
12
2
48
30
30
33
20
20
18
32
P olitecnico
di M ilano
Example
14
14
48
30
48
38
20
18
33
P olitecnico
di M ilano
Some Observations1
Distributed snapshot effectively selects a consistent cut
from the history of execution
Proof:
Consider ei and ej events at pi and pj, so that ei ej
For the cut to be consistent, if ej is in the cut, so does ei
In other words, if ej occurred before pj recorded its state, the same
must have happened for ei and pi
Obvious if pi = pj
Assume that ei and ej are in the cut, although pi recorded its state
before ei
Consider the messages m1 mn that establish ei ej
FIFO and marker rules are such that the marker would have
reached pj before ej
Therefore, ej cannot be in the cut
34
P olitecnico
di M ilano
Some Observations2
Important: the distributed snapshot algorithm does
not require blocking of the computation
Collecting the snapshot is interleaved with processing
35