Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion...

39
Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions

Transcript of Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion...

Page 1: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Synchronization

Chapter 6

• Part I Clock Synchronization & Logical clocks• Part II Mutual Exclusion• Part III Election Algorithms• Part IV Transactions

Page 2: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

– Most of the lecture notes are based on slides by Prof. Jalal Y. Kawash at Univ. of Calgary

– Some slides are from Prof. Steve Goddard at University Nebraska, Lincoln, Prof. Harandi, Prof. Hou, Prof. Gupta and Prof. Vaidya from University of Illinois, Prof. Kulkarni from Princeton University

– I have modified them and added new slides

Giving credit where credit is due:

CSCE455/855 Distributed Operating Systems

Page 3: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Motivation

• Synchronization is important if we want to– control access to a single, shared resource

– agree on the ordering of events

• Synchronization in Distributed Systems is much more difficult than in uniprocessor systems

We will study:1. Synchronization based on “Actual Time”.

2. Synchronization based on “Relative Time”.

3. Synchronization based on Co-ordination (with Election Algorithms).

4. Distributed Mutual Exclusion.

5. Distributed Transactions.

Page 4: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

SynchronizationChapter 6

Part IClock Synchronization

& Logical clocks

Page 5: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Lack of Global Time in DS

• It is impossible to guarantee that physical clocks run at the same frequency

• Lack of global time, can cause problems

• Example: UNIX make– Edit output.c at a client

– output.o is at a server (compile at server)– Client machine clock can be lagging behind

the server machine clock

Page 6: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Lack of Global Time – Example

When each machine has its own clock, an event that occurred after another event may nevertheless be assigned an earlier time.

Page 7: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

A Note on Real Time

• Universal Coordinated Time (UTC): basis of all modern civil timekeeping

• Radio stations can broadcast UTC for receivers to collect it

– WWV SW radio station in Colorado

Page 8: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Physical Clock Synchronization (1)

• External: synchronize with an external resource, UTC source

|S(t) – Ci(t)| < D

• Internal: synchronize without access to an external resource

|Ci(t) – Cj(t)| < D

Page 9: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Cristian’s Algorithm – External Synch

• External source S• Denote clock value at process X by C(X)• Periodically, a process P:

1. send message to S, requesting time

2. Receive message from S, containing time C(S)

3. Adjust C at P, should we set C(P) = C(S)?

• Reply takes time• When P adjusts C(P) to C(S), C(S) > C(P)

Page 10: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Cristian’s Algorithm – External Synch

• How to estimate machine A’s time offset relative to B?

B

A

T2

T1

T3

T4

Page 11: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Global Positioning System

• Computing a position in a two-dimensional space

Page 12: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Berkeley Algorithm – Internal Synch• Internal: synchronize without access to an

external resource

|Ci(t) – Cj(t)| < D

Periodically, S: send C(S) to each client P

P: calculate P = C(P) – C(S)

send P to S

S: receive all P’s compute an average

send -P to client P

P: apply -P to C(P)

Page 13: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

The Berkeley Algorithm

a) The time daemon asks all the other machines for their clock values

b) The machines answer

c) The time daemon tells everyone how to adjust their clock

•Propagation time?•Time server fails?

Page 14: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Importance of Synchronized Clocks

• New H/W and S/W for synchronizing clocks is easily available

• Nowadays, it is possible to keep millions of clocks synchronized to within few milliseconds of UTC

• New algorithms can benefit

Page 15: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Event Ordering in Centralized Systems

• A process makes a kernel call to get the time• Process which tries to get the time later will always get a higher (or equal) time value

no ambiguity in the order of events and their time

Page 16: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Logical Clocks

• For many DS algorithms, associating an event to an absolute real time is not essential, we only need to know an unambiguous order of events

• Lamport's timestamps• Vector timestamps

Page 17: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Logical Clocks (Cont.)

• Synchronization based on “relative time”.– Example: Unix make (Is output.c updated after the generation of

output.o?)

• “relative time” may not relate to the “real time”.

• What’s important is that the processes in the Distributed System agree on the ordering in which certain events occur.

• Such “clocks” are referred to as Logical Clocks.

Page 18: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Example: Why Order Matters?

– Replicated accounts in New York(NY) and San Francisco(SF)– Two updates occur at the same time

• Current balance: $1,000• Update1: Add $100 at SF; Update2: Add interest of 1% at NY• Whoops, inconsistent states!

Page 19: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Lamport Algorithm

• Clock synchronization does not have to be exact– Synchronization not needed if there is no interaction

between machines– Synchronization only needed when machines

communicate– i.e. must only agree on ordering of interacting events

Page 20: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Lamport’s “Happens-Before” Partial Order

• Given two events e & e’, e < e’ if:

1. Same process: e <i e’, for some process Pi

2. Same message: e = send(m) and e’=receive(m) for some message m

3. Transitivity: there is an event e* such that e < e* and e* < e’

Page 21: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Concurrent Events

• Given two events e & e’:

• If neither e < e’ nor e’< e, then e || e’

P1

P2

P3

Real Time

a b

c

f

d

e

m1

m2

Page 22: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Lamport Logical Clocks

• Substitute synchronized clocks with a global ordering of events– ei < ej LC(ei) < LC(ej)

– LCi is a local clock: contains increasing values

• each process i has own LCi

– Increment LCi on each event occurrence

– within same process i, if ej occurs before ek

• LCi(ej) < LCi(ek)

– If es is a send event and er receives that send, then

• LCi(es) < LCj(er)

