[ L OGICAL C LOCKS ] Shrideep Pallickara Computer Science Colorado - - PDF document

l ogical c locks
SMART_READER_LITE
LIVE PREVIEW

[ L OGICAL C LOCKS ] Shrideep Pallickara Computer Science Colorado - - PDF document

CS555: Distributed Systems [Fall 2019] Dept. Of Computer Science , Colorado State University CS 555: D ISTRIBUTED S YSTEMS [ L OGICAL C LOCKS ] Shrideep Pallickara Computer Science Colorado State University CS555: Distributed Systems [Fall 2019]


slide-1
SLIDE 1

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.1

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS 555: DISTRIBUTED SYSTEMS

[LOGICAL CLOCKS]

Shrideep Pallickara Computer Science Colorado State University

September 3, 2019

L3.1 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.2 Professor: SHRIDEEP PALLICKARA

Frequently asked questions from the previous class survey

September 3, 2019

¨ NTP ¤ Why use the closest server to synchronize?

slide-2
SLIDE 2

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.2

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.3 Professor: SHRIDEEP PALLICKARA

Topics covered in this lecture

¨ Clock synchronization ¤ Berkley Algorithm ¤ Cristian’s Algorithm ¤ Synchronization in wireless settings ¨ Logical Clocks ¤ Lamport’s clocks

September 3, 2019 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

THE BERKLEY ALGORITHM

September 3, 2019

L3.4

slide-3
SLIDE 3

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.3

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.5 Professor: SHRIDEEP PALLICKARA

The Berkley Algorithm for time synchronization

September 3, 2019

¨ In NTP, the time server is passive ¤ Server merely responds to queries from the server ¨ The time server is active in the Berkley Algorithm ¤ Polls every machine periodically for their time ¨ Collects responses and computes the average ¤ Tell machines to slowdown or speedup their clocks

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.6 Professor: SHRIDEEP PALLICKARA

Berkley Algorithm: An example

Machine 1 Machine 2 Machine 3

3:00 2:50 3:25

NETWORK

TIME DAEMON

3:00 3:00 3:00

September 3, 2019

slide-4
SLIDE 4

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.4

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.7 Professor: SHRIDEEP PALLICKARA

Berkley Algorithm: An example

Machine 1 Machine 2 Machine 3

3:00 2:50 3:25

NETWORK

TIME DAEMON

  • 10

+25

September 3, 2019 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.8 Professor: SHRIDEEP PALLICKARA

Berkley Algorithm: An example

Machine 1 Machine 2 Machine 3

3:05 3:05 3:05

NETWORK

TIME DAEMON

+5 +15

  • 20

September 3, 2019

slide-5
SLIDE 5

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.5

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.9 Professor: SHRIDEEP PALLICKARA

Time in the Berkley Algorithm

September 3, 2019

¨ Not necessary for the computed time to agree with real time ¨ It is sufficient that all machines agree on some time ¨ No harm done if there is no contact with external machines

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CRISTIAN’S ALGORITHM

September 3, 2019

L3.10

slide-6
SLIDE 6

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.6

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.11 Professor: SHRIDEEP PALLICKARA

Cristian’s algorithm

¨ Uses a time server to synchronize services ¤ The server receives signals from a UTC source ¨ When a request is received, the server process supplies the time

September 3, 2019 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.12 Professor: SHRIDEEP PALLICKARA

Clock synchronization using a time server

Process p

Time Server, S

mr mt

September 3, 2019

slide-7
SLIDE 7

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.7

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.13 Professor: SHRIDEEP PALLICKARA

Some notations

¨ A process p requests time in a message mr ¨ Process p receives time value in a message mt ¨ Time t is inserted in mt at the last possible moment by Server S ¨ Process p records total round-trip time Tround ¤ To send request mr and to receive the reply mt

September 3, 2019 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.14 Professor: SHRIDEEP PALLICKARA

Measurement of the round trip

September 3, 2019

