synchronization chapter 5. table of contents clock synchronization logical clocks global state...

Post on 18-Jan-2016

251 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Synchronization

Chapter 5

Table of Contents

• Clock Synchronization

• Logical Clocks

• Global State

• Election Algorithms

• Mutual Exclusion

5.1 Clock Synchronization

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

Physical Clocks

Computation of the mean solar day.

Clock Synchronization Algorithms

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

Cristian's Algorithm

• A time server, a couple of clients• Each client sends a time request at T0

and receives the reply from the time server at T1

• (T1 - T0 - I)/2 for one-way propagation time

• A series of measurements

Cristian's Algorithm

• Getting the current time from a time server.

The Berkeley Algorithm

a) The time daemon asks all the other machines for their clock values

b) The machines answer

c) The time daemon tells everyone how to adjust their clock

Averaging Algorithm

• Decentralized clock synchronization Alg.

• Every machine broadcasts its time to others.

• Every machine collects all other broadcasts in some interval.

• Average the values.

5.2 Logical Clocks

• For a certain class of algorithms, it is the internal consistency of the clocks that matters, not whether they are particularly close to the real time.

• output.o, output.c

• Logical clocks

Lamport timestamps

• a happens-before b• If a and b are events in the same process

and a occurs before b, then a->b is true

• If a is the event of a message being sent by one process, and b is the event of the message being received by another process, then a->b is also true.

• Transitive

Lamport timestamps

• a and b are concurrent• !(a -> b) !(b->a) ∧• We can’t say which one happens first.

• C(x) is the value based on the time x

• Property: if a -> b then C(a) -> C(b).

Lamport timestamps

• Assign time to all events in DS:• If a->b in the same process, C(a) < C(b)

• If a and b represent the sending and receiving of a message, respectively, C(a) < C(b)

• For all distinctive events a and b, C(a) ≠ C(b)

Lamport Timestamps

Three processes, each with its own clock. The clocks run at different rates.

Lamport's algorithm corrects the clocks.

0 0 0

6 A 8 10

12 16 20

18 24 B 30

24 32 40

30 40 50

36 48 C 60

42 56 70

48 D 64 80

54 72 90

60 80 100

61

69

77

85

70

76

Example: Totally-Ordered Multicasting

• Updating a replicated database and leaving it in an inconsistent state.

Example: Totally-Ordered Multicasting

• A group of processes multicast messages to each other:• Each message is always timestamped with

the current time of its sender.

• When a process receives a message, it is put into a local queue, ordered by its timestamp, and multicasts an ACK to other processes

• Lamport’s alg

Example: Totally-Ordered Multicasting

• Assumptions:• Messages are sent to all processes

• Messages are never lost.

• Messages from the same sender are received in the order they were sent.

• Property:

• All processes will eventually have the same copy of the local queue.

5.3 Global State

• Global State of a DS:• Local state of each process

• Messages that are currently in transit

• Why is the global state important?

• Tracing-based GC

• Deadlock

5.3 Global State

• Distributed snapshot• Reflects a consistent global state

• If process P received a message from Q, then Q had actually sent that message;

• If Q has sent a message to P, then either the message is still in transit or P has received it.

• A cut represents the last event that has been recorded for each process.

Global State (1)

A consistent cut An inconsistent cut

Global State (1)

A consistent cut An inconsistent cut

Global State (2)

• Distributed snapshot Algorithm• Assumption: the DS is a collection of

processes connected to each other through unidirectional point-to-point communication channels.

Global State (2)

• Distributed snapshot Algorithm• Step

• Process P initiates the algorithm by first recording its own local state, then sends a marker along each of its outgoing channels.

• When a process Q receives a marker through an incoming channel C, • Record its own state if it has not done so, and

sends a marker along each of its outgoing channels

• Otherwise, records the channel state.

Global State (2)

a) Organization of a process and channels for a distributed snapshot

Global State (2)

b) Process Q receives a marker for the first time and records its local state

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

the state of the incoming channel

Global State (2)

• Distributed snapshot Algorithm• A process is said to have finished its part

of the algorithm when it has received a marker along each of its incoming channels and processed each one.

5.4 Election Algorithm

• A coordinator is required to perform some special role.

• Assumptions• All processes are exactly the same.• Each process has a unique number.• The process with the highest number wins.• Every process knows the process number of

others, but it does not know which one is up or down.

The Bully Algorithm (1)

The bully election algorithm• Process 4 holds an election• Process 5 and 6 respond, telling 4 to stop• Now 5 and 6 each hold an election

The Bully Algorithm (2)

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

A Ring Algorithm

Election algorithm using a ring.

5.5 Mutual Exclusion

• Multiple processes

• Shared data

• Mutual exclusion

• Critical regions are protected by:

• semaphores

• monitors

A Centralized Algorithm

a) Process 1 asks the coordinator for permission to enter a critical region. Permission is grantedb) 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, when then replies to 2

A Centralized Algorithm

• Problems:• If the coordinator is dead, the entire system

may go down.

• If processes normally block after making a request, there is no way to distinguish a dead coordinator from “permission denied”.

• A possible bottleneck.

A Distributed Algorithm

• Description• When a process wants to enter a critical

region, it builds a message to all others including its timestamp

• When a process receives a request, • If it is not currently in the CR and does not

