CS162 Operating Systems and Systems Programming Review (II)

Slides:



Advertisements
Similar presentations
Database Systems (資料庫系統)
Advertisements

1 Concurrency Control Chapter Conflict Serializable Schedules  Two actions are in conflict if  they operate on the same DB item,  they belong.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17 Sections
Transaction Overview and Concurrency Control Chapters 16 & 17.
ICS 421 Spring 2010 Transactions & Concurrency Control (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
Concurrency Control R&G - Chapter 17 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue Book.
Concurrency Control. Example Schedules Constraint: The sum of A+B must be the same Before: After: T1 read(A) A = A -50 write(A) read(B)
1 Transaction Management Overview Yanlei Diao UMass Amherst March 15, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
1 Transaction Management Database recovery Concurrency control.
Concurrency. Correctness Principle A transaction is atomic -- all or none property. If it executes partly, an invalid state is likely to result. A transaction,
CS194-3/CS16x Introduction to Systems Lecture 8 Database concurrency control, Serializability, conflict serializability, 2PL and strict 2PL September.
Concurrency Control. General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Transaction Processing.
1 Transaction Management Overview Chapter Transactions  Concurrent execution of user programs is essential for good DBMS performance.  Because.
1 Transaction Management Overview Chapter Transactions  A transaction is the DBMS’s abstract view of a user program: a sequence of reads and writes.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Chapter 18.
08_Transactions_LECTURE2 DBMSs should guarantee ACID properties (Atomicity, Consistency, Isolation, Durability). This is typically done by guaranteeing.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Lecture 21 Ramakrishnan - Chapter 18.
CS 162 Discussion Section Week 9 11/11 – 11/15. Today’s Section ●Project discussion (5 min) ●Quiz (10 min) ●Lecture Review (20 min) ●Worksheet and Discussion.
Introduction to Data Management CSE 344 Lecture 23: Transactions CSE Winter
Database Systems/COMP4910/Spring05/Melikyan1 Transaction Management Overview Unit 2 Chapter 16.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#21: Concurrency Control (R&G ch. 17)
Concurrency control. Lock-based protocols One way to ensure serializability is to require the data items be accessed in a mutually exclusive manner One.
TRANSACTION MANAGEMENT R.SARAVANAKUAMR. S.NAVEEN..
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Instructor: Xintao Wu.
Chapter 15: Transactions Loc Hoang CS 157B. Definition n A transaction is a discrete unit of work that must be completely processed or not processed at.
1 Concurrency Control Lecture 22 Ramakrishnan - Chapter 19.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are.
Jinze Liu. ACID Atomicity: TX’s are either completely done or not done at all Consistency: TX’s should leave the database in a consistent state Isolation:
CS 440 Database Management Systems Concurrency Control 1.
MULTIUSER DATABASES : Concurrency and Transaction Management.
CS 440 Database Management Systems
Transaction Management
Anthony D. Joseph and Ion Stoica
Transaction Management and Concurrency
Transaction Management Overview
Concurrency Control.
Part- A Transaction Management
Transaction Management Overview
April 9, 2014 Anthony D. Joseph CS162 Operating Systems and Systems Programming Lecture 19 Transactions, Two Phase.
Transaction Management
Transaction Management Overview
Anthony D. Joseph and John Canny
April 4, 2011 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 18 Transactions April 4, 2011 Ion.
Anthony D. Joseph and Ion Stoica
Transaction Management
Transaction Management Overview
Lock Granularity and Consistency Levels (Lecture 7, cs262a)
March 9th – Transactions
Transactions Sylvia Huang CS 157B.
Lecture#21: Concurrency Control – Part 1
Lecture 21: Concurrency & Locking
October 31, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 18 TCP’s Flow Control, Transactions.
Chapter 15 : Concurrency Control
Transaction Management Overview
Lecture 22: Intro to Transactions & Logging IV
Concurrency Control R&G - Chapter 17
Transaction Management
CPSC-608 Database Systems
Transaction Management Overview
Database Systems (資料庫系統)
Lecture 18: Concurrency Control
Database Systems (資料庫系統)
Transaction Management Overview
Based on slides by Ali Ghodsi and Ion Stoica
Transactions, Properties of Transactions
Presentation transcript:

CS162 Operating Systems and Systems Programming Review (II) May 5, 2011 Ion Stoica http://inst.eecs.berkeley.edu/~cs162

Reliability & Flow control Transactions & two phase locking

Reliable Transfer Retransmit missing packets Do this efficiently Numbering of packets and ACKs Do this efficiently Keep transmitting whenever possible Detect missing ACKs and retransmit quickly Two schemes Stop & Wait Sliding Window (Go-back-n and Selective Repeat)

Stop & Wait Send; wait for ack If timeout, retransmit; else repeat DATA Receiver Sender RTT Inefficient if TRANS << RTT ACK Time

Sliding Window window = set of adjacent sequence numbers The size of the set is the window size Assume window size is n Let A be the last ack’d packet of sender without gap; then window of sender = {A+1, A+2, …, A+n} Sender can send packets in its window Let B be the last received packet without gap by receiver, then window of receiver = {B+1,…, B+n} Receiver can accept out of sequence, if in window

Go-Back-n (GBN) Transmit up to n unacknowledged packets If timeout for ACK(k), retransmit k, k+1, …

GBN Example w/o Errors Window size = 3 packets 1 {1} 2 {1, 2} {} 3 Sender Window Window size = 3 packets Receiver Window 1 {1} 2 {1, 2} {} 3 {1, 2, 3} {} 4 {2, 3, 4} {} 5 {3, 4, 5} . 6 {4, 5, 6} . Time Sender Receiver

GBN Example with Errors Window size = 3 packets 1 2 3 4 5 {} 4 is missing Timeout Packet 4 6 {5} {5,6} 4 5 6 {} Sender Receiver

Selective Repeat (SR) Sender: transmit up to n unacknowledged packets; assume packet k is lost Receiver: indicate packet k is missing Sender: retransmit packet k

SR Example with Errors Window size = 3 packets {1} 1 {1, 2} 2 {1, 2, 3} 3 {2, 3, 4} 4 {3, 4, 5} 5 6 {4, 5, 6} Nack = 4 {4,5,6} 4 Time {7} 7 Sender Receiver

Observations With sliding windows, it is possible to fully utilize a link, provided the window size is large enough. Throughput is ~ (n/RTT) Stop & Wait is like n = 1. Sender has to buffer all unacknowledged packets, because they may require retransmission Receiver may be able to accept out-of-order packets, but only up to its buffer limits

Reliability & Flow control Transactions & two phase locking

Concurrent Execution & Transactions Concurrent execution essential for good performance Disk slow, so need to keep the CPU busy by working on several user programs concurrently DBMS only concerned about what data is read/written from/to the database Not concerned about other operations performed by program on data Transaction - DBMS’s abstract view of a user program, i.e., a sequence of reads and writes.

Transaction - Example BEGIN; --BEGIN TRANSACTION UPDATE accounts SET balance = balance - 100.00 WHERE name = 'Alice'; UPDATE branches SET balance = balance - 100.00 WHERE name = (SELECT branch_name FROM accounts WHERE name = 'Alice'); UPDATE accounts SET balance = balance + 100.00 WHERE name = 'Bob'; UPDATE branches SET balance = balance + 100.00 WHERE name = (SELECT branch_name FROM accounts WHERE name = 'Bob'); COMMIT; --COMMIT WORK

The ACID properties of Transactions Atomicity: all actions in the transaction happen, or none happen Consistency: if each transaction is consistent, and the DB starts consistent, it ends up consistent Isolation: execution of one transaction is isolated from that of all others Durability: if a transaction commits, its effects persist

Transaction Scheduling Serial schedule: A schedule that does not interleave the operations of different transactions Transactions run serially (one at a time) Equivalent schedules: For any database state, the effect (on the database) and output 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 Intuitively: with a serializable schedule you only see things that could happen in situations where you were running transactions one-at-a-time.

Conflict Serializable Schedules Two operations conflict if they Belong to different transactions Are on the same data At least one of them is a write. Two schedules are conflict equivalent iff: Involve same operations of same transactions Every pair of conflicting operations is ordered the same way Schedule S is conflict serializable if S is conflict equivalent to some serial schedule

Conflict Equivalence – Intuition If you can transform an interleaved schedule by swapping consecutive non-conflicting operations of different transactions into a serial schedule, then the original schedule is conflict serializable Example: T1:R(A),W(A), R(B),W(B) T2: R(A),W(A), R(B),W(B) T1:R(A),W(A), R(B), W(B) T2: R(A), W(A), R(B),W(B) T1:R(A),W(A),R(B), W(B) T2: R(A),W(A), R(B),W(B)

Conflict Equivalence – Intuition (cont’d) If you can transform an interleaved schedule by swapping consecutive non-conflicting operations of different transactions into a serial schedule, then the original schedule is conflict serializable Example: T1:R(A),W(A),R(B), W(B) T2: R(A),W(A), R(B),W(B) T1:R(A),W(A),R(B), W(B) T2: R(A), W(A),R(B),W(B) T1:R(A),W(A),R(B),W(B) T2: R(A), W(A),R(B),W(B)

Conflict Equivalence – Intuition (cont’d) If you can transform an interleaved schedule by swapping consecutive non-conflicting operations of different transactions into a serial schedule, then the original schedule is conflict serializable Is this schedule serializable? T1:R(A), W(A) T2: R(A),W(A),

Dependency Graph Dependency graph: Transactions represented as nodes Edge from Ti to Tj: an operation of Ti conflicts with an operation of Tj Ti appears earlier than Tj in the schedule Theorem: Schedule is conflict serializable if and only if its dependency graph is acyclic

Example Conflict serializable schedule: No cycle! T1:R(A),W(A), R(B),W(B) T2: R(A),W(A), R(B),W(B) A B T1 T2 Dependency graph

Example Conflict that is not serializable: Cycle: The output of T1 depends on T2, and vice-versa T1:R(A),W(A), R(B),W(B) T2: R(A),W(A),R(B),W(B) A T1 T2 Dependency graph B

Notes on Conflict Serializability Conflict Serializability doesn’t allow all schedules that you would consider correct This is because it is strictly syntactic - it doesn’t consider the meanings of the operations or the data In practice, Conflict Serializability is what gets used, because it can be done efficiently Note: in order to allow more concurrency, some special cases do get implemented, such as for travel reservations, … Two-phase locking (2PL) is how we implement it

Locks  “Locks” to control access to data Two types of locks: Lock shared (S) lock – multiple concurrent transactions allowed to operate on data exclusive (X) lock – only one transaction can operate on data at a time S X  – Lock Compatibility Matrix

Two-Phase Locking (2PL) 1) Each transaction must obtain: S (shared) or X (exclusive) lock on data before reading, X (exclusive) lock on data before writing 2) A transaction can not request additional locks once it releases any locks. Thus, each transaction has a “growing phase” followed by a “shrinking phase” Lock Point! Growing Phase Shrinking Phase

