5 3 Logical clocks 20201122 1 Lamports logical

  • Slides: 22
Download presentation
5. 3 Logical clocks 2020/11/22 1

5. 3 Logical clocks 2020/11/22 1

Lamport’s logical clock Time P Q R 2020/11/22 p 1 1 1 q 1

Lamport’s logical clock Time P Q R 2020/11/22 p 1 1 1 q 1 1 r 1 p 2 2 q 2 2 2 r 2 p 3 p 4 45 3 q 3 3 q 4 4 3 r 3 4 q 5 5 r 4 2

Clock Ci maintained by process Pi 2020/11/22 3

Clock Ci maintained by process Pi 2020/11/22 3

“Happened-before” relation (review) n a b defined by: If a occurs earlier than b

“Happened-before” relation (review) n a b defined by: If a occurs earlier than b in same process, then a b. u If a is sending event and b is receiving event of same message, then a b. u If a b and b c, then a c. [Transitive] u n n If a b, then a happened before b a and b are concurrent if neither a b nor b a. 2020/11/22 4

Clock condition: 2020/11/22 If a b then C(a) < C(b) 5

Clock condition: 2020/11/22 If a b then C(a) < C(b) 5

Comments 2020/11/22 6

Comments 2020/11/22 6

Causality Violation Suppose M 1’s sending event happened before M 3’s sending event. n

Causality Violation Suppose M 1’s sending event happened before M 3’s sending event. n Causality violation occurs if M 1 is delivered after M 3 (In particular, non-FIFO delivery is causality violation). n Migrate foo to P 2 P 1 P 2 “ P 3 foo e v a h u o Do y “Given to P 2 M 1 Time 2020/11/22 ? ” (=M 2)” M 3)” = ( ? o ave fo h u o y o “D “Nope” 7

Ordering options 2020/11/22 8

Ordering options 2020/11/22 8

Total ordering vs. causal ordering Totally ordered deliveries. Causally ordered deliveries. 2020/11/22 9

Total ordering vs. causal ordering Totally ordered deliveries. Causally ordered deliveries. 2020/11/22 9

Delivery mechanism 2020/11/22 10

Delivery mechanism 2020/11/22 10

Hold-back Q and delivery Q Communication subsystem messages 2020/11/22 11

Hold-back Q and delivery Q Communication subsystem messages 2020/11/22 11

How can we prevent causality violation? Q 1 Q 2. Qn Head 2020/11/22 12

How can we prevent causality violation? Q 1 Q 2. Qn Head 2020/11/22 12

Problems Vector clock condition: 2020/11/22 a b iff V(a) < V(b) 13

Problems Vector clock condition: 2020/11/22 a b iff V(a) < V(b) 13

Event counter P 1 P 2 P 3 1 1 2 Event counter at

Event counter P 1 P 2 P 3 1 1 2 Event counter at Pi : Initialized at 0 and incremented for each event 1 2 2 3 3 4 2020/11/22 14

Vector clock P 1 P 2 P 3 V 2 at t 2 =

Vector clock P 1 P 2 P 3 V 2 at t 2 = ( t 1, t 2, t 3 ) t 1, t 2, t 3 are latest event counts at P 1, P 2 , P 3 that are reachable backwards from there (0 if non-reachable): (2, 4, 2) in the example 2020/11/22 t 1 t 3 t 2 15

Updating vector clock P 1 P 3 0 V 2 at t 2 =

Updating vector clock P 1 P 3 0 V 2 at t 2 = [ t 1, t 2, t 3 ] V 3 at s 3 = [s 1, 0 , s 3 ] max P 2 s 1 t 3 max s 3 V 2 at r 2 = ( t 1, t 2+1, s 3 ) Local event count M is timestamped by V 3 2020/11/22 t 2 M r 2 16

Vector logical clock at Pi n n Pi initializes its vector clock Vi[ ]

Vector logical clock at Pi n n Pi initializes its vector clock Vi[ ] = [0, …, 0]. For each event of Pi (self-component) u n n Vi[i]++ When sending msg, Pi attaches vector Vi[ ] as timestamp (after incrementing) Upon receiving msg with timestamp, ts=Vj[ ], Pi updates Vi[ ] by Vi[i]++ u V [k] = Max { V [k], V [k] } for all k != i. i i j u 2020/11/22 17

Example a P 1 P 2 000 100 010 P 3 000 001 200

Example a P 1 P 2 000 100 010 P 3 000 001 200 220 300 230 240 250 450 243 260 550 2020/11/22 242 264 273 18

Example c P 1 P 2 000 100 For fixed event e. x e

Example c P 1 P 2 000 100 For fixed event e. x e iff V(x) < V(e) 000 x 010 001 200 220 300 230 240 250 450 550 242 243 260 2020/11/22 P 3 264 273 e 19

Example b P 1 P 2 000 100 For fixed event e. 200 e

Example b P 1 P 2 000 100 For fixed event e. 200 e x iff V(a) < V(x) 300 010 450 242 243 260 x 2020/11/22 000 001 220 230 e 240 250 550 P 3 264 273 20

Notation n n We define global V(e) = Vi(e) if event e happens in

Notation n n We define global V(e) = Vi(e) if event e happens in Pi We write V(a) V(b) if u V(a)[k] V(b)[k] for all k. u n Here V(a)[k] denotes the kth component of V(a). We write V(a) < V(b) if u V(a)[k] V(b)[k] for all k, and u 2020/11/22 V(a)[j] < V(b)[j] for at least one j 21

a b iff V(a) < V(b) P 1 P 2 P 3 Proof: For

a b iff V(a) < V(b) P 1 P 2 P 3 Proof: For a fixed b, a b iff a is in shaded area iff each component of V(a) corresponding component of V(b). a t 3 t 1 For formal proof see lecture notes. b 2020/11/22 t 2 22