Concurrency Control 9-1

Similar documents
Chapter 9: Concurrency Control

Distributed Databases Systems

Distributed Transaction Management

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

Control. CS432: Distributed Systems Spring 2017

Chapter 13 : Concurrency Control

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

Concurrency Control in Distributed Systems. ECE 677 University of Arizona

Multi-User-Synchronization

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

Chapter 15 : Concurrency Control

Lecture 22 Concurrency Control Part 2

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

Transaction Management. Pearson Education Limited 1995, 2005

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

Chapter 12 : Concurrency Control

Implementing Isolation

DB2 Lecture 10 Concurrency Control

Transaction Processing Concurrency control

Lecture 13 Concurrency Control

CS 347 Parallel and Distributed Data Processing

Transactional Information Systems:

CSIT5300: Advanced Database Systems

transaction - (another def) - the execution of a program that accesses or changes the contents of the database

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.

Optimistic Concurrency Control. April 18, 2018

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

2 nd Semester 2009/2010

CS 347 Parallel and Distributed Data Processing

Optimistic Concurrency Control. April 13, 2017

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

Lecture 21 Concurrency Control Part 1

Pessimistic v.s. Optimistic. Outline. Timestamps. Timestamps. Timestamps. CSE 444: Database Internals. Main invariant:


Last Lecture. More Concurrency. Concurrency So Far. In This Lecture. Serialisability. Schedules. Database Systems Lecture 15

Transactions and Concurrency Control

Information Systems (Informationssysteme)

Chapter 6 Distributed Concurrency Control

Lecture 7: Transactions Concurrency Control

Database Management Systems

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)

Concurrency. Consider two ATMs running in parallel. We need a concurrency manager. r1[x] x:=x-250 r2[x] x:=x-250 w[x] commit w[x] commit

Chapter 5. Concurrency Control Techniques. Adapted from the slides of Fundamentals of Database Systems (Elmasri et al., 2006)

Introduction to Database Systems CSE 444

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"

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

CSC 261/461 Database Systems Lecture 24

Intro to Transactions

12.3 Nonlocking schedulers. Timestamp ordering. TO concurrency control guarantees conflict-serializable schedules

Concurrency Control Algorithms

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

CS 541 Database Systems. Two Phase Locking 2

230 Chapter 17. (c) Otherwise, T writes O and WTS(O) is set to TS(T).

Unit 10.5 Transaction Processing: Concurrency Zvi M. Kedem 1

Intro to Transaction Management

International Journal of Advanced Research in Computer Science and Software Engineering

CS Reading Packet: "Transaction management, part 2"

T ransaction Management 4/23/2018 1

Chapter 16 : Concurrency Control

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

14 Concurrency control

CSE 344 MARCH 25 TH ISOLATION

Chapter 22. Transaction Management

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

Concurrency control CS 417. Distributed Systems CS 417

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

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

TRANSACTION MANAGEMENT

Concurrency Control & Recovery

UNIT 4 TRANSACTIONS. Objective

CSE 444: Database Internals. Lectures 13 Transaction Schedules

Lecture 5: Transactions Concurrency Control. Wednesday October 26 th, 2011

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

Lock-based concurrency control. Q: What if access patterns rarely, if ever, conflict? Serializability. Concurrency Control II (OCC, MVCC)

Chapter 10 : Concurrency Control

Transaction Management and Concurrency Control. Chapter 16, 17

Distributed Systems. 13. Distributed Deadlock. Paul Krzyzanowski. Rutgers University. Fall 2017

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

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

(Pessimistic) Timestamp Ordering. Rules for read and write Operations. Read Operations and Timestamps. Write Operations and Timestamps

Transaction Processing: Basics - Transactions

Database Systems. Announcement

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

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

Concurrency Control. Conflict Serializable Schedules. Example. Chapter 17

6 Multiversion Concurrency Control

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

(Pessimistic) Timestamp Ordering

Lecture 21. Lecture 21: Concurrency & Locking

Overview. Introduction to Transaction Management ACID. Transactions

Lock-based Concurrency Control

CS54200: Distributed Database Systems

Transaction Management

Chapter 18 Concurrency Control Techniques

More Concurrency. Database Systems Lecture 17 Natasha Alechina

3. Concurrency Control for Transactions Part One

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

Database Systems CSE 414

UNIT-IV TRANSACTION PROCESSING CONCEPTS

Transcription:

Concurrency Control The problem of synchronizing concurrent transactions such that the consistency of the database is maintained while, at the same time, maximum degree of concurrency is achieved. Principles: We want to interleave the execution of transactions for performance reasons E.g., execute operations of another transaction when the first one starts doing I/O. However, we want the results of interleaved executions to be equivalent to non-interleaved execution for correctness We need to be able to reason about the execution order of transactions. 9-1

