You are on page 1of 31

Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Distributed and Parallel Systems

M. T. Bennani
Assistant Professor, FST - El Manar University, LISI-INSAT

-Academic Year 2018-2019-

M. T. Bennani Assistant Professor, FST - El Manar University, LISI-INSAT


Chapter I. Logical Time
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Why Is Time Interesting?

Ordering of events: what happened first?


I Storage of data in memory, file, database, . . .
I Requests for exclusive access - who asked first?
I Interactive exchange - who answered first?
I Debugging - what could have caused the fault?

Causality is linked to temporal ordering:


I if ei causes ej , it must happen before ej

2 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Distributed System Model


We consider the following asynchronous distributed system:
I N processes pi , i = 1, ..., N
I Each process executes on a single processor
I Processors do not share memory –> processes communicate
only by message passing
I Actions of a process pi : communicating actions (Send or
Receive) or state transforming actions (such as changing the
value of a variable)

Event:
I occurrence of a single action that a process carries out as it
executes

3 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

What Do We Know About Time?


I We can’t synchronize clocks perfectly across a distributed
system
We cannot in general use physical time to find out the order
of any arbitrary pair of events occurring within a distributed
system. [Lamport, 1978]
I The sequence of events within a single process pi can be
placed in a total ordering, denoted by the relation –>i (occurs
before) between the events.
e–>i e’ if and only if the event e occurs before e’ at pi
In other words: if two events occurred at the same process pi,
then they occurred in the order in which pi observes them.
I Whenever a message is sent between two processes, the event
of sending the message occurred before the event of receiving
the message.
4 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Happened-Before Relation (− >)


Lamport’s happened-before relation − > (or causal ordering):
I HB1: If ∃ process pi : e − >i e’, then e − > e’.
I HB2: For any message m, send(m) − > receive(m)
I HB3: If e, e’, e” are events such that e − > e’ and e’ − > e”
then e − > e”.

Thus, if e and e’ are events, and if e − > e’, then we can find
a series of events e1, e2, ..., en occurring at one or more
processes such that
I e = e1 , e’ = en ,
I for i = 1, 2, ..., N-1 either HB1 or HB2 applies between ei
and ei+1 .
- Either they occur in succession at the same process, or there is a
message m such that ei = send(m) and ei+1 = receive(m).
5 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Happened-Before Relation (− >): Example

I a − > b, since the events occur in this order at process p1 (a


− >1 b).
I c − > d.
I b − > c, since these events are the sending and reception of
message m1 .
I d − > f, similarly.
I Combining these relations, we may also say that, for example,
a − > f.
6 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Happened-Before Relation (− >): Irreflexive


I Note that the − > relation is an IRREFLEXIVE PARTIAL
ORDERING on the set of all events in the distributed system.
I Irreflexivity: ¬(a− > a)
I Partial ordering: not all the events can be related by − >

- ¬(a− > e) and ¬(e− > a) since they occur at different


processes, and there is no chain of messages intervening between
them.
- We say that a and e are not ordered by − >; a and e are
concurrent (a || e).
7 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Introduction

I Each process pi keeps its own logical clock, Li , which it uses


to apply so-called Lamport timestamps to events.
I Intuition: a logical clock is a monotonically increasing
software counter, which associates a value in an ordered
domain with each event in a system.

Definition:
local logical clock Li in process pi is a function which associates a
value, Li (e), in an ordered set V with each event e in pi .
I Note that values of a logical clock need bear no particular
relationship to any physical clock.

8 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Logical Clocks Rules

I To match the definition of the ordering relation − >, we


require the following clock rules:
I CR1: If ∃ process pi such that e − >i e’, then Li (e) < Li (e 0 ).
I CR2: If a is the sending of a message by pi and b is the
receipt of the same message by pj , then Li (a) < Lj (b).
I CR3: If e, e’, e” are three events such that L(e) < L(e 0 ) and
L(e 0 ) < L(e”) then L(e) < L(e”).

9 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

