1 chapter 3. synchronization. stempusan national university stem-pnu 2 synchronization in...

31
1 Chapter 3. Synchronization

Upload: stewart-blake

Post on 16-Dec-2015

224 views

Category:

Documents


1 download

TRANSCRIPT

1

Chapter 3. Synchronization

STEMPusan National University

STEM-PNU

2

Synchronization in Distributed Systems

Synchronization in a single machine Same clock Shared data for synchronization

Semaphore for mutual exclusion

Synchronization in distributed system Difficult to handle it No same physical clock No shared data for synchronization

Machine 1 Machine 2

Semaphore

Remote Access : Delay and Error

STEMPusan National University

STEM-PNU

3

Clock

In distributed systems Multiple Physical Clocks : No Centralized Clock

Logical Clock rather than a Global Physical Clock

STEMPusan National University

STEM-PNU

4

Global Clock and Logical Clock

Global Clock TAI (Temps Atomique International) at Paris Time Server Broadcasting from Satellite Granularity

Logical Clock Not absolute time but for ordering Lamport Algorithm

Correction of Clock T(A, tx) < T(B, rx)

STEMPusan National University

STEM-PNU

5

Logical Clock

Logical Clock Not absolute time but for ordering Lamport Algorithm

Correction of Clock C(A, tx) < C(B, rx)

12345

56789

1011121314

678

151617

101112

910

1314

1819

12345

56789

1011121314

678

151617

101617

1819

1819

1819

STEMPusan National University

STEM-PNU

6

Global Clock and Time Server

Global Clock TAI (Temps Atomique International) at Paris Time Server Broadcasting from Satellite Granularity

Global Clock Server Adjust local clock

Cristian’s Algorithm Berkeley Algorithm

Time never runsbackward

STEMPusan National University

STEM-PNU

7

Global State

Global State States of each local system Messages in transit Very important to determine the status of distributed system

Example : Deadlock Detection

Distributed Snapshot : State of distributed systems in a given time

STEMPusan National University

STEM-PNU

8

Mutual Exclusion : Monitor (Coordinator)

In a single system : Easy to implement by semaphore In distributed systems : No shared data for semaphore A Centralized Algorithm

Simple But single point of failure

STEMPusan National University

STEM-PNU

9

Mutual Exclusion : Distributed Algorithm

No central coordinator Rule : When a system receive a request

Not in CS or Not to enter : OK to requester In CS : No reply To enter : compare Timestamp and one with lower TS wins

STEMPusan National University

STEM-PNU

10

Mutual Exclusion : Token Ring

One with token can enter CS If a system wants to enter CS :

wait Token and process it Otherwise, pass token to the next

Token

Wait until

it gets the token

STEMPusan National University

STEM-PNU

11

Mutual Exclusion : Comparison

# of messagesper request

delay

per request

Problem

Monitor 3 2 Crash of

monitor

DistributedAlgorithm

2(n-1) 2(n-1) n points of

Crash

Token Ring 0 to n-1 0 to n-1 Token lost

STEMPusan National University

STEM-PNU

12

Election : Bully Algorithm

When it is found that a coordinator is required

STEMPusan National University

STEM-PNU

13

Election : Ring Algorithm

When it is found that the coordinator has crashed, Circulate message with priority

STEMPusan National University

STEM-PNU

14

Distributed Transaction

Transaction

Example : Flight Reservation

Consistent

State

Consistent

State

Set of operations

BEGIN_TRANSACTION reserve WP -> JFK; reserve JFK -> Nairobi; reserve Nairobi -> Malindi;END_TRANSACTION

(a)

BEGIN_TRANSACTION reserve WP -> JFK; reserve JFK -> Nairobi; reserve Nairobi -> Malindi full =>ABORT_TRANSACTION (b)

STEMPusan National University

STEM-PNU

15

ACID Properties : Atomicity and Consistency

Atomicity. All or Nothing Not Partially Done Example : Failure in Flight Reservation

Consistency. Execution of a transaction preserves the consistency of the database.

State 1 State 2

All

Nothing

State 2’PartiallyDone

Consistent

Consistent

STEMPusan National University

STEM-PNU

16

Transaction States

Active

PartiallyCommitted

Failed Aborted

