Title: Chapter 3. Synchronization
1Chapter 3. Synchronization
2Synchronization 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
3Clock
- In distributed systems
- Multiple Physical Clocks No Centralized Clock
- Logical Clock rather than a Global Physical Clock
4Global 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) lt T(B, rx)
5Logical Clock
- Logical Clock
- Not absolute time but for ordering
- Lamport Algorithm
- Correction of Clock
- C(A, tx) lt C(B, rx)
5
10
5
10
6
11
6
11
3
3
7
12
7
12
4
4
8
13
8
13
5
5
9
14
9
14
6
15
10
6
15
10
7
16
11
7
16
16
8
17
12
8
17
17
9
13
18
18
18
18
10
14
19
19
19
19
6Global Clock and Time Server
- Global Clock
- TAI (Temps Atomique International) at Paris
- Time Server
- Broadcasting from Satellite
- Granularity
- Global Clock Server
- Adjust local clock
- Cristians Algorithm
- Berkeley Algorithm
- Time never runsbackward
7Global 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
8Mutual 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
9Mutual 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
10Mutual 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
Wait until it gets the token
Token
11Mutual 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
12Election Bully Algorithm
- When it is found that a coordinator is required
13Election Ring Algorithm
- When it is found that the coordinator has
crashed, - Circulate message with priority
14Distributed Transaction
- Transaction
- Example Flight Reservation
Consistent State
Consistent State
Set of operations
BEGIN_TRANSACTION reserve WP -gt JFK reserve JFK -gt Nairobi reserve Nairobi -gt MalindiEND_TRANSACTION (a) BEGIN_TRANSACTION reserve WP -gt JFK reserve JFK -gt Nairobi reserve Nairobi -gt Malindi full gtABORT_TRANSACTION (b)
15ACID 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
16Transaction States
PartiallyCommitted
Committed
the initial state the transaction stays in this
state while it is executing
ALL
Active
NOTHING
Failed
Aborted
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
17ACID 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
18Example
- 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
19Example 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
20Serializability
- What happens after these transactions ?
- Serial Schedule
- Always Correct
- T1? T2 and T2? T1
- Concurrent Schedule
- Serializable if
- Result (T1T2) Result(T1? T2) or
- Result(T2? T1)
21Transaction 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
22Safe 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
23Log
- Log
- History of operations
- Used for rollback
- (Transaction, Pre-state, Post-state)
- (Trans, DataItem, OldValue, NewValue)
System
Transaction
operation1
operation2
operation3
log
log
log
24Deferred Updates
- Reflect Logs after Commit
- Shadowing
- If the transaction fails, then discard logs
Transaction
Begin
Write or update operations
operation1
. . .
System
operationn
Commit
25Deferred Updates Recovery
Log File
(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)
1. Transaction with commit OK2. Transaction
with begin but without commit Delete log
records
26Immediate Updates
- Execute operation immediately
- If the transaction fails, undo operations
Transaction
Begin
System
operation1
operation2
operation3
Commit
log
log
log
27Which one is better ?
28Transaction 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)
Trans 1
Time
r(a)
w(a)
r(b)
w(b)
Trans 2
29Time Stamping
- If conflict occurs, then abort
- For read(T,x), ts(T) lt tsWR(x), then abort T
- For write(T,x), ts(T) lt tsWR(x) or ts(T) lt
tsRD(x), then abort T
30Two Phase Locking
2PL
31Implementation 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 distributed systems
In a single machine