1555d59e1db337cb9c90920f00d59ced.ppt
- Количество слайдов: 30
Advanced Databases CG 096 Lecture 3: Transactions and Recovery n Transactions (ACID) n Recovery Nick Rossiter [Emma-Jane Phillips-Tait] 1
Content q What is a Transaction? q ACID properties q Transaction Processing q Database Recovery 2
1. What is a Transaction? Definition n The sequence of logically linked actions that access a common database n often used in online or live systems Examples n Airlines operation n n ATM Cash operation n n Check credentials. Check money. Withdraw amount from account. Pay amount. Credit card sale n n Reserve an airline seat. Buy an airline ticket. Assemble cabin crew. Fly. Log on with the card. Verify credit card details. Check money. Deliver goods. Issue withdrawal. Internet sale n Request an item from an on-line catalogue. Check availability. Provide credit card details. Check details. Issue order. Dispatch. Issue withdrawal. 3
Origin and Needs for Transactions in DB 4
Automated Teller Machines (ATM) 5
2. A. C. I. D. properties n Transactions have 4 main properties n Atomicity - all or nothing n Consistency - preserve database integrity n Isolation - execute as if they were run alone n Durability - results are not lost by a failure 6
2. 1 Atomicity n n n All-or-nothing, no partial results. An event either happens and is committed or fails and is rolled back. n e. g. in a money transfer, debit one account, credit the other. Either both debiting and crediting operations succeed, or neither of them do. n Transaction failure is called Abort Commit and abort are irrevocable actions. There is no undo for these actions. An Abort undoes operations that have already been executed n For database operations, restore the data’s previous value from before the transaction (Rollback-it); a Rollback command will undo all actions taken since the last commit for that user. n But some real world operations are not undoable. Examples - transfer money, print ticket, fire missile 7
2. 2 Consistency n n n Every transaction should maintain DB consistency n Referential integrity - e. g. each order references an existing customer number and existing part numbers n The books balance (debits = credits, assets = liabilities) Consistency preservation is a property of a transaction, not of the database mechanisms for controlling it (unlike the A, I, and D of ACID) If each transaction maintains consistency, then a serial execution of transactions does also 8
2. 3 Isolation Intuitively, the effect of a set of transactions should be the same as if they ran independently. n Formally, an interleaved execution of transactions is serializable if its effect is equivalent to a serial one. n Implies a user view where the system runs each user’s transaction stand-alone. Of course, transactions in fact run with lots of concurrency, to use device parallelism – this will be covered later. n Transactions can use common data (shared data) n They can use the same data processing mechanisms (time sharing) 9
2. 4 Durability n n n When a transaction commits, its results will survive failures (e. g. of the application, OS, DB system … even of the disk). Makes it possible for a transaction to be a legal contract. Implementation is usually via a log n DB system writes all transaction updates to a log file n to commit, it adds a record “commit(Ti)” to the log n when the commit record is on disk, the transaction is committed. n system waits for disk ack before acknowledging to user 10
3. Transaction Processing Can be automatic (controlled by the RDBMS) or programmatic (programmed using SQL or other supported programming languages, like PL/SQL) n Identifying critical points for database changes n n Preparation for control over transaction progress n n through set of database states using labels of transaction states Management of the transactions n using explicit manipulation of transaction states and enforcing transaction operations 11
3. 1 Database State and Changes D 1, D 2 - Logically consistent states of the database data TTransaction for changing the database t 1, t 2 - Absolute time before and after the transaction 12
Transaction Parameters n diff D = D 2 D 1 can have different scale: n single data item in one memory area n many items across several files and databases n structural changes such as new database schema t = t 2 - t 1 is the time for executing T n T occupies real physical resources n n between D 1 and D 2 there may be intermediate states D 11, D 12 …; n n n some of them can be inconsistent the final state D 2 could be unreachable When T fails n first come back to D 1 (recovery) n then try again to reach D 2 (redo) 13
Transaction Operations 1 • For recovery purposes the system needs to keep track of when a transaction starts, terminates and commits. n begin: marks the beginning of a transaction execution n end: specifies that the read and write operations have ended n n marks the end limit of transaction execution commit: signals a successful end of the transaction n Any updates executed by the transaction can be safely committed to the database and will not be undone 14
Transaction Operations 2 n rollback: signals that the transaction has ended unsuccessfully n n undo: similar to rollback n n Any changes that the transaction may have applied to the database must be undone but it applies to a single operation rather than to a whole transaction redo: specifies that certain transaction operations must be redone n to ensure that all the operations of a committed transaction have been applied successfully to the database 15
Reading and Writing Specify read or write operations on the database items that are executed as part of a transaction n read (X): reads a database item named X into a program variable also named X. 1. find the address of the disk block that contains item X 2. copy that disk block into a buffer in the main memory 3. copy item X from the buffer to the program variable n write (X): writes the value of program variable X into the database 1. find the address of the disk block that contains item X 2. copy that disk block into a buffer in the main memory 3. copy item X from the program variable named X into its current location in the buffer 4. store the updated block in the buffer back to disk (this step updates the database on disk) 16
3. 2 Transaction State and Progress A transaction reaches its commit point when all operations accessing the database are completed and the result has been recorded in the log. It then writes a [commit, <transaction-id>] and terminates. BEGIN END active READ , WRITE COMMIT partially committed ROLLBACK aborted terminated When a system failure occurs, search the log file for entries [start, <transaction-id>] and if there are no logged entries [commit, <transaction-id>] then undo all operations that have logged entries [write, <transaction-id>, X, old_value, new_value] 17
3. 3 Controlling Transactions 18
Logging transaction states n n n Save the initial database state D 1 before starting the transaction T: D 1 ->D 2 (transaction begins) Save all intermediate states D 11, D 12 … (checkpoint logs) In the case of a failure at an intermediate state D 1 i before reaching D 2 n n n restore D 1 (rollback) the simplest strategy is to apply a series of atomic actions R which change the state to the initial state R: D 1 i->D 1 In the case of successful reach of the last intermediate state D 2, force-write or flush the log file to disk and change the database state to it (transaction ends) Note: if the transactions are controlled in SQL (using COMMIT), the rollback operation should be initiated explicitly (using ROLLBACK) 19
Entries in the log file n n n [start, <transaction-id>]: the start of the execution of the transaction identified by transaction-id [read, <transaction-id>, X]: the transaction identified by transaction-id reads the value of database item X [write, <transaction-id>, X, old-value, new -value]: the transaction identified by transaction-id changes the value of database item X from old-value to newvalue [commit, <transaction-id>]: the transaction identified by transaction-id has completed all data manipulations and its effect can be recorded [rollback, <transaction-id>]: the transaction identified by transaction-id has been aborted and its effect lost Procedure Credit ( trans_id INTEGER, accno INTEGER, bcode CHAR(6), amount NUMBER) old NUMBER; new NUMBER; begin SELECT balance INTO old FROM account WHERE no = accno and branch = bcode; new : = old + amount; UPDATE account SET amount = new WHERE no = accno and branch = bcode; COMMIT; EXCEPTION WHEN FAILURE THEN ROLLBACK; END credit; 17 20
Controlling Subtransactions n n All intermediate states of the transaction which are end states of the defined subtransactions should become consistent database states In the case of successful reach of an intermediate state of this type the actions are n n temporary suspension of transaction execution forced writing of all updated database blocks in main memory buffers to disk flush the log file resume transaction execution Note: If the transactions are controlled in SQL, the rollback operation can be made to an intermediate state which is labeled (using ROLLBACK TO <label>) 21
Adding checkpoints to the log file n n n A [checkpoint, <label>] record is created each time a new checkpoint is encountered [commit, <transaction-id>] entries for the active subtransactions are automatically written when the system writes to the database the effect of write operations of a successful transaction In the case of a rollback to a given checkpoint within a transaction n n an entry [commit, <transaction-id>] is logged against this subtransaction In the case of a rollback of the global transaction to a given checkpoint n no subtransactions in the path will be committed either 22
4. Database Recovery n n Need for recovery from failure during transaction n for preventing the loss of data n for avoiding global inconsistency of the database n for analyzing the possible reasons for failure Factors considered in database recovery n what is the nature of the failure? n when did the problem occur in the transaction? n what do we need to recover? 23
4. 1 Categories of Transactions at Failure T 1 - Can be ignored (committed before the previous checkpoint) T 2 - Must Redo complete (the database will be rolled back to a state when the transaction was not committed) T 3 - Must Undo (not finished, and rollback to a state when not finished) T 4 - Must Redo if possible (finished, but not committed) T 5 - Must Undo (did not finish and the rollback will lead to a state before it was even started) 24
4. 2 Types of Failure If an error or hardware/software crash occurs between the begin and end of transaction, the database will be inconsistent n Catastrophic failure n Restore a previous copy of the database from archival backup n Apply transaction log n to reconstruct a more current state by redoing committed transaction operations up to failure point Perform an incremental dump logging each transaction Non-catastrophic failure n Reverse the changes that caused the inconsistency by undoing the operations and possibly redoing legitimate changes which were lost n The entries kept in the system log are consulted during recovery. n No need to use the complete archival copy of the database. n n 25
4. 3 Recovery Strategy n n n Mirroring n keep two copies of the database maintained simultaneously Backup n periodically dump the complete state of the database to some form of tertiary storage System Logging n keep track of all transaction operations affecting the values of database items. n the log is kept on disk so that it is not affected by failures except for disk and catastrophic failures. 26
Write-ahead Logging n n n Deferred Update: n no actual update of the database until the transaction reaches its commit point n 1. Updates recorded in log n 2. Transaction commit point n 3. Force log to the disk n 4. Update the database n n FAILURE! • REDO database from log entries • No UNDO necessary because database has not been altered Immediate Update: n the database may be updated by some operations of a transaction before it reaches its commit point. 1. Update X recorded in log FAILURE! 2. Update X in database • UNDO X 3. Update Y recorded in log 4. Transaction commit point 5. Force log to the disk FAILURE! 6. Update Y in database • REDO Y FAILURE! • UNDO in reverse order to log • REDO in committed log order (using the write log entry) 27
Page Buffering Technique n Data is not updated ‘in place’ n The database is considered to be made up of a number of n fixed-size disk blocks or pages, for recovery purposes. n n A page table with n entries is constructed where the ith page table entry points to the ith database page on disk. The current page table points to most recent current database pages on disk Database data pages/blocks page 5 Page table 1 2 3 4 5 6 page 1 page 4 page 2 page 3 page 6 28
Paging Technique – cont. n When a transaction begins executing n the current page table is copied into a buffer page table n buffer page table is then saved n buffer page table is never modified during transaction execution n write operations— new copy of database page is created and current page table entry modified to point to new disk page/block Current page table After updating pages 2, 6 1 2 3 4 5 6 Database data pages (blocks) page 5 (old) page 1 page 4 page 2 (old) page 3 Buffer page table (not updated) 1 2 3 4 5 6 page 2 (new) page 5 (new) 29
Paging Technique - final n n n Database To recover from a failure data pages n check the state of the database (blocks) (before transaction execution) page 5 through the buffer page table Current page table (old) n free modified pages After updating page 1 n discard current page table pages 2, 6 n Recover state by reinstating page 4 1 the buffer page table to 2 page 2 become the current page table 3 (old) 4 once more 5 Commit a transaction page 3 6 n discard previous buffer page 6 n free old referenced page tables page 2 (new) Garbage collection page 5 (new) Buffer page table (not updated) 1 2 3 4 5 6 30
1555d59e1db337cb9c90920f00d59ced.ppt