Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 ICS 214A: Database Management Systems Fall 2002 Lecture 16: Crash Recovery Professor Chen Li.

Similar presentations


Presentation on theme: "1 ICS 214A: Database Management Systems Fall 2002 Lecture 16: Crash Recovery Professor Chen Li."— Presentation transcript:

1 1 ICS 214A: Database Management Systems Fall 2002 Lecture 16: Crash Recovery Professor Chen Li

2 ICS214ANotes 162 Integrity or correctness of data We would like data to be “accurate” or “correct” at all times EMP Name Tom Jack Mary Age 52 3421 1

3 ICS214ANotes 163 Integrity or consistency constraints Predicates data must satisfy Examples: - x is key of relation R - x  y holds in R - Domain(x) = {Red, Blue, Green}  is valid index for attribute x of R -no employee should make more than twice the average salary SQL uses “constraints” or “triggers” to enforce integrity –“CHECK age > 16 AND age <= 99”

4 ICS214ANotes 164 Definitions Consistent state: satisfies all constraints Consistent DB: DB in consistent state Ideally: database should reflect real world DB Reality

5 ICS214ANotes 165 A1 A2.... 500........ 600.... 500.... 600.... Example: Salary A1 = Salary A2 (constraint) Deposit $100 in A1 : A1  A1 + 100 Deposit $100 in A2 : A2  A2 + 100 DB may not be always consistent!

6 ICS214ANotes 166 Transaction: collection of actions that preserve consistency Consistent DBConsistent DB’ T

7 ICS214ANotes 167 Big assumption: If T starts with consistent state + T executes in isolation  T leaves consistent state Correctness (informally) If we stop running transactions, DB left consistent Each transaction sees a consistent DB

8 ICS214ANotes 168 How can constraints be violated? Transaction bug DBMS bug Hardware failure e.g., disk crash alters balance of account Data sharing e.g.: T1: give 10% raise to programmers T2: change programmers  systems analysts

9 ICS214ANotes 169 How can we prevent/fix violations? Chapter 8 (17): due to system failures only Chapter 9 (18): due to data sharing only Chapter 10 (19): due to failures and sharing Customers don’t want to lose their data! –Think about 9/11! Here we focus on “system failures”

10 ICS214ANotes 1610 Coping with system failures Question 1: What is our Failure Model? Events Desired Undesired Expected Unexpected CPU Memory Disk

11 ICS214ANotes 1611 Desired events: see product manuals… Undesired expected events: System crash - memory lost - cpu halts, resets

12 ICS214ANotes 1612 Examples: Disk data is lost Memory lost without CPU halt CPU dies… Undesired Unexpected: Everything else!

13 ICS214ANotes 1613 Is this model reasonable? Approach: Add low level checks + redundancy E.g., Replicate disk storage (stable store) Memory parity CPU checks

14 ICS214ANotes 1614 Question 2 Storage hierarchy Memory Disk x x

15 ICS214ANotes 1615 Operations: Input (x): block with x  memory Output (x): block with x  disk Read (x,t): do input(x) if necessary t  value of x in block Write (x,t): do input(x) if necessary value of x in block  t Memory Disk x x t

16 ICS214ANotes 1616 Key problem: Unfinished transaction ExampleConstraint: A=B T 1 : A  A  2 B  B  2

17 ICS214ANotes 1617 T 1 :Read (A,t); t  t  2 Write (A,t); Read (B,t); t  t  2 Write (B,t); Output (A); Output (B); A: 8 B: 8 A: 8 B: 8 memory disk 16 failure!

18 ICS214ANotes 1618 Need atomicity: execute all actions ofa transaction or none at all

19 ICS214ANotes 1619 A log is a sequence of log records Each record tells something about what some transaction has done Possible log records: – –Update record: Managed by the “Log Manager” Log

20 ICS214ANotes 1620 Topics Undo logging Redo logging Undo/redo logging, why both? Checkpoints …

21 ICS214ANotes 1621 T 1 :Read (A,t); t  t  2 A=B Write (A,t); Read (B,t); t  t  2 Write (B,t); Output (A); Output (B); A:8 B:8 A:8 B:8 memory disk log Undo logging (Immediate modification) 16 16 16

22 ICS214ANotes 1622 Undo logging rules 1.For every action, generate undo log record (containing old value). 2.Before x is modified on disk, log records pertaining to x must be on disk (write ahead logging: WAL). 3.Only after all writes of transaction must be reflected on disk can the “commit” record is flushed to log.

23 ICS214ANotes 1623 Log in memory Log is first written in memory Not written to disk on every action memory DB Log A: 8 16 B: 8 16 Log: A: 8 B: 8 16 BAD STATE # 1

