Introduction to Database Systems1 Concurrency Control CC.Lecture 1.

Slides:



Advertisements
Similar presentations
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Advertisements

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
1 Integrity Ioan Despi Transactions: transaction concept, transaction state implementation of atomicity and durability concurrent executions serializability,
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
TRANSACTION PROCESSING SYSTEM ROHIT KHOKHER. TRANSACTION RECOVERY TRANSACTION RECOVERY TRANSACTION STATES SERIALIZABILITY CONFLICT SERIALIZABILITY VIEW.
Transactions (Chapter ). What is it? Transaction - a logical unit of database processing Motivation - want consistent change of state in data Transactions.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. –Because disk accesses are.
Transaction Management Overview R & G Chapter 16 There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget.
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
Transaction Management Overview R & G Chapter 16 There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget.
1 Concurrency Control and Recovery Module 6, Lecture 1.
Transaction Management Overview R & G Chapter 16 There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget.
1 Transaction Management Overview Yanlei Diao UMass Amherst March 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
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.
Transaction Management
1 Lecture 08: Transaction management overview
Transaction Processing
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.
CS4432: Database Systems II Transaction Management Motivation 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
Transaction Processing Concepts
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
1 Transaction Management Overview Chapter Transactions  A transaction is the DBMS’s abstract view of a user program: a sequence of reads and writes.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
Transactions Sylvia Huang CS 157B. Transaction A transaction is a unit of program execution that accesses and possibly updates various data items. A transaction.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Lecture 21 Ramakrishnan - Chapter 18.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
International Computer Institute, Izmir, Turkey Transactions Asst. Prof. Dr. İlker Kocabaş UBİ502 at
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.
1 Transaction Processing Chapter Transaction Concept A transaction is a unit of program execution that accesses and possibly updates various data.
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.
Transactions. What is it? Transaction - a logical unit of database processing Motivation - want consistent change of state in data Transactions developed.
II.I Selected Database Issues: 2 - Transaction ManagementSlide 1/20 1 II. Selected Database Issues Part 2: Transaction Management Lecture 4 Lecturer: Chris.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 14: Transactions.
Transaction Management and Recovery, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
1 Database Systems ( 資料庫系統 ) December 27/28, 2006 Lecture 13 Merry Christmas & New Year.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 16.
1 Concurrency Control. 2 Why Have Concurrent Processes? v Better transaction throughput, response time v Done via better utilization of resources: –While.
Transaction Management and Concurrency
Chapter 14: Transactions
Transaction Management Overview
Database Systems (資料庫系統)
Database Management System
Transaction Management Overview
Transaction Management
Transaction Management Overview
Temple University – CIS Dept. CIS661 – Principles of Data Management
Transaction Management Overview
Transaction Management
Transaction Management Overview
Concurrency.
Transactions Sylvia Huang CS 157B.
Lecture 21: Concurrency & Locking
Lecture 21: Intro to Transactions & Logging III
Transaction management
Transaction Management
Database Management systems Subject Code: 10CS54 Prepared By:
Transaction Management Overview
C. Faloutsos Transactions
Temple University – CIS Dept. CIS616– Principles of Data Management
Transaction Management Overview
UNIT -IV Transaction.
Transaction Management Overview
Presentation transcript:

Introduction to Database Systems1 Concurrency Control CC.Lecture 1

Introduction to Database Systems2 Why Have Concurrent Processes? v Better transaction throughput, response time v Done via better utilization of resources: –while one processes is doing a disk read, another can be using the CPU or reading another disk  DANGER!  DANGER! Concurrency could lead to incorrectness! –Must carefully manage concurrent data access –There’s (much!) more here than the usual OS tricks!

Introduction to Database Systems3 Transactions v Basic concurrency/recovery concept: a Transaction (Xact) –a sequence of many actions (hence the name) which are considered to be one atomic unit of work v DBMS “actions”: –reads, writes –special actions: commit, abort –for now, assume reads and writes are on tuples; we’ll revisit this assumption later.

Introduction to Database Systems4 ACID The ACID Properties v A v A tomicity: all actions in the Xact happen, or none happen v C v C onsistency: if each Xact is consistent, and the DB starts consistent, it ends up consistent v I v I solation: execution of one Xact is isolated from that of other Xacts v D v D urability: if a Xact commits, its effects persist

Introduction to Database Systems5 Passing the ACID Test v Concurrency Control –guarantees Consistency and Isolation v Logging and Recovery –guarantees Atomicity and Durability v We’ll do C. C. first: –what problems could arise? –what is acceptable behavior? –how do we guarantee acceptable behavior?

Introduction to Database Systems6 Schedules v Schedule: An interleaving of actions from a set of Xacts, where the actions of any 1 Xact are in the original order. –represents some actual sequence of database actions –example: R 1 (A), W 1 (A), R 2 (B), W 2 (B), R 1 (C), W 1 (C) –in a complete schedule, each Xact ends in Commit or Abort  Initial State + Schedule  Final State

