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

Similar documents
Transaction Management: Concurrency Control

Crash Recovery. The ACID properties. Motivation

Crash Recovery. Chapter 18. Sina Meraji

Review: The ACID properties. Crash Recovery. Assumptions. Motivation. More on Steal and Force. Handling the Buffer Pool

A tomicity: All actions in the Xact happen, or none happen. D urability: If a Xact commits, its effects persist.

Crash Recovery Review: The ACID properties

Database Recovery Techniques. DBMS, 2007, CEng553 1

COURSE 4. Database Recovery 2

Atomicity: All actions in the Xact happen, or none happen. Consistency: If each Xact is consistent, and the DB starts consistent, it ends up

ACID Properties. Transaction Management: Crash Recovery (Chap. 18), part 1. Motivation. Recovery Manager. Handling the Buffer Pool.

Crash Recovery CMPSCI 645. Gerome Miklau. Slide content adapted from Ramakrishnan & Gehrke

Transaction Management: Crash Recovery (Chap. 18), part 1

some sequential execution crash! Recovery Manager replacement MAIN MEMORY policy DISK

CAS CS 460/660 Introduction to Database Systems. Recovery 1.1

Last time. Started on ARIES A recovery algorithm that guarantees Atomicity and Durability after a crash

Review: The ACID properties. Crash Recovery. Assumptions. Motivation. Preferred Policy: Steal/No-Force. Buffer Mgmt Plays a Key Role

UNIT 9 Crash Recovery. Based on: Text: Chapter 18 Skip: Section 18.7 and second half of 18.8

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

Transaction Management and Concurrency Control. Chapter 16, 17

Aries (Lecture 6, cs262a)

Overview of Transaction Management

Transaction Management Overview

Concurrency Control & Recovery

Concurrency Control & Recovery

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

Introduction. Storage Failure Recovery Logging Undo Logging Redo Logging ARIES

Outline. Purpose of this paper. Purpose of this paper. Transaction Review. Outline. Aries: A Transaction Recovery Method

Introduction to Data Management. Lecture #18 (Transactions)

Intro to Transactions

Intro to Transaction Management

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

Database Recovery. Lecture #21. Andy Pavlo Computer Science Carnegie Mellon Univ. Database Systems / Fall 2018

Problems Caused by Failures

1/29/2009. Outline ARIES. Discussion ACID. Goals. What is ARIES good for?

ARIES (& Logging) April 2-4, 2018

Database Management System

COURSE 1. Database Management Systems

Database Applications (15-415)

Database Systems. Announcement

Transaction Management: Introduction (Chap. 16)

Database Systems ( 資料庫系統 )

The transaction. Defining properties of transactions. Failures in complex systems propagate. Concurrency Control, Locking, and Recovery

Overview. Introduction to Transaction Management ACID. Transactions

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

Transactions. Transaction. Execution of a user program in a DBMS.

INSTITUTO SUPERIOR TÉCNICO Administração e optimização de Bases de Dados

Introduction to Data Management. Lecture #24 (Transactions)

Introduction TRANSACTIONS & CONCURRENCY CONTROL. Transactions. Concurrency

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

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

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

Introduction to Database Systems CSE 444

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

Transaction Management & Concurrency Control. CS 377: Database Systems

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. Chapter 17. Comp 521 Files and Databases Fall

CSE 544 Principles of Database Management Systems. Fall 2016 Lectures Transactions: recovery

Introduction to Transaction Management

CSC 261/461 Database Systems Lecture 24

CSE 444: Database Internals. Lectures 13 Transaction Schedules

Lecture 16: Transactions (Recovery) Wednesday, May 16, 2012

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

CompSci 516 Database Systems

Transaction Management. Readings for Lectures The Usual Reminders. CSE 444: Database Internals. Recovery. System Crash 2/12/17

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)

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"

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

Database Administration and Tuning

Transaction Management

Advances in Data Management Transaction Management A.Poulovassilis

Database Management System

COMPSCI/SOFTENG 351 & 751. Strategic Exercise 5 - Solutions. Transaction Processing, Crash Recovery and ER Diagrams. (May )

Concurrency Control. Conflict Serializable Schedules. Example. Chapter 17

CS122 Lecture 18 Winter Term, All hail the WAL-Rule!

CompSci 516: Database Systems

Databases - Transactions

Advanced Database Management System (CoSc3052) Database Recovery Techniques. Purpose of Database Recovery. Types of Failure.

Chapter 17: Recovery System

Failure Classification. Chapter 17: Recovery System. Recovery Algorithms. Storage Structure

