Concurrency Control.

Slides:



Advertisements
Similar presentations
1 Shivnath Babu Concurrency Control (II) CS216: Data-Intensive Computing Systems.
Advertisements

Cs4432concurrency control1 CS4432: Database Systems II Lecture #21 Concurrency Control : Theory Professor Elke A. Rundensteiner.
1 Lecture 10: Transactions. 2 The Setting uDatabase systems are normally being accessed by many users or processes at the same time. wBoth queries and.
1 Concurrency Control Conflict serializability Two phase locking Optimistic concurrency control Source: slides by Hector Garcia-Molina.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control Professor Elke A. Rundensteiner.
1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 2: Enforcing Serializable Schedules Professor Chen Li.
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.
1 CS216 Advanced Database Systems Shivnath Babu Notes 12: Concurrency Control (II)
CS4432: Database Systems II Lecture #26 Concurrency Control and Recovery Professor Elke A. Rundensteiner.
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 17 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue Book.
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.
Transactions or Concurrency Control. Introduction A program which operates on a DB performs 2 kinds of operations: –Access to the Database (Read/Write)
Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)
Concurrency. Correctness Principle A transaction is atomic -- all or none property. If it executes partly, an invalid state is likely to result. A transaction,
CS 245Notes 091 CS 245: Database System Principles Notes 09: Concurrency Control Hector Garcia-Molina.
Concurrency Control 18.1 – 18.2 Chiu Luk CS257 Database Systems Principles Spring 2009.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #21 Concurrency Control Professor Elke A. Rundensteiner.
CS 277 – Spring 2002Notes 091 CS 277: Database System Implementation Notes 09: Concurrency Control Arthur Keller.
CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner.
Cs4432concurrency control1 CS4432: Database Systems II Concurrency Control.
CS4432: Database Systems II Transaction Management Motivation 1.
Chapter 181 Chapter 18: Concurrency Control (Slides by Hector Garcia-Molina,
CS 245Notes 091 CS 245: Database System Principles Notes 09: Concurrency Control Hector Garcia-Molina.
Cs4432concurrency control1 CS4432: Database Systems II Lecture #22 Concurrency Control: Locking-based Protocols Professor Elke A. Rundensteiner.
1 Notes 09: Transaction Processing Slides are modified from the CS 245 class slides of Hector Garcia- Molina.
DBMS 2001Notes 8: Concurrency1 Principles of Database Management Systems 8: Concurrency Control Pekka Kilpeläinen (after Stanford CS245 slide originals.
TRANSACTIONS. Objectives Transaction Concept Transaction State Concurrent Executions Serializability Recoverability Implementation of Isolation Transaction.
1 CSE232A: Database System Principle Concurrency Control.
1 Concurrency Control II: Locking and Isolation Levels.
Chapter 15: Transactions Loc Hoang CS 157B. Definition n A transaction is a discrete unit of work that must be completely processed or not processed at.
1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner.
1 Concurrency Control Lecture 22 Ramakrishnan - Chapter 19.
Jinze Liu. Option 1: run system, recording P(S); at end of day, check for P(S) cycles and declare if execution was good.
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:
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
Chapter 14: Transactions
Database Systems II Concurrency Control
CS216: Data-Intensive Computing Systems
Database Management System
CS 245: Database System Principles Notes 09: Concurrency Control
Transaction Management
CS 245: Database System Principles Notes 09: Concurrency Control
Transaction Management Overview
Cse 344 March 25th – Isolation.
March 21st – Transactions
Concurrency Control 11/22/2018.
Transaction Management
March 9th – Transactions
Lecture 21: Concurrency & Locking
Yan Huang - CSCI5330 Database Implementation – Concurrency Control
CS162 Operating Systems and Systems Programming Review (II)
Notes 09: Transaction Processing
Transaction Management Overview
Lecture 22: Intro to Transactions & Logging IV
Transaction management
CPSC-608 Database Systems
Transaction Management
Temple University – CIS Dept. CIS661 – Principles of Data Management
CPSC-608 Database Systems
Transaction Management Overview
CS 245: Database System Principles Notes 09: Concurrency Control
Introduction to Database Systems CSE 444 Lectures 17-18: Concurrency Control November 5-7, 2007.
CPSC-608 Database Systems
Data-Intensive Computing Systems
Database Systems (資料庫系統)
Lecture 18: Concurrency Control
Database Systems (資料庫系統)
Presentation transcript:

Concurrency Control

Concurrency Control T1 T2 … Tn DB (consistency constraints)

Transactions A transaction is an execution of a program having the following properties: Atomicity A transaction either happens or doesn’t. A transaction either completes and the results become visible or no results are visible. Consistency Transactions preserve correctness of database.

Isolation Each transaction is unaware of other transactions executing concurrently Durability The results of a completed transaction are permanently installed within D.B. ACID properties

A transaction has exactly one of two possible outcomes: a) Commit Program execution completes and the results become permanent in database. b) Abort Program execution was not successful. “Results” are not installed into database. Transactions may abort due to hardware failure, system error, bad input data, or as a means to ensure consistency.

