Issues with Clocks
Problem Lack of global time –Need to compare different events in a distributed system
Problem (Continued) Consider the problem of detecting insider trading in a stock exchange –Assume that no communication occurs outside the computer systems –Each entity is represented by a computer and they communicate among themselves –Whenever a process sends a message, it includes ALL the information it has learnt so far Lets not worry about the cost of implementing this –Each entity is required to follow the protocol you choose
Problem (Continued) Consider two events –An inside event, say e, that affects the company A –An event f, where an officer, X, of company A sells stock Question –Is it possible that X is guilty of insider trading?
Situation 1 Assume global time –Event e occurred at 8am –Event f occurred at 10am (same day) –Answer: –Event e occurred at 10am –Event f occurred at 8am (same day) –Answer:
Approach What we need is a way to define causality –Can event e affect event f? –This is defined as the happened before relation –Please do not confuse it with the English meaning of this
happened before Let a, b, c be events –An event can be a local event, send event or a receive event Definition : a b (read as a happened before b) iff either one of the following condition is true –a and b are events on the same process and a occurred before b –a is a send event and b is the corresponding receive event –there exists event c such that a c and c b
Revisiting the Previous Problem Consider two events –An inside event, say e, that affects the company A –An event f, where an officer, X, of company A sells stock If e f then X is guilty of insider trading
Logical Clocks Goal of logical clock is –Assign each event a timestamp –If e f then the timestamp of e should be less than that of f. –To solve this, each process maintains a logical clock cl cl.j : clock value of process j cl.m : clock value of message m cl.a : clock value of event a
Program for Logical Timestamps Let a be a new event –If a is a local event at j cl.j := cl.j + 1 cl.a := cl.j –If a is a send event at j cl.j := cl.j + 1 cl.m := cl.j cl.a := cl.j –If a is a receive of message m at j cl.j := max(cl.j, cl.m) + 1 cl.a := cl.j
Properties of Logical Clocks If a b then cl.a < cl.b
Proving Properties of Logical Clocks Theorem: At any time, the following statement is true for all events (that have occurred in the system so far) – x, y :: x y cl.x < cl.y, –where x and y range over all the events, processes (latest event on the process) and messages (corresponding send event) Proof by induction Base case –No events created. Hence, trivially true –Show that whenever a new event is created
Invariant The predicate –a b cl.a < cl.b –Is called an invariant of the logical timestamp program. An invariant of a program is a predicate such that –If any step of the program is executed in a state where the invariant is true then the resulting state is also one where the invariant predicate is true –If the program execution starts in a state where the invariant is true then its execution is correct.
Logical Timestamps The time associated with an event is a pair, the clock and the process where the event occurred. For event a at process j, the timestamp ts.a is –ts.a = Lexicographical comparison iff x1 < y1 ( (x1 = y1) (x2 < y2) )
Observation about Logical Clocks For any two distinct events a and b, either ts.a < ts.b ts.b < ts.a The event timestamps form a total order.
So What? Consider two events –An inside event, say e, that affects the company A –An event f, where an officer, X, of company A sells stock Question –Is it possible that X is guilty of insider trading? Case 1 –cl.e = 8, cl.f = 10 –Answer: Case 2 –cl.e = 10, cl.f = 8 –Answer:
Problem Logical timestamps provide a partial information about causality. Extending logical timestamps for complete knowledge of causality