Logical clocks

2,233 views
2,046 views

Published on

lamport timestamps, vector clocks

Published in: Business, Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,233
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
61
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Logical clocks

  1. 1. Logical Clocks Dani Kim (dani.kim@geekple.com)Tuesday, September 20,
  2. 2. Why use it? • In a distributed system, used to determine the order of eventsTuesday, September 20,
  3. 3. Lamport timestamps 1/2 • will not be perfectly synchronized • but, simple algorithm • minimal overhead • each process P has its own clock CTuesday, September 20,
  4. 4. Lamport timestamps 2/2 • Increments counter before each event • When a process sends a message, it includes its counter with the message • On receiving a message, receiver’s counter = max(received counter, own counter) + 1Tuesday, September 20,
  5. 5. Happend-Before (->) • If ‘a’ and ‘b’ occur on same process, a->b if event ‘a’ occur before ‘b’ • if ‘a’ is the sending of a message and ‘b’ is the reception of the message sent in event ‘a’, a->bTuesday, September 20,
  6. 6. Concurrent (||) • “not (a->b)” and “not (b->a)” • these events are concurrent • a||bTuesday, September 20,
  7. 7. Example 1 1 1 P1 P2 P3 P4Tuesday, September 20,
  8. 8. Example 4 3 2 2 1 1 1 P1 P2 P3 P4Tuesday, September 20,
  9. 9. Example 5 4 3 2 2 1 1 1 P1 P2 P3 P4Tuesday, September 20,
  10. 10. Example 5 2 1 4 3 2 1 1 P1 P2 P3 P4Tuesday, September 20,
  11. 11. Features • If a->b, then C(a) < C(b) • But C(a) < C(b) does not imply a->b • Just C(a) <= C(b) implies “not (b->a)” • “not (b->a)” is equivalent a->b or a||b • (P1, 1) <= (P3, 3), (P1, 1) || (P3, 3)Tuesday, September 20,
  12. 12. Vector Clocks 1/2 • more powerful of Lamport timestamp • use several clocks instead of a single clockTuesday, September 20,
  13. 13. Vector Clocks 2/2 • Each time a process experiences an internal event, it increments its own logical clock in the vector by one • Each time a process prepares to send a message, it increments its own logical clock in the vector by one and then send its entire vector along with the message being sent • Each time a process receives a message, it increments its own logical clock in the vector by one and updates each element in its vector by taking the maximum of the value in its own vector clock and the value in the vector in the received message • http://en.wikipedia.org/wiki/Vector_clocksTuesday, September 20,
  14. 14. Example [0,0,0,1] [0,1,0,0] [1,0,0,0] P1 P2 P3 P4Tuesday, September 20,
  15. 15. Example [0,1,3,1] [0,1,2,1] [0,1,1,0] [1,2,0,0] [0,0,0,1] [0,1,0,0] [1,0,0,0] P1 P2 P3 P4Tuesday, September 20,
  16. 16. Example [2,1,3,1] [0,1,3,1] [0,1,2,1] [0,1,1,0] [1,2,0,0] [0,0,0,1] [0,1,0,0] [1,0,0,0] P1 P2 P3 P4Tuesday, September 20,
  17. 17. Example [2,1,3,1] [1,2,0,0] [1,0,0,0] [0,1,3,1] [0,1,2,1] [0,1,1,0] [0,0,0,1] [0,1,0,0] P1 P2 P3 P4Tuesday, September 20,
  18. 18. Pseudo code function is_concurrent(own[], other[]) greater=false, less=false FOR i = 0 to numberOfElements by 1 IF own[i] > other[i] THEN greater = true ELSE own[i] < other[i] less = true IF greater AND less THEN RETURN true ELSE RETURN falseTuesday, September 20,
  19. 19. Features • if x->y, then VC(x) < VC(y) • if VC(x) < VC(y), then x->y • if VC(x) < VC(y), then “not (y->x)”Tuesday, September 20,
  20. 20. Conclusion • They do not resolve conflicts by themselvesTuesday, September 20,
  21. 21. References • http://en.wikipedia.org/wiki/Vector_clock • http://en.wikipedia.org/wiki/Logical_clockTuesday, September 20,

×