Logging and Recovery CC Lecture 2 Introduction to

  • Slides: 26
Download presentation
Logging and Recovery CC Lecture 2 Introduction to Database Systems 1

Logging and Recovery CC Lecture 2 Introduction to Database Systems 1

Review: The ACID properties v Atomicity: all actions in the Xact happen, or none

Review: The ACID properties v Atomicity: all actions in the Xact happen, or none happen v Consistency: if each Xact is consistent, and the DB starts consistent, it ends up consistent v Isolation: execution of one Xact is isolated from that of other Xacts v Durability: if a Xact commits, its effects persist v The Recovery Manager guarantees Atomicity & Durability Introduction to Database Systems 2

Motivation v Atomicity: – Transactions may abort (“Rollback”) v Durability: – What if DBMS

Motivation v Atomicity: – Transactions may abort (“Rollback”) v Durability: – What if DBMS stops running (causes? ) v Desired behavior after system restarts: – T 1, T 2 & T 3 should be durable – T 4 & T 5 should be aborted (effects not seen). Introduction to Database Systems T 1 T 2 T 3 T 4 T 5 crash! 3

Assumptions v Concurrency control is in effect – Strict 2 PL, in particular v

Assumptions v Concurrency control is in effect – Strict 2 PL, in particular v Updates are happening “in place” – i. e. data is overwritten on (deleted from) the disk. v A simple scheme to guarantee Atomicity & Durability? Introduction to Database Systems 4

Handling the Buffer Pool v Force writes to disk? – poor response time –

Handling the Buffer Pool v Force writes to disk? – poor response time – but provides durability v Steal buffer-pool frames from uncommited Xacts? – if not, poor throughput – if so, how to provide atomicity? Introduction to Database Systems No Steal Force No Force Steal Trivial Desired 5

Examples v STEAL (why Atomicity is a problem) – steal frame F: some page

Examples v STEAL (why Atomicity is a problem) – steal frame F: some page P is written to disk – what if the Xact with the lock on P aborts? – must remember the old value of P at steal time! u v to support UNDOing the write to P NO FORCE (why Durability is a problem) – how to guarantee durability without writing? u Can’t be done! – So write as little as possible, in a convenient place, at commit time u to support REDOing actions Introduction to Database Systems 6

Basic Idea: Logging v Store REDO and UNDO information in a log – for

Basic Idea: Logging v Store REDO and UNDO information in a log – for every update, generate UNDO & REDO info – 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 v Log: An ordered list of REDO/UNDO actions – log record contains u <XID, page. ID, offset, len, old data, new data> – and additional control info (which we’ll see soon) Introduction to Database Systems 7

Write-Ahead Logging (WAL) v The Write-Ahead Logging Protocol: must force the log record for

Write-Ahead Logging (WAL) v The Write-Ahead Logging Protocol: must force the log record for an update before the corresponding data page gets to disk ‚ must write all log records for a Xact before commit. #1 guarantees Atomicity v #2 guarantees Durability v v Exactly how is logging (and recovery!) done? – We’ll study the ARIES algorithms Introduction to Database Systems 8

WAL & the Log v LSNs DB RAM page. LSNs flushed. LSN Each log

WAL & the Log v LSNs DB RAM page. LSNs flushed. LSN Each log record has a unique Log Sequence Number (LSN) – LSN’s always increasing v Each data page contains a page. LSN – the LSN of the most recent log record for an update to that page. v System keeps track of flushed. LSN – the max LSN flushed so far – log records in memory form the “tail” of the log v WAL sez: before a page is written, – page. LSN £ flushed. LSN Introduction to Database Systems 9

Log Records Log. Record update records only prev. LSN XID type page. ID length

Log Records Log. Record update records only prev. LSN XID type page. ID length offset before-image after-image Introduction to Database Systems Possible log record types: v Update v Commit v Abort v End (signifies end of commit or abort) v Compensation Log Records (CLRs) – for UNDO actions 10

Other Log-Related State v Transaction Table – one entry per active Xact – contains

Other Log-Related State v Transaction Table – one entry per active Xact – contains XID, status (running/commited/aborted), and last. LSN v Dirty Page Table – one entry per dirty page in buffer pool – contains rec. LSN -- the LSN of the log record which first caused the page to be dirty Introduction to Database Systems 11

The Big Picture DB Log. Records prev. LSN XID type page. ID length update

The Big Picture DB Log. Records prev. LSN XID type page. ID length update offset records before-image only after-image Introduction to Database Systems page. LSNs RAM Xact Table last. LSN status Dirty Page Table rec. LSN flushed. LSN 12

Normal Execution of an Xact Strict 2 PL v Series of reads & writes,

Normal Execution of an Xact Strict 2 PL v Series of reads & writes, followed by commit or abort v – assume that write is atomic on disk v STEAL, NO-FORCE buffer management, with Write-Ahead Logging Introduction to Database Systems 13

Simple Transaction Abort v For now, consider an explicit abort of a Xact –

Simple Transaction Abort v For now, consider an explicit abort of a Xact – no crash involved v We want to “play back” the log in reverse order, UNDOing updates – get last. LSN of Xact from Xact table – can follow chain of log records backward via the prev. LSN field – Before starting UNDO, write an Abort log record u for recovering from crash during UNDO! Introduction to Database Systems 14

Abort, cont. v To perform UNDO, must have a lock on data! – No

Abort, cont. v To perform UNDO, must have a lock on data! – No problem! v Before restoring old value of a page, write a CLR to the log – you continue logging while you UNDO!! – CLR has one extra field: undonext. LSN – points to the next LSN to undo (i. e. the prev. LSN of the record we’re currently undoing) v At end of UNDO, write an “end” log record Introduction to Database Systems 15

