Professional Documents
Culture Documents
DS Lecture Chapter 6
DS Lecture Chapter 6
Chapter 6: Synchronization
March 18, 2020
Fuad Yimer
Overview
Overview
We Discuss
The issue of synchronization based on time (actual time and relative
ordering)
How a group of processes can appoint a process as a coordinator; can be
done by means of election algorithms
Distributed mutual exclusion to protect shared resources from
simultaneous access by multiple processes
Distributed transactions, which also do the same thing, but optimize
access through advanced concurrency control mechanisms
2 / 38
Overview Essentiality of Synchronization
Essentiality of Synchronization
Synchronization is all about doing the right thing at the right time.
Communication between processes requires total ordering of all actions.
Total ordering of events
Total ordering of execution
Ordering of access to resources
Thus, synchronization of all actions forms the heart of distributed
systems, and requires some concept of time.
In distributed systems, synchronization is a necessary part of cooperation
and coordination between processes.
3 / 38
Overview Issues in Synchronization
Issues in Synchronization
Clock Synchronization
Physical clocks
Clock Synchronization
Basics
Algorithms
5 / 38
Clock Synchronization Physical clocks
Physical clocks
Sometimes we need the exact time to timestamp events in distributed
system.
But what exactly is a second, or a day?
The earth is slowing down due to tidal friction and atmospheric drag.
The day is becoming longer.
Mean solar second is computed as [avg(largeNumberofdays)]/86400.
How do we measure time?
’Absolute’ time [Einstein says no absolute time]
Astronomical time
- Based on earth’s rotation
- Not stable as the length of earths rotation varies.
- E.g., Greenwich Mean Time (GMT)
International Atomic Time (IAT)
- Based on oscillations of Cesium-133 atom.
- But a day in IAT is 3 msec less than a mean solar day.
Coordinated Universal Time (UTC)
- Calculated as an average of several worldwide atomic clocks.
- Leap seconds adjustment is made to compensate the discrepancy between
IATs day and mean solar day.
6 / 38
Clock Synchronization Clock Synchronization
7 / 38
Clock Synchronization Clock Synchronization
Goal: Never let two clocks in any system differ by more than δ time
units
Synchronize(in software) at least every δ /(2ρ) seconds , where ρ is the
maximum drift rate
In practice: 1 − ρ ≤ dC
dt ≤ 1 + ρ.
dC
>1
dt dC
Clock time, C =1
dt
ck
k
oc
clo
cl
dC
ct
st <1
rfe
ck dt
Fa
Pe
clo
w
Slo
UTC, t
9 / 38
Clock Synchronization Clock Synchronization
10 / 38
Clock Synchronization Clock Synchronization
12 / 38
Clock Synchronization Clock Synchronization
a The time server asks all the other machines for their clock values using a polling
mechanism (and providing the current time).
b The machines answer, indicating how they differ from the time sent.
c The time server tells everyone how to adjust their clock based on a calculation
of the average time value.
13 / 38
Clock Synchronization Clock Synchronization
Etc.
14 / 38
Clock Synchronization Clock Synchronization
Logical Clock
Happens-Before Relationship
Lamports Logical Clocks
Vector Clocks
16 / 38
Logical Clock Happens-Before Relationship
Happens-Before Relationship
17 / 38
Logical Clock Happens-Before Relationship
Happens-Before Relationship
18 / 38
Logical Clock Happens-Before Relationship
P1 P2 P3 P1 P2 P3
0 0 0 0 0 0
6 m1 8 10 6 m1 8 10
12 16 20 12 16 20
18 24 m2 30 18 24 m2 30
24 32 40 24 32 40
30 40 50 30 P2 adjusts 40 50
36 48 60 36 its clock 48 60
42 56 m3 70 42 61 m3 70
48 64 80 48 69 80
54 m4 72 90 70 m4 77 90
60 80 100 76 P1 adjusts 85 100
its clock
(a) (b)
a Three processes, each with its own clock running at different rates
b Lamports algorithm corrects the clocks
21 / 38
Logical Clock Happens-Before Relationship
Network layer
22 / 38
Logical Clock Happens-Before Relationship
Replicated database
Update 1 is Update 2 is
performed before performed before
update 2 update 1
Result
In absence of proper synchronization:
replica #1 ← $1111, while replica #2 ← $1110.
23 / 38
Logical Clock Happens-Before Relationship
Vector Clocks
Reading Assignment
25 / 38
Mutual Execution Overview
Overview
26 / 38
Mutual Execution A Centralized Algorithm
A Centralized Algorithm
Use a central coordinator to simulate how it is done in a one-processor
system.
A Decentralized Algorithm
Each resource has n coordinators.
A client acquires the lock by sending a request to each coordinator.
Access requires a majority vote from m(> n/2)coordinators.
If a resource is already locked, then it will be rejected (as opposed to just
blocking).
When a coordinator crashes, it will recover quickly, but will have
forgotten about permissions it had granted.
28 / 38
Mutual Execution A Distributed Algorithm
A Distributed Algorithm
When a process wants a resource, it creates a message with the name of
the resource, its process number, and the current (logical) time.
It then reliably sends the message to all processes and waits for an OK
from everyone.
When a process receives a message:
If the receiver is not accessing the resource and is not currently trying to
access it, it sends back an OK message to the sender.
If the receiver already has access to the resource, it simply does not reply.
Instead, it queues the request.
If the receiver wants to access the resource as well but has not yet done so,
it compares the timestamp of the incoming message with the one
contained in the message that it has sent everyone.
- If the incoming message has a lower timestamp, the receiver sends back an
OK.
- If its own message has a lower timestamp, it queues it up.
When done using a resource, send an OK on its queue and delete them
all from the queue.
29 / 38
Mutual Execution A Distributed Algorithm
A Distributed Algorithm
Example
30 / 38
Mutual Execution A Token Ring Algorithm
Comparison
Algorithm # msgs per Delay before entry Problems
entry/exit (in msg times)
Centralized 3 2 Coordinator crash
Decentralized 2mk + m, k = 1,2,... 2mk Starvation , low efficiency .
Distributed 2 (n – 1) 2 (n – 1) Crash of any process
Token ring 1 to ∞ 0 to n – 1 Lost token, process crash
k: number of attempts
m: number of coordinators contacted
n: the number of process.
32 / 38
Election Algorithm Overview
Overview
Many distributed systems require a process to act as coordinator (for
various reasons, e.g. to handle mutual exclusion).
The question is how to select his special process dynamically.
In many systems the coordinator is chosen manually (e.g. file servers).
The selection can also be performed automatically by an ”election
algorithm”.
- The Bully Algorithm
- A Ring Algorithm
- Superpeer Selection
The overriding goal of all election algorithms is to have all the processes
in a group agree on a coordinator.
For simplicity, we assume the following:
Processes each have a unique, positive identifier.
All processes know all other process identifiers.
The process with the highest valued identifier is duly elected coordinator.
When an election ”concludes”, a coordinator has been chosen and is
known to all processes.
33 / 38
Election Algorithm The Bully Algorithm
1 1 1
2 5 2 5 2 5
n
ctio OK Election
Ele
n
ctio
Election OK
4 6 4 6 4 6
Ele
n
El
tio
ec
ec
tio
El
n
0 3 0 3 0 3
7 7 7
Previous coordinator
has crashed
(a) (b) (c)
1 1
2 5 2 5
OK
Coordinator
4 6 4 6
0 3 0 3
7 7
(d) (e)
A Ring Algorithm
The processes are ordered in a logical ring, with each process knowing
the identifier of its successor (and the identifiers of all the other
processes in the ring).
Process with the highest priority should be elected as coordinator.
When a process notices that a coordinator is down, it creates an election
message (which contains its own number) and starts to circulate the
message around the ring.
If a successor is down, the message is passed on to the next successor.
If a message is passed on, the sender adds itself to the list.
When it gets back to the initiator, everyone had a chance to make its
presence known.
The one with the highest priority is elected as coordinator.
The initiator sends a coordinator message around the ring containing a list
of all living processes.
36 / 38
Election Algorithm A Ring Algorithm
Any Question
38 / 38