<<

Concurrency Tuning Introduction to Transactions Outline

Database Tuning Concurrency Tuning

Nikolaus Augsten 1 Concurrency Tuning

University of Salzburg Introduction to Transactions Department of Group

Unit 7 – WS 2013/2014

Adapted from “Database Tuning” by Dennis Shasha and Philippe Bonnet.

Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 1 / 16 Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 2 / 16

Concurrency Tuning Introduction to Transactions Concurrency Tuning Introduction to Transactions What is a Transaction?1 ACID Properties

A transaction is a unit of program execution that accesses and possibly updates various data items. Example: transfer $50 from account A to account B Database system must guarantee ACID for transactions: 1. R(A) Atomicity: either all operations of the transaction are executed or none 2. A A 50 Consistency: execution of a transaction in isolation preserves the ← − 3. W (A) consistency of the database 4. R(B) Isolation: although multiple transactions may execute concurrently, 5. B B + 50 each transaction must be unaware of the other concurrent transactions. ← 6. W (B) Durability: After a transaction completes successfully, changes to the Two main issues: database persist even in case of system failure. 1. concurrent execution of multiple transactions 2. failures of various kind (e.g., hardware failure, system crash) 1 Slides of section “Introduction to Transactions” are adapted from the slides “Database System Concepts”, 6th Ed., Silberschatz, Korth, and Sudarshan

Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 3 / 16 Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 4 / 16 Concurrency Tuning Introduction to Transactions Concurrency Tuning Introduction to Transactions Atomicity Consistency

Example: transfer $50 from account A to account B Example: transfer $50 from account A to account B 1. R(A) 2. A A 50 1. R(A) ← − 2. A A 50 3. W (A) 3. W←(A) − 4. R(B) 5. B B + 50 4. R(B) ← 5. B B + 50 6. W (B) 6. W←(B) Consistency in example: sum A + B must be unchanged What if failure (hardware or software) after step 3? Consistency in general: money is lost explicit integrity constraints (e.g., ) database is inconsistent implicit integrity constraints (e.g., sum of all account balances of a Atomicity: bank branch must be equal to branch balance) either all operations or none Transaction: updates of partially executed transactions not reflected in database must see consistent database during transaction inconsistent state allowed after completion database must be consistent again

Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 5 / 16 Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 6 / 16

Concurrency Tuning Introduction to Transactions Concurrency Tuning Introduction to Transactions Isolation – Motivating Example Isolation

Trivial isolation: run transactions serially Example: transfer $50 from account A to account B Isolation for concurrent transactions: For every pair of transactions Ti 1. R(A) and Tj , it appears to Ti as if either Tj finished execution before Ti 2. A A 50 started or T started execution after T finished. 3. W←(A) − j i 4. R(B) Schedule: 5. B B + 50 specifies the chronological order of a sequence of instructions from 6. W←(B) various transactions Imagine second transaction T : equivalent schedules result in identical if they start with 2 identical databases T : R(A), R(B), print(A + B) 2 Serializable schedule: T2 is executed between steps 3 and 4 T2 sees an inconsistent database and gives wrong result equivalent to some serial schedule serializable schedule of T 1 and T 2 is either equivalent to T 1, T 2 or T 2, T 1

Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 7 / 16 Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 8 / 16 Concurrency Tuning Introduction to Transactions Concurrency Tuning Introduction to Transactions Durability Locks

When a transaction is done it commits. Example: transaction commits too early A is a mechanism to control concurrency on a data item. transaction writes A, then commits Two types of locks on a data item A: A is written to the disk buffer exclusive– xL(A): data item A can be both read and written then system crashes shared– sL(A): data item A can only be read. value of A is lost Lock request are made to manager. Durability: After a transaction has committed, the changes to the database persist even in case of system failure. Transaction is blocked until lock is granted. Commit only after all changes are permanent: Unlock A – uL(A): release the lock on a data item A either written to log file or directly to database database must recover in case of a crash

Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 9 / 16 Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 10 / 16

Concurrency Tuning Introduction to Transactions Concurrency Tuning Introduction to Transactions Lock Compatibility Locking Protocol

Lock compatibility matrix: Example transaction T2 with locking: T1 T2 shared exclusive ↓ → 1. sL(A), R(A), uL(A) shared true false 2. sL(B), R(B), uL(B) exclusive false false 3. print(A + B)

T1 holds shared lock on A: T2 uses locking, but is not serializable shared lock is granted to T2 A and/or B could be updated between steps 1 and 2 exclusive lock is not granted to T2 printed sum may be wrong T2 holds exclusive lock on A: Locking protocol: shared lock is not granted to T2 set of rules followed by all transactions while requesting/releasing locks exclusive lock is not granted to T2 locking protocol restricts the set of possible schedules Shared locks can be shared by any number of transactions.

Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 11 / 16 Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 12 / 16 Concurrency Tuning Introduction to Transactions Concurrency Tuning Introduction to Transactions Pitfalls of Locking Protocols – Deadlock Pitfalls of Locking Protocols – Starvation

Example: two concurrent money transfers T : R(A), A A + 10, R(B), B B 10, W (A), W (B) 1 ← ← − Starvation: transaction continues to wait for lock T2: R(B), B B + 50, R(A), A A 50, W (A), W (B) possible concurrent← scenario with← locks:− Examples: T1.xL(A), T1.R(A), T2.xL(B), T2.R(B), T2.xL(A), T1.xL(B),... the same transaction is repeatedly rolled back due to deadlocks T1 and T2 block each other – no progress possible a transaction continues to wait for an exclusive lock on an item while a Deadlock: situation when transactions block each other sequence of other transactions are granted shared locks Handling deadlocks: Well-designed concurrency manager avoids starvation. one of the transactions must be rolled back (i.e., undone) rolled back transaction releases locks

Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 13 / 16 Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 14 / 16

Concurrency Tuning Introduction to Transactions Concurrency Tuning Introduction to Transactions Two-Phase Locking Two-Phase Locking – Example

Example: two concurrent money transfers

Protocol that guarantees serializability. T1: R(A), A A + 10, R(B), B B 10, W (A), W (B) Phase 1: growing phase T : R(A), A ← A 50, R(B), B ← B +− 50, W (A), W (B) 2 ← − ← transaction may obtain locks Possible two-phase locking schedule: transaction may not release locks 1. T : xL(A), xL(B), R(A), R(B), W (A A + 10), uL(A) 1 ← Phase 2: shrinking phase 2. T2 : xL(A), R(A), xL(B) (wait) transaction may release locks 3. T1 : W (B B 10), uL(B) 4. T : R(B),←W (A− A 50), W (B B + 50), uL(A), uL(B) transaction may not obtain locks 2 ← − ← Equivalent serial schedule: T1, T2

Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 15 / 16 Nikolaus Augsten (DIS) DBT – Concurrency Tuning Unit 7 – WS 2013/2014 16 / 16