Distributed Computing
Synchronization
Dr. Muhammad Khuram Shahzad
A-308
SEECS Faculty Block
Special thanks to Dr. Asad Waqar Malik for Course Materials, lectures, labs, and
course contents can be updated for the good.
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?
❏ You will miss the bus!
❏ What if your watch is Fast by 15 minutes?
❏ You will end up waiting for a longer time
❏ Time Synchronization is required in both cases 2
Synchronization In the Cloud
❏ Consider Airline reservation system
❏ Server A receives a client request to purchase last ticket on flight ABC 123
❏ Server A timestamps purchase using local clock 9h:15m:32.45s and logs it.
❏ Replies ok to client
❏ 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
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 7
❏ Pitcher knows e1 happens
before e6, which happens
before e7
❏ Home plate umpire knows e2 is
before e3, which is before e4,
which is before e8,…….
❏ Relationship between e8 and e9
is unclear
Ways to Synchronize…...
❏ Send message from first base to home?
❏ Use a central timekeeper
❏ How long does this message take to arrive?
❏ Synchronize clocks before the game?
❏ Clock drift
8
Nothing like Perfect World…..!
❏ Messages always arrive, with propagation delay exactly d
❏ Sender sends time T in a message
❏ Receiver sets clock to T+d
❏ Real networks are unreliable
9
T
d
T+d
A
B
Synchronization
Is it possible to synchronize all clocks in distributed system?
10
Clock Synchronization - Topics
❏ Physical clock synchronization
❏ Cristian's algorithm
❏ The Berkeley Algorithm
❏ What is logical clock?
❏ Why we need logical clock?
11
Physical Clock
❏ Computer timer is depend on quartz crystal
❏ Crystal oscillate at well defined frequency
❏ Frequency depend on kind of crystal, how crystal is cut, tension
wire
Associate with crystal, two registers
❏ Counter
❏ holding register
❏ Each oscillation, decrement the counter, on zero interrupt
generated and counter reloaded from holding register 12
Cristian’s Protocol
❏ Proposed in 1989, commonly used in many protocols
❏ Server is passive
❏ Server is equipped with accurate clock or receiver
❏ Client request the server for time
❏ The problem is message delay outdated the reported time
13
Cristian’s Protocol
14
Cristian’s Protocol
❏ A send a request to B with timestamped T1
❏ B in turn will record the time of receipt T2
❏ Return response with T3, piggyback with T2
❏ A records the time of the response arrival T4
❏ Assume propagation delay is roughly the same from A-B, B-A
15
Cristian’s Protocol
16
Cristian’s Protocol - Network Time Protocol
❏ In NTP, B will also probe A for its current time
❏ The offset is computed as given above
❏ The delay estimation is calculated as
❏ delay = [(T2 - T1) + (T4 - T3) ]/2
❏ Eight pair of values are buffered (offset, delay)
❏ Finally taking the minimum value as the best estimation
17
The Berkeley Algorithm
❏ The Berkeley algorithm is a method of clock synchronization
which assumes no machine has an accurate time source
❏ It was developed by Gusella and Zatti at the University of
California, Berkeley in 1989
❏ intended for use within intranets
❏ The server process in the Berkeley algorithm, called the master,
periodically polls other slave processes.
18
The Berkeley Algorithm
❏ A master is chosen via an election process
❏ The master polls the slaves who reply with their time
❏ The master then averages the clock times
❏ Instead of sending the updated current time, the master then
sends out the amount (positive or negative) that each slave must
adjust its clock. This avoids further uncertainty due to RTT at the
slave processes
19
The Berkeley Algorithm
20
The Berkeley Algorithm
21
The Berkeley Algorithm
22
The Berkeley Algorithm
23
Lesson learned…..
❏ Clocks never exactly synchronized - Clock drift
❏ Often inadequate for distributed systems
❏ might need totally-ordered events
24
Distributed Systems
Logical Clocks
Logical Clock
❏ Consider UNIX make program again
❏ How much source and object file is outdated
❏ Thats speak for the logical clock
26
Lamport’s Logical Clocks
❏ if two process do not interact, it is not necessary their clocks be
synchronized lack of synchronization would not be observable
❏ It does not matter all processes would agree on what time it is
rather they agree on the order in which event occurs
❏ In make program what matters is input.c is older or newer
than input.o Not their absolute time creation
27
Lamport’s Logical Clocks
❏ Define a relationship “happens-before”
❏ a -> b read a happens before b
❏ Happens before relation can be observed in two situations:
❏ 1). a and b are two events and a occurs before b, then a -> b is
true
❏ 2). if a the event of message being sent by one process and b is
the event of message received by another process then a -> b is
also true
28
Lamport’s Logical Clocks
❏ Happen-before is a transitive relationship
❏ Concurrent events
❏ Two events x and y happen in different processes do not
exchange messages
x -> y or y -> x
29
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
❏ Lamport propose for assigning times to events
30
Lamport’s Logical Clocks
❏ At time 6, P1 sends message m1 to process p2
How long this will take?
❏ if message carries starting time, mean it took
10 ticks message m2 from p2 to p3 takes 16
ticks
❏ Consider message m3, it leave p3 at 60 and
arrive at p2 at 56?
❏ This is the situation that must be 31
Lamport’s Logical Clocks
❏ Lamport’s algorithm follow happens-before
relation
❏ since m3 left at 60
❏ it must arrive at 61 or later
❏ on arrival if the receivers clock shows a value
prior to the time message was sent
❏ receiver fast forward its clock
32
Lamport’s Logical Clocks Implementation
❏ Lamport’s logical clocks implementation
❏ Each process maintain a local counter Ci
1). Before executing any event, Pi executes Ci = Ci + 1
2). When Pi send a message m to Pj its set m’s timestamp equal
to Ci after executing previous step
3).Upon receipt of a message m, Pj adjusts its own local counter
as Cj max {Cj , m(ts) } and execute the step-1 and deliver the
message to the application
33
34
How to solve this
issue?
35
How to solve this issue?
36
37
Lamport’s Logical Clocks - Class Activity …1
Lamport’s Logical Clocks - Class Activity …2
38
39
40
Lamport’s Logical Clocks - Limitation
Case Study - Synchronization
In some situations, an additional requirement is desirable, i.e. no two
events ever occur at exactly at the same time. How will your
implementation ensure such a behavior with n process; where 0 < n <
1000
41
Case Study - Synchronization
Assume that each process’s logical clock is set to 0
42
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
43
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.
44
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.
45
Algorithm: Totally-Ordered Multicast
1) Update message is time stamped with sender’s logical time
2) Update message is multicast (including sender itself)
3) When message is received
4) put into local queue
5) Ordered according to timestamp,
6) Multicast acknowledgement
46
Algorithm: Totally-Ordered Multicast
❏ Message is delivered to applications only when
❏ It is at head of queue
❏ It has been acknowledged by all involved processes
❏ Pi sends an acknowledgement to Pj if
❏ Pi has not made an update request
❏ Pi’s identifier is greater than Pj’s identifier
❏ Pi’s update has been processed;
❏ Lamport algorithm (extended for total order) ensures total
ordering of events
47
Algorithm: Totally-Ordered Multicast
48
Algorithm: Totally-Ordered Multicast
❏ If P2 gets (n,1.2) before (m,1.1)
does it still multicast an
acknowledgement for (n,1.2)?
❏ At this point, how does P2 know
that there are other updates that
should be done ahead of the one it
issued?
❏ Does P2 multicast an
acknowledgement for (m,1.1) when 49
Algorithm: Totally-Ordered Multicast
50
Algorithm: Totally-Ordered Multicast
❏ To summarize, the following messages have been sent:
❏ P1 and P2 have issued update operations
❏ P1 has multicasted an acknowledgement message for (m,1.1)
❏ P2 has multicasted acknowledgement messages for (m,1.1),
(n,1.2)
❏ P1 and P2 have received an acknowledgement message from
all processes for (m,1.1)
❏ Hence, the update represented by m can proceed in both P1
and P2 51
Algorithm: Totally-Ordered Multicast
52
Vector Clocks
53
Vector Clocks
❏ Problem with Lamport’s algorithms is based on happens before
relation
❏ In Lamport clock nothing can be said about relationship between
two events a and b by looking C(a) and C(b)
54
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
❏ v1 = v2 if all n fields same
❏ v1 v2
≤ if every field in v1 is less than or equal to the
corresponding field in v2
55
Vector Clocks
Lamport clock do not capture
causality
56
Vector Clock
Lamport clock do not capture causality
This causality can be captured through
vector clock
57
Vector Clock Concept
❏ A vector clock VC(a) assigned to an event a has the property that
if
VC(a) < VC(b) for some event b
❏ Then event is known to casually precede event b
58
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
First property is maintained by incrementing VCi[i] at the occurrence of each
event
59
Vector Clock Algorithm
1) Before executing an event Pi executes VCi[i] VCi[i]+1
←
2) When process Pi sends a message m to Pj, it sets m’s vector
timestamp ts(m) equal to VCi after having executed the previous
step
3) Upon receipt of a message m, process Pj adjusts its own vector
by setting VCj[k] max{VCj[k], ts(m)[k]} for each k, after which it
←
executes the first step and delivers the message to the
application
60
61
Vector Clock
Vector Clock
A (1,0,0)
62
Vector Clock
A (1,0,0)
B (2,0,0)
63
(2,0,0)
Vector Clock
A (1,0,0)
B (2,0,0)
C(2,1,0)
64
(2,0,0)
(2,1,0)
Vector Clock
A (1,0,0)
B (2,0,0)
C(2,1,0)
D(2,2,0)
65
(2,0,0)
(2,1,0)
(2,2,0)
Vector Clock
A (1,0,0)
B (2,0,0)
C(2,1,0)
D(2,2,0)
E(0,0,1)
F(2,2,2) 66
(2,0,0)
(2,1,0)
(2,2,0)
(0,0,1)
(2,2,2)
Vector Clock
A (1,0,0)
B (2,0,0)
C(2,1,0)
D(2,2,0)
E(0,0,1)
F(2,2,2) 67
(2,0,0)
(2,1,0)
(2,2,0)
(0,0,1)
(2,2,2)
Find the
concurrent
events
Vector Clock - Obeying Causality
A (1,0,0) -> B (2,0,0)
(1,0,0) < B (2,0,0)
E(0,0,1) -> F(2,2,2)
(0,0,1) < (2,2,2)
A(1,0,0) ? E(0,0,1)
68
(2,0,0)
(2,1,0)
(2,2,0)
(0,0,1)
(2,2,2)
Vector Clock - Class Activity ..3
69
A B C D E
E’
F G
H I J
Class Activity
70
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 71
72
https://www.youtube.com/watch?v=x0VYs3n2YR4&ab_channel=NatarajanMeghanathan
Algorithms for Causal ordering of Messages
1) BSS- Birman-Schiper Stephenson Algorithm
2) SEC- Schiper-Eggli-Sandoz Algorithm
3) Matrix Algorithm
73
BSS- Birman-Schiper Stephenson Algorithm
❏ Broadcast based: a message sent is received by all other
processes
❏ Deliver a message to a process only if the message preceding it
has been delivered to the process
❏ Otherwise, buffer the message
❏ No clock increment on receiving
❏ Accomplished by using a vector accompanying the message
74
Example: https://www.geeksforgeeks.org/birman-schiper-stephenson-protocol/
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
75
BSS- Birman-Schiper Stephenson Algorithm
76
(0,0,0)
(0,0,0)
77
Class Activity- BSS
78
Solution: https://www.scribd.com/document/434169957/BSS-Example-pdf
SES- Schiper-Eggli-Sandoz Algorithm
❏ No need to broadcast - only used unicast
❏ Few messages
❏ Large-size of messages
❏ Lot of state-information
❏ Clock increment on receiving
79
80
https://www.youtube.com/watch?v=y5HvzJjYhv8&ab_channel=NatarajanMeghanathan
81
82
Question
83
Solution
https://www.youtube.com/watch?v=y5HvzJjYhv8&ab_channel=NatarajanMeghanathan
Matrix Clock
❏ Motivation
❏ My vector clock describe what I “see”
❏ In some applications, I also want to know what other people
see
❏ Matrix clock
❏ Each event has n vector clocks, one for each process
❏ The i th vector on process i is called process i ’s principle vector
❏ Principle vector is the same as vector clock before
❏ Non-principle vectors are just piggybacked on messages to 84
Matrix Clock
❏ For principle vector C on process i
❏ Increment C[i] at each “local computation” and “send” event
❏ When sending a message, all n vectors are attached to the
message
❏ At each “receive” event, let V be the principle vector of the sender,
❏ C = pairwise-max(C, V); C[i]++;
❏ For non-principle vector C on process i, suppose it corresponds to
process j
❏ At each “receive” event, let V be the vector corresponding to 85
Matrix Clock
86
Matrix Clock
Theorem:
On any given process,
any non-principle vector
is smaller than the
principle vector.
87
Matrix Clock
88

