Database Recovery Course outlines Database Recovery an overview
Database Recovery Course outlines Database Recovery – an overview Ê Failure Classification Ê Algorithms/techniques and Storage Structures Data Access Recovery and Atomicity Log-Based Recovery Ê Deferred Database Modification Ê Immediate Database Modification Checkpoints – an overview Ê Checkpoints recovery steps - example Ê Recovery With Concurrent Transactions Buffer Management - Log Record Buffering Failure with Loss of Nonvolatile Storage Shadow Paging
Recovery System Database Recovery – an overview Purpose of Database Recovery- Recovery techniques ensure Ê database consistency Ê and transaction atomicity and durability despite failures Example: Fund transfer –system crashes before a fund transfer transaction completed Transaction transferring $50 from account A to account B: Read(A), (A = A– 50), Write(A) Read(B), (B = B + 50), Write(B) Failure Classification Ê Transaction failure : Ê Logical errors: transaction cannot complete due to some internal error condition Ê System errors: the database system must terminate an active transaction due to an error condition (e. g. , deadlock) Ê System crash: a power failure or other hardware or software failure causes the system to crash. System may fail because of addressing error, application error, operating system fault, etc. 2
Database Recovery - Basic assumptions Recovery – Algorithms/techniques and Storage Structures Recovery Algorithms and techniques: Recovery algorithms have two parts 1. Actions taken during normal transaction processing to ensure enough information exists to recover from failures 2. Actions taken after a failure to recover the database contents to a state that ensures atomicity, consistency and durability Recovery techniques and Storage Structures: Ê Volatile storage: does not survive system crashes – i. e. main memory, cache … Ê Nonvolatile storage: survives system crashes – i. e. disk, tape, flash memory. Ê Stable storage: storage that survives all failures Stable-Storage Implementation P Maintain multiple copies of each block on separate nonvolatile media (disks) -copies can be at remote sites to protect against disasters 3 (fire or flooding).
Physical blocks are those blocks residing on the disk. Buffer blocks are the blocks residing Ê Block movements between disk andinmain temporarily main memory. Disk memory are initiated through the following A two operations: Output (A) Data Access B Ê input(B) transfers the physical block B to main memory. Ê output(B) transfers the buffer block B to the disk, Buffer and replaces the appropriate physical block. Input (B) b. A Ê Each transaction has its private work-area in which local copies of all data items accessed and updated by it are kept. Read (b. A) T 1 Ê Transaction transfers data items between Work system buffer blocks and its private work-area of using the read and write operations T 1 Ê Transactions Ê Perform read(X) while accessing X for the first time; b. B Write (b. B) t. A T 2 t. B Main Memory Disk 4
Recovery and Atomicity Ê Modifying the database without ensuring that the transaction will commit may leave the database in an inconsistent state. A; -50 Consider transaction T that transfers $50 from account A to account B; goal is either to perform all database modifications made by T or B; +50 none at all. Ê Several output operations may be required for T (to output A and B). A failure may occur after one of these modifications have been made but before all of them are made. Ê To ensure atomicity despite failures, we first output information describing the modifications to stable storage without modifying the database itself. C We assume (initially) that Ê We study two approaches: transactions Ê log-based recovery - Two approaches using logsrun serially, ÊDeferred database modification that is, one 5
Log-Based Recovery Transaction Log: Ê For recovery from any type of failure data values prior to modification and the new value after modification are required. Ê These values and other information is stored in a sequential file called “Transaction log”. A log is kept on stable storage. The log (Transaction Log) is a sequence of log records, and maintains a record of update activities on the database. 1. When transaction T starts, it registers itself by writing a <T start> log record 2. Before T executes write(X), a log record <T, X, V 1, V 2 > is written, where V 1 is the value of X before the write, and V 2 is the value to be written to X. C it. We thatit log records directly t 3. When T finishes lastassume statement, registers theare<Twritten commit> log record. 6
Log-Based Recovery No need to regist Deferred Database Modification old values in the Ê The deferred database modification scheme records all modifications to the log, but defers all the writes to after partial commit. All modified data items in the cache is written either after a transaction ends its execution or after a fixed number of transactions have completed their execution. Ê Assume that transactions execute serially <T start> 1. Transaction starts by writing <T start> record to log. … 2. A write(X) operation results in a log record <T, X, V> being written, <T, X, V> where V is the new value for X (old value is not needed for this scheme) … 1. The write is not performed on X at this time, but is deferred. 2. When T partially commits, <T commit> is written to the log <T commit> 1. During recovery after a crash, Ê a transaction needs to be redone if and only if both <T start> and <T commit> are there in the log. Ê Redoing a transaction T ( redo T) sets the value of all data items updated by the transaction to the new values. New Stable Old Stable REDO Database State Transaction Log 7
Log-Based Recovery - Deferred Database Modificati Example - a transaction log at three instances Example: Consider the following transactions T 0 and T 1 (T 0 executes before T 1): T 0 : T 1 : read (A) read (C) A = A - 50 C = C- 100 Write (A) write (C) read (B) B =B + 50 write (B) Below we show the log as it appears at three instances of time. If log on stable storage at time of crash is as in case: (a) No redo actions need to be taken (b) redo(T 0) must be performed since <T 0 commit> is present (c) redo(T 0) must be performed followed by 8
Log-Based Recovery Immediate Database Modification Ê The immediate database modification scheme allows database updates of an uncommitted transaction to be made as the writes are issued C since undoing may be needed, C update logs must have both old value and new value Ê Update log record must be written before database item is written the log record is output directly (flushed) to stable storage Ê Output of updated blocks can take place at any time before or after transaction commit Order in which blocks are output can be different from the order in which they are written). Log <T 0 start> <T 0, A, 1000, 950> <To, B, 2000, 2050> <T 0 commit> <T 1 start> <T 1, C, 700, 600> Write Outp ut A= 950 B= 2050 C= 600 B B, BC 9
Log-Based Recovery - Immediate Database Modification - Recovery Ê Recovery procedure has two operations instead of one: Ê undo(Ti) restores the value of all data items updated by Ti to their old values, going backwards from the last log record for Ti Ê redo(Ti) sets the value of all data items updated by Ti to the new values, going forward from the first log record for Ti Remark: That is, even if the operation is executed multiple times the effect is the same as if it is executed once Ê When recovering after failure: Ê Transaction Ti needs to be undone if the log contains the record <Ti start>, but does not contain the record <Ti commit>. Ê Transaction Ti needs to be redone if the log contains both the record <Ti start> and the record <Ti commit>. Ê Undo operations are performed first, then redo operations. New Stable Database State Undo Old Stable Database State Transaction Log 10
Log-Based Recovery - Immediate Database Modification – Recovery Example: Consider the same example as before: T 0 : T 1 : read (A) read (C) A = A - 50 C = C- 100 Write (A) write (C) read (B) B =B + 50 write (B) Below we show the log as it appears at three instances of time. Recovery actions in each case above are: (a) undo (T 0): B is restored to 2000 and A to 1000. (b) undo (T 1) and redo (T 0): C is restored to 700, and then A and B are set to 950 and 2050. (c) redo (T 0) and redo (T 1): A and B are set to 950 and 2050 respectively. 11
Database Recovery Checkpoints – an overview Problems in recovery procedure as discussed earlier : 1. searching the entire log is time-consuming 2. we might unnecessarily redo transactions which have already output their updates to the database. ° When there are many concurrent transactions in the system, a system failure may cause many transactions to be redone or undone. ° Cascade rollback may, in the extreme case, cause the system to rollback to a very old state (maybe several days ago) A checkpoint is the latest database state in time which is consistent ° Checkpoints reduces the amount of redo and undo operations since all undo and redo operations can be done from check-pointed database state onwards. Streamline recovery procedure by periodically performing checkpointing 1. Output all log records currently residing in main memory onto stable 12
Database Recovery - Checkpoints recovery steps example During recovery we need to consider only the most recent transaction Ti that started before the checkpoint, and transactions that started after Ti. 1. Scan backwards from end of log to find the most recent <checkpoint> record 2. Continue scanning backwards till a record <Ti start> is found. 3. Need only consider the part of log following above start record. C Earlier part of log can be ignored during recovery, and can be erased whenever desired. 4. For all transactions (starting from Ti or later) with no <Ti commit>, execute undo(Ti). (Done only in case of immediate modification. ) 5. Scanning forward in the log, for all. Tctransactions starting from. TTf i or T 1 later with a <Ti commit>, execute redo(Ti). Example of Checkpoints T 1 can be ignored (updates already output to disk due to T 2 T 3 T 4 checkpoint system failure 13
Database Recovery With Concurrent Transactions (1/2) We modify the log-based recovery schemes to allow multiple transactions to execute concurrently. Ê All transactions share a single disk buffer and a single log Ê A buffer block can have data items updated by one or more transactions Ê Log records of different transactions may be interspersed in the log. Ê We assume concurrency control using strict twophase locking; i. e. the updates of uncommitted transactions should not be visible to other transactions Otherwise how to perform undo if T 1 updates A, then T 2 updates A and commits, and finally T 1 has to abort? ü The checkpointing technique and actions taken on recovery have to be changed C since several transactions may be active when a checkpoint 14 is
Database Recovery With Concurrent Transactions (2/2) No updates are in progress while the checkpoint is carried out The checkpoint log record is now of the form < checkpoint L>, where L is the list of transactions active at the time of the checkpoint When the system recovers from a crash, it first does the following: 1. Initialize undo-list and redo-list to empty 2. Scan the log backwards from the end, stopping when the first <checkpoint L> record is found. For each record found during the backward scan: Ê if the record is <Ti commit>, add Ti to redo-list Ê if the record is <Ti start>, then if Ti is not in redo-list, add Ti to undo-list 3. For every Ti in L, if Ti is not in redo-list, add Ti to undo-list At this point: Ê undo-list consists of incomplete transactions which must be undone, Ê and redo-list consists of finished transactions that must be redone. Recovery now continues as follows: 15
Recovery With Concurrent Transactions Example of Recovery Go over the steps of the recovery algorithm on the following log: When the system recovers from a crash, it first does the following: <T 0 start> 1. Initialize undo-list and redo-list to empty <T 0, A, 0, 10> 2. Scan the log backwards from the end, stopping when the first <checkpoint L> record <T 0 commit> is found. <T start> For each record found during the backward undo 1 <T 1, B, 0, 10> scan: Ê if the record is <Ti commit>, add Ti to redo-list <T 2 start> Ê if the record is <Ti start>, then if Ti is not in redo<T , C, 0, 10> list, add Ti to undo-list undo 2 3. For every Ti in L, if Ti is not in redo-list, add Ti <T 2, C, 10, 20> to undo-list <checkpoint {T 1, T 2}> <T 3 start> <T 3, A, 10, 20> Recovery now continues as follows: 1. Scan log backwards from most recent redo <T , D, 0, 10> 3 record, stopping when <Ti start> records have been encountered for <T 3 commit> Redo. L: T 3 Undo. L: T 1, T 2 every Ti in undo-list. Ê During the scan, perform undo for each log record that belongs to a transaction in undo-list. 2. Locate the most recent <checkpoint L> record. 3. Scan log forwards from the <checkpoint L>16
Buffer Management Log Record Buffering Ê Log records are buffered in main memory, instead of of being output directly to stable storage. Log records are output to stable storage when a block of log records in the buffer is full, or a log force operation is executed. Ê Log force is performed to commit a transaction by forcing all its log records (including the commit record) to stable storage. Ê Several log records can thus be output using a single output operation, reducing the I/O cost. The rules below must be followed if log records are buffered: Ê Log records are output to stable storage in the order in which they are created. Ê Transaction Ti enters the commit state only when the log record <Ti commit> has been output to stable storage. Ê Before a block of data in main memory is output to the database, all 17
Database Buffering Buffer Management (1/2) Database maintains an in-memory buffer of data blocks Ê When a new block is needed, if buffer is full an existing block needs to be removed from buffer Ê If the block chosen for removal has been updated, it must be output to disk K As a result of the write-ahead logging rule, if a block with uncommitted updates is output to disk, log records with undo information for the updates are output to the log on stable storage first. No updates should be in progress on a block when it is output to disk. Can be ensured as follows. Ê Before writing a data item, transaction acquires exclusive lock on block containing the data item Ê Lock can be released once the write is completed ( locks held for short duration are called latches). Ê Before a block is output to disk, the system acquires an exclusive latch on the block Database buffer can be implemented either: Ê in an area of real main-memory reserved for the database, 18
Database Buffering Operating system Buffer Management (2/2) with Database sys Database buffers are generally implemented in virtual memory in spite of some drawbacks: When operating system needs to evict a page that has been modified, C the page is written to swap space on disk. When database decides to write buffer page to disk, C buffer page may be in swap space, and may have to be read from swap space on disk and output to the database on disk, resulting in extra I/O (dual paging problem)! Ideally when swapping out a database buffer page, Ê operating system should pass control to database, which in turn outputs page to database instead of to swap space (making sure to output log records first) Ê Dual paging can thus be avoided, but common operating 19 systems do not support such functionality.
Database Recovery Failure with Loss of Nonvolatile Storage O we assumed no loss of non-volatile storage Technique similar to checkpointing used to deal with loss of non-volatile storage Ê Periodically dump the entire content of the database to stable storage A procedure similar to checkpointing must take place ÊOutput all log records currently residing in main memory onto stable storage. ÊOutput all buffer blocks onto the disk. O No transaction ÊCopy the contents of the database to stable storage. ÊOutput a record <dump> to log on stable storage. may be active during the To recover from disk failure dump ÊRestore database from most recent dump. procedure; ÊConsult the log and redo all transactions that committed after the dump C Can be extended to allow transactions to be active 20
Database Recovery - an alternative to log-based rec Shadow Paging Shadow paging is an alternative to logbased recovery; this scheme is useful if transactions execute serially Sample Page Table Idea: maintain two page tables during the lifetime of a transaction Ê the current page table, Ê and the shadow page table Store the shadow page table in nonvolatile storage, such that Ê State of the database prior to transaction execution may be recovered. 21
Database Recovery - Shadow Paging Example of Shadow Paging To start with, Ê Both the page tables are identical. Ê Only current page table is used for data item accesses during execution of the transaction. P 4 Whenever any page is about to be written for the first time Ê A copy of this page is made onto an unused page. Ê The current page table is then made to point to the copy (P 4’) Ê The update is performed on the P 4’ copy Shadow and current page tables after write to page 4 22
Database Recovery - Shadow Paging – usability! To commit a transaction : 1. Flush all modified pages in main memory to disk 2. Output current page table to disk 3. Make the current page table the new shadow page table, as follows: Ê keep a pointer to the shadow page table at a fixed (known) location on disk. Ê to make the current page table the new shadow page table, simply update the pointer to point to current page table on disk J Transaction is committed once pointer to shadow page table has been written. Analysis: é Advantages of shadow-paging over log-based schemes Ê no overhead of writing log records Ê recovery is trivial- no recovery is needed after a crash! ê Disadvantages : Ê Ê Copying the entire page table is very expensive Commit overhead is high - need to flush every updated page, and page table Data gets fragmented (related pages get separated on disk) After every transaction completion, the database pages containing old versions of modified data need to be garbage collected (Pages not 23
- Slides: 23