Presentation on theme: "Time, Clocks, and the Ordering of Events in a Distributed System"— Presentation transcript:
1Time, Clocks, and the Ordering of Events in a Distributed System Leslie LamportMassachusetts Computer Associates, Inc.Presented byLokendra
2Problem! A Distributed System Q. WHICH REQUEST WAS MADE FIRST? A common ‘Resource’request Brequest ANode ANode BA Distributed System
3Problem! A Distributed System Q. WHICH REQUEST WAS MADE FIRST? A common ‘Resource’Node ANode Brequest Arequest BA Distributed SystemSolutionA Global Clock ??Global Synchronization?
4Problem! A Distributed System Q. WHICH REQUEST WAS MADE FIRST? A common ‘Resource’Node ANode Brequest Arequest BA Distributed SystemSolutionIndividual Clocks?Are individual clocks accurate, precise?One clock might run faster/slower?10:02 AM10:00 AM
5Problem! Synchronization in a Distributed System! Event Ordering : Which event occurred first?How to sync the clocks across the nodes?Can we define notion of ‘happened-before’ without using physical clocks?
6Approach towards solution Develop a logical model of event ordering (without the use of physical clocks)Might result into Anomalous Behavior?Bring in ‘Physical’ Clocks to integrate with the logical modelFix anomalous behaviorMake sure that the ‘Physical’ Clocks are synced.
7Partial Ordering The system is composed of a collection of processes Each process consists of a sequence of events (instructions/subprogram)Process P :instr1instr2instr3 … (Total Order)‘Sending’ and ‘Receiving’ messages among processes‘Send’ : an event‘Receive’ : an event
8‘Happened Before’ Relation a ‘Happened Before’ b : a→bIf a and b are events in the same process, and a comes before b, then a → b.Ifa :message sentb : receipt of the same messagethen a → b.If a → b and b → c then a → c.Two distinct events a and b are said to be concurrent if a -/->b and b -/->a
10Logical ClocksA clock Ci for each process Pi to be a function which assigns a number Ci(a) to any event a in that process.Logical Clock (Ci ) has no relation with the Physical Clock.Clock Condition:For any event a, b:If a→b, then C(a) < C(b)Converse NOT True!Concurrent processes?Clock Condition is satisfied if :C1. If a and b are events in process Pi, and a comes before b, then Ci(a) < Ci(b).C2. If a is the sending of a message by process Pi and b is the receipt of that message by process Pj, thenCi(a) < Cj(b)
11Space Time Diagram A clock ticks through every event The clicks happen between the events
12Logical ClocksFollowing implementation rules are proposed to satisfy the clock condition:IR1. Each process Pi increments Ci between any two successive events.IR2. (a) If event a is the sending of a message m by process Pi, then the message m contains a timestampTm = Ci (a).(b) Upon receiving a message m, process Pi sets Ci greater than or equal to its present value and greater than Tm.
14Total Ordering of events One more relation < to break ties, arbitrary total order of eventsThe total order relation => is defined as:a => b, if and only if(i) Ci(a) < Cj(b)(ii) Ci(a) = Cj(b) and Pi< Pj
15Mutual Exclusion Problem A process using the resource must release it before it can be given to another process.Requests for the resource must be granted in the order in which they were made.Does not indicate what should be done when two processes request for the resource at the same time?If every process using the resource eventually releases it, then every request is eventually granted (no starvation)
16Solution -Distributed Algorithm Each process maintains its own request queue:The Algorithm:Resource request:Process Pi sends the message Tm : Pi requests resource to every otherPi also puts the request message on its request queue.Resource request receipt.:Pi receives Pi’s request message.Pj then puts the message on its request queuePj sends an acknowledgement to Pi (timestamped later)Resource release:Pi removes request message Tm : Pi requests resource from its queuesends the release message Pi releases resource to every other process.Resource release receiptPj receive’s Pi’s resource release message.Pj removes the Tm : Pi requests resource from its request queue.Resource allocation.Pi is allocated the resource when:There is a Tm : Pi requests resource message in Pi ’s request queue which is ordered before and other request in the queue by ).Pi has received messages from every other process timestamped later than Tm .
17Solution -Distributed Algorithm NOTE:Each process independently follows these rulesThere is no central synchronizing process or central storage.Can be viewed as State Machine C x S→ SC : set of commands for eg resource requestS : Machine’s state
19Anomalous Behavior (contd) Two possible ways to avoid such anomalous behavior:Give the user the responsibility for avoiding anomalous behavior.When the call is made, b be given a later timestamp than a.Let S : set of all system eventsŠ : Set containing S + relevant external eventsLet → denote the “happened before” relation for Š .Strong Clock Condition:For any events a; b in Š : if a → b then C(a) < C(b)One can construct physical clocks, running quite independently, and having the Strong Clock Condition, therefore eliminating anomalous behavior.
20Physical Clocks PC1: | dCi(t)/dt - 1 | < ĸ ĸ <<1PC2: For all i,j : |Ci(t) – Cj(t)| < ԑ2 clocks don’t run at the same rate:They may drift furtherNeed of an algorithm that PC2 always holds
21Physical ClocksLet μ be a number such that a→b and a occurs at t in Pi and b in Pj, then b occurs later than t+ μμ : less than the shortest transmission timeTo avoid anomalous behavior, we need:Ci(t+ μ) – Cj(t) > 0, guaranteed if ԑ/(1- ĸ) ≤ μ
22Algorithmμm is the minimum delay of a message and is known by the processesIR1’ : For each i, Ci is differentiable at t and dCi(t)/dt >0IR2’:a) Pi sends a message at t, timestamp Tm=Ci(t)Upon receipt of m, Pj sets Cj(t’) to max (Cj(t’), Tm+ μm )
23Some related concepts Lamport clocks limitation: If (ab) then C(a) < C(b) butReverse is not true!!Nothing can be said about events by comparing time- stamps!If C(A) < C(B), then ??Need to maintain causalityCausal delivery:If send(m) -> send(n) => deliver(m) -> deliver(n)Need a time-stamping mechanism such that:If T(A) < T(B) then A should have causally preceded B
24Vector Clocks (1, 0 , 0) (2, 0, 0) (3, 5, 2) e11 e12 e13 (0, 1, 0) (2, 2, 0)(2, 3, 1)(2, 5, 2)(0, 0, 1)(0, 0, 2)e21e22e23e24e31e32P1P2P3(2,4,2)e25Each process i maintains a vector ViVi[i] : number of events that have occurred at iVi[j] : number of events i knows have occurred at process jLess than or equal: ts(a) ≤ ts(b) if ts(a)[i] is ≤ ts(b)[i] for all i.For eg [ (3,3,5) ≤ (3,4,5) ]ts(e11) = (1, 0, 0) and ts(e22) = (2, 2, 0), which shows e11 e22Causal Delivery :if m is sent by P1 and ts(m) is (3, 4, 0) and you are P3, you should already have received exactly 2 messages from P1 and at least 4 from P2