Chapter 17: Recovery System
Recovery System
Basic Concepts
Log-Based Recovery
Checkpointing
Recovery Algorithm
Failure with Loss of Nonvolatile Storage
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
1
Recovery System
Goals:
Ensure that atomicity and durability properties of transactions are preserved.
Restore the database to the consistent state that existed before the failure.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Failure Classification
Transaction failure :
Logical errors: transaction cannot complete due to some internal error conditions
Example: bad input, data not found, overflow, resource limit exceeded
System errors: the database system must terminate an active transaction due to an error condition
Example: deadlock
System crash: a power failure or other hardware or software failure causes the system to crash and the loss of the content of volatile storage.
Fail-stop assumption: non-volatile storage contents are assumed not to be corrupted by system crash
Well-designed systems have numerous internal checks that bring the system to a halt when there is an error.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
3
Failure Classification (cont.)
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
Copies of the data on other disks are used to recover from failure.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
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 (battery backed up) 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
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
5
Recovery Algorithms
Consider transaction Ti that transfers $50 from account A to account B
Two updates: subtract 50 from A and add 50 to B
Transaction Ti requires updates to A and B to be output to the database.
A failure may occur after one of these modifications have been made but before both of them are made
Modifying the database without ensuring that the transaction will commit may leave the database in an inconsistent state
Not modifying the database may result in lost updates if failure occurs just after transaction commits
Recovery algorithms have two parts
Actions taken during normal transaction processing to ensure enough information exists to recover from failures
Actions taken after a failure to recover the database contents to a state that ensures atomicity, consistency and durability
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
6
Data Access
A database system
resides permanently on nonvolatile storage such as disks, and
parts of the database are in memory at any time.
Physical blocks are those blocks residing on the disk.
Buffer blocks are the blocks residing temporarily in main memory in an area called disk buffer.
Block movements between disk and main memory are initiated through the following two operations:
input(B) transfers the physical block B to main memory.
output(B) transfers the buffer block B to the disk, and replaces the appropriate physical block there.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
7
Example of Data Access
X
Y
A
B
x1
y1
Disk buffer
buffer block A
buffer block B
input(A)
output(B)
read(X)
write(Y)
disk
work area
of T1
work area
of T2
memory
z2
Z
physical
block A
physical
block B
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
8
Data Access (Cont.)
Each transaction Ti has its private work-area in which local copies of all data items accessed and updated by it are kept.
Ti’s local copy of a data item X is called xi.
Transferring data items between system buffer blocks and its private work-area is done by:
read(X) assigns the value of data item X to the local variable xi; need to issue input(BX) if BX in not in main memory (BX: block containing X).
write(X) assigns the value of local variable xi to data item X in the buffer block.
Note: output(BX) need not immediately follow write(X). System can perform the output operation when it deems fit.
Transactions
must perform read(X) before accessing X for the first time (subsequent reads can be from local copy)
write(X) can be executed at any time before the transaction commits
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
9
Recovery and Atomicity
Reminder: Atomicity: Either all operations of the transaction are reflected properly in the database, or none are.
To ensure atomicity despite failures, we first output information describing the modifications (log) to stable storage before modifying the database itself.
Ensure that all modifications performed by committed transactions are reflected in the database.
Ensure that no modifications made by an aborted transaction persist in the database.
We study log-based recovery mechanisms in detail
We first present key concepts
And then present the actual recovery algorithm
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
10
Recovery System
Basic Concepts
Log-Based Recovery
Checkpointing
Recovery Algorithm
Failure with Loss of Nonvolatile Storage
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
11
Log-Based Recovery
A log is a sequence of log records, which 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
Before Ti executes write(X), an update log record
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 its last statement, the log record
When Ti has aborted, the log record
We assume that every log record is output directly to stable storage once created.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
12
Immediate Database Modification
To understand the role of log records in recovery, we need to consider the steps a transaction takes in modifying a data item.
The transaction performs some computations in its own private work area.
The transaction modifies the corresponding data block in the disk buffer.
The database system executes the output operation that writes the data block to disk.
A transaction modifies the database if it performs an update on a disk buffer or on the disk itself.
If a transaction is allowed to modify the database before it commits, we call this immediate-modification.
Update log record must be written before modifying the database.
Output of updated blocks to disk 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.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
13
Transaction Commit
A transaction is said to have committed when its commit log record is output to stable storage
all previous log records of the transaction must have been output already
Writes performed by a transaction may still be in the buffer when the transaction commits, and may be output later. So, a recovery algorithm must consider the following possibilities.
A transaction may have modified the database before it commits and, as a result of a subsequent failure, may need to abort.
A transaction may have committed although some of its database modifications exist only in the disk buffer and not on disk.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
14
Immediate Database Modification Example
Log Write Output
A = 950
B = 2050
C = 600
BB , BC
BA
Note: BX denotes block containing X.
Output BC before T1 commits
Output BA and BB after T0 commits
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
15
Concurrency Control and Recovery
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)
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.
Log records of different transactions may be interspersed in the log.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Undo and Redo Operations
Undo of a log record
Redo of a log record
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 V1 a special redo-only log record
when undo of a transaction is complete, a log record
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
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Undo and Redo on Recovering from Failure
When recovering after failure:
Transaction Ti needs to be undone if the log
contains the record
but does not contain either the record
Transaction Ti needs to be redone if the log
contains the records
and contains the record
Note that if transaction Ti was undone earlier and the
Such a redo redoes all the original actions including the steps that restored old values (the redo-only log records)
Known as repeating history
Seems wasteful, but simplifies recovery greatly
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
18
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, and log records
(b) redo (T0) and undo (T1): A and B are set to 950 and 2050 and C is restored to 700. Log records
(c) redo (T0) and redo (T1): A and B are set to 950 and 2050
respectively. Then C is set to 600.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
19
Recovery System
Basic Concepts
Log-Based Recovery
Checkpointing
Recovery Algorithm
Failure with Loss of Nonvolatile Storage
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
20
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
Output all log records currently residing in main memory onto stable storage.
Output all modified buffer blocks to the disk.
Write a log record < checkpoint L> onto stable storage where L is a list of all transactions active at the time of checkpoint.
All updates are stopped while doing checkpointing
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
21
Checkpoints (Cont.)
Transactions that committed or aborted before the checkpoint must have written their updates to the database either
prior to the checkpoint or
as part of the checkpoint itself.
So, there is no need to consider these transactions at recovery time.
During recovery
Scan backwards from end of log to find the most recent
Only transactions that are in L or started after the checkpoint need to be redone or undone
Some earlier part of the log may be needed for undo operations
Continue scanning backwards till a record
Parts of log prior to earliest
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
22
Example of Checkpoints
T1 can be ignored (updates already output to disk due to checkpoint)
T2 and T3 redone.
T4 undone
Tc
Tf
T1
T2
T3
T4
checkpoint
system failure
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
23
Recovery System
Basic Concepts
Log-Based Recovery
Checkpointing
Recovery Algorithm
Failure with Loss of Nonvolatile Storage
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
24
Recovery Algorithm
Logging (during normal operation):
Transaction rollback (during normal operation)
Let Ti be the transaction to be rolled back
Scan log backwards from the end and for each log record of Ti of the form
perform the undo by writing V1 to Xj,
write a log record
such redo-only log records are also called compensation log records
Once the record
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Recovery from failure: Two phases
Redo phase: replay updates of all transactions, whether they committed, aborted, or are incomplete (repeating history)
Undo phase: undo all incomplete transactions
Redo phase:
Find last
Scan forward from the
Whenever a record
Whenever a log record
Whenever a log record
Recovery Algorithm (Cont.)
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Recovery Algorithm (Cont.)
Undo phase:
Scan log backwards from end
Whenever a log record
perform undo by writing V1 to Xj.
write a log record
Whenever a log record
Write a log record
Remove Ti from undo-list
Stop when undo-list is empty
i.e.
After undo phase completes, normal transaction processing can resume.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Example of Recovery
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
Recovery System
Basic Concepts
Log-Based Recovery
Checkpointing
Recovery Algorithm
Failure with Loss of Nonvolatile Storage
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
29
Failure with Loss of Nonvolatile Storage
Reminder: 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
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
30
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 to bring the database to the most recent consistent state.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
31
More….
In addition to immediate modifications, there are deferred modifications: all the write operations of a transaction are deferred until the transaction has been committed.
In addition to the popular no-force policy: a transaction is allowed to commit even if its updated blocks have not yet been written back to disk, there is force policy: transactions would force-output all modified buffer blocks to disk when they commit.
In addition to the popular steal policy: the system is allowed to write blocks containing updates of uncommitted transactions to disk, there is no-steal policy: blocks modified by a transaction that is still active should not be written to disk.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
32
More….
Although it has been assumed that every log record is output directly to stable storage once created, log records may be buffered in main memory temporarily before output to stable storage, under additional requirements (because such log records are lost if the system crashes).
Although it has been assumed that all updates to the database be temporarily suspended while the checkpoint is in progress, updates may be permitted during checkpointing but we need to deal with incomplete checkpoints when the system crashes before checkpoint is done.
©Silberschatz, Korth and Sudarshan
‹#›
Database System Concepts
33
/docProps/thumbnail.jpeg