CONCURRENCY CONTROL, TRANSACTIONS, LOCKING, AND RECOVERY

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

Defining properties of transactions

Transactions. CS6450: Distributed Systems Lecture 16. Ryan Stutsman

TWO-PHASE COMMIT ATTRIBUTION 5/11/2018. George Porter May 9 and 11, 2018

PRIMARY-BACKUP REPLICATION

CS122 Lecture 15 Winter Term,

Transaction Management & Concurrency Control. CS 377: Database Systems

Transactions and Recovery Study Question Solutions

Database System Concepts

Topics in Reliable Distributed Systems

UNIT-IV TRANSACTION PROCESSING CONCEPTS

Database Recovery. Dr. Bassam Hammo

TRANSACTION PROCESSING CONCEPTS

In This Lecture. Transactions and Recovery. Transactions. Transactions. Isolation and Durability. Atomicity and Consistency. Transactions Recovery

CSE 190D Database System Implementation

Lecture X: Transactions

Database Systems. Announcement

Overview of Transaction Management

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

Transaction Management. Pearson Education Limited 1995, 2005

Problems Caused by Failures

Transaction Management Overview

Database Management Systems

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

Concurrency Control & Recovery

Transaction Processing. Introduction to Databases CompSci 316 Fall 2018

Chapter 17: Recovery System

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

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

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

) Intel)(TX)memory):) Transac'onal) Synchroniza'on) Extensions)(TSX))) Transac'ons)

Example: Transfer Euro 50 from A to B

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

CSE 444: Database Internals. Lectures 13 Transaction Schedules

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

Log-Based Recovery Schemes

DATABASE DESIGN I - 1DL300

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

Database Management Systems CSEP 544. Lecture 9: Transactions and Recovery

CSE 344 MARCH 5 TH TRANSACTIONS

CSE 444: Database Internals. Lectures Transactions

) Intel)(TX)memory):) Transac'onal) Synchroniza'on) Extensions)(TSX))) Transac'ons)

Intro to Transactions

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

Concurrency Control & Recovery

CS 5614: Transaction Processing 121. Transaction = Unit of Work Recall ACID Properties (from Module 1)

CSE 124: REPLICATED STATE MACHINES. George Porter November 8 and 10, 2017

T ransaction Management 4/23/2018 1

Introduction. Storage Failure Recovery Logging Undo Logging Redo Logging ARIES

) Intel)(TX)memory):) Transac'onal) Synchroniza'on) Extensions)(TSX))) Transac'ons)

Introduction to Data Management CSE 344

Lecture 21: Logging Schemes /645 Database Systems (Fall 2017) Carnegie Mellon University Prof. Andy Pavlo

Chapter 22. Transaction Management

Transactions and Concurrency Control. Dr. Philip Cannata

Transaction Management Exercises KEY

Database Applications (15-415)

REPLICATED STATE MACHINES

Chapter 16: Recovery System. Chapter 16: Recovery System

CS122 Lecture 19 Winter Term,

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

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

UNIT 4 TRANSACTIONS. Objective

CSE 344 MARCH 9 TH TRANSACTIONS

Database Systems CSE 414

Chapter 6 Distributed Concurrency Control

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

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

Chapter 17: Recovery System

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

CSE 344 MARCH 21 ST TRANSACTIONS

UNIT IV TRANSACTION MANAGEMENT

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

Recovery System These slides are a modified version of the slides of the book Database System Concepts (Chapter 17), 5th Ed McGraw-Hill by

COURSE 1. Database Management Systems

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

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

RECOVERY CHAPTER 21,23 (6/E) CHAPTER 17,19 (5/E)

XI. Transactions CS Computer App in Business: Databases. Lecture Topics

Overview. Introduction to Transaction Management ACID. Transactions

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

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

Silberschatz and Galvin Chapter 18

Chapter 13: Transactions

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

6.830 Lecture Recovery 10/30/2017

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

Introduction to Data Management CSE 414

Causal Consistency and Two-Phase Commit

Announcements. Motivating Example. Transaction ROLLBACK. Motivating Example. CSE 444: Database Internals. Lab 2 extended until Monday

Transaction Concept. Two main issues to deal with:

