CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos – A. Pavlo Lecture#22: Concurrency Control – Part 2 (R&G.

Slides:



Advertisements
Similar presentations
Database Systems (資料庫系統)
Advertisements

Transaction Management and Concurrency Control
Chapter 16 Concurrency. Topics in this Chapter Three Concurrency Problems Locking Deadlock Serializability Isolation Levels Intent Locking Dropping ACID.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Accessing data Transactions. Agenda Questions from last class? Transactions concurrency Locking rollback.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
1 CS216 Advanced Database Systems Shivnath Babu Notes 12: Concurrency Control (II)
Concurrency Control Amol Deshpande CMSC424. Approach, Assumptions etc.. Approach  Guarantee conflict-serializability by allowing certain types of concurrency.
4/16/20151 PSU’s CS Concurrency Control and Recovery (only for DBs with updates…..!)- Review  Concurrency Control  Transaction  ACID  Isolation.
Managing Hierarchies of Database Elements (18.6) 1 Presented by Sarat Dasika (114) February 16, 2012.
Concurrency Control II. General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Physical Design.
Concurrency Control Part 2 R&G - Chapter 17 The sequel was far better than the original! -- Nobody.
1 Supplemental Notes: Practical Aspects of Transactions THIS MATERIAL IS OPTIONAL.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. –Because disk accesses are.
Concurrency Control R &G - Chapter 19 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue Book.
Managing Hierarchies of Database Elements (18.6) -Neha Saxena Class Id: 214.
Concurrency Control Managing Hierarchies of Database Elements (18.6) 1 Presented by Ronak Shah (214) March 9, 2009.
Transaction Management and Concurrency Control
Concurrency Control II R &G - Chapter 17 Lecture 20 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17.
Final Exam Review Last Lecture R&G - All Chapters Covered The end crowns all, And that old common arbitrator, Time, Will one day end it. William Shakespeare.
GRANULARITY OF LOCKS IN SHARED DATA BASE J.N. Gray, R.A. Lorie and G.R. Putzolu.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
1 Concurrency Control. 2 Transactions A transaction is a list of actions. The actions are reads (written R T (O)) and writes (written W T (O)) of database.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#22: Concurrency Control – Part 2 (R&G.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17 Modified by Donghui Zhang.
Concurrency Control.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#21: Concurrency Control (R&G ch. 17)
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications Concurrency Control, II (R&G ch. 17)
CPSC 461. Outline I. Transactions II. Concurrency control Conflict Serializable Schedules Two – Phase protocol Lock Management Deadlocks detection and.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Department of Computer Science and Engineering, HKUST 1 More on Isolation.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#21: Concurrency Control (R&G ch. 17)
V. Megalooikonomou Concurrency control (based on slides by C. Faloutsos at CMU and on notes by Silberchatz,Korth, and Sudarshan) Temple University – CIS.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Concurrency control.
 Concurrent execution of user programs is essential for good DBMS performance. Disk accesses are frequent, and relatively slow. Want to keep the CPU working.
1 Concurrency Control II: Locking and Isolation Levels.
Transaction Management: Concurrency Control CS634 Class 18, Apr 9, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
1 Concurrency Control Chapter Conflict Serializable Schedules  Two schedules are conflict equivalent if:  Involve the same actions of the same.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#21: Concurrency Control (R&G ch. 17)
1 Concurrency Control Lecture 22 Ramakrishnan - Chapter 19.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#23: Concurrency Control – Part 2 (R&G.
1 Advanced Database Concepts Transaction Management and Concurrency Control.
Lecture 9- Concurrency Control (continued) Advanced Databases Masood Niazi Torshiz Islamic Azad University- Mashhad Branch
1 Concurrency Control Chapter Conflict Serializable Schedules  Two schedules are conflict equivalent if:  Involve the same actions of the same.
Managing Hierarchies of Database Elements Section 18.6 CS257 Jack Price.
Database Applications (15-415) DBMS Internals- Part XII Lecture 23, April 12, 2016 Mohammad Hammoud.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
1 Concurrency Control. 2 Why Have Concurrent Processes? v Better transaction throughput, response time v Done via better utilization of resources: –While.
CS 440 Database Management Systems
Concurrency Control Managing Hierarchies of Database Elements (18.6)
Concurrency Control Techniques
Transaction Management and Concurrency Control
Lecture#23: Concurrency Control – Part 2
Extra slide #3.
Concurrency Control More !
Concurrency Control Chapter 17 Modified by Donghui Zhang
J.N GRAY R.A LORIE IBM RESEARCH LAB, SAN JOSE, CALIFORNIA
Database Applications (15-415) DBMS Internals- Part XII Lecture 24, April 11, 2018 Mohammad Hammoud.
Anthony D. Joseph and Ion Stoica
March 9th – Transactions
Chapter 10 Transaction Management and Concurrency Control
Lecture#21: Concurrency Control – Part 1
Introduction of Week 13 Return assignment 11-1 and 3-1-5
Transactions and Concurrency
Concurrency Control E0 261 Prasad Deshpande, Jayant Haritsa
Temple University – CIS Dept. CIS661 – Principles of Data Management
Database Management System
CONCURRENCY Concurrency is the tendency for different tasks to happen at the same time in a system ( mostly interacting with each other ) .   Parallel.
Presentation transcript:

CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#22: Concurrency Control – Part 2 (R&G ch. 17)

CMU SCS Last Class A concurrency control scheme uses locks and aborts to ensure correctness. Conflict vs. View Serializability (Strict) 2PL is popular. We need to handle deadlocks in 2PL: –Detection: Waits-for graph –Prevention: Abort some txns, defensively Faloutsos/PavloCMU SCS /6152

CMU SCS Last Class Assumption We assumed that the database was fixed collection of independent objects. –No objects are added or deleted. –No relationship between objects. –No indexes. Faloutsos/PavloCMU SCS /6153

CMU SCS Today’s Class Lock Granularities Locking in B+Trees The Phantom Problem Transaction Isolation Levels Faloutsos/PavloCMU SCS /6154

CMU SCS Lock Granularities When we say that a txn acquires a “lock”, what does that actually mean? –On a field? Record? Page? Table? Ideally, each txn should obtain fewest number of locks that is needed… Faloutsos/PavloCMU SCS /6155

CMU SCS Database Lock Hierarchy Faloutsos/PavloCMU SCS /6156 Database Table 1Table 2 Tuple 1 Attr 1 Tuple 2 Attr 2 Tuple n … Attr n … T1

CMU SCS Example T1: Get the balance of Christos’ shady off- shore bank account. T2: Increase all account balances by 1%. Q: What locks should they obtain? Faloutsos/PavloCMU SCS /6157

CMU SCS Example Q: What locks should they obtain? A: Multiple –Exclusive + Shared for leafs of lock tree. –Special Intention locks for higher levels Faloutsos/PavloCMU SCS /6158

CMU SCS Intention Locks Intention locks allow a higher level node to be locked in S or X mode without having to check all descendent nodes. If a node is in an intention mode, then explicit locking is being done at a lower level in the tree. Faloutsos/PavloCMU SCS /6159

CMU SCS Intention Locks Intention-Shared (IS): Indicates explicit locking at a lower level with shared locks. Intention-Exclusive (IX): Indicates locking at lower level with exclusive or shared locks Shared+Intention-Exclusive (SIX): The subtree rooted by that node is locked explicitly in shared mode and explicit locking is being done at a lower level with exclusive-mode locks. Faloutsos/PavloCMU SCS /61510

CMU SCS Compatibility Matrix Faloutsos/PavloCMU SCS /61511 ISIXSSIXX IS ✔✔✔✔ X IX ✔ XXX S ✔ XX SIX XX X X T1 Holds T2 Wants

CMU SCS Multiple Granularity Protocol Faloutsos/PavloCMU SCS /61512 IS SIX SIX X Privileges Stronger Weaker

CMU SCS Locking Protocol Each txn obtains appropriate lock at highest level of the database hierarchy. To get S or IS lock on a node, the txn must hold at least IS on parent node. –What if txn holds SIX on parent? S on parent? To get X, IX, or SIX on a node, must hold at least IX on parent node. Faloutsos/PavloCMU SCS /61513

CMU SCS Example – Two-level Hierarchy Faloutsos/PavloCMU SCS /61514 Table R Tuple 2Tuple 1 Tuple n … T1 S IS T1 T2 X IX T2 Read Write

CMU SCS Example – Threesome Assume three txns execute at same time: –T1: Scan R and update a few tuples. –T2: Scan a portion of tuples in R. –T3: Scan all tuples in R. Faloutsos/PavloCMU SCS /61515 Table R Tuple 2Tuple 1 Tuple n …

CMU SCS Example – Threesome Faloutsos/PavloCMU SCS /61516 Table R Tuple 1 Tuple n … T1 S SIX T1 T3 X T1 IS T2 Read Write T2 Write S T3 Tuple 2 X T3 S

CMU SCS Example – Threesome T1: Get an SIX lock on R, then get X lock on tuples that are updated. T2: Get an IS lock on R, and repeatedly get an S lock on tuples of R. T3: Two choices: –T3 gets an S lock on R. –OR, T3 could behave like T2; can use lock escalation to decide which. Faloutsos/PavloCMU SCS /61517

