25
Time and Clocks

Time and Clocks - courses.physics.illinois.edu

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Time and Clocks

Key PropertiesMultiple computers

◦ Concurrent execution◦ Independent failures◦ Autonomous administrators ◦ Heterogeneous capacities, properties◦ Large numbers (scalability)

Networked communication◦ Asynchronous execution◦ Unreliable delivery◦ Insecure medium

Common goal◦ Consistency – can discuss whole-system properties◦ Transparency – can use the system without knowing details

2021-08-31

ClocksWhy do we use clocks?

◦ Synchronization◦ Ordering

2021-08-31

Clock SynchronizationLast bus for my house leaves at 6:12pm from transit plaza

◦ What happens if my watch is 15 minutes slow?◦ What happens if my watch is 15 minutes fast?

2021-08-31

Ordering

2021-08-31

FundamentalsA distributed system contains several processes

Each process has state◦ Variables / memory / …

An event is a change of state◦ Send/receive message◦ Perform computation

Goal: map an event to a time (timestamp)◦ Challenge: do this across processes

2021-08-31

Time SynchronizationEach process has an internal clock

Clocks between processes can (will) be different (if asynchronous DS)◦ Clock skew = relative difference between clock values◦ Clock drift = relative difference in clock rates

◦ First derivative of skew

◦ Maximum drift rate (MDR): bound on drift

2021-08-31

External vs. InternalExternal synchronization

◦ Synchronize to standard time source◦ Universal Coordinated Time (UTC)

Internal synchronization◦ Synchronize between two processes

A & B each externally synchronized withskew < T

◦ What is the bound on their relative skew?◦ 2T (same for MDR)

2021-08-31

Cristian’s algorithm

2021-08-31

Client Server

What time is it?

It’s 2:13:05.33

What should we set the time to?

Cristian’s algorithmMeasure round trip time (RTT)

New time = (received time) + (RTT/2)◦ Guaranteed to be within RTT/2 of correct time

If new time > local time then local time = new time

If new time < local time then adjust gradually (why?)

Can also calculate drift over multiple readings, change clock rate

2021-08-31

Berkeley Algorithm

2021-08-31

ServerClient

ClientClient

Client

Client

What time is it?

Berkeley Algorithm

2021-08-31

ServerClient

ClientClient

Client

Client

9:01:11

9:01:039:01:02

9:00:51

9:00:59

Berkeley AlgorithmEstimate local time @ each client

◦ (Using RTT/2)

Average times

Tell each machine how to adjust

Problems?◦ Internal synchronization only – can drift wrt UTC◦ Server may fail

2021-08-31

The Network Time Protocol (NTP) Build a tree of network servers

Root connected to external time source

2021-08-31

Secondary, synched primary

Primary, UTC synch

Strata 3, synched by the secondary

1

2 2 2

3 3 3 3 3 3

Symmetric NTP mode

2021-08-31

Ti

Ti-1Ti-2

Ti- 3

Server B

Server A

Time

m m'

Time

Each message bears timestamps of recent message events: the local timewhen the previous NTP message was sent and received, and the local timewhen the current message was transmitted.

Theoretical Base for NTP

2021-08-31

Ti

Ti-1Ti-2

Ti- 3

Server B

Server A

Time

m m'

Time

• t and t’: actual transmission times for m and m’(unknown)

• o: true offset of clock at Brelative to clock at A

• oi: estimate of actual offset between the two clocks

• di: estimate of accuracy of oi ;total transmission times for mand m’; di=t+t’

i−2T = i−3T + t +o

iT = i−1T + t '−oThis leads to

id = t + t ' = i−2T − i−3T + iT − i−1To = io + (t '− t) / 2, where

io = ( i−2T − i−3T + i−1T − iT ) / 2.It can then be shown that

io − id / 2 ≤ o ≤ io + id / 2.

=(t-t’+2*o)/2

Logical Clocks Often, we need ordering but not precise time

Happens-before (®) relationship◦ Monotonicity: on same process, time(a) < time(b) then a ® b◦ Causality: If p1 sends m to p2, then send(m) ® receive(m)◦ Transitivity: a ® b and b ® c then a ® c

Lamport algorithm◦ Initialize all local clocks to 0◦ Increment local clock for each event◦ Send local clock value with message◦ Update local clock with max(local,received message+1)

2021-08-31

Events Occurring at Three Processes

2021-08-31

p1

p2

p3

a b

c d

e f

m1

m2

Physicaltime

Lamport Timestamps

2021-08-31

a b

c d

e f

m1

m2

21

3 4

51

p1

p2

p3

Physical time

3

GuaranteesIf a ® b, then time(a) < time(b)

If time(a) < time(b)?◦ Either a ® b◦ Or a and b concurrent

◦ (® is a partial order)

Can we tell which is which?

2021-08-31

Vector Logical Clocks Vector Logical time addresses this issue:

◦ Each process uses vector of clocks◦ ith element is the clock for process i

◦ Each process increments its own entry in vector timestamp for every event◦ Each message carries vector timestamp @ send time◦ For a receive(message) event,

Max(Vreceiver[j] , Vmessage[j]), if j is not selfVreceiver[j] + 1 otherwise

2021-08-31

Vreceiver[j] =

Vector Timestamps

2021-08-31

a b

c d

e f

m1

m2

(2,0,0)(1,0,0)

(2,1,0) (2,2,0)

(2,2,2)(0,0,1)

p1

p2

p3

Physical time

Example: Vector Timestamps

2021-08-31

p 1

p 2

p 3

p 4

0,0,0,0

Vector logical clock

Message(vector timestamp)

Physical Time

0,0,0,0

0,0,0,0

0,0,0,0

(1,0,0,0)

1,0,0,0

1,1,0,0

2,0,0,0

2,0,1,0

(2,0,0,0)

2,0,2,0

2,0,2,1

(2,0,2,0)

1,2,0,0

2,2,3,0

(1,2,0,0)

4,0,2,2

4,2,4,2

(4,0,2,2)

2,0,2,2

3,0,2,2

(2,0,2,2)

2,0,2,3

4,2,5,3

(2,0,2,3)

n,m,p,q

Comparing Vector Timestamps VT1 = VT2,

iff VT1[i] = VT2[i], for all i = 1, … , n

VT1 <= VT2,

iff VT1[i] <= VT2[i], for all i = 1, … , n

VT1 < VT2,

iff VT1 <= VT2 &

$ j (1 < j < n & VT1[j] < VT2 [j])

Then: VT1 is concurrent with VT2

iff (not VT1 <= VT2 AND not VT2 <= VT1)

2021-08-31

Summary, Announcements Time synchronization important for distributed systems

◦ Cristian’s algorithm◦ Berkeley algorithm◦ NTP

Relative order of events enough for practical purposes◦ Lamport’s logical clocks◦ Vector clocks

Next class: Global Snapshots. Reading: 14.5

2021-08-31