CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #17: Transac0ons 2: 2PL and Deadlocks

Similar documents
Review. Review. Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Lecture #21: Concurrency Control (R&G ch.

Page 1. Goals of Todayʼs Lecture" Two Key Questions" Goals of Transaction Scheduling"

Page 1. Goals of Today s Lecture" Two Key Questions" Goals of Transaction Scheduling"

Page 1. Goals of Today s Lecture. The ACID properties of Transactions. Transactions

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #17: Transac0ons 1: Intro. to ACID

CS 5614: (Big) Data Management Systems. B. Aditya Prakash Lecture #6: Transac/ons 1: Intro. to ACID

Concurrency control 12/1/17

Concurrency Control! Snapshot isolation" q How to ensure serializability and recoverability? " q Lock-Based Protocols" q Other Protocols"

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Last Class. Last Class. Faloutsos/Pavlo CMU /615

WarmGup#Exercise# CS#133:#Databases# TwoGPhase#Locking#(2PL)# Goals#for#Today#

Transaction Management and Concurrency Control. Chapter 16, 17

Page 1. CS194-3/CS16x Introduction to Systems. Lecture 8. Database concurrency control, Serializability, conflict serializability, 2PL and strict 2PL

Goal of Concurrency Control. Concurrency Control. Example. Solution 1. Solution 2. Solution 3

CAS CS 460/660 Introduction to Database Systems. Transactions and Concurrency Control 1.1

Today s Class. Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Formal Properties of Schedules. Conflicting Operations

Database design and implementation CMPSCI 645. Lectures 18: Transactions and Concurrency

Conflict Equivalent. Conflict Serializability. Example 1. Precedence Graph Test Every conflict serializable schedule is serializable

Transaction Overview and Concurrency Control

Lock Granularity and Consistency Levels (Lecture 7, cs262a) Ali Ghodsi and Ion Stoica, UC Berkeley February 7, 2018

Concurrency Control. Concurrency Control Ensures interleaving of operations amongst concurrent transactions result in serializable schedules

Transaction Management & Concurrency Control. CS 377: Database Systems

Transaction Management: Concurrency Control

Concurrency Control. R &G - Chapter 19

Chapter 22. Transaction Management

L i (A) = transaction T i acquires lock for element A. U i (A) = transaction T i releases lock for element A

Intro to Transaction Management

CSE 344 MARCH 9 TH TRANSACTIONS

Introduction to Data Management CSE 414

CSC 261/461 Database Systems Lecture 21 and 22. Spring 2017 MW 3:25 pm 4:40 pm January 18 May 3 Dewey 1101

Intro to Transactions

Why Transac'ons? Database systems are normally being accessed by many users or processes at the same 'me.

Phantom Problem. Phantom Problem. Phantom Problem. Phantom Problem R1(X1),R1(X2),W2(X3),R1(X1),R1(X2),R1(X3) R1(X1),R1(X2),W2(X3),R1(X1),R1(X2),R1(X3)

Including Aborts in Serializability. Conflict Serializable Schedules. Recall Conflicts. Conflict Equivalent

CSE 444: Database Internals. Lectures Transactions

TRANSACTION MANAGEMENT

Overview of Transaction Management

Database Systems CSE 414

Database System Concepts

Transaction Processing: Concurrency Control. Announcements (April 26) Transactions. CPS 216 Advanced Database Systems

Transaction Management Overview

Lock-based Concurrency Control

Concurrency Control. Chapter 17. Comp 521 Files and Databases Fall

h p:// Authors: Tomáš Skopal, Irena Holubová Lecturer: Mar n Svoboda, mar

CSE 344 MARCH 25 TH ISOLATION

Concurrency Control. Chapter 17. Comp 521 Files and Databases Spring

Concurrency Control. Chapter 17. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Intro to DB CHAPTER 15 TRANSACTION MNGMNT

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

Transaction Management: Introduction (Chap. 16)

Implementing Isolation

Concurrency Control & Recovery

CS377: Database Systems Concurrency Control. Li Xiong Department of Mathematics and Computer Science Emory University

2 nd Semester 2009/2010

Introduction to Data Management CSE 344