Transaction Management: Introduction (Chap. 16)

Chapter 15: Transactions

Chapter 22. Introduction to Database Recovery Protocols. Copyright 2012 Pearson Education, Inc.

Transactions and Concurrency Control

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

Transactions. Chapter 15. New Chapter. CS 2550 / Spring 2006 Principles of Database Systems. Roadmap. Concept of Transaction.

Database Management Systems 2010/11

Database System Concepts

Database System Concepts

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

Transcription:

CONCURRENCY CONTROL, TRANSACTIONS, LOCKING, AND RECOVERY George Porter May 18, 2018 ATTRIBUTION These slides are released under an Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0) Creative Commons license These slides incorporate material from: Tanenbaum and Van Steen, Dist. Systems: Principles and Paradigms Kyle Jamieson, Princeton University (also under a CC BY-NC-SA 3.0 Creative Commons license) 1

OUTLINE 1. Quorums 2. Distributed transactions and twophase locking 3. Write-ahead logging (WAL) NETWORK PARTITIONS Some failure (either network or host) keeps replicas from communicating with one another How to proceed with read/write transactions in case where not all replicas can be contacted? 2

QUORUM CONSENSUS TECHNIQUES Allow updates to be completed even when only a subset of replicas are available Or have the latest version of a data item May need to bring some set of replicas up to date to proceed One technique is the weighted voting scheme Assign a number of votes to each replica Determine a write quorum W and a read quorum R W > half the total votes, R+W>total number of votes Ensures that there is some overlap between read and write quorums Read operation guaranteed access to one site with latest version QUORUM EXAMPLE ts:2 ts:2 ts:2 ts:1 ts:1 Write quorum Read quorum 3

QUORUM CONSENSUS Write operations can be propagated in background to replicas not in quorum Assumes eventual repair of any network partition Operations are slowed by the necessity of first gathering a quorum Though previously, all writes had to go to all replicas With quorum system, must only contact subset of replicas QUORUM EXAMPLE ts:2 ts:2 ts:2 ts:1 ts:1 Write quorum Read quorum 5 replicas, read quorum: 5, write quorum: 1 R+W>5 votes ensures overlap between any read/write quorum How does this perform for reads? How does this perform for writes? 4

QUORUM EXAMPLE ts:2 ts:2 ts:2 ts:1 ts:1 Write quorum Read quorum 5 replicas, read quorum: 1, write quorum: 5 R+W>5 votes ensures overlap between any read/write quorum How does this perform for reads? How does this perform for writes? QUORUM EXAMPLE ts:2 ts:2 ts:2 ts:1 ts:1 Write quorum Read quorum 5 replicas, read quorum: 3, write quorum: 3 R+W>5 votes ensures overlap between any read/write quorum How does this perform for reads? How does this perform for writes? 5

MANAGING CONCURRENCY What happens if W=1, R=5, and two clients concurrently update a variable? Might choose different and orthogonal write quorums Solution 1: Synchronization outside of the system Solution 2: Choose W > (N/2) where N=# nodes OUTLINE 1. Quorums 2. Distributed transactions and twophase locking 3. Write-ahead logging (WAL) 6

TWO CONCURRENT TRANSACTIONS transaction sum(a, B): begin_tx a read(a) b read(b) print a + b commit_tx transaction transfer(a, B): begin_tx a read(a) if a < 10 then abort_tx else write(a, a 10) b read(b) write(b, b+10) commit_tx ISOLATION BETWEEN TRANSACTIONS Isolation: sum appears to happen either completely before or completely after transfer Sometimes called before-after atomicity Schedule for transactions is an ordering of the operations performed by those transactions 7

PROBLEM FOR CONCURRENT EXECUTION: INCONSISTENT RETRIEVAL Serial execution of transactions transfer then sum: debit transfer: r A w A r B w B sum: r A r B Concurrent execution resulting in inconsistent retrieval, result differing from any serial execution: debit credit credit transfer: r A w A r B w B sum: r A r B Time = commit SERIAL EQUIVALENCE One approach to avoiding concurrency problems is to execute transactions in serial order Do not being transaction n+1 until transaction n either commits or aborts Results in unacceptable performance What if transactions are not accessing the same data items? Another approach is to ensure that transactions execute in serial equivalent order Interleave operations only when it is safe to do so Final result same as if transactions performed in serial order How to achieve serial equivalence? 8

