1 of 24

MATRUSRI ENGINEERING COLLEGEDEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SUBJECT NAME: DataBase Management Systems

FACULTY NAME: K Sunil Manohar Reddy

Insert Your Photo here🡪

MATRUSRI

ENGINEERING COLLEGE

2 of 24

INTRODUCTION: �THIS UNIT DEALS WITH THE CONCURRENCY CONTROL AND RECOVERY SYSTEM

UNIT-V

OUTCOMES:

Upon completion of this unit, student will be able to:

  • To familiarize theory of serializability and Understand the working of concurrency control and recovery mechanisms

MATRUSRI

ENGINEERING COLLEGE

3 of 24

CONTENTS: RECOVERY SYSTEM: FAILURE CLASSIFICATION, STORAGE STRUCTURE, RECOVERY AND ATOMICITY, LOG-BASED RECOVERY, RECOVERY WITH CONCURRENT TRANSACTIONS, BUFFER MANAGEMENT, FAILURE WITH LOSS OF NON-VOLATILE STORAGE, ADVANCED RECOVERY TECHNIQUES, REMOTE BACKUP SYSTEMS

OUTCOMES:

Upon completion of this module, student will be able to:

  • To familiarize theory of recovery mechanisms

MODULE-II

MATRUSRI

ENGINEERING COLLEGE

4 of 24

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.
    • Fail-stop assumption: non-volatile storage contents are assumed to not be corrupted by system crash
      • Database systems have numerous integrity checks to prevent corruption of disk data
  • Disk failure: a head crash or similar disk failure destroys all or part of disk storage
    • Destruction is assumed to be detectable: disk drives use checksums to detect failures

MATRUSRI

ENGINEERING COLLEGE

5 of 24

Storage Structure

Volatile storage:

Does not survive system crashes

Examples: main memory, cache memory

Nonvolatile storage:

Survives system crashes

Examples: disk, tape, flash memory, non-volatile RAM

But may still fail, losing data

Stable storage:

A mythical form of storage that survives all failures

Approximated by maintaining multiple copies on distinct nonvolatile media

See book for more details on how to implement stable storage

MATRUSRI

ENGINEERING COLLEGE

6 of 24

Stable-Storage Implementation

  • Maintain multiple copies of each block on separate disks
    • 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
    • Successful completion
    • Partial failure: destination block has incorrect information
    • Total failure: destination block was never updated
  • Protecting storage media from failure during data transfer (one solution):
    • 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.

MATRUSRI

ENGINEERING COLLEGE

7 of 24

Protecting storage media from failure (Cont.)

  • Copies of a block may differ due to failure during output operation.
  • To recover from failure:

1. 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 (Flash, Non-volatile RAM or special area of disk).

Use this information during recovery to find blocks that may be inconsistent, and only compare copies of these.

Used in hardware RAID systems

2. 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.

MATRUSRI

ENGINEERING COLLEGE

8 of 24

Recovery and Atomicity

To ensure atomicity despite failures, we first output information describing the modifications to stable storage without modifying the database itself.

We study log-based recovery mechanisms in detail

We first present key concepts

And then present the actual recovery algorithm

Less used alternative: shadow-copy and shadow-paging (brief details in book)

MATRUSRI

ENGINEERING COLLEGE

shadow-copy

9 of 24

Log-Based Recovery

  • A log is a sequence of log records. The records keep information about update activities on the database.
    • The 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 (the old

value), and V2 is the value to be written to X (the new value).

  • When Ti finishes it last statement, the log record <Ti commit> is written.
  • Two approaches using logs

Immediate database modification

Deferred database modification.

MATRUSRI

ENGINEERING COLLEGE

10 of 24

Concurrency Control and Recovery

  • With concurrent transactions, all transactions share a single disk buffer and a single log
    • A buffer block can have data items updated by one or more transactions
  • We assume that if a transaction Ti has modified an item, no other transaction can modify the same item until Ti has committed or aborted
    • i.e., the updates of uncommitted transactions should not be visible to other transactions
      • Otherwise, how to perform undo if T1 updates A, then T2 updates A and commits, and finally T1 has to abort?
    • Can be ensured by obtaining exclusive locks on updated items and holding the locks till end of transaction (strict two-phase locking)
  • Log records of different transactions may be interspersed in the log.

MATRUSRI

ENGINEERING COLLEGE

11 of 24

Undo and Redo Operations

  • Undo and Redo of Transactions
    • 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
      • Each time a data item X is restored to its old value V a special log record <Ti , X, V> is written out
      • When undo of a transaction is complete, a log record �<Ti abort> is written out.
    • 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
      • No logging is done in this case

MATRUSRI

ENGINEERING COLLEGE

12 of 24

Recovering from Failure

  • When recovering after failure:
    • Transaction Ti needs to be undone if the log
      • Contains the record <Ti start>,
      • But does not contain either the record <Ti commit> or <Ti abort>.
    • Transaction Ti needs to be redone if the log
      • Contains the records <Ti start>
      • And contains the record <Ti commit> or <Ti abort>
  • Suppose that transaction Ti was undone earlier and the <Ti abort> record was written to the log, and then a failure occurs,
  • On recovery from failure transaction Ti is redone
    • Such a redo redoes all the original actions of transaction Ti including the steps that restored old values
      • Known as repeating history
      • Seems wasteful, but simplifies recovery greatly

MATRUSRI

ENGINEERING COLLEGE

13 of 24

Advanced recovery techniques