CMU SCS Lock Escalation Lock escalation dynamically asks for coarser-grained locks when too many low level locks acquired. Faloutsos/PavloCMU SCS /61518

CMU SCS Multiple Lock Granularities Useful in practice as each txn only needs a few locks. Intention locks help improve concurrency: –Intention-Shared (IS): Intent to get S lock(s) at finer granularity. –Intention-Exclusive (IX): Intent to get X lock(s) at finer granularity. –Shared+Intention-Exclusive (SIX): Like S and IX at the same time. Faloutsos/PavloCMU SCS /61519

CMU SCS Today’s Class Lock Granularities Locking in B+Trees The Phantom Problem Transaction Isolation Levels Faloutsos/PavloCMU SCS /61520

CMU SCS Locking in B+Trees Q: What about locking indexes? A: They are not quite like other database elements so we can treat them differently: –It’s okay to have non-serializable concurrent access to an index as long as the accuracy of the index is maintained. Faloutsos/PavloCMU SCS /61521

CMU SCS Example T1 wants to insert in H T2 wants to insert in I Q: Why not plain 2PL? A: Because txns have to hold on to their locks for too long! Faloutsos/PavloCMU SCS /61522 GIH FED CB A... X T1 X X X root

CMU SCS Lock Crabbing Improves concurrency for B+Trees. Get lock for parent; get lock for child; release lock for parent if “safe”. Safe Nodes: Any node that won’t split or merge when updated. –Not full (on insertion) –More than half-full (on deletion) Faloutsos/PavloCMU SCS /61523

CMU SCS Lock Crabbing Search: Start at root and go down; repeatedly, –S lock child –then unlock parent Insert/Delete: Start at root and go down, obtaining X locks as needed. Once child is locked, check if it is safe: –If child is safe, release all locks on ancestors. Faloutsos/PavloCMU SCS /61524

CMU SCS Example #1 – Search 38 Faloutsos/PavloCMU SCS / A B FC GHIDE 3510 SSSS

CMU SCS 3841 Example #2 – Delete 38 Faloutsos/PavloCMU SCS / A B FC GHIDE 3510 XXXX We know that C will not need to merge with F, so it’s safe to release A+B.

CMU SCS 3841 Example #3 – Insert 45 Faloutsos/PavloCMU SCS / A B FC GHIDE 3510 XXXX E has room so it won’t split, so we can release B+C. We know that if C needs to split, B has room so it’s safe to release A.

CMU SCS 3841 Example #4 – Insert 25 Faloutsos/PavloCMU SCS / A B FC GHIDE 3510 XXXX We need to split H so we need to keep the lock on its parent node.

CMU SCS Problems Q: What was the first step that all of the update examples did on the B+Tree? Faloutsos/PavloCMU SCS / A X Delete A X Insert A X Insert 25

CMU SCS Problems Q: What was the first step that all of the update examples did on the B+Tree? A: Locking the root every time becomes a bottleneck with higher concurrency. Can we do better? Faloutsos/PavloCMU SCS /61530

CMU SCS Better Tree Locking Algorithm Main Idea: –Assume that the leaf is ‘safe’, and use S-locks & crabbing to reach it, and verify. –If leaf is not safe, then do previous algorithm. Rudolf Bayer, Mario Schkolnick: Concurrency of Operations on B-Trees. Acta Inf. 9: 1-21 (1977) Faloutsos/PavloCMU SCS /61531

CMU SCS Better Tree Locking Algorithm Search: Same as before. Insert/Delete: –Set locks as if for search, get to leaf, and set X lock on leaf. –If leaf is not safe, release all locks, and restart txn using previous Insert/Delete protocol. Gambles that only leaf node will be modified; if not, S locks set on the first pass to leaf are wasteful. Faloutsos/PavloCMU SCS /61532

CMU SCS 3841 Example #2 – Delete 38 Faloutsos/PavloCMU SCS / A B FC GHIDE 3510 SSSX D will not need to coalesce, so we’re safe!

CMU SCS 3841 Example #4 – Insert 25 Faloutsos/PavloCMU SCS / A B FC GHIDE 3510 SSSX 25 We need to split H so we have to restart and re- execute like before.

CMU SCS Another Alternative Textbook has a third variation, that uses lock-upgrades instead of restarting. This approach may lead to deadlocks. Faloutsos/PavloCMU SCS /61535

CMU SCS Additional Points Q: Which order to release locks in multiple- granularity locking? A: From the bottom up Q: Which order to release locks in tree- locking? A: As early as possible to maximize concurrency. Faloutsos/PavloCMU SCS /61536

CMU SCS Today’s Class Lock Granularities Locking in B+Trees The Phantom Problem Transaction Isolation Levels Faloutsos/PavloCMU SCS /61537

