KEMBAR78
Logical Clocks (Distributed computing) | PPT
Logical Clocks Paul Krzyzanowski [email_address] [email_address] Distributed Systems Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Logical clocks Assign sequence numbers to messages All cooperating processes can agree on order of events vs.  physical clocks : time of day Assume no central time source Each system maintains its own local clock No total ordering of events No concept of  happened-when
Happened-before Lamport’s “ha ppe ned-before” notation a     b   event  a  happened before event  b e.g.:  a :  message being sent,  b : message receipt Transitive: if  a      b  and  b      c  then  a      c
Logical clocks & concurrency Assign “clock” value to each event if  a  b   then  clock( a ) < clock( b ) since time cannot run backwards If  a  and  b  occur on different processes that do not exchange messages, then neither  a    b  nor  b    a  are true These events are  concurrent
Event counting example Three systems: P 0 , P 1 , P 2 Events  a ,  b ,  c , … Local event counter on each system Systems occasionally communicate
Event counting example a b h i k P 1 P 2 P 3 1 2 1 3 2 1 d f g 3 c 2 4 6 e 5 j
Event counting example a b i k j P 1 P 2 P 3 1 2 1 3 2 1 d f g 3 c 2 4 6 Bad ordering: e    h f    k h e 5
Lamport’s algorithm Each message carries a timestamp of the sender’s clock When a message arrives: if receiver’s clock < message timestamp set system clock to (message timestamp + 1) else do nothing Clock must be advanced between any two events in the same process
Lamport’s algorithm Algorithm allows us to maintain time ordering among related events Partial ordering
Event counting example a b i k j P 1 P 2 P 3 1 2 1 7 2 1 d f g 3 c 2 4 6 6 7 h e 5
Summary Algorithm needs monotonically increasing software counter Incremented at least when events that need to be timestamped occur Each event has a  Lamport timestamp  attached to it For any two events, where a    b: L(a) < L(b)
Problem: Identical timestamps a  b, b  c, …: local events sequenced i  c, f  d , d  g, … : Lamport imposes a send  receive  relationship Concurrent events (e.g., a & i)  may  have the same timestamp … or not a b h i k j P 1 P 2 P 3 1 2 1 7 7 1 d f g 3 c 6 4 6 e 5
Unique timestamps (total ordering) We can force each timestamp to be unique Define  global logical timestamp  (T i , i) T i  represents local Lamport timestamp i represents process number (globally unique) E.g. (host address, process ID) 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
Unique (totally ordered) timestamps a b i k j P 1 P 2 P 3 1.1 2.1 1.2 7.2 7.3 1.3 d f g 3.1 c 6.2 4.1 6.1 h e 5.1
Problem: Detecting causal relations If L(e) < L(e’) Cannot conclude that e  e’ Looking at Lamport timestamps Cannot conclude which events are causally related Solution: use a  vector clock
Vector clocks Rules: Vector initialized to 0 at each process V i  [ j ] = 0 for  i, j  =1, …,  N Process increments its element of the vector in local vector before timestamping event:   V i  [ i ] = V i  [ i ] +1  Message is sent from process P i  with V i  attached to it When P j  receives message, compares vectors 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
Comparing vector timestamps Define V = V’  iff  V   [ i  ] = V’[ i  ]   for  i  = 1 …  N   V    V’  iff  V   [ i  ]    V’[ i  ]   for  i  = 1 …  N  For any two events e, e’ if e    e’  then V(e) < V(e’) 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)
Vector timestamps a b c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0)
Vector timestamps a b c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0)
Vector timestamps a b c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) (2,0,0)
Vector timestamps a b c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) (2,0,0) (2,1,0)
Vector timestamps a b c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) (2,0,0) (2,1,0) (2,2,0)
Vector timestamps a b c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) e (0,0,1) (2,0,0) (2,1,0) (2,2,0) (0,0,1)
Vector timestamps a b c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) e (0,0,1) f (2,2,2) (2,0,0) (2,1,0) (2,2,0) (0,0,1) (2,2,2)
Vector timestamps (0,0,1) (1,0,0) a b c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) e (0,0,1) f (2,2,2) (2,0,0) (2,1,0) (2,2,0) (2,2,2) concurrent events
Vector timestamps a b c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) e (0,0,1) f (2,2,2) (2,0,0) (2,1,0) (2,2,0) (0,0,1) (2,2,2) concurrent events
Vector timestamps a b c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) e (0,0,1) f (2,2,2) (2,0,0) (2,1,0) (2,2,0) (0,0,1) (2,2,2) concurrent events
Vector timestamps a b c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) e (0,0,1) f (2,2,2) (2,0,0) (2,1,0) (2,2,0) (0,0,1) (2,2,2) concurrent events
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 Partial Ordering Causal events are sequenced Total Ordering All events are sequenced
The end.

