Last time: Saw how to implement atomicity in the presence of crashes, but only one transaction running at a time

Similar documents
Concurrency Control & Recovery

COURSE 1. Database Management Systems

Introduction to Data Management CSE 344

Lectures 8 & 9. Lectures 7 & 8: Transactions

Concurrency control 12/1/17

CSE 344 MARCH 5 TH TRANSACTIONS

Transaction Management Overview

Introduction to Data Management CSE 344

Lecture 21. Lecture 21: Concurrency & Locking

Introduction to Data Management. Lecture #24 (Transactions)

CSE 444: Database Internals. Lectures 13 Transaction Schedules

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

Database Systems CSE 414

Concurrency Control & Recovery

Transactions & Update Correctness. April 11, 2018

TRANSACTION MANAGEMENT

L i (A) = transaction T i acquires lock for element A. U i (A) = transaction T i releases lock for element A

CSE 344 MARCH 25 TH ISOLATION

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

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

Transaction Management & Concurrency Control. CS 377: Database Systems

Intro to Transaction Management

Lecture 12. Lecture 12: The IO Model & External Sorting

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

Overview of Transaction Management

Concurrency Control - Two-Phase Locking

Introduction to Data Management. Lecture #18 (Transactions)

CSE 344 MARCH 9 TH TRANSACTIONS

Transaction Management and Concurrency Control. Chapter 16, 17

Database Systems CSE 414

Transactions and Concurrency Control

Overview. Introduction to Transaction Management ACID. Transactions

5/17/17. Announcements. Review: Transactions. Outline. Review: TXNs in SQL. Review: ACID. Database Systems CSE 414.

Introduction to Data Management CSE 414

Transactions. 1. Transactions. Goals for this lecture. Today s Lecture

Database Systems CSE 414

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

6.830 Lecture Recovery 10/30/2017

Concurrency control CS 417. Distributed Systems CS 417

Transaction Management: Concurrency Control

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

Introduction to Data Management CSE 344

CSC 261/461 Database Systems Lecture 24

CSE 190D Database System Implementation

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

6.830 Lecture Transactions October 23, 2017

Transaction Management: Introduction (Chap. 16)

6.830 Lecture Recovery 10/30/2017

Database Systems. Announcement

Introduction to Data Management CSE 344

Database Management System

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

6.033: Fault Tolerance: Isolation Lecture 17 Katrina LaCurts,

Introduction to Data Management CSE 344

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

CSE 444: Database Internals. Lectures Transactions

Page 1. Goals for Today" What is a Database " Key Concept: Structured Data" CS162 Operating Systems and Systems Programming Lecture 13.

Implementing Isolation

Introduction TRANSACTIONS & CONCURRENCY CONTROL. Transactions. Concurrency

Topics in Reliable Distributed Systems

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

Lock Granularity and Consistency Levels (Lecture 7, cs262a) Ali Ghodsi and Ion Stoica, UC Berkeley February 7, 2018

Transactions. Juliana Freire. Some slides adapted from L. Delcambre, R. Ramakrishnan, G. Lindstrom, J. Ullman and Silberschatz, Korth and Sudarshan

Lecture X: Transactions

Chapter 20 Introduction to Transaction Processing Concepts and Theory

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

CSE 530A ACID. Washington University Fall 2013

Databases - Transactions II. (GF Royle, N Spadaccini ) Databases - Transactions II 1 / 22

How Oracle Does It. No Read Locks

DATABASE TRANSACTIONS. CS121: Relational Databases Fall 2017 Lecture 25

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

Example: Transfer Euro 50 from A to B

Lock-based Concurrency Control

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

Introduction to Data Management CSE 344

References. Transaction Management. Database Administration and Tuning 2012/2013. Chpt 14 Silberchatz Chpt 16 Raghu

Final Exam CSE232, Spring 97

T ransaction Management 4/23/2018 1

CAS CS 460/660 Introduction to Database Systems. Transactions and Concurrency Control 1.1

Outline. Database Management and Tuning. Isolation Guarantees (SQL Standard) Undesirable Phenomena of Concurrent Transactions. Concurrency Tuning

Transaction Management Exercises KEY

CS122 Lecture 19 Winter Term,

Database Management System

Transactions and Concurrency Control. Dr. Philip Cannata

