Professional Documents
Culture Documents
9.DS Clock Synchronization
9.DS Clock Synchronization
9.DS Clock Synchronization
2
Hw/Sw Clocks
• Physical clocks in computers are realized as crystal oscillation
counters at the hardware level
– Correspond to counter register H(t)
– Used to generate interrupts
• Usually scaled to approximate physical time t, yielding software clock
C(t), C(t) = αH(t) + β
– C(t) measures time relative to some reference event, e.g., 64 bit counter for
# of nanoseconds since last boot
– Simplification: C(t) carries an approximation of real time
– Ideally, C(t) = t (never 100% achieved)
– Note: Values given by two consecutive clock queries will differ only if clock
resolution is sufficiently smaller than processor cycle time
3
Problems with H/W or S/W Clock
• Skew: Disagreement in the reading of two clocks
• Drift: Difference in the rate at which two clocks count the time
– Due to physical differences in crystals, plus heat, humidity, voltage, etc.
– Accumulated drift can lead to significant skew
4
Challenges
• Two clocks do not agree perfectly
• Skew: The time difference between two clocks
• Quartz oscillators vibrate at different rates
• Drift: The difference in rates of two clocks
• If we had two perfect clocks
– Skew = 0
– Drift = 0
5
Clock Skew
• When we detect a clock has a skew
• Eg: it is 5 seconds behind Or 5 seconds ahead
• What can we do?
6
Clock Skew: Impacts & Solutions
• When we detect a clock has a skew
• Eg: it is 5 seconds behind – We can advance it 5 seconds to correct/Run it
faster until it catches up
• Or 5 seconds ahead – Pushing back 5 seconds is a bad idea/Run it slower
until it catches up
• • This does not guarantee correct clock in future
• – Need to check and adjust periodically
• Problems due to Skew:
– Message was received before it was sent
– Document closed before it was saved etc..
– We want monotonicity: time always increases
7
How clocks synchronize ?
Obtain time from Time server:
Request Time
Client Server
Reply: Time: 00:05:20
8
Causes of Inaccurate Time
9
Clock Inaccuracies
• Clock inaccuracies cause problems and troublesome in solving tasks
in distributed systems.
• The clocks of different nodes need to be synchronized to limit errors.
• Need of an efficient communication or resource sharing.
• Clocks need to be monitored and adjusted continuously. Otherwise,
the clocks drift apart.
• Similarly clock skew also introduces mismatch in time value of two
clocks.
• Both these are to be addressed to make an efficient usage of
features of distributed systems.
Even when initially set accurately, real clocks will differ after some amount of
time due to clock drift,
11
Solutions
The synchronization solution using a central server is trivial; the server will dictate the system
time. (Single point of failure)
(2) Concepts of Logical clock in distributed systems for Clock Synchronization (Logical):
(a) Lamport timestamps & (b) Vector clocks
12
Solutions
Wired Distributed Systems: Internet, LAN, MAN, WAN, PAN etc
Network Time Protocol (NTP): Works on client-server architecture
It also suffers in implementations using a single server, making it unsuitable for many
distributive applications where redundancy may be crucial.
14
Cristian's algorithm
Client To T1
15
Cristian's algorithm
Cristian's algorithm works between a process P, and a time server S connected to a time reference source.
16
Berkeley algorithm (Physical Clock Synchronization)
Developed by Gusella and Zatti at the University of California, Berkeley in 1989.
The server process (called the leader) periodically polls other follower processes
requesting for time.
Based on the answers, it computes an average time & tells all the other nodes to advance
their clocks to the new time or slow their clocks down until some specific reduction has
been achieved
The time daemons time must be set manually by the operator periodically
17
Example
The time daemon sends its own clock value and asks all other nodes
for their clock values (i.e., 3.00)
3.00
Time Daemon
Network
2.50 P1 P2 3.25
18
Example
The nodes answer the difference in their time w.r.t time at Time
Daemon ( i.e., -10 & +25)
3.00
Time Daemon
2.50 P1 P2 3.25
19
Example
The time daemon computes the average of time of all the nodes including time daemon i.e, (3.00 + 2.50 +
3.25)/3 = 9.15/3 = 3.05.
Time Daemon
3.05
3.05 P1 P2 3.05
The time daemon tells other nodes to adjust their cock values by increasing or
decreasing by sending difference in values (i.e., 15 & -20) instead of average
values.
Berkeley algorithm
A leader is chosen via an election process such as Chang and Roberts algorithm.
The leader polls the followers who reply with their time in a similar way to Cristian's algorithm.
The leader observes the round-trip time (RTT) of the messages and estimates the time of
each follower and its own.
The leader then averages the clock times, ignoring any values it receives far outside the values
of the others.
Instead of sending the updated current time back to the other process, the leader then sends
out the amount (positive or negative) that each follower must adjust its clock.
Computer systems normally avoid rewinding their clock when they receive a negative clock
alteration from the leader. This would break the property of monotonic time, which is a
fundamental assumption in certain algorithms in the system itself or in programs such
as make.
A simple solution to this problem is to halt the clock for the duration specified by the leader,
but this simplistic solution can also cause problems, although they are less severe. For minor
corrections, most systems slow the clock (known as "clock slew"), applying the correction over
a longer period of time.
Often, any client whose clock differs by a value outside of a given tolerance is disregarded
when averaging the results. This prevents the overall system time from being drastically
skewed due to one erroneous clock.
22
Logical Clock
Due to lack of global physical clock in a distributed system and limitations of Berkeley
algorithm, Lamports introduced the concept of logical clock based on event ordering instead of
using physical clock.
Two event ordering clocks : (i) Lamports Clock (Also known as Scalar Clock)
For Partial ordering of events
(ii) Vector Clocks (modification of Lamport Clocks)
Lamports has defined certain rules to increament the counter values which are assigned to
events in the processes of distributed system
Clock Drift rate is usually assumed 1 unit however any value greater than 1 can be assumed
Network delay is usually assumed 1 unit however any value greater than 1 can be assumed
23
Each process has n number of instructions or tasks
An Event ?
24
Three Conditions proposed by Lamport:
(1) a -> b C(a) < C (b) (Happened Before Relations) indicates event a is always earlier than b
(2) If a is a sending event of message m and b is a Receive event of message m then C(a) < C(b)
Where a, b & c are events may be executed in same or different processes and
25
Logical Clocks
Physical clocks are physical entities that assign physical times to events,
Logical clocks order events logically by assigning logical timestamps instead of physical
ordering.
In fact, the logical clock decides the order of execution of different parallel or concurrent or
independent processes
Whereas the logical clocks are simply a conceptualization of a mathematical function that
assigns numbers to events. These numbers act as timestamps that help in ordering events.
Refer to implementing a protocol on all machines within your distributed system, so that the
machines are able to maintain consistent ordering of events within some virtual timespan.
26
Logical Clocks
More formally, each process Pi has a clock Ci which is a function from events to the integers.
The system clock, C = f(from events to the integers) where C(e)=Ci(e) and e is an event in Pi
Causal Functionality:
Given 2 events (e1, e2) where one is caused by the other (e1 contributes to e2 occurring). Then the
timestamp of the ‘caused by’ event (e1) is less than the other event (e2).
27
Implementation Rules
To provide this functionality any Logical Clock must provide 2 rules:
Rule 1: this determines how a local process updates its own clock when an event occurs.
Before executing an event (excluding the event of receiving a message) increment the local clock by 1.
Local_clock = local_clock + 1
Rule 2: determines how a local process updates its own clock when it receives a message from another
process. This can be described as how the process brings its local clock inline with information about the
global time.
When receiving a message (the message must include the senders local clock value) set your local
clock to the maximum of the received clock value and the local clock value. After this, increment
your local clock by 1
2. local_clock = local_clock + 1
28
Lamports Logical Clocks
Key Idea:
(1) Processes exchange messages
(2) Messages must be send before they are received
(3) Send/Receive is used to order the events & synchronize the logical clocks
Let
Pi is process i
a, b & c …. are events in processes
Ci(a) is the time stamp of event ‘a’ of process Pi
IR is the implementation rule
Clock Condition to evaluate the logical clocks with the following correctness criterion
a, b, c, d, e, f, g, h, i, j, k, l, m are events
Process P1 has h, i, and j has 3 events, Time stamps are 1,2, & 3
C(d) = 4;C(m) = 3 4 > 3 does not satisfy lamports happened before relationship
(2+1)
(2+1) (6+1)
Clock Value of e17: Max(C(e24) + 1,C(e24) + 1) = Max( 4+1, 6+1) = Max (5,7) = 7
34
Another Example
35
36
37
Logical Clock
38
Logical Clock
39
Another Example
40
Clock Values: 1 2 7 8
P1 e11 e12 e13 e14
Clock Values: 1 2 3 5 6
e21x e22 e23 e24 e25
P2
41
Limitations of Lamports Clock or Scalar Clock
W.R.T Implementation rule 1 and 2:
Space
Total Ordering of Events: Used to solve the problem of partial ordering of events using an arbitrary
mathmatical functions
Example: Multiply by 10 and I to the clock value of Pi so that the values are different from each other
43
Total Ordering of Events
Clock Val*10+2: 12 22 32 52 62
e21 e22 e23 e24 e25
P2
By looking at the clock, we can observe whether one event caused another event.
Each process keeps a vector (a list of integers) with an integer for each local clock
of every process within the system.
45
Vector Clocks
Given a process (Pi) with a vector (v), Vector Clocks implement the Logical Clock rules as follows:
Rule 1: Before executing an event (excluding the event of receiving a message) process Pi
increments the value v[i] within its local vector by 1.
This is the element in the vector that refers to Node(i)’s local clock.
local_vector[i] = local_vector[i] + 1
Rule 2: When receiving a message (the message must include the senders vector) loop through
each element in the vector sent and compare it to the local vector, updating the local vector
to be the maximum of local and received clock value.
Disadvantages: Costly due to the need of sending the entire Vector to each
process for every message sent, in order to keep the vector clocks in sync.
When there are a large number of processes this technique can become
extremely expensive, as the vector sent is extremely large.
Clock Val: (0 1 0) (2 2 0) (2 3 1) (2 4 1)
P2 e21 e22 e23 e24
P3 e31 e32
Clock val (0 0 1) (0 0 2)
TIME ------
Clock Val: (0 1 0) (0 2 1) (2 3 1) (2 4 1)
P2 e21 e22 e23 e24
TIME ------
52
References
https://www.youtube.com/watch?v=VqZa4raMv_Q
53
Thank
You
54