¨ This is reasonably accurate if clock drift rate is small ¨ Round-trip time in a LAN is 1-10 ms ¤ If the clock drift rate is 10-6 seconds/second? n That clock would vary at most by 10-8 seconds (10-6x 10-2) or 10-5 milliseconds

slide-8
SLIDE 8

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.8

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.15 Professor: SHRIDEEP PALLICKARA

Estimating what the time at p should be [1/2]

September 3, 2019

¨ A simple estimate of this is t + Tround/2 ¤ Elapsed time is split equally before and after S placed t in mt ¨ If the value of the minimum transmission time min is known (or

conservatively estimated)?

¤ We can determine accuracy of the result

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.16 Professor: SHRIDEEP PALLICKARA

Estimating what the time at p should be [2/2]

September 3, 2019

¨ The earliest point at which S could have placed the time in mt? ¤ min after p dispatched mr ¨ The latest point at which S could have placed the time in mt? ¤ min before mt arrived at p ¨ Time by S’s clock when mt the arrives at p ¤ Range: [t+ min, t+Tround – min] ¤ Width of the range is Tround – 2min ¤ Accuracy is: ±(Tround/2 – min)

slide-9
SLIDE 9

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.9

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.17 Professor: SHRIDEEP PALLICKARA

An interesting example

September 3, 2019

¨ An interesting case involves Google’s TrueTime API ¤ Explicitly reports the confidence interval on the local clock ¤ When you ask it for the current time, you get back two values: n [ earliest , latest ] n The earliest possible and the latest possible timestamp.

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.18 Professor: SHRIDEEP PALLICKARA

Theoretical limit for coping with multiple faulty clocks?

September 3, 2019

¨ Dolev et al in 1986 ¨ If f is the number of faulty clocks out of N ¤ Then we must have N > 3f if other, correct, clocks are still to be able to

achieve agreement

slide-10
SLIDE 10

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.10

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CLOCK SYNCHRONIZATION IN WIRELESS NETWORKS

September 3, 2019

L3.19 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.20 Professor: SHRIDEEP PALLICKARA

Sensor network settings

¨ Nodes are resource constrained ¨ Multihop routing is expensive ¨ Optimize algorithms for energy consumption

September 3, 2019

slide-11
SLIDE 11

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.11

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.21 Professor: SHRIDEEP PALLICKARA

Reference broadcast synchronization (RBS)

¨ Objective is not to provide all nodes with UTC time ¨ Goal is to internally synchronize all clocks ¨ RBS is not a two-way protocol ¤ Only the receivers synchronize ¨ In RBS, a sender broadcasts a reference message that will allow its

receivers to adjust their clocks

September 3, 2019 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.22 Professor: SHRIDEEP PALLICKARA

Sensor network communications

September 3, 2019

¨ Time to propagate a signal to other nodes ¤ Roughly constant ¤ Assume that there is no multi-hop routing ¨ Wireless networks based on contention protocol ¨ Propagation time is measured from the moment message leaves NIC of

sender

¤ Two elements no longer play role in estimating delay n Time to construct the message n Time to access the network

slide-12
SLIDE 12

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.12

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.23 Professor: SHRIDEEP PALLICKARA

Critical paths in determining network delays: Regular Settings

Message Preparation Time spent in NIC Delivery time to application

A B C

Critical Path

September 3, 2019 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.24 Professor: SHRIDEEP PALLICKARA

Critical paths in determining network delays: RBS

Message Preparation Time spent in NIC Delivery time to application

A B C

Critical Path

September 3, 2019

slide-13
SLIDE 13

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.13

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.25 Professor: SHRIDEEP PALLICKARA

When a node broadcasts a reference message m

¨ Each node p, records Tp,m ¤ The time it received m ¤ Based on p’s local clock ¨ Two nodes, p and q can compare delivery times to compute mutual,

relative offset

Offset[p,q] = Tp,k −

k=1 M

Tq,k M

M is the total number

  • f reference messages

September 3, 2019 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

LOGICAL CLOCKS

September 3, 2019

L3.26

slide-14
SLIDE 14

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.14

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.27 Professor: SHRIDEEP PALLICKARA

