CMU 15-445/645 Database Systems (Fall 2018) :: Two-Phase Locking

CMU 15-445/645 Database Systems (Fall 2018) :: Two-Phase Locking

Two-Phase Locking Lecture #17 Database Systems Andy Pavlo 15-445/15-645 Computer Science Fall 2018 AP Carnegie Mellon Univ. 2 L AST CL ASS Conflict Serializable → Verify using either the "swapping" method or dependency graphs. → Any DBMS that says that they support "serializable" isolation does this. View Serializable → No efficient way to verify. → Andy doesn't know of any DBMS that supports this. CMU 15-445/645 (Fall 2018) 3 EXAMPLE Schedule T1 T2 BEGIN R(A) BEGIN R(A) W(A) TIME W(A) COMMIT R(A) COMMIT CMU 15-445/645 (Fall 2018) 4 OBSERVATION We need a way to guarantee that all execution schedules are correct (i.e., serializable) without knowing the entire schedule ahead of time. Solution: Use locks to protect database objects. CMU 15-445/645 (Fall 2018) 5 EXECUTING WITH LOCKS Schedule Lock Manager T1 T2 BEGIN LOCK(A) Granted (T1→A) R(A) BEGIN LOCK(A) W(A) R(A) TIME UNLOCK(A) R(A) W(A) COMMIT UNLOCK(A) COMMIT CMU 15-445/645 (Fall 2018) 5 EXECUTING WITH LOCKS Schedule Lock Manager T1 T2 BEGIN LOCK(A) Granted (T1→A) R(A) BEGIN LOCK(A) Denied! W(A) R(A) TIME UNLOCK(A) R(A) W(A) COMMIT UNLOCK(A) COMMIT CMU 15-445/645 (Fall 2018) 5 EXECUTING WITH LOCKS Schedule Lock Manager T1 T2 BEGIN LOCK(A) Granted (T1→A) R(A) BEGIN LOCK(A) Denied! W(A) R(A) TIME UNLOCK(A) Released (T1→A) R(A) W(A) COMMIT UNLOCK(A) COMMIT CMU 15-445/645 (Fall 2018) 5 EXECUTING WITH LOCKS Schedule Lock Manager T1 T2 BEGIN LOCK(A) Granted (T1→A) R(A) BEGIN LOCK(A) Denied! W(A) R(A) TIME UNLOCK(A) Released (T1→A) R(A) Granted (T2→A) W(A) COMMIT UNLOCK(A) Released (T2→A) COMMIT CMU 15-445/645 (Fall 2018) 6 TODAY'S AGENDA Lock Types Two-Phase Locking Deadlock Detection + Prevention Hierarchical Locking Isolation Levels CMU 15-445/645 (Fall 2018) 7 LOCKS VS. LATCHES Locks Latches User transactions Threads Database Contents In-Memory Data Structures Entire Transactions Critical Sections Shared, Exclusive, Update, Read, Write Intention Deadlock Detection & Resolution Avoidance Waits-for, Timeout, Aborts Coding Discipline Kept Lock Manager Protected Data Structure Source: Goetz Graefe CMU 15-445/645 (Fall 2018) 8 BASIC LOCK TYPES S-LOCK: Shared locks for reads. X-LOCK: Exclusive locks for writes. Compatibility Matrix Shared Exclusive Shared ✔ X Exclusive X X CMU 15-445/645 (Fall 2018) 9 EXECUTING WITH LOCKS Transactions request locks (or upgrades). Lock manager grants or blocks requests. Transactions release locks. Lock manager updates its internal lock-table. → It keeps track of what transactions hold what locks and what transactions are waiting to acquire any locks. CMU 15-445/645 (Fall 2018) 10 EXECUTING WITH LOCKS Schedule Lock Manager T1 T2 BEGIN X-LOCK(A) Granted (T1→A) R(A) W(A) UNLOCK(A) BEGIN TIME X-LOCK(A) W(A) UNLOCK(A) S-LOCK(A) R(A) UNLOCK(A) COMMIT COMMIT CMU 15-445/645 (Fall 2018) 10 EXECUTING WITH LOCKS Schedule Lock Manager T1 T2 BEGIN X-LOCK(A) Granted (T1→A) R(A) W(A) UNLOCK(A) Released (T1→A) BEGIN TIME X-LOCK(A) W(A) UNLOCK(A) S-LOCK(A) R(A) UNLOCK(A) COMMIT COMMIT CMU 15-445/645 (Fall 2018) 10 EXECUTING WITH LOCKS Schedule Lock Manager T1 T2 BEGIN X-LOCK(A) Granted (T1→A) R(A) W(A) UNLOCK(A) Released (T1→A) BEGIN TIME X-LOCK(A) Granted (T2→A) W(A) UNLOCK(A) Released (T2→A) S-LOCK(A) R(A) UNLOCK(A) COMMIT COMMIT CMU 15-445/645 (Fall 2018) 10 EXECUTING WITH LOCKS Schedule Lock Manager T1 T2 BEGIN X-LOCK(A) Granted (T1→A) R(A) W(A) UNLOCK(A) Released (T1→A) BEGIN TIME X-LOCK(A) Granted (T2→A) W(A) UNLOCK(A) Released (T2→A) S-LOCK(A) Granted (T →A) R(A) 1 UNLOCK(A) Released (T1→A) COMMIT COMMIT CMU 15-445/645 (Fall 2018) 10 EXECUTING WITH LOCKS Schedule Lock Manager T1 T2 BEGIN X-LOCK(A) Granted (T1→A) R(A) W(A) UNLOCK(A) Released (T1→A) BEGIN TIME X-LOCK(A) Granted (T2→A) W(A) UNLOCK(A) Released (T2→A) S-LOCK(A) Granted (T →A) R(A) 1 UNLOCK(A) Released (T1→A) COMMIT COMMIT CMU 15-445/645 (Fall 2018) 11 CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn is allowed to access an object in the database on the fly. The protocol does not need to know all of the queries that a txn will execute ahead of time. CMU 15-445/645 (Fall 2018) 12 TWO-PHASE LOCKING Phase #1: Growing → Each txn requests the locks that it needs from the DBMS’s lock manager. → The lock manager grants/denies lock requests. Phase #2: Shrinking → The txn is allowed to only release locks that it previously acquired. It cannot acquire new locks. CMU 15-445/645 (Fall 2018) 13 TWO-PHASE LOCKING The txn is not allowed to acquire/upgrade locks after the growing phase finishes. Transaction Lifetime # of Locks # of Growing Phase Shrinking Phase TIME CMU 15-445/645 (Fall 2018) 14 TWO-PHASE LOCKING The txn is not allowed to acquire/upgrade locks after the growing phase finishes. 2PL Violation! Transaction Lifetime # of Locks # of Growing Phase Shrinking Phase TIME CMU 15-445/645 (Fall 2018) 15 EXECUTING WITH 2PL Schedule Lock Manager T1 T2 BEGIN X-LOCK(A) Granted (T1→A) R(A) W(A) BEGIN X-LOCK(A) TIME R(A) UNLOCK(A) COMMIT W(A) UNLOCK(A) COMMIT CMU 15-445/645 (Fall 2018) 15 EXECUTING WITH 2PL Schedule Lock Manager T1 T2 BEGIN X-LOCK(A) Granted (T1→A) R(A) W(A) BEGIN X-LOCK(A) Denied! TIME R(A) UNLOCK(A) COMMIT W(A) UNLOCK(A) COMMIT CMU 15-445/645 (Fall 2018) 15 EXECUTING WITH 2PL Schedule Lock Manager T1 T2 BEGIN X-LOCK(A) Granted (T1→A) R(A) W(A) BEGIN X-LOCK(A) Denied! TIME R(A) UNLOCK(A) Released (T1→A) COMMIT W(A) UNLOCK(A) COMMIT CMU 15-445/645 (Fall 2018) 15 EXECUTING WITH 2PL Schedule Lock Manager T1 T2 BEGIN X-LOCK(A) Granted (T1→A) R(A) W(A) BEGIN X-LOCK(A) Denied! TIME R(A) UNLOCK(A) Released (T1→A) COMMIT W(A) Granted (T2→A) UNLOCK(A) Released (T →A) COMMIT 2 CMU 15-445/645 (Fall 2018) 16 TWO-PHASE LOCKING 2PL on its own is sufficient to guarantee conflict serializability. → It generates schedules whose precedence graph is acyclic. But it is subject to cascading aborts. CMU 15-445/645 (Fall 2018) 17 2PL CASCADING ABORTS Schedule T1 T2 BEGIN BEGIN X-LOCK(A) X-LOCK(B) R(A) W(A) UNLOCK(A) TIME X-LOCK(A) R(A) W(A) R(B) ⋮ W(B) ABORT CMU 15-445/645 (Fall 2018) 17 2PL CASCADING ABORTS Schedule This is a permissible schedule in T1 T2 BEGIN BEGIN 2PL, but the DBMS has to also X-LOCK(A) abort T when T aborts. X-LOCK(B) 2 1 R(A) → Any information about T1 cannot W(A) be "leaked" to the outside world. UNLOCK(A) TIME X-LOCK(A) R(A) W(A) This is all wasted work! R(B) ⋮ W(B) ABORT CMU 15-445/645 (Fall 2018) 18 2PL OBSERVATIONS There are potential schedules that are serializable but would not be allowed by 2PL. → Locking limits concurrency. May still have "dirty reads". → Solution: Strict 2PL May lead to deadlocks. → Solution: Detection or Prevention CMU 15-445/645 (Fall 2018) 19 STRICT TWO-PHASE LOCKING The txn is not allowed to acquire/upgrade locks after the growing phase finishes. Allows only conflict serializable schedules, but it is often stronger than needed for some apps. Release all locks at end of txn. # of Locks # of Growing Phase Shrinking Phase TIME CMU 15-445/645 (Fall 2018) 20 STRICT TWO-PHASE LOCKING A schedule is strict if a value written by a txn is not read or overwritten by other txns until that txn finishes. Advantages: → Does not incur cascading aborts. → Aborted txns can be undone by just restoring original values of modified tuples. CMU 15-445/645 (Fall 2018) 21 EXAMPLES T1 – Move $100 from Andy’s account (A) to his bookie’s account (B). T2 – Compute the total amount in all accounts and return it to the application. T1 T2 BEGIN BEGIN A=A-100 ECHO A+B B=B+100 COMMIT COMMIT CMU 15-445/645 (Fall 2018) 22 NON-2PL EXAMPLE Schedule Initial Database State T 1 T2 A=1000, B=1000 BEGIN BEGIN X-LOCK(A) R(A) S-LOCK(A) A=A-100 W(A) UNLOCK(A) R(A) UNLOCK(A) TIME S-LOCK(B) X-LOCK(B) R(B) UNLOCK(B) R(B) ECHO A+B B=B+100 COMMIT W(B) UNLOCK(B) COMMIT CMU 15-445/645 (Fall 2018) 22 NON-2PL EXAMPLE Schedule Initial Database State T 1 T2 A=1000, B=1000 BEGIN BEGIN X-LOCK(A) R(A) S-LOCK(A) A=A-100 W(A) UNLOCK(A) R(A) UNLOCK(A) TIME S-LOCK(B) X-LOCK(B) R(B) UNLOCK(B) R(B) ECHO A+B B=B+100 COMMIT W(B) UNLOCK(B) COMMIT CMU 15-445/645 (Fall 2018) 22 NON-2PL EXAMPLE Schedule Initial Database State T 1 T2 A=1000, B=1000 BEGIN BEGIN X-LOCK(A) R(A) S-LOCK(A) A=A-100 W(A) UNLOCK(A) R(A) UNLOCK(A) TIME S-LOCK(B) X-LOCK(B) R(B) UNLOCK(B) R(B) ECHO A+B B=B+100 COMMIT W(B) UNLOCK(B) COMMIT CMU 15-445/645 (Fall 2018) 22 NON-2PL EXAMPLE Schedule Initial Database State T 1 T2 A=1000, B=1000 BEGIN BEGIN X-LOCK(A) R(A) S-LOCK(A) A=A-100 T Output W(A) 2 UNLOCK(A) R(A) A+B=1100 UNLOCK(A) TIME S-LOCK(B) X-LOCK(B) R(B) UNLOCK(B) R(B) ECHO A+B B=B+100 COMMIT W(B) UNLOCK(B) COMMIT CMU 15-445/645 (Fall 2018) 23 2PL EXAMPLE Schedule Initial Database State T 1 T2 A=1000, B=1000 BEGIN BEGIN X-LOCK(A) R(A) S-LOCK(A) A=A-100 W(A) X-LOCK(B) UNLOCK(A) R(A) S-LOCK(B) TIME R(B) B=B+100 W(B) UNLOCK(B) R(B) COMMIT UNLOCK(A) UNLOCK(B) ECHO A+B COMMIT CMU 15-445/645 (Fall 2018) 23 2PL EXAMPLE Schedule Initial Database State T 1 T2 A=1000, B=1000 BEGIN BEGIN X-LOCK(A) R(A) S-LOCK(A) A=A-100 W(A) X-LOCK(B) UNLOCK(A) R(A) S-LOCK(B) TIME R(B) B=B+100 W(B) UNLOCK(B) R(B) COMMIT UNLOCK(A) UNLOCK(B) ECHO A+B COMMIT CMU 15-445/645 (Fall 2018) 23 2PL EXAMPLE Schedule Initial Database State T 1 T2 A=1000, B=1000 BEGIN BEGIN X-LOCK(A) R(A) S-LOCK(A) A=A-100 T Output W(A) 2 X-LOCK(B) UNLOCK(A) R(A) A+B=2000 S-LOCK(B) TIME R(B) B=B+100 W(B) UNLOCK(B) R(B) COMMIT UNLOCK(A) UNLOCK(B) ECHO A+B COMMIT CMU 15-445/645 (Fall 2018) 24 STRICT 2PL EXAMPLE Schedule Initial Database State T 1 T2 A=1000, B=1000 BEGIN BEGIN X-LOCK(A) R(A) S-LOCK(A) A=A-100 W(A) X-LOCK(B) R(B) B=B+100 W(B) TIME

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    89 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us