Title: Lecture 11: Logging
1Lecture 11 Logging Recovery ARIES
Sept. 29, 2006 ChengXiang Zhai
Most slides are adapted from Kevin Changs
lecture slides
2The ACID Properties of Transactions
- Atomicity
- Either all actions are done, or none
- Consistency
- DB satisfies all the consistency constraints
- Transactions are expected to preserve consistency
- Isolation
- As if each transaction were executed alone
- Durability
- Once a transaction is completed, its effect must
persist
Concurrency/Lock Manager consistency isolation
Recovery/Log Manager atomicity durability
3Motivation
- How to ensure atomicity and durability?
- transactions may abort (need to rollback)
- what if DBMS stops running?
- Desired status after system restarts
- T1, T2 T3 should be durable.
- T4 T5 should be aborted (effects not seen).
crash!
T1 T2 T3 T4 T5
4Assumptions
- Concurrency control is in effect
- strict 2PL, in particular.
- request s/x locks before read/write
- all the locks held until EOT (strict locking)
- Updates are happening in place (no shadow
pages) - Data is overwritten on (or deleted from) the disk
- A simple scheme to guarantee atomicity
durability?
5Handling the Buffer Pool
No Steal
Steal
- Force writing to disk at commit?
- poor response time
- but provides durability
- Steal buffer-pool frames from uncommitted
transactions? - if not, inefficient use of the buffer
- if so, how can we ensure atomicity?
- Recovery scheme vs. B.M.
- undo-only can steal? must force?
- redo-only no steal? no force?
Force
Trivial
Desired
No Force
6Basic Idea Logging
- Record redo and undo information in log
- sequential writes to log (put it on a separate
disk). - minimal info (diff) written to log, so multiple
updates fit in a single log page - log ordered list of redo/undo actions
- log record contains
- ltXID, pageID, offset, length, old data, new datagt
- and additional control info (which well see soon)
7Write-Ahead Logging (WAL)
- Write-Ahead Logging Protocol
- must force the log record for an update before
the corresponding data page gets to disk. - must force all log records for a xact before
commit. - 1 guarantees atomicity (undo)
- 2 guarantees durability (redo)
- Exactly how is logging (and recovery!) done?
- well study the ARIES algorithms
8ARIES Main Principles
- WAL
- Repeating history during REDO
- Logging changes during UNDO
- Enables
- simplicity and flexibility
- finer granularity locking (than a page)
- updates to (different parts of) same page are
streamed in redo/undo - redoing and undoing not necessarily exact
physical inverse
9WAL the Log
- Each log record has unique Log Sequence Number
- LSNs always increasing
- Each data page contains a pageLSN
- LSN of the most recent log record of latest
update - System keeps track of flushedLSN
- the max LSN flushed so far
- WAL before writing a page,
- pageLSN flushedLSN
- pageLSN in flushed already
Log records flushed to disk
Log tail in RAM
10Log Records
- Possible log record types
- Update
- Commit
- Abort
- End
- end of commit or abort
- Compensation Log Records (CLRs)
- for UNDO actions
LogRecord fields
update records only
11Other Log-Related State
- Transaction table
- one entry per active Xact
- contains XID, status (running/committed/aborted),
and lastLSN - Dirty page table
- one entry per dirty page in buffer pool
- contains recLSN -- the LSN of the log record
which first caused the page to be dirty
12Normal Execution of an Xact
- Series of reads writes, followed by commit or
abort - Strict 2PL
- STEAL, NO-FORCE buffer management, with
write-ahead logging
13Checkpointing
- Periodical checkpoint
- minimize the (analysis) time to recover from
system crash - Write to log
- begin_checkpoint record indicates when chkpt
began - end_checkpoint record contains current xact
table and dirty page table. Fuzzy checkpoint - other xacts continue to run these tables
accurate only as of the time of the
begin_checkpoint record - no attempt to force dirty pages to disk
- effectiveness limited by earliest recLSN in dirty
page table - oldest unwritten change to a dirty page
- so a good idea to periodically flush dirty pages
to disk! - Store LSN of chkpt record in master record
14Big Picture Whats Stored Where
LOG
RAM
DB
LogRecords
Xact Table lastLSN (last log) status Dirty
Page Table recLSN (first log) flushedLSN
Data pages each with a pageLSN
master record
15Simple Transaction Abort
- For now, consider an explicit abort of a xact
- e.g., validation error, deadlock no crash
involved - Play back the log in reverse order,
- UNDOing updates
- get lastLSN of xact from xact table
- can follow chain of log records backward via the
prevLSN field - can we do so in crash-recovery undoing?
- before starting undo, write an abort log record.
- for recovering from crash during undo
16Abort, cont.
- To perform UNDO, must have a lock on data
- no problem (strict locking)
- Before restoring old value of a page, write a
CLR - you continue logging while you undo
- CLR has one extra field undoNextLSN
- points to the next LSN to undo (i.e. the prevLSN
of the record were currently undoing) - CLRs never undone (but might be redone when
repeating history after another crash) - At end of UNDO, write an End log record.
- 120 CLR
- undo 101
- undonextLSN98
- (T1 lastLSN120)
T1 abort T1 lastLSN101
101
98
17Transaction Commit
- Write commit record to log
- All log records up to xacts lastLSN are flushed.
- guarantees that flushedLSN ³ lastLSN
- Commit() returns (after synchronous IO)
- Write End record to log
18Crash Recovery Big Picture
Oldest log rec. of Xact active at crash
- Start from a checkpoint (found via master record)
- Three phases. Need to
- figure out which xacts committed since
checkpoint, which failed (Analysis). - REDO all actions.
- repeat history
- UNDO effects of failed xacts.
Smallest recLSN in dirty page table after Analysis
Last chkpt
CRASH
A
R
U
19Crash Recovery vs. Transaction Abort?
- What are the differences?
20Crash Recovery vs. Transaction Abort?
- Abort
- (state in memory, then) undo one xact
- Recovery
- reconstruct state, then undo all uncommitted xact
- reconstruction analysis redo
- undo must consider global ordering of undos
21Recovery Analysis Phase
- Goal reconstruct two state tables
- xact-table what xacts to abort (undo)?
- dirty-page table where to start redo?
- (init) Restore state at checkpoint
- via end_checkpoint record
- (delta after ckpt) Scan log forward from ckpt
- End record remove xact from xact table
- Other records
- add Xact to Xact table, set lastLSNLSN
- change xact status if commit seen
- Update record only If P not in Dirty Page Table,
- add P to DPT, set its recLSNLSN
22Recovery REDO Phase
- Repeat History to reconstruct state at crash
- reapply all updates (even of aborted xacts!) and
redo CLRs - (CLRs are now simply dirty-data before last
crash) - Scan forward from earliest recLSN in DPT
- Redo each CLR or update log rec LSN, unless
- affected page is not in the Dirty Page Table, or
- affected page is in DPT, but has recLSN gt LSN
- why can this happen? page out and in after this
LSN - pageLSN (in DB) ³ LSN
- why this is done last? (in fact, this also checks
the above two) - To REDO an action
- reapply logged action (not only work for
image-based!) - set pageLSN to LSN. No additional logging!
23Recovery UNDO Phase
T1
T3
T2
- ToUndo lastLSN of all loser xacts
- Repeat
- choose largest LSN among ToUndo
- if this LSN is a CLR and undoNextLSNNULL
- write an End record for this xact
- If this LSN is a CLR, and undoNextLSN ! NULL
- add undoNextLSN to ToUndo
- (what happens to other CLRs of this xact?)
- only last CLR seen on this chain others not on
chain - Else this LSN is an update. Undo the update,
write a CLR, add prevLSN to ToUndo. - Until ToUndo is empty
1
2
3
4
5
24Example of Recovery
LSN LOG
begin_checkpoint end_checkpoint update T1
writes P5 update T2 writes P3 T1 abort CLR Undo
T1 LSN 10 T1 End update T3 writes P1 update T2
writes P5 CRASH, RESTART
00 05 10 20 30 40
45 50 60
prevLSNs
Xact Table lastLSN status DPT recLSN flushedL
SN
ToUndo
25Example Crash During Restart!
LSN LOG
begin_checkpoint, end_checkpoint update T1
writes P5 update T2 writes P3 T1 abort CLR Undo
T1 LSN 10, T1 End update T3 writes P1 update T2
writes P5 CRASH, RESTART CLR Undo T2 LSN 60 CLR
Undo T3 LSN 50, T3 end CRASH, RESTART CLR Undo
T2 LSN 20, T2 end
00,05 10 20 30 40,45 50
60 70 80,85 90
undoNextLSN
Xact Table lastLSN status DPT recLSN flushedL
SN
ToUndo
26Summary of Logging/Recovery
- Recovery Manager guarantees atomicity
durability. - Use WAL to allow STEAL/NO-FORCE w/o sacrificing
correctness. - LSNs identify log records linked into backwards
chains per transaction (via prevLSN). - pageLSN allows comparison of data page and log
records (so redo becomes simple)
27Summary, Cont.
- Checkpointing A quick way to limit the amount
of log to scan on recovery - Recovery works in 3 phases
- Analysis Forward from checkpoint.
- Redo Forward from oldest recLSN.
- Undo Backward from end to first LSN of oldest
Xact alive at crash. - Upon Undo, write CLRs.
- Redo repeats history simplifies the logic!
28What You Should Know
- Basic idea of ARIES
- What are the three phases of recovery?
- How does each phase work?
- How do the data structures support the 3 phases?
29Carry Away Messages
- Opening a problem vs. closing a problem
- Both are milestones
- Look for long-standing important problems and try
to close it (aiming at the best solution) - Go for simplicity
- Simple uniform processing (no/few special
rules) - Simple invariant properties
- Simple press the critical button
- Ensure the considered solution space to be
complete - Dont overlook unusual solutions