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

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

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

2 nd Semester 2009/2010

Chapter 12 : Concurrency Control

Comp 5311 Database Management Systems. 14. Timestamp-based Protocols

Chapter 13 : Concurrency Control

Lecture 22 Concurrency Control Part 2

CSIT5300: Advanced Database Systems

Graph-based protocols are an alternative to two-phase locking Impose a partial ordering on the set D = {d 1, d 2,..., d h } of all data items.

Chapter 15 : Concurrency Control

Lecture 13 Concurrency Control

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

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

Database System Concepts

UNIT 4 TRANSACTIONS. Objective

Chapter 16 : Concurrency Control

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

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

Multiversion Schemes to achieve Snapshot Isolation Timestamped Based Protocols

Lecture 21 Concurrency Control Part 1

References. Concurrency Control. Administração e Optimização de Bases de Dados 2012/2013. Helena Galhardas e

Multiversion schemes keep old versions of data item to increase concurrency. Multiversion Timestamp Ordering Multiversion Two-Phase Locking Each

Transactions and Concurrency Control. Dr. Philip Cannata

UNIT IV TRANSACTION MANAGEMENT

Chapter 10 : Concurrency Control

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

Transaction Management

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

Chapter 10: Concurrency Control! Chapter 10 : Concurrency Control! Intuition of Lock-based Protocols! Lock-Based Protocols!

Chapter 10 : Concurrency Control!

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

Transaction Management & Concurrency Control. CS 377: Database Systems

Chapter 13: Transactions

Advanced Databases. Lecture 9- Concurrency Control (continued) Masood Niazi Torshiz Islamic Azad University- Mashhad Branch

CSIT5300: Advanced Database Systems

Chapter 15: Transactions

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

mywbut.com Concurrency Control

Chapter 6 Distributed Concurrency Control

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

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

Distributed Transaction Management. Distributed Database System

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

BİL 354 Veritabanı Sistemleri. Transaction (Hareket)

Concurrency Control 9-1

Chapter 9: Concurrency Control

Checkpoints. Logs keep growing. After every failure, we d have to go back and replay the log. This can be time consuming. Checkpoint frequently

Transactions. Prepared By: Neeraj Mangla

Advanced Databases (SE487) Prince Sultan University College of Computer and Information Sciences. Dr. Anis Koubaa. Spring 2014

Chapter 15: Transactions

Database System Concepts

Concurrency Control Algorithms


Transaction Management. Chapter 14

Chapter 14: Transactions

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

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

Database Management System 21 Concurrency Control

Database System Concepts

Transaction Concept. Two main issues to deal with:

Chapter 22. Transaction Management

Lock-based Concurrency Control

Transactions. Lecture 8. Transactions. ACID Properties. Transaction Concept. Example of Fund Transfer. Example of Fund Transfer (Cont.

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

Distributed Databases Systems

Transactions These slides are a modified version of the slides of the book Database System Concepts (Chapter 15), 5th Ed

Concurrency control 12/1/17

TRANSACTION PROCESSING CONCEPTS

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

Intro to DB CHAPTER 15 TRANSACTION MNGMNT

ICOM 5016 Database Systems. Chapter 15: Transactions. Transaction Concept. Chapter 15: Transactions. Transactions

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

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

Transaction Processing: Basics - Transactions

Concurrency Control. Conflict Serializable Schedules. Example. Chapter 17

Implementing Isolation

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)

UNIT 5. Failure Classification. Recovery Techniques. UNIT V Crash Recovery

Transactions and Concurrency Control

More Concurrency. Database Systems Lecture 17 Natasha Alechina

Transaction Management and Concurrency Control. Chapter 16, 17

Transaction Concept. Chapter 15: Transactions. Example of Fund Transfer. ACID Properties. Example of Fund Transfer (Cont.)

CS 370 Concurrency worksheet. T1:R(X); T2:W(Y); T3:R(X); T2:R(X); T2:R(Z); T2:Commit; T3:W(X); T3:Commit; T1:W(Y); Commit

T ransaction Management 4/23/2018 1

Concurrency Control. Data Base Management Systems. Inherently Concurrent Systems: The requirements

UNIT-IV TRANSACTION PROCESSING CONCEPTS

Database Management Systems Written Exam

Transactions. Silberschatz, Korth and Sudarshan

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

Concurrency Control in Distributed Systems. ECE 677 University of Arizona

Overview of Transaction Management

Concurrency control CS 417. Distributed Systems CS 417

Transaction Management: Concurrency Control

Introduction to Data Management CSE 414

CS322: Database Systems Transactions

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

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