AC61/AT61 DATABASE MANAGEMENT SYSTEMS JUNE 2013

T ransaction Management 4/23/2018 1

Transaction Management. Pearson Education Limited 1995, 2005

Lectures 8 & 9. Lectures 7 & 8: Transactions

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

Concurrency Control CHAPTER 17 SINA MERAJI

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

Implementing Isolation

Distributed Systems

Introduction to Data Management CSE 344

Transactions Processing (i)

Final Review. May 9, 2017

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

CSE 444: Database Internals. Lectures Transactions

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

Final Review. May 9, 2018 May 11, 2018

Motivating Example. Motivating Example. Transaction ROLLBACK. Transactions. CSE 444: Database Internals

Transaction Processing. Introduction to Databases CompSci 316 Fall 2018

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #19: Logging and Recovery 1

ARIES. Handout #24. Overview

Transcription:

DBMS Architecture Query Parser Transaction Management Query Rewriter Query Optimizer Query Executor Yanlei Diao UMass Amherst Lock Manager Concurrency Control Access Methods Buffer Manager Log Manager Recovery Disk Space Manager DB Slides Courtesy of R. Ramakrishnan and J. Gehrke 2 Motivating Examples Banking Ticketing Have you tried to get popular concert tickets online? E-commerce Have you experienced online shopping during sales? Voting Telecommunications Online gaming Concurrent User Queries Concurrent execution of queries for improved performance. E.g., when Query 1 is doing I/O, Query 2 can utilize CPU. Improve average response time (average delay that a user query experiences). Improve system throughput (number of user queries processed in each time unit). 4 5 Transactions User programs (e.g., your php code) include queries but may do many things on data retrieved. E.g., add a user rating, update avg rating. E.g., search for a ticket, think about it, and buy it. E.g., operations on Bob s bank account. DBMS is only concerned about what data is read from/ written to the database backend. A transaction is DBMS s abstract view of a user program: a sequence of reads and writes of database objects They are translated from a series of queries. We use A, B, C to denote objects, e.g. tables, tuples Concurrency Many users submit xacts, but each user thinks of his as executing by itself. DMBS interleaves reads and writes of xacts for concurrency. Consistency: each xact starts and ends with a consistent state (i.e., satisfying all integrity constraints). E.g., if an IC states that all accounts must have a positive balance, no transaction can violate this rule. Isolation: execution of one xact appears isolated from others. Nobody else can see the data in its intermediate state, e.g., account A being debited but B not being credited. 6 7 1

Recovery A transaction might commit after completing all its actions, or it could be aborted after executing some actions. Atomicity: either all actions of a xact are performed or none of them is (all-or-none). DBMS logs all actions so that it can undo the actions of aborted xacts. Durability: once a user program has been notified of success, its effect will persist despite system failure. DBMS logs all actions (a form of redundant storage) so that it can redo the actions of committed xacts. James Gray & Turing Award Jim Gray won Turing Award in 1998 for for seminal contributions to database and transaction processing research and technical leadership in system implementation 8 9 Outline Example Transaction management overview Serializability & recoverability Lock-based concurrency control Recovery Consider two transactions: T1: BEGIN A=A+0, B=B-0 END T2: BEGIN A=1.06*A, B=1.06*B END No guarantee that T1 will execute before T2 or vice-versa, if both are submitted together. However, the net effect must be equivalent to these two transactions running serially in some order! 11 Example (Contd.) Scheduling Transactions Consider a possible interleaving schedule: T1: A=A+0, B=B-0 T2: A=1.06*A, B=1.06*B But what about: T1: A=A+0, B=B-0 T2: A=1.06*A, B=1.06*B The DBMS s view of the second schedule: T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B) Serial schedule: Schedule that does not interleave the actions of different transactions. Equivalent schedules: For any database state, the effect of executing the first schedule is identical to the effect of executing the second schedule. Serializable schedule: A schedule that is equivalent to some serial execution of the transactions. If each transaction preserves consistency, every serializable schedule preserves consistency. 12 13 2

