distributed systems
play

Distributed Systems events vs. physical clocks : time of day - PDF document

Logical clocks Assign sequence numbers to messages All cooperating processes can agree on order of Distributed Systems events vs. physical clocks : time of day Assume no central time source Logical Clocks Each system maintains its


  1. Logical clocks Assign sequence numbers to messages – All cooperating processes can agree on order of Distributed Systems events – vs. physical clocks : time of day Assume no central time source Logical Clocks – Each system maintains its own local clock – No total ordering of events • No concept of happened-when Paul Krzyzanowski pxk@cs.rutgers.edu Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License. Page 1 Page 1 Page 2 Happened-before Logical clocks & concurrency Lamport’s “happened - before” notation Assign “clock” value to each event – if a  b then clock( a ) < clock( b ) a  b event a happened before event b – since time cannot run backwards e.g.: a : message being sent, b : message receipt If a and b occur on different processes that do not exchange messages, then neither a  b nor Transitive: b  a are true if a  b and b  c then a  c – These events are concurrent Page 3 Page 4 Event counting example Event counting example • Three systems: P 0 , P 1 , P 2 e a b c d f P 1 3 4 5 • Events a , b , c , … 1 2 6 g h i P 2 • Local event counter on each system 2 1 3 j k P 3 • Systems occasionally communicate 1 2 Page 5 Page 6 1

  2. Event counting example Lamport’s algorithm • Each message carries a timestamp of the e a b c d f sender’s clock P 1 3 4 5 1 2 6 g h i P 2 2 1 3 • When a message arrives: j k P 3 – if receiver’s clock < message timestamp 1 2 set system clock to (message timestamp + 1) Bad ordering: – else do nothing e  h f  k • Clock must be advanced between any two events in the same process Page 7 Page 8 Lamport’s algorithm Event counting example Algorithm allows us to maintain time ordering a b c d e f among related events P 1 1 2 3 4 5 6 g h i – Partial ordering P 2 1 2 7 j 6 k P 3 1 2 7 Page 9 Page 10 Summary Problem: Identical timestamps • Algorithm needs monotonically increasing e a b c d f software counter P 1 3 4 5 1 2 6 g h i P 2 • Incremented at least when events that need 6 1 7 j k to be timestamped occur P 3 1 7 • Each event has a Lamport timestamp a  b, b  c, …: local events sequenced attached to it i  c, f  d , d  g, … : Lamport imposes a send  receive relationship • For any two events, where a  b: L(a) < L(b) Concurrent events (e.g., a & i) may have the same timestamp … or not Page 11 Page 12 2

  3. Unique timestamps (total ordering) Unique (totally ordered) timestamps We can force each timestamp to be unique e a b c d f P 1 – Define global logical timestamp (T i , i) 3.1 4.1 5.1 1.1 2.1 6.1 g h i • T i represents local Lamport timestamp P 2 • i represents process number (globally unique) 6.2 1.2 7.2 j k – E.g. (host address, process ID) P 3 1.3 7.3 – Compare timestamps: (T i , i) < (T j , j) if and only if T i < T j or T i = T j and i < j Does not relate to event ordering Page 13 Page 14 Vector clocks Problem: Detecting causal relations Rules: 1. Vector initialized to 0 at each process If L(e) < L(e’) V i [ j ] = 0 for i, j =1, …, N – Cannot conclude that e  e’ 2. Process increments its element of the vector in local vector before timestamping event: Looking at Lamport timestamps V i [ i ] = V i [ i ] +1 3. Message is sent from process P i with V i – Cannot conclude which events are causally related attached to it 4. When P j receives message, compares vectors Solution: use a vector clock element by element and sets local vector to higher of two values V j [ i ] = max(V i [ i ], V j [ i ]) for i =1, …, N Page 15 Page 16 Comparing vector timestamps Vector timestamps Define (0,0,0) a b P 1 V = V’ iff V [ i ] = V’[ i ] for i = 1 … N V  V’ iff V [ i ]  V’[ i ] for i = 1 … N (0,0,0) c d P 2 For any two events e, e’ (0,0,0) e f if e  e’ then V(e) < V(e’) P 3 • Just like Lamport’s algorithm if V(e) < V(e’) then e  e’ Two events are concurrent if neither V(e)  V(e’) nor V(e’)  V(e) Page 17 Page 18 3

  4. Vector timestamps Vector timestamps (1,0,0) (1,0,0) (2,0,0) (0,0,0) (0,0,0) a b a b P 1 P 1 (0,0,0) (0,0,0) c d c d P 2 P 2 (0,0,0) (0,0,0) e f e f P 3 P 3 Event timestamp Event timestamp a (1,0,0) a (1,0,0) b (2,0,0) Page 19 Page 20 Vector timestamps Vector timestamps (1,0,0) (2,0,0) (1,0,0) (2,0,0) (0,0,0) (0,0,0) a b a b P 1 P 1 (2,1,0) (2,1,0) (2,2,0) (0,0,0) (0,0,0) c d c d P 2 P 2 (0,0,0) (0,0,0) e f e f P 3 P 3 Event timestamp Event timestamp a (1,0,0) a (1,0,0) b (2,0,0) b (2,0,0) c (2,1,0) c (2,1,0) d (2,2,0) Page 21 Page 22 Vector timestamps Vector timestamps (1,0,0) (2,0,0) (1,0,0) (2,0,0) (0,0,0) (0,0,0) a b a b P 1 P 1 (2,1,0) (2,2,0) (2,1,0) (2,2,0) (0,0,0) (0,0,0) c d c d P 2 P 2 (0,0,1) (0,0,1) (2,2,2) (0,0,0) (0,0,0) e f e f P 3 P 3 Event timestamp Event timestamp a (1,0,0) a (1,0,0) b (2,0,0) b (2,0,0) c (2,1,0) c (2,1,0) d (2,2,0) d (2,2,0) e (0,0,1) e (0,0,1) f (2,2,2) Page 23 Page 24 4

  5. Vector timestamps Vector timestamps (1,0,0) (2,0,0) (1,0,0) (2,0,0) (0,0,0) (0,0,0) a b a b P 1 P 1 (2,1,0) (2,2,0) (2,1,0) (2,2,0) (0,0,0) (0,0,0) c d c d P 2 P 2 (0,0,1) (2,2,2) (0,0,1) (2,2,2) (0,0,0) (0,0,0) e f e f P 3 P 3 Event timestamp Event timestamp a (1,0,0) a (1,0,0) b (2,0,0) concurrent b (2,0,0) concurrent c (2,1,0) c (2,1,0) events events d (2,2,0) d (2,2,0) e (0,0,1) e (0,0,1) f (2,2,2) f (2,2,2) Page 25 Page 26 Vector timestamps Vector timestamps (1,0,0) (2,0,0) (1,0,0) (2,0,0) (0,0,0) (0,0,0) a b a b P 1 P 1 (2,1,0) (2,2,0) (2,1,0) (2,2,0) (0,0,0) (0,0,0) c d c d P 2 P 2 (0,0,1) (2,2,2) (0,0,1) (2,2,2) (0,0,0) (0,0,0) e f e f P 3 P 3 Event timestamp Event timestamp a (1,0,0) a (1,0,0) b (2,0,0) b (2,0,0) concurrent c (2,1,0) c (2,1,0) concurrent d (2,2,0) events d (2,2,0) events e (0,0,1) e (0,0,1) f (2,2,2) f (2,2,2) Page 27 Page 28 Summary: Logical Clocks & Partial Ordering • 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 The end. • Partial Ordering – Causal events are sequenced • Total Ordering – All events are sequenced Page 29 Page 30 Page 30 5

Recommend


More recommend