Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed...

Preview:

Citation preview

Synchronization

Clock SynchronizationIn a centralized system time is unambiguous.

In a distributed system agreement on time is not obvious.

When each machine has its own clock, an event that occurred after another event may be assigned an earlier time.

Physical clocks

Computer Clock ≡ Timer• Oscillating crystal (under tension)• 2 register: counter and holding register• Interrupt ( clock tick) when counter gets to zero• N clocks n rates (clock skew)

UTC (Universal Coordinated Time) is the current universal time standard Radio pulse to synchronize (±10 msec) Satellite (± 0.5 msec)

Clock Synchronization Algorithms

The relation between clock time and UTC when clocks tick at different rates.

If 1 – dC/dt < 1 + maximum drift rateIf 2 clock drift from UTC in the opposite directions, after Δt,

ε = 2t

Resynchronization interval maximum time difference

Cristian's Algorithmsynchronization with a time server

Periodically (T < ssynchronizationrequest to time server

• Time must never run backward gradual changes• Non zero message propagation time (estimate value = (T1-T0-I) /2 )

The Berkeley Algorithmsynchronization without time server

a) The time daemon asks all the other machines for their clock value discrepanciesb) The machines answerc) The time daemon tells everyone how to adjust their clock to the average no Universal Coordinated Time available

•Centralized algorithms have disadvantages.

•Decentralized algorithms can use averaging methods.

•NTP (Network Time Protocol) provides an accuracy of 1-50 msec using advanced algorithms.

•For many purposes it is sufficient that all machines agree on the same time.

Logical clocks

Often processes need to agree on the order in which events occur.

Lamport TimestampsIf a b C(a) < C(b)

If a snd., b rcv. C(a) < C(b) C(a) C(b)

If C(b) < C(a) C(b) = C(a)+1

We obtain a total ordering of all events in the system but Lamport timestamps don’t capture causality

Vector timestampseach process Pi has a vector Vi so that:

Vi[i] is the number of events occurred so far at Pi

If Vi[j]=k Pi knows k events occurred at Pj

happens before

Global StateIt is the local state of each process, together with the messages currently in transit in a

distributed system

A distributed snapshot reflects a consistent global state

Global State

Organization of a process and channels for a distributed snapshot

a) A process P sends a marker along each of its outgoing channels

Knowledge of the state in which a distributed system currently is, is useful

Any process P may initiate the algorithm recording its local state.

P

Global State

b) Process Q receives a marker for the first time, records its local state and send a marker along each outgoing channel

c) Q records all incoming messaged) Q receives a marker for its incoming channel and finishes recording the

state of the incoming channelA process has finished its part of the algorithm when it has received a markeralong each of its incoming channels, and processed each one. Then its state is

collected

Many snapshots may be in progress at the same time.

Election AlgorithmsThe Bully Algorithm (1/2)

The bully election algorithma) Process 4 holds an electionb) Process 5 and 6 respond, telling 4 to stopc) Now 5 and 6 each hold an election

Selecting a coordinator

d) Process 6 tells 5 to stope) Process 6 wins and tells everyone

The Bully Algorithm (2/2)

A Ring Algorithm

Election algorithm using a ring (without token).

Each process knows who its successor is.

Mutual Exclusion: critical regions in distributed systems

A CentralizedCentralized Algorithm(to simulate a single processor system, needs a coordinator)

a) Process 1 asks the coordinator for permission to enter a critical region. Permission is granted

b) Process 2 then asks permission to enter the same critical region. The coordinator does not reply.

c) When process 1 exits the critical region, it tells the coordinator, which then replies to 2

A Distributed Distributed Algorithmrequires a total ordering of all events in the system

A message contains the critical region name, the process number and the current timecurrent time

a) Two processes (0,2) want to enter the same critical region at the same moment.b) Process 0 has the lowest timestamp, so it wins and enters the critical region. c) When process 0 is done, it sends an OK also, so 2 can now enter the critical

region. This algorithm is worse than the centralized one (n points of failure, scaling,

multiple messages…)

A Token Ring Algorithmwhen the process acquires the token, it accesses the critical region (if needed)

a) An unordered group of processes on a network. b) A logical, ordered, ring constructed in software. Each process knows who is the next in line

start

Mutual Exclusion

A comparison of three mutual exclusion algorithms.

AlgorithmMessages per

entry/exitDelay before entry * (in message times)

Problems

Centralized 3 2 Coordinator crash

Distributed 2 ( n – 1 ) 2 ( n – 1 )Crash of any process

Token ring 1 to 0 to n – 1Lost token, process crash

* With dependence on the mechanism

The Transaction Model A transaction permits that a whole set of related instructions

would be successfully completed or none would be completed.Special primitives are requested ( supplied by system or language)

Examples of primitives for transactions.

Primitive Description

BEGIN_TRANSACTION Make the start of a transaction

END_TRANSACTION Terminate the transaction and try to commit

ABORT_TRANSACTION Kill the transaction and restore the old values

READ Read data from a file, a table, or otherwise

WRITE Write data to a file, a table, or otherwise

The Transaction Model

Transactions are ACID:

• Atomic : to outside, they happen indivisibly

• Consistent : they don’t violate system invariants

• Isolated : concurrent transactions don’t interfere

• Durable : the changes are permanent

These flat transactions have some limitations...

• No partial result is permitted

• In a large system they can take a lot of time

Nested and Distributed Transactions

a) A nested transaction: it follows a logical (hierarchical) division of the work

b) A distributed transaction: it is logically flat and operates on distributed data

How to implement transactions?

Private Workspace

Copying everything in a private workspace is prohibitive…

Example: dealing with a file system:a) The file index and disk blocks for a three-block fileb) The situation after a transaction has modified block 0 and appended block 3c) After committing

Write-ahead Log

Files are actually modified in place, but a record is written to log all the changesa) A transactionb) – d) The log before each statement is executed

In distributed systems each machine keeps its own log of changes to its local file system

x = 0;

y = 0;

BEGIN_TRANSACTION;

x = x + 1;

y = y + 2

x = y * y;

END_TRANSACTION;

(a)

Log

[x = 0 / 1]

(b)

Log

[x = 0 / 1]

[y = 0/2]

(c)

Log

[x = 0 / 1]

[y = 0/2]

[x = 1/4]

(d)

rollback

Concurrency Control allows several transaction to be executed simultaneously, leaving data in a

consistent way. Transactions access data in a specific order

General (layered) organization of managers for handling transactions.

Concurrency Control

General organization of managers for

handling distributed transactions.

Serializability

a) – c) Three transactions T1, T2, and T3

Legal serialized result : x= 1,2,3 depending upon which one runs lastd) Possible schedulesSynchronization can be achieved or with mutex or with explicit timestamp and

following pessimistic or optimistic approach.

BEGIN_TRANSACTION x = 0; x = x + 1;END_TRANSACTION

(a)

BEGIN_TRANSACTION x = 0; x = x + 2;END_TRANSACTION

(b)

BEGIN_TRANSACTION x = 0; x = x + 3;END_TRANSACTION

(c)

Schedule 1 x = 0; x = x + 1; x = 0; x = x + 2; x = 0; x = x + 3 Legal

Schedule 2 x = 0; x = 0; x = x + 1; x = x + 2; x = 0; x = x + 3; Legal

Schedule 3 x = 0; x = 0; x = x + 1; x = 0; x = x + 2; x = x + 3; Illegal

(d)

Recommended