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

Similar documents
Concurrency Control. Module 6, Lectures 1 and 2

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

Lecture 7: Concurrency control. Rasmus Pagh

Transaction Management Overview

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

Concurrency control. Concurrency problems. Database Management System

Concurrency Control: Locking, Optimistic, Degrees of Consistency

CMSC724: Concurrency

Database Tuning and Physical Design: Execution of Transactions

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

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

Transactional properties of DBS

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

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

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

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

CS 245 Final Exam Winter 2013

Introduction to Database Management Systems

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

High-Performance Concurrency Control Mechanisms for Main-Memory Databases

CHAPTER 6: DISTRIBUTED FILE SYSTEMS

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

Universiteit Twente. Design of an object-oriented framework for atomic transactions

Database Concurrency Control and Recovery. Simple database model

Distributed Databases

Lecture 1: Data Storage & Index

Data Management in the Cloud

Transactions. SET08104 Database Systems. Napier University

Distributed Data Management

Overview of Storage and Indexing

Applying Attribute Level Locking to Decrease the Deadlock on Distributed Database

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

Physical Data Organization

On Transaction Processing with Partial Validation and Timestamp Ordering in Mobile Broadcast Environments. Abstract

Synchronization and recovery in a client-server storage system

GRANULARITY OF LOCKS IN A SHARED DATA BASE. IBM Research Laboratory San Jose, California 95193

Distributed Architectures. Distributed Databases. Distributed Databases. Distributed Databases

Generalized Isolation Level Definitions

! Volatile storage: ! Nonvolatile storage:

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

Transaction Processing Monitors

Redo Recovery after System Crashes

Textbook and References

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

Review: The ACID properties

Database Replication with Oracle 11g and MS SQL Server 2008

File Management. Chapter 12

DDB Functionalities by Major DMBS Products. Haibin Liu Shcherbak Maryna Nassrat Hatem

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

Udai Shankar 2 Deptt. of Computer Sc. & Engineering Madan Mohan Malaviya Engineering College, Gorakhpur, India

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

Recovery System C H A P T E R16. Practice Exercises

MaaT: Effective and scalable coordination of distributed transactions in the cloud

COS 318: Operating Systems

Simulations of the implementation of primary copy two-phase locking in distributed database systems

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

Overview of Storage and Indexing. Data on External Storage. Alternative File Organizations. Chapter 8

Chapter 12 File Management. Roadmap

Chapter 12 File Management

Introduction to Database Systems CS4320. Instructor: Christoph Koch CS

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

Distributed Transactions

Serializable Isolation for Snapshot Databases

Chapter 16: Recovery System

How To Recover From Failure In A Relational Database System

Real Time Database Systems

Databases and Information Systems 1 Part 3: Storage Structures and Indices

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

CS 525 Advanced Database Organization - Spring 2013 Mon + Wed 3:15-4:30 PM, Room: Wishnick Hall 113

Chapter 14: Recovery System

Distributed Database Management Systems

Recovering from Main-Memory

PostgreSQL Concurrency Issues

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur

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

Bigdata High Availability (HA) Architecture

Principles of Distributed Database Systems

Quiz! Database Indexes. Index. Quiz! Disc and main memory. Quiz! How costly is this operation (naive solution)?

2 nd Semester 2008/2009

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Chapter 10. Backup and Recovery

File Management. Chapter 12

F1: A Distributed SQL Database That Scales. Presentation by: Alex Degtiar (adegtiar@cmu.edu) /21/2013

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

Prerequisites Attended the previous technical session: Understanding geodatabase editing workflows: Introduction

Lecture 18: Reliable Storage

Chapter 13 File and Database Systems

Chapter 13 File and Database Systems

Database Replication Techniques: a Three Parameter Classification

Pangea: An Eager Database Replication Middleware guaranteeing Snapshot Isolation without Modification of Database Servers

Week 1 Part 1: An Introduction to Database Systems. Databases and DBMSs. Why Use a DBMS? Why Study Databases??

DATABASE DESIGN - 1DL400

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

The Oracle Universal Server Buffer Manager

Chapter 6, The Operating System Machine Level

Tivoli Storage Manager Explained

Chapter 15: Recovery System