Checkpoints

  • Redoing/undoing all transactions recorded in the log can be very slow
    • Processing the entire log is time-consuming if the system has run for a long time
    • We might unnecessarily redo transactions which have already 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.

    • Output all modified buffer blocks to the disk.

3. Write a log record < checkpoint L> onto stable storage where L is a

list of all transactions active at the time of checkpoint.

4. All updates are stopped while doing checkpointing

MATRUSRI

ENGINEERING COLLEGE

14 of 24

Checkpoints (Cont.)

  • During recovery we need to consider only the most recent transaction Ti that started before the checkpoint, and transactions that started after Ti.
    • Scan backwards from end of log to find the most recent <checkpoint L> record
    • Only transactions that are in L or started after the checkpoint need to be redone or undone
    • Transactions that committed or aborted before the checkpoint already have all their updates output to stable storage.
  • Some earlier part of the log may be needed for undo operations
    • Continue scanning backwards till a record <Ti start> is found for every transaction Ti in L.
    • Parts of log prior to earliest <Ti start> record above are not needed for recovery, and can be erased whenever desired.

MATRUSRI

ENGINEERING COLLEGE

15 of 24

Fuzzy Checkpointing

  • To avoid long interruption of normal processing during checkpointing, allow updates to happen during checkpointing
  • Fuzzy checkpointing is done as follows:

1. Temporarily stop all updates by transactions

2. Write a <checkpoint L> log record and force log to stable storage

3. Note list M of modified buffer blocks

4. Now permit transactions to proceed with their actions

5. Output to disk all modified buffer blocks in list M

blocks should not be updated while being output

Follow WAL: all log records pertaining to a block must be output before the block is output

6. Store a pointer to the checkpoint record in a fixed position

last_checkpoint on disk

MATRUSRI

ENGINEERING COLLEGE

16 of 24

Database Buffering

  • 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
  • The recovery algorithm supports the no-force policy: i.e., updated blocks need not be written to disk when transaction commits
    • force policy: requires updated blocks to be written at commit
      • More expensive commit
  • The recovery algorithm supports the steal policy: i.e., blocks containing updates of uncommitted transactions can be written to disk, even before the transaction commits

MATRUSRI

ENGINEERING COLLEGE

17 of 24

Database Buffering (Cont.)

  • 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
    • (Write ahead logging)
  • 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.

Such locks held for short duration are called latches.

  • To output a block to disk

1. First acquire an exclusive latch on the block

      • Ensures no update can be in progress on the block

2. Then perform a log flush

3. Then output the block to disk

4. Finally release the latch on the block

MATRUSRI

ENGINEERING COLLEGE

18 of 24

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:
    • Memory is partitioned before-hand between database buffer and applications, limiting flexibility.
    • 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!

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
      1. Output the page to database instead of to swap space (making sure to output log records first), if it is modified
      2. 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.

MATRUSRI

ENGINEERING COLLEGE

19 of 24

Failure with Loss of Nonvolatile Storage

  • So far 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
    • 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.

MATRUSRI

ENGINEERING COLLEGE

20 of 24

Recovering from Failure of Non-Volatile Storage

  • To recover from disk failure
    • restore database from most recent dump.
    • Consult the 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
    • Similar to fuzzy checkpointing

MATRUSRI

ENGINEERING COLLEGE

21 of 24

Remote Backup Systems

  • Remote backup systems provide high availability by allowing transaction processing to continue even if the primary site is destroyed.

MATRUSRI

ENGINEERING COLLEGE

22 of 24

Remote Backup Systems (Cont.)

  • Detection of failure: Backup site must detect when primary site has failed
    • to distinguish primary site failure from link failure maintain several communication links between the primary and the remote backup.
    • Heart-beat messages
  • Transfer of control:
    • To take over control backup site first perform recovery using its copy of the database and all the long records it has received from the primary.
      • Thus, completed transactions are redone and incomplete transactions are rolled back.
    • When the backup site takes over processing it becomes the new primary
    • To transfer control back to old primary when it recovers, old primary must receive redo logs from the old backup and apply all updates locally.

MATRUSRI

ENGINEERING COLLEGE

23 of 24

Remote Backup Systems (Cont.)

  • Time to recover: To reduce delay in takeover, backup site periodically process the redo log records (in effect, performing recovery from previous database state), performs a checkpoint, and can then delete earlier parts of the log.
  • Hot-Spare configuration permits very fast takeover:
    • Backup continually processes redo log record as they arrive, applying the updates locally.
    • When failure of the primary is detected the backup rolls back incomplete transactions, and is ready to process new transactions.
  • Alternative to remote backup: distributed database with replicated data
    • Remote backup is faster and cheaper, but less tolerant to failure

MATRUSRI

ENGINEERING COLLEGE

24 of 24

Remote Backup Systems (Cont.)

  • Ensure durability of updates by delaying transaction commit until update is logged at backup; avoid this delay by permitting lower degrees of durability.
  • One-safe: commit as soon as transaction’s commit log record is written at primary
    • Problem: updates may not arrive at backup before it takes over.
  • Two-very-safe: commit when transaction’s commit log record is written at primary and backup
    • Reduces availability since transactions cannot commit if either site fails.
  • Two-safe: proceed as in two-very-safe if both primary and backup are active. If only the primary is active, the transaction commits as soon as is commit log record is written at the primary.
    • Better availability than two-very-safe; avoids problem of lost transactions in one-safe.

MATRUSRI

ENGINEERING COLLEGE