Sie sind auf Seite 1von 30

Crash Recovery

 Recovery manager is responsible for


restoring the database to the consistent state
after failure.
 It should be highly available.
 Ensures atomicity and durability
Failure classification
 Transaction failure
 Logical error – can occur due to bad i/p, data
not found, overflow etc.
 System error – deadlock

 System crash – h/w malfunction, bug in the


database s/w or os.
 Disk failure – head crash, failure during data
transfer
Stealing frames and forcing pages
 Can the changes made to an object O in the
buffer by T be written to disk before T
commits?
 This write is executed when another transaction
wants to bring in a page and buffer manage
chooses to replace the frame having O
 If such writes are allowed it is termed as a

steal approach
 When a transaction commits must we ensure
that all the changes it has made to objects in
the buffer are immediately forced to disk?
 If this approach is used it is termed as force
approach.
 From the view point of recovery manager it is
simplest to use a buffer manager with a no-
steal, force approach.
 But due to the disadvantages of no-steal
force approach most systems use a steal, no
force approach.
Recovery related steps during normal
execution
 Recovery manager maintains a log of all
modifications to the database on stable
storage.
 Stable storage is implemented by maintaining
multiple copies of information on non-volatile
storage devices.
 Log entries describing a change to the
database are written to stable storage before
the change is made.
 If the transactions are running serially then
there are 2 ways for recovery. They are;
 Log based recovery
 Shadow paging
Log based recovery
 Also known as trail or journal.
 Consists of log records.
 Log record has;
 Transaction identifier
 Data item identifier
 Old value
 New value
 When transaction T starts, it registers itself by writing
a
<T start>log record
 Before T executes write(X), a log record
<T, X, V1, V2> is written.
 When T finishes it last statement, the log record <T
commit> is written
Types of log based recovery
 Deferred database modification
 Immediate database modification
 Checkpoints
Deferred database modification

 Here all the database modifications are


recorded in the log, but the execution of write
operation is deferred or postponed until the
transaction partially commits.
 When the transaction partially commits the log
info is used to execute deferred writes.
 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.
 This operations must be idempotent. That is, even if the
operation is executed multiple times the effect is the same as
if it is executed once.
 Example transactions T0 and T1 (T0 executes before T1):
T0: read (A) T1 : read (C)
A: - A - 50 C:- C- 100
Write (A) write (C)
read (B)
B:- B + 50
write (B)
 A write (X) operation results in a log record
<T, X, V> being written, where V is the new
value for X.
 Old value is not needed for this scheme.
 The write is not performed on X at this time,
but is deferred.
 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
 This scheme allows database updates of an
uncommitted transaction to be made as the
writes are issued
 Since undoing may be needed, update logs
must have both old value and new value.
 Recovery procedure has two operations
 undo(T) restores the value of all data items
updated by T to their old values, going
backwards from the last log record for T.
 redo(T) sets the value of all data items
updated by T to the new values, going forward
from the first log record for T.
 Both operations must be idempotent:
 When recovering after failure:
 Transaction T needs to be undone if the log contains the
record
<T start>, but does not contain the record <T commit>.
 Transaction Ti needs to be redone if the log contains both
the record <T start> and the record <T commit>.
 Undo operations are performed first, then redo
operations.
Recovery actions in each case 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 are;
1. Searching the entire log is time-consuming
2. We might unnecessarily redo transactions which have
already output their updates to the database.
 To overcome this the system can streamline recovery
procedure by periodically performing check pointing.
The steps involved are;
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.
Steps performed by system during
recovery
1. Scan backwards from end of log to find the most
recent <checkpoint> record.
2. Continue scanning backwards till a record <T start> is
found.
3. Need only consider the part of log following this start
record. Earlier part of log can be ignored during
recovery, and can be erased whenever desired.
4. For all transactions T’ (starting from T or later) with no
<T’ commit>, execute undo(T’). (Done only in case
of immediate modification.)
5. Scanning forward in the log, for all transactions T’
(starting from T or later) with a <T’ commit>, execute
redo(T’).
Example of Checkpoints
Tc Tf
T1
T2
T3
T4

checkpoint system failure

 T1 can be ignored (updates already output to disk due to


checkpoint)
 Redo T2 and T3.
 Undo T4.
Shadow paging
 It is an alternative to log-based recovery;
 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.
 Shadow page table is never modified during
execution
Example of a page table
 Initially, both the page tables are identical. Only
current page table is used for data item accesses
during execution of the transaction.
 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.
 The update is performed on the copy.
Example of Shadow Paging
Shadow and current page tables after write to page 4
 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.
 Once pointer to shadow page table has been written, transaction is
committed.
 Advantages of shadow-paging over log-based
schemes:
 No overhead of writing log records.
 Recovery is faster.
 Disadvantages:
 Commit overhead
 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.
Interaction with concurrency control
 Transaction rollback
 Checkpoints
Granularity
 Portion of the data which is locked
 Field
 Record
 Table

Das könnte Ihnen auch gefallen