Transaction Commit Write commit record to log. v All log records up to last.

Transaction Commit Write commit record to log. v All log records up to last. LSN are flushed v – guarantees that flushed. LSN ³ last. LSN – note that log flushes are sequential, synchronous writes – many log records per log page Commit() returns v write end record to log v Introduction to Database Systems 16

Checkpoints Periodically, want to get a “snapshot” of the DBMS -- speeds up recovery!

Checkpoints Periodically, want to get a “snapshot” of the DBMS -- speeds up recovery! v new log records: begin_checkpoint, end_checkpoint. v – write a begin_checkpoint record as a new Xact – end_checkpoint record contains the current state of the Xact and Dirty Page tables – after end_checkpoint is flushed, the LSN of the begin_checkpoint record is stored in a special master record v Note: this is a “fuzzy checkpoint”! – no locking involved. good as of begin_checkpt. Introduction to Database Systems 17

Recovering from a Crash Start from a checkpoint (found via master record) v Three

Recovering from a Crash Start from a checkpoint (found via master record) v Three phases. Need to: v – figure out which Xacts committed since checkpoint, which failed (Analysis) – REDO all actions (repeat history) – UNDO effects of failed Xacts Oldest log rec. of Xact active at crash Smallest rec. LSN in dirty page table after Analysis Last chkpt CRASH Introduction to Database Systems A R U 18

Recovery: The Analysis Phase v reconstruct state at checkpoint – via end_checkpoint record v

Recovery: The Analysis Phase v reconstruct state at checkpoint – via end_checkpoint record v scan log forward from chkpt. – End record: remove Xact from Xact table – Other records: add Xact to Xact table, set last. LSN=LSN, change Xact status on commit – Update record: if P not in D. P. T. u add P to dirty page table, set rec. LSN=LSN Introduction to Database Systems 19

Recovery: The REDO Phase v Repeat History to reconstruct state at crash: – reapply

Recovery: The REDO Phase v Repeat History to reconstruct state at crash: – reapply all updates (even of aborted Xacts!) – redo any actions in CLRs v Start with smallest rec. LSN in D. P. T. Redo each action unless: – affected page is not in the Dirty Page Table – affected page is in DPT, but has rec. LSN > LSN – page. LSN (in DB) ³ LSN v To REDO an action: – reapply logged action – set page. LSN to LSN. No additional logging! Introduction to Database Systems 20

Recovery: The UNDO Phase To. Undo={ l | l a last. LSN of a

Recovery: The UNDO Phase To. Undo={ l | l a last. LSN of a “loser” Xact} v Repeat: v – choose largest LSN among To. Undo – if this LSN is a CLR and undonext. LSN==NULL u write an End record for this Xact – if this LSN is a CLR, and undonext. LSN != NULL u Add undonext. LSN to To. Undo u (Q: what happens to other CLRs? ) – else this LSN is an update. Undo the update, write a CLR, add prev. LSN to To. Undo. Until To. Undo is empty. Introduction to Database Systems 21

Example of Recovery LSN RAM Xact Table last. LSN status Dirty Page Table rec.

Example of Recovery LSN RAM Xact Table last. LSN status Dirty Page Table rec. LSN flushed. LSN To. Undo LOG 00 begin_checkpoint 05 end_checkpoint 10 update: T 1 writes P 5 20 update T 2 writes P 3 30 T 1 abort 40 CLR: Undo T 1 LSN 10 45 T 1 End 50 update: T 3 writes P 1 60 update: T 2 writes P 5 prev. LSNs CRASH, RESTART Introduction to Database Systems 22

Example: Crash During Restart! LSN 00, 05 RAM Xact Table last. LSN status Dirty

Example: Crash During Restart! LSN 00, 05 RAM Xact Table last. LSN status Dirty Page Table rec. LSN flushed. LSN To. Undo LOG begin_checkpoint, end_checkpoint 10 update: T 1 writes P 5 20 update T 2 writes P 3 30 T 1 abort 40, 45 undonext. LSN CLR: Undo T 1 LSN 10, T 1 End 50 update: T 3 writes P 1 60 update: T 2 writes P 5 CRASH, RESTART 70 80, 85 CLR: Undo T 2 LSN 60 CLR: Undo T 3 LSN 50, T 3 end CRASH, RESTART Introduction to Database Systems 90 CLR: Undo T 2 LSN 20, T 2 end 23

Additional Crash Issues What happens if system crashed during Analysis? During REDO? v How

Additional Crash Issues What happens if system crashed during Analysis? During REDO? v How do you limit the amount of work in REDO? v – flush asynchronously in the background – watch “hot spots”! v How do you limit the amount of work in UNDO? – avoid long-running Xacts Introduction to Database Systems 24

Summary of Logging/Recovery Manager guarantees Atomicity & Durability v Use WAL to allow STEAL/NO-FORCE

Summary of Logging/Recovery Manager guarantees Atomicity & Durability v Use WAL to allow STEAL/NO-FORCE w/o sacrificing correctness v LSNs identify log records; linked into backwards chains per transaction (via prev. LSN) v page. LSN allows comparison of data page and log records v Introduction to Database Systems 25

Summary, Cont. Checkpointing: a quick way to limit the amount of log to scan

Summary, Cont. Checkpointing: a quick way to limit the amount of log to scan on recovery v Recovery works in 3 phases v – Analysis since checkpoint – Redo since oldest rec. LSN – Undo from end to first LSN of oldest Xact alive at crash Upon Undo, write CLRs v Redo “repeats history”: simplifies the logic! v Introduction to Database Systems 26