Two-Phase Locking (2PL) 2PL guarantees conflict serializability Doesn’t allow dependency cycles; Why? Answer: a cyclic dependency cycle leads to deadlock Edge from Ti to Tj means that Ti acquires lock first and Tj needs to wait Thus, both T1 and Tj wait for each other  deadlock Schedule of conflicting transactions is conflict equivalent to a serial schedule ordered by “lock point”

Deadlock Prevention Assign priorities based on timestamps. Assume Ti wants a lock that Tj holds. Two policies are possible: Wait-Die: If Ti is older, Ti waits for Tj; otherwise Ti aborts Wound-wait: If Ti is older, Tj aborts; otherwise Ti waits If a transaction re-starts, make sure it gets its original timestamp Why?

Example T1 transfers $50 from account A to account B T2 outputs the total of accounts A and B Initially, A = $1000 and B = $2000 What are the possible output values? T1:Read(A),A:=A-50,Write(A),Read(B),B:=B+50,Write(B) T2:Read(A),Read(B),PRINT(A+B)

Is this a 2PL Schedule? No, and it is not serializable Lock_X(A) <granted> Read(A) Lock_S(A) A: = A-50 Write(A) Unlock(A) <granted> Lock_S(B) <granted> Lock_X(B) Read(B) Unlock(B) PRINT(A+B) B := B +50 Write(B) No, and it is not serializable