Serializability Serializability theory concerns the schedules of transactions that are not (explicitly) aborted. Given a set of such xacts submitted on the fly, ideally want to allow any serializable schedule. Recognizing any serializable schedule is highly complex, if possible. Instead, allow only a subset of serializable schedules that are easy to detect. Conflict Serializability Two schedules are conflict equivalent if: Involve the same actions of the same transactions. Every pair of conflicting actions is ordered the same way. Schedule S is conflict serializable if S is conflict equivalent to some serial schedule. 14 15 Dependency Graph Precedence graph: One node per Xact; Example T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B) Edge from Xact Ti to Xact Tj if an action of Ti precedes and conflicts with one of Tj s actions (RW, WR, WW operations on the same object). T1 A T2 Precedence graph Theorem: Schedule is conflict serializable if and only if its precedence graph is acyclic. B The schedule is not conflict serializable: The cycle in the graph reveals the problem. The output of T1 depends on T2, and vice-versa. Solution: abort one xact to break the cycle, and restart it! 16 17 Recoverability Recoverability (Contd.) Recoverability theory concerns schedules that involve aborted transactions. T1: R(A),W(A) Abort T2: R(A),W(A) Commit Unrecoverable! A schedule S is recoverable if each xact commits only after all xacts from which it read have committed. T1: R(A),W(A) Abort T2: R(A),W(A) Abort Recoverable, but with cascading aborts. S avoids cascading rollback if each xact may read only those values written by committed xacts. 18 19 3

Recoverability (Contd.) Venn Diagram for Schedules T1: R(A), W(A) Abort T2: R(A) W(A) (Commit) Recoverable, no cascading aborts, but update by T2 may be overwritten when aborting T1. So need to consider other xacts during abort! S is strict if each xact may read and write only objects previously written by committed xacts. No cascading aborts. All schedules Recoverable Avoid cascading aborts Strict Serializable Conflict serializable Serial Commited Xacts Actions of aborted xacts can be simply undone by restoring the original values of modified objects. Also Aborted Xacts 21 An Exercise: Outline T1: R(A) W(A) Commit T2: W(A) Abort Regarding only committed xacts: Serializable? Conflict serializable? Regarding both committed & aborted xacts: Recoverable? Avoids cascading aborts? Strict? Transaction management overview Serializability & recoverability Lock-based concurrency controls Recovery 22 23 Compatibility (1) Locking Protocol: Strict 2PL Strict Two-Phase Locking (Strict 2PL) Protocol: 1. Each Xact must obtain a S (shared) lock on object before reading, an X (exclusive) lock on object before writing. 2. If an Xact holds an X lock on an object, other Xact s cannot get a lock (S or X) on that object and become blocked. 3. All locks held by a xact are released when it completes. Other blocked xact s can resume now. Shared Shared Y N Exclusive N N Exclusive #. locks commit time Strict 2PL (contd.) Theorem: Strict 2PL allows only schedules whose precedence graph is acyclic. Strict 2PL only allows conflict serializable schedules! Strict 2PL is strict with respect to recoverability. A schedule is strict if each xact may read and write only objects previously written by committed xacts. Strict 2PL is recoverable without anomalies related to aborted transactions. Hence, it simplifies transaction aborts. 24 25 4

Locking Protocol: Nonstrict 2PL Nonstrict 2PL (contd.) Nonstrict Two-Phase Locking Protocol 1. Each Xact must obtain a S (shared) lock on object before reading, an X (exclusive) lock on object before writing. 2. If an Xact holds an X lock on an object, other Xact s cannot get a lock (S or X) on that object and become blocked. 3. A xact cannot request additional locks once it releases any locks. It releases locks earlier, so blocked xact s can resume earlier. Growing Shrinking Thm: Nonstrict 2PL ensures acyclicity of precedence graph. Nonstrict 2PL allows only conflict serializable schedules. An equivalent serial schedule is given by the order of xacts entering their shrinking phase. Nonstrict 2PL is recoverable but not strict! A schedule S is recoverable if each xact commits only after all xacts from which it read have committed. Nonstrict 2PL involves complex abort processing, but allows xacts to go through more quickly. #. locks #. locks commit time 26 C time 27 (2) Deadlocks Deadlock Detection Deadlock: Cycle of transactions waiting for locks to be released by each other. Two ways of dealing with deadlocks: Deadlock detection Deadlock prevention Create a waits-for graph: Nodes are Xacts. There is an edge from Xact Ti to Xact Tj if Ti is waiting for Tj to release a lock. Cycles indicate deadlocks! Periodically check for cycles in the waits-for graph. Resolve a deadlock by aborting a transaction on the cycle and releasing all its locks. 28 29 Deadlock Detection (Contd.) Waits-for vs. Precedence Graphs T1: S(A), R(A), S(B) T2: X(B),W(B) X(C) T3: S(C), R(C) T4: X(B) T1 Now T1, T2, T3 are all blocked, waiting infinitely Abort one! T2 T1 T2 X(A) Precedence graph is a theoretical tool to detect conflict serializable schedules. Real implementation: (strict) 2PL + deadlock handling 2PL prevents conflicting actions from being materialized in the DB by blocking xact s. Waits-for graph fixes infinite blocking when xact s are waiting for each other to release locks. T4 T3 T4 T3 31 5

