Concurrency Control. Module 6, Lectures 1 and 2



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

Course Content. Transactions and Concurrency Control. Objectives of Lecture 4 Transactions and Concurrency Control

Transaction Management Overview

Lecture 7: Concurrency control. Rasmus Pagh

Concurrency Control: Locking, Optimistic, Degrees of Consistency

Database Tuning and Physical Design: Execution of Transactions

Homework 8. Revision : 2015/04/14 08:13

Concurrency control. Concurrency problems. Database Management System

Roadmap DB Sys. Design & Impl. Detailed Roadmap. Paper. Transactions - dfn. Reminders: Locking and Consistency

Transactions and Concurrency Control. Goals. Database Administration. (Manga Guide to DB, Chapter 5, pg , ) Database Administration

Goals. Managing Multi-User Databases. Database Administration. DBA Tasks. (Kroenke, Chapter 9) Database Administration. Concurrency Control

Crash Recovery. Chapter 18. Database Management Systems, 3ed, R. Ramakrishnan and J. Gehrke

Transactional properties of DBS

Transactions. SET08104 Database Systems. Napier University

Transactions: Definition. Transactional properties of DBS. Transactions: Management in DBS. Transactions: Read/Write Model

CMSC724: Concurrency

Transactions and Recovery. Database Systems Lecture 15 Natasha Alechina

Textbook and References

Database Concurrency Control and Recovery. Simple database model

Introduction to Database Management Systems

Review: The ACID properties

Recovery Theory. Storage Types. Failure Types. Theory of Recovery. Volatile storage main memory, which does not survive crashes.

Module 3 (14 hrs) Transactions : Transaction Processing Systems(TPS): Properties (or ACID properties) of Transactions Atomicity Consistency

Comp 5311 Database Management Systems. 16. Review 2 (Physical Level)

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

Transactions and the Internet

Introduction to Database Systems. Module 1, Lecture 1. Instructor: Raghu Ramakrishnan UW-Madison

INTRODUCTION TO DATABASE SYSTEMS

Chapter 10. Backup and Recovery

CS 245 Final Exam Winter 2013

Recovery and the ACID properties CMPUT 391: Implementing Durability Recovery Manager Atomicity Durability

Transaction Processing Monitors

Distributed Data Management

CHAPTER 6: DISTRIBUTED FILE SYSTEMS

Distributed Databases

Chapter 14: Recovery System

Information Systems. Computer Science Department ETH Zurich Spring 2012

The ConTract Model. Helmut Wächter, Andreas Reuter. November 9, 1999

Lecture 18: Reliable Storage

Introduction to Database Systems CS4320. Instructor: Christoph Koch CS

Redo Recovery after System Crashes

Distributed Transactions

Data Management in the Cloud

! Volatile storage: ! Nonvolatile storage:

Database Management System Prof. D. Janakiram Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Synchronization and recovery in a client-server storage system

Failure Recovery Himanshu Gupta CSE 532-Recovery-1

Chapter 15: Recovery System

Chapter 16: Recovery System

Cloud DBMS: An Overview. Shan-Hung Wu, NetDB CS, NTHU Spring, 2015

CPS221 Lecture - ACID Transactions

In This Lecture. More Concurrency. Deadlocks. Precedence/Wait-For Graphs. Example. Example

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

Recovery algorithms are techniques to ensure transaction atomicity and durability despite failures. Two main approaches in recovery process

How To Recover From Failure In A Relational Database System

Serializable Isolation for Snapshot Databases

Topics. Distributed Databases. Desirable Properties. Introduction. Distributed DBMS Architectures. Types of Distributed Databases

Unit 12 Database Recovery

Topics. Introduction to Database Management System. What Is a DBMS? DBMS Types

COS 318: Operating Systems

Logistics. Database Management Systems. Chapter 1. Project. Goals for This Course. Any Questions So Far? What This Course Cannot Do.

How To Write A Transaction System

Implementing New Approach for Enhancing Performance and Throughput in a Distributed Database

Microkernels & Database OSs. Recovery Management in QuickSilver. DB folks: Stonebraker81. Very different philosophies

Chapter 6 The database Language SQL as a tutorial

Transaction Management in Distributed Database Systems: the Case of Oracle s Two-Phase Commit

Agenda. Transaction Manager Concepts ACID. DO-UNDO-REDO Protocol DB101

