Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2...

40
Chapter 5 Synchroniza tion Presenter: Maria Riaz

description

Distributed Systems – Fall 2004 – Prof. SY Lee3 Synchronization – Why we need it ! Stand-alone System –Exclusive access to shared resources Distributes System –Exclusive access to shared resources –Ordering of Events  Each node in a distributed system has separate local clock  Notion of physical time might differ among various nodes of the system

Transcript of Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2...

Page 1: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

Chapter 5Synchroniza

tion

Presenter:Maria Riaz

Page 2: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

2 Distributed Systems – Fall 2004 – Prof. SY Lee

Sequence of Presentation• Synchronization• Clock Synchronization• Logical Clocks• Global State• Election Algorithms• Mutual Exclusion• Distributed Transactions

Page 3: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

3 Distributed Systems – Fall 2004 – Prof. SY Lee

Synchronization – Why we need it !

• Stand-alone System– Exclusive access to shared resources

• Distributes System– Exclusive access to shared resources– Ordering of Events

Each node in a distributed system has separate local clock Notion of physical time might differ among various nodes

of the system

Page 4: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

4 Distributed Systems – Fall 2004 – Prof. SY Lee

- Clock Synchronization -• Problem due to different time values at

different nodes– When each machine has its own clock, an event that

occurred after another event may nevertheless be assigned an earlier time

Page 5: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

5 Distributed Systems – Fall 2004 – Prof. SY Lee

Physical Clocks (1)• Can all clocks in a distributed system be synchronized ?• If we start all clocks in the system with same initial value,

will they remain synchronized for the rest of their operations ?

• Some terminology– Skew: instantaneous difference between readings– Drift: different rates of counting time

• physical variations of underlying oscillators• variance with temperature• even extremely small differences accumulate over a large number of

oscillations– Drift Rate: difference in reading bet. a clock and a nominal

“perfect clock” per unit of time measured by the reference clock• 10-6 seconds/sec for quartz crystals• 10-7 - 10-8 seconds/sec for high precision quartz crystals

• Problem – How do we synchronize them with real-world clocks– How do we synchronize the clocks with each other

Page 6: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

6 Distributed Systems – Fall 2004 – Prof. SY Lee

Physical Clocks (2)• Some methods to measure time

– Mean solar second: measuring a large numbers of day -- taking average -- dividing by 86400

– TAI (International Atomic Time): the mean number of ticks of the cesium 133 clocks (since 1/1/1958) divided by 9,192,631,770

• Very small drift rate ~ 10-13 seconds/second– UTC: broadcast by NIST from Fort Collins,

Colorado over shortwave radio station WWV.

Page 7: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

7 Distributed Systems – Fall 2004 – Prof. SY Lee

Clock Synchronization Algorithms (1)

• The relation between clock time and UTC when clocks tick at different rates– maximum drift rate ()– every t seconds, the worst case drift between two clocks will be at most 2t– to guarantee two clocks never differ by more than , the clocks must re-

synchronize every /2 seconds

Page 8: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

8 Distributed Systems – Fall 2004 – Prof. SY Lee

Clock Synchronization Algorithms (2)

• Centralized Algorithms– Cristian’s Algorithm (1989)– Berkeley Algorithm (1989)

• Decentralized Algorithms– Averaging Algorithms (e.g. NTP)– Multiple External Time Sources

Page 9: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

9 Distributed Systems – Fall 2004 – Prof. SY Lee

Cristian’s Algorithm• Assume one machine (the time server) has a

WWV receiver and all other machines are to stay synchronized with it.– Every /2 seconds, each machine sends a message

to the time server asking for the current time.– Time server responds with message containing

current time, CUTC.• Problem

– time must never run backward

Page 10: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

10 Distributed Systems – Fall 2004 – Prof. SY Lee

Berkeley Algorithm

a) The time daemon asks all the other machines for their clock values.b) The machines answer and the time daemon computes the average.c) The time daemon tells everyone how to adjust their clock.

Page 11: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

11 Distributed Systems – Fall 2004 – Prof. SY Lee

Averaging Algorithms• At the beginning of each interval, every machine

broadcasts the current time according to its clock• Then it starts a local timer to collect all other broadcasts

that arrive during some interval S

• The simplest algorithm is just to average the values from all other machines– A slightly more sophisticated algorithm :: Discard the m

highest and m lowest to reduce the effect of a set of faulty clocks

– Another improved algorithm :: Correct each message by adding to the received time an estimate of the propagation time from the ith source

• extra probe messages are needed to use this scheme

• One of the most widely used algorithms in the Internet is the Network Time Protocol (NTP)

Page 12: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

12 Distributed Systems – Fall 2004 – Prof. SY Lee

- Logical Clocks -• Mostly ‘absolute’ time is not important rather ‘relative’ time

is of significance– Internal consistency and ordering of events– If two process don’t interact no need for synchronization

between them• A logical clock is a

