CIS 330: Applied Database Systems Lecture 31: Transactions and Recovery Alan Demers ademers@cs.cornell.edu
(Last of) Three Topics 1. Storage and Indexing 2. Query Processing 3. Transaction Management
Transactions v Concurrent execution of user programs is essential for good DBMS performance. § Because disk accesses are frequent, and relatively slow, it is important to keep the cpu busy by working on several user programs concurrently. v A user’s program may carry out many operations on the data retrieved from the database, but the DBMS is only concerned about what data is read from or written to the database. v A transaction (Txn) is the DBMS’s abstract view of a user program: a sequence of reads and writes.
Concurrency in a DBMS v Users submit transactions, and can think of each transaction as executing by itself. § Concurrency is achieved by the DBMS, which interleaves actions (reads/writes of DB objects) of various transactions. § Each transaction must leave the database in a consistent state if the DB is consistent when the transaction begins. • DBMS will enforce some ICs, depending on the ICs declared in CREATE TABLE statements. • Beyond this, the DBMS does not really understand the semantics of the data. (e.g., it does not understand how the interest on a bank account is computed). v Issues: Effect of interleaving Txns, and crashes .
Atomicity of Transactions v A Txn might commit after completing all its actions, or it could abort (or be aborted by the DBMS) after executing some actions. v A very important property guaranteed by the DBMS for all Txns is that they are atomic . That is, a user can think of a Txn as always executing all its actions in one step, or not executing any actions at all. § DBMS logs all actions so that it can undo the actions of aborted Txns.
Example v Consider two transactions: T1: BEGIN A=A+100, B=B-100 END T2: BEGIN A=1.06*A, B=1.06*B END v Intuitively, the first transaction is transferring $100 from B’s account to A’s account. The second is crediting both accounts with a 6% interest payment. v There is 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.
Example (Contd.) v Consider a possible interleaving ( schedule ): T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B v This is OK. But what about: T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B v 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)
Scheduling Transactions v Serial schedule: Schedule that does not interleave the actions of different transactions. v Equivalent schedules : For any database state, the effect (on the set of objects in the database) of executing the first schedule is identical to the effect of executing the second schedule. v Serializable schedule : A schedule that is equivalent to some serial execution of the transactions. (Note: If each transaction preserves consistency, every serial schedule schedule preserves consistency, hence every serializable one does. )
Anomalies with Interleaved Execution v Reading Uncommitted Data (WR Conflicts, “dirty reads”): T1: R(A), W(A), R(B), W(B), Abort T2: R(A), W(A), C v Unrepeatable Reads (RW Conflicts): T1: R(A), R(A), W(A), C T2: R(A), W(A), C
Anomalies (Continued) v Overwriting Uncommitted Data (WW Conflicts): T1: W(A), W(B), C T2: W(A), W(B), C
Lock-Based Concurrency Control v Strict Two-phase Locking (Strict 2PL) Protocol : § Each Txn must obtain a S ( shared ) lock on object before reading, and an X ( exclusive ) lock on object before writing. § All locks held by a transaction are released when the transaction completes • (Non-strict) 2PL Variant: Release locks anytime, but cannot acquire locks after releasing any lock. § If a Txn holds an X lock on an object, no other Txn can get a lock (S or X) on that object. v Strict 2PL allows only serializable schedules. § Additionally, it simplifies transaction aborts § (Non-strict) 2PL also allows only serializable schedules, but involves more complex abort processing
Transaction Deadlock v Consider T1 and T2, each has a RW conflict with the other: T1: R(A), W(B), C T2: R(B), W(A), C v Clearly not serializable ... v Using strict 2PL this schedule is a deadlock: § neither Txn can proceed until the other has completed! v Most DBMS’s detect deadlocks and abort one (randomly chosen) participant. § How? See below.
Detecting deadlock: v Construct a directed graph (“waits-for”): § Nodes: one for each Txn. § Edges: Ti Tj if Ti needs a lock that is incompatible with one that Tj holds. (B) T1 T2 (A) § Deadlock if the graph contains a cycle. v If deadlock is found, abort one participant.
Avoiding Deadlock v In general, the DBMS can’t prevent this ... T1 T2 (A) v ... from turning into this ... (B) T1 T2 (A) v ... because it can’t predict the arrival of the W(B) operation at the end of T1! v But the application programmer can prevent it!
Avoiding Deadlock (continued) v Consider operation pay(x, payee) - lock my bank account - lock payee bank account - subtract x from my account - add x to payee account v Suppose two concurrent invocations: - Alice invokes pay(2, ‘Bob’) - Bob invokes pay(10, ‘Alice’) v That’s a potential deadlock! v How could we avoid this?
Avoiding Deadlock (continued) v Technique of lock ordering: § Define an ordering function • a partial order on objects ... • ... that must be defined for every pair of objects that might be locked in the same Txn. § Txn must lock objects in that order. v For the pay(x, payee) example: § Ordering function compares users’ account numbers (numerically) § Lock the account balances in order of increasing account number!
Aborting a Transaction v If a transaction Ti is aborted, all its actions have to be undone. Not only that, if Tj has read an object last written by Ti , Tj must be aborted as well! v Most systems avoid such cascading aborts by releasing a transaction’s locks only at commit time. § If Ti writes an object, Tj can read this only after Ti commits. v In order to undo the actions of an aborted Txn, most DBMS’s maintains a log in which every write is recorded. This mechanism is also used to recover from system crashes: all Txns that were active at the time of the crash are aborted when the system comes back up.
The Log v The following actions are recorded in the log: § Ti writes an object : at least one of old value, new value. • Log record must go to disk before the changed page! § Ti commits/aborts : a log record indicating this action. v Log records are chained together by Txn id, so it’s easy to undo a specific Txn. v Log is often duplexed and archived on stable storage. v All log-related activities (and in fact, all CC related activities such as lock/unlock, dealing with deadlocks etc.) are handled transparently by the DBMS.
Redo Logging v Log record contains new value ... v Cannot write any data from buffer cache to disk until commit record is forced to log! v Abort: § Just discard modified blocks from buffer cache. v Recovery: § Scan log in reverse to identify all committed Txns. § Scan log forward, rewriting all committed data. v Problem: § If a single Txn changes many blocks, there may not be enough room in the buffer cache!
Enhancement ... v Write “DONE” records in log after all blocks have been written to disk v If DONE is seen during initial back scan of recovery phase, no need to redo the block
Undo Logging v Log record contains old value ... v Cannot force C record to log until all data buffers have been written to disk! v Abort: § Just discard modified blocks from buffer cache (assuming strict 2PL). v Recovery: § Scan log forward to identify uncommitted Txns. § Scan log backward, restore uncommitted data. v Problem: § High latency for commit.
Write performance ... v If a block is used frequently, we would prefer not to have to write it back to the disk after each Txn! v With Redo logging this is fairly easy, using DONE records as discussed above. v With Undo logging it is not so easy ... v v Redo: high buffer space requirements! v Undo: high commit cost!
Undo/Redo Recovery v There are 3 phases in the Aries recovery algorithm: § Analysis : Scan the log forward (from the most recent checkpoint ) to identify all Txns that were active, and all dirty pages in the buffer pool at the time of the crash. § Redo : Redo all updates to dirty pages in the buffer pool, as needed, to ensure that all logged updates are in fact carried out and written to disk. § Undo : The writes of all Txns that were active at the crash are undone (by restoring the before value of the update, which is in the log record for the update), working backwards in the log. (Some care must be taken to handle the case of a crash occurring during the recovery process!)
Recommend
More recommend