物理时钟,Lamport 时钟与 Vector 时钟 Physical Clock, Lamport Logical Clock, and Vector Clock
Time, Clock, and Ordering of Event
Ordering of Event
Why need to order event?
- In real life, some things should happen in order.
- For example,
- News should be corrected before being released.
- Membership card is only activated after the user paid
- Multiple updates of content should happen in order.
- The privacy should be updated before posting
How to order event?
- Label each event with its physical time
Physical Lock
Approach #1 synchronization
Beacon-based approach
- Designate server with GPS/atomic clock as the master
- The master periodically broadcasts the time to clients. Clients resets the time upon receipt
- Problem: the unstable latency in event sending and receiving
Approach #2 Interrogation-based protocols
- Client queries server
- Time =
T1 + (T2-T0)/2
- Multiple samples, average over several servers; throw out outliers
- Take into account the clock rate skew
- Protocols:
- NTP (Network Time Protocol), used in server sync, log records, etc.
- PTP (Precision Time Protocol), used in electricity network, financial trading system, et.
Physical Lock Problem
- Physical lock is not accurate.
- Physical lock is not the same among multiple machines.
How to order events without physical clock?
Happens-before relationship
- Happens-before relationship captures logical (causal) dependencies between events.
- For example, Cooking before Eating; Eating before Sleep.
- (Irreflexive) partial ordering:
->
a -/-> a
, (an event cannot happen before itself)a -> b, then b -\->a
, (ifa
happens beforeb
,b
cannot happen beforea
)a -> b, b -> c then a -> c
, (ifa
happens beforeb
andb
happens beforec
, thena
happens beforec
)
Irreflexive Explanation
Irreflexive means that no element is related to itself. In the context of happens-before relationships, it means an event cannot happen before itself (
a -/-> a
).Partial Ordering Explanation
A partial ordering is a mathematical concept used to describe a set of elements where some pairs of elements are comparable, but not necessarily all. In the context of happens-before relationships, it helps define how events are related in terms of their order.
Happens before In distributed systems
- Processes
- Messages
- Events
- Send msg; Receive msg; Event happens
Properties
-
Happends-before
-
Within a process,
a
comes beforeb
, we havea -> b
-
a -> b
meansb
could have been influenced bya
-
-
Transitivity
- if
a -> b
andb -> c
thena -> c
- if
-
Concurent
a -/-> b
andb -/-> a
: events are concurrent- Concurrent means: No one can tell whether a or b happened first.
Example
- Events with happens-before relationship
- Cooking before Eating,
- Eating before Sleeping,
- Washing before Sleeping
- Transitivity: Cooking Before Sleep
- Concurent: Washing is concurrent with Eating, we cannot determine the order based on limited given happens-before infomation.
Lamport Logical Clock
Goal
We need to mark each event with timestamp C
to preserve the happens-before order of events.
if a -> b
, then C(a) < C(b)
Implementation
- Keep a local clock
T
on each process - Increment T whenever an event happens
- Send clock time on all messages as
Tm
- On message receipt:
T = max(T, Tm) + 1
Use the logical clock to form a total ordering
The following is just to define a new relation: =>
based on C() < C()
大白话来讲,如果 a 的时间戳比 b 小,我们可以说
a => b
。如果 a 发生在 b 之前,即a -> b
,我们可以说 a 的时间戳一定比 b 小, 即a => b
。但反之不成立。
- If
C(a) < C(b)
, thena => b
- If
C(a)==C(b)
, the processID ofa
andb
shoudl be different a->b
meansa=>b
- In converse,
a=>b
, i.e.C(a)<C(b)
doesn't meana->b
. For example, the eventB(T=4)
ie concurent toD(T=1)
, we cannot saidD -> B
based on theC(D) > C(B)
- In converse,
Logical Lock Problem
We cannot tell the ordering of events with conccurrent logical lock
- When
a -> b
, thenC(a) < C(b)
But in converse, ifC(a) < C(b)
, it doesn't meana -> b
, they could also be concurrent in different processes - For other concurrents cases which not even
C(a) < C(b)
, we cannot tell the ordering of event as well based on physical lock.
Vector Clock
Vector clock will resolve the logical lock problem.
Rules
- Clock is a vector, its length is
n
, i.e. the number of nodes. - On node
i
, incrementC[i]
on each event- For exmaple
- Node
#0
with vector lock(3, 5, 2)
; - After a event happen:
(4, 5, 2)
- Node
- For exmaple
- On receipt of a message with clock
Cm
on nodei
:- Increment
C[i]
- For each
j != i
,C[j] = max(C[j], Cm[j])
- For example:
- Node
#0
with vector lock(4, 5, 2)
receives a message(2, 7, 0)
, its vector lock is updated to(5, 7, 2)
- Node
- For example:
- Increment
Properties
- Happens before
- If
Cx[i] <= Cy[i]
for alli
, and there exists j such thatCx[j] < Cy[j]
- That means
Cx
happens beforeCy
- That means
- If
- Concurrent
- For two vectors x and y, if
Cx[i] < Cy[i]
, andCx[j] > Cy[j]
for somei
andj
- For two vectors x and y, if
Others
TCP ensures the msg received is the newest and latest in the msg tunnel between a and b
Reference
- NUS CS5223 Distributed System Course
- Time, Clock, and the Ordering of Events in a Distributed System
- Designing Data-Intensive Applications