File System Reliability (part 2)

Relational Databases

The Classical Architecture. Storage 1 / 36

Distributed Architectures. Distributed Databases. Distributed Databases. Distributed Databases

The Oracle Universal Server Buffer Manager

CAP Theorem and Distributed Database Consistency. Syed Akbar Mehdi Lara Schmidt

Outline. Failure Types

Applying Attribute Level Locking to Decrease the Deadlock on Distributed Database

File Management. Chapter 12

Entangled Transactions. Nitin Gupta, Milos Nikolic, Sudip Roy, Gabriel Bender, Lucja Kot, Johannes Gehrke, Christoph Koch

Low Overhead Concurrency Control for Partitioned Main Memory Databases

Chapter Outline. Chapter 2 Distributed Information Systems Architecture. Middleware for Heterogeneous and Distributed Information Systems

Generalized Isolation Level Definitions

Database Internals (Overview)

TRANSACÇÕES. PARTE I (Extraído de SQL Server Books Online )

b. Examine the following histories. Draw their serialization graph and identify which of them is serializable given reasons.

Optimizing Concurrent Processing of Write-then-Read Transactions

Recovery: An Intro to ARIES Based on SKS 17. Instructor: Randal Burns Lecture for April 1, 2002 Computer Science Johns Hopkins University

Part I: Entity Relationship Diagrams and SQL (40/100 Pt.)

UVA. Failure and Recovery. Failure and inconsistency. - transaction failures - system failures - media failures. Principle of recovery

A Shared-nothing cluster system: Postgres-XC

Java DB Performance. Olav Sandstå Sun Microsystems, Trondheim, Norway Submission ID: 860

ZooKeeper. Table of contents

The first time through running an Ad Hoc query or Stored Procedure, SQL Server will go through each of the following steps.

DATABASE MANAGEMENT SYSTEMS. Question Bank:

Running a Workflow on a PowerCenter Grid

Transcription:

Concurrency Control Module 6, Lectures 1 and 2 The controlling intelligence understands its own nature, and what it does, and whereon it works. -- Marcus Aurelius Antoninus, 121-180 A. D. Database Management Systems 1

Why Have Concurrent Processes? Better transaction throughput, response time Done via better utilization of resources: While one processes is doing a disk read, another can be using the CPU or reading another disk. DANGER DANGER! Concurrency could lead to incorrectness! Must carefully manage concurrent data access. There s (much!) more here than the usual OS tricks! Database Management Systems 2

Transactions Basic concurrency/recovery concept: a transaction (Xact). A sequence of many actions which are considered to be one atomic unit of work. DBMS actions : reads, writes Special actions: commit, abort for now, assume reads and writes are on tuples; we ll revisit this assumption later. Database Management Systems 3

The ACID Properties A tomicity: All actions in the Xact happen, or none happen. C onsistency: If each Xact is consistent, and the DB starts consistent, it ends up consistent. I solation: Execution of one Xact is isolated from that of other Xacts. Durability: If a Xact commits, its effects persist. Database Management Systems 4

Passing the ACID Test Concurrency Control Guarantees Consistency and Isolation, given Atomicity. Logging and Recovery Guarantees Atomicity and Durability. We ll do C. C. today: What problems could arise? What is acceptable behavior? How do we guarantee acceptable behavior? Database Management Systems 5

T1 T2 Schedules Schedule: An interleaving of actions from a set of Xacts, where the actions of any 1 Xact are in the original order. Represents some actual sequence of database actions. Example: R 1 (A), W 1 (A), R 2 (B), W 2 (B), R 1 (C), W 1 (C) In a complete schedule, each Xact ends in commit or abort. Initial State + Schedule Final State R(A) W(A) R(C) W(C) R(B) W(B) Database Management Systems 6

