logical time in distributed systems - github pages€¦ · logical clock • the logical clock,...

28
Logical Time in Distributed Systems Sistemi di Calcolo (II semestre) Roberto Baldoni

Upload: others

Post on 12-Oct-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Logical Time in Distributed

Systems

Sistemi di Calcolo (II semestre) – Roberto Baldoni

Page 2: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Logical clock

• Physical clock synchronization algorithms try to

coordinate distributed clocks to reach a common value

– Based on the estimation of transmission times

• It can be hard to find a good estimation.

– In several applications it is not important when things

happened but in which order they happened

• Reliable way of ordering events is required!

Page 3: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Notes:

1. Two events occurred at some process pi happened in the same

order as pi observes them

2. When pi sends a message to pj the send event happens before

the receive event

Lamport introduced the relation that captures the causal

dependencies between events (causal order relation)

We denote with i the ordering relation between events in a process pi

We denote with the happened-before relation between any pair of

events

Page 4: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Happened-Before Relation: Definition

• Two events e and e’ are related by happened-before relation (e e’) if:

– pi | e i e’

e e’ pi

pj

Page 5: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Happened-Before Relation: Definition

• Two events e and e’ are related by happened-before relation (e e’) if:

– pi | e i e’

– message m send(m) receive(m)

• send(m) is the event of sending a message m

• receive(m) is the event of receipt of the same message m

e pi

e’ m

pj

Page 6: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Happened-Before Relation: Definition

• Two events e and e’ are related by happened-before relation (e e’) if:

– pi | e i e’

– message m send(m) receive(m)

• send(m) is the event of sending a message m

• receive(m) is the event of receipt of the same message m

– e, e’, e’’ | (e e’’) (e’’ e’) (happened-before relation is transitive)

e pi

e’ m

e’’ e’ pj

Page 7: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Happened-Before Relation

• Using these three rules it is possible to define a causal-ordered sequence of events e1, e2, … , en

• Notes:

– The sequence e1, e2, …, en may not be unique

– It may exists a pair of events <e1,e2> such that e1 and e2 are not in happened-before relation

– If e1 and e2 are not in happened-before relation then they are concurrent (e1||e2)

– For any two events e1 and e2 in a distributed system, either

• e1 e2

• e2 e1

• e1||e2

Page 8: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

happened-before: example

p3

p2

p1

e11 e2

1 e31

e12 e2

2 e32

e13 e2

3 e33

eji is j-th event of

process pi

S1 = < e11, e

12, e

22, e

23, e

33, e

31, e

41, e

51, e

42 >

e41

e43

S2 = < e13, e

21, e

31, e

41, e

53 >

e51

e53

Note: e13 and e

12 are concurrent

e42

Page 9: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Logical Clock

• The Logical Clock, introduced by Lamport, is a software counting register

monotonically increasing its value

– Logical clock is not related to physical clock

• Each process pi employs its logical clock Li to apply a timestamp to events

• Li(e) is the “logical” timestamp assigned, using the logical clock, by a

process pi to event e

• Property:

– If e e’ then L(e) < L(e’)

• Observation:

– The ordering relation obtained through logical timestamps is only a

partial order. Consequently, timestamps could not be sufficient to

relate two events

Page 10: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Scalar Logical Clock: an implementation

• Each process pi initializes its logical clock Li=0 ( i = 1….N)

• pi increases Li of 1 when it generates an event (either send or

receive)

– Li = Li + 1

• When pi sends a message m

– creates an event send(m)

– increases Li

– timestamps m with t = Li

• When pi receives a message m with timestamp t

– Updates its logical clock Li = max(t, Li)

– Produces an event receive(m)

– Increases Li

Page 11: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Scalar Logical Clock: example

p3

p2

p1

e11 e2

1 e31

e12 e2

2 e32

e13 e2

3 e33

e41

e43

e51

e53

L1=0

L2=0

L3=0

L1=1

L3=1

L2=2

L3=4

L2=3

L3=5 L3=6

L1=6 L1=2

e42

L1=7 L1=8

L3=8

L2=7 L2=9

eji is j-th event of process pi

Li is the logical clock of pi

Note:

e11 e2

1 and timestamps reflect this property

e11 || e

13 and respective timestamps have the same value

e12 || e

13 but respective timestamps have different values

Page 12: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Limits of Scalar Logical Clock

• Scalar logical clock can guarantee the following property

– IF e e’ then L(e) < L(e’)

• But it is not possible to guarantee

– IF L(e) < L(e’) then e e’

• Consequently:

– It is not possible to determine, by analysing only scalar clocks, if two

events are concurrent or correlated by the happened-before relation

• Mattern [1989] and Fridge [1991] proposed an improved version of logical

clock where events are time-stamped with local logical clock and node

identifier

– Vector Clock

Page 13: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Logical Time and

Ricart-Agrawala Mutual Exclusion Algorithm

Page 14: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Logical clock in distributed algorithms

Scalar Clock can be used to solve

Lamport’s Mutual Exclusion problem

in a distributed setting

Page 15: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Ricart-Agrawala’s algorithm:

implementation (see also lecture notes)

• Local variables

– #replies (initially 0)

– State {Requesting, CS, NCS} (initially NCS)

