Logical Clocks
                               Dani Kim (dani.kim@geekple.com)




Tuesday,	 September	 20,
Why use it?



                      • In a distributed system, used to determine
                             the order of events




Tuesday,	 September	 20,
Lamport timestamps 1/2


                      • will not be perfectly synchronized
                       • but, simple algorithm
                       • minimal overhead
                      • each process P has its own clock C


Tuesday,	 September	 20,
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) + 1




Tuesday,	 September	 20,
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->b




Tuesday,	 September	 20,
Concurrent (||)



                      • “not (a->b)” and “not (b->a)”
                       • these events are concurrent
                       • a||b



Tuesday,	 September	 20,
Example




                                                    1
                                           1
                                  1

                             P1       P2       P3       P4

Tuesday,	 September	 20,
Example




                                                   4
                                                            3
                                                   2
                                      2

                                                                1
                                               1
                                  1

                             P1           P2           P3           P4

Tuesday,	 September	 20,
Example



                                  5




                                                   4
                                                            3
                                                   2
                                      2

                                                                1
                                               1
                                  1

                             P1           P2           P3           P4

Tuesday,	 September	 20,
Example



                                  5   2



                                  1

                                                   4
                                                            3
                                                   2


                                                                1
                                               1


                             P1           P2           P3           P4

Tuesday,	 September	 20,
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,
Vector Clocks 1/2



                      • more powerful of Lamport timestamp
                      • use several clocks instead of a single
                             clock




Tuesday,	 September	 20,
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_clocks

Tuesday,	 September	 20,
Example




                                                                [0,0,0,1]
                                               [0,1,0,0]
                              [1,0,0,0]

                             P1           P2               P3               P4

Tuesday,	 September	 20,
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                           P4

Tuesday,	 September	 20,
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                           P4

Tuesday,	 September	 20,
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                           P4

Tuesday,	 September	 20,
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 false




Tuesday,	 September	 20,
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,
Conclusion



                      • They do not resolve conflicts by
                             themselves




Tuesday,	 September	 20,
References



                      •      http://en.wikipedia.org/wiki/Vector_clock

                      •      http://en.wikipedia.org/wiki/Logical_clock




Tuesday,	 September	 20,

Logical clocks

  • 1.
    Logical Clocks Dani Kim (dani.kim@geekple.com) Tuesday, September 20,
  • 2.
    Why use it? • In a distributed system, used to determine the order of events Tuesday, September 20,
  • 3.
    Lamport timestamps 1/2 • will not be perfectly synchronized • but, simple algorithm • minimal overhead • each process P has its own clock C Tuesday, September 20,
  • 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) + 1 Tuesday, September 20,
  • 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->b Tuesday, September 20,
  • 6.
    Concurrent (||) • “not (a->b)” and “not (b->a)” • these events are concurrent • a||b Tuesday, September 20,
  • 7.
    Example 1 1 1 P1 P2 P3 P4 Tuesday, September 20,
  • 8.
    Example 4 3 2 2 1 1 1 P1 P2 P3 P4 Tuesday, September 20,
  • 9.
    Example 5 4 3 2 2 1 1 1 P1 P2 P3 P4 Tuesday, September 20,
  • 10.
    Example 5 2 1 4 3 2 1 1 P1 P2 P3 P4 Tuesday, September 20,
  • 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.
    Vector Clocks 1/2 • more powerful of Lamport timestamp • use several clocks instead of a single clock Tuesday, September 20,
  • 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_clocks Tuesday, September 20,
  • 14.
    Example [0,0,0,1] [0,1,0,0] [1,0,0,0] P1 P2 P3 P4 Tuesday, September 20,
  • 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 P4 Tuesday, September 20,
  • 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 P4 Tuesday, September 20,
  • 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 P4 Tuesday, September 20,
  • 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 false Tuesday, September 20,
  • 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.
    Conclusion • They do not resolve conflicts by themselves Tuesday, September 20,
  • 21.
    References • http://en.wikipedia.org/wiki/Vector_clock • http://en.wikipedia.org/wiki/Logical_clock Tuesday, September 20,