2pl and occ
TRANSCRIPT
![Page 1: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/1.jpg)
2PL and OCC
Some material taken/derived from: • Princeton COS-418 materials created by Michael Freedman and Kyle Jamieson.• MIT 6.824 by Robert Morris, Frans Kaashoek, and Nickolai Zeldovich.Licensed for use under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
CS 475: Concurrent & Distributed Systems (Fall 2021)Lecture 15
Yue Cheng
![Page 2: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/2.jpg)
Recap: Transaction serializability
Serializability:
Execution of a set of transactions over multiple items is equivalent to some serial execution of transactions
Y. Cheng GMU CS475 Fall 2021 2
![Page 3: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/3.jpg)
Q: How to ensure correctnesswhen running concurrent transactions?
3
![Page 4: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/4.jpg)
What does correctness mean?
Transactions should have property of isolation, i.e., all operations in a transaction appear to happen together at the same time
Y. Cheng GMU CS475 Fall 2021 4
![Page 5: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/5.jpg)
What does correctness mean?
Transactions should have property of isolation, i.e., all operations in a transaction appear to happen together at the same time
We need serializability
Y. Cheng GMU CS475 Fall 2021 5
![Page 6: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/6.jpg)
Fixing concurrency problems
Strawman: Just run transactions serially —prohibitively bad performance
Y. Cheng GMU CS475 Fall 2021 6
![Page 7: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/7.jpg)
Fixing concurrency problems
Strawman: Just run transactions serially —prohibitively bad performance
Observation: Problems only arise when:1. Two transactions touch the same data2. At least one of these transactions involves a
write to the data
Y. Cheng GMU CS475 Fall 2021 7
![Page 8: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/8.jpg)
Fixing concurrency problems
Strawman: Just run transactions serially —prohibitively bad performance
Observation: Problems only arise when:1. Two transactions touch the same data2. At least one of these transactions involves a
write to the data
Key idea: Only permit schedules whose effects are guaranteed to be equivalent to serial schedulesY. Cheng GMU CS475 Fall 2021 8
![Page 9: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/9.jpg)
Serializability of schedules
Two operations conflict if1. They belong to different transactions2. They operate on the same data3. One of them is a write
Y. Cheng GMU CS475 Fall 2021 9
![Page 10: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/10.jpg)
Serializability of schedules
Two operations conflict if1. They belong to different transactions2. They operate on the same data3. One of them is a write
Two schedules are equivalent if1. They involve the same transactions and
operations2. All conflicting operations are ordered the same
way
Y. Cheng GMU CS475 Fall 2021 10
![Page 11: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/11.jpg)
Serializability of schedulesTwo operations conflict if1. They belong to different transactions2. They operate on the same data3. One of them is a write
Two schedules are equivalent if1. They involve the same transactions and
operations2. All conflicting operations are ordered the same
way
A schedule is serializable if it is equivalent to a serial schedule
Y. Cheng GMU CS475 Fall 2021 11
![Page 12: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/12.jpg)
Testing for serializability
Intuition: Swap non-conflicting operations until you reach a serial schedule
Y. Cheng GMU CS475 Fall 2021 12
![Page 13: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/13.jpg)
Testing for serializability
Y. Cheng GMU CS475 Fall 2021 13
T1: R(A), W(A), CommitT2: R(A), R(B), W(B), Commit
time
Intuition: Swap non-conflicting operations until you reach a serial schedule
![Page 14: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/14.jpg)
Testing for serializability
Y. Cheng GMU CS475 Fall 2021 14
T1: R(A), W(A), CommitT2: R(A), R(B), W(B), Commit
time
Intuition: Swap non-conflicting operations until you reach a serial schedule
![Page 15: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/15.jpg)
Testing for serializability
Y. Cheng GMU CS475 Fall 2021 15
time
Intuition: Swap non-conflicting operations until you reach a serial scheduleT1: R(A), W(A), CommitT2: R(A), R(B), W(B), Commit
Intuition: Swap non-conflicting operations until you reach a serial schedule
![Page 16: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/16.jpg)
Testing for serializability
Y. Cheng GMU CS475 Fall 2021 16
time
Intuition: Swap non-conflicting operations until you reach a serial scheduleT1: R(A), W(A), CommitT2: R(A), R(B), W(B) Commit
Intuition: Swap non-conflicting operations until you reach a serial schedule
Serializable
![Page 17: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/17.jpg)
Testing for serializability
Y. Cheng GMU CS475 Fall 2021 17
time
Intuition: Swap non-conflicting operations until you reach a serial scheduleT1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A), Commit
Intuition: Swap non-conflicting operations until you reach a serial schedule
![Page 18: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/18.jpg)
Testing for serializability
Y. Cheng GMU CS475 Fall 2021 18
time
Intuition: Swap non-conflicting operations until you reach a serial scheduleT1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A), Commit
Intuition: Swap non-conflicting operations until you reach a serial schedule
![Page 19: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/19.jpg)
Testing for serializability
Y. Cheng GMU CS475 Fall 2021 19
time
Intuition: Swap non-conflicting operations until you reach a serial scheduleT1: R(A), W(A) W(B), CommitT2: R(B), W(B), R(A), Commit
Intuition: Swap non-conflicting operations until you reach a serial schedule
![Page 20: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/20.jpg)
Testing for serializability
Y. Cheng GMU CS475 Fall 2021 20
time
Intuition: Swap non-conflicting operations until you reach a serial schedule
NOT serializable
T1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A), Commit
Intuition: Swap non-conflicting operations until you reach a serial schedule
![Page 21: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/21.jpg)
Testing for serializability
Another way to test serializability• Draw arrows between conflicting operations• Arrow points in the direction of time• If no cycles between transactions, the schedule
is serializable
Y. Cheng GMU CS475 Fall 2021 21
![Page 22: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/22.jpg)
Testing for serializability
Another way to test serializability• Draw arrows between conflicting operations• Arrow points in the direction of time• If no cycles between transactions, the schedule
is serializable
Y. Cheng GMU CS475 Fall 2021 22
T1: R(A), W(A), CommitT2: R(A), R(B), W(B), Commit
time
![Page 23: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/23.jpg)
Testing for serializability
Another way to test serializability• Draw arrows between conflicting operations• Arrow points in the direction of time• If no cycles between transactions, the schedule
is serializable
Y. Cheng GMU CS475 Fall 2021 23
time
T1: R(A), W(A), CommitT2: R(A), R(B), W(B), Commit
![Page 24: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/24.jpg)
Testing for serializability
Another way to test serializability• Draw arrows between conflicting operations• Arrow points in the direction of time• If no cycles between transactions, the schedule
is serializable
Y. Cheng GMU CS475 Fall 2021 24
time
T1: R(A), W(A), CommitT2: R(A), R(B), W(B), Commit
![Page 25: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/25.jpg)
Testing for serializability
Another way to test serializability• Draw arrows between conflicting operations• Arrow points in the direction of time• If no cycles between transactions, the schedule
is serializable
Y. Cheng GMU CS475 Fall 2021 25
time
T1: R(A), W(A), CommitT2: R(A), R(B), W(B), Commit
No cycles,serializable
![Page 26: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/26.jpg)
Testing for serializability
Another way to test serializability• Draw arrows between conflicting operations• Arrow points in the direction of time• If no cycles between transactions, the schedule
is serializable
Y. Cheng GMU CS475 Fall 2021 26
time
T1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A), Commit
![Page 27: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/27.jpg)
Testing for serializability
Another way to test serializability• Draw arrows between conflicting operations• Arrow points in the direction of time• If no cycles between transactions, the schedule
is serializable
Y. Cheng GMU CS475 Fall 2021 27
time
T1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A), Commit
![Page 28: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/28.jpg)
Testing for serializability
Another way to test serializability• Draw arrows between conflicting operations• Arrow points in the direction of time• If no cycles between transactions, the schedule
is serializable
Y. Cheng GMU CS475 Fall 2021 28
time
T1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A), Commit
![Page 29: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/29.jpg)
Testing for serializability
Another way to test serializability• Draw arrows between conflicting operations• Arrow points in the direction of time• If no cycles between transactions, the schedule
is serializable
Y. Cheng GMU CS475 Fall 2021 29
time
T1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A), Commit
Cycle exists(T1 ⇄ T2),
NOT serializable
![Page 30: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/30.jpg)
Linearizability vs. Serializability
Y. Cheng GMU CS475 Fall 2021 30
• Linearizability: a guarantee about single operations on single objects• Once write completes, all later
reads (by wall clock) should reflect that write
• Serializability is a guarantee about transactions over one or more objects• Doesn’t impose real-time
constraints
• Linearizability + serializability = strict serializability– Transaction behavior equivalent to some serial
execution• And that serial execution agrees with real-time
![Page 31: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/31.jpg)
Some new terms
Lost update: the result of a transaction is overwritten by another transaction
Y. Cheng GMU CS475 Fall 2021 31
![Page 32: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/32.jpg)
Some new terms
Lost update: the result of a transaction is overwritten by another transaction
Dirty read: uncommitted results are read by a transaction
Y. Cheng GMU CS475 Fall 2021 32
![Page 33: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/33.jpg)
Some new terms
Lost update: the result of a transaction is overwritten by another transaction
Dirty read: uncommitted results are read by a transaction
Non-repeatable read: two reads in the same transaction return different results
Y. Cheng GMU CS475 Fall 2021 33
![Page 34: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/34.jpg)
Some new terms
Lost update: the result of a transaction is overwritten by another transaction
Dirty read: uncommitted results are read by a transaction
Non-repeatable read: two reads in the same transaction return different results
Phantom read: later reads in the same transaction return extra rowsY. Cheng GMU CS475 Fall 2021 34
![Page 35: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/35.jpg)
Serial schedule – No problem
Y. Cheng GMU CS475 Fall 2021 35
T1: R(A), W(A), R(B), W(B), AbortT2: R(A), W(A), Commit
time
![Page 36: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/36.jpg)
Quiz: Which concurrency problem is this?
Y. Cheng GMU CS475 Fall 2021 36
Quiz: Which concurrency problem is this?T1: R(A), W(A) R(B), W(B), AbortT2: R(A), W(A), Commit
Lost update Dirty read Non-repeatable read Phantom read
time
??
![Page 37: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/37.jpg)
Quiz: Which concurrency problem is this?
Y. Cheng GMU CS475 Fall 2021 37
Quiz: Which concurrency problem is this?T1: R(A) R(A), W(A), CommitT2: R(A), W(A), Commit
time
Lost update Dirty read Non-repeatable read Phantom read ??
![Page 38: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/38.jpg)
Quiz: Which concurrency problem is this?
Y. Cheng GMU CS475 Fall 2021 38
Quiz: Which concurrency problem is this?
time
Lost update Dirty read Non-repeatable read Phantom read
T1: R(A), W(A) W(B), CommitT2: R(A) W(A), W(B), Commit
??
![Page 39: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/39.jpg)
Quiz: Which concurrency problem is this?
Y. Cheng GMU CS475 Fall 2021 39
Quiz: Which concurrency problem is this?
time
Lost update Dirty read Non-repeatable read Phantom read
T1: R(A), W(A) W(A), CommitT2: R(A), R(B), W(B), Commit
??
![Page 40: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/40.jpg)
40
Lock-based concurrency control
Y. Cheng GMU CS475 Fall 2021
• Big Global Lock: Results in a serial transaction schedule at the cost of performance
• 2PL: Two-phase locking with finer-grain locks:• Growing phase when txn acquires locks• Shrinking phase when txn releases locks (typically
commit)• Allows txns to execute concurrently, improving
performance
![Page 41: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/41.jpg)
2PL
• 2PL guarantees serializability by disallowingcycles between txns
• There could be dependencies in the waits-forgraph among txns waiting for locks:• Edge from T2 to T1 means T1 acquired lock first and
T2 has to wait• Edge from T1 to T2 means T1 acquired lock first and
T2 has to wait• Cycles mean DEADLOCK, and in that case 2PL
won’t proceed
Y. Cheng GMU CS475 Fall 2021 41
![Page 42: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/42.jpg)
2PL
Deal with deadlocks by aborting one of the twn txns (e.g.,detect with timeout)
Y. Cheng GMU CS475 Fall 2021 42
time
T1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A) Commit
![Page 43: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/43.jpg)
2PL
Y. Cheng GMU CS475 Fall 2021 43
time
T1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A) Commit
Lock_X(A)
![Page 44: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/44.jpg)
2PL
Y. Cheng GMU CS475 Fall 2021 44
time
T1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A) Commit
Lock_X(A)
Lock_X(B)
![Page 45: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/45.jpg)
2PL
Y. Cheng GMU CS475 Fall 2021 45
time
T1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A) Commit
Lock_X(A)
Lock_X(B) Lock_S(A)
![Page 46: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/46.jpg)
2PL
Y. Cheng GMU CS475 Fall 2021 46
time
T1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A) Commit
Lock_X(A)
Lock_X(B) Lock_S(A)
Lock_X(B)
![Page 47: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/47.jpg)
2PL
Y. Cheng GMU CS475 Fall 2021 47
time
T1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A) Commit
Lock_X(A)
Lock_X(B) Lock_S(A)
Lock_X(B)
DEADLOCK!
![Page 48: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/48.jpg)
2PL
Deal with deadlocks by aborting one of the two txns (e.g.,detect with timeout)
Y. Cheng GMU CS475 Fall 2021 48
time
T1: R(A), W(A), W(B), CommitT2: R(B), W(B), R(A) Commit
Lock_X(A)
Lock_X(B) Lock_S(A)
Lock_X(B)
DEADLOCK!
![Page 49: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/49.jpg)
2PL: Releasing locks too soon?What if we release the lock as soon as we can?
Y. Cheng GMU CS475 Fall 2021 49
![Page 50: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/50.jpg)
2PL: Releasing locks too soon?What if we release the lock as soon as we can?
Y. Cheng GMU CS475 Fall 2021 50
time
T1: R(A), W(A), AbortT2: R(B), W(B), R(A) Abort Abort
![Page 51: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/51.jpg)
2PL: Releasing locks too soon?What if we release the lock as soon as we can?
Y. Cheng GMU CS475 Fall 2021 51
time
T1: R(A), W(A), AbortT2: R(B), W(B), R(A) Abort Abort
Lock_X(A)
![Page 52: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/52.jpg)
2PL: Releasing locks too soon?What if we release the lock as soon as we can?
Y. Cheng GMU CS475 Fall 2021 52
time
T1: R(A), W(A), AbortT2: R(B), W(B), R(A) Abort Abort
Lock_X(A) Unlock_X(A)
![Page 53: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/53.jpg)
2PL: Releasing locks too soon?What if we release the lock as soon as we can?
Y. Cheng GMU CS475 Fall 2021 53
time
T1: R(A), W(A), AbortT2: R(B), W(B), R(A) Abort Abort
Lock_X(A) Unlock_X(A)
Lock_X(B)
![Page 54: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/54.jpg)
2PL: Releasing locks too soon?What if we release the lock as soon as we can?
Y. Cheng GMU CS475 Fall 2021 54
time
T1: R(A), W(A), AbortT2: R(B), W(B), R(A) Abort Abort
Lock_X(A) Unlock_X(A)
Lock_X(B) Lock_S(A)
![Page 55: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/55.jpg)
2PL: Releasing locks too soon?What if we release the lock as soon as we can?
Y. Cheng GMU CS475 Fall 2021 55
time
T1: R(A), W(A), AbortT2: R(B), W(B), R(A) Abort Abort
Lock_X(A)
Lock_X(B) Lock_S(A)
Unlock_X(A)
Rollback of T1 requires rollback of T2, since T2 reads a value written by T1
![Page 56: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/56.jpg)
2PL: Releasing locks too soon?What if we release the lock as soon as we can?
Y. Cheng GMU CS475 Fall 2021 56
time
T1: R(A), W(A), AbortT2: R(B), W(B), R(A) Abort Abort
Lock_X(A)
Lock_X(B) Lock_S(A)
Unlock_X(A)
Rollback of T1 requires rollback of T2, since T2 reads a value written by T1Cascading aborts: the rollback of one txn causes rollback of another
![Page 57: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/57.jpg)
Strict 2PL
• Release locks at the end of the transaction
• Variant of 2PL implemented by most DBs in practice
Y. Cheng GMU CS475 Fall 2021 57
![Page 58: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/58.jpg)
Y. Cheng GMU CS475 Fall 2021 58
Q: What if access patterns rarely, if ever, conflict?
![Page 59: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/59.jpg)
59
Today
Y. Cheng GMU CS475 Fall 2021
• Optimistic concurrency control (OCC)• Be optimistic, or opportunistic, that conflicts rarely
happen
![Page 60: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/60.jpg)
Be optimistic!
• Goal: Low overhead for non-conflicting txns
• Assume success!• Process transaction as if would succeed• Check for serializability only at commit time• If fails, abort transaction
• Optimistic Concurrency Control (OCC) • Higher performance when few conflicts vs. locking• Lower performance when many conflicts vs. locking
Y. Cheng GMU CS475 Fall 2021 60
![Page 61: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/61.jpg)
OCC: Three-phase approach
• Begin: Record timestamp marking the transaction’s beginning
Y. Cheng GMU CS475 Fall 2021 61
![Page 62: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/62.jpg)
OCC: Three-phase approach
• Begin: Record timestamp marking the transaction’s beginning• Modify phase: • Txn can read values of committed data items• Updates only to local copies (versions) of items (in DB
cache)
Y. Cheng GMU CS475 Fall 2021 62
![Page 63: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/63.jpg)
OCC: Three-phase approach
• Begin: Record timestamp marking the transaction’s beginning• Modify phase: • Txn can read values of committed data items• Updates only to local copies (versions) of items (in DB
cache)
• Validate phase
Y. Cheng GMU CS475 Fall 2021 63
![Page 64: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/64.jpg)
OCC: Three-phase approach
• Begin: Record timestamp marking the transaction’s beginning• Modify phase: • Txn can read values of committed data items• Updates only to local copies (versions) of items (in DB
cache)
• Validate phase• Commit phase• If validates, transaction’s updates applied to DB• Otherwise, transaction restarted• Care must be taken to avoid “TOCTTOU” issues
Y. Cheng GMU CS475 Fall 2021 64
![Page 65: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/65.jpg)
OCC: Three-phase approach
• Begin: Record timestamp marking the transaction’s beginning• Modify phase: • Txn can read values of committed data items• Updates only to local copies (versions) of items (in DB
cache)
• Validate phase• Commit phase• If validates, transaction’s updates applied to DB• Otherwise, transaction restarted• Care must be taken to avoid “TOCTTOU” issues
Y. Cheng GMU CS475 Fall 2021 65
Execute optimistically!
![Page 66: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/66.jpg)
OCC: Three-phase approach
• Begin: Record timestamp marking the transaction’s beginning• Modify phase: • Txn can read values of committed data items• Updates only to local copies (versions) of items (in DB
cache)
• Validate phase• Commit phase• If validates, transaction’s updates applied to DB• Otherwise, transaction restarted• Care must be taken to avoid “TOCTTOU” issues
Y. Cheng GMU CS475 Fall 2021 66
Execute optimistically!
These should happen together!
![Page 67: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/67.jpg)
67
OCC: Why validation is necessary!
txncoordinator O
Q
P
When commits txn updates,create new versions at some timestamp t
• New txn creates shadow copies of P and Q
• P and Q’s copies at inconsistent state
txncoordinator
Y. Cheng GMU CS475 Fall 2021
![Page 68: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/68.jpg)
• Transaction is about to commit. System must ensure:• Initial consistency: Versions of accessed objects at start
consistent• No conflicting concurrency: No other txn has committed an
operation at object that conflicts with one of this txn’s invocations
• Consider transaction T: For all other txns O either committed or in validation phase, one of the following holds:
A. O completes commit before T starts modifyB. T starts commit after O completes commit,
and ReadSet T and WriteSet O are disjoint C. Both ReadSet T and WriteSet T are disjoint from WriteSet O,
and O completes modify phase
• When validating T, first check (A), then (B), then (C). If all fail, validation fails and T aborted
68
OCC: Validate phase
Y. Cheng GMU CS475 Fall 2021
![Page 69: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/69.jpg)
Atomic commit for OCC
• Use two-phase commit (2PC) to achieve atomic commit (validate + commit writes)
• Recall 2PC protocol:1. Coordinator sends prepare messages to all nodes,
other nodes vote yes or noa. If all nodes accept, proceedb. If any node declines, abort
2. Coordinator sends commit or abort messages to all nodes, and all nodes act accordingly
Y. Cheng GMU CS475 Fall 2021 69
![Page 70: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/70.jpg)
Atomic commit for OCC• Execute optimistically: Read committed values, write
changes locally• Validate: Check if data has changed since original read• Commit (Write): Commit if no change, else abort
• Phase 1: send prepare to each shard: include buffered write + original reads for that shard• Shards validate reads and acquire locks (exclusive for write
locations, shared for read locations)• If this succeeds, respond with yes; else respond with no
• Phase 2: collect votes, send result (abort or commit) to all shards • If commit, shards apply buffered writes• All shards release locks
Y. Cheng GMU CS475 Fall 2021 70
Phase 1
Phase 2
![Page 71: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/71.jpg)
Atomic commit for OCC• Execute optimistically: Read committed values, write
changes locally• Validate: Check if data has changed since original read• Commit (Write): Commit if no change, else abort
• Phase 1: send prepare to each shard: include buffered write + original reads for that shard• Shards acquire locks and validate reads (exclusive for write
locations, shared for read locations)• If this succeeds, respond with yes; else respond with no
• Phase 2: collect votes, send result (abort or commit) to all shards • If commit, shards apply buffered writes• All shards release locks
Y. Cheng GMU CS475 Fall 2021 71
Phase 1
Phase 2
![Page 72: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/72.jpg)
Atomic commit for OCC• Execute optimistically: Read committed values, write
changes locally• Validate: Check if data has changed since original read• Commit (Write): Commit if no change, else abort
• Phase 1: send prepare to each shard: include buffered write + original reads for that shard• Shards acquire locks and validate reads (exclusive for write
locations, shared for read locations)• If this succeeds, respond with yes; else respond with no
• Phase 2: collect votes, send result (abort or commit) to all shards • If commit, shards apply buffered writes• All shards release locks
Y. Cheng GMU CS475 Fall 2021 72
Phase 1
Phase 2
![Page 73: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/73.jpg)
Two ways of implementing serializability: 2PL, OCC• 2PL (pessimistic):• Assume conflict, always lock• High overhead for non-conflicting txn• Must check for deadlock
• OCC (optimistic):• Assume no conflict• Low overhead for low-conflict workloads (but high for
high-conflict workloads)• Ensure correctness by aborting txns if conflict occurs
Y. Cheng GMU CS475 Fall 2021 73
![Page 74: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/74.jpg)
Lock_X(A) <granted>Read(A) Lock_S(A)
A := A-50Write(A)
Unlock(A) <granted>Read(A)
Unlock(A)Lock_S(B) <granted>
Lock_X(B)Read(B)
<granted> Unlock(B)
Read(B)B := B +50Write(B)
Unlock(B)
Is this a 2PL schedule?No
Is this a serializable schedule?No
![Page 75: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/75.jpg)
Lock_X(A) <granted>Read(A) Lock_S(A)
A := A-50Write(A)
Lock_X(B) <granted>Unlock(A) <granted>
Read(A)Lock_S(B)
Read(B)B := B +50Write(B)
Unlock(B) <granted>Unlock(A)Read(B)
Unlock(B)
Is this a 2PL schedule?Yes, and it is serializable
Is this a Strict 2PL schedule?No, cascading aborts possible
![Page 76: 2PL and OCC](https://reader030.vdocuments.mx/reader030/viewer/2022012806/61bd402a61276e740b10e1c6/html5/thumbnails/76.jpg)
Lock_X(A) <granted>Read(A) Lock_S(A)
A := A-50Write(A)
Lock_X(B) <granted>Read(B)
B := B +50Write(B)
Unlock(A)Unlock(B) <granted>
Read(A)Lock_S(B) <granted>
Read(B)Unlock(A)Unlock(B)
Is this a 2PL schedule?Yes, and it is serializable
Is this a Strict 2PL schedule?Yes, cascading aborts not possible