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; 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
Use of time in distributed systems:
Clock Synchronization make example When each machine has its own clock, an event that occurred after another event may nevertheless be assigned an earlier time.
Clock Synchronization Algorithms ! Centralized Algorithms ! Cristian’s Algorithm (1989) ! Berkeley Algorithm (1989) ! Decentralized Algorithms ! Averaging Algorithms (e.g. NTP) ! Multiple External Time Sources
Cristian’s Algorithm Assume one machine (the time server) has a WWV receiver and all other machines are to stay synchronized with it. Every specific seconds, each machine sends a message to the time server asking for the current time. ! Time server responds with message containing current time, CUTC.
Cristian's Algorithm Getting the current time from a time server.
Cristian's Algorithm A 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
Cristian’s Algorithm Minor problem What to do? – 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 !
The Berkeley Algorithm The time daemon asks all the other machines for their clock values The machines answer The time daemon tells everyone how to adjust their clock
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 algorithm the new local time is set as the average of the value received from all other machines.
Averaging Algorithms ! A slightly more sophisticated algorithm :: Discard the m highest and m lowest to reduce the effect of a set of faulty clocks. – Average the rest. ! Another improved algorithm :: Correct each message by adding to the received time an estimate of the propagation time from the source. ! One of the most widely used algorithms in the Internet is the Network Time Protocol (NTP).
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
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” A and B are in same process, and A occurs before B: a -> b 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
Lamport Algorithm Assign time value C(A) such that Lamport Algorithm If a happens before b in the same process, C(a) < C(b) 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
Lamport Timestamp Example Clocks run at different rate 6 12 18 24 30 36 42 48 54 60 8 16 32 40 56 64 72 80 10 20 50 70 90 100 A B C D
Correct clocks using Lamport’s Algorithm Solutions Message C From process 2 leaves at 60 > must arrive at 61 or later 6 12 18 24 30 36 42 48 70 76 8 16 32 40 61 69 77 85 10 20 50 60 80 90 100 A B C D Correct clocks using Lamport’s Algorithm
Example: Totally-Ordered Multicast Application of Lamport timestamps Scenario 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 ??
Inconsistent State $1000*.01+1000=1010 $1000+$100=1100 1010+100 =$1110 1100*.01+1100=$1111
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.
Global state local state of each process eg: database records (not temporary records) with messages are in transit ( have been sent but not delivered)
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
Global State (1) A consistent cut An inconsistent cut
Global State (2) Organization of a process and channels for a distributed snapshot
ALGORITHM 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.
Global State (3) Process Q receives a marker for the first time and records its local state Q records all incoming message Q receives a marker for its incoming channel and finishes recording the state of the incoming channel
A DONE message is sent if 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
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
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
The Bully Algorithm (1) The bully election algorithm; pcs 7 as coordinator=> crashed Process 4 is the first on noticed the crashed >> send ELECTION process to 5, 6 and 7 (higher pcs) Process 5 and 6 respond, telling 4 to stop Now 5 and 6 each hold an election
The Bully Algorithm (2) Process 6 tells 5 to stop Process 6 wins and tells everyone
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?
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
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.
A Ring Algorithm(2) Process 6 fails.
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.
A Ring Algorithm(4) Process 5 passes the message on, adding its own id to the message
A Ring Algorithm(5) Process 0 passes the message on, adding its own id to the message.
A Ring Algorithm(6) Process 1 passes the message on, adding its own id to the message.
A Ring Algorithm(7) Process 4 passes the message on, adding its own id to the message
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
A Ring Algorithm(9) Process 5 passes on the coordinator message
A Ring Algorithm(10) Process 0 passes on the coordinator message.
A Ring Algorithm(11) Process 1 passes on the coordinator message.