Operating Systems. Operating Systems Sina Meraji U of T

Database Management Systems 2010/11

Transcription:

Concurrency Control Concurrency Control Ensures interleaving of operations amongst concurrent transactions result in serializable schedules How? transaction operations interleaved following a protocol 44

How to enforce serializable schedules? Prevent P(S) cycles from occurring using a concurrency control manager: ensures interleaving of operations amongst concurrent transactions only result in serializable schedules. T1 T2.. Tn CC Scheduler DB 45

Concurrency Via Locks Idea: Data items modified by one transaction at a time Locks Control access to a resource Can block a transaction until lock granted Two modes: Shared (read only) exclusive (read & write) 46

Granting Locks Requesting locks Must request before accessing a data item Granting Locks No lock on data item? Grant Existing lock on data item? Check compatibility: Compatible? Grant Not? Block transaction shared exclusive shared Yes No exclusive No No 47

Lock instructions New instructions - lock-s: shared lock request - lock-x: exclusive lock request - unlock: release previously held lock Example: T1 lock-x(b) read(b) B ßB-50 write(b) unlock(b) lock-x(a) read(a) A ßA + 50 write(a) unlock(a) T2 lock-s(a) read(a) unlock(a) lock-s(b) read(b) unlock(b) display(a+b) 48

Locking Issues Starvation T1 holds shared lock on Q T2 requests exclusive lock on Q: blocks T3, T4,..., Tn request shared locks: granted T2 is starved! Solution? Do not grant locks if older transaction is waiting 49

Locking Issues No transaction proceeds: Deadlock - T1 waits for T2 to unlock A - T2 waits for T1 to unlock B Rollback transactions Can be costly... T1 lock-x(b) read(b) B ß B-50 write(b) lock-x(a) T2 lock-s(a) read(a) lock-s(b) 50

Locking Issues Locks do not ensure serializability by themselves: T1 lock-x(b) read(b) B ßB-50 write(b) unlock(b) lock-x(a) read(a) A ßA + 50 write(a) unlock(a) T2 lock-s(a) read(a) unlock(a) lock-s(b) read(b) unlock(b) display(a+b) T2 displays 50 less!! 51

The Two-Phase Locking Protocol This is a protocol which ensures conflict-serializable schedules. Phase 1: Growing Phase transaction may obtain locks transaction may not release locks Phase 2: Shrinking Phase transaction may release locks transaction may not obtain locks The protocol assures serializability. It can be proved that the transactions can be serialized in the order of their lock points (i.e. the point where a transaction acquired its final lock). Locks can be either X, or S/X. 52