W03-L06 Physial and Logical Clocks (3).pptx

  • 1.
    Distributed Computing Synchronization Dr. MuhammadKhuram Shahzad A-308 SEECS Faculty Block Special thanks to Dr. Asad Waqar Malik for Course Materials, lectures, labs, and course contents can be updated for the good.
  • 2.
    Why synchronization? ❏ Youwant 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? ❏ You will miss the bus! ❏ What if your watch is Fast by 15 minutes? ❏ You will end up waiting for a longer time ❏ Time Synchronization is required in both cases 2
  • 3.
    Synchronization In theCloud ❏ Consider Airline reservation system ❏ Server A receives a client request to purchase last ticket on flight ABC 123 ❏ Server A timestamps purchase using local clock 9h:15m:32.45s and logs it. ❏ Replies ok to client ❏ 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
  • 4.
    A BaseBall Example Tenevents, 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
  • 5.
    A BaseBall Example Tenevents, 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
  • 6.
  • 7.
    A BaseBall Example Tenevents, 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 7 ❏ Pitcher knows e1 happens before e6, which happens before e7 ❏ Home plate umpire knows e2 is before e3, which is before e4, which is before e8,……. ❏ Relationship between e8 and e9 is unclear
  • 8.
    Ways to Synchronize…... ❏Send message from first base to home? ❏ Use a central timekeeper ❏ How long does this message take to arrive? ❏ Synchronize clocks before the game? ❏ Clock drift 8
  • 9.
    Nothing like PerfectWorld…..! ❏ Messages always arrive, with propagation delay exactly d ❏ Sender sends time T in a message ❏ Receiver sets clock to T+d ❏ Real networks are unreliable 9 T d T+d A B
  • 10.
    Synchronization Is it possibleto synchronize all clocks in distributed system? 10
  • 11.
    Clock Synchronization -Topics ❏ Physical clock synchronization ❏ Cristian's algorithm ❏ The Berkeley Algorithm ❏ What is logical clock? ❏ Why we need logical clock? 11
  • 12.
    Physical Clock ❏ Computertimer is depend on quartz crystal ❏ Crystal oscillate at well defined frequency ❏ Frequency depend on kind of crystal, how crystal is cut, tension wire Associate with crystal, two registers ❏ Counter ❏ holding register ❏ Each oscillation, decrement the counter, on zero interrupt generated and counter reloaded from holding register 12
  • 13.
    Cristian’s Protocol ❏ Proposedin 1989, commonly used in many protocols ❏ Server is passive ❏ Server is equipped with accurate clock or receiver ❏ Client request the server for time ❏ The problem is message delay outdated the reported time 13
  • 14.
  • 15.
    Cristian’s Protocol ❏ Asend a request to B with timestamped T1 ❏ B in turn will record the time of receipt T2 ❏ Return response with T3, piggyback with T2 ❏ A records the time of the response arrival T4 ❏ Assume propagation delay is roughly the same from A-B, B-A 15
  • 16.
  • 17.
    Cristian’s Protocol -Network Time Protocol ❏ In NTP, B will also probe A for its current time ❏ The offset is computed as given above ❏ The delay estimation is calculated as ❏ delay = [(T2 - T1) + (T4 - T3) ]/2 ❏ Eight pair of values are buffered (offset, delay) ❏ Finally taking the minimum value as the best estimation 17
  • 18.
    The Berkeley Algorithm ❏The Berkeley algorithm is a method of clock synchronization which assumes no machine has an accurate time source ❏ It was developed by Gusella and Zatti at the University of California, Berkeley in 1989 ❏ intended for use within intranets ❏ The server process in the Berkeley algorithm, called the master, periodically polls other slave processes. 18
  • 19.
    The Berkeley Algorithm ❏A master is chosen via an election process ❏ The master polls the slaves who reply with their time ❏ The master then averages the clock times ❏ Instead of sending the updated current time, the master then sends out the amount (positive or negative) that each slave must adjust its clock. This avoids further uncertainty due to RTT at the slave processes 19
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
    Lesson learned….. ❏ Clocksnever exactly synchronized - Clock drift ❏ Often inadequate for distributed systems ❏ might need totally-ordered events 24
  • 25.
  • 26.
    Logical Clock ❏ ConsiderUNIX make program again ❏ How much source and object file is outdated ❏ Thats speak for the logical clock 26
  • 27.
    Lamport’s Logical Clocks ❏if two process do not interact, it is not necessary their clocks be synchronized lack of synchronization would not be observable ❏ It does not matter all processes would agree on what time it is rather they agree on the order in which event occurs ❏ In make program what matters is input.c is older or newer than input.o Not their absolute time creation 27
  • 28.
    Lamport’s Logical Clocks ❏Define a relationship “happens-before” ❏ a -> b read a happens before b ❏ Happens before relation can be observed in two situations: ❏ 1). a and b are two events and a occurs before b, then a -> b is true ❏ 2). if a the event of message being sent by one process and b is the event of message received by another process then a -> b is also true 28
  • 29.
    Lamport’s Logical Clocks ❏Happen-before is a transitive relationship ❏ Concurrent events ❏ Two events x and y happen in different processes do not exchange messages x -> y or y -> x 29
  • 30.
    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 ❏ Lamport propose for assigning times to events 30
  • 31.
    Lamport’s Logical Clocks ❏At time 6, P1 sends message m1 to process p2 How long this will take? ❏ if message carries starting time, mean it took 10 ticks message m2 from p2 to p3 takes 16 ticks ❏ Consider message m3, it leave p3 at 60 and arrive at p2 at 56? ❏ This is the situation that must be 31
  • 32.
    Lamport’s Logical Clocks ❏Lamport’s algorithm follow happens-before relation ❏ since m3 left at 60 ❏ it must arrive at 61 or later ❏ on arrival if the receivers clock shows a value prior to the time message was sent ❏ receiver fast forward its clock 32
  • 33.
    Lamport’s Logical ClocksImplementation ❏ Lamport’s logical clocks implementation ❏ Each process maintain a local counter Ci 1). Before executing any event, Pi executes Ci = Ci + 1 2). When Pi send a message m to Pj its set m’s timestamp equal to Ci after executing previous step 3).Upon receipt of a message m, Pj adjusts its own local counter as Cj max {Cj , m(ts) } and execute the step-1 and deliver the message to the application 33
  • 34.
  • 35.
    How to solvethis issue? 35
  • 36.
    How to solvethis issue? 36
  • 37.
    37 Lamport’s Logical Clocks- Class Activity …1
  • 38.
    Lamport’s Logical Clocks- Class Activity …2 38
  • 39.
  • 40.
  • 41.
    Case Study -Synchronization In some situations, an additional requirement is desirable, i.e. no two events ever occur at exactly at the same time. How will your implementation ensure such a behavior with n process; where 0 < n < 1000 41
  • 42.
    Case Study -Synchronization Assume that each process’s logical clock is set to 0 42
  • 43.
    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 43
  • 44.
    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. 44
  • 45.
    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. 45
  • 46.
    Algorithm: Totally-Ordered Multicast 1)Update message is time stamped with sender’s logical time 2) Update message is multicast (including sender itself) 3) When message is received 4) put into local queue 5) Ordered according to timestamp, 6) Multicast acknowledgement 46
  • 47.
    Algorithm: Totally-Ordered Multicast ❏Message is delivered to applications only when ❏ It is at head of queue ❏ It has been acknowledged by all involved processes ❏ Pi sends an acknowledgement to Pj if ❏ Pi has not made an update request ❏ Pi’s identifier is greater than Pj’s identifier ❏ Pi’s update has been processed; ❏ Lamport algorithm (extended for total order) ensures total ordering of events 47
  • 48.
  • 49.
    Algorithm: Totally-Ordered Multicast ❏If P2 gets (n,1.2) before (m,1.1) does it still multicast an acknowledgement for (n,1.2)? ❏ At this point, how does P2 know that there are other updates that should be done ahead of the one it issued? ❏ Does P2 multicast an acknowledgement for (m,1.1) when 49
  • 50.
  • 51.
    Algorithm: Totally-Ordered Multicast ❏To summarize, the following messages have been sent: ❏ P1 and P2 have issued update operations ❏ P1 has multicasted an acknowledgement message for (m,1.1) ❏ P2 has multicasted acknowledgement messages for (m,1.1), (n,1.2) ❏ P1 and P2 have received an acknowledgement message from all processes for (m,1.1) ❏ Hence, the update represented by m can proceed in both P1 and P2 51
  • 52.
  • 53.
  • 54.
    Vector Clocks ❏ Problemwith Lamport’s algorithms is based on happens before relation ❏ In Lamport clock nothing can be said about relationship between two events a and b by looking C(a) and C(b) 54
  • 55.
    Motivation to VectorClock 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 ❏ v1 = v2 if all n fields same ❏ v1 v2 ≤ if every field in v1 is less than or equal to the corresponding field in v2 55
  • 56.
    Vector Clocks Lamport clockdo not capture causality 56
  • 57.
    Vector Clock Lamport clockdo not capture causality This causality can be captured through vector clock 57
  • 58.
    Vector Clock Concept ❏A vector clock VC(a) assigned to an event a has the property that if VC(a) < VC(b) for some event b ❏ Then event is known to casually precede event b 58
  • 59.
    Vector Clock Construction Vectorclock 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 First property is maintained by incrementing VCi[i] at the occurrence of each event 59
  • 60.
    Vector Clock Algorithm 1)Before executing an event Pi executes VCi[i] VCi[i]+1 ← 2) When process Pi sends a message m to Pj, it sets m’s vector timestamp ts(m) equal to VCi after having executed the previous step 3) Upon receipt of a message m, process Pj adjusts its own vector by setting VCj[k] max{VCj[k], ts(m)[k]} for each k, after which it ← executes the first step and delivers the message to the application 60
  • 61.
  • 62.
  • 63.
    Vector Clock A (1,0,0) B(2,0,0) 63 (2,0,0)
  • 64.
    Vector Clock A (1,0,0) B(2,0,0) C(2,1,0) 64 (2,0,0) (2,1,0)
  • 65.
    Vector Clock A (1,0,0) B(2,0,0) C(2,1,0) D(2,2,0) 65 (2,0,0) (2,1,0) (2,2,0)
  • 66.
    Vector Clock A (1,0,0) B(2,0,0) C(2,1,0) D(2,2,0) E(0,0,1) F(2,2,2) 66 (2,0,0) (2,1,0) (2,2,0) (0,0,1) (2,2,2)
  • 67.
    Vector Clock A (1,0,0) B(2,0,0) C(2,1,0) D(2,2,0) E(0,0,1) F(2,2,2) 67 (2,0,0) (2,1,0) (2,2,0) (0,0,1) (2,2,2) Find the concurrent events
  • 68.
    Vector Clock -Obeying Causality A (1,0,0) -> B (2,0,0) (1,0,0) < B (2,0,0) E(0,0,1) -> F(2,2,2) (0,0,1) < (2,2,2) A(1,0,0) ? E(0,0,1) 68 (2,0,0) (2,1,0) (2,2,0) (0,0,1) (2,2,2)
  • 69.
    Vector Clock -Class Activity ..3 69 A B C D E E’ F G H I J
  • 70.
  • 71.
    Causality If a->b thenevent 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 71
  • 72.
  • 73.
    Algorithms for Causalordering of Messages 1) BSS- Birman-Schiper Stephenson Algorithm 2) SEC- Schiper-Eggli-Sandoz Algorithm 3) Matrix Algorithm 73
  • 74.
    BSS- Birman-Schiper StephensonAlgorithm ❏ Broadcast based: a message sent is received by all other processes ❏ Deliver a message to a process only if the message preceding it has been delivered to the process ❏ Otherwise, buffer the message ❏ No clock increment on receiving ❏ Accomplished by using a vector accompanying the message 74 Example: https://www.geeksforgeeks.org/birman-schiper-stephenson-protocol/
  • 75.
    BSS- Birman-Schiper StephensonAlgorithm 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 75
  • 76.
    BSS- Birman-Schiper StephensonAlgorithm 76 (0,0,0) (0,0,0)
  • 77.
  • 78.
    Class Activity- BSS 78 Solution:https://www.scribd.com/document/434169957/BSS-Example-pdf
  • 79.
    SES- Schiper-Eggli-Sandoz Algorithm ❏No need to broadcast - only used unicast ❏ Few messages ❏ Large-size of messages ❏ Lot of state-information ❏ Clock increment on receiving 79
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
    Matrix Clock ❏ Motivation ❏My vector clock describe what I “see” ❏ In some applications, I also want to know what other people see ❏ Matrix clock ❏ Each event has n vector clocks, one for each process ❏ The i th vector on process i is called process i ’s principle vector ❏ Principle vector is the same as vector clock before ❏ Non-principle vectors are just piggybacked on messages to 84
  • 85.
    Matrix Clock ❏ Forprinciple vector C on process i ❏ Increment C[i] at each “local computation” and “send” event ❏ When sending a message, all n vectors are attached to the message ❏ At each “receive” event, let V be the principle vector of the sender, ❏ C = pairwise-max(C, V); C[i]++; ❏ For non-principle vector C on process i, suppose it corresponds to process j ❏ At each “receive” event, let V be the vector corresponding to 85
  • 86.
  • 87.
    Matrix Clock Theorem: On anygiven process, any non-principle vector is smaller than the principle vector. 87
  • 88.

Editor's Notes

  • #12 In practise when a system has n computers all n crystals will run at slightly different rates causing clock gradually to get out of sync and give different values when read out. This difference in time value is called CLOCK SKEW WWV receiver receives time through Clock skew - difference between the readings of two clocks Clock drift - difference in clock frequency with reference clock