Concurrency Control II

Slides:



Advertisements
Similar presentations
CM20145 Concurrency Control
Advertisements

Concurrency Control III. General Overview Relational model - SQL Formal & commercial query languages Functional Dependencies Normalization Physical Design.
Lecture plan Transaction processing Concurrency control
1 Concurrency Control III Dead Lock Time Stamp Ordering Validation Scheme.
1 Shivnath Babu Concurrency Control (II) CS216: Data-Intensive Computing Systems.
Database Systems (資料庫系統)
More About Transaction Management Chapter 10. Contents Transactions that Read Uncommitted Data View Serializability Resolving Deadlocks Distributed Databases.
Unit 9 Concurrency Control. 9-2 Wei-Pang Yang, Information Management, NDHU Content  9.1 Introduction  9.2 Locking Technique  9.3 Optimistic Concurrency.
1 Concurrency Control Chapter Conflict Serializable Schedules  Two actions are in conflict if  they operate on the same DB item,  they belong.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
1 Concurrency Control Conflict serializability Two phase locking Optimistic concurrency control Source: slides by Hector Garcia-Molina.
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.
Concurrency Control Amol Deshpande CMSC424. Approach, Assumptions etc.. Approach  Guarantee conflict-serializability by allowing certain types of concurrency.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17 Sections
1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 5: Tree-based Concurrency Control and Validation Currency Control Professor.
Lecture 11 Recoverability. 2 Serializability identifies schedules that maintain database consistency, assuming no transaction fails. Could also examine.
1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 6: Cascading Rollbacks, Deadlocks, and Long Transactions Professor Chen Li.
1 More Transaction Management Concurrency control and recovery Resolving deadlocks Logical logging Source: slides by Hector Garcia-Molina.
Sekolah Tinggi Ilmu Statistik (STIS) 1 Dr. Said Mirza Pahlevi, M.Eng.
Quick Review of Apr 29 material
Concurrent Transactions Even when there is no “failure,” several transactions can interact to turn a consistent state into an inconsistent state.
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
CS 245Notes 101 CS 245: Database System Principles Notes 10: More TP Hector Garcia-Molina.
Transaction Processing: Concurrency and Serializability 10/4/05.
Transaction Management
ACS-4902 R. McFadyen 1 Chapter 18 Database Concurrency Control Locking 18.1 Shared/Exclusive (aka Read/Write) Locks Lock Operations Read_lock(X) Write_lock(X)
Cs4432concurrency control1 CS4432: Database Systems II Concurrency Control with Recovery.
©Silberschatz, Korth and Sudarshan16.1Database System Concepts 3 rd Edition Chapter 16: Concurrency Control Lock-Based Protocols Timestamp-Based Protocols.
Concurrency Control John Ortiz.
CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner.
© 1997 UW CSE 11/13/97N-1 Concurrency Control Chapter 18.1, 18.2, 18.5, 18.7.
Chapter 181 Chapter 18: Concurrency Control (Slides by Hector Garcia-Molina,
CS 245Notes 101 CS 245: Database System Principles Notes 10: More TP Hector Garcia-Molina.
ICS (072)Concurrency Control Techniques1 Concurrency Control Techniques Chapter 18 Dr. Muhammad Shafique.
Chapter 11 Concurrency Control. Lock-Based Protocols  A lock is a mechanism to control concurrent access to a data item  Data items can be locked in.
Concurrency Control Techniques Chapter 18
Chapter 15 Concurrency Control Yonsei University 1 st Semester, 2015 Sanghyun Park.
Concurrency Control Concurrency Control By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
Chapter 18 Concurrency Control Techniques Copyright © 2004 Pearson Education, Inc.
1 Concurrency Control II: Locking and Isolation Levels.
Concurrency Control Introduction Lock-Based Protocols
1 Concurrency control lock-base protocols timestamp-based protocols validation-based protocols Ioan Despi.
1 Database Systems ( 資料庫系統 ) December 27, 2004 Chapter 17 By Hao-hua Chu ( 朱浩華 )
1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.
Concurrency Control The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems, 4th edition,
Jinze Liu. Tree-based concurrency control Validation concurrency control.
1 Concurrency Control By Ankit Patel. 2 INTRODUCTION Enforcing serializability by locks Locks Locking scheduler Two phase locking Locking systems with.
DBMS Deadlock.
CS 245: Database System Principles Notes 10: More TP
Transaction Management
CS 245: Database System Principles Notes 10: More TP
Lecture 3 Concurrency control techniques
Concurrency Control Techniques
Concurrency Control.
Part- A Transaction Management
Chapter 16: Concurrency Control
Chapter 16: Concurrency Control
4. Concurrency control techniques
Ch 22: Databases Concurrency Control
Basic Two Phase Locking Protocol
Chapter 15 : Concurrency Control
Concurrency Control Chapter 17
Transaction Management
Concurrency Control Techniques
CONCURRENCY Concurrency is the tendency for different tasks to happen at the same time in a system ( mostly interacting with each other ) .   Parallel.
Database Systems (資料庫系統)
Lecture 18: Concurrency Control
Database Systems (資料庫系統)
Presentation transcript:

Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan Huang Learning Objectives Variations of two phase locking Dealing with Deadlock and Starvation Time Stamp Ordering Technique Validation Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Schedules Interleaved (or non-interleaved) actions from several transactions A schedule is good if it can be transformed into one of the serial schedules by switching two consecutive non-conflict actions We’ve learned 2PL to achieve serializability It is a pessimistic scheme Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Recall 2PL (two phase locking) Locks of Ti growing shrinking time Each transaction has to follow, no locking anymore after the first unlocking Database Implementation – Concurrency Control Yan Huang

Why 2PL serializability? Acyclic precedence graph = serializability Basic idea: No cycle in precedence graph Because if there is a arc from Ti to Tj in precedence graph, the first unlocking of Ti precede the first unlocking of Tj (proof details in ccI notes) If there is circle, you will have Ti < Ti Database Implementation – Concurrency Control Yan Huang

Who will follow 2PL in practice? Looks like it is DB application developers’ job. But, they can not be trusted  and too much work Checking conformity of every transaction is costly In practice, CC subsystems of DBMS take over the responsibility Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Variations of 2PL Basic 2PL Conservative 2PL Strict 2PL Rigorous 2PL Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Basic 2PL 2PL with binary locks Covered in last class Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Shared locks So far: S = ...l1(A) r1(A) u1(A) … l2(A) r2(A) u2(A) … Do not conflict Instead: S=... ls1(A) r1(A) ls2(A) r2(A) …. us1(A) us2(A) Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Lock actions l-ti(A): lock A in t mode (t is S or X) u-ti(A): unlock t mode (t is S or X) Shorthand: ui(A): unlock whatever modes Ti has locked A Database Implementation – Concurrency Control Yan Huang

Well formed transactions Ti =... l-S1(A) … r1(A) … u1 (A) … Ti =... l-X1(A) … w1(A) … u1 (A) … Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang What about transactions that read and write same object? Option 1: Request exclusive lock Ti = ...l-X1(A) … r1(A) ... w1(A) ... u1(A) … Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang What about transactions that read and write same object? Option 2: Upgrade (E.g., need to read, but don’t know if will write…) Ti=... l-S1(A) … r1(A) ... l-X1(A) …w1(A) ...u1(A)… Think of - Get 2nd lock on A, or - Drop S, get X lock Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Compatibility matrix Comp S X S true false X false false Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Schedule T1 T2 l-s1(A);Read(A) A A+100;Write(A) l-x1(B); u1(A) l-s2(A);Read(A) A Ax2;Write(A); l-x2(B) Read(B);B B+100 Write(B); u1(B) l-x2(B); u2(A);Read(B) B Bx2;Write(B);u2(B); delayed Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Conservative 2PL Lock all items it needs then transaction starts execution If any locks can not be obtained, then do not lock anything Difficult but deadlock free first action starts growing locks shrinking time Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Strict 2PL T does not release any write locks until it commits or aborts Good for recoverability Since reads or writes on what T writes Deadlock free? growing locks shrinking time First write unlock Database Implementation – Concurrency Control Yan Huang T commits or aborts

Database Implementation – Concurrency Control Yan Huang Rigorous 2PL T does not release any locks until it commits or aborts Easy to implement Deadlock free? T commits or aborts growing locks shrinking time Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Does basic 2PL guarantee serializability? Does conservative 2PL guarantee serializability? Does strict 2PL guarantee serializability? Does rigorous 2PL guarantee serializability? Database Implementation – Concurrency Control Yan Huang

Compare variations of 2PL Deadlock Only conservative 2PLis deadlock free Q: give a schedule of two transactions following 2PL but result in deadlock. Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Exercises: S1: r1(y)r1(x)w1(x)w2(x)w2(y) S2: r1(y)r3(x)w1(x)w3(x)w2(y)w2(x) S3: r3(y)w1(x)w3(x)r1(z)w2(y)w2(x) Assuming binary lock right before read or write; and rigorous 2PL (release all locks right after last operation), are S1, S2,and S3 possible? Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Deadlocks Detection Wait-for graph Prevention Resource ordering Timeout Wait-die Wound-wait Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Deadlock Detection Build Wait-For graph Use lock table structures Build incrementally or periodically When cycle found, rollback victim T5 T2 T1 T7 T4 T6 T3 Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Resource Ordering Order all elements A1, A2, …, An A transaction T can lock Ai after Aj only if i > j Problem : Ordered lock requests not realistic in most cases Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Timeout If transaction waits more than L sec., roll it back! Simple scheme Hard to select L Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Wait-die Transactions are given a timestamp when they arrive …. ts(Ti) Ti can only wait for Tj if ts(Ti)< ts(Tj) ...else die Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Example: T1 (ts =10) T2 (ts =20) T3 (ts =25) wait wait? wait Very high level: only older ones have the privilege to wait, younger ones die if they attempt to wait for older ones Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Wound-wait Transactions are given a timestamp when they arrive … ts(Ti) Ti wounds Tj if ts(Ti)< ts(Tj) else Ti waits “Wound”: Tj rolls back and gives lock to Ti Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Example: T1 (ts =25) T2 (ts =20) T3 (ts =10) wait wait wait Very high level: younger ones wait; older ones kill (wound) younger ones who hold needed locks Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Who die? Looks like it is always the younger ones either die automatically or killed What is the reason? Will the younger ones starve? Suggestions? Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Timestamp Ordering Key idea: Transactions access variables according to an order decided by their time stamps when they enter the system No cycles are possible in the precedence graph Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Timestamp System time when transactions starts An increasing unique number given to each stransaction Denoted by ts(Ti) Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang The way it works Two time stamps associated with each variable x RS(x): the largest time stamp of the transactions read it WS(x): the largest time stamp of the transactions write it Protocol: ri(x) is allowed if ts(Ti) >= WS(x) wi(x) is allowed if ts(Ti) >=WS(x) and ts(Ti) >=RS(x) Disallowed ri(x) or wi(x) will kill Ti, Ti will restart Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang x y z RS=-1 RS=-1 RS=-1 WS=-1 WS=-1 WS=-1 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); R (y); W(z); R(y); W(x); Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang x y z RS=100 RS=-1 RS=-1 WS=-1 WS=-1 WS=-1 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang x y z RS=100 RS=-1 RS=-1 WS=-1 WS=100 WS=-1 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang x y z RS=100 RS=200 RS=-1 WS=-1 WS=100 WS=-1 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); R (y); Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang x y z RS=100 RS=200 RS=-1 WS=-1 WS=100 WS=300 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); R (y); W(z); Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang x y z RS=200 RS=200 RS=-1 WS=-1 WS=100 WS=300 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); R (y); W(z); Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang x y z RS=200 RS=200 RS=-1 WS=-1 WS=100 WS=300 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); R (y); W(z); T1 is rolled back Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang x y z RS=200 RS=200 RS=-1 WS=-1 WS=100 WS=300 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); R (y); W(z); What happen to RS and WS? T1 is rolled back Database Implementation – Concurrency Control Yan Huang