24 ICS214ANotes 1624 Log in memory Log is first written in memory Not written to disk on every action memory DB Log A: 8 16 B: 8 16 Log: A: 8 B: 8 16 BAD STATE # 2...

25 ICS214ANotes 1625 Solution The transaction needs to call “FLUSH LOG” explicitly to force the log in memory to be flushed to disk Notice: even without calling “flush log”, some log records may have already been on disk! T 1 : Read (A,t); t  t  2 Write (A,t); Read (B,t); t  t  2 Write (B,t); FLUSH LOG; (A and B log records flushed to disk) Output (A); Output (B); FLUSH LOG; (“COMMIT” record flushed to disk)

26 ICS214ANotes 1626 Recovery rules: Undo logging For every Ti with in log: - If or in log, do nothing - Else For all in log: write (X, v) output (X ) Write to log IS THIS CORRECT?? NO!

27 ICS214ANotes 1627 Recovery rules: Undo logging (1) Let S = set of transactions with in log, but no (or ) record in log (2) For each in log, in reverse order (latest  earliest) do: - if Ti  S then - write (X, v) - output (X) (3) For each Ti  S do - write to log

28 ICS214ANotes 1628 T 1 :Read (A,t); t  t  2 A=B Write (A,t); Read (B,t); t  t  2 Write (B,t); Flush log; Output (A); Output (B); Flush Log; A:8 B:8 disk log Example 1 failure! Recovery: write

29 ICS214ANotes 1629 T 1 :Read (A,t); t  t  2 A=B Write (A,t); Read (B,t); t  t  2 Write (B,t); Flush log; Output (A); Output (B); Flush Log; A:16 B:8 disk log Example 2 failure!

30 ICS214ANotes 1630 T 1 :Read (A,t); t  t  2 A=B Write (A,t); Read (B,t); t  t  2 Write (B,t); Flush log; Output (A); Output (B); Flush Log; A:16 B:8 disk log Example 2 (cont) 8 8 failure!

31 ICS214ANotes 1631 T 1 :Read (A,t); t  t  2 A=B Write (A,t); Read (B,t); t  t  2 Write (B,t); Flush log; Output (A); Output (B); Flush Log; A:16 B:16 disk log Example 3 failure! Recovery same as Example 2

32 ICS214ANotes 1632 What if failure during undo recovery? No problem! Since UNDO is idempotent, i.e., applying “UNDO” many times yields the same result as applying “UNDO” once.

33 ICS214ANotes 1633 Redo logging (deferred modification) T 1: Read(A,t); t t  2; write (A,t); Read(B,t); t t  2; write (B,t); Output(A); Output(B) A: 8 B: 8 A: 8 B: 8 memory DB LOG 16 output 16

34 ICS214ANotes 1634 Redo logging rules 1.For every action, generate redo log record (containing new value) 2.Before X is modified on disk (DB), all log records for transaction that modified X (including commit) must be on disk

35 ICS214ANotes 1635 T 1 :Read (A,t); t  t  2 A=B Write (A,t); Read (B,t); t  t  2 Write (B,t); FLUSH LOG; Output (A); Output (B); A:8 B:8 disk log Flush log before modifying DB elements

36 ICS214ANotes 1636 T 1 :Read (A,t); t  t  2 A=B Write (A,t); Read (B,t); t  t  2 Write (B,t); FLUSH LOG; Output (A); Output (B); A:8 B:8 disk log failure! Transactions without “COMMIT” log record Remember: log records could be on disk before “flush log” is called.

37 ICS214ANotes 1637 For every Ti with in log: –For all in log: Write(X, v) Output(X) Recovery rules: Redo logging IS THIS CORRECT? NO!

38 ICS214ANotes 1638 (1) Let S = set of transactions with in log (2) For each in log, in forward order (earliest  latest) do: if Ti  S then Write(X, v), Output(X) (3) For each incomplete transaction Ti, write record to the log. Recovery rules: Redo logging

39 ICS214ANotes 1639 T 1 :Read (A,t); t  t  2 A=B Write (A,t); Read (B,t); t  t  2 Write (B,t); FLUSH LOG; Output (A); Output (B); A:8 B:8 disk log failure! Recovery: Example 1 Recovery: write an “abort” record

40 ICS214ANotes 1640 T 1 :Read (A,t); t  t  2 A=B Write (A,t); Read (B,t); t  t  2 Write (B,t); FLUSH LOG; Output (A); Output (B); A:8 B:8 disk log Recovery: Example 2 failure! 16


Download ppt "1 ICS 214A: Database Management Systems Fall 2002 Lecture 16: Crash Recovery Professor Chen Li."

Similar presentations


Ads by Google