Introduction to Database Systems7 Acceptable Schedules v One sensible “isolated, consistent” schedule: –run Xacts one at a time, in a series. –this is called a serial schedule –NOTE: different serial schedules can have different final states; why? all are “OK”. v Serializable schedules: –final state is what some serial schedule would have produced –aborted Xacts are not part of schedule, ignore them for now.

Introduction to Database Systems8 Serializability Violations v Two actions conflict when 2 xacts access the same item: –W-R conflict: T2 reads something T1 wrote –R-W and W-W are similar v WR conflict (dirty read) –result is not equal to any serial execution! transfer $100 from A to B add 6% interest to A & B Database is inconsistent!

Introduction to Database Systems9 More Conflicts v RW Conflicts (Unrepeatable Read) –T2 overwrites what T1 read –If T1 reads it again, it will see something new! u Example when this would happen? u The increment/decrement example –Again, not equivalent to a serial execution v WW Conflicts (Overwriting Uncommited Data) –T2 overwrites what T1 wrote u Example: 2 Xacts to update items to be kept equal –Usually occurs in conjunction w/other anomalies u unless you have “blind writes”

Introduction to Database Systems10 Now, Aborted Transactions v Serializable schedule: equivalent to a serial schedule of committed Xacts –as if aborted Xacts never happened v Two Issues: –How does one undo the effects of an xact? u we’ll cover this in logging/recovery –What if another Xact sees these effects??

Introduction to Database Systems11 Cascading Aborts v Abort of T1 requires abort of T2! –Cascading Abort v A Recoverable Schedule is one in which cascading abort cannot happen. –i.e. a Xact commits only after all the Xacts it “depends on” (i.e. it reads from) commit v What about WW & aborts? –T2 overwrites a value that T1 writes –T1 aborts: its “remembered” values are restored –lose T2’s write! We will see how to solve this, too.

Introduction to Database Systems12 Degrees of Consistency v 3 distinct notions of consistency v Defn 1: Absence of anomalies –purely logical definition (thou shalt not steal!) v Defn 2: Analysis of conflicts in schedules –post-priori detection (did something get stolen?) v Defn 3: Using locking –one evaluation procedure (install burglar alarm)

Introduction to Database Systems13 Consistency: Defn 1 v Degree 3 (serializable, recoverable schedule) –No overwrites of uncommitted updates (W-W) –No cascading aborts (I.e. recoverable schedules) –No dirty reads (W-R) –No unrepeatable reads (R-W) v Degree 2 : Degree 3, but unrepeatable reads –Also called “Cursor Stability” v Degree 1 : Degree 2, but dirty reads v Degree 0 : Degree 1, but unrecoverable

Introduction to Database Systems14 Precedence Graph v A Precedence (or Serializability) graph: –node for each commited Xact –arc from Ti to Tj if an action of Ti precedes and conflicts with an action of Tj v T1 transfers $100 from A to B, T2 adds 6% –R 1 (A), W 1 (A), R 2 (A), W 2 (A), R 2 (B), W 2 (B), R 1 (B), W 1 (B) T1T2

Introduction to Database Systems15 Conflict Serializability v 2 schedules are conflict equivalent if: –they have the same sets of actions –each pair of conflicting actions is ordered in the same way v A schedule is conflict serializable if it is conflict equivalent to a serial schedule –note: some serializable schedules are not conflict serializable!

Introduction to Database Systems16 Defn 2 (ignore Recoverability) v Based on analysis of complete schedules. v Actions a1(T1) and a2(T2) on same data object. v T1 <<< T2 if (a1,a2) = (W,W),(W,R) or (R,W) v T1 << T2 if (a1,a2) = (W,W) or (W,R) v T1 < T2 if (a1,a2) = (W,W) v Degree 3 : <<< graph is acyclic v Degree 2 : << graph is acyclic v Degree 1 : < graph is acyclic

Introduction to Database Systems17 Locking: A Technique for C. C. v Concurrency control usually done via locking v Lock info maintained by a “lock manager” –stores (XID, RID, Mode) triples u (this is a simplistic view for now) –Mode  {S,X} –Lock compatibility table v If a Xact can’t get a lock, it is suspended until it can be granted -- SX S X     

Introduction to Database Systems18 Two-Phase Locking (2PL) v 2PL: –if T wants to read an object, first obtains an S lock –if T wants to modify an object, first obtains X lock –If T releases any lock, it can acquire no new locks! v Locks are automatically obtained by DBMS v Guarantees Serializability!! –why? Time # of locks lock point growing phase shrinking phase

Introduction to Database Systems19 Strict 2PL v Strict 2PL: –if T wants to read an object, first obtains an S lock –if T wants to modify an object, first obtains X lock –Hold all locks until end of transaction v Guarantees recoverable schedule, too! –also avoids WW problems! Time # of locks