Contents RELATIONAL DATABASES

CIS 631 Database Management Systems Sample Final Exam

Transcription:

Concurrency Control Chapter 17 Comp 521 Files and Databases Fall 2010 1

Conflict Serializable Schedules Recall conflicts (WR, RW, WW) were the cause of sequential inconsistency Two schedules are conflict equivalent if: Involve the same actions over the same transactions Every pair of conflicting actions is ordered the same way A schedule is conflict serializable if it is conflict equivalent to some serializable schedule Comp 521 Files and Databases Fall 2010 2

Example 1 A non-serializable schedule that is also not conflict serializable: T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B) T1 A B The cycle in the graph reveals the problem. The output of T1 depends on T2, and viceversa. T2 Precedence graph Comp 521 Files and Databases Fall 2010 3

Example 2 A serializable schedule that is not conflict serializable: T1: R(A), W(A), C T2: W(A), C T3: W(A), C T 1 T 2 T 3 Serializable because it is equiv to T1, T2, T3, or T2, T1, T3 Not conflict serializable, because the ordering: R 1 (A),W 2 (A),W 1 (A),W 3 (A) is not consistent with any ordering Importance of this distinction is that it can be proven that Strict 2PL permits only conflict serializable schedules Comp 521 Files and Databases Fall 2010 4

Review: Strict 2PL Strict Two-phase Locking (Strict 2PL) Protocol: Each Xact must obtain a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing. All locks held by a transaction are released when the transaction completes If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object. Strict 2PL allows only schedules whose precedence graph is acyclic (a DAG) Comp 521 Files and Databases Fall 2010 5

Two-Phase Locking (2PL) Two-Phase Locking Protocol Each Xact must obtain a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing. A transaction can release its locks once it has performed its desired operation (R or W). A transaction cannot request additional locks once it releases any locks. If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object. Note: locks can be released before Xact completes (commit/ abort), thus relaxing Strict 2PL. 2PL starts with a growing phase, where locks are requested followed by a shrinking phase, where locks are released Comp 521 Files and Databases Fall 2010 6

View Serializability Schedules S1 and S2 are view equivalent if: If Ti reads initial value of A in S1, then Ti also reads initial value of A in S2 If Ti reads value of A written by Tj in S1, then Ti also reads value of A written by Tj in S2 If Ti writes final value of A in S1, then Ti also writes final value of A in S2 T1: R(A) W(A) T2: W(A) T3: W(A) T1: R(A),W(A) T2: W(A) T3: W(A) Enforcing view serializabiliy is expensive, thus mainly of theoretical interest Comp 521 Files and Databases Fall 2010 7

Lock Management Lock and unlock requests are handled by the lock manager Lock table entry (per table, record, or index): Number of transactions currently holding a lock Type of lock held (shared or exclusive) Pointer to queue of lock requests Locking and unlocking must be atomic Lock upgrades: transaction that holds a shared lock can be upgraded to hold an exclusive lock Comp 521 Files and Databases Fall 2010 8

Deadlocks Deadlock: Cycle of transactions waiting for locks to be released by each other. Relatively rare schedules lead to deadlock Two ways of dealing with deadlocks: Deadlock detection Deadlock prevention Comp 521 Files and Databases Fall 2010 9

Deadlock Detection Create a waits-for graph: Nodes are transactions Edge from Ti to Tj indicates Ti is waiting for Tj to release a lock DBMS periodically checks for cycles in the waits-for graph ex: T1: A = f(b), T2: B = g(c), T3: C = h(a), arriving T1,T3,T2 T1: S(B),R(B), X(A), T2: S(C),R(C),X(B), T3: S(A),R(A), X(C), T1 T2 T3 Comp 521 Files and Databases Fall 2010 10

Deadlock Detection (Continued) Example: T1: S(A), R(A), S(B) T2: X(B),W(B) X(C) T3: S(C), R(C) X(A) T4: X(B) T1 T2 T1 T2 T4 T3 T3 T3 Comp 521 Files and Databases Fall 2010 11

