Time and Clocks LAMPORTS LOGICAL & VECTOR CLOCK
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
Time and Clocks
Introduction Clocks, events and process states Synchronizing physical clocks Logical time and logical clocks
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
Introduction
We need to measure time accurately:
to know the time an event occurred at a computer to do this we need to synchronize its clock with an authoritative external clock
Algorithms for clock synchronization useful for
concurrency control based on timestamp ordering authenticity of requests e.g. in Kerberos this chapter discusses clock accuracy and synchronisation It gives ordering of events - also useful for consistency of replicated data
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
There is no global clock in a distributed system
Logical time is an alternative
Computer clocks and timing events
Each computer in a DS has its own internal clock
used by local processes to obtain the value of the current time processes on different computers can timestamp their events but clocks on different computers may give different times computer clocks drift from perfect time and their drift rates differ from one another. clock drift rate: the relative amount that a computer clock differs from a perfect clock
Even if clocks on all computers in a DS are set to the same time, their clocks will eventually vary quite significantly unless corrections are applied
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
Clocks, events and process states
A distributed system is defined as a collection P of N processes pi, i = 1,2, N Each process pi has a state si consisting of its variables (which it transforms as it executes) Processes communicate only by messages (via a network) Actions of processes:
Send, Receive, change own state
Event: the occurrence of a single action that a process carries out as it executes e.g. Send, Receive, change state Events at a single process pi, can be placed in a total ordering denoted by the relation i between the events. i.e.
e i e if and only if e occurs before e at pi
A history of process pi: is a series of events ordered by i
history(pi)= hi = <ei0, ei1, ei2, >
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
Clocks
We have seen how to order events (happened before) To timestamp events, use the computers clock At real time, t, the OS reads the time on the computers hardware clock Hi(t) It calculates the time on its software clock Ci(t)= Hi(t) + e.g. a 64 bit number giving nanoseconds since some base time in general, the clock is not completely accurate but if Ci behaves well enough, it can be used to timestamp events at pi
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
Skew between computer clocks in a distributed system
Network
Figure 10.1
Computer clocks are not generally in perfect agreement Skew: the difference between the times on two clocks (at any instant) Computer clocks are subject to clock drift (they count time at different rates) Clock drift rate: the difference per unit of time from some ideal reference clock Ordinary quartz clocks drift by about 1 sec in 11-12 days. (10-6 secs/sec). High precision quartz clocks drift rate is about 10-7 or 10-8 secs/sec
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
Logical time and logical clocks (Lamport 1978)
Instead of synchronizing clocks, event ordering can be used
1. 2. 3.
If two events occurred at the same process pi (i = 1, 2, N) then they occurred in the order observed by pi, that is when a message, m is sent between two processes, send(m) happened before receive(m) The happened before relation is transitive
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
Logical time and logical clocks (Lamport 1978)
a b (at p1) c d (at p2) b c because of m1 also d f because of m2 Not all events are related by consider a and e (different processes and no chain of messages to relate them) they are not related by ; they are said to be concurrent; write as a || e
HB1, HB2 and HB3 (page 397) are formal statements of these 3 points
the happened before relation is the relation of causal ordering
p1 a p2 c d m2 b m1
Figure10.5
Physical time
p3 e
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
Lamports logical clocks
A logical clock is a monotonically increasing software counter. It need not relate to a physical clock. Each process pi has a logical clock, Li which can be used to apply logical timestamps to events
LC1: Li is incremented by 1 before each event at process pi LC2: (a) when process pi sends message m, it piggybacks t = Li (b) when pj receives (m,t) it sets Lj := max(Lj, t) and applies LC1 before timestamping the event receive (m)
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
Give an example to show the converse is not true
e.g. L(b) > L(e) but b || e
each of p1, p2, p3 has its logical clock initialised to zero, the clock values are those immediately after the event. for 1 for is piggybacked and c gets max(0,2)+1 e.g. m1, 2a, 2 for b. =3
1 p1 a 2 b m1 3 p2 c 1 p3 e
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
e e implies L(e)<L(e) The converse is not true, that is L(e)<L(e') does not imply e e
Figure 10.6
4 d m2 5 f Physical time
Vector clocks overcome the shortcoming of Lamport logical clocks (L(e) < L(e) does not imply e happened before e) Vector timestamps are used to timestamp local events They are applied in schemes for replication of data and causal multicast
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
Vector clock Vi at process pi is an array of N integers VC1:initially Vi[j] = 0 for i, j = 1, 2, N VC2:before pi timestamps an event it sets Vi[i] := Vi[i] +1 VC3:pi piggybacks t = Vi on every message it sends VC4:when pi receives (m,t) it sets Vi[j] := max(Vi[j] , t[j]) j = 1, 2, N ( then before next event adds I to own
element using VC2)
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
Can you see a pair of parallel events?. Vi[i]] is the number of events that pi has timestamped Viji] ( j i) is the number|| e( parallel) pj that pi has been affected bynor V(e) <= V(c). c of events at because neither V(c) <= V(e)
At p1 a(1,0,0) b (2,0,0) piggyback (2,0,0) on m1
Vector clocks
At p2 on receipt of m1 get max ((0,0,0), (2,0,0)) = (2, 0, 0) add 1 to own element = (2,1,0)
Meaning of =, <=, max etc for vector timestamps - compare elements pairwise Note that e e implies L(e)<L(e). The converse is also
true. See Exercise 10.3 for a proof.
(1,0,0) p1 a (2,0,0) b m1 (2,1,0) p2 c (0,0,1) p3 e
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE
Figure 10.7
(2,2,0) d m2 (2,2,2) f Physical time
Summary on time and clocks in distributed systems
accurate timekeeping is important for distributed systems. algorithms (e.g. Cristians and NTP) synchronize clocks in spite of their drift and the variability of message delays. for ordering of an arbitrary pair of events at different computers, clock synchronization is not always practical. the happened-before relation is a partial order on events that reflects a flow of information between them. Lamport clocks are counters that are updated according to the happenedbefore relationship between events. vector clocks are an improvement on Lamport clocks,
we can tell whether two events are ordered by happened-before or are concurrent by comparing their vector timestamps
DS BY KAPIL SHRIVASTAVA ,ASST. PROFESSOR @CSE