– Monotonically increasing SW counters (COULOURIS)– Clocks on different computers that are somehow consistent

(LAMPORT)• Potential Requirements for logical clocks:

– Timestamps : C(a), C(b)– If a happens before b in the same process, C(a) < C(b).

• a b => C(a) < C(b)– If a and b represent the sending and receiving of a message,

respectively, C(a) < C(b).– For all distinctive events a and b, C(a) ≠ C(b).

• Two methods for assigning logical timestamps– Lamport’s Timestamps– Vector Timestamps

Page 13: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

13 Distributed Systems – Fall 2004 – Prof. SY Lee

Lamport’s Timestamps (1)• Lamport defined a relation ”happens before”. a b ‘a

happens before b’ (1978)• Each Process has local clock LCi

– with each local event e: LCi := LCi + 1; e– with each sending of a message by process Pi: LCi := LCi

+1; send (LCi,m)– with each reception of a message “(M,LCm)” by Pj: LCj :=

MAX(LCm, LCj ); LCj := LCj +1

Page 14: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

14 Distributed Systems – Fall 2004 – Prof. SY Lee

Lmaport’s Timestamps (2)• Can be used to implement ‘totally

ordered multicast’– A multicast operation by which all

messages are delivered in the same order to each receiver

Page 15: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

15 Distributed Systems – Fall 2004 – Prof. SY Lee

Vector Timestamps• Each process Pi has its own vector clock Ci

– Ci : n-dimensional vector (n: number of processes)• Notation: Ci[j] : the timestamp of the last event in Pj

by which Pi has potentially been effected

Initially:all c[i] = 0

Increment C[i]:-Events-Send msg-Receive msg

Page 16: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

16 Distributed Systems – Fall 2004 – Prof. SY Lee

- Global State -• Like a ‘distributed snapshot’ reflecting a state

in which the system might have been– represents the last event recorded for each process

• Graphically represented by a cut– Consistent : for every received message, the sender

can be identified• Cause Effect

Page 17: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

17 Distributed Systems – Fall 2004 – Prof. SY Lee

Global State (2)

Organization of a process and channels for a distributed snapshot

• Process Q receives a marker (start) for the first time and records its local state

• Q records all incoming message• Q receives a marker (end) for its incoming

channel and finishes recording the state of the incoming channel

• final recorded state

Page 18: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

18 Distributed Systems – Fall 2004 – Prof. SY Lee

- Election Algorithms -• Election algorithms

– algorithms for electing a coordinator (using this as a generic name for the special process)

– attempt to locate the process with the highest process number and designate it as coordinator

• Bully Algorithm• Ring Algorithm

• Goal– ensure that when an election starts, it

concludes with all processes agreeing on who the new coordinator is to be

Page 19: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

19 Distributed Systems – Fall 2004 – Prof. SY Lee

Bully Algorithm

• A process P detects failure of coordinator and holds an election to be the coordinator– All process with ID > P response– If P receives such a response, it will step back– Processes having ID > P can hold elections

and repeat same procedure– If no response from any process with higher ID,

election holder becomes the new coordinator• Example

a) Process 4 holds electionb) Process 5 and 6 respond, telling 4 to stopc) Now 5 and 6 each hold an electiond) Process 6 tells 5 to stope) Process 6 wins and tells everyone

Page 20: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

20 Distributed Systems – Fall 2004 – Prof. SY Lee

Ring Algorithm• Process detects failure of coordinator• Send message to neighbor with its ID• Neighbor adds its ID and pass along• When all process have added their ID, the one with highest ID

becomes the coordinator• The message is rotated once again so everyone knows

Page 21: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

21 Distributed Systems – Fall 2004 – Prof. SY Lee

- Mutual Exclusion -• To control access to a critical

section

– Centralized Algorithm– Distributed Algorithm– Token Ring Algorithm

Page 22: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

22 Distributed Systems – Fall 2004 – Prof. SY Lee

Centralized Algorithm

a) Process 1 asks the coordinator for permission to enter a critical region. Permission is granted

b) Process 2 then asks permission to enter the same critical region. The coordinator does not reply

c) When process 1 exits the critical region, it tells the coordinator, when then replies to 2

Page 23: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

23 Distributed Systems – Fall 2004 – Prof. SY Lee

Distributed Algorithm

a) Two processes want to enter the same critical region at the same moment

b) Process 0 has the lowest timestamp, so it winsc) When process 0 is done, it sends an OK also, so 2 can

now enter the critical region

Page 24: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

24 Distributed Systems – Fall 2004 – Prof. SY Lee

Token Ring Algorithma) unordered group of processes on a networkb) logical ring constructed in software

A token is passed along the ring to allow access to the critical section

Page 25: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

25 Distributed Systems – Fall 2004 – Prof. SY Lee

Comparison

Algorithm Messages per entry/exit

Delay before entry (in message times) Problems

Centralized 3 2 Coordinator crash