Deadlock Prevention When there is high contention for locks, detection and aborting can hurt performance Assign priorities (eg. based on timestamps). Assume Ti wants a lock that Tj holds. Two policies are possible: Wait-Die: It Ti has higher priority, Ti waits for Tj; otherwise abort Ti Wound-wait: If Ti has higher priority, abort Tj; otherwise Ti waits When Ti re-starts, it retains its original timestamp, thus moving up the priority list Comp 521 Files and Databases Fall 2010 12

Multi-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 Comp 521 Files and Databases Fall 2010 13

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. For unlock, go from specific to general (i.e., bottom-up). SIX mode: Like holding the S & IX locks at the same time. -- IS IX Comp 521 Files and Databases Fall 2010 14 -- IS IX S X Grant request rules S X

Multiple Granularity Lock Protocol Each Xact starts from the root of the hierarchy. To get S or IS lock on a node, must first hold an IS or IX lock on the node s. To get X or IX or SIX on a node, must hold IX or SIX on parent node. Must release locks in bottom-up order. Protocol is correct in that it is equivalent to directly setting locks at the leaf levels of the hierarchy. Comp 521 Files and Databases Fall 2010 15

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 lock escalation to decide which. -- IS IX use Comp 521 Files and Databases Fall 2010 16 -- IS IX S X S X

Dynamic Databases If we relax the assumption that the DB is a fixed collection of objects, even Strict 2PL will not assure serializability: T1 locks all pages containing sailor records with rating = 1, and finds oldest sailor (say, age = 71). Next, T2 inserts a new sailor; rating = 1, age = 96. T2 also deletes oldest sailor with rating = 2 (and, say, age = 80), and commits. T1 now locks all pages containing sailor records with rating = 2, and finds oldest (say, age = 63). No consistent DB state where T1 is correct! Comp 521 Files and Databases Fall 2010 17

The Problem T1 implicitly assumes that it has locked the set of all sailor records with rating = 1. Assumption only holds if no sailor records are added while T1 is executing! Need some mechanism to enforce this assumption. (Index locking and predicate locking.) Example shows that conflict serializability guarantees serializability only if the set of objects is fixed! Comp 521 Files and Databases Fall 2010 18

Index Locking Index r = 1 If there is a dense index on the rating field using Alternative (2), T1 should lock the index page containing the data entries with rating = 1. If there are no records with rating = 1, T1 must lock the index page where such a data entry would be, if it existed! If there is no suitable index, T1 must lock all pages, and lock the file/table to prevent new pages from being added, to ensure that no new records with rating = 1 are added. Data Comp 521 Files and Databases Fall 2010 19

Predicate Locking Grant lock on all records that satisfy some logical predicate, e.g. age > 2*salary. Index locking is a special case of predicate locking for which an index supports efficient implementation of the predicate lock. What is the predicate in the sailor example? In general, predicate locking has a lot of locking overhead. Comp 521 Files and Databases Fall 2010 20

Locking in B+ Trees How can we efficiently lock a particular leaf node? One solution: Ignore the tree structure, just lock pages while traversing the tree, following 2PL. This has terrible performance! Root node (and many higher level nodes) become bottlenecks because every tree access begins at the root. Comp 521 Files and Databases Fall 2010 21

Two Useful Observations Higher levels of the tree only direct searches for leaf pages. For inserts, a node on a path from root to modified leaf must be locked (in X mode), only if a split can propagate up to it from the modified leaf. (Similar point holds w.r.t. deletes.) We can exploit these observations to design efficient locking protocols that guarantee serializability even though they violate 2PL. Comp 521 Files and Databases Fall 2010 22

A Simple Tree Locking Algorithm Search: Start at root and go down; repeatedly, S lock child then unlock parent. Insert/Delete: Start at root and go down, obtaining X locks as needed. Once child is locked, check if it is safe: If child is safe, release all locks on ancestors. Safe node: Node such that changes will not propagate up beyond this node. Inserts: Node is not full. Deletes: Node is not half-empty. Comp 521 Files and Databases Fall 2010 23

ROOT Example 20 A 35 B Do: 1) Search 38* 2) Delete 38* 3) Insert 45* 4) Insert 25* 23 F 38 44 C 20* G H I D 22* 23* 24* 35* 36* 38* 41* 44* E Comp 521 Files and Databases Fall 2010 24