ACHIEVING SERIAL EQUIVALENCE Approaches to serial equivalence Locking Optimistic concurrency control (check on commit) Achieve serial equivalence by locking necessary data items Avoiding deadlock Breaking deadlock Granularity of locks Read/write (shared/exclusive) locks Two phase commit for distributed lock management TWO-PHASE LOCKING Serial equivalence requires that all data accesses be serialized w/respect to other transactions If two transactions conflict, then all operations within transactions must be (logically) done either before or after other transaction Two-phase locking: do not acquire any new locks after releasing any lock Growing phase, followed by shrinking phase Strict two-phase locking Must worry about aborted transactions (dirty writes) Do not release locks until transaction aborts/commits 9

DEADLOCK EXAMPLE Transaction T Withdraw(A, 4); Deposit(B, 4) Balance=A.Read() A.Write(balance-4) Balance=B.read() Read A Read B Transaction U Withdraw(C, 3); Deposit(B, 3) Balance = C.Read() C.Write(balance-3) Balance=B.Read() Read C Read B time DEADLOCK EXAMPLE Transaction T Withdraw(A, 4); Deposit(B, 4) Balance=A.Read() A.Write(balance-4) Balance=B.read() B.Write(balance+4) Read A Read B Wait U Transaction U Withdraw(C, 3); Deposit(B, 3) Balance = C.Read() C.Write(balance-3) Balance=B.Read() Read C Read B time 10

DEADLOCK EXAMPLE Transaction T Withdraw(A, 4); Deposit(B, 4) Balance=A.Read() A.Write(balance-4) Balance=B.read() B.Write(balance+4) Read A Read B Wait U Transaction U Withdraw(C, 3); Deposit(B, 3) Balance = C.Read() C.Write(balance-3) Balance=B.Read() Read C Read B B.Write(balance+3) Wait T time Deadlock DEADLOCKS Deadlock occurs when a loop is created in a logical waits for graph Transaction T acquires A, Transaction U acquires B Transaction T waits for B, Transaction U waits for B Neither transaction able to make forward progress Held By A C Held By T Waits For Waits For U Held By B Held By 11

WHAT ABOUT WHEN A TRANSACTION GETS STUCK? WAITS-FOR GRAPH Held By A C Held By T Waits For Waits For U Held By B Held By Cycle: T B U B T 12

DEADLOCKS Deadlock occurs when a loop is created in a logical waits for graph Transaction T acquires A, Transaction U acquires B Transaction T waits for B, Transaction U waits for A Neither transaction able to make forward progress Loop can be arbitrarily long T U V W Z T Typically, loops are short (one hop) DEADLOCK PREVENTION Gather all locks at beginning of transaction Can we still run into problems? 13

DEADLOCK PREVENTION Gather all locks at beginning of transaction Can we still run into problems? Yes, if locks are acquired in different order Transaction T Lock A Lock B (Wait) Transaction U Lock B Lock A (Wait) Deadlock DEADLOCK PREVENTION Gather all locks at beginning of transaction Can we still run into problems? Yes, if locks are acquired in different order Solution: gather locks in canonical order Won t work in many circumstances Cannot predict locking requirements of interactive apps Unnecessarily reduces concurrency E.g., acquire read (write) lock for long-running transaction Prevent any other transaction from acquiring write (read) lock 14

DEADLOCK DETECTION Concurrent thread keeps track of waits-for graph Typically lock manager For each successful lock operation, track resources held by each transaction Not strictly necessary For each conflicting lock operation (condition var wait), track transaction waits for relationship On lock release, delete edges corresponding to signaled transactions DEADLOCK DETECTION Search for loops in waits for graph on adding edge Abort one transaction in the loop Release all locks associated with transaction Releasing locks signals blocked transaction in loop, break deadlock Aborted transaction must restart Starvation Which transaction to abort? 15