Example: T1 in 2PL Growing phase { 2PL T1 lock-x(b) read(b) B ß B - 50 write(b) lock-x(a) read(a) A ß A - 50 write(a) Shrinking phase { unlock(b) unlock(a) 53

Recall: Precedence Graph 2PL & Serializability T1 T2 T3 read(q) write(q) read(r) write(r) read(s) T1 R/W(Q) T2 T3 R/W(R) 54

Recall: Precedence Graph 2PL & Serializability T1 T2 T3 read(q) write(s) write(q) read(r) write(r) read(s) T1 R/W(S) R/W(Q) T2 Cycle à Non-serializable T3 R/W(R) 55

2PL & Serializability Relation between Growing & Shrinking phase: T 1 G < T 1 S T1 T 2 G < T 2 S T2 T 3 G < T 3 S T3 T1 must release locks for other to proceed T 1 S < T 2 G T 2 S < T 3 G T 3 S < T 1 G T 1 G < T 1 S < T 2 G < T 2 S < T 3 G < T 3 S < T 1 G Not Possible under 2PL! It can be generalized for any set of transactions... 56

2PL Issues As observed earlier, 2PL does not prevent deadlock > 2 transactions involved? - Rollbacks expensive. We will revisit later. T1 lock-x(b) read(b) B ß B-50 write(b) lock-x(a) T2 lock-s(a) read(a) lock-s(b) 57

2PL Variants Strict two phase locking Exclusive locks must be held until transaction commits Ensures data written by transaction can t be read by others Prevents cascading rollbacks 58

Strict 2PL T1 T2 T3 Strict 2PL will not allow that lock-x(a) read(a) lock-s(b) read(b) write(a) unlock(a) lock-x(a) read(a) write(a) unlock(a) lock-s(a) read(a) <xaction fails> 59

Strict 2PL & Cascading Rollbacks Ensures any data written by uncommited transaction not read by another Strict 2PL would prevent T2 and T3 from reading A T2 & T3 wouldn t rollback if T1 does 60

Deadlock Handling Consider the following two transactions: T 1 : write (X) T 2 : write(y) write(y) Schedule with deadlock write(x) 61

Deadlock Handling System is deadlocked if there is a set of transactions such that every transaction in the set is waiting for another transaction in the set. Deadlock prevention protocols ensure that the system will never enter into a deadlock state. Some prevention strategies : Require that each transaction locks all its data items before it begins execution (predeclaration). Impose partial ordering of all data items and require that a transaction can lock data items only in the order specified by the partial order (graph-based protocol). 62

More Deadlock Prevention Strategies Following schemes use transaction timestamps for the sake of deadlock prevention alone. wait-die scheme non-preemptive older transaction may wait for younger one to release data item. Younger transactions never wait for older ones; they are rolled back instead. a transaction may die several times before acquiring needed data item wound-wait scheme preemptive older transaction wounds (forces rollback) of younger transaction instead of waiting for it. Younger transactions may wait for older ones. 63

Deadlock Prevention Wait / Die Wound / Wait O Needs a resource held by Y O Waits Y Dies Y needs a resource held by O Y Dies Y Waits Req. by Old X Req by Young X Req by Old X Req by Young X Locked by Young Locked by Old Locked by Young Locked by Old WAIT / DIE WOUND / WAIT 64

Dealing with Deadlocks How do you detect a deadlock? Wait-for graph Directed edge from Ti to Tj If Ti waiting for Tj T1 T2 T4 T1 T2 T3 T4 T3 S(V) X(V) S(W) X(Z) S(V) X(W) Suppose T4 requests lock-s(z)... 65

Detecting Deadlocks Wait-for graph has a cycle à deadlock T2, T3, T4 are deadlocked T1 T2 T4 Build wait-for graph, check for cycle T3 How often? - Tunable IF expect many deadlocks or many transactions involved run often to reduce aborts ELSE run less often to reduce overhead 66

Recovering from Deadlocks Rollback one or more transaction Which one? Rollback the cheapest ones Cheapest ill-defined Was it almost done? How much will it have to redo? Will it cause other rollbacks? How far? May only need a partial rollback Avoid starvation Ensure same xction not always chosen to break deadlock 67

Timestamp-Based Protocols Idea: Decide in advance ordering of transactions. Ensure concurrent schedule serializes to that serial order. Timestamps 1. TS(T i ) is time T i entered the system 2. Data item timestamps: 1. W-TS(Q): Largest timestamp of any xction that wrote Q 2. R-TS(Q): Largest timestamp of any xction that read Q Timestamps -> serializability order 68

Timestamp CC Idea: If action p i of Xact T i conflicts with action q j of Xact T j, and TS(T i ) < TS(T j ), then p i must occur before q j. Otherwise, restart violating Xact. 69

When Xact T wants to read Object O If TS(T) < W-TS(O), this violates timestamp order of T w.r.t. writer of O. So, abort T and restart it with a new, larger TS. (If restarted with same TS, T will fail again!) If TS(T) > W-TS(O): Allow T to read O. Reset R-TS(O) to max(r-ts(o), TS(T)) Change to R-TS(O) on reads must be written to disk! This and restarts represent overhead. U writes O T reads O T start U start 70

When Xact T wants to Write Object O If TS(T) < R-TS(O), then the value of O that T is producing was needed previously, and the system assumed that that value would never be produced. write rejected, T is rolled back. If TS(T) < W-TS(O), then T is attempting to write an obsolete value of O. Hence, this write operation is rejected, and T is rolled back. Otherwise, the write operation is executed, and W-TS(O) is set to TS(T). U reads O T writes O T start U start 71

Timestamp-Ordering Protocol Rollbacks still present On rollback, new timestamp & restart T1 rollback since TS(T1) < W-TS(O)=TS(T2) T1 Read(O) Write(O) T2 Write(O) Can reduce one rollback situation When transaction writes an obsolete value, ignore it: Thomas write-rule does not rollback T1 72

Example Use of the Protocol A partial schedule for several data items for transactions with initial timestamps 1, 2, 3, 4, 5 T 1 T 2 T 3 T 4 T 5 write(x) read(y) read(y) write(y) write(z) read(z) read(x) abort read(q) write(z) abort write(y) write(z) T 6 read(y) read(x) T 7 73

Correctness of Timestamp-Ordering Protocol The timestamp-ordering protocol guarantees serializability since all the arcs in the precedence graph are of the form: transaction with smaller timestamp transaction with larger timestamp Thus, there will be no cycles in the precedence graph Timestamp protocol ensures freedom from deadlock as no transaction ever waits. 74