CMSC 424 Database design Lecture 22 Concurrency/recovery. Mihai Pop

Transaction Management Overview. Transactions. Concurrency in a DBMS. Chapter 16

Database Systems CSE 414

doc. RNDr. Tomáš Skopal, Ph.D.

Concurrency Control. [R&G] Chapter 17 CS432 1

Concurrency Control & Recovery

Database transactions

Slides Courtesy of R. Ramakrishnan and J. Gehrke 2. v Concurrent execution of queries for improved performance.

Database Management Systems

What are Transactions? Transaction Management: Introduction (Chap. 16) Major Example: the web app. Concurrent Execution. Web app in execution (CS636)

References. Transaction Management. Database Administration and Tuning 2012/2013. Chpt 14 Silberchatz Chpt 16 Raghu

Deadlock Prevention (cont d) Deadlock Prevention. Example: Wait-Die. Wait-Die

Transactions and Concurrency Control

Chapter 6 Distributed Concurrency Control

mywbut.com Concurrency Control

Introduction to Data Management CSE 344

Concurrency Control CHAPTER 17 SINA MERAJI

UNIT 4 TRANSACTIONS. Objective

Concurrency Control. Conflict Serializable Schedules. Example. Chapter 17

CSC 261/461 Database Systems Lecture 24

Transaction Processing. Introduction to Databases CompSci 316 Fall 2018

Lecture 21. Lecture 21: Concurrency & Locking

CSE 444: Database Internals. Lectures 13 Transaction Schedules

Introduction to Data Management. Lecture #18 (Transactions)

Database Management System

Chapter 15 : Concurrency Control

Transaction Management. Pearson Education Limited 1995, 2005

Lecture 21 Concurrency Control Part 1

Database Systems. Announcement

Concurrency Control Overview. COSC 404 Database System Implementation. Concurrency Control. Lock-Based Protocols. Lock-Based Protocols (2)

Lecture 13 Concurrency Control

Transaction Processing: Concurrency Control ACID. Transaction in SQL. CPS 216 Advanced Database Systems. (Implicit beginning of transaction)

Introduction to Transaction Management

Introduction to Data Management. Lecture #26 (Transactions, cont.)

Concurrency Control. Transaction Management. Lost Update Problem. Need for Concurrency Control. Concurrency control

CSE 344 MARCH 5 TH TRANSACTIONS

Chapter 12 : Concurrency Control

TRANSACTION PROCESSING CONCEPTS

Database Management System 21 Concurrency Control

Transactions and Concurrency Control

Chapter 13 : Concurrency Control

Introduction TRANSACTIONS & CONCURRENCY CONTROL. Transactions. Concurrency

UNIT IV TRANSACTION MANAGEMENT

UNIT-IV TRANSACTION PROCESSING CONCEPTS

6.830 Lecture Transactions October 23, 2017

! A lock is a mechanism to control concurrent access to a data item! Data items can be locked in two modes :

Transcription:

CS 4604: Introduc0on to Database Management Systems B. Aditya Prakash Lecture #17: Transac0ons 2: 2PL and Deadlocks

Review (last lecture) DBMSs support ACID Transac0on seman0cs. Concurrency control and Crash Recovery are key components Prakash 2015 VT CS 4604 2

Review For Isola0on property, serial execu0on of transac0ons is safe but slow Try to find schedules equivalent to serial execu0on One solu0on for conflict serializable schedules is Two Phase Locking (2PL) Prakash 2015 VT CS 4604 3

Outline 2PL/2PLC Lock Management Deadlocks detec0on Preven0on Specialized Locking Prakash 2015 VT CS 4604 4

Serializability in Prac0ce DBMS does not test for conflict serializability of a given schedule Imprac0cal as interleaving of opera0ons from concurrent Xacts could be dictated by the OS Approach: Use specific protocols that are known to produce conflict serializable schedules But may reduce concurrency Prakash 2015 VT CS 4604 5

Solu0on? One solu0on for conflict serializable schedules is Two Phase Locking (2PL) Prakash 2015 VT CS 4604 6

Answer (Full answer:) use locks; keep them un0l commit ( strict 2 phase locking ) Let s see the details Prakash 2015 VT CS 4604 7