Why Transac'ons? Database systems are normally being accessed by many users or processes at the same 'me.

Final Review. May 9, 2018 May 11, 2018

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

Final Review. May 9, 2017

h p:// Authors: Tomáš Skopal, Irena Holubová Lecturer: Mar n Svoboda, mar

Transaction Management: Concurrency Control, part 2

Locking for B+ Trees. Transaction Management: Concurrency Control, part 2. Locking for B+ Trees (contd.) Locking vs. Latching

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

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

Database Tuning and Physical Design: Execution of Transactions

Optimistic Concurrency Control. April 18, 2018

Outline. Database Tuning. Undesirable Phenomena of Concurrent Transactions. Isolation Guarantees (SQL Standard) Concurrency Tuning.

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

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

Problems Caused by Failures

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

Transcription:

6.033 Lecture 17 Isolation 4/9/2014 Last time: Saw how to implement atomicity in the presence of crashes, but only one transaction running at a time [show slides] This time, going to see how we preserve atomicity when we run multiple transactions. I.e., how to isolate running transactions from each other. Remember our goal: want state of the database after concurrent transactions run to be equivalent to running the transactions in some serial order, i.e., to be serializable So why not just run transactions serially, i.e., one at a time? Performance! Can t use multiple CPUs to execute multiple transactions at the same time that don t touch the same data, and can t make progress on some other transactions while one transactions is waiting for I/O or user input. Can t we just manually acquire locks on all records before we read / write them? Maybe, but that s pretty tricky have to know exactly what objects a transaction will access, and insert acquire and release statements at the right point to ensure isolation. Would be better if system did this automatically. Especially complex if using SQL, e.g., UPDATE emp WHERE sal > 100k Some definitions: read-set = objects read by transactions write-set = objects written by transactions For two transactions T1 and T2, if their neither their read sets nor their write sets intersection, can clearly be run in parallel. If read sets intersect, but write sets don t, also OK to run in parallel If write set of one intersections write or read set of other, then trickier.

Consider banking example: xfer(a, B, amt): begin A = A - amt B = B + amt commit int(rate): begin for each account x: x = x*(1+rate) commit [show slide] What happens if we have A=100, B=50, and concurrent xfer(a,b,10) & int(0.1)? Serial executions:.. -> [xfer] -> A=90, B=60 -> [int] -> A=99, B=66.. -> [int] -> A=110, B=55 -> [xfer] -> A=100, B=65 According to serializability def n, either outcome is OK if xfer() & int() are executed concurrently. What could happen if we run these operations concurrently? xfer: RA [100] WA [90] RB [50] WB [60] int: RA [90] WA [99] RB [60] WB [66] This is called a "schedule" for these two transactions. The two transactions didn't run serially, but is this schedule serializable? Yes: outcome is the same as if they ran sequentially.

What about: xfer: int: RA [100] //before WA in int RA [100] //before WA in xfer WA [90] //before WA in int WA [110] //after WA in xfer RB [50] WB [60] RB [60] WB [66] Not serializable: could not have possibly gotten (A=110,B=66) serially. [show slide] Suppose we have two transactions T1 & T2. For a read of object o in T1, define conflicts to be all writes of o in T2. For a write of object o in T1, define conflicts to be all reads or writes of o in T2. Formally, a schedule is serializable if, for two transactions T1 & T2, if: for all conflicts in T1, every conflicting read or write is ordered before the operation it conflicts with in T2, OR for all conflicts in T2, every conflicting read or write is ordered before the operation it conflicts with in T1 In example, neither xfer nor int sees each other s write to A, which couldn t have happened in a serializable schedule So how can we ensure we get serializability? Introduce a locking protocol that our transaction system follows as it runs to ensure a serializable execution Plan: Associate a lock with every variable; grab lock before accessing var. [ slide: locking protocol ]

Q: When should we release? Should not release right after read/write operation. Can we release after txn is done with some variable (e.g., A or B)? xfer: int: RA [100] \ T1 holds A.lock WA [90] / RA [90] \ T2 holds A.lock WA [99] / RB [50] \ T2 holds B.lock WB [55] / RB [55] \ T1 holds B.lock WB [65] / No good: (99,65) is not serial-equivalent. Violates conflict serializability rule since xfer does WA before int reads it, but int does WB before xfer reads it. If we wait until after commit to release, we will be good. This ensures our conflict serializability goal, since for the first conflicting operation in T1 (RA): - either T2 already has lock (in which case T2 will complete all of its conflict operations before T1 does any of them), or - T1 will get the lock, in which case T2 will have to wait for T1 to finish all of it s conflicting operations. Demo: waiting transactions postgres -D /usr/local/var/postgres test=# select * from accounts; username balance ----------+--------- mike 100 sam 200 (2 rows)