Page 23: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Lamport Algorithm

• Each process increments local clock between any two successive events

• Message contains a timestamp

• Upon receiving a message, if received timestamp is ahead, receiver fast forward its clock to be one more than sending time

Page 24: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Lamport Algorithm (cont.)

• Timestamp– Each event is given a timestamp t

– If es is a send message m from pi, then t=LCi(es)

– When pj receives m, set LCj value as follows

• If t < LCj, increment LCj by one– Message regarded as next event on j

• If t ≥ LCj, set LCj to t+1

Page 25: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Lamport’s Algorithm Analysis (1)

• Claim: ei < ej LC(ei) < LC(ej)

• Proof: by induction on the length of the sequence of events relating to ei and ej

P1

P2

P3

Real Timea b

c

f

d

e

m1

m2

1 2

3

5

4

1

g

5

Page 26: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Lamport’s Algorithm Analysis (2)

• LC(ei) < LC(ej) ei < ej ?

• Claim: if LC(ei) < LC(ej), then it is not necessarily true that ei < ej

P1

P2

P3

Real Timea b

c

f

d

e

m1

m2

1 2

3

5

4

1

g

2

Page 27: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Total Ordering of Events

• Happens before is only a partial order

• Make the timestamp of an event e of process Pi be:

(LC(e),i)

• (a,b) < (c,d) iff a < c, or a = c and b < d

Page 28: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Application: Totally-Ordered Multicasting

– Message is timestamped with sender’s logical time

– Message is multicast (including sender itself)– When message is received

• It is put into local queue

• Ordered according to timestamp

• Multicast acknowledgement

– Message is delivered to applications only when• It is at head of queue

• It has been acknowledged by all involved processes

Page 29: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Application: Totally-Ordered Multicasting

• Update 1 is time-stamped and multicast. Added to local queues. • Update 2 is time-stamped and multicast. Added to local queues.

• Acknowledgements for Update 2 sent/received. Update 2 can now be processed.• Acknowledgements for Update 1 sent/received. Update 1 can now be processed.

• (Note: all queues are the same, as the timestamps have been used to ensure the “happens-before” relation holds.)

Page 30: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Limitation of Lamport’s Algorithm

• ei < ej LC(ei) < LC(ej)• However, LC(ei) < LC(ej) does not imply ei < ej

– for instance, (1,1) < (1,3), but events a and e are concurrent

P1

P2

P3

Real Timea b

c

f

d

e

m1

m2

(1,1) (2,1)

(3,2)

(5,3)

(4,2)

(1,3)

g

(2,3)

Page 31: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Vector Timestamps

• Pi’s clock is a vector VTi[]

• VTi[i] = number of events Pi has stamped

• VTi[j] = what Pi thinks number of events Pj has stamped (i j)

Page 32: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Vector Timestamps (cont.)

• Initialization– the vector timestamp for each process is

initialized to (0,0,…,0)

• Local event– when an event occurs on process Pi, VTi[i]

VTi[i] + 1• e.g., on processor 3, (1,2,1,3) (1,2,2,3)

Page 33: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

• Message passing– when Pi sends a message to Pj, the message has

timestamp t[]=VTi[]

– when Pj receives the message, it sets VTj[k] to max (VTj[k],t[k]), for k = 1, 2, …, N

• e.g., P2 receives a message with timestamp (3,2,4) and P2’s timestamp is (3,4,3), then P2 adjusts its timestamp to (3,4,4)

Vector Timestamps (cont.)

Page 34: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Comparing Vectors

• VT1 = VT2 iff VT1[i] = VT2[i] for all i

• VT1 VT2 iff VT1[i] VT2[i] for all i

• VT1 < VT2 iff VT1 VT2 & VT1 VT2

– for instance, (1, 2, 2) < (1, 3, 2)

Page 35: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Vector Timestamp Analysis

• Claim: e < e’ iff e.VT < e’.VT

P1

P2

P3

Real Timea b

c

f

d

e

m1

m2

[1,0,0]

[2,0,0]

[2,1,0]

[2,2,3]

[2,2,0]

[0,0,1]

g

[0,0,2]

Page 36: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Application: Causally-Ordered Multicasting

• For ordered delivery of related messages

– Vi[i] is only incremented when sending

– When k gets a msg from j, with timestamp ts, the msg is buffered until:

• 1: ts[j] = Vk[j] + 1 – (timestamp indicates this is the next msg that k is expecting

from j)

• 2: ts[i] ≤ Vk[i] for all i ≠ j – (k has seen all msgs that were seen by j when j sent the msg)

Page 37: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Causally-Ordered MulticastingP2

a

P1

c

d

P3

e

g

[1,0,0]

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

[1,0,1]

[1,0,1]

Post a

r: Reply a

Two messages: message a from P1; message r from P3 replying to message a; Scenario1: at P2, message a arrives before the reply r

b

[1,0,1]

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

Page 38: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Causally-Ordered Multicasting (cont.)P2

a

P1 P3

d

g

[1,0,0]

[1,0,0]

[1,0,1]

Post a

r: Reply a

Buffered

c[1,0,0]

Scenario2: at P2, message a arrives after the reply r. The reply is not delivered right away.

b

[1,0,1]

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

Deliver r

Page 39: Synchronization Chapter 6 Part I Clock Synchronization & Logical clocks Part II Mutual Exclusion Part III Election Algorithms Part IV Transactions.

Ordered Communication

• Totally ordered multicast– Use Lamport timestamps

• Causally ordered multicast– Use vector timestamps