Potential Anomalies Due to Concurrent Execution Lost updates The effects of some transactions are not reflected in the database. Transaction T 2 reading uncommitted changes to data made by transaction T 1. Write-Read conflicts Transaction T 2 overwriting uncommitted changes of transaction T 1. Write-Write conflicts Inconsistent retrievals (unrepeatable reads) A transaction, if it reads the same data item more than once, should always read the same value. Transaction T 2 modifies data that is being accessed by transaction T 1. Read-Write conflicts 9-2

Execution Schedule (or History) An order in which the operations of a set of transactions are executed. A schedule (history) can be defined as a partial order over the operations of a set of transactions. T 1 : Read T 2 : Write T 3 : Read Write Write(y) Read(y) Commit Read(z) Read(z) Commit Commit H 1 =W 2 R 1 W 1 C 1 W 2 (y) (y) R 2 (z) C 2 (z) C 3 } 9-3

Formalization of Schedule A complete schedule SC(T) over a set of transactions T={T 1,, T n } is a partial order SC(T)={Σ T, < T } where ❶ Σ T = i Σ i, for i = 1, 2,, n ❷ < T i < i, for i = 1, 2,, n ❸ For any two conflicting operations o ij, o kl Σ T, either o ij < T o kl or o kl < T o ij (Remember: o ij is an operation of transaction T i ) 9-4

Complete Schedule Example Given three transactions T 1 : Read T 2 : Write T 3 : Read Write Write(y) Read(y) Commit Read(z) Read(z) Commit Commit A possible complete schedule is given as the DAG R 1 W 2 W 1 W 2 (y) (y) C 1 R 2 (z) (z) C 2 C 3 9-5

Schedule Definition A schedule is a prefix of a complete schedule such that only some of the operations and only some of the ordering relationships are included. T 1 : Read T 2 : Write T 3 : Read Write Write(y) Read(y) Commit Read(z) Read(z) Commit Commit R 1 W 2 R 1 W 2 W 1 W 2 (y) (y) W 2 (y) (y) C 1 R 2 (z) (z) R 2 (z) (z) C 2 C 3 9-6

Serial Schedule All the actions of a transaction occur consecutively. No interleaving of transaction operations. If each transaction is consistent (obeys integrity rules), then the database is guaranteed to be consistent at the end of executing a serial schedule. T 1 : Read T 2 : Write T 3 : Read Write Write(y) Read(y) Commit Read(z) Read(z) Commit Commit H s = W 2 W 2 (y) R 2 (z) C 2 R 1 W 1 C 1 (y) (z) C 3 } T 2 T 1 T 3 9-7

Serializable Schedule Transactions execute concurrently, but the net effect of the resulting schedule upon the database is equivalent to some serial schedule. Equivalent with respect to what? Conflict equivalence: the relative order of execution of the conflicting operations belonging to committed transactions in two schedules are the same. Conflicting operations: two incompatible operations (e.g., Read and Write) conflict if they both access the same data item. Incompatible operations of each transaction is assumed to conflict; do not change their execution orders. If two operations from two different transactions conflict, the corresponding transactions are also said to conflict. 9-8

Serializable Schedule T 1 : Read T 2 : Write T3: Read Write Write(y) Read(y) Commit Read(z) Read(z) Commit Commit The following are not conflict equivalent H s =W 2 W 2 (y) R 2 (z) C 2 R 1 W 1 C 1 (y) (z) C 3 H 1 =W 2 R 1 W 1 C 1 W 2 (y) (y) R 2 (z) C 2 (z) C 3 The following are conflict equivalent; therefore H 2 is serializable. H s =W 2 W 2 (y) R 2 (z) C 2 R 1 W 1 C 1 (y) (z) C 3 H 2 =W 2 R 1 W 1 C 1 W 2 (y) (y) R 2 (z) C 2 (z) C 3 9-9

Serializability Graph Serializability graph SG H ={V,E} for schedule H: V={T T is a committed transaction in H} E={T i T j if o ij T i and o kl T k conflict and o ij < H o kl } T 2 T 1 T 2 T 1 T 3 T 3 H 1 Theorem: Schedule H is serializable iff SG H does not contain any cycles. H 2 9-10

Concurrency Control Algorithms Pessimistic Two-Phase Locking-based (2PL) Timestamp Ordering (TO) Optimistic 9-11

