Professional Documents
Culture Documents
W 5 Lectures
W 5 Lectures
Synchronization
Asad W. Malik
asad.malik{@}seecs.edu.pk
National University of Sciences and Technology (NUST)
Why synchronization?
❏ You want to catch a bus at 6:00 pm, but your watch is off by 15 minutes
❏ What if your watch is late by 15 minutes?
❏ That was the last seat. Server A send message to Server B saying “flight full”
❏ B enters “Flight ABC 123 full” + its own local clock value (which reads
9h:10m:10.11s) into its log
❏ Server C queries A’s and B’s log. Is confused that a client purchased a ticket at A
after the flight became full at B
❏ This may lead to further incorrect actions by C 3
A BaseBall Example
Ten events, three entities - Pitcher, first base, third base and home
e1: pitcher throws ball to home
e2: ball arrives at home
e3: batter hits ball to pitcher
e4: batter runs to first base
e5: runner runs to home
e6: ball arrives at pitcher
e7: pitcher throws ball to first base
e8: runner arrives at home
e9: ball arrives at first base
e10: batter arrives at first base 4
A BaseBall Example
Ten events, three entities - Pitcher, first base, third base and home
e1: pitcher throws ball to home
e2: ball arrives at home
e3: batter hits ball to pitcher
e4: batter runs to first base
e5: runner runs to home
e6: ball arrives at pitcher
e7: pitcher throws ball to first base
e8: runner arrives at home
e9: ball arrives at first base
e10: batter arrives at first base 5
A BaseBall Example
6
A BaseBall Example
Ten events, three entities - Pitcher, first base, third base and home
e1: pitcher throws ball to home
e2: ball arrives at home ❏ Pitcher knows e1 happens before
e3: batter hits ball to pitcher e6, which happens before e7
e4: batter runs to first base ❏ Home plate umpire knows e2 is
e5: runner runs to home before e3, which is before e4,
e6: ball arrives at pitcher which is before e8,…….
e7: pitcher throws ball to first base ❏ Relationship between e8 and e9 is
e8: runner arrives at home unclear
e9: ball arrives at first base
e10: batter arrives at first base 7
Ways to Synchronize…...
❏ Clock drift
8
Nothing like Perfect World…..!
B
T+d
9
Synchronization
10
Clock Synchronization - Topics
11
Clock Synchronization
13
Physical Clock
15
Cristian’s Protocol
16
Cristian’s Protocol
17
Cristian’s Protocol
18
Cristian’s Protocol
19
Cristian’s Protocol - Network Time Protocol
20
The Berkeley Algorithm
21
The Berkeley Algorithm
22
The Berkeley Algorithm
23
The Berkeley Algorithm
24
The Berkeley Algorithm
25
Lesson learned…..
27
Distributed Systems
Logical Clocks
Logical Clock
29
Lamport’s Logical Clocks
30
Lamport’s Logical Clocks
31
Lamport’s Logical Clocks
messages
x -> y or y -> x
32
Lamport’s Logical Clocks
❏ Need a way to measure a notion of time, for every event ‘a’ we can
assign a time value C(a)
❏ if a -> b then C(a) < C(b) ’a’ and ‘b’ occur on same process
❏ ’a’ is sending of a message by one process and ‘b’ is receiving of a
message by another process then
❏ C(a) < C(b) must be assign, everyone agrees
33
Lamport’s Logical Clocks
35
Lamport’s Logical Clocks Implementation
38
How to solve this issue?
39
Lamport’s Logical Clocks - Class Activity
40
41
42
Case Study - Synchronization
43
Case Study - Synchronization
Assume that each process’s logical clock is set to 0
44
Case Study - Synchronization
❏ A timestamp of 1 is associated with events a,
e, j in processes P1, P2, P3 respectively
❏ A timestamp of 2 is associated with events b, k
in processes P1, P3 respectively
❏ The times may be the same but the events are
distinct
❏ We would like to create a total order of all
events i.e. for an event a, b we would like to
say that either a -> b or b -> a
45
Example: Totally-Ordered Multicast
❏ Scenario
❏ Replicated accounts in New York(NY) and San
Francisco(SF)
❏ Two transactions occur at the same time and
multicast
❏ Current balance: $1,000
❏ Add $100 at SF
❏ Add interest of 1% at NY
❏ If not done in the same order at each site then
one site will record a total amount of $1,111 and
the other records $1,110.
46
Example: Totally-Ordered Multicast
❏ We must ensure that the two update operations are performed in the same
order at each copy
❏ Although it makes a difference whether the deposit is processed before the
interest update or the other way around, it does matter which order is followed
from the point of view of consistency
❏ We need totally-ordered multicast, that is a multicast operation by which all
messages are delivered in the same order to each receiver.
❏ NOTE: Multicast refers to the sender sending a message to a collection of
receivers.
47
Algorithm: Totally-Ordered Multicast
48
Algorithm: Totally-Ordered Multicast
50
Algorithm: Totally-Ordered Multicast
52
Algorithm: Totally-Ordered Multicast
55
Algorithm: Totally-Ordered Multicast
What if there was a third process e.g., P3 that issued an update (call it o) at
about the same time as P1 and P2.
The algorithm works as before.
P1 will not multicast an acknowledgement for o until m has been done.
P2 will not multicast an acknowledgement for o until n has been done.
Since an operation can’t proceed until acknowledgements for all processes
have been received, o will not proceed until n and m have finished
56
Totally-Ordered Multicast
57
Vector Clocks
58
Vector Clocks
59
Motivation to Vector Clock
Logical clock - Event s happens before event t => the logical clock value of s
is smaller than the logical clock value of t.
Vector clock - Event s happens before event t means the vector clock value
of s is “smaller” than the vector clock value of t
Each event has a vector of n integers as its vector clock value
61
Vector Clock
62
Vector Clock Concept
63
Vector Clock Construction
Vector clock is constructed by letting each process Pi maintain a vector VCi with two
properties
1) VCi[i] is number of events that have occurred so far at Pi. VCi[i] is logical clock at
Pi
2) VCi[j] = k then Pi knows that k events have occurred at Pj. Pi has a knowledge of
local time at Pj
The second is maintained by piggybacking vectors along with messages that are sent 64
Vector Clock Algorithm
65
Vector Clock
66
Vector Clock
A (1,0,0)
67
Vector Clock
A (1,0,0)
(2,0,0)
B (2,0,0)
68
Vector Clock
A (1,0,0)
(2,0,0)
B (2,0,0)
C(2,1,0)
(2,1,0)
69
Vector Clock
A (1,0,0)
(2,0,0)
B (2,0,0)
(2,2,0)
C(2,1,0)
(2,1,0)
D(2,2,0)
70
Vector Clock
A (1,0,0)
(2,0,0)
B (2,0,0)
(2,2,0)
C(2,1,0)
(2,1,0)
D(2,2,0) (0,0,1)
E(0,0,1) (2,2,2)
F(2,2,2) 71
Find the
concurrent
Vector Clock
events
A (1,0,0)
(2,0,0)
B (2,0,0)
(2,2,0)
C(2,1,0)
(2,1,0)
D(2,2,0) (0,0,1)
E(0,0,1) (2,2,2)
F(2,2,2) 72
Vector Clock - Obeying Causality
(2,2,2)
A(1,0,0) ? E(0,0,1)
73
Class Activity
A B C D E
E’
F G
H I J
74
Class Activity
75
Causality
If a->b then event a can affect event b
Concurrency
If neither a->b nor b->a then one event cannot affect the other
Partial Ordering
Causal events are sequenced
Total Ordering
All events are sequenced
76
77
Algorithms for Causal ordering of Messages
78
BSS- Birman-Schiper Stephenson Algorithm
79
BSS- Birman-Schiper Stephenson Algorithm
1) Process Pi increments the vector time VTpi[i], time stamps and broadcasts the message m.
VTpi[i] - 1 denotes the number of messages preceding m.
2) Pj != Pi receives m. m is delivered when:
a. VTpj[i] == VTm[i] - 1
b. VTpj[k] >= VTm[k] for all k in {1,2,..n} - {i}, n is the total number of processes. Delayed
message are queued in a sorted manner.
c. Concurrent messages are ordered by time of receipt.
3) When m is delivered at Pj, VTpj updated according Rule 2 of vector clocks
3(a) : Pj has received all Pi’s messages preceding m
3(b): Pj has received all other messages received by Pi before sending m
80
BSS- Birman-Schiper Stephenson Algorithm
81
82
Class Activity- BSS
83