Lost update problem - no locks T1 Read(N) N = N - 1 Write(N) T2 Read(N) N = N - 1 Write(N) Prakash 2015 VT CS 4604 8

Solu0on part 1 with locks: lock manager: grants/denies lock requests Prakash 2015 VT CS 4604 9

Lost update problem with locks T1 T2 lock manager 0me lock(n) Read(N) N=N- 1 Write(N) Unlock(N) grants lock lock(n) denies lock T2: waits grants lock to T2 Read(N)... Prakash 2015 VT CS 4604 10

Locks Q: I just need to read N - should I s0ll get a lock? Prakash 2015 VT CS 4604 11

Solu0on part 1 Locks and their flavors exclusive (or write- ) locks shared (or read- ) locks <and more... > T2 wants T1 has S X compa0bility matrix S X Prakash 2015 VT CS 4604 12

Solu0on part 1 Locks and their flavors exclusive (or write- ) locks shared (or read- ) locks <and more... > T2 wants T1 has S X compa0bility matrix S Yes X Prakash 2015 VT CS 4604 13

Solu0on part 1 transac0ons request locks (or upgrades) lock manager grants or blocks requests transac0ons release locks lock manager updates lock- table Prakash 2015 VT CS 4604 14

Solu0on part 2 locks are not enough eg., the inconsistent analysis problem Prakash 2015 VT CS 4604 15

Inconsistent analysis 0me T1 Read(A) A=A-10 Write(A) T2 Read(A) Sum = A Read(B) Sum += B Read(B) B=B+10 Write(B) Prakash 2015 VT CS 4604 16

Inconsistent analysis w/ locks 0me T1 L(A) Read(A)... U(A) T2 L(A)... L(B)... the problem remains! T2 reads an inconsistent DB state Solu0on?? Prakash 2015 VT CS 4604 17

General solu0on: Protocol(s) Most popular protocol: 2 Phase Locking (2PL) Prakash 2015 VT CS 4604 18

2PL X- lock version: transac0ons issue no lock requests, aner the first unlock THEOREM: if ALL transac0ons in the system obey 2PL - - > all schedules are serializable Prakash 2015 VT CS 4604 19

2PL example inconsistent analysis how does 2PL help? how would it be under 2PL? Prakash 2015 VT CS 4604 20

2PL X/S lock version transac0ons issue no lock/upgrade request, aner the first unlock/downgrade In general: growing and shrinking phase # locks growing phase shrinking phase 0me Prakash 2015 VT CS 4604 21

2PL X/S lock version transac0ons issue no lock/upgrade request, aner the first unlock/downgrade In general: growing and shrinking phase # locks viola0on of 2PL 0me Prakash 2015 VT CS 4604 22

Two- Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee conflict serializability (i.e., schedules whose precedence graph is acyclic), but, it is subject to Cascading Aborts. # locks held acquisition phase release phase time Prakash 2015 VT CS 4604 23

2PL Problem: Cascading Aborts Example: rollback of T1 requires rollback of T2! T1: R(A), W(A), R(B), W(B), Abort T2: R(A), W(A) Solu0on: Strict 2PL, i.e, keep all locks, un0l commit Prakash 2015 VT CS 4604 24

Strict 2PL Allows only conflict serializable schedules, but it is actually stronger than needed for that purpose. # locks held acquisition phase release all locks at end of xact time Prakash 2015 VT CS 4604 25

Strict 2PL == 2PLC (2PL 0ll Commit) In effect, shrinking phase is delayed un0l Transac0on commits (commit log record on disk), or Aborts (then locks can be released aner rollback). # locks held acquisition phase release all locks at end of xact time Prakash 2015 VT CS 4604 26

Non-2PL, A= 1000, B=2000, Output =? Lock_X(A) Read(A) A: = A-50 Write(A) Unlock(A) Lock_S(A) Read(A) Unlock(A) Lock_S(B) Read(B) Unlock(B) PRINT(A+B) Lock_X(B) Read(B) B := B +50 Write(B) Prakash 2015 VT CS 4604 27 Unlock(B)

