Synchronization

45
1 Synchronization Clock Synchronization and algorithm

description

Synchronization. Clock Synchronization and algorithm. Synchronization. Concentrate on how process can synchronize Not simultaneously access a shared resources. Multiple process can agree on the ordering of event/access the shared resources; - PowerPoint PPT Presentation

Transcript of Synchronization

Page 1: Synchronization

1

Synchronization

Clock Synchronization and algorithm

Page 2: Synchronization

2

Synchronization Concentrate on how process can synchronize

– Not simultaneously access a shared resources.– Multiple process can agree on the ordering of event/access the shared resources;

• E.g: process p1 should send message m1 prior to pcs p2 and message m2

Synchronization in DS is much more difficult rather that in uniprocessor/multiprocessor system

Page 3: Synchronization

3

Use of time in distributed systems:

Page 4: Synchronization

4

Page 5: Synchronization

5

Clock Synchronizationmake example

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

Page 6: Synchronization

6

Clock Synchronization Algorithms Centralized Algorithms

Cristian’s Algorithm (1989)Berkeley Algorithm (1989)

Decentralized AlgorithmsAveraging Algorithms (e.g. NTP)Multiple External Time Sources

Page 7: Synchronization

7

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

a WWV receiver and all other machines areto stay synchronized with it.

Every specificseconds, each machine sends a message to the time server asking for the current time.

Time server responds with message containing current time, CUTC.

Page 8: Synchronization

8

Cristian's Algorithm Getting the current time from a time server.

Page 9: Synchronization

9

Cristian's AlgorithmA major problem – the sender clock/client is fast arriving value of CUTC from the time server will be smaller than client’s current time, C.

What to do?One needs to gradually slow down client clock by adding less time per tick.

Normally each interrupt add 10msec => 9msec per tick.

or add 11 msec per tick to advance the time

Page 10: Synchronization

10

Cristian’s Algorithm Minor problem

– the one-way delay from the server to client is “significant” and may vary considerably.

What to do? Measure this delay and add it to CUTC.

The best estimate of delay is (T1 – T0)/2 for the message propagation time.

Can subtract off I (the server interrupt handling time). one way propagation time = (T1 – T0-I)/2

Page 11: Synchronization

11

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

Page 12: Synchronization

12

Averaging Algorithm Every R seconds, each machine broadcasts its current time.The local machine collects all other broadcast time samples during some

time interval, S.

The simple algorithm algorithmthe new local time is set as the average of the value received from all other machines.

Page 13: Synchronization

13

Averaging Algorithms

A slightly more sophisticated algorithm :: Discardthe m highest and m lowest to reduce the effect of aset of faulty clocks. – Average the rest.

Another improved algorithm :: Correct eachmessage by adding to the received time an estimateof the propagation time from the source.

One of the most widely used algorithms in theInternet is the Network Time Protocol (NTP).

Page 14: Synchronization

14

Logical Clocks

All machine must agree with one time/clock. Logical clock: no matter its not the same to the real time. Lamport “ all process must agree with the sequence of event occurs” Either input.c is older or newer then input.o

Page 15: Synchronization

15

Logical Clock and Lamport Timestamp Logical clocks

– Order of events matters more than absolute time– E.g. UNIX make: input.c input.o

Lamport timestamp– Synchronize logical clocks

Happens-before relation– A -> B : A happens before B– Two cases which determine “happens-before”1. A and B are in same process, and A occurs before B: a -> b2. A is send-event of message M, and B is receive-event of same message M

Transitive relation– If A -> B and B -> C, then A-> C

Concurrent events– Neither A -> B nor B -> A is true

Page 16: Synchronization

16

Lamport Algorithm Assign time value C(A) such that

1. If a happens before b in the same process, C(a) < C(b)

2. If a and b represent the sending and receiving of a message, C(a) < C(b)

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 it clock to be one more than sending time

Extension for total ordering– Requirement: For all distinctive events a and b, C(a) C(b)

– Solution: Break tie between concurrent events using process number

Page 17: Synchronization

17

Lamport Timestamp ExampleClocks run at different rate

0

6

12

18

24

30

36

42

48

54

60

0

8

16

24

32

40

48

56

64

72

80

0

10

20

30

40

50

60

70

80

90

100

A

B

C

D

Page 18: Synchronization

18

Solutions Message C From process 2 leaves at 60 > must arrive at

61 or later

0

6

12

18

24

30

36

42

48

70

76

0

8

16

24

32

40

48

61

69

77

85

0

10

20

30

40

50

60

70

80

90

100

A

B

C

D

Correct clocks using Lamport’s Algorithm

Page 19: Synchronization

19

Application of Lamport timestampsScenario

– Replicated accounts in New York(NY) and San Francisco(SF)

– Two transactions occur at the same time and multicast

• Current balance: $1,000• Add $100 at SF• Add interest of 1% at NY

– Possible results ??•

Example: Totally-Ordered Multicast

Page 20: Synchronization

20

$1000+$100=1100 $1000*.01+1000=1010

1100*.01+1100=$1111

1010+100

=$1110

Inconsistent State

Page 21: Synchronization

21

Totally Ordered Multicast Use Lamport timestamps Algorithm

– Message is time stamped 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

– Lamport algorithm (extended) ensures total ordering of events– All processes will eventually have the same copy