In Practice
I To capture the − > relation, processes update their logical
clocks and transmit the values of their logical clocks in
messages as follows:
I LC1: Li is incremented before each event is issued at process
pi : Li := Li + 1.
I LC2: (a) When pi sends a msg m, it piggybacks on m the
value t = Li .
(b) On receiving (m, t), a process pj computes
Lj := max(Lj , t) and then applies LC1 before timestamping
the event receive(m).
I Although we increment clocks by 1, we could have chosen any
positive value.
I Clocks which follow these rules are known as LAMPORT
LOGICAL CLOCKS.
e− > e 0 ⇒ L(e) < L(e 0 )

10 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

[Lamport Clocks] Example 1

I LC1: Li is incremented before each event is issued at process


pi : Li := Li + 1.
I LC2: (a) When pi sends a msg m, it piggybacks on m the
value t = Li .
(b) On receiving (m, t), a process pj computes Lj :=
max(Lj, t) and then applies LC1 before timestamping the
event receive(m).

11 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

[Lamport Clocks] Example 2

I LC1: Li is incremented before each event is issued at process


pi : Li := Li + 1.
I LC2: (a) When pi sends a msg m, it piggybacks on m the
value t = Li .
(b) On receiving (m, t), a process pj computes Lj :=
max(Lj, t) and then applies LC1 before timestamping the
event receive(m).

12 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

[Lamport Clocks] Example 3

I LC1: Li is incremented before each event is issued at process


pi : Li := Li + 1.
I LC2: (a) When pi sends a msg m, it piggybacks on m the
value t = Li .
(b) On receiving (m, t), a process pj computes Lj :=
max(Lj, t) and then applies LC1 before timestamping the
event receive(m).

13 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

[Lamport Clocks] Example 4

I LC1: Li is incremented before each event is issued at process


pi : Li := Li + 1.
I LC2: (a) When pi sends a msg m, it piggybacks on m the
value t = Li .
(b) On receiving (m, t), a process pj computes Lj :=
max(Lj, t) and then applies LC1 before timestamping the
event receive(m).

14 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

[Lamport Clocks] Exercises

Exercise 1
By considering a chain of zero or more messages connecting events
e and e’ and using induction on the length of any sequence of
events relating e and e’, show that e − > e’ ⇒ L(e) < L(e’).

Exercise 2

15 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Shortcoming of Lamport clocks

A significant problem with Lamport clocks is that if L(e) ¡ L(e’),


then we cannot infer that e − > e’.

L(e) < L(b) but not e− >b

16 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Shortcoming of Lamport clocks

I The message arriving at time 6 in R breaks the usual rules of


causal ordering:
I Event 1 in P causes event 5 in R
I Event 1 in P causes event 6 in R
The message appears to be able to cause or caused by the event at
time 5 !!
17 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

How to overcome those problems?

I This problem arises because only a single number is used to


represent time.
I Idea: more information is needed to tell the receiving process
what the sending process kenw about the other clocks in the
system when it sent the message.
I It would then become clear that the message arriving at time
6 in R was sent before the message arriving at time 5.

18 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Mattern and Fidge Vector Clocks

I Developed to overcome the shortcoming of Lamport clocks


I Lamport clocks: e − > f then L(e) < L(f)
I Vector clocks: e − > f iff V(e) < V(f)
I Intuition: Lamport clocks try to describe global time by a
single number, which ”hides” essential information.
I Idea: processes keep information on what they know about
the other clocks in the system and use this information when
sending a message

19 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Vector Clocks
I A vector clock for a system of N processes: array of N
integers.
I Each process pi keeps its own vector clock Vi , which it uses to
timestamp local events.

I Then Vi [j] describes pi ’s KNOWLEDGE of pj ’s LOCAL


LOGICAL CLOCK.
I Example: if an event of p2 is timestamped with (1, 1, 0) then
p2 knows that the value of the logical clocks are: 1 for p1 , 1
for p2 , 0 for p3 .

20 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Note That . . .

Vi[j] (j 6= i):
I Latest clock value received by pi from process pj .
I Number of events that have occurred at pj that pi has
potentially been affected by.
I Process pj may have timestamped more events by this point,
but no information has flowed to pi about them in messages
yet!

21 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

[Vector Clocks] Implementation Rules