CMU SCS Dynamic Databases Recall that so far we have only dealing with transactions that read and update data. But now if we have insertions deletions, we have new problems… Faloutsos/PavloCMU SCS /61538

CMU SCS TIME The Phantom Problem Faloutsos/PavloCMU SCS /61539 BEGIN COMMIT T1 T2 Schedule SELECT MAX(age) FROM sailors WHERE rating=1 BEGIN COMMIT INSERT INTO sailors (age=96, rating=1) SELECT MAX(age) FROM sailors WHERE rating=

CMU SCS How did this happen? Because T1 locked only existing records and not ones under way! Conflict serializability on reads and writes of individual items guarantees serializability only if the set of objects is fixed. Solution? Faloutsos/PavloCMU SCS /61540

CMU SCS Predicate Locking Lock records that satisfy a logical predicate: –Example: rating=1. In general, predicate locking has a lot of locking overhead. Index locking is a special case of predicate locking that is potentially more efficient. Faloutsos/PavloCMU SCS /61541

CMU SCS Index Locking If there is a dense index on the rating field then the txn can lock index page containing the data with rating=1. If there are no records with rating=1, the txn must lock the index page where such a data entry would be, if it existed. Faloutsos/PavloCMU SCS /61542

CMU SCS Locking without an Index If there is no suitable index, then the txn must obtain: –A lock on every page in the table to prevent a record’s rating from being changed to 1. –The lock for the table itself to prevent records with rating=1 from being added or deleted. Faloutsos/PavloCMU SCS /61543

CMU SCS Phantom Problem Faloutsos/PavloCMU SCS /61544

CMU SCS Today’s Class Lock Granularities Locking in B+Trees The Phantom Problem Weaker Levels of Consistency Faloutsos/PavloCMU SCS /61545

CMU SCS Weaker Levels of Consistency Serializability is useful because it allows programmers to ignore concurrency issues. But enforcing it may allow too little concurrency and limit performance. We may want to use a weaker level of consistency to improve scalability. Faloutsos/PavloCMU SCS /61546

CMU SCS Isolation Levels Controls the extent that a txn is exposed to the actions of other concurrent txns. Provides for greater concurrency at the cost of exposing txns to uncommitted changes: –Dirty Reads –Unrepeatable Reads –Phantom Reads Faloutsos/PavloCMU SCS /61547

CMU SCS Isolation Levels SERIALIZABLE: No phantoms, all reads repeatable, no dirty reads. REPEATABLE READS: Phantoms may happen. READ COMMITTED: Phantoms and unrepeatable reads may happen. READ UNCOMMITTED: All of them may happen. Faloutsos/PavloCMU SCS /61548 Isolation (High→Low)

CMU SCS Isolation Levels Faloutsos/PavloCMU SCS /61549 Dirty Read Unrepeatable ReadPhantom READ UNCOMMITTED Maybe READ COMMITTED NoMaybe REPEATABLE READ No Maybe SERIALIZABLE No

CMU SCS Isolation Levels SERIALIZABLE: Obtain all locks first; plus index locks, plus strict 2PL. REPEATABLE READS: Same as above, but no index locks. READ COMMITTED: Same as above, but S locks are released immediately. READ UNCOMMITTED: Same as above, but allows dirty reads (no S locks). Faloutsos/PavloCMU SCS /61550

CMU SCS SQL-92 Isolation Levels Default: SERIALIZABLE Not all DBMS support all isolation levels in all execution scenarios (e.g., replication). Faloutsos/PavloCMU SCS /61551 SET TRANSACTION ISOLATION LEVEL ;

CMU SCS Access Modes You can also provide hints to the DBMS about whether a txn will modify the database. Only two possible modes: –READ WRITE –READ ONLY Faloutsos/PavloCMU SCS /61552

CMU SCS SQL-92 Access Modes Default: READ WRITE Not all DBMSs will optimize execution if you set a txn to in READ ONLY mode. Faloutsos/PavloCMU SCS /61553 SET TRANSACTION ; START TRANSACTION ; SQL-92 Postgres + MySQL 5.6

CMU SCS Transaction Demo Faloutsos/PavloCMU SCS /61554

CMU SCS Summary Multiple granularity locking: leads to few locks, at appropriate levels Tree-structured indexes: –Lock crabbing and safe nodes Important distinction: –Multiple granularity locking releases locks bottom-up. –Tree-locking releases top-down to maximize concurrency. Faloutsos/PavloCMU SCS /61555

CMU SCS Summary The Phantom Problem occurs if insertions/deletions Use Predicate locking to prevent this: –Index Locking –Table Locking Faloutsos/PavloCMU SCS /61556