Logical Clocks (Distributed computing)

  • 1.
    Logical Clocks PaulKrzyzanowski [email_address] [email_address] Distributed Systems Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
  • 2.
    Logical clocks Assignsequence numbers to messages All cooperating processes can agree on order of events vs. physical clocks : time of day Assume no central time source Each system maintains its own local clock No total ordering of events No concept of happened-when
  • 3.
    Happened-before Lamport’s “happe ned-before” notation a  b event a happened before event b e.g.: a : message being sent, b : message receipt Transitive: if a  b and b  c then a  c
  • 4.
    Logical clocks &concurrency Assign “clock” value to each event if a  b then clock( a ) < clock( b ) since time cannot run backwards If a and b occur on different processes that do not exchange messages, then neither a  b nor b  a are true These events are concurrent
  • 5.
    Event counting exampleThree systems: P 0 , P 1 , P 2 Events a , b , c , … Local event counter on each system Systems occasionally communicate
  • 6.
    Event counting examplea b h i k P 1 P 2 P 3 1 2 1 3 2 1 d f g 3 c 2 4 6 e 5 j
  • 7.
    Event counting examplea b i k j P 1 P 2 P 3 1 2 1 3 2 1 d f g 3 c 2 4 6 Bad ordering: e  h f  k h e 5
  • 8.
    Lamport’s algorithm Eachmessage carries a timestamp of the sender’s clock When a message arrives: if receiver’s clock < message timestamp set system clock to (message timestamp + 1) else do nothing Clock must be advanced between any two events in the same process
  • 9.
    Lamport’s algorithm Algorithmallows us to maintain time ordering among related events Partial ordering
  • 10.
    Event counting examplea b i k j P 1 P 2 P 3 1 2 1 7 2 1 d f g 3 c 2 4 6 6 7 h e 5
  • 11.
    Summary Algorithm needsmonotonically increasing software counter Incremented at least when events that need to be timestamped occur Each event has a Lamport timestamp attached to it For any two events, where a  b: L(a) < L(b)
  • 12.
    Problem: Identical timestampsa  b, b  c, …: local events sequenced i  c, f  d , d  g, … : Lamport imposes a send  receive relationship Concurrent events (e.g., a & i) may have the same timestamp … or not a b h i k j P 1 P 2 P 3 1 2 1 7 7 1 d f g 3 c 6 4 6 e 5
  • 13.
    Unique timestamps (totalordering) We can force each timestamp to be unique Define global logical timestamp (T i , i) T i represents local Lamport timestamp i represents process number (globally unique) E.g. (host address, process ID) 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
  • 14.
    Unique (totally ordered)timestamps a b i k j P 1 P 2 P 3 1.1 2.1 1.2 7.2 7.3 1.3 d f g 3.1 c 6.2 4.1 6.1 h e 5.1
  • 15.
    Problem: Detecting causalrelations If L(e) < L(e’) Cannot conclude that e  e’ Looking at Lamport timestamps Cannot conclude which events are causally related Solution: use a vector clock
  • 16.
    Vector clocks Rules:Vector initialized to 0 at each process V i [ j ] = 0 for i, j =1, …, N Process increments its element of the vector in local vector before timestamping event: V i [ i ] = V i [ i ] +1 Message is sent from process P i with V i attached to it When P j receives message, compares vectors 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
  • 17.
    Comparing vector timestampsDefine V = V’ iff V [ i ] = V’[ i ] for i = 1 … N V  V’ iff V [ i ]  V’[ i ] for i = 1 … N For any two events e, e’ if e  e’ then V(e) < V(e’) 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)
  • 18.
    Vector timestamps ab c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0)
  • 19.
    Vector timestamps ab c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0)
  • 20.
    Vector timestamps ab c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) (2,0,0)
  • 21.
    Vector timestamps ab c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) (2,0,0) (2,1,0)
  • 22.
    Vector timestamps ab c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) (2,0,0) (2,1,0) (2,2,0)
  • 23.
    Vector timestamps ab c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) e (0,0,1) (2,0,0) (2,1,0) (2,2,0) (0,0,1)
  • 24.
    Vector timestamps ab c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) e (0,0,1) f (2,2,2) (2,0,0) (2,1,0) (2,2,0) (0,0,1) (2,2,2)
  • 25.
    Vector timestamps (0,0,1)(1,0,0) a b c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) e (0,0,1) f (2,2,2) (2,0,0) (2,1,0) (2,2,0) (2,2,2) concurrent events
  • 26.
    Vector timestamps ab c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) e (0,0,1) f (2,2,2) (2,0,0) (2,1,0) (2,2,0) (0,0,1) (2,2,2) concurrent events
  • 27.
    Vector timestamps ab c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) e (0,0,1) f (2,2,2) (2,0,0) (2,1,0) (2,2,0) (0,0,1) (2,2,2) concurrent events
  • 28.
    Vector timestamps ab c d f e (0,0,0) P 1 P 2 P 3 (0,0,0) (0,0,0) (1,0,0) Event timestamp a (1,0,0) b (2,0,0) c (2,1,0) d (2,2,0) e (0,0,1) f (2,2,2) (2,0,0) (2,1,0) (2,2,0) (0,0,1) (2,2,2) concurrent events
  • 29.
    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 Partial Ordering Causal events are sequenced Total Ordering All events are sequenced
  • 30.