Committedthe initial state; the transaction stays in this state while it is

executing

after the discovery that normal execution can no longer proceed.

after the transaction has been rolled back and the database restored to its state prior to the start of the transaction.

- restart the transaction or - kill the transaction

ALL

NOTHING

STEMPusan National University

STEM-PNU

17

ACID Properties : Isolation and Durability

Isolation. Although multiple transactions may execute concurrently, each

transaction must be unaware of other concurrently executing transactions.

Intermediate transaction results must be hidden from other concurrently executed transactions.

Durability. After a transaction completes successfully, the changes it has made to

the database persist, even if there are system failures.

DB

Transaction 1

Transaction 2

No Effect

STEMPusan National University

STEM-PNU

18

Example

Transaction : Transfer $50 from account A to account B:1. read(A)

2. A := A – 50

3. write(A)

4. read(B)

5. B := B + 50

6. write(B)

Consistency requirement the sum of A and B is unchanged after the transaction.

Atomicity requirement Durability Isolation

STEMPusan National University

STEM-PNU

19

Example : Concurrent Execution

Two Transactions T1 : transfer $50 from A to B,

T2 transfer 10% of the balance from A to B

Serial Schedule Concurrent Schedule

STEMPusan National University

STEM-PNU

20

Serializability

What happens after these transactions ? Serial Schedule :

Always Correct T1 T2 and T2 T1

Concurrent Schedule Serializable if

Result (T1||T2) = Result(T1 T2) or

Result(T2 T1)

STEMPusan National University

STEM-PNU

21

Transaction : Atomicity

Failure during transaction Abort the partial execution How to discard incomplete transaction

Deferred updates Immediate updates

Safe transaction Decrease failure probability

Rollback Recovery

Return to initial state Without minimum cost

STEMPusan National University

STEM-PNU

22

Safe Transaction

Decrease the possibility of failure

Two Phase Commit Wedding Ceremony Protocol 1st Phase : send ready message 2nd Phase : execute only when OK messages are received from

all

STEMPusan National University

STEM-PNU

23

Log

Log History of operations Used for rollback

(Transaction#, Pre-state, Post-state) (Trans#, DataItem, OldValue, NewValue)

Transaction

operation1

operation3

operation2 SystemSystem

log log log

STEMPusan National University

STEM-PNU

24

Deferred Updates

Reflect Logs after Commit Shadowing

If the transaction fails, then discard logs

Transaction

operation1

operationn

. . .

System

Begin

Commit

Write or update operations

log log log

STEMPusan National University

STEM-PNU

25

(T1, begin)(T1, A, 20, 30)(T2, begin)(T1, B, 30, 40)(T2, B, 40, 50)(T1, commit)(T3, begin)(T2, C, 100, 50)(T2, commit)(T3, D, 10, 20)

Delete them

OK

Deferred Updates : Recovery

Log File

Fail Here

1. Transaction with commit: OK2. Transaction with begin but without commit :

Delete log records

STEMPusan National University

STEM-PNU

26

Transaction

Begin

Commit

Immediate Updates

Execute operation immediately

If the transaction fails, undo operations

operation1

operation3

operation2 System

log log log

STEMPusan National University

STEM-PNU

27

Which one is better ?

STEMPusan National University

STEM-PNU

28

Transaction : Serializability

Serialization Exchange non-conflict operation Conflict operation :

Result(T1.Op,T2.Op) ≠ Result(T2.Op,T1.Op)

Time-stamping Two Phase Locking

r(a) w(a) r(b) w(b)

r(a) w(a)r(b) w(b)

Trans 1

Trans 2

Time

STEMPusan National University

STEM-PNU

29

Time Stamping

If conflict occurs, then abort For read(T,x), ts(T) < tsWR(x), then abort T For write(T,x), ts(T) < tsWR(x) or ts(T) < tsRD(x), then abort T

STEMPusan National University

STEM-PNU

30

Two Phase Locking

2PL

Strict 2PL

STEMPusan National University

STEM-PNU

31

Implementation of Concurrency Control in Distributed Systems

Three Managers TM (Transaction Manager) : Ensure the Atomicity Scheduler : Main Responsibility for Concurrency Control DM (Data Manager) : Simple Read/Write

In a single machine

In distributed systems