Optimistic 2PL Basic premise: Most Xacts do not contend for the same object Idea: Make a local modified copy, and get locks when ready to commit Modified Algorithm: Obtain S locks as usual. Make changes to private copies of objects. Obtain all X locks at end of Xact, make local writes global, then release all locks. Comp 521 Files and Databases Fall 2010 25

Timestamp CC Idea: Give each object 2 timestamps and each transaction a timestamp: read-timestamp (RTS), when it was last read write-timestamp (WTS), when it was last written give each Xact a timestamp (TS) when it begins: If action ai of Xact Ti conflicts with action aj of Xact Tj, and TS(Ti) < TS(Tj), then ai must occur before aj. Otherwise, abort violating Xact. Comp 521 Files and Databases Fall 2010 26

When Xact T wants to read Object O If TS(T) < WTS(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! Contrast use of timestamps in 2PL for ddlk prevention.) If TS(T) > WTS(O): Allow T to read O. Reset RTS(O) to max(rts(o), TS(T)) Change to RTS(O) on reads must be written to disk! This and restarts represent overheads. Comp 521 Files and Databases Fall 2010 27

When Xact T wants to Write Object O If TS(T) < RTS(O), this violates timestamp order of T w.r.t. writer of O; abort and restart T. If TS(T) < WTS(O), violates timestamp order of T w.r.t. writer of O. Thomas Write Rule: We can safely ignore such outdated writes; need not restart T! (T s write is effectively followed by another write, with no intervening reads.) Allows some serializable but non conflict serializable schedules: Else, allow T to write O. Same result as T1; T2 T1 R(A) W(A) Commit Comp 521 Files and Databases Fall 2010 28 T2 W(A) Commit

Timestamp CC and Recoverability Unfortunately, unrecoverable schedules are allowed: Timestamp CC can be modified to allow only recoverable schedules: T1 W(A) Buffer all writes until writer commits (but update WTS(O) when the write is allowed.) Block readers T (where TS(T) > WTS(O)) until writer of O commits. Similar to writers holding X locks until commit, but still not quite 2PL. Comp 521 Files and Databases Fall 2010 29 T2 R(A) W(B) Commit

Multiversion Timestamp CC Idea: Let writer make a new copy while readers use an appropriate old copies: MAIN SEGMENT (Current versions of DB objects) O O O VERSION POOL (Older versions that may be useful for some active readers.) Readers are always allowed to proceed. But may be blocked until writer commits. Comp 521 Files and Databases Fall 2010 30

Multiversion CC (Contd.) Each version of an object has its writer s TS as its WTS, and the TS of the Xact that most recently read this version as its RTS. Versions are chained backward; we can discard versions that are too old to be of interest. Each Xact is classified as Reader or Writer. Writer may write some object; Reader never will. Xact declares whether it is a Reader when it begins. Comp 521 Files and Databases Fall 2010 31

Summary There are several lock-based concurrency control schemes (Strict 2PL, 2PL). Conflicts between transactions can be detected in the dependency graph The lock manager keeps track of the locks issued. Deadlocks can either be prevented or detected. Naïve locking strategies may have the phantom problem Comp 521 Files and Databases Fall 2010 32

Summary (Contd.) Index locking is common, and affects performance significantly. Needed when accessing records via index. Needed for locking logical sets of records (index locking/predicate locking). Tree-structured indexes: Straightforward use of 2PL very inefficient. In practice, better techniques now known; do record-level, rather than page-level locking. Comp 521 Files and Databases Fall 2010 33

Summary (Contd.) Multiple granularity locking reduces the overhead involved in setting locks for nested collections of objects (e.g., a file of pages); should not be confused with tree index locking! Optimistic CC aims to minimize CC overheads in an optimistic environment where reads are common and writes are rare. Optimistic CC has its own overheads however; most real systems use locking. Comp 521 Files and Databases Fall 2010 34

Summary (Contd.) Timestamp CC is another alternative to 2PL; allows some serializable schedules that 2PL does not (although converse is also true). Ensuring recoverability with Timestamp CC requires ability to block Xacts, which is similar to locking. Multiversion Timestamp CC is a variant which ensures that read-only Xacts are never restarted; they can always read a suitable older version. Additional overhead of version maintenance. Comp 521 Files and Databases Fall 2010 35