2PL, A= 1000, B=2000, Output =? Lock_X(A) Read(A) A: = A-50 Write(A) Lock_X(B) Unlock(A) Lock_S(A) Read(A) Read(B) B := B +50 Write(B) Unlock(B) Lock_S(B) Unlock(A) Read(B) Unlock(B) Prakash 2015 VT CS 4604 28

Strict 2PL, A= 1000, B=2000, Output =? Lock_X(A) Read(A) A: = A-50 Write(A) Lock_X(B) Read(B) B := B +50 Write(B) Unlock(A) Unlock(B) Lock_S(A) Read(A) Lock_S(B) Read(B) PRINT(A+B) Unlock(A) Prakash 2015 VT CS 4604 29 Unlock(B)

Venn Diagram for Schedules All Schedules Conflict Serializable Avoid Cascading Abort Serial Prakash 2015 VT CS 4604 30

Q: Which schedules does Strict 2PL All Schedules allow? Conflict Serializable Avoid Cascading Abort Serial Prakash 2015 VT CS 4604 31

Q: Which schedules does Strict 2PL All Schedules allow? Conflict Serializable Avoid Cascading Abort Serial Prakash 2015 VT CS 4604 32

Another Venn diagram 2PL schedules serializable schedules 2PLC serial sch s Prakash 2015 VT CS 4604 33

Outline 2PL/2PLC Lock Management Deadlocks detec0on Preven0on Specialized Locking Prakash 2015 VT CS 4604 34

Lock Management Lock and unlock requests handled by the Lock Manager (LM). LM contains an entry for each currently held lock. Q: structure of a lock table entry? Prakash 2015 VT CS 4604 35

Lock Management Lock and unlock requests handled by the Lock Manager (LM). LM contains an entry for each currently held lock. Lock table entry: Ptr. to list of transac0ons currently holding the lock Type of lock held (shared or exclusive) Pointer to queue of lock requests Prakash 2015 VT CS 4604 36

Lock Management, cont. When lock request arrives see if any other xact holds a conflic0ng lock. If not, create an entry and grant the lock Else, put the requestor on the wait queue Lock upgrade: transac0on that holds a shared lock can be upgraded to hold an exclusive lock Prakash 2015 VT CS 4604 37

Lock Management, cont. Two- phase locking is simple enough, right? We re not done. There s an important wrinkle Prakash 2015 VT CS 4604 38

Example: Output =? Lock_X(A) Read(A) A: = A-50 Write(A) Lock_X(B) Lock_S(B) Read(B) Lock_S(A) Prakash 2015 VT CS 4604 39

Example: Output =? Lock_X(A) Read(A) A: = A-50 Write(A) Lock_X(B) Lock_S(B) Read(B) Lock_S(A) lock mgr: grant grant wait wait Prakash 2015 VT CS 4604 40

Outline Lock Management Deadlocks detec0on Preven0on Specialized Locking Prakash 2015 VT CS 4604 41

Deadlocks Deadlock: Cycle of transac0ons wai0ng for locks to be released by each other. Two ways of dealing with deadlocks: Deadlock preven0on Deadlock detec0on Many systems just punt and use Timeouts What are the dangers with this approach? Prakash 2015 VT CS 4604 42

Deadlock Detec0on Create a waits- for graph: Nodes are transac0ons Edge from Ti to Tj if Ti is wai0ng for Tj to release a lock Periodically check for cycles in waits- for graph Prakash 2015 VT CS 4604 43

Deadlock Detec0on (Con0nued) Example: T1: S(A), S(D), S(B) T2: X(B) X(C) T3: S(D), S(C), X(A) T4: X(B) T1 T2 T4 T3 Prakash 2015 VT CS 4604 44

Another example T1 T3 T2 T4 is there a deadlock? if yes, which xacts are involved? Prakash 2015 VT CS 4604 45

Another example T1 T3 T2 T4 now, is there a deadlock? if yes, which xacts are involved? Prakash 2015 VT CS 4604 46

Deadlock detec0on how onen should we run the algo? how many transac0ons are typically involved? Prakash 2015 VT CS 4604 47

Deadlock handling Q: what to do? T1 T2 T3 T4 Prakash 2015 VT CS 4604 48