Deadlock Prevention Assign priorities based on timestamps. The older the timestamp, the higher the xact s priority. Wait-Die: Ti wants a lock that Tj holds. If Ti has higher priority, Ti waits for Tj; otherwise Ti aborts. Lower priority xacts can never wait. Wound-wait: Ti wants a lock that Tj holds. If Ti has higher priority, Tj aborts; otherwise Ti waits. Higher priority xacts never wait. If a transaction re-starts, make sure it has its original timestamp so its priority increases. Finally, (Strict) 2PL + Deadlock detection/prevention + Index locking Concurrency Control 32 36 Outline Transaction management overview Serializability & recoverability Lock-based concurrency control Recovery 37 38 Recovery in DBMS: Motivation Assumptions Atomicity: all-or-none Transactions may abort ( rollback ). Durability: Effects of committed xacts should survive crashes. Desired behavior after system restarts: T1, T2 & T3 should be durable. T4 & T5 should be aborted (effects not seen). T1 T2 T3 T4 T5 crash! Concurrency control is in effect. Strict 2PL, in particular. Updates are happening in place. i.e. updates of an object are written from memory to the only copy of it of on disk. A simple scheme to guarantee atomicity & durability? 39 40 6

Handling the Buffer Pool Logging (a form of redundant storage) Force every write to disk at commit time? Provides durability. Poor response time. Why? G No force, how can we ensure durability? Force No Steal Simple Steal Log: A history of actions executed by DBMS Records for REDO/UNDO information for every update <XID, pageid, offset, length, old data, new data> All commits and aborts And additional control info (which we ll see soon). Steal buffer-pool frames from uncommitted Xacts? If not, poor throughput. Why? G If steal, how can we ensure atomicity? No Force Desired Writing log records to disk is more efficient than data pages Sequential writes to log (put it on a separate disk). Minimal info written to log, often smaller than a data record; multiple updates fit in a single log page. 41 42 Protocol: Write-Ahead Logging Write-Ahead Logging (WAL) Protocol: 1. Must force the log record for an update before the corresponding data page gets to disk (when steal). Guarantees atomicity 2. Must write all log records for a Xact before commit. Guarantees durability Exactly how is logging (and recovery) done? We ll study the ARIES algorithm. ARIES LSNs Each log record has a unique Log Sequence Number (LSN). LSNs always increasing. DB pagelsns Each data page contains a pagelsn. The LSN of the most recent log record for an update to that page. System keeps track of. The max LSN written to disk so far. pagelsn data page Log records flushed to disk Log tail in 43 44 ARIES LSNs DB pagelsns Big Picture: What s Stored Where Each log record has a unique Log Sequence Number (LSN). LSNs always increasing. Each data page contains a pagelsn. The LSN of the most recent log record for an update to that page. System keeps track of. The max LSN written to disk so far. WAL (1): Before a page is written, flush its log record: pagelsn pagelsn data page Log records flushed to disk Log tail in DB Data pages each w. a pagelsn master record latest checkpoint LOG LogRecords LSN, prevlsn XID type pageid length offset before-image after-image lastlsn status reclsn (earliest log record that updated the page) 45 flushed In 49 7

1. Transaction Commit When an Xact is running, Generate a log record with LSN for each update operation. Update Xact table and Dirty Page table in memory. Upon commit, Write commit record to log. WAL (2): All log records up to Xact s lastlsn are flushed. Guarantees that lastlsn. Log writes are sequential; many log records per log page. When commit returns, write end record to log. 2. Simple Transaction Abort For now, consider an explicit abort of a Xact. No crash involved. Play back the log in reverse order, UNDOing updates. Get lastlsn of Xact from Xact table. Follow chain of log records backward via prevlsn field. To perform UNDO, also need to have a lock on data! Logging continues in UNDOs! 1. Before starting UNDO, write an Abort log record. For recovering from crash during UNDO! 51 Abort (Contd.) Example of Rollback 1234 2. Before restoring old value of a page, write a Compensation Log Record (CLR): CLR has one extra field, undonextlsn, pointing to the next LSN to undo (i.e. the prevlsn of the record we re undoing now). 3. At end of UNDO, write an end log record. lastlsn status reclsn (earliest) LSN 00 05 40 45 60 LOG begin_checkpoint end_checkpoint update: T1 writes P5 update: T2 writes P3 update: T1 writes P2 T1 aborts CLR: Undo T1 LSN CLR: Undo T1 LSN T1 ends prevlsns 52 53 3. Crash Recovery: Big Picture (1) Recovery: The Analysis Phase Oldest log rec. of Xact active at crash Smallest reclsn in dirty page table after Analysis Last chkpt CRASH A R U Analysis figures which Xacts had committed or failed at crash time. Checkpoint: a snapshot of xact table and dirty page table. Start from most recent checkpoint (via master record). REDO all actions; repeat history Start from smallest reclsn in Dirty Page Table. UNDO effects of failed Xacts. Back to oldest LSN of a running xact at crash. 54 First, reconstruct state at checkpoint. Get begin_checkpoint record via the master recod. Find its end_checkpoint record, read the xact table and dirty page table (DPT). Reconstruct Xact Table and Dirty Page Table at crash; scan log forward (from last checkpoint). : End record: Remove xact from Xact Table. Other records: Add xact to Xact Table (if not there), set its lastlsn to this LSN, change xact status upon commit/abort. : Update record: If page P not in Dirty Page Table, add P to D.P.T., set its reclsn (earliest update) to this LSN. 55 8

