Lecture 7.1 Coordination
Lecture 7.1 Coordination
Clock Synchronization
• Problem: When each machine has its own clock, an event that occurred
after another event may be assigned an earlier time
Physical Clock
Quartz Clocks
Atomic Clocks
• When greater accuracy is required, atomic clocks are used
• These clocks are based on quantum mechanical properties of certain
atoms, such as Caesium or Rubidium
• Atomic clocks combine a quartz crystal oscillator with an ensemble of
atoms to achieve greater stability
• The time unit of one second in the International System of Units (SI)
is defined using caesium-133 atom (International Atomic Time (TAI): 1
day is 24 × 60 × 60 × 9,192,631,770 periods of cesium-133’s resonant
frequency)
• Atomic clocks deviate only 1 second in up to 100 million years
• Atomic clocks are too expensive and bulky to build into every computer
and phone, so quartz clocks are used
Universal Time (UT1):
• Universal Time (UT) is a time standard that reflects the average speed
of the Earth's rotation
• It is not measured by clocks but by looking at the stars
Issues:
• The speed of rotation of the planet is not constant
In computing:
• A timestamp is a representation of a particular point in time
• Two representations of timestamps are commonly used: Unix time and ISO
8601
Unix time
• For Unix time, zero corresponds to the arbitrarily chosen date of 1
January 1970, known as the epoch
• Ignore leap seconds
Disadvantage
• This complicates software that needs to work with dates and times
• To be correct, software that works with timestamps needs to know about
leap seconds
• For example, if you want to calculate how many seconds elapsed between
two timestamps, you need to know how many leap seconds were inserted
between those two dates
• Besides, OS and distributed systems often need timings with sub-second
accuracy
Pragmatic solution:
• “smear” (spread out) the leap second over the course of a day
ISO 8601:
year, month, day, hour, minute, second, and time zone offset relative to UTC
example: 2020-11-09T09:50:17+00:00
• We can estimate that the server’s clock will have moved on to t3 plus
the one-way network latency
• We then subtract the client’s current time t4 from the estimated server
time to obtain the estimated skew between the two clocks
• Once the client has estimated the clock skew θ, it needs to apply that
correction to its clock
• If |θ| < 125 ms, slew the clock:
slightly speed it up or slow it down by up to 500 ppm (brings clocks in
sync within ≈ 5 minutes)
• If 125 ms ≤ |θ| < 1,000 s, step the clock:
suddenly reset client clock to estimated server timestamp
• If |θ| ≥ 1,000 s, panic and do nothing
(leave the problem for a human operator to resolve)
Systems that rely on clock sync need to monitor clock skew
Hierarchy of clock servers arranged into strata:
o Stratum 0: atomic clock or GPS receiver
o Stratum 1: synced directly with stratum 0 device
o Stratum 2: servers that sync with stratum 1, etc.
• May contact multiple servers, discard outliers, average rest
• Makes multiple requests to the same server, use statistics to reduce
random error due to variations in network latency
• Reduces clock skew to a few milliseconds in good network conditions but
can be much worse!
Time-of-day clock:
• Time since a fixed date (e.g., 1 January 1970 epoch)
• May suddenly move forwards or backwards (NTP stepping), subject to leap
second adjustments
• Timestamps can be compared across nodes (if synced)
• Java: System.currentTimeMillis()
• Linux: clock_gettime(CLOCK_REALTIME)
Monotonic clock:
• Time since arbitrary point (e.g., when machine booted up)
• Always moves forwards at near-constant rate
• Good for measuring elapsed time on a single node
• Java: System.nanoTime()
• Linux: clock_gettime(CLOCK_MONOTONIC)
• Steps
o The time daemon tells all machine its time
o Other machines answers how far ahead or behind
o The time daemon computes the average and tell other how to adjust
Causality and happens-before
Ordering of messages
Problem:
Causality
Vector clocks
• Given two arbitrary Lamport timestamps L(a) and L(b) with L(a) < L(b)
we can’t tell whether a → b or a||b
• Vector clock can do that
• If one event’s clock comes before another’s, then that event comes
before the other, i.e., it is a two-way condition
• Assume n nodes in the system, N = ⟨N1, N2, ..., Nn⟩
• Vector timestamp of event a is V(a)=⟨t1, t2, ..., tn⟩
• ti is number of events observed by node Ni
• Each node has a current vector timestamp T
• On event at node Ni, increment vector element T[i]
• Attach current vector timestamp to each message
• Recipient merges message vector into its local vector
Vector clocks example
• (V(a)<V(b)) ⇐⇒(a→b)
• (V(a)=V(b)) ⇐⇒(a=b)
• (V(a)∥ V(b)) ⇐⇒(a∥b)
• We say that one vector is less than or equal to another vector if every
element of the first vector is less than or equal to the corresponding
element of the second vector
• One vector is strictly less than another vector if they are less than
or equal, and if they differ in at least one element
• However, two vectors are incomparable if one vector has a greater value
in one element, and the other has a greater value in a different
element