Net result of TO scheduling Conflict pairs of actions are taken in the order of their home transactions But the basic TO does not guarantee recoverability later Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Validation An optimistic scheme Transactions have 3 phases: (1) Read all DB values read writes to temporary storage no locking (2) Validate check if schedule so far is serializable (3) Write if validate ok, write to DB Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Key idea Make validation atomic If T1, T2, T3, … is validation order, then resulting schedule will be conflict equivalent to Ss = T1 T2 T3... Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang To implement validation, system keeps two sets: FIN = transactions that have finished phase 3 (and are all done) VAL = transactions that have successfully finished phase 2 (validation) Database Implementation – Concurrency Control Yan Huang

Example of what validation must prevent: RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C}  =  T2 start T2 validated T3 validated T3 start time Database Implementation – Concurrency Control Yan Huang

Example of what validation must prevent: allow Example of what validation must prevent: RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C}  =  T2 start T2 validated T3 validated T3 start T2 finish phase 3 T3 start time Database Implementation – Concurrency Control Yan Huang

Another thing validation must prevent: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C,D} T2 validated T3 validated BAD: w3(D) w2(D) finish T2 time Database Implementation – Concurrency Control Yan Huang

Another thing validation must prevent: allow Another thing validation must prevent: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C,D} T2 validated T3 validated finish T2 finish T2 time Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang Validation Rule When start validating T Check RS(T)  WS(U) is empty for U that started but did not finish validation before T started Check WS(T)  WS(U) is empty for any U that started but did not finish validation T start validation Database Implementation – Concurrency Control Yan Huang

Database Implementation – Concurrency Control Yan Huang start validate finish Exercise: U: RS(U)={B} WS(U)={D} W: RS(W)={A,D} WS(W)={A,C} T: RS(T)={A,B} WS(T)={A,C} V: RS(V)={B} WS(V)={D,E} Database Implementation – Concurrency Control Yan Huang