(2) Recovery: The REDO Phase (3) Recovery: The UNDO Phase Repeat history to reconstruct DB state at crash: Reapply all updates, even those of aborted Xacts and redo CLRs. Take a set of loser xacts, undo all in reverse order of LSN! ToUndo = { l l: lastlsn of a loser xact} Repeat: a. Scan forward from the log record containing smallest reclsn in D.P.T. b. For each update or CLR log record, REDO the action. Optimizations are in textbook, but not required in this class. No additional logging! Choose largest LSN among ToUndo. If this LSN is a CLR and undonextlsn==null Write an End record for this Xact. If this LSN is a CLR, and undonextlsn!= NULL Add undonextlsn to ToUndo Else this LSN is an update. Undo the update, write a CLR, add prevlsn to ToUndo. 56 Until ToUndo is empty. 57 Example of Recovery lastlsn, status reclsn (earliest) LSN 00 05 40 45 60 LOG begin_checkpoint end_checkpoint update: T1 writes P5 update: T2 writes P3 T1 aborts CLR: Undo T1 LSN T1 ends update: T3 writes P1 update: T2 writes P5 Atomicity & Durability: none of T1, T2, T3 should have any effect on DB! prevlsns 58 Example of Recovery lastlsn, status T2: 60, running T3:, running reclsn (earliest) P5: P3: P1: LSN 00 05 40 45 60 LOG begin_checkpoint end_checkpoint update: T1 writes P5 update: T2 writes P3 T1 aborts CLR: Undo T1 LSN T1 ends update: T3 writes P1 update: T2 writes P5 Recovery algorithm: ANALYSIS: TT & DPT? REDO: T1, T2, T3 UNDO: T2, T3 prevlsns 59 Crash During Restart! lastlsn, status reclsn (earliest) Recovery algorithm: ANALYSIS: TT & DPT? LSN LOG REDO: T1, T2, T3 00,05 begin_checkpoint, UNDO: end_checkpoint T2 40,45 60 65,70 75,80,85 update: T1 writes P5 update T2 writes P3 T1 abort CLR: Undo T1 LSN, T1 End update: T3 writes P1 update: T2 writes P5 Atomicity & Durability: none of T1, T2, T3 should have any effect on DB! T2 abort, CLR: Undo T2 LSN 60 undonextlsn T3 abort, CLR: Undo T3 LSN, T3 end 61 Crash During Restart! lastlsn, status reclsn (earliest) LSN LOG 00,05 begin_checkpoint, end_checkpoint 40,45 60 65,70 75,80,85 90,95 update: T1 writes P5 update T2 writes P3 T1 abort CLR: Undo T1 LSN, T1 End update: T3 writes P1 update: T2 writes P5 T2 abort, CLR: Undo T2 LSN 60 T3 abort, CLR: Undo T3 LSN, T3 end CLR: Undo T2 LSN, T2 end undonextlsn 62 9

Main Principles behind ARIES Write-ahead logging (WAL) Any change to an object is recorded in the log. The log is written to disk before the change to the object is written, or upon commit. Repeating history during REDO On restart after a crash, repeat all actions before the crash, brings the system back to the exact state that it was in before the crash. Then undo the xacts still active at crash time. Logging changes during UNDO Changes made to DB while undoing are logged to ensure such an action isn't repeated in the event of repeated restarts. Fuzzy checkpointing to expedite recovery An efficient way to create a snapshot of Xact Table & D.P.T. NO! Logging + Replication + ARIES recovery (w. checkpoints)! 63 64