Physical time in a distributed system is problematic

September 3, 2019

¨ This is not because of the effects of special relativity, which are

negligible or non-existent for normal computers

¤ Unless you count computers travelling in spaceships ¨ It is because of the inability to accurately timestamp events at different

nodes

¤ We need this to order any pairs of events

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.28 Professor: SHRIDEEP PALLICKARA

If two processes do not interact with each other?

¨ Their clocks need not be synchronized ¨ Lack of synchronization is not observable ¤ Does not cause problems

September 3, 2019

slide-15
SLIDE 15

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.15

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.29 Professor: SHRIDEEP PALLICKARA

Logical clocks

¨ Within a single process, events are ordered uniquely by times shown

  • n local clock

¨ But we cannot synchronize clocks perfectly across a distributed system

[Lamport 1978]

¤ We cannot use physical time to find out the order of an arbitrary pair of

events in a distributed system

September 3, 2019 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.30 Professor: SHRIDEEP PALLICKARA

We can use a scheme that is similar to physical causality to order events

September 3, 2019

① If two events occurred at the same process pi (i=1, 2, …, N) ?

¤ Then they occurred in the order in which pi observes them n This is the order ài

② When a message is sent between processes?

¤ The event of sending the message occurred before the event of receiving the

message

slide-16
SLIDE 16

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.16

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.31 Professor: SHRIDEEP PALLICKARA

The à relation

¨ Lamport called the partial ordering obtained by generalizing the

previous 2 relationships

¤ The happened-before or happens-before relation ¨ Sometimes also known as the relation of causal ordering or potential

causal ordering

September 3, 2019 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.32 Professor: SHRIDEEP PALLICKARA

Lamport’s logical clocks

September 3, 2019

¨ The happens-before relation è ¨ a and b are events in the process; and a occurs before b ¤ Then a è b is true ¨ a is event of message sent by one process;

b is event of message being received in another process

n Then a è b is true

slide-17
SLIDE 17

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.17

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.33 Professor: SHRIDEEP PALLICKARA

Some more things about the happens-before relation

September 3, 2019

¨ If a è b and b è c; then a è c ¤ Transitive ¨ If events x and y occur in processes that do not exchange messages,

then …

¤ x è y is not true ¤ But, neither is y è x ¤ These events are said to be concurrent

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.34 Professor: SHRIDEEP PALLICKARA

Events occurring at three processes

X

Y Z a c e f d b

q a à b and c à d

– These occur within the same process

q b à c and d à f

– Events that correspond to sending and receiving messages

q We can use transitivity to say a à f q No relationship between a and e; these are concurrent a || e

September 3, 2019

slide-18
SLIDE 18

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.18

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.35 Professor: SHRIDEEP PALLICKARA

If the à relation holds between two processes

September 3, 2019

¨ The first event might or might-not have caused the second ¤ The à relation only captures potential causality n i.e. two events can be related by à without a real connection between them ¨ EXAMPLE 1: If the server receives a request and sends a response? ¤ Then reply is caused by the request ¨ EXAMPLE 2: A process might receive a request and subsequently issue

another message

¤ But this could be one that it issues every 5 minutes anyway

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.36 Professor: SHRIDEEP PALLICKARA

A simple example of Lamport timestamps

X Y Z 1 3 1 5 4 2

September 3, 2019

slide-19
SLIDE 19

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.19

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.37 Professor: SHRIDEEP PALLICKARA

An example of Lamport’s algorithm:

6 12 18 24 30 36 42 48 54 60 8 16 24 32 40 48 56 64 72 80 10 20 30 40 50 60 70 80 90

100

m1 m2

Each clock runs at a constant (but different rate)

m3 m4

September 3, 2019

Each message carries the sending time according to the sender’sclock

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.38 Professor: SHRIDEEP PALLICKARA

An example of Lamport’s algorithm:

6 12 18 24 30 36 42 48 8 16 24 32 40 48 10 20 30 40 50 60 70 80 90

100

m1 m2