Locking-Based Algorithms Transactions indicate their intentions by requesting locks from the scheduler (called lock manager). Locks are either read lock (rl) [also called shared lock] or write lock (wl) [also called exclusive lock] Read locks and write locks conflict (because Read and Write operations are incompatible rl wl rl yes no wl no no Locking works nicely to allow concurrent processing of transactions. 9-12

Two-Phase Locking (2PL) ❶ A Transaction locks an object before using it. ❷ When an object is locked by another transaction, the requesting transaction must wait. ❸ When a transaction releases a lock, it may not request another lock. Lock point Obtain lock No. of locks Release lock Phase 1 Phase 2 BEGIN END 9-13

Strict 2PL Hold locks until the end. Obtain lock Release lock BEGIN period of data item use END Transaction duration 9-14

Timestamp Ordering ❶ Transaction (T i ) is assigned a globally unique timestamp ts(t i ). ❷ Transaction manager attaches the timestamp to all operations issued by the transaction. ❸ Each data item is assigned a write timestamp (wts) and a read timestamp (rts): rts = largest timestamp of any read on x wts = largest timestamp of any write on x ❹ Conflicting operations are resolved by timestamp order. Basic T/O: for R i : if ts(t i ) < wts then reject R i else {accept R i for W i : if ts(t i ) < rts or ts(t i ) < wts then reject W i else {accept W i rts ts(t i ) } wts ts(t i ) } 9-15

Multiversion Timestamp Ordering Do not modify the values in the database, create new values. A R i is translated into a read on one version of x. Find a version of x (say x v ) such that ts(x v ) is the largest timestamp less than ts(t i ). A W i is translated into W i (x w ) and accepted if the scheduler has not yet processed any R j (x r ) such that ts(t i ) < ts(x r ) < ts(t j ) W i Some other transaction created x r R j (x r ) 9-16

Optimistic Concurrency Control Pessimistic execution Algorithms Validate Read Compute Write Optimistic execution Read Compute Validate Write 9-17

Optimistic CC Validation Test ❶ If all transactions T k where ts(t k ) < ts(t i ) have completed their write phase before T i has started its read phase, then validation succeeds Transaction executions in serial order T k R V W T i R V W 9-18

Optimistic CC Validation Test ❷ If there is any transaction T k such that ts(t k )<ts(t i ) and which completes its write phase while T i is in its read phase, then validation succeeds if WS(T k ) RS(T i ) = Ø Read and write phases overlap, but T i does not read data items written by T k R V W T k R V W T i 9-19

Optimistic CC Validation Test ❸ If there is any transaction T k such that ts(t k )< ts(t i ) and which completes its read phase before T i completes its read phase, then validation succeeds if WS(T k ) RS(T i ) = Ø and WS(T k ) WS(T i ) = Ø They overlap, but don't access any common data items. R V W T k R V W T i 9-20

A transaction is deadlocked if it is blocked and will remain blocked until there is intervention. Locking-based CC algorithms may cause deadlocks. Wait-for graph Deadlock If transaction T i waits for another transaction T j to release a lock on an entity, then T i T j in WFG. T i T j 9-21

Deadlock Management Prevention Guaranteeing that deadlocks can never occur in the first place. Check transaction when it is initiated. Requires no run time support. Avoidance Detecting potential deadlocks in advance and taking action to insure that deadlock will not occur. Requires run time support. Detection and Recovery Allowing deadlocks to form and then finding and breaking them. As in the avoidance scheme, this requires run time support. 9-22

Deadlock Prevention All resources that may be needed by a transaction must be predeclared. The system must guarantee that none of the resources will be needed by an ongoing transaction. Resources must only be reserved, but not necessarily allocated a priori Unsuitable in database environment Suitable for systems that have no provisions for undoing processes. Evaluation: Reduced concurrency due to pre-allocation Evaluating whether an allocation is safe leads to added overhead. Difficult to determine (partial order) + No transaction rollback or restart is caused. 9-23

Deadlock Avoidance Transactions are not required to request resources a priori. Transactions are allowed to proceed unless a requested resource is unavailable. In case of conflict, transactions may be allowed to wait for a fixed time interval. Order the data items and always request locks in that order. More attractive than prevention in a database environment. 9-24

Deadlock Avoidance Wait-Die & Wound-Wait Algorithms WAIT-DIE Rule: If T i requests a lock on a data item which is already locked by T j, then T i is permitted to wait iff ts(t i )<ts(t j ). If ts(t i )>ts(t j ), then T i is aborted and restarted with the same timestamp. if ts(t i )<ts(t j ) then T i waits else T i dies non-preemptive: T i never preempts T j WOUND-WAIT Rule: If T i requests a lock on a data item which is already locked by T j, then T i is permitted to wait iff ts(t i )>ts(t j ). If ts(t i )<ts(t j ), then T j is aborted and the lock is granted to T i. if ts(t i )<ts(t j ) then T j is wounded else T i waits preemptive: T i preempts T j if it is younger 9-25

Deadlock Detection Transactions are allowed to wait freely. Wait-for graphs and cycles. 9-26