– of the local queue consistent global ordering.

Page 22: Synchronization

22

Global state

local state of each process

eg: database records (not temporary records)

with messages are in transit ( have been sent but not delivered)

Page 23: Synchronization

23

Distributed Snapshot: Intro

Reflects the state in which a system might have been– Chandy and Lamport (1985)

If it is recorded that Q recd a msg from P– then it should also be recorded that P sent it

However, If P’s sending is recorded, but not that of Q receiving it, that’s allowed

Assumption: processes are connected to each other via uni-directional point-to-point channels

Any process can initiate the algorithm – Use a marker with the message to initiate communication

Page 24: Synchronization

24

Global State (1)

a) A consistent cutb) An inconsistent cut

Page 25: Synchronization

25

Global State (2)

a) Organization of a process and channels for a distributed snapshot

Page 26: Synchronization

26

1. Any process can initiate the algorithm.

2. Initiating process P starts by recording its own local state. Then it sends a marker

along each of its outgoing channels.

3. When a process Q receives a marker through an incoming channel C:

• If Q hasn’t already saved its local state,

Q first records its local state and then sends a marker along each of its own outgoing channels.

• If Q has already recorded its state earlier, the marker on channel C is an indicator that Q

should record the state of the channel.

4. A process is done when it has received a marker on each of its coming channels. The

local state of the process and the state of each of its incoming channels are sent to the

initiating process.

ALGORITHM

Page 27: Synchronization

27

Global State (3)

b) Process Q receives a marker for the first time and records its local state

c) Q records all incoming messaged) Q receives a marker for its incoming channel and finishes recording

the state of the incoming channel

Page 28: Synchronization

28

When Q finishes its role in the snapshot it can send one of the two messages to it predecessor

DONE or CONTINUE

A DONE message is sent if All of Q’s successors have returned a “DONE”

Q has not received any message

ElSE

CONTINUE message will sent to its predecessor

Page 29: Synchronization

29

Election Algorithms

Need to find one process that is the coordinator Assume

– Each process has a unique identifier• network address for example

– One process per machine

– Every process knows the process number of every other process

– Processes don’t know which processes are down and which ones are still running

End result of the algorithm: all processes agree on who is the new coordinator/leader

Bully algorithm & Ring Algorithm

Page 30: Synchronization

30

Bully Algorithm (Garcia-Molina) A process notices that coordinator is not responding

– it starts an election (any process can start one) Election algorithm

– P sends an ELECTION message to processes with higher numbers

– If no one responds, P wins the election– If some process with higher process number responds

• P’s job is done, that process takes over • the receiver sends an OK message to P• receiver starts an election process

Eventually all processes give up, except one This process sends out a message saying that it is the new

“COORDINATOR” A process that was down, when it comes back up starts a new

election of its own

Page 31: Synchronization

31

The Bully Algorithm (1)

The bully election algorithm; pcs 7 as coordinator=> crashed(a) Process 4 is the first on noticed the crashed >> send

ELECTION process to 5, 6 and 7 (higher pcs)(b) Process 5 and 6 respond, telling 4 to stop(c) Now 5 and 6 each hold an election

Page 32: Synchronization

32

The Bully Algorithm (2)d) Process 6 tells 5 to stope) Process 6 wins and tells everyone

Page 33: Synchronization

33

Ring Algorithm (1)

Does NOT use a token Assume

– processes are ordered– each process knows its successor

• and the successor’s successor, and so on (needed in case of failures)

Process P detects that the coordinator is dead– sends an ELECTION message to its successor– includes its process number in the message– each process that receives it

• adds its own process number and then forwards it to its successor

– eventually it gets back that message• now what does it do?

Page 34: Synchronization

34

Ring Algorithm (2)

The process that initiated it, then sends out a message saying “COORDINATOR”– the process with highest number in list is the leader– when this comes back, then process P deletes it

Page 35: Synchronization

35

A Ring Algorithm(1)Election algorithm using a ring.

We start with 6 processes,     connected in a logical ring.

Process 6 is the leader,     as it has the highest number.

Page 36: Synchronization

36

A Ring Algorithm(2)

Process 6 fails.

Page 37: Synchronization

37

A Ring Algorithm(3)

Process 3 notices that Process 6 does not respond

So it starts an election, sending a message containing its id to the next node in the ring.

Page 38: Synchronization

38

A Ring Algorithm(4)

Process 5 passes the message on,     adding its own id to the message

Page 39: Synchronization

39

A Ring Algorithm(5)

Process 0 passes the message on,     adding its own id to the message.

Page 40: Synchronization

40

A Ring Algorithm(6)

Process 1 passes the message on,     adding its own id to the message.

Page 41: Synchronization

41

A Ring Algorithm(7)

Process 4 passes the message on,     adding its own id to the message

Page 42: Synchronization

42

A Ring Algorithm(8)

When Process 3 receives the message back,     it knows the message has gone around the ring,         as its own id is in the list.

Picking the highest id in the list,     it starts the coordinator message         "5 is the leader" around the ring

Page 43: Synchronization

43

A Ring Algorithm(9)

Process 5 passes on the coordinator message

Page 44: Synchronization

44

A Ring Algorithm(10)

Process 0 passes on the coordinator message.

Page 45: Synchronization

45

A Ring Algorithm(11)

Process 1 passes on the coordinator message.