DEADLOCK DETECTION Search for loops in waits for graph on adding edge Abort one transaction in the loop Release all locks associated with transaction Releasing locks signals blocked transaction in loop, break deadlock Aborted transaction must restart Starvation Which transaction to abort? Assign priorities to transactions? Shortest running transaction? PREVENTING DEADLOCKS THROUGH TIMEOUTS Assign a timeout with each lock When timeout expires, lock becomes vulnerable If no other transaction waiting for lock, continue as normal If other transaction waiting, abort transaction holding vulnerable lock Pros/cons? 16

PREVENTING DEADLOCKS THROUGH TIMEOUTS Assign a timeout with each lock When timeout expires, lock becomes vulnerable If no other transaction waiting for lock, continue as normal If other transaction waiting, abort transaction holding vulnerable lock Pros/cons Pros: simpler to implement? Cons: abort transaction when no deadlock exists, what value to set for timeout (lightly loaded vs. heavily loaded system) GROUP EXERCISE Serial equivalence requires that once a transaction has released a lock on an object, it is not allowed to obtain any more locks. Assume that transactions have two operations: read(i) and write(i, value) True or false? Why or why not? 17

OUTLINE 1. Quorums 2. Write-ahead logging (WAL) 3. Distributed transactions and twophase locking FAILURE MODEL: CRASH FAILURES Standard crash failure model: Machines are prone to crashes: Disk contents (non-volatile storage) okay Memory contents (volatile storage) lost Machines don t misbehave ( Byzantine ) 18

ACCOUNT TRANSFER TRANSACTION Transfers $10 from account A to account B transaction transfer(a, B): begin_tx a read(a) if a < 10 then abort_tx else write(a, a 10) b read(b) write(b, b+10) commit_tx PROBLEM Suppose $100 in A, $100 in B transaction transfer(a, B): begin_tx a read(a) if a < 10 then abort_tx else write(a, a 10) b read(b) write(b, b+10) commit_tx commit_tx starts the commit protocol: write(a, $90) to disk write(b, $110) to disk What happens if system crash after first write, but before second write? After recovery: Partial writes, money is lost 19

SYSTEM STRUCTURE Smallest unit of storage that can be atomically written to non-volatile storage is called a page Buffer manager moves pages between buffer pool (in volatile memory) and disk (in non-volatile storage) Buffer manager Buffer pool Page Disk Non-volatile storage TWO DESIGN CHOICES 1. Force all a transaction s writes to disk before transaction commits? Yes: force policy No: no-force policy 1. May uncommitted transactions writes overwrite committed values on disk? Yes: steal policy No: no-steal policy 20

PERFORMANCE IMPLICATIONS 1. Force all a transaction s writes to disk before transaction commits? Yes: force policy Then slower disk writes appear on the critical path of a committing transaction 1. May uncommitted transactions writes overwrite committed values on disk? No: no-steal policy Then buffer manager loses write scheduling flexibility UNDO & REDO 1. Force all a transaction s writes to disk before transaction commits? Choose no: no-force policy Need support for redo: complete a committed transaction s writes on disk 2. May uncommitted transactions writes overwrite committed values on disk? Choose yes: steal policy Need support for undo: removing the effects of an uncommitted transaction on disk 21

HOW TO IMPLEMENT UNDO & REDO? Log: A sequential file that stores information about transactions and system state Resides in separate, non-volatile storage One entry in the log for each update, commit, abort operation: called a log record Log record contains: Monotonic-increasing log sequence number (LSN) Old value (before image) of the item for undo New value (after image) of the item for redo SYSTEM STRUCTURE Buffer pool (volatile memory) and disk (nonvolatile) The log resides on a separate partition or disk (in non-volatile storage) Buffer pool Buffer manager Page Disk Log Non-volatile storage 22

WRITE-AHEAD LOGGING (WAL) Ensures atomicity in the event of system crashes under no-force/steal buffer management 1. Force all log records pertaining to an updated page into the (non-volatile) log before any writes to page itself 2. A transaction is not considered committed until all its log records (including commit record) are forced into the log WAL EXAMPLE force_log_entry(a, old=$100, new=$90) force_log_entry(b, old=$100, new=$110) write(a, $90) write(b, $110) force_log_entry(commit) Does not have to flush to disk What if the commit log record size > the page size? How to ensure each log record is written atomically? Write a checksum of entire log entry 23

24