





















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






















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
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:
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.
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 T i 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)
granted waiting T 144 T1 T 14 T 17 123 T23 T1 T8 T 1912
Each transaction is issued a timestamp when it enters the system. If an old transaction Ti has time- stamp TS ( Ti ), a new transaction Tj is assigned time-stamp TS ( Tj ) such that TS ( Ti ) < TS ( Tj ). ๏ The protocol manages concurrent execution such that the time-stamps determine the Serializability order. ๏ In order to assure such behavior, the protocol maintains for each data Q two timestamp values: o W-timestamp ( Q ) is the largest time-stamp of any transaction that executed write ( Q ) successfully. o R-timestamp ( Q ) is the largest time-stamp of any transaction that executed read ( Q ) successfully. ๏ The timestamp ordering protocol ensures that any conflicting read and write operations are executed in timestamp order. ๏ Suppose a transaction Ti issues a read ( Q ) o If TS( Ti ) ๏ฃ W - timestamp( Q ), then Ti needs to read a value of Q that was already overwritten. ๏ง Hence, the read operation is rejected, and Ti is rolled back. o If TS( Ti )๏ณ W - timestamp( Q ), then the read operation is executed, and R-timestamp( Q ) is set to max (R-timestamp( Q ), TS( Ti )). ๏ Suppose that transaction Ti issues write ( Q ). o If TS( Ti ) < R-timestamp( Q ), then the value of Q that Ti is producing was needed previously, and the system assumed that that value would never be produced. ๏ง Hence, the write operation is rejected, and Ti is rolled back. o If TS( Ti ) < W-timestamp( Q ), then Ti is attempting to write an obsolete value of Q. ๏ง Hence, this write operation is rejected, and Ti is rolled back. o Otherwise, the write operation is executed, and W-timestamp( Q ) is set to TS( Ti ).
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 T 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
Schedule Produced by Validation: Example of schedule produced using validation T14 T read ( B ) read ( A ) ( validate ) display ( A + B ) read ( B ) B : = B- 50 read ( A ) A : = A+ ( validate ) write ( B ) write ( A )
๏ 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
modes with multiple granularity:
Compatibility Matrix with Intention Lock Modes: The compatibility matrix for all lock modes is: Multiple Granularity Locking Scheme: Transaction Ti can lock a node Q , using the following rules:
๏ Multiversion schemes keep old versions of data item to increase concurrency. o Multiversion Timestamp Ordering o Multiversion Two-Phase Locking ๏ Each successful write results in the creation of a new version of the data item written. ๏ Use timestamps to label versions. ๏ When a read ( Q ) operation is issued, select an appropriate version of Q based on the timestamp of the transaction, and return the value of the selected version. ๏ read s never have to wait as an appropriate version is returned immediately. Multiversion Timestamp Ordering: ๏ Each data item Q has a sequence of versions < Q1, Q2,...., Qm >. Each version Qk contains three data fields: o Content -- the value of version Qk. o W-timestamp ( Qk ) -- timestamp of the transaction that created (wrote) version Qk o R-timestamp ( Qk ) -- largest timestamp of a transaction that successfully read version Qk ๏ When a transaction Ti creates a new version Qk of Q , Qk's W-timestamp and R-timestamp are initialized to TS( Ti ). ๏ R-timestamp of Qk is updated whenever a transaction Tj reads Qk , and TS( Tj ) > R- timestamp( Qk ). ๏ Suppose that transaction Ti issues a read ( Q ) or write ( Q ) operation. Let Qk denote the version of Q whose write timestamp is the largest write timestamp less than or equal to TS( T i). o If transaction Ti issues a read ( Q ), then the value returned is the content of version Qk. o If transaction Ti issues a write ( Q ) ๏ง if TS( Ti ) < R-timestamp( Qk ), then transaction Ti is rolled back. ๏ง if TS( T i) = W-timestamp( Qk ), the contents of Qk are overwritten ๏ง else a new version of Q is created.
๏ 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.
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.
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.
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
๏ 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
๏ 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
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.
๏ Maintain multiple copies of each block on separate disks o Copies can be at remote sites to protect against disasters such as fire or flooding. ๏ Failure during data transfer can still result in inconsistent copies: Block transfer can result in o Successful completion o Partial Failure : destination block has incorrect information o Total Failure : destination block was never updated ๏ Protecting storage media from failure during data transfer: o Execute output operation as follows (assuming two copies of each block): ๏ง Write the information onto the first physical block. ๏ง When the first write successfully completes, write the same information onto the second physical block. ๏ง The output is completed only after the second write successfully completes. ๏ Copies of a block may differ due to failure during output operation. To recover from failure: o First find inconsistent blocks: ๏ง Expensive solution : Compare the two copies of every disk block. ๏ง Better solution : Record in-progress disk writes on non-volatile storage. Use this information during recovery to find blocks that may be inconsistent, and only compare copies of these. Used in hardware RAID systems o If either copy of an inconsistent block is detected to have an error (bad checksum), overwrite it by the other copy. If both have no error, but are different, overwrite the second block by the first block.
are the blocks residing temporarily in main memory. ๏ Block movements between disk and main memory are initiated through the following two operations: o input ( B ) transfers the physical block B to main memory. o output ( B ) transfers the buffer block B to the disk, and replaces the appropriate physical block there. ๏ Each transaction Ti has its private work-area in which local copies of all data items accessed and updated by it are kept. o Ti 's local copy of a data item X is called xi. ๏ We assume, for simplicity, that each data item fits in, and is stored inside, a single block. ๏ Transaction transfers data items between system buffer blocks and its private work-area using the following operations : o read ( X ) assigns the value of data item X to the local variable xi. o write ( X ) assigns the value of local variable xi to data item { X } in the buffer block. o both these commands may necessitate the issue of an input (BX) instruction before the assignment, if the block BX in which X resides is not already in memory. ๏ Transactions o Perform read ( X ) while accessing X for the first time; o All subsequent accesses are to the local copy. o After last access, transaction executes write ( X ). ๏ output ( BX ) need not immediately follow write ( X ). System can perform the output operation when it deems fit.
Block Storage Operations:
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 commi t> is present (c) redo ( T 0) must be performed followed by redo( T 1) since < T 0 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 < T 0 start > < T0, A, 1000, 950> T o , B, 2000, 2050 A = 950 B = 2050 < T 0 commit > < T 1 start > < T 1, C, 700, 600> C = 600 BB , BC < T 1 commit > BA Note: BX denotes block containing X. ๏ Recovery procedure has two operations instead of one: o undo ( T i) 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 ( T i) 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 ( 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 respectively. (c) Redo ( T 0) and redo ( T 1): A and B are set to 950 and 2050 respectively. Then C is set to 600
Problems in recovery procedure include:
Buffer Management:
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: o Log records are output to stable storage in the order in which they are created. o Transaction Ti enters the commit state only when the log record < Ti commit > has been output to stable storage. o Before a block of data in main memory is output to the database, all log records pertaining to data in that block must have been output to stable storage. This rule is called the write-ahead logging or WAL rule ๏ง Strictly speaking WAL only requires undo information to be output
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 ๏ 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. o Before writing a data item, transaction acquires exclusive lock on block containing the data item o Lock can be released once the write is completed. ๏ง Such locks held for short duration are called latches. o Before a block is output to disk, the system acquires an exclusive latch on the block ๏ง Ensures no update can be in progress on the block
๏ผ 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 :
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
๏ข 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 ).