Lecture 22: Intro to Transactions & Logging IV

Slides:



Advertisements
Similar presentations
Cs4432concurrency control1 CS4432: Database Systems II Lecture #21 Concurrency Control : Theory Professor Elke A. Rundensteiner.
Advertisements

Database Systems (資料庫系統)
1 Concurrency Control Chapter Conflict Serializable Schedules  Two actions are in conflict if  they operate on the same DB item,  they belong.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control Professor Elke A. Rundensteiner.
Concurrency Control II
Cs4432concurrency control1 CS4432: Database Systems II Lecture #23 Concurrency Control Professor Elke A. Rundensteiner.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control: Locking-based Protocols Professor Elke A. Rundensteiner.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17 Sections
Lecture 12 Transactions: Isolation. Transactions What’s hard? – ACID – Concurrency control – Recovery.
1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 6: Cascading Rollbacks, Deadlocks, and Long Transactions Professor Chen Li.
CS4432: Database Systems II Lecture #26 Concurrency Control and Recovery Professor Elke A. Rundensteiner.
Quick Review of Apr 29 material
ICS 421 Spring 2010 Transactions & Concurrency Control (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
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.
Concurrency Control R&G - Chapter 17 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue Book.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17.
Concurrency. Busy, busy, busy... In production environments, it is unlikely that we can limit our system to just one user at a time. – Consequently, it.
Transaction Processing: Concurrency and Serializability 10/4/05.
Concurrency. Correctness Principle A transaction is atomic -- all or none property. If it executes partly, an invalid state is likely to result. A transaction,
CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
Concurrency Control R &G - Chapter 19. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses.
Database Systems/COMP4910/Spring05/Melikyan1 Transaction Management Overview Unit 2 Chapter 16.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#21: Concurrency Control (R&G ch. 17)
 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.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Instructor: Xintao Wu.
1 Concurrency Control Chapter Conflict Serializable Schedules  Two schedules are conflict equivalent if:  Involve the same actions of the same.
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.
1 Database Systems ( 資料庫系統 ) December 27, 2004 Chapter 17 By Hao-hua Chu ( 朱浩華 )
Jinze Liu. ACID Atomicity: TX’s are either completely done or not done at all Consistency: TX’s should leave the database in a consistent state Isolation:
MULTIUSER DATABASES : Concurrency and Transaction Management.
Lectures 8 & 9 Transactions.
Transaction Management
Database Systems (資料庫系統)
Transaction Management Overview
CS216: Data-Intensive Computing Systems
Concurrency Control.
Transaction Management Overview
Lectures 7 & 8: Transactions
Transaction Management
Transaction Management Overview
Anthony D. Joseph and John Canny
Transaction Management
Transaction Management Overview
Lecture#21: Concurrency Control – Part 1
Concurrency Control Chapter 17
Lecture 21: Concurrency & Locking
CS162 Operating Systems and Systems Programming Review (II)
6.830 Lecture 12 Transactions: Isolation
Chapter 15 : Concurrency Control
Concurrency Control Chapter 17
Lecture 21: Intro to Transactions & Logging III
Concurrency Control R&G - Chapter 17
Transaction Management
CPSC-608 Database Systems
Transaction Management Overview
Database Systems (資料庫系統)
Lecture 18: Concurrency Control
Database Systems (資料庫系統)
Transaction Management Overview
Presentation transcript:

Lecture 22: Intro to Transactions & Logging IV Professor Xiannong Meng Spring 2018 Lecture and activity contents are based on what Prof Chris Ré of Stanford used in his CS 145 in the fall 2016 term with permission

2. Conflict Serializability, Locking & Deadlock

What you will learn about in this section RECAP: Concurrency Conflict Serializability DAGs & Topological Orderings Strict 2PL Deadlocks

Recall: Concurrency as Interleaving TXNs Serial Schedule: For our purposes, having TXNs occur concurrently means interleaving their component actions (R/W) T1 R(A) W(A) R(B) W(B) T2 R(A) W(A) R(B) W(B) We call the particular order of interleaving a schedule Interleaved Schedule: T1 R(A) W(A) R(B) W(B) T2 R(A) W(A) R(B) W(B)

Recall: “Good” vs. “bad” schedules Serial Schedule: Interleaved Schedules: T1 T2 R(A) R(B) W(A) W(B) T1 T2 R(A) R(B) W(A) W(B) X Why? T1 T2 R(A) R(B) W(A) W(B) We want to develop ways of discerning “good” vs. “bad” schedules

Ways of Defining “Good” vs. “Bad” Schedules Recall from last time: we call a schedule serializable if it is equivalent to some serial schedule We used this as a notion of a “good” interleaved schedule, since a serializable schedule will maintain isolation & consistency Now, we’ll define a stricter, but very useful variant: Conflict serializability We’ll need to define conflicts first..

Conflicts Two actions conflict if they are part of different TXNs, involve the same variable, and at least one of them is a write T1 T2 R(A) R(B) W(A) W(B) W-W Conflict W-R Conflict

Conflicts Two actions conflict if they are part of different TXNs, involve the same variable, and at least one of them is a write T1 T2 R(A) R(B) W(A) W(B) All “conflicts”!

Conflict Serializability Two schedules are conflict equivalent if: They involve the same actions of the same TXNs Every pair of conflicting actions of two TXNs are ordered in the same way Schedule S is conflict serializable if S is conflict equivalent to some serial schedule Conflict serializable ⇒ serializable So if we have conflict serializable, we have consistency & isolation!

Recall: “Good” vs. “bad” schedules Serial Schedule: Interleaved Schedules: T1 T2 R(A) R(B) W(A) W(B) T1 T2 R(A) R(B) W(A) W(B) X Note that in the “bad” schedule, the order of conflicting actions is different than the above (or any) serial schedule! T1 T2 R(A) R(B) W(A) W(B) Conflict serializability also provides us with an operative notion of “good” vs. “bad” schedules!

Note: Conflicts vs. Anomalies Conflicts are things we talk about to help us characterize different schedules Present in both “good” and “bad” schedules Anomalies are instances where isolation and/or consistency is broken because of a “bad” schedule We often characterize different anomaly types by what types of conflicts predicated them

The Conflict Graph Let’s now consider looking at conflicts at the TXN level Consider a graph where the nodes are TXNs, and there is an edge from Ti Tj if any actions in Ti precede and conflict with some actions in Tj T1 T2 R(A) R(B) W(A) W(B) T1 T2

What can we say about “good” vs. “bad” conflict graphs? Serial Schedule: Interleaved Schedules: T1 T2 R(A) R(B) W(A) W(B) T1 T2 R(A) R(B) W(A) W(B) X A bit complicated… T1 T2 R(A) R(B) W(A) W(B)

What can we say about “good” vs. “bad” conflict graphs? Serial Schedule: Interleaved Schedules: T1 T2 T1 T2 X T1 T2 Simple! Theorem: Schedule is conflict serializable if and only if its conflict graph is acyclic

Let’s unpack this notion of acyclic conflict graphs…

DAGs & Topological Orderings A topological ordering of a directed graph is a linear ordering of its vertices that respects all the directed edges A directed acyclic graph (DAG) always has one or more topological orderings (And there exists a topological ordering if and only if there are no directed cycles)

DAGs & Topological Orderings Ex: What is one possible topological ordering here? Ex: 0, 1, 2, 3 (or: 0, 1, 3, 2) 1 2 3

DAGs & Topological Orderings Ex: What is one possible topological ordering here? 1 There is none! 2 3

Relation to conflict serializability In the conflict graph, a topological ordering of nodes corresponds to a serial ordering of TXNs Thus an acyclic conflict graph  conflict serializable! Theorem: Schedule is conflict serializable if and only if its conflict graph is acyclic

Strict Two-Phase Locking We consider locking- specifically, strict two-phase locking- as a way to deal with concurrency, because is guarantees conflict serializability (if it completes- see upcoming…) Also (conceptually) straightforward to implement, and transparent to the user!

Strict Two-phase Locking (Strict 2PL) Protocol: Two types of lock- “exclusive” and “shared” are used. TXNs obtain: An X (exclusive) lock on object before writing. If a TXN holds an X lock, no other TXN can get a lock (S or X) on that object. An S (shared) lock on object before reading If a TXN holds an S lock , no other TXN can get an X lock on that object All locks held by a TXN are released when TXN completes. Note: Terminology here- “exclusive”, “shared”- meant to be intuitive- no tricks!

Strict 2PL Theorem: Strict 2PL allows only schedules whose dependency graph is acyclic Proof Intuition: In strict 2PL, if there is an edge Ti  Tj (i.e. Ti and Tj conflict) then Tj needs to wait until Ti is finished – so cannot have an edge Tj  Ti Therefore, Strict 2PL only allows conflict serializable ⇒ serializable schedules

Strict 2PL If a schedule follows strict 2PL and locking, it is conflict serializable… …and thus serializable …and thus maintains isolation & consistency! Not all serializable schedules are allowed by strict 2PL. So let’s use strict 2PL, what could go wrong?

Two-phase Locking (2PL) Protocol: A variation of Strict 2PL TXNs obtain: An X (exclusive) lock on object before writing. If a TXN holds an X lock, no other TXN can get a lock (S or X) on that object. An S (shared) lock on object before reading If a TXN holds an S lock , no other TXN can get an X lock on that object TXNs cannot request any locks once it releases any locks Note: 2PL says TXNs can release some locks before commit.

Deadlock Detection: Example Waits-for graph: T1 T1 T2 S(A) R(A) T2 First, T1 requests a shared lock on A to read from it

Deadlock Detection: Example Waits-for graph: T1 T1 T2 S(A) R(A) T2 S(B) R(B) Next, T2 requests a shared lock on B to read from it

Deadlock Detection: Example Waits-for graph: T1 T1 T2 S(A) R(A) Waiting… T2 S(B) R(B) X(A) W(A) T2 then requests an exclusive lock on A to write to it- now T2 is waiting on T1…

Deadlock Detection: Example Waits-for graph: T1 Waiting… T1 T2 S(A) R(A) X(B) W(B) Waiting… T2 S(B) R(B) X(A) W(A) Cycle = DEADLOCK Finally, T1 requests an exclusive lock on B to write to it- now T1 is waiting on T2… DEADLOCK!

T1 T2 The problem? Deadlock!??! ERROR: deadlock detected DETAIL: Process 321 waits for ExclusiveLock on tuple of relation 20 of database 12002; blocked by process 4924. Process 404 waits for ShareLock on transaction 689; blocked by process 552. HINT: See server log for query details. The problem? Deadlock!??! T1 T2 http://elioxman.blogspot.com/2013/02/postgres-deadlock.html

Deadlocks Deadlock: Cycle of transactions waiting for locks to be released by each other. Two ways of dealing with deadlocks: Deadlock prevention Deadlock detection

Deadlock Prevention There are a number of possible mechanisms to prevent deadlocks Do not allow “hold-and-wait”, the transaction holding a lock while requesting another must release the lock it is holding. Set priorities among transactions so that the transaction with higher priority can take the lock away from the ones with lower priority.

Deadlock Detection Create the waits-for graph: Nodes are transactions There is an edge from Ti  Tj if Ti is waiting for Tj to release a lock Periodically check for (and break) cycles in the waits-for graph

Summary Concurrency achieved by interleaving TXNs such that isolation & consistency are maintained We formalized a notion of serializability that captured such a “good” interleaving schedule We defined conflict serializability, which implies serializability Locking allows only conflict serializable schedules If the schedule completes… (it may deadlock!)