SYNCHORNIZATION Logical Clocks Logical Clocks For many algorithms

  • Slides: 17
Download presentation
SYNCHORNIZATION Logical Clocks

SYNCHORNIZATION Logical Clocks

Logical Clocks • For many algorithms, associating an event to an absolute “real time”

Logical Clocks • For many algorithms, associating an event to an absolute “real time” is not essential – What’s important is that processes agree on the ordering of events – “relative time” is not the same as “real time”. • Relative time measured by Logical Clocks – Lamport's timestamps – Vector timestamps

Lamport’s Logical Clocks • Lamport clocks define a relation called "happensbefore" • “happens before”

Lamport’s Logical Clocks • Lamport clocks define a relation called "happensbefore" • “happens before” relation denoted as → • The relation can be observed in two situations: – If a and b are events in the same process, and a occurs before b, then a → b – If a is the action of sending a message, and b is the event of receiving that message, then a → b

“Happens Before” Relation • The “happens before” relation is transitive – a → b

“Happens Before” Relation • The “happens before” relation is transitive – a → b and b → c then a → c • If two events e 1 and e 2 happen in different processes that do not exchange messages, then – e 1 → e 2 is not true – e 1 and e 2 are concurrent – No information about the order of these events

Concurrent Events • If two events happen without any message, then we can't say

Concurrent Events • If two events happen without any message, then we can't say anything about their relative occurrence in time. • We can say that a → b → c → d → f, but we can say little about e other than e → f. P 1 b a m 1 P 2 P 3 c d m 2 e f

Lamport Logical Clocks • Measuring “time” – Each event is assigned a time value

Lamport Logical Clocks • Measuring “time” – Each event is assigned a time value • ei → ej LC(ei) < LC(ej) – LCi is a local clock and contains increasing values • Each process has its own LC • Increment LC on each event occurrence – Within same process i, if ej occurs before ek • LCi(ej) < LCi(ek) – If es is a send event and er a receive event, then • LCi(es) < LCj(er)

Lamport’s Logical Clocks • Three processes, each with its own clock. – The clocks

Lamport’s Logical Clocks • Three processes, each with its own clock. – The clocks run at different rates. • Message m 3 arrives before it was sent!

Logical Clocks – Readjusting • Lamport’s algorithm corrects the clocks.

Logical Clocks – Readjusting • Lamport’s algorithm corrects the clocks.

Lamport’s Logical Clocks § To implement Lamport’s algorithm, each process, Pi, maintains a local

Lamport’s Logical Clocks § To implement Lamport’s algorithm, each process, Pi, maintains a local counter, Ci § Updating counter Ci for process Pi 1. 2. 3. Before executing an event Pi executes Ci = Ci + 1. When process Pi sends a message m to Pj, it sets m’s timestamp ts (m) equal to Ci after having executed the previous step. Upon the receipt of a message m, process Pj adjusts its own local counter as Cj = max{Cj , ts (m)}, after which it then executes the first step and delivers the message to the application.

Limitation of Lamport’s Algorithm • • ei → ej LC(ei) < LC(ej) However, LC(ei)

Limitation of Lamport’s Algorithm • • ei → ej LC(ei) < LC(ej) However, LC(ei) < LC(ej) does not imply ei → ej – For example, (1, 1) < (1, 3), but events a and e are concurrent P 1 a b (1, 1) (2, 1) P 2 P 3 Real Time m 1 c d (3, 2) (4, 2) e g (1, 3) (2, 3) m 2 (5, 3) f

Vector Clocks (1) • Lamport clocks do not capture causality – Sending m 3

Vector Clocks (1) • Lamport clocks do not capture causality – Sending m 3 may have depended on m 1 • Causality can be captured by Vector Clocks

Vector Clocks (2) • Each process and message stores a vector of clock values

Vector Clocks (2) • Each process and message stores a vector of clock values – – • Each process stores the last value seen of every other process’ clock Processes send their vector with each message A vector VCi with the following two properties: 1. VCi [ i ] is the number of events that have occurred so far at Pi. • VCi [ i ] is the local logical clock at process Pi. 2. If VCi [ j ] = k then Pi knows that k events have occurred at Pj. • Pi’s knowledge of the local time at Pj

Vector Clocks – Basic Steps carried out to accomplish property 2: 1. 2. 3.

Vector Clocks – Basic Steps carried out to accomplish property 2: 1. 2. 3. Before executing an event Pi executes VCi [ i ] ← VCi [i ] + 1. 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. Upon the 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.

Vector Clocks –Local Events • Initialization – The vector timestamp for each process is

Vector Clocks –Local Events • Initialization – The vector timestamp for each process is initialized to (0, 0, …, 0) • Local Event – When an event occurs on process Pi: • VTi[i] + 1 • For example on processor 3, (1, 2, 1, 3) (1, 2, 2, 3)

Vector Clocks – Messages • Message Passing – When Pi sends a message to

Vector Clocks – Messages • Message Passing – When Pi sends a message to Pj, the message has timestamp t[]=VTi[] – When Pj receives the message, it sets VTj[k] to max(VTj[k], t[k]), for k = 1, 2, …, N • For example, P 2 receives a message with timestamp (3, 2, 4) and P 2’s timestamp is (3, 4, 3), then P 2 adjusts its timestamp to (3, 4, 4)

Comparing Clock Vectors • VT 1 = VT 2 iff VT 1[i] = VT

Comparing Clock Vectors • VT 1 = VT 2 iff VT 1[i] = VT 2[i] for all i • VT 1 VT 2 iff VT 1[i] VT 2[i] for all i • VT 1 < VT 2 iff VT 1 VT 2 & VT 1 VT 2 – For example, (1, 2, 2) < (1, 3, 2)

Vector Clocks Analysis • P 1 Claim – e → e’ iff e. VT

Vector Clocks Analysis • P 1 Claim – e → e’ iff e. VT < e’. VT m 1 [1, 0, 0] P 2 P 3 Real Time b [2, 0, 0] a c d [2, 2, 0] [2, 1, 0] e [0, 0, 1] g [0, 0, 2] m 2 f [2, 2, 3]