Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CONCURRENCY CONTROL - Database Management Systems (DBMS), Lecture notes of Database Management Systems (DBMS)

Concurrency Control: Lock-based Protocols, Timestamp-based Protocols, Validation-based Protocols, Multiple Granularity, Multi-version Schemes, Deadlock Handling, Insert and Delete Operations, Weak Levels of Consistency, Concurrency of Index Structures. Recovery System: Failure Classification, Storage Structure, Recovery and Atomicity, Log-Based Recovery, Recovery with Concurrent Transactions, Buffer Management, Failure with Loss of Nonvolatile Storage, Advanced Recovery Techniques, Remote Backup

Typology: Lecture notes

2021/2022

Available from 08/07/2022

ajithjoseph27
ajithjoseph27 🇮🇳

4 documents

1 / 29

Toggle sidebar

Related documents


Partial preview of the text

Download CONCURRENCY CONTROL - Database Management Systems (DBMS) and more Lecture notes Database Management Systems (DBMS) in PDF only on Docsity! UNIT V Concurrency Control: Lock-based Protocols, Timestamp-based Protocols, Validation-based Protocols, Multiple Granularity, Multi-version Schemes, Deadlock Handling, Insert and Delete Operations, Weak Levels of Consistency, Concurrency of Index Structures. Recovery System: Failure Classification, Storage Structure, Recovery and Atomicity, Log-Based Recovery, Recovery with Concurrent Transactions, Buffer Management, Failure with Loss of Nonvolatile Storage, Advanced Recovery Techniques, Remote Backup Systems CONCURRENCY CONTROL LOCK-BASED PROTOCOLS: A locking protocol is a set of rules followed by all transactions while requesting and releasing locks. Locking protocols restrict the set of possible schedules. A lock is a mechanism to control concurrent access to a data item. Data items can be locked in two modes: 1. exclusive (X) mode. Data item can be both read as well as written. X-lock is requested using lock-X instruction. 2. shared (S) mode. Data item can only be read. S-lock is requested using lock-S instruction. Lock requests are made to concurrency-control manager. Transaction can proceed only after request is granted. Lock-compatibility matrix:  A transaction may be granted a lock on an item if the requested lock is compatible with locks already held on the item by other transactions  Any number of transactions can hold shared locks on an item, o But if any transaction holds an exclusive on the item no other transaction may hold any lock on the item.  If a lock cannot be granted, the requesting transaction is made to wait till all incompatible locks held by other transactions have been released. The lock is then granted. Example of a transaction performing locking: T2: lock-S(A); read (A); unlock(A); lock-S(B); read (B); unlock(B); display(A+B)  Locking as above is not sufficient to guarantee Serializability — if A and B get updated in- between the read of A and B, the displayed sum would be wrong. Pitfalls of Lock-Based Protocols: Consider the partial schedule Neither T3 nor T4 can make progress — executing lock-S (B) causes T4 to wait for T3 to release its lock on B, while executing lock-X (A) causes T3 to wait for T4 to release its lock on A. Such a situation is called a deadlock. To handle a deadlock one of T3 or T4 must be rolled back and its locks released. The potential for deadlock exists in most locking protocols.  Starvation is also possible if concurrency control manager is badly designed. For example: A transaction may be waiting for an X-lock on an item, while a sequence of other transactions request and are granted an S-lock on the same item. The same transaction is repeatedly rolled back due to deadlocks.  Concurrency control manager can be designed to prevent starvation. The Two-Phase Locking Protocol: This is a protocol which ensures conflict- serializable schedules. Phase 1: Growing Phase: The transaction may obtain locks and transaction may not release locks. Phase 2: Shrinking Phase: The transaction may release locks and transaction may not obtain locks.  This protocol assures Serializability. It can be proved that the transactions can be serialized in the order of their lock points (i.e. the point where a transaction acquired its final lock).  Two-phase locking does not ensure freedom from deadlocks  Cascading roll-back is possible under two-phase locking. To avoid this, follow a modified protocol called strict two-phase locking. Here a transaction must hold all its exclusive locks till it commits/aborts.  Rigorous two-phase locking is even stricter: here all locks are held till commit/abort. In this protocol transactions can be serialized in the order in which they commit.  There can be conflict serializable schedules that cannot be obtained if two-phase locking is used.  However, in the absence of extra information (Ex: ordering of access to data), two-phase locking is needed for conflict Serializability in the following sense: Given a transaction Ti that does not follow two-phase locking, we can find a transaction Tj that uses two-phase locking, and a schedule for Ti and Tj that is not conflict serializable. Lock Conversions  Two-phase locking with lock conversions:  First Phase: o can acquire a lock-S on item o can acquire a lock-X on item o can convert a lock-S to a lock-X (upgrade) Example Use of the Protocol: A partial schedule for several data items for transactions with timestamps 1, 2, 3, 4, 5 T1 T2 T3 T4 T5 read(Y) read(X) read(Y) read(X) abort write(Y) write(Z) write(Z) abort read(X) read(Z) write(Y) write(Z) Correctness of Timestamp-Ordering Protocol: The timestamp-ordering protocol guarantees Serializability since all the arcs in the precedence graph are of the form:  Thus, there will be no cycles in the precedence graph Timestamp protocol ensures freedom from deadlock as no transaction ever waits. But the schedule may not be cascade-free, and may not even be recoverable. Recoverability and Cascade Freedom  Problem with timestamp-ordering protocol: o Suppose Ti aborts, but Tj has read a data item written by Ti o Then Tj must abort; if Tj had been allowed to commit earlier, the schedule is not recoverable. o Further, any transaction that has read a data item written by Tj must abort o This can lead to cascading rollback --- that is, a chain of rollbacks  Solution 1: o A transaction is structured such that its writes are all performed at the end of its processing o All writes of a transaction form an atomic action; no transaction may execute while a transaction is being written o A transaction that aborts is restarted with a new timestamp  Solution 2: Limited form of locking: wait for data to be committed before reading it  Solution 3: Use commit dependencies to ensure recoverability transaction with smaller timestamp transaction with larger timestamp Thomas’ Write Rule  Modified version of the timestamp-ordering protocol in which obsolete write operations may be ignored under certain circumstances.  When Ti attempts to write data item Q, if TS(Ti) < W-timestamp(Q), then Ti is attempting to write an obsolete value of {Q}. o Rather than rolling back Ti as the timestamp ordering protocol would have done, this {write} operation can be ignored.  Otherwise this protocol is the same as the timestamp ordering protocol.  Thomas' Write Rule allows greater potential concurrency. o Allows some view-serializable schedules that are not conflict-serializable. Validation-Based Protocol Execution of transaction Ti is done in three phases. 1) Read and execution phase: Transaction Ti writes only to temporary local variables 2) Validation phase: Transaction Ti performs a “validation test'' to determine if local variables can be written without violating Serializability. 3) Write phase: If Ti is validated, the updates are applied to database; otherwise, Ti is rolled back. The three phases of concurrently executing transactions can be interleaved, but each transaction must go through the three phases in that order. Assume for simplicity that the validation and write phase occur together, atomically and serially i.e., only one transaction executes validation/write at a time. Also called as optimistic concurrency control since transaction executes fully in the hope that all will go well during validation Each transaction Ti has 3 timestamps 1. Start(Ti) : the time when Ti started its execution 2. Validation(Ti): the time when Ti entered its validation phase 3. Finish(Ti) : the time when Ti finished its write phase Serializability order is determined by timestamp given at validation time, to increase concurrency. Thus TS(Ti) is given the value of Validation(Ti). This protocol is useful and gives greater degree of concurrency if probability of conflicts is low. o because the Serializability order is not pre-decided, and o Relatively few transactions will have to be rolled back. Validation Test for Transaction Tj  If for all Ti with TS (Ti) < TS (Tj) either one of the following condition holds: o finish(Ti) < start(Tj) o start(Tj) < finish(Ti) < validation(Tj) and the set of data items written by Ti does not intersect with the set of data items read by Tj.  Then validation succeeds and Tj can be committed. Otherwise, validation fails and Tj is aborted.  Justification: Either the first condition is satisfied, and there is no overlapped execution, or the second condition is satisfied and o Writes of Tj do not affect reads of Ti since they occur after Ti has finished its reads. o Writes of Ti do not affect reads of Tj since Tj does not read any item written by Ti. Schedule Produced by Validation: Example of schedule produced using validation T14 T15 read(B) read(A) (validate) display (A+B) read(B) B:= B-50 read(A) A:= A+50 (validate) write (B) write (A) MULTIPLE GRANULARITY  Allow data items to be of various sizes and define a hierarchy of data granularities, where the small granularities are nested within larger ones  Can be represented graphically as a tree (but don't confuse with tree-locking protocol)  When a transaction locks a node in the tree explicitly, it implicitly locks all nodes descendents in the same mode.  Granularity of locking (level in tree where locking is done): o fine granularity (lower in tree): high concurrency, high locking overhead o coarse granularity (higher in tree): low locking overhead, low concurrency The levels, starting from the coarsest (top) level are 1. database 2. area 3. file 4. record Intention Lock Modes: In addition to S and X lock modes, there are three additional lock modes with multiple granularity: 1. intention-shared (IS): Indicates explicit locking at a lower level of the tree but only with shared locks. 2. intention-exclusive (IX): Indicates explicit locking at a lower level with exclusive or shared locks 3. shared and intention-exclusive (SIX): The sub tree rooted by that node is locked explicitly in shared mode and explicit locking is being done at a lower level with exclusive-mode locks.  Intention locks allow a higher level node to be locked in S or X mode without having to check all descendent nodes.  System is deadlocked if there is a set of transactions such that every transaction in the set is waiting for another transaction in the set to unlock the data item. Deadlock Prevention: Deadlock prevention protocols ensure that the system will never enter into a deadlock state. Some prevention strategies: o Require that each transaction locks all its data items before it begins execution (pre declaration). o Impose partial ordering of all data items and require that a transaction can lock data items only in the order specified by the partial order (graph-based protocol). Deadlock Prevention Strategies: Following schemes use transaction timestamps for the sake of deadlock prevention alone. 1. Wait-die scheme (non-preemptive): Older transaction may wait for younger one to release data item. Younger transactions never wait for older ones; they are rolled back instead. A transaction may die several times before acquiring needed data item 2. Wound-wait scheme (preemptive) : Older transaction wounds (forces rollback) of younger transaction instead of waiting for it. Younger transactions may wait for older ones. May be fewer rollbacks than wait-die scheme. Both in wait-die and in wound-wait schemes, a rolled back transaction is restarted with its original timestamp. Older transactions thus have precedence over newer ones, and starvation is hence avoided. 3. Timeout-Based Schemes: A transaction waits for a lock only for a specified amount of time. After that, the wait times out and the transaction is rolled back. Thus deadlocks are not possible. It is simple to implement; but starvation is possible. It is also difficult to determine good value of the timeout interval. Deadlock Detection: Deadlocks can be described as a wait-for graph, which consists of a pair G = (V,E). o V is a set of vertices (all the transactions in the system) o E is a set of edges; each element is an ordered pair Ti Tj.  If Ti  Tj is in E, then there is a directed edge from Ti to Tj, implying that Ti is waiting for Tj to release a data item.  When Ti requests a data item currently being held by Tj, then the edge Ti Tj is inserted in the wait-for graph. This edge is removed only when Tj is no longer holding a data item needed by Ti.  The system is in a deadlock state if and only if the wait-for graph has a cycle. Must invoke a deadlock-detection algorithm periodically to look for cycles. Wait-for graph without a cycle Wait-for graph with a cycle Deadlock Recovery: When a deadlock is detected:  Some transaction will have to rolled back (made a victim) to break deadlock. Select that transaction as victim that will incur minimum cost.  Rollback -- determine how far to roll back transaction o Total rollback: Abort the transaction and then restart it. o More effective to roll back transaction only as far as necessary to break deadlock.  Starvation happens if same transaction is always chosen as victim. Include the number of rollbacks in the cost factor to avoid starvation Insert and Delete Operations:  If two-phase locking is used : o A delete operation may be performed only if the transaction deleting the tuple has an exclusive lock on the tuple to be deleted. o A transaction that inserts a new tuple into database is given an X-mode lock on the tuple  Insertions and deletions can lead to the phantom phenomenon. o A transaction that scans a relation (Ex: find sum of balances of all accounts in Perryridge) and a transaction that inserts a tuple in the relation (Ex: insert a new account at Perryridge) conflict (conceptually) in spite of not accessing any tuple in common. o If only tuple locks are used, non-serializable schedules can result  Ex: the scan transaction does not see the new account, but reads some other tuple written by the update transaction  The transaction scanning the relation is reading information that indicates what tuples the relation contains, while a transaction inserting a tuple updates the same information. The information should be locked.  One solution: o Associate a data item with the relation, to represent the information about what tuples the relation contains. o Transactions scanning the relation acquire a shared lock in the data item, o Transactions inserting or deleting a tuple acquire an exclusive lock on the data item.  Above protocol provides very low concurrency for insertions/deletions.  Index locking protocols provide higher concurrency while preventing the phantom phenomenon, by requiring locks on certain index buckets. Index Locking Protocol: According to this protocol every relation must have at least one index. A transaction can access tuples only after finding them through one or more indices on the relation  A transaction Ti that performs a lookup must lock all index leaf nodes that it accesses, in S-mode o Even if the leaf node does not contain any tuple satisfying the index lookup  A transaction Ti that inserts, updates or deletes a tuple ti in a relation r o must update all indices to r o must obtain exclusive locks on all index leaf nodes affected by the insert/update/delete  The rules of the two-phase locking protocol must be observed  Guarantees that phantom phenomenon won’t occur Concurrency in Index Structures: Indices are unlike other database items in that their only job is to help in accessing data.  Index-structures are typically accessed very often, much more than other database items. o Treating index-structures like other database items, Ex: by 2-phase locking of index nodes can lead to low concurrency.  There are several index concurrency protocols where locks on internal nodes are released early, and not in a two-phase fashion. o It is acceptable to have nonserializable concurrent access to an index as long as the accuracy of the index is maintained.  In particular, the exact values read in an internal node of a B+-tree are irrelevant so long as we land up in the correct leaf node.  Example of index concurrency protocol:  Use crabbing instead of two-phase locking on the nodes of the B+-tree, as follows. During search/insertion/deletion: o First lock the root node in shared mode. o After locking all required children of a node in shared mode, release the lock on the node. o During insertion/deletion, upgrade leaf node locks to exclusive mode. o When splitting or coalescing requires changes to a parent, lock parent in exclusive mode.  Above protocol can cause excessive deadlocks o Searches coming down the tree deadlock with updates going up the tree o Can abort and restart search, without affecting transaction  Better protocols are available; such as the B-link tree protocol o Intuition: Release lock on parent before acquiring lock on child and deal with changes that may have happened between lock release and acquire Weak Levels of Consistency  Degree-two consistency: differs from two-phase locking in that S-locks may be released at any time, and locks may be acquired at any time o X-locks must be held till end of transaction o Serializability is not guaranteed, programmer must ensure that no erroneous database state will occur.  Cursor stability: o For reads, each tuple is locked, read, and lock is immediately released o X-locks are held till end of transaction o Special case of degree-two consistency Weak Levels of Consistency in SQL: SQL allows non-serializable executions  Serializable: is the default  Repeatable read: allows only committed records to be read, and repeating a read should return the same value (so read locks should be retained) o However, the phantom phenomenon need not be prevented  T1 may see some records inserted by T2, but may not see others inserted by T2  Read committed: same as degree two consistency, but most systems implement it as cursor- stability  Read uncommitted: allows even uncommitted data to be read In many database systems, read committed is the default consistency level and has to be explicitly changed to serializable when required as: n set isolation level serializable Recovery and Atomicity:  Modifying the database without ensuring that the transaction will commit may leave the database in an inconsistent state.  Consider transaction Ti that transfers $50 from account A to account B; goal is either to perform all database modifications made by Ti or none at all.  Several output operations may be required for Ti (to output A and B). A failure may occur after one of these modifications has 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. Log-Based Recovery:  Log is a sequence of log records, and maintains a record of update activities on the database. A log is kept on stable storage.  When transaction Ti starts, it registers itself by writing a <Ti start>log record  Before Ti executes write(X), a log record <Ti, X, V1, V2> is written, where V1 is the value of X before the write, and V2 is the value to be written to X. o Log record notes that Ti has performed a write on data item Xj Xj had value V1 before the write, and will have value V2 after the write.  When Ti finishes it last statement, the log record <Ti commit> is written.  We assume for now that log records are written directly to stable storage.  Two approaches using logs o Deferred database modification o Immediate database modification Deferred Database Modification:  The deferred database modification scheme records all modifications to the log, but defers all the writes to after partial commit.  Assume that transactions execute serially  Transaction starts by writing <Ti start> record to log.  A write(X) operation results in a log record <Ti, X, V> being written, where V is the new value for X o Note: old value is not needed for this scheme  The write is not performed on X at this time, but is deferred.  When Ti partially commits, <Ti commit> is written to the log  Finally, the log records are read and used to actually execute the previously deferred writes. 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(T0) must be performed since <T0 commit> is present (c) redo(T0) must be performed followed by redo(T1) since <T0 commit> and <Ti commit> are present Immediate Database Modification: The immediate database modification scheme allows database updates of an uncommitted transaction to be made as the writes are issued o since undoing may be needed, update logs must have both old value and new value  Update log record must be written before database item is written o We assume that the log record is output directly to stable storage o Can be extended to postpone log record output, so long as prior to execution of an output(B) operation for a data block B, all log records corresponding to items B must be 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. Immediate Database Modification Example Log Write Output <T0 start> <T0, A, 1000, 950> To, B, 2000, 2050 A = 950 B = 2050 <T0 commit> <T1 start> <T1, C, 700, 600> C = 600 BB, BC <T1 commit> BA Note: BX denotes block containing X.  Recovery procedure has two operations instead of one: o 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 o 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  Both operations must be idempotent. i.e., even if the operation is executed multiple times the effect is the same as if it is executed once  Needed since operations may get re-executed during recovery  When recovering after failure: o Transaction Ti needs to be undone if the log contains the record <Ti start>, but does not contain the record <Ti commit>. o 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. Immediate DB Modification Recovery Example Below we show the log as it appears at three instances of time. Recovery actions in each case above are: (a) Undo (T0): B is restored to 2000 and A to 1000. (b) Undo (T1) and redo (T0): C is restored to 700, and then A and B are set to 950 and 2050 respectively. (c) Redo (T0) and redo (T1): A and B are set to 950 and 2050 respectively. Then C is set to 600 Checkpoints: Problems in recovery procedure include: 1. Searching the entire log is time-consuming 2. We might unnecessarily redo transactions which have already 3. Output their updates to the database. Streamline recovery procedure by periodically performing checkpointing 1. Output all log records currently residing in main memory onto stable storage. 2. Output all modified buffer blocks to the disk. 3. Write a log record <checkpoint> onto stable storage. 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 to only consider the part of log following above start record. Earlier part of log can be ignored during recovery, and can be erased whenever desired.  For all transactions (starting from Ti or later) with no <Ti commit>, execute undo(Ti).  Scanning forward in the log, for all transactions starting from Ti or later with a <Ti commit>, execute redo(Ti). Example of Checkpoints T1 can be ignored (updates already output to disk due to checkpoint) T2 and T3 redone. T4 undone Buffer Management:  Database buffer can be implemented either in an area of real main-memory reserved for the database, or in virtual memory  Implementing buffer in reserved main-memory has drawbacks: 1. Memory is partitioned before-hand between database buffer and applications, limiting flexibility. 2. Needs may change, and although operating system knows best how memory should be divided up at any time, it cannot change the partitioning of memory.  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, the page is written to swap space on disk.  When database decides to write buffer page to disk, 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!. This is known as dual paging problem.  Ideally when OS needs to evict a page from the buffer, it should pass control to database, which in turn should  Output the page to database instead of to swap space (making sure to output log records first), if it is modified  Release the page from the buffer, for the OS to use Dual paging can thus be avoided, but common operating systems do not support such functionality. Failure with Loss of Nonvolatile Storage: A technique similar to checkpointing is used to deal with loss of non-volatile storage  Periodically dump the entire content of the database to stable storage  No transaction may be active during the dump procedure; 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.  Copy the contents of the database to stable storage.  Output a record <dump> to log on stable storage. Recovering from Failure of Non-Volatile Storage: To recover from disk failure, Restore DB from most recent dump. Consult log and redo all transactions that committed after the dump.  Can be extended to allow transactions to be active during dump; known as fuzzy dump or online dump Advanced Recovery Algorithm Key Features:  Support for high-concurrency locking techniques, which release locks early.  Recovery based on “repeating history”, whereby recovery executes exactly the same actions as normal processing.  including redo of log records of incomplete transactions, followed by subsequent undo  Supports logical undo.  Easier to understand/show correctness. Logical Undo Logging: Operations like B+ tree insertions and deletions release locks early. They cannot be undone by restoring old values (physical undo), since once a lock is released, other transactions may have updated the B+ tree. Instead, insertions (resp. deletions) are undone by executing a deletion (resp. insertion) operation (known as logical undo).  For such operations, undo log records should contain the undo operation to be executed. Such logging is called logical undo logging, in contrast to physical undo logging. Operations are called logical operations.  Other examples: o delete of tuple, to undo insert of tuple, allows early lock release on space allocation information o subtract amount deposited, to undo deposit, allows early lock release on bank balance Physical Redo: Redo information is logged physically (i.e., new value for each write) even for operations with logical undo. Physical redo logging does not conflict with early lock release. Logical redo is very complicated since database state on disk may not be “operation consistent” when recovery starts. Operation Logging: Operation logging is done as follows: 1. When operation starts, log <Ti, Oj, operation-begin>. Here Oj is a unique identifier of the operation instance. 2. While operation is executing, normal log records with physical redo and physical undo information are logged. 3. When operation completes, <Ti, Oj, operation-end, U> is logged, where U contains information needed to perform a logical undo information. Example: insert of (key, record-id) pair (K5, RID7) into index I9  If crash/rollback occurs before operation completes: The operation-end log record is not found, and the physical undo information is used to undo operation.  If crash/rollback occurs after the operation completes:  the operation-end log record is found, and in this case  logical undo is performed using U; physical undo information for the operation is ignored.  Redo of operation (after crash) still uses physical redo information. Transaction Rollback: Rollback of transaction Ti is done as follows: Scan the log backwards, 1. If a log record <Ti, X, V1, V2> is found, perform the undo and log a special redo-only log record <Ti, X, V1>. 2. If a <Ti, Oj, operation-end, U> record is found  Rollback the operation logically using the undo information U.  Updates performed during roll back are logged just like during normal operation execution.  At the end of the operation rollback, instead of logging an operation-end record, generate a record <Ti, Oj, operation-abort>.  Skip all preceding log records for Ti until the record <Ti, Oj operation-begin> is found 3. If a redo-only record is found ignore it 4. If a <Ti, Oj, operation-abort> record is found:  skip all preceding log records for Ti until the record <Ti, Oj, operation-begin> is found. 5. Stop the scan when the record <Ti, start> is found 6. Add a <Ti, abort> record to the log Some points to note:  Cases 3 and 4 above can occur only if the database crashes while a transaction is being rolled back.  Skipping of log records as in case 4 is important to prevent multiple rollback of the same operation. Transaction Rollback Example: Example with a complete and an incomplete operation <T1, start> <T1, O1, operation-begin> …. <T1, X, 10, K5> <T1, Y, 45, RID7> <T1, O1, operation-end, (delete I9, K5, RID7)> <T1, O2, operation-begin> <T1, Z, 45, 70>  T1 Rollback begins here <T1, Z, 45>  redo-only log record during physical undo (of incomplete O2) <T1, Y, .., ..>  Normal redo records for logical undo of O1 … <T1, O1, operation-abort>  What if crash occurred immediately after this? <T1, abort> Crash Recovery: The following actions are taken when recovering from system crash 1. Redo Phase: Scan log forward from last < checkpoint L> record till end of log 1. Repeat history by physically redoing all updates of all transactions, 2. Create an undo-list during the scan as follows  undo-list is set to L initially  Whenever <Ti start> is found Ti is added to undo-list  Whenever <Ti commit> or <Ti abort> is found, Ti is deleted from undo-list 1) Page LSN: Each page contains a PageLSN which is the LSN of the last log record whose effects are reflected on the page  To update a page: o X-latch the page, and write the log record o Update the page o Record the LSN of the log record in PageLSN o Unlock page  To flush page to disk, must first S-latch page o Thus page state on disk is operation consistent.  Required to support physiological redo  PageLSN is used during recovery to prevent repeated redo, thus ensuring idempotence 2) Log Record: Each log record contains LSN of previous log record of the same transaction  LSN in log record may be implicit  Special redo-only log record called compensation log record (CLR) used to log actions taken during recovery that never need to be undone  Serves the role of operation-abort log records used in advanced recovery algorithm  Has a field UndoNextLSN to note next (earlier) record to be undone  Records in between would have already been undone  Required to avoid repeated undo of already undone actions 3) DirtyPage Table: List of pages in the buffer that have been updated contains,  PageLSN of the page  RecLSN is an LSN such that log records before this LSN have already been applied to the page version on disk  Set to current end of log when a page is inserted into dirty page table  Recorded in checkpoints, helps to minimize redo work 4) Checkpoint Log: Contains:  DirtyPageTable and list of active transactions  For each active transaction, LastLSN, the LSN of the last log record written by the transaction  Fixed position on disk notes LSN of last completed checkpoint log record  Dirty pages are not written out at checkpoint time. Instead, they are flushed out continuously, in the background  Checkpoint is thus very low overhead. It can be done frequently ARIES Recovery Algorithm: ARIES recovery involves three passes 1. Analysis Pass: Determines  Which transactions to undo  Which pages were dirty (disk version not up to date) at time of crash  RedoLSN: LSN from which redo should start 2. Redo Pass: Repeats history, redoing all actions from RedoLSN  RecLSN and PageLSNs are used to avoid redoing actions already reflected on page 3. Undo Pass: Rolls back all incomplete transactions. Transactions whose abort was complete earlier are not undone  Key idea: no need to undo these transactions: earlier undo actions were logged, and are redone as required 1. ARIES Analysis Pass: Analysis determines where redo should start. It starts from last complete checkpoint log record  Reads DirtyPageTable from log record  Sets RedoLSN = min of RecLSNs of all pages in DirtyPageTable  In case no pages are dirty, RedoLSN = checkpoint record’s LSN  Sets undo-list = list of transactions in checkpoint log record  Reads LSN of last log record for each transaction in undo-list from checkpoint log record  Scans forward from checkpoint  If any log record found for transaction not in undo-list, adds transaction to undo-list  Whenever an update log record is found  If page is not in DirtyPageTable, it is added with RecLSN set to LSN of the update log record  If transaction end log record found, delete transaction from undo-list  Keeps track of last log record for each transaction in undo-list  May be needed for later undo  At end of analysis pass:  RedoLSN determines where to start redo pass  RecLSN for each page in DirtyPageTable used to minimize redo work  All transactions in undo-list need to be rolled back 2. ARIES Redo Pass: Repeats history by replaying every action not already reflected in the page on disk, as follows:  Scans forward from RedoLSN. Whenever an update log record is found: 1. If the page is not in DirtyPageTable or the LSN of the log record is less than the RecLSN of the page in DirtyPageTable, then skip the log record 2. Otherwise fetch the page from disk. If the PageLSN of the page fetched from disk is less than the LSN of the log record, redo the log record NOTE: if either test is negative the effects of the log record have already appeared on the page. First test avoids even fetching the page from disk. 3. ARIES Undo Pass: Performs backward scan on log undoing all transaction in undo-list. Undo has to go back till start of earliest incomplete transaction  Backward scan optimized by skipping unneeded log records as follows:  Next LSN to be undone for each transaction set to LSN of last log record for transaction found by analysis pass.  At each step pick largest of these LSNs to undo, skip back to it and undo it  After undoing a log record  For ordinary log records, set next LSN to be undone for transaction to PrevLSN noted in the log record  For compensation log records (CLRs) set next LSN to be undo to UndoNextLSN noted in the log record  All intervening records are skipped since they would have been undone already  Undos are performed as described earlier ARIES Undo Actions: When an undo is performed for an update log record  Generate a CLR containing the undo action performed (actions performed during undo are logged physicaly or physiologically).  CLR for record n noted as n’ in figure below  Set UndoNextLSN of the CLR to the PrevLSN value of the update log record  Arrows indicate UndoNextLSN value  ARIES supports partial rollback  Used Ex: to handle deadlocks by rolling back just enough to release reqd. locks  Figure indicates forward actions after partial rollbacks  records 3 and 4 initially, later 5 and 6, then full rollback
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved