37
Distributed Systems CS 15-440 Synchronization – Part II Lecture 8, Sep 28, 2011 Majd F. Sakr, Vinay Kolar, Mohammad Hammoud

Distributed Systems CS 15-440

  • Upload
    magar

  • View
    42

  • Download
    0

Embed Size (px)

DESCRIPTION

Distributed Systems CS 15-440. Synchronization – Part II Lecture 8, Sep 28, 2011 Majd F. Sakr, Vinay Kolar , Mohammad Hammoud. Today…. Last Session Synchronization: Clock Synchronization and Cristian’s Algorithm Today’s session Quiz (20 minutes) Synchronization - PowerPoint PPT Presentation

Citation preview

Page 1: Distributed Systems CS 15-440

Distributed SystemsCS 15-440

Synchronization – Part IILecture 8, Sep 28, 2011

Majd F. Sakr, Vinay Kolar, Mohammad Hammoud

Page 2: Distributed Systems CS 15-440

Today…

Last Session Synchronization: Clock Synchronization and Cristian’s Algorithm

Today’s session Quiz (20 minutes) Synchronization

Clock Synchronization: Berkeley’s Algorithm and NTP Logical Clocks: Lamport’s Clock, Vector Clocks

Announcements Assignment 2 posted (Due Oct 13th) Assignment 1 will be graded by Monday Project 1 due on Oct 3rd

Page 3: Distributed Systems CS 15-440

Time SynchronizationPhysical Clock Synchronization (or, simply, Clock Synchronization)

Here, actual time on the computers are synchronizedLogical Clock Synchronization

Computers are synchronized based on the relative ordering of events

Mutual ExclusionHow to coordinate between processes that access the same resource?

Election AlgorithmsHere, a group of entities elect one entity as the coordinator for solving a problem

Where do We Stand in Synchronization Chapter?Previous lecture

Next lecture

Toda

y’s

lect

ure

Page 4: Distributed Systems CS 15-440

Types of Time Synchronization

Computer 1

Computer 2

Computer 3

Computer 4

Clo

cks

are

sync

hron

ized

ove

r th

e ne

twor

k

Computer 1

Computer 2

Computer 3

Computer 4Lo

gica

l Clo

cks

are

sync

hron

ized

ov

er th

e ne

twor

k on

ly w

hen

an

even

t occ

urs

00

00

00

00

00

01

01

01

01

01

02

02

02

02

0203

03

03

03

Clock-based Time Synchronization

Event-based Time Synchronization

Page 5: Distributed Systems CS 15-440

OverviewTime Synchronization

Clock SynchronizationLogical Clock Synchronization

Mutual Exclusion

Election Algorithms

Page 6: Distributed Systems CS 15-440

Clock Synchronization

Coordinated Universal TimeTracking Time on a ComputerClock Synchronization Algorithms

Cristian’s AlgorithmBerkeley AlgorithmNetwork Time Protocol

Page 7: Distributed Systems CS 15-440

Berkeley Algorithm

Approach:1. A time server periodically (approx. once in 4

minutes) sends its time to all the computers and polls them for the time difference

2. The computers compute the time difference and then reply

3. The server computes an average time difference for each computer

4. The server commands all the computers to update their time (by gradual time synchronization)

3:00

3:252:50

Time server

3:003:00

-0:10 +0:25

+0:00 +0:05

+0:15

-0:20

Berkeley Algorithm is a distributed approach for time synchronization

3:05 3:05

3:05

Page 8: Distributed Systems CS 15-440

Berkeley Algorithm – Discussion1. Assumption about packet transmission delays

• Berkeley’s algorithm predicts network delay (similar to Cristian’s algorithm)• Hence, it is effective in intranets, and not accurate in wide-area networks

2. No UTC Receiver is necessary

• The clocks in the system synchronize by averaging all the computer’s times

3. Decreases the effect of faulty clocks

• Fault-tolerant averaging, where outlier clocks are ignored, can be easily performed in Berkeley Algorithm

4. Time server failures can be masked

• If a time server fails, another computer can be elected as a time server

Page 9: Distributed Systems CS 15-440

Clock Synchronization

Coordinated Universal TimeTracking Time on a ComputerClock Synchronization Algorithms

Cristian’s AlgorithmBerkeley AlgorithmNetwork Time Protocol

Page 10: Distributed Systems CS 15-440

Network Time Protocol (NTP)NTP defines an architecture for a time service and a protocol to distribute time information over the Internet

In NTP, servers are connected in a logical hierarchy called synchronization subnet

The levels of synchronization subnet is called strataStratum 1 servers have most accurate time information (connected to a UTC receiver)Servers in each stratum act as time servers to the servers in the lower stratum

Page 11: Distributed Systems CS 15-440

Hierarchical organization of NTP Servers

Page 12: Distributed Systems CS 15-440

Operation of NTP ProtocolWhen a time server A contacts time server B for synchronization

If stratum(A) <= stratum(B), then A does not synchronize with BIf stratum(A) > stratum(B), then:

Time server A synchronizes with B An algorithm similar to Cristian’s algorithm is used to synchronize. However, larger statistical samples are taken before updating the clockTime server A updates its stratumstratum(A) = stratum(B) + 1

Page 13: Distributed Systems CS 15-440

Discussion of NTP Design

Page 14: Distributed Systems CS 15-440

Summary of Clock Synchronization

Physical clocks on computers are not accurate

Clock synchronization algorithms provide mechanisms to synchronize clocks on networked computers in a DS

Computers on a local network use various algorithms for synchronization

Some algorithms (e.g, Cristian’s algorithm) synchronize time with by contacting centralized time serversSome algorithms (e.g., Berkeley algorithm) synchronize in a distributed manner by exchanging the time information on various computers

NTP provides architecture and protocol for time synchronization over wide-area networks such as Internet

Page 15: Distributed Systems CS 15-440

OverviewTime Synchronization

Clock SynchronizationLogical Clock Synchronization

Mutual Exclusion

Election Algorithms

Page 16: Distributed Systems CS 15-440

Why Logical Clocks?Lamport (in 1978) showed that:

Clock synchronization is not necessary in all scenariosIf two processes do not interact, it is not necessary that their clocks are synchronized

Many times, it is sufficient if processes agree on the order in which the events has occurred in a DS

For example, for a distributed make utility, it is sufficient to know if an input file was modified before or after its object file

Page 17: Distributed Systems CS 15-440

Logical Clocks

Logical clocks are used to define an order of events without measuring the physical time at which the events occurred

We will study two types of logical clocks1. Lamport’s Logical Clock (or simply, Lamport’s Clock)2. Vector Clock

Page 18: Distributed Systems CS 15-440

Logical Clocks

We will study two types of logical clocks1. Lamport’s Clock2. Vector Clock

Page 19: Distributed Systems CS 15-440

Lamport’s Logical ClockLamport advocated maintaining logical clocks at the processes to keep track of the order of events

To synchronize logical clocks, Lamport defined a relation called “happened-before”

The expression ab (read as “a happened before b”) means that all entities in a DS agree that event a occurred before event b

Page 20: Distributed Systems CS 15-440

Happened-before RelationThe happened-before relation can be observed directly in two situations:

1. If a and b are events in the same process, and a occurs before b, then ab is true

2. If a is an event of message m being sent by a process, and b is the event of the message m being received by another process, the ab is true.

1. The happened-before relation is transitiveIf ab and bc, then ac

Page 21: Distributed Systems CS 15-440

Time values in Logical Clocks

For every event a, assign a logical time value C(a) on which all processes agree

Time value for events have the property thatIf ab, then C(a)< C(b)

Page 22: Distributed Systems CS 15-440

Properties of Logical Clock

From happened-before relation, we can infer that:

If two events a and b occur within the same process and ab, then assign C(a) and C(b) such that C(a) < C(b)

If a is the event of sending the message m from one process, and b is the event of receiving the message m, then

the time values C(a) and C(b) are assigned such that all processes agree that C(a) < C(b)

The clock time C must always go forward (increasing), and never backward (decreasing)

Page 23: Distributed Systems CS 15-440

Synchronizing Logical Clocks

Three processes P1, P2 and P3 running at different rates

If the processes communicate between each other, there might be discrepancies in agreeing on the event ordering

Ordering of sending and receiving messages m1 and m2 are correct

However, m3 and m4 violate the happens-before relationship

06121824303642485460

08

162432404856647280

0102030405060708090100

P1 P2 P3

m1

m2

m3

m4

Page 24: Distributed Systems CS 15-440

Lamport’s Clock AlgorithmWhen a message is being sent:

Each message carries a timestamp according to the sender’s logical clock

When a message is received:If the receiver logical clock is less than message sending time in the packet, then adjust the receiver’s clock such that

currentTime = timestamp + 1

06

121824303642485460

08

162432404856647280

0102030405060708090100

P1 P2 P3

m3:60

61m4:69

54697785

7076

Page 25: Distributed Systems CS 15-440

Logical Clock Without a Physical Clock

Previous examples assumed that there is a physical clock at each computer (probably running at different rates)

How to attach a time value to an event when there is no global clock?

Page 26: Distributed Systems CS 15-440

Implementation of Lamport’s ClockEach process Pi maintains a local counter Ci and adjusts this counter according to the following rules:

1. For any two successive events that take place within Pi, Ci is incremented by 1

2. Each time a message m is sent by process Pi , the message receives a timestamp ts(m) = Ci

3. Whenever a message m is received by a process Pj, Pj adjusts its local counter Cj to max(Cj, ts(m)) + 1

P0

P1

P2

C0=1 C0=2C0=0

C1=0

C2=0

m:2 C1=3

Page 27: Distributed Systems CS 15-440

Placement of Logical ClockIn a computer, several processes use Logical Clocks

Similar to how several processes on a computer use one physical clock

Instead of each process maintaining its own Logical Clock, Logical Clocks can be implemented as a middleware for time service

Network layer

Application layer

Adjust local clock and timestamp message Adjust local clock

Application sends a message

Middleware sends a message

Message is received

Message is delivered to the application

Page 28: Distributed Systems CS 15-440

Limitation of Lamport’s ClockLamport’s Clock ensures that if ab, then C(a) < C(b)However, it does not say anything about any two events a and b by comparing their time values

For any two events a and b, C(a) < C(b) does not mean that ab

Example:

06

121824303642485460

08

162432404856647280

0102030405060708090

100

P1 P2 P3

61

54

m1:6m2:20

m3:32

Compare m1 and m3

P2 can infer that m1m3

Compare m1 and m2

P2 cannot infer that m1m2 or m2m1

Page 29: Distributed Systems CS 15-440

Summary of Lamport’s ClockLamport advocated using logical clocks

Processes synchronize based on their time values of the logical clock rather than the absolute time on the physical time

Which applications in DS need logical clocks? Applications with provable ordering of events

Perfect physical clock synchronization is hard to achieve in practice. Hence we cannot provably order the events

Applications with rare eventsEvents are rarely generated, and physical clock synchronization overhead is not justified

However, Lamport’s clock cannot guarantee perfect ordering of events by just observing the time values of two arbitrary events

Page 30: Distributed Systems CS 15-440

Logical Clocks

We will study two types of logical clocks1. Lamport’s Clock2. Vector Clocks

Page 31: Distributed Systems CS 15-440

Vector ClocksVector Clocks was proposed to overcome the limitation of Lamport’s clock: the fact that C(a)<C(b) does not mean that ab

The property of inferring that a occurred before b is called as causality property

A Vector clock for a system of N processes is an array of N integers

Every process Pi stores its own vector clock VCi

Lamport’s time value for events are stored in VCi

VCi(a) is assigned to an event a

If VCi(a) < VCi(b), then we can infer that ab

Page 32: Distributed Systems CS 15-440

Updating Vector Clocks

Vector clocks are constructed by the following two properties:

1. VCi[i] is the number of events that have occurred at process Pi so far

VCi[i] is the local logical clock at process Pi

2. If VCi[j]=k, then Pi knows that k events have occurred at Pj

VCi[j] is Pi’s knowledge of local time at Pj

Increment VCi whenever a new event occurs

Pass VCj along with the message

Page 33: Distributed Systems CS 15-440

Whenever there is a new event at Pi, increment VCi[i]When a process Pi sends a message m to Pj:

Increment VCi[i]Set m’s timestamp ts(m) to the vector VCi

When message m is received process Pj :VCj[k] = max(VCj[k], ts(m)[k]) ; (for all k)Increment VCj[j]

Vector Clock Update Algorithm

P0

P1

P2

VC0=(1,0,0) VC0=(2,0,0)VC0=(0,0,0)

VC1=(0,0,0)

VC2=(0,0,0)

m:(2,0,0) VC1=(2,1,0)

Page 34: Distributed Systems CS 15-440

Inferring Events with Vector Clocks

Let a process Pi send a message m to Pj with timestamp ts(m), then:

Pj knows the number of events at the sender Pi that causally precede m

(ts(m)[i] – 1) denotes the number of events at Pi

Pj also knows the minimum number of events at other processes Pk that causally precede m

(ts(m)[k] – 1) denotes the minimum number of events at Pk

P0

P1

P2

VC0=(1,0,0) VC0=(2,0,0)VC0=(0,0,0)

VC1=(0,0,0)

VC2=(0,0,0)

m:(2,0,0) VC1=(2,2,0)

VC2=(2,3,1)m’:(2,3,0)

VC1=(2,3,0)VC1=(0,1,0)

Page 35: Distributed Systems CS 15-440

Summary – Logical Clocks

Logical Clocks are employed when processes have to agree on relative ordering of events, but not necessarily actual time of events

Two types of Logical ClocksLamport’s Logical Clocks

Supports relative ordering of events across different processes by using happen-before relationship

Vector ClocksSupports causal ordering of events

Page 36: Distributed Systems CS 15-440

Next ClassMutual Exclusion

How to coordinate between processes that access the same resource?

Election AlgorithmsHere, a group of entities elect one entity as the coordinator for solving a problem

Page 37: Distributed Systems CS 15-440

Referenceshttp://en.wikipedia.org/wiki/Causality