Each clock runs at a constant (but different rate)

m3 m4

61 69 77 85 70 76

September 3, 2019

Each message carries the sending time according to the sender’sclock

slide-20
SLIDE 20

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.20

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.39 Professor: SHRIDEEP PALLICKARA

Implementing Lamport’s clocks

① Before executing an event; Pi executes

Ci = Ci + 1

② When Pi sends a message m to Pj ; it sets m’s timestamp ts(m) to Ci

in previous step

③ Upon receipt of message m, Pj adjusts its own local counter

Cj = max {Cj, ts(m)} do step (1) and deliver message

September 3, 2019 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.40 Professor: SHRIDEEP PALLICKARA

The positioning of Lamport’s clocks in distributed systems

September 3, 2019

Middleware sends message MIDDLEWARE LAYER APPLICATION LAYER NETWORK LAYER Message is received Adjust local clock and timestamp message Adjust local clock Application sends message Message is delivered to application

slide-21
SLIDE 21

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.21

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.41 Professor: SHRIDEEP PALLICKARA

An application of Lamport’s clock: User has $1000 in bank account initially

REPLICATED DATABASE

San Francisco New York

Add $100 to account Update with 1% interest

Add $100 …. Total:$1100 Give 1% interest on total= $11

Balance: $1111

Give 1% interest … Total= $1010 Add $100

Balance: $1110

September 3, 2019 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.42 Professor: SHRIDEEP PALLICKARA

There is a difference when the orders are reversed

¨ Our objective for now is consistency ¨ Both copies must be exactly the same

September 3, 2019

slide-22
SLIDE 22

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.22

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.43 Professor: SHRIDEEP PALLICKARA

Use Lamport’s clock to order messages

September 3, 2019

¨ Process puts received messages into local queue ¤ Ordered according to the message’s timestamp ¨ Message can be delivered only if it is acknowledged by all the other

processes

¨ If a message is at the head of the queue, and acknowledged by all

processes

¤ It is delivered and processed

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.44 Professor: SHRIDEEP PALLICKARA

Lamport’s Clocks order events based on the happened-before relationship

¨ If a happened before b, then C(a) < C(b) ¨ But nothing can be said about two events a and b by merely

comparing their values

¨ C(a) < C(b)? ¤ Does not mean a happened before b

September 3, 2019

slide-23
SLIDE 23

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.23

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.45 Professor: SHRIDEEP PALLICKARA

Let’s look a little closer

¨ Tsnd(mi) : Time mi was sent ¨ Trcv(mi) : Time mi was received ¨ Tsnd(mi) < Trcv(mi) ¨ BUT ¤ Tsnd(mi) < Trcv(mj) ? n NO

September 3, 2019 CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.46 Professor: SHRIDEEP PALLICKARA

Concurrent message transmissions

6 12 18 24 30 36 42 48 70 76 8 16 24 32 40 48 61 69 77 85 10 20 30 40 50 60 70 80 90

100

m1 m3 m4 m5 m2

Sending m3 MAY HAVE depended on m1

Trcv(m1) < Tsnd(m2)

But sending of m2 has nothing to do with receipt of m1

Lamport clocks do not capture causality

September 3, 2019

slide-24
SLIDE 24

SLIDES CREATED BY: SHRIDEEP PALLICKARA L3.24

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

CS555: Distributed Systems [Fall 2019]

  • Dept. Of Computer Science, Colorado State University

L3.47 Professor: SHRIDEEP PALLICKARA

The contents of this slide-set are based on the following references

¨ Distributed Systems: Principles and Paradigms. Andrew S. Tanenbaum and Maarten Van

der Steen. 2nd Edition. Prentice Hall. ISBN: 0132392275/978-0132392273. [Chapter 6]

¨ Distributed Systems: Concepts and Design. George Coulouris, Jean Dollimore, Tim

Kindberg, Gordon Blair. 5th Edition. Addison Wesley. ISBN: 978-0132143011. [Chapter 14]

¨ http://en.wikipedia.org/wiki/Matrix_clocks September 3, 2019