– Q pending requests queue (initially empty)

– Last_Req (initially MAX_INT)

– Num (initially 0)

• Algorithm:

begin 1. State = Requesting

2. Num = Num + 1; Last_Req = num

3.i = 1…N, send REQUEST to pi

4. Wait until #replies == N - 1

5. State = CS

6. CS

7.rQ, send REPLY to r

8. Q = ; State = NCS; #replies = 0;

Last_Req = MAX_INT

Upon receipt REQUEST(t) from pj

1. Num = max(t, Num)

2. If State == CS or (State == Requesting and {Last_Req,i} < {t,j})

3. Then insert in Q{t, j}

4. Else send REPLY to pj

Upon receipt of REPLY from pj

1.#replies = #replies + 1

Page 16: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

NCS

NCS

NCS

P2 requires the

CS

R

Num = 1

Last_Req = Num = 1 1

1

P1

P2

P3

Ricart-Agrawala’s algorithm: example

Page 17: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

P3 receives the

request of P2

R

Num = 1

Last_Req = Num = 1 1

1

Num = max(t, Num) =

= max(1, 0) = 1

P1

P2

P3

Reply

NCS

NCS

NCS

Ricart-Agrawala’s algorithm: example

Page 18: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

Also P1 tries to

access the CS

R

Num = 1

Last_Req = Num = 1 1

1 Reply

R

Num = 1

Last_Req = Num = 1

P1

P2

P3

1

1

NCS

NCS

NCS

Num = 1

Ricart-Agrawala’s algorithm: example

Page 19: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

P1 receives the

request of P2

R

Num = 1

Last_Req = Num = 1 1

1

Num = 1

R

Num = 1

Last_Req = Num = 1

1

1

Ricart-Agrawala’s algorithm: example

Reply

NCS

NCS

NCS

P1

P2

P3

Page 20: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

R

Num = 1

Last_Req = Num = 1 1

1

Num = 1

R

Num = 1

Last_Req = Num = 1

1

1

{Last_Req , i} < {t, j}?

{1, 1} < {1, 2}? YES

Reply

P1

P2

P3

NCS

NCS

NCS

Ricart-Agrawala’s algorithm: example

Page 21: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

R

Num = 1

Last_Req = Num = 1 1

1

Num = 1

R

Num = 1

Last_Req = Num = 1

1

1

1,2

Num = max(t, Num) =

= max(1, 1) = 1

P1

P2

P3

Reply

NCS

NCS

NCS

Ricart-Agrawala’s algorithm: example

Page 22: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

P3 receives the

request of P1

R

Num = 1

Last_Req = Num = 1 1

1

Num = 1

R

Num = 1

Last_Req = Num = 1

1

1

1,2 Q

Reply

Num = max(t, Num) =

= max(1, 1) = 1

Ricart-Agrawala’s algorithm: example

P1

P2

P3

Reply

NCS

NCS

NCS

Page 23: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

P2 receives the

request of P1

R

Num = 1

Last_Req = 1

1

1

Num = 1

Reply

R

Num = 1

Last_Req = 1

1

1

1,2 Q

Reply

Num = 1

{Last_Req , i} < {t, j}?

{1, 2} < {1, 1}? NO

P1

P2

P3

NCS

NCS

NCS

Ricart-Agrawala’s algorithm: example

Page 24: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

P2 receives the

request of P1

R 1

1

Num = 1

Reply

R

Num = 1

Last_Req = 1

1

1

1,2 Q

Reply

Num = 1

Reply

P1

P2

P3

NCS

NCS

NCS

Num = 1

Last_Req = 1

Ricart-Agrawala’s algorithm: example

Page 25: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

P2 receives the

Reply sent by P3

R

Num = 1

Last_Req = 1

1

1

Num = 1

Reply

R

Num = 1

Last_Req = 1

1

1

1,2 Q

Reply

Num = 1

#replies = 1

P1

P2

P3

NCS

NCS

NCS

Reply

Ricart-Agrawala’s algorithm: example

Page 26: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

P1 receives the

Reply sent by P2

R 1

1

Num = 1

Reply

R

Num = 1

Last_Req = 1

1

1

1,2 Q

Reply

Num = 1

#replies = 1

P1

P2

P3

NCS

NCS

NCS

Reply

Num = 1

Last_Req = 1

#replies = 1

Ricart-Agrawala’s algorithm: example

Page 27: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

P1 receives the

Reply sent from P3

R 1

1

Num = 1

Reply

R

Num = 1

Last_Req = 1

1

1

1,2 Q

Reply

Num = 1

#replies = 2

CS CS

P1

P2

P3

NCS

NCS

NCS

Num = 1

Last_Req = 1 Reply

#replies = 1

Ricart-Agrawala’s algorithm: example

Page 28: Logical Time in Distributed Systems - GitHub Pages€¦ · Logical Clock • The Logical Clock, introduced by Lamport, is a software counting register monotonically increasing its

P2 receives the

second Reply and

accesses CS

Reply

Num = 1

Reply

#replies = 1

#replies = 0 CS CS NCS

Reply

Q

#replies = 0 CS CS NCS

#replies = 2

P1

P2

P3

Ricart-Agrawala’s algorithm: example