Is this a 2PL Schedule? Yes, so it is serializable Lock_X(A) <granted> Read(A) Lock_S(A) A: = A-50 Write(A) Lock_X(B) <granted> Unlock(A) <granted> Lock_S(B) Read(B) B := B +50 Write(B) Unlock(B) PRINT(A+B) Yes, so it is serializable

Cascading Aborts Example: T1 aborts T1:R(A),W(A), R(B),W(B), Abort Note: this is a 2PL schedule Rollback of T1 requires rollback of T2, since T2 reads a value written by T1 Solution: Strict Two-phase Locking (Strict 2PL): same as 2PL except All locks held by a transaction are released only when the transaction completes T1:R(A),W(A), R(B),W(B), Abort T2: R(A),W(A)

Strict 2PL (cont’d) In effect, “shrinking phase” is delayed until: All locks held by a transaction are released only when the transaction completes In effect, “shrinking phase” is delayed until: Transaction has committed (commit log record on disk), or Decision has been made to abort the transaction (then locks can be released after rollback).

Is this a Strict 2PL schedule? Lock_X(A) <granted> Read(A) Lock_S(A) A: = A-50 Write(A) Lock_X(B) <granted> Unlock(A) <granted> Lock_S(B) Read(B) B := B +50 Write(B) Unlock(B) PRINT(A+B) No: Cascading Abort Possible

Is this a Strict 2PL schedule? Lock_X(A) <granted> Read(A) Lock_S(A) A: = A-50 Write(A) Lock_X(B) <granted> Read(B) B := B +50 Write(B) Unlock(A) Unlock(B) <granted> Lock_S(B) <granted> PRINT(A+B)