Deadlock handling T1 T3 T2 T4 Q0: what to do? A: select a victim & rollback Q1: which/how to choose? Prakash 2015 VT CS 4604 49

Deadlock handling T1 T3 T2 T4 Q1: which/how to choose? A1.1: by age A1.2: by progress A1.3: by # items locked already... A1.4: by # xacts to rollback Q2: How far to rollback? Prakash 2015 VT CS 4604 50

Deadlock handling T1 T3 T2 T4 Q2: How far to rollback? A2.1: completely A2.2: minimally Q3: Starvation?? Prakash 2015 VT CS 4604 51

Deadlock handling T1 T3 T2 T4 Q3: Starvation?? A3.1: include #rollbacks in victim selection criterion. Prakash 2015 VT CS 4604 52

Outline Lock Management Deadlocks detec0on Preven0on Specialized Locking Prakash 2015 VT CS 4604 53

Deadlock Preven0on Assign priori0es based on 0mestamps (older - > higher priority) We only allow old- wait- for- young (or only allow young- wait- for- old ) and rollback violators. Specifically: Say Ti wants a lock that Tj holds - two policies: Wait- Die: If Ti has higher priority, Ti waits for Tj; otherwise Ti aborts (ie., old wait for young) Wound- wait: If Ti has higher priority, Tj aborts; otherwise Ti waits (ie., young wait for old) Prakash 2015 VT CS 4604 54

Deadlock Preven0on Wait-Die Wound-Wait Ti wants Tj has Ti wants Tj has Priori0es Prakash 2015 VT CS 4604 55

Deadlock Preven0on Q: Why do these schemes guarantee no deadlocks? A: Q: When a transac0on restarts, what is its (new) priority? A: Prakash 2015 VT CS 4604 56

Deadlock Preven0on Q: Why do these schemes guarantee no deadlocks? A: only one type of direc0on allowed. Q: When a transac0on restarts, what is its (new) priority? A: its original 0mestamp. - - Why? Prakash 2015 VT CS 4604 57

SQL statement usually, conc. control is transparent to the user, but LOCK <table- name> [EXCLUSIVE SHARED] Prakash 2015 VT CS 4604 58

Quiz: is there a serial schedule (= interleaving) that is not serializable? is there a serializable schedule that is not serial? can 2PL produce a non- serializable schedule? (assume no deadlocks) Prakash 2015 VT CS 4604 59

Quiz - cont d is there a serializable schedule that can not be produced by 2PL? a xact obeys 2PL - can it be involved in a non- serializable schedule? all xacts obey 2PL - can they end up in a deadlock? Prakash 2015 VT CS 4604 60

Outline Lock Management Deadlocks detec0on Preven0on Specialized Locking Prakash 2015 VT CS 4604 61

Things we will not study We assumed 0ll now DB objects are fixed and independent- - - not true in many cases! Mul0- level locking Lock db or file or pages or record? What about locking indexes? E.g. B+- trees Crabbing Algorithm What about dynamic databases? phantom problem Solu0on: predicate locking Non- locking based Techniques Timestamp based Concurrency Control All these are in the textbook though SKIP Prakash 2015 VT CS 4604 62

Transac0on Support in SQL- 92 SERIALIZABLE No phantoms, all reads repeatable, no dirty (uncommited) reads. REPEATABLE READS phantoms may happen. READ COMMITTED phantoms and unrepeatable reads may happen READ UNCOMMITTED all of them may happen. Prakash 2015 VT CS 4604 63

Transac0on Support in SQL- 92 SERIALIZABLE : obtains all locks first; plus index locks, plus strict 2PL REPEATABLE READS as above, but no index locks READ COMMITTED as above, but S- locks are released immediately READ UNCOMMITTED as above, but allowing dirty reads (no S- locks) Prakash 2015 VT CS 4604 64

Transac0on Support in SQL- 92 SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY Defaults: SERIALIZABLE READ WRITE isolation level access mode Prakash 2015 VT CS 4604 65

Conclusions 2PL/2PL- C (=Strict 2PL): extremely popular Deadlock may s0ll happen detec0on: wait- for graph preven0on: abort some xacts, defensively philosophically: concurrency control uses: locks and aborts Prakash 2015 VT CS 4604 66