Acceptable Schedules One sensible isolated, consistent schedule: Run Xacts one at a time, in a series. This is called a serial schedule. NOTE: Different serial schedules can have different final states; all are OK -- DBMS makes no guarantees about the order in which concurrently submitted Xacts are executed. Serializable schedules: Final state is what some serial schedule would have produced. Aborted Xacts are not part of schedule; ignore them for now (they are made to `disappear by using logging). Database Management Systems 7

Serializability Violations Two actions conflict when 2 xacts access the same item: W-R conflict: T2 reads something T1 wrote. R-W and W-W conflicts: Similar. WR conflict (dirty read): Result is not equal to any serial execution! Database is inconsistent! transfer $100 from A to B Database Management Systems 8 T1 R(A) W(A) R(B) W(B) Commit add 6% interest to A & B T2 R(A) W(A) R(B) W(B) Commit

More Conflicts RW Conflicts (Unrepeatable Read) T2 overwrites what T1 read. If T1 reads it again, it will see something new! Example when this would happen? The increment/decrement example. Again, not equivalent to a serial execution. WW Conflicts (Overwriting Uncommited Data) T2 overwrites what T1 wrote. Example: 2 Xacts to update items to be kept equal. Usually occurs in conjunction w/other anomalies. Unless you have blind writes. Database Management Systems 9

Now, Aborted Transactions Serializable schedule: Equivalent to a serial schedule of committed Xacts. as if aborted Xacts never happened. Two Issues: How does one undo the effects of an xact? We ll cover this in logging/recovery What if another Xact sees these effects?? Must undo that Xact as well! Database Management Systems 10

T1 T2 Cascading Aborts R(A) W(A) Abort of T1 requires abort of T2! Cascading Abort What about WW conflicts & aborts? T2 overwrites a value that T1 writes. T1 aborts: its remembered values are restored. Lose T2 s write! We will see how to solve this, too. An ACA (avoids cascading abort) schedule is one in which cascading abort cannot arise. abort R(A) W(A) A Xact only reads/writes data from committed Xacts. Database Management Systems 11

T1 T2 Recoverable Schedules R(A) W(A) Abort of T1 requires abort of T2! But T2 has already committed! A recoverable schedule is one in which this cannot happen. abort i.e. a Xact commits only after all the Xacts it depends on (i.e. it reads from or overwrites) commit. Recoverable implies ACA (but not vice-versa!). Real systems typically ensure that only recoverable schedules arise (through locking). R(A) W(A) commit Database Management Systems 12

Locking: A Technique for C. C. Concurrency control usually done via locking. Lock info maintained by a lock manager : Stores (XID, RID, Mode) triples. This is a simplistic view; suffices for now. Mode {S,X} Lock compatibility table: If a Xact can t get a lock, it is suspended on a wait queue. -- S X -- S X Database Management Systems 13

Two-Phase Locking (2PL) 2PL: If T wants to read an object, first obtains an S lock. If T wants to modify an object, first obtains X lock. If T releases any lock, it can acquire no new locks! Locks are automatically obtained by DBMS. Guarantees serializability! Why? growing phase # of locks Time lock point shrinking phase Database Management Systems 14

Strict 2PL Strict 2PL: If T wants to read an object, first obtains an S lock. If T wants to modify an object, first obtains X lock. Hold all locks until end of transaction. Guarantees serializability, and recoverable schedule, too! also avoids WW problems! # of locks Time Database Management Systems 15

Precedence Graph A Precedence (or Serializability) graph: Node for each commited Xact. Arc from Ti to Tj if an action of Ti precedes and conflicts with an action of Tj. T1 transfers $100 from A to B, T2 adds 6% R 1 (A), W 1 (A), R 2 (A), W 2 (A), R 2 (B), W 2 (B), R 1 (B), W 1 (B) T1 T2 Database Management Systems 16

Conflict Serializability 2 schedules are conflict equivalent if: they have the same sets of actions, and each pair of conflicting actions is ordered in the same way. A schedule is conflict serializable if it is conflict equivalent to a serial schedule. Note: Some serializable schedules are not conflict serializable! Database Management Systems 17

Conflict Serializability & Graphs Theorem: A schedule is conflict serializable iff its precedence graph is acyclic. Theorem: 2PL ensures that the precedence graph will be acyclic! Strict 2PL improves on this by avoiding cascading aborts, problems with undoing WW conflicts; i.e., ensuring recoverable schedules. Database Management Systems 18

Lock Manager Implementation Question 1: What are we locking? Tuples, pages, or tables? Finer granularity increases concurrency, but also increases locking overhead. Question 2: How do you lock something?? Lock Table: A hash table of Lock Entries. Lock Entry: OID Mode List: Xacts holding lock List: Wait Queue Database Management Systems 19

Handling a Lock Request Mode==X Lock Request (XID, OID, Mode) Mode==S Currently Locked? No Yes Put on Queue Grant Lock Empty Wait Queue? Yes No Currently X-locked? Yes No Database Management Systems 20

More Lock Manager Logic On lock release (OID, XID): Update list of Xacts holding lock. Examine head of wait queue. If Xact there can run, add it to list of Xacts holding lock (change mode as needed). Repeat until head of wait queue cannot be run. Note: Lock request handled atomically! via latches (i.e. semaphores/mutex; OS stuff). Database Management Systems 21

Lock Upgrades Think about this scenario: T1 locks A in S mode, T2 requests X lock on A, T3 requests S lock on A. What should we do? In contrast: T1 locks A in S mode, T2 requests X lock on A, T1 requests X lock on A. What should we do? Allow such upgrades to supersede lock requests. Consider this scenario: S1(A), X2(A), X1(A): DEADLOCK! BTW: Deadlock can occur even w/o upgrades: X1(A), X2(B), S1(B), S2(A) Database Management Systems 22

Deadlock Prevention X1(A), X2(B), S1(B), S2(A) Assign a timestamp to each Xact as it enters the system. Older Xacts have priority. Assume Ti requests a lock, but Tj holds a conflicting lock. Wait-Die: If Ti has higher priority, it waits; else Ti aborts. Wound-Wait: If Ti has higher priority, abort Tj; else Ti waits. Note: After abort, restart with original timestamp! Both guarantee deadlock-free behavior! Pros and cons of each? Database Management Systems 23

An Alternative to Prevention In theory, deadlock can involve many transactions: T1 waits-for T2 waits-for T3...waits-for T1 In practice, most deadlock cycles involve only 2 transactions. Don t need to prevent deadlock! What s the problem with prevention? Allow it to happen, then notice it and fix it. Deadlock detection. Database Management Systems 24

Deadlock Detection Lock Mgr maintains a Waits-for graph: Node for each Xact. Arc from Ti to Tj if Tj holds a lock and Ti is waiting for it. Periodically check graph for cycles. Shoot some Xact to break the cycle. Simpler hack: time-outs. T1 made no progress for a while? Shoot it. To lock such rascal counters from his friends, Be ready, gods, with all your thunderbolts: Dash him to pieces! -- Shakespeare, Julius Caesar Database Management Systems 25

Prevention vs. Detection Prevention might abort too many Xacts. Detection might allow deadlocks to tie up resources for a while. Can detect more often, but it s time-consuming. The usual answer: Detection is the winner. Deadlocks are pretty rare. If you get a lot of deadlocks, reconsider your schema/workload! Database Management Systems 26

Multiple-Granularity Locks Hard to decide what granularity to lock (tuples vs. pages vs. tables). Shouldn t have to decide! Data containers are nested: Database contains Tables Pages Tuples Database Management Systems 27

Solution: New Lock Modes, Protocol Allow Xacts to lock at each level, but with a special protocol using new intention locks: Before locking an item, Xact must set intention locks on all its ancestors. -- -- IS IX S X For unlock, go from specific to general (i.e., bottom-up). IS IX SIX mode: Like S & IX at the same time. S X Database Management Systems 28

Examples T1 scans R, and updates a few tuples: T1 gets an SIX lock on R, then repeatedly gets an S lock on tuples of R, and occasionally upgrades to X on the tuples. T2 uses an index to read only part of R: T2 gets an IS lock on R, and repeatedly gets an S lock on tuples of R. T3 reads all of R: T3 gets an S lock on R. OR, T3 could behave like T2; can use lock escalation to decide which. -- IS IX S X -- IS IX S X Database Management Systems 29

Summary of C.C. Concurrency control key to a DBMS. More than just mutexes! Transactions and the ACID properties: C & I are handled by concurrency control. A & D coming soon with logging & recovery. Conflicts arise when two Xacts access the same object, and one of the Xacts is modifying it. Serial execution is our model of correctness. Database Management Systems 30

Summary, cont. Serializability allows us to simulate serial execution with better performance. 2PL: A simple mechanism to get serializability. Strict 2PL also gives us recoverability. Lock manager module automates 2PL so that only the access methods worry about it. Lock table is a big main-mem hash table Deadlocks are possible, and typically a deadlock detector is used to solve the problem. Database Management Systems 31