Example: T1: Read(A) T2: Read(A) A  A+100 A  A2 Write(A) Write(A) Read(B) Read(B) B  B+100 B  B2 Write(B) Write(B) Constraint: A=B

Main idea of concurrency control An execution without any interleaving is OK serial: T1 ; T2 or T2 ; T1 If an execution has the same effect as a serial execution then it is also acceptable serializable Main goal of concurrency control: guarantee serializability

Serial Schedule A (“good” by definition) A B 25 25 125 250 250 250 T1 T2 Read(A); A  A+100 Write(A); Read(B); B  B+100; Write(B); Read(A);A  A2; Read(B);B  B2;

Serial Schedule B (equally “good”) A B 25 25 50 150 150 150 T1 T2 Read(A);A  A2; Write(A); Read(B);B  B2; Write(B); Read(A); A  A+100 Read(B); B  B+100;

Interleaved Schedule C (good because it is equivalent to A) A B 25 25 125 250 250 250 T1 T2 Read(A); A  A+100 Write(A); Read(A);A  A2; Read(B); B  B+100; Write(B); Read(B);B  B2; A and C are equivalent because if they start from same initial values they end up with same results

Interleaved Schedule D (bad!) A B 25 25 125 250 50 150 250 150 T1 T2 Read(A); A  A+100 Write(A); Read(A);A  A2; Read(B);B  B2; Write(B); Read(B); B  B+100;

Schedule E (good by “accident”) Same as Schedule D but with new T2’ Schedule E (good by “accident”) A B 25 25 125 25 125 125 T1 T2’ Read(A); A  A+100 Write(A); Read(A);A  A1; Read(B);B  B1; Write(B); Read(B); B  B+100; The accident being the particular semantics

Only look at order of read and writes Want schedules that are “good”, I.e., equivalent to serial regardless of initial state and transaction semantics Only look at order of read and writes

What we say to a database “delete all sailors not qualified to sail any boat” read, read, write, read, read…. What the database hears Example of a read/write schedule: SC=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B)

Definition S1, S2 are conflict equivalent schedules if S1 can be transformed into S2 by a series of swaps on non-conflicting actions. Non-conflicting actions: actions on different data read/read on the same data

Definition A schedule is conflict serializable if it is conflict equivalent to some serial schedule.

Example: SC=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B) SC’=r1(A)w1(A) r1(B)w1(B)r2(A)w2(A)r2(B)w2(B) T1 T2

SD=r1(A)w1(A)r2(A)w2(A) r2(B)w2(B)r1(B)w1(B) However, for SD: SD=r1(A)w1(A)r2(A)w2(A) r2(B)w2(B)r1(B)w1(B) as a matter of fact, T2 must precede T1 in any equivalent schedule, i.e., T2  T1 And vice versa

T1 T2 SD cannot be rearranged into a serial schedule Also, T1  T2 T1 T2 SD cannot be rearranged into a serial schedule SD is not “equivalent” to any serial schedule SD is “bad”

Returning to SC SC=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B) T1  T2 T1  T2  no cycles  SC is “equivalent” to a serial schedule (in this case T1,T2)

Precedence graph P(S) (S is schedule) Nodes: transactions in S Arcs: Ti  Tj whenever - pi(A), qj(A) are actions in S - pi(A) <S qj(A) - at least one of pi, qj is a write <S : order of appearance in schedule

Exercise: What is P(S) for S = w3(A) w2(C) r1(A) w1(B) r1(C) w2(A) r4(A) w4(D) Is S serializable? T1 T2 T3 T4

Lemma S1, S2 conflict equivalent  P(S1)=P(S2) Proof: Assume P(S1)  P(S2)   Ti, Tj: Ti  Tj in S1 and not in S2  S1 = …pi(A)... qj(A)… pi, qj S2 = …qj(A)…pi(A)... conflict  S1, S2 not conflict equivalent

Note: P(S1)=P(S2)  S1, S2 conflict equivalent Counter example: S1=w1(A) r2(A) w2(B) r1(B) S2=r2(A) w1(A) r1(B) w2(B)

Theorem P(S1) acyclic  S1 conflict serializable () Assume S1 is conflict serializable   Ss: Ss, S1 conflict equivalent  P(Ss) = P(S1)  P(S1) acyclic since P(Ss) is acyclic

Theorem P(S1) acyclic  S1 conflict serializable T1 T2 T3 () Assume P(S1) is acyclic Transform S1 as follows: (1) Take T1 to be transaction with no incoming arcs (2) Move all T1 actions to the front S1 = ……. qj(A)…….p1(A)….. (3) we now have S1 = < T1 actions ><... rest ...> (4) repeat above steps to serialize rest!

How to enforce serializable schedules? Option 1: run system, recording P(S); check for P(S) cycles and declare if execution was good; or abort transactions as soon as they generate a cycle

