Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State...

63
Synchronization Chapter 5

Transcript of Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State...

Page 1: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Synchronization

Chapter 5

Page 2: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Table of Contents

• Clock Synchronization

• Logical Clocks

• Global State

• Election Algorithms

• Mutual Exclusion

Page 3: 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.

Page 4: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Physical Clocks

Computation of the mean solar day.

Page 5: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Clock Synchronization Algorithms

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

Page 6: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 7: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Cristian's Algorithm

• Getting the current time from a time server.

Page 8: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 9: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 10: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 11: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 12: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 13: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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)

Page 14: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 15: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Example: Totally-Ordered Multicasting

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

Page 16: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 17: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 18: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 19: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 20: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Global State (1)

A consistent cut An inconsistent cut

Page 21: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Global State (1)

A consistent cut An inconsistent cut

Page 22: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 23: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 24: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Global State (2)

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

Page 25: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 26: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 27: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 28: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 29: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

The Bully Algorithm (2)

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

Page 30: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

A Ring Algorithm

Election algorithm using a ring.

Page 31: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

5.5 Mutual Exclusion

• Multiple processes

• Shared data

• Mutual exclusion

• Critical regions are protected by:

• semaphores

• monitors

Page 32: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 33: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 34: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 35: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 36: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 37: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 38: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 39: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

A Toke Ring Algorithm

a) An unordered group of processes on a network.

b) A logical ring constructed in software.

Page 40: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

A Toke Ring Algorithm

• Problems• The token could be lost and detecting

whether this situation is difficult.

• A process may crash.

Page 41: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 42: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 43: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

The Transaction Model (1)

Updating a master tape is fault tolerant.

Page 44: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 45: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 46: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 47: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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)

Page 48: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 49: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 50: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Distributed Transactions

a) A nested transactionb) A distributed transaction

Page 51: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Transaction Implementation

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

a transaction begins.

• Cost of copying everything.

Page 52: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 53: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 54: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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)

Page 55: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 56: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Concurrency Control (1)

General organization of managers for handling transactions.

Page 57: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

Concurrency Control (2)

General organization of managers for handling distributed transactions.

Page 58: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 59: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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)

Page 60: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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

Page 61: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.

Page 62: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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)

Page 63: Synchronization Chapter 5. Table of Contents Clock Synchronization Logical Clocks Global State Election Algorithms Mutual Exclusion.

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.