one terminal (blue): begin; update accounts set balance=balance+5 where username='mike'; other terminal (red): begin; update accounts set balance=balance-10 where username='mike'; transaction in red terminal hangs: why? because it conflicts with T1 would result in non-serializable behavior if it didnt; must block abort blue transaction 2nd one commits cool! Hands-on: several isolation levels in Postgres Snapshot isolation (which is slightly different from serializability as defined above) Read committed isolation (which is weaker than snapshot and serializability) I'll pretend that Postgres is using locking, even though it isn't for reads (it use an optimistic multiversion scheme; see textbook 9.4.3). What s a problem we can have here? Deadlock! Terminal 1: begin; update accounts set balance=balance-10 where username='sam'; update accounts set balance=balance-10 where username='mike'; Terminal 2: begin; update accounts set balance=balance+5 where username='mike'; update accounts set balance=balance+5 where username='sam'; What is the problem here? Terminal 1 is waiting for terminal 2, and terminal 2 is waiting for terminal 1. What do we think Postgres will do? (Kill one)

How did it do that? ERROR: deadlock detected DETAIL: Process 2101 waits for ShareLock on transaction 1008; blocked by process 2099. Process 2099 waits for ShareLock on transaction 1007; blocked by process 2101. HINT: See server log for query details. You can see what it did it detected that T1 was waiting for T2, and T2 was waiting for T1. Waits for graph graph of which other transaction a transaction is waiting to have complete sam T1 > T2 < mike When there is a cycle in this graph, it indicates a deadlock To resolve, kill one transaction Ok - -couple of things that are inefficient about what we have so far. Optimization 1) First, transactions always wait for each other, even when they are just reading the same data. Conflicts are only when at least one operation is a writer. Multiple readers should be able to operate on the same data concurrently. Introduce a new idea reader/writer locks [ slide: locking with rw locks ] A reader can acquire lock at the same time as other readers (but no writers). A writer can acquire lock only if no other readers or writers. Optimization 2) Release locks early Suppose we knew all the objects we were going to access and when we were done with them then we could lock them up front, and release locks as we finish w/ objects.

This will be fine because all conflicting operations in other transactions will still run after all conflicting operations in our transactions. Our earlier example: xfer: int: RA [100] \ T1 locks A and B lock point WA [90] /... > T1 releases A.lock it s done w/ A RA [90] \ T2 holds A.lock WA [99] / lock B: wait RB [50] \ T1 holds B.lock WB [60] / T1 releases B.lock RB [60] \ T2 holds B.lock, lock point, waited for T1 WB [66] / This is called "two-phase locking" (2PL). Phase 1: acquire read and write locks, until transaction reaches lock point. Phase 2: release locks, after lock point & done with object. If we don t know locks up front, we can still release early if we know we are done, but programmer would have to tell us this. What is problem with releasing write locks early? What if xfer() aborts? Now int will have read uncommitted data, and will have to abort this is called cascading aborts. In practice most systems implement what is known as strict 2PL, i.e., they hold write locks til end of transaction. Optimization 3: relaxed consistency Don t always need to enforce serializability Ex, read committed [show slide] W/ serializable, T1 will wait for T2 W/ read commi7ed, T2 will release read lock a9er select, which will allow T1 to run; T2 will see T1 s update (but do we care)?

Recap: saw how reason about concurrent action using conflicts, and how to ensure serializability 2PL protocol allows transaction system to implement locking for us Have now developed a complete protocol for providing atomicity in the presence of concurrent operations and crashes. Next time will start talking about how this works in a multi-node system. What to do about external actions, e.g., dispensed cash, or fire a missile, or shipped a package? Can t undo those things (usually). And aren t idempotent, so don t want to redo them! Require special care. One approach: wait for software that controls action to commit, then take the action afterwards, but have some special way to detect if action already happened (e.g., sensor of amount of cash, or whether missile bay is empty / full.)