want to enter it, it sends “OK” back.• If it is already in CR, it queues the request• If it is preparing to enter the CR, it compares

the timestamp with its own, the lower one wins. It sends “OK” or queues the request.

A Distributed Algorithm

• Description• When “OK”s come from all processes, the

process can enter the CR.

• When a process exits the CR, it sends the “OK” to all processes on its queue and deletes them all from the queue.

A Distributed Algorithm

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

region.

A Distributed Algorithm

• Problems• The number of messages required per

entry is now 2(n-1), where n is the # of processes.

• The single point of failure has been replaced by n points of failure.

• All processes are involved in all decisions concerning entry into the CR, still bottleneck.

A receiver always sends a message back, either granting or denying the permission.

A process is allowed to enter a CR when it gets permission from the majority. Each process can only grant one permission at a time.

A Toke Ring Algorithm

• Description• A logical ring is constructed in which each

process is assigned a position in the ring.

• When the ring is initialized, process 0 is given a token.

• The token circulates around the ring.

• A process can enter the CR if it gets the token.

A Toke Ring Algorithm

a) An unordered group of processes on a network.

b) A logical ring constructed in software.

A Toke Ring Algorithm

• Problems• The token could be lost and detecting

whether this situation is difficult.

• A process may crash.

Comparison

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

5.6 Distributed Transactions

• Transactions also protect a shared resource against simultaneous access by several concurrent processes.

• They allow a process to access and modify multiple data items as a single atomic operation.

The Transaction Model (1)

Updating a master tape is fault tolerant.

The Transaction Model (1)

• If a run failed for some reason, all the tapes could be rewound and the job started with no harm done.

• All-or-nothing property of a transaction. It is primitive.

You are disconnected to the

internet.

The Transaction Model (2)

• An online banking application:1. Withdraw an amount a from your

checking account;

2. Deposit amount a to your saving account.

Programming with Transactions

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

Programming with Transactions

a) Transaction to reserve three flights commitsb) Transaction aborts when third flight is unavailable

BEGIN_TRANSACTION reserve Nanjing -> Shanghai; reserve Shanghai -> Chicago; reserve Chicago-> Milwaukee;END_TRANSACTION

(a)

BEGIN_TRANSACTION reserve Nanjing -> Shanghai; reserve Shanghai -> Chicago; reserve Chicago-> Milwaukee full =>ABORT_TRANSACTION (b)

Transaction properties: ACID

• Atomic: To the outside world, the transaction happens indivisibly.

• Consistent: The transaction does not violate system invariants.

• Isolated /serializable: Concurrent transactions do not interfere with each other.

• Durable: Once a transaction commits, the changes are permanent.

Classification of Transaction

• Flat transaction

• Nested transaction• A transaction can recursively fork off child

transactions running in parallel on different machines

• Private copies of all data

• Distributed Transaction• A (flat) subtransaction operates on data

distributed across multiple machines.

Distributed Transactions

a) A nested transactionb) A distributed transaction

Transaction Implementation

• Private Workspace• A private workspace is given at the instant

a transaction begins.

• Cost of copying everything.

Private Workspace

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

Transaction Implementation

• Writeahead Log• Files are actually modified in place, but

before any block is changed, a record is written to a log telling

• which transaction is making the changes.

• which file and block is being changed.

• what the old and new values are.

Writeahead Log

• a) A transaction• b) – d) The log before each statement is executed

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)

Concurrency Control

• Goal• Allow several transactions to be executed

simultaneously, but in such a way that the collection of data is left in a consistent state.

• This consistency is achieved by giving transactions access to data items in a specific order whereby the final result is the same as if all transaction had run sequentially.

Concurrency Control (1)

General organization of managers for handling transactions.

Concurrency Control (2)

General organization of managers for handling distributed transactions.

Serializability

• The main purpose of concurrency control algorithms is to guarantee that multiple transactions can be executed simultaneously while still being isolated at the same time.

• The final result should be the same as if the transactions were executed one after the other in some specific order.

Serializability

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

• d) Possible schedules

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)

Serializability

• Scheduling the conflicting operations is important in the concurrency control:• Read-write conflict

• Write-write conflict

• Synchronization• Mutual exclusion on shared data

• Ordering operations using timestamps

Serializability

• Two approaches of the concurrency control:• Pessimistic approaches

• If something can go wrong, it will• Operations are synchronized before they are

carried out

• Optimistic approaches• Nothing will go wrong and no

synchronization.• If conflicts happened, then abort transactions.

Homework #2 due 12/10, 2pm

• Chapter 4 question 21, 22:• Is it possible in generation reference counting

that an entry G[i] becomes less than 0?• In reference listing, if no response is received

after sending a ping message to process P, the process is removed from the object’s reference list. Is it always correct to remove the process?

• Chapter 5 question 18, 7:• Systems that use locking for concurrency control

usually distinguish read locks from write locks. What should happen if a process (see the next page)

Homework #2 due 12/10, 2pm

has already acquired a read lock and now wants to change it into a write lock? What about changing a write lock into a read lock?

• In Fig. 5-12 (or page 31 of this slide) we have two ELECTION messages circulating simultaneously. While it does no harm to have two of them, it would be more elegant if one could be killed off. Devise an algorithm for doing this without affecting the operation of the basic election algorithm.

top related