Introduction to Database Systems20 Defn 3: based on locking v Degree 3 –long X lock on data written –long S lock on data read v Degree 2 : –long X lock on data written –short S lock on data read v Degree 1 : –long X lock on data written v Degree 0 : –short X lock on data written

Introduction to Database Systems21 Conflict Serializability & Graphs v Theorem: A schedule is conflict serializable iff its precedence graph is acyclic. v Theorem: 2PL ensures that the precedence graph will be acyclic! v Strict 2PL improves on this by avoiding cascading aborts, problems with undoing WW conflicts v Strict 2PL: The Magic Bullet for C. C.! –All we need is to enforce Strict 2PL, and life is good. Right?

Introduction to Database Systems22 Lock Manager Implementation v Question 1: what are we locking? –tuples, pages, tables? –finer granularity increases concurrency, but also increases locking overhead v Question 2: How do you “lock” something?? v Lock Table: a hash table of Lock Entries: –Lock Entry u OID u Lock Mode u List: Xacts holding lock u List: Wait Queue

Introduction to Database Systems23 Handling a Lock Request Lock Request (XID, OID, Mode) Currently Locked? Grant Lock Empty Wait Queue? Currently X? Mode==X Mode==S No Yes No Yes Put on Queue Yes No

Introduction to Database Systems24 More Lock Manager Logic v On lock release (OID, XID): –update list of Xacts holding lock –examine head of wait queue –if Xact there can run, add it to list of Xacts holding lock (change mode as needed) –repeat until head of wait queue cannot be run v Think about this scenario: –T1 locks A in S mode, T2 request X lock on A, T3 requests S lock on A v Note: lock request handled atomically! –via latches (i.e. semaphore/mutex; OS stuff)

Introduction to Database Systems25 Deadlock v Deadlocks : T1 waits for T2 and vice versa. –X1(A), X2(B), S1(B), S2(A) v Cycle of lock dependencies v Two basic strategies –Prevention : preempt the possibility of deadlock –Detection: handle it when it happens v Simple prevention strategy: –always access data items in a specific order

Introduction to Database Systems26 Deadlock Prevention v Assign a timestamp to each Xact as it enters the system. “Older” Xacts have priority. v Assume Ti requests a lock, but Tj holds a conflicting lock –Wait-Die: If Ti has higher priority, it waits; else Ti aborts –Wound-Wait: If Ti has higher priority, abort Tj; else Ti waits –both guarantee deadlock-free behavior! why? –too much wasted work X1(A), X2(B), S1(B), S2(A)

Introduction to Database Systems27 An Alternative to Prevention v In theory, deadlock can involve many transactions –T1 waits-for T2 waits-for T3...waits-for T1 v Fact: in practice, most “deadlock cycles” involve only 2 transactions v Don’t need to prevent deadlock! –what’s the problem with prevention? v Allow it to happen, then notice it and fix it –deadlock detection

Introduction to Database Systems28 Deadlock Detection v Lock Mgr maintains a “Waits-for” graph –node for each Xact –arc from Ti to Tj if Tj holds a lock and Ti is waiting for it v Periodically check graph for cycles –How often? Every request? v Kill some “victim” Xact to break the cycle –Choice of victim: Least work? Fewest locks? –Furthest from completion? Fewest restarts?

Introduction to Database Systems29 Prevention vs. Detection v Prevention might abort too many Xacts v Detection might tie up resources for a while v The usual answer: –detection is the winner –deadlocks are pretty rare –if you get a lot of deadlocks, reconsider your schema/workload! v Simple detection hack: time-outs –T1 made no progress for a while? Kill it.

Introduction to Database Systems30 Multiple-Granularity Locks v Hard to decide what granularity to lock (tuples vs. pages vs. tables) v Shouldn’t have to decide! v Data “containers” are nested Tables Pages Tuples Database contains

Introduction to Database Systems31 Solution: New Lock Modes, Protocol v Allow people to lock at each level, but with a special protocol using new “intention” locks: -- ISIX -- IS IX      SX   S X      v Before locking an item, you must set “intention locks” on all its ancestors v For unlock, go from specific to general (i.e. bottom-up) 

Introduction to Database Systems32 Summary of C.C. v Concurrency control key to a DBMS –more than mutexes v Transactions and the ACID properties –C & I are handled by concurrency control –A & D coming soon with logging & recovery v Conflicts arise when two Xacts access the same object, and one of the Xacts is modifying it v Serial execution is our model of correctness

Introduction to Database Systems33 Summary, cont. v Serializability allows us to “simulate” serial execution with better performance v 2PL: a simple mechanism to get serializability –Strict 2PL also gives us recoverability v Lock manager module automates 2PL so that only the access methods worry about it –lock table is a big main-memory hash table v Deadlocks are possible, and typically a deadlock detector is used.