Professional Documents
Culture Documents
SUMSEM-2021-22 CSE4001 ETH VL2021220701879 Reference Material III 22-08-2022 6.3 2pcommit
SUMSEM-2021-22 CSE4001 ETH VL2021220701879 Reference Material III 22-08-2022 6.3 2pcommit
1
Optimistic concurrency control
• Optimistic concurrency control (OCC) is
based on the assumption that two
transactions won’t occur at the same time
• Reading phase:
Read database values to a copy of the
transaction which is not yet bound to the
object. Several different transactions of
this type may coexist
OCC phases
• Validate phase:
Controls if the transaction conflicts with
another transaction on the same object
• Writing phase:
– Committed: If there is no conflict, make all
changes part of the official state of the database
– Aborted: If there is a conflict, resolve it, typically
by aborting the transaction, although other
resolution schemes are possible
95-702 Transactions 6
Validation rules
95-702 Transactions 8
OCC merits and demerits
9
Does this allow for any
concurrency?
In reality, the
Transaction T1 Transaction T2 coordinator
would do the
locking.
Lock_Item(x) Lock_Item(y)
T1 uses x T2 uses y
Unlock_Item(x) Unlock_Item(y)
If x differs from y these two transactions proceed concurrently.
If both want to use x, one waits until the other completes.
10
Distributed Transactions (More than
one server)
Begin transaction BookTrip
book a plane from Qantas
book hotel from Hilton
book rental car from Hertz
End transaction BookTrip
The Two Phase Commit Protocol is a classic solution for atomicity and
consistency.
11
95-702 Transactions 12
Interacting with a coordinator
Transaction T
Coordinator Interface:
tid = openTransaction();
openTransaction() -> transID
a.withdraw(tid,100); closeTransaction(transID) ->
commit or abort
b.deposit(tid,100); abortTransaction(TransID)
c.withdraw(tid,200);
d.deposit(tid,200);
closeTransaction(tid) or
Think about atomicity and
abortTransaction(tid) consistency.
13
Client Talks to a Coordinator
Different servers
Any server
BookPlane Participant
BookTrip Recoverable objects needed
Coordinator to book a plane
BookHotel Participant
Recoverable objects needed
openTrans Unique Transaction ID to book a hotel.
TID
BookRentalCar Participant
Recoverable objects needed
BookTrip Client to rent a car.
TID = openTransaction()
14
95-702 Transactions 15
95-702 Transactions 16
95-702 Transactions 17
Two-Phase Commit Protocol
BookPlane
Vote_Request
BookTrip Vote_Commit
Coordinator
Vote Request
BookHotel
Vote Commit
Vote Request
BookRentalCar
Vote Commit
Phase 1 BookTrip coordinator
sends a Vote_Request to each
process. Each process returns
a Vote_Commit or Vote_Abort.
18
Two-Phase Commit Protocol
BookPlane
Global Commit
BookTrip ACK
Coordinator BookHotel
Global Commit
ACK
Global Commit
Init
Init
Vote-request Vote-request
Commit
----------------- -----------------
----------
Vote-abort Vote-commit
Vote-request
Ready
wait
Vote-abort Vote-commit Global-commit
-------------- ---------------- -------------------
Global-abort Global-commit ACK
Global-abort
----------------
Abort Commit
ACK
Commit
Abort
21
2PC Blocks in Three Places
Init
Init
Vote-request
Commit If waiting too long
-----------------
---------- After Vote-request
Vote-commit
Vote-request Send a Global-Abort
Vote-request Ready
wait -----------------
Vote-abort Vote-commit Vote-abort Global-commit
-------------- ---------------- -------------------
Global-abort Global-commit ACK
Global-abort
----------------
Abort Commit
ACK
Commit
Abort
22
2PC Blocks in Three Places
If waiting too long we can’t simply abort! We must wait
until the coordinator recovers. We might also make queries
on other participants.
Init
Init
Vote-request
Commit Vote-request -----------------
---------- ----------------- Vote-commit
Vote-request Vote-abort
Ready
wait
Vote-abort Vote-commit Global-commit
-------------- ---------------- -------------------
Global-abort Global-commit ACK
Global-abort
----------------
Abort Commit
ACK
Commit
Abort
23
Example
Transfer $1000
From A:$3000
To B:$2000
client
Bank A Bank B
Bank A Bank B
Strawman solution
transaction bank A bank B
client coordinator
start
A=A-1000
done B=B+1000