How to enforce serializable schedules? Option 2: prevent P(S) cycles from occurring T1 T2 ….. Tn Scheduler DB

A locking protocol Two new actions: lock (exclusive): li (A) unlock: ui (A) T1 T2 lock table scheduler

Rule #1: Well-formed transactions Ti: … li(A) … pi(A) … ui(A) ...

Rule #2 Legal scheduler S = …….. li(A) ………... ui(A) ……... no lj(A)

Exercise: What schedules are legal? What transactions are well-formed? S1 = l1(A)l1(B)r1(A)w1(B)l2(B)u1(A)u1(B) r2(B)w2(B)u2(B)l3(B)r3(B)u3(B) S2 = l1(A)r1(A)w1(B)u1(A)u1(B) l2(B)r2(B)w2(B)l3(B)r3(B)u3(B) S3 = l1(A)r1(A)u1(A)l1(B)w1(B)u1(B) l2(B)r2(B)w2(B)u2(B)l3(B)r3(B)u3(B)

Exercise: What schedules are legal? What transactions are well-formed? S1 = l1(A)l1(B)r1(A)w1(B)l2(B)u1(A)u1(B) r2(B)w2(B)u2(B)l3(B)r3(B)u3(B) S2 = l1(A)r1(A)w1(B)u1(A)u1(B) l2(B)r2(B)w2(B)l3(B)r3(B)u3(B) S3 = l1(A)r1(A)u1(A)l1(B)w1(B)u1(B) l2(B)r2(B)w2(B)u2(B)l3(B)r3(B)u3(B)

Just having locks is not enough! Example: this allows Schedule F (bad) T1 T2 l1(A);Read(A) A A+100;Write(A);u1(A) l2(A);Read(A) A Ax2;Write(A);u2(A) l2(B);Read(B) B Bx2;Write(B);u2(B) l1(B);Read(B) B B+100;Write(B);u1(B)

Rule #3 Two phase locking (2PL) for transactions Ti = ……. li(A) ………... ui(A) ……... no unlocks no locks

# locks held by Ti Time Growing Shrinking Phase Phase

2PL prevents Schedule F: T1 T2 l1(A);Read(A) A A+100;Write(A) l1(B); u1(A) l2(A);Read(A) A Ax2;Write(A);l2(B) delayed

2PL prevents Schedule F: T1 T2 l1(A);Read(A) A A+100;Write(A) l1(B); u1(A) l2(A);Read(A) A Ax2;Write(A);l2(B) Read(B);B B+100 Write(B); u1(B) delayed

2PL prevents Schedule F: T1 T2 l1(A);Read(A) A A+100;Write(A) l1(B); u1(A) l2(A);Read(A) A Ax2;Write(A);l2(B) Read(B);B B+100 Write(B); u1(B) l2(B); u2(A);Read(B) B Bx2;Write(B);u2(B); delayed

2PL may however result in deadlock: T1 T2 l1(A); Read(A) l2(B);Read(B) A A+100;Write(A) B Bx2;Write(B) l1(B) l2(A) delayed delayed Usual fix: deadlocked transactions are aborted and rolled back

Next step: Show that rules #1,2,3  conflict- serializable schedules

Theorem Rules #1,2,3  conflict (2PL) serializable schedule To help in proof: Definition Shrink(Ti) = SH(Ti) = first unlock action of Ti

Lemma Ti  Tj in S  SH(Ti) <S SH(Tj) Proof of lemma: Ti  Tj means that S = … pi(A) … qj(A) …; p,q conflict By rules 1,2: S = … pi(A) … ui(A) … lj(A) ... qj(A) … By rule 3: SH(Ti) SH(Tj) So, SH(Ti) <S SH(Tj)

Theorem Rules #1,2,3  conflict (2PL) serializable schedule Proof: (1) Assume P(S) has cycle T1  T2 …. Tn  T1 (2) By lemma: SH(T1) < SH(T2) < ... < SH(T1) (3) Impossible, so P(S) acyclic (4)  S is conflict serializable

How does locking work in practice? Every system is different But here is one (simplified) way ...

Sample Locking System: (1) Don’t trust transactions to request/release locks (2) Hold all locks until transaction commits # locks time

l(A),Read(A),l(B),Write(B)… Scheduler, part I lock table Read(A),Write(B) l(A),Read(A),l(B),Write(B)… Scheduler, part I lock table Scheduler, part II DB

What are the objects we lock? Relation A Tuple A Disk block A Tuple B Relation B Tuple C Disk block B ... ... ... DB DB DB

If we lock large objects (e.g., Relations) Locking works in any case, but should we choose small or large objects? If we lock large objects (e.g., Relations) Need few locks Low concurrency If we lock small objects (e.g., tuples,fields) Need more locks More concurrency

Beyond this simple 2PL protocol, it is all a matter of improving performance and allowing more concurrency…. Shared locks Multiple granularity Inserts, deletes Other types of C.C. mechanisms Weaker guarantees (e.g. in the cloud)