Distributed 2 ( n – 1 ) 2 ( n – 1 )Crash of any processGroup communication

Token ring 1 to 0 to n – 1 Lost token, process crash

A comparison of three mutual exclusion algorithms

Page 26: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

26 Distributed Systems – Fall 2004 – Prof. SY Lee

- Distributed Transactions -• Basic (Flat) Transactions &

Limitations• Alternatives

– Distributed Transactions– Nested Transactions

• Problems– Concurrency Control– Synchronization

Page 27: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

27 Distributed Systems – Fall 2004 – Prof. SY Lee

The Transaction Model (1)

Updating a master tape is fault tolerant

Page 28: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

28 Distributed Systems – Fall 2004 – Prof. SY Lee

The Transaction Model (2)

Examples of primitives for transactions

Primitive Description

BEGIN_TRANSACTION Make the start of a transaction

END_TRANSACTION Terminate the transaction and try to commit

ABORT_TRANSACTION Kill the transaction and restore the old values

READ Read data from a file, a table, or otherwise

WRITE Write data to a file, a table, or otherwise

Page 29: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

29 Distributed Systems – Fall 2004 – Prof. SY Lee

ACID - Four Characteristics• Atomic: to the outside world, the transaction

happens indivisibly

• Consistent: the transaction does not violate system invariants

• Isolated: concurrent transactions do not interfere with each other

• Durable: once a transaction commits, the changes are permanent

Page 30: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

30 Distributed Systems – Fall 2004 – Prof. SY Lee

Limitations of Flat TransactionsMain limitation: do not allow partial

results to be committed or aborted

updating all of the hyperlinks to a webpage W, which moved to a new location

BEGIN_TRANSACTION reserve WP -> JFK; reserve JFK -> Nairobi; reserve Nairobi -> Malindi;END_TRANSACTION (a)

BEGIN_TRANSACTION reserve WP -> JFK; reserve JFK -> Nairobi; reserve Nairobi -> Malindi full => ABORT_TRANSACTION (b)

Page 31: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

31 Distributed Systems – Fall 2004 – Prof. SY Lee

Classification of Transactions

a) A nested transactionb) A distributed transaction

Page 32: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

32 Distributed Systems – Fall 2004 – Prof. SY Lee

Private Workspace

a) The file index and disk blocks for a three-block file

b) The situation after a transaction has modified block 0 and appended block 3

b) After committing

Make a copy of the original workspace and perform all operation in the copied ‘private’ space before committing

read only no need for private copy

Page 33: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

33 Distributed Systems – Fall 2004 – Prof. SY Lee

Write-Ahead Log

a) A transactionb) – d) The log before each statement is executed

x = 0;y = 0;BEGIN_TRANSACTION; x = x + 1; y = y + 2 x = y * y;END_TRANSACTION; (a)

Log

[x = 0 / 1]

(b)

Log

[x = 0 / 1][y = 0/2]

(c)

Log

[x = 0 / 1][y = 0/2][x = 1/4]

(d)

Page 34: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

34 Distributed Systems – Fall 2004 – Prof. SY Lee

Concurrency Control (1)

General organization of managers for handling transactions

Page 35: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

35 Distributed Systems – Fall 2004 – Prof. SY Lee

Concurrency Control (2) General organization of managers for

handling distributed transactions

Page 36: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

36 Distributed Systems – Fall 2004 – Prof. SY Lee

Synchronization

• Two operations are serializable if the order of operations does not change the outcome i.e., the operations do not conflict– properly schedule conflicting operations (two

read operations never conflict)

• Mechanism for synchronization– Mutual Exclusion mechanisms on shared data

(i.e locking)• Two-Phase Locking• Strict Two-Phase Locking

– Explicitly ordering operations using timestamps • Pessimistic Timestamp Ordering

Page 37: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

37 Distributed Systems – Fall 2004 – Prof. SY Lee

Two-phase locking

• A transaction T is granted a lock if there is no conflict• The scheduler will never release a lock for data item x, until the data

manager acknowledges it has performed the operation for which the lock was set

• Once the scheduler has released a lock on behalf of a transaction T, it will never grant another lock on behalf of T

Page 38: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

38 Distributed Systems – Fall 2004 – Prof. SY Lee

Strict two-phase locking

• In centralized 2PL: a single site is responsible for granting and releasing locks

• In primary 2PL: each data item is assigned a primary copy• In distributed 2PL: the schedulers on each machine not only take

care that locks are granted and released, but also that the operation is forwarded to the (local) data manager

Page 39: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

39 Distributed Systems – Fall 2004 – Prof. SY Lee

Pessimistic Timestamp Ordering

Concurrency control using Timestamps

Page 40: Chapter 5 Synchronization Presenter: Maria Riaz. Distributed Systems – Fall 2004 – Prof. SY Lee2 Sequence of Presentation Synchronization Clock Synchronization.

40 Distributed Systems – Fall 2004 – Prof. SY Lee

Thank you !

Questions ?