26
Synchronization

Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

Embed Size (px)

Citation preview

Page 1: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

Synchronization

Page 2: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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.

Page 3: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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)

Page 4: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 5: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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 )

Page 6: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 7: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

•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.

Page 8: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 9: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 10: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 11: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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.

Page 12: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 13: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

The Bully Algorithm (2/2)

Page 14: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

A Ring Algorithm

Election algorithm using a ring (without token).

Each process knows who its successor is.

Page 15: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 16: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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…)

Page 17: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 18: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 19: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 20: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 21: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 22: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 23: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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

Page 24: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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.

Page 25: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

Concurrency Control

General organization of managers for

handling distributed transactions.

Page 26: Synchronization. Clock Synchronization In a centralized system time is unambiguous. In a distributed system agreement on time is not obvious. When each

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)