- VC1: Initially, Vi [j] := 0, for i, j = 1, 2, ...., N.

- VC2: Just before pi timestamps an event, it sets


Vi [i] := Vi [i] + 1.

- VC3: pi includes the value t = Vi in every message it sends.

- VC4: When pi receives a timestamp in a message, it sets


Vi [j] := max(Vi [j], t[j]) for j = 1, 2, ...., N
and then applies VC2 before timestamping the event receive(m).

22 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

[Vector Clocks] Example

23 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Ordering on Vectors

I For vector clocks using rules VC1-4, it follows that


e − > e’ ⇔ V(e) < V(e’)
I Ordering relation (≤) on vectors:
V ≤ V’ ⇔ V[j] ≤ V’[j] for j = 1, 2, ..., N
I In particular:
I V = V’ ⇔ V[j] = V’[j] for j = 1, 2, ..., N
I V < V’ ⇔ V ≤ V’ ∧ V 6= V’
I V || V’ ⇔ ¬(V < V’) ∧ ¬(V’ < V)

24 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

[Vector Clocks] Example

I V(a) < V(f), reflecting the fact that a − > f.


I c || e becauxe neither V(c) ≤ V(e) nor V(e) ≤ V(c).

25 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

[Vector Clocks] Example

26 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Elements
In a system of matrix clocks, the time is represented by a set of
nxn matrices of non-negative integers.
A process pi maintains a matrix mti [1..n, 1..n] where,
I mti [i, i] denotes the local logical clock of pi and tracks the
progress of the computation at process pi .
I mti [i, j] denotes the latest knowledge that process pi has
about the local logical clock, mtj [j, j], of process pj .
I mti [j, k] represents the knowledge that process pi has about
the latest knowledge that pj has about the local logical clock,
mtk [k, k], of pk .
I The entire matrix mti denotes pi ’s local view of the global
logical time.

27 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Rules
Process pi uses the following rules R1 and R2 to update its clock:
I R1 : Before executing an event, process pi updates its local
logical time as follows:
mti [i, i] := mti [i, i] + d / (d > 0)
I R2: Each message m is piggybacked with matrix time mt.
When pi receives such a message (m,mt) from a process pj , pi
executes the following sequence of actions:
1. Update its global logical time as follows:
(a) 1 ≤ k ≤ n : mti [i, k] := max(mti [i, k], mt[j, k])
(That is, update its row mti [i, *] with the pj ’s row in the
received timestamp, mt.)
(b) 1 ≤ k, l ≤ n : mti [k, l] := max(mti [k, l], mt[k, l])
2. Execute R1.
3. Deliver message m.

28 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Example

I We assume d = 1.
I Let us consider the following events: e which is the xi -th
event at process pi , ek1 and ek2 which are the xk1 -th and xk2 -th
events at process pk , and ej1 and ej2 which are the xj1 -th and
xj2 -th events at process pj .
I Let mte denote the matrix timestamp associated with event e.
Due to message m4 , ek2 is the last event of pk that causally
precedes e, therefore, we have mte [i, k]=mte [k, k] = xk2 .
I Likewise, mte [i, j]=mte [j, j]=xj2 . The last event of pk known
by pj , to the knowledge of pi when it executed event e, is ek1 ;
therefore, mte [j, k]=xk1 . Likewise, we have mte [k, j]=xj1 .

29 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Example

30 / 31
Introduction Logical Clocks: Time Stamp Vector Time Matrix Time

Basic properties
I Vector mti [i,.] contains all the properties of vector clocks.
I In addition, matrix clocks have the following property:
mink (mti [k, l]) ≥ t ⇒ process pi knows that every other
process pk knows that pl ’s local time has progressed till t.
I If this is true, it is cleat that process pi knows that all other
processes know that pl will never send information with a local
time ≤ t.
I In many applications, this implies that processes will no longer
require from pl certain information and can use this fact to
discard obsolete information.
I If d is always 1 in the rule R1, then mti [k, l] denotes the
number of events occurred at pl and known by pk as far as
pi ’s knowledge is concerned.

31 / 31

You might also like