28
Undo Logging Rules Undo 1: If transaction T modifies the database element X that held value old Write hT,X, oldi to the log Only when the log record appears on disk can we write the new value for X to disk. Undo 2: If transaction T commits, then Write all pages with modified database elements to disk Then, write hCOMMIT T i to the log and disk, as soon as possible. Solution of the exercises 1

Undo Logging Rules - Université libre de Bruxellescs.ulb.ac.be/public/.../teaching/infoh417/05_-_logging-sol-slides.pdf · Undo 1: If transaction T modi es the database element X

  • Upload
    buikien

  • View
    217

  • Download
    2

Embed Size (px)

Citation preview

Undo Logging Rules

Undo 1:

If transaction T modifies the database element X that held value old

•Write 〈T,X,old〉 to the log

• Only when the log record appears on disk can we write the new value for Xto disk.

Undo 2:

If transaction T commits, then

•Write all pages with modified database elements to disk

• Then, write 〈COMMITT 〉 to the log and disk, as soon as possible.

Solution of the exercises 1

Redo Logging Rules

Redo 1:

If transaction T modifies the database element X setting its value to new

•Write 〈T,X,new〉 to the log

Redo 2:

If transaction T commits, then

•Write 〈COMMITT 〉 to the log, and flush the log to the disk.

• Only then, write the new value for X to disk.

Hence, all log entries must be written to disk, before modifying any databaseelement on disk.

Solution of the exercises 2

Undo/Redo Logging Rules

Undo/Redo 1:

If transaction T modifies database element X that held the value old to thevalue new

•Write 〈T,X,old,new〉 to the log

• Log records must be flushed to disk before corresponding modified pages arewritten to disk.

•When the transaction commits, write 〈COMMITT 〉 to the log and flush thelog.

•Modified database pages can be flushed before or after commit.

Solution of the exercises 3

Database System Recovery

Task:

Consider the following log:

〈STARTT 〉〈T,A, 10, 11〉〈T,B, 20, 21〉〈COMMITT 〉

Tell all sequences of events that are legal for an Undo-, Redo-, andUndo/Redo-based recovery system, where the events are:

Output(A),Output(B), Flush-Log(A), Flush-Log(B),Commit

Note that for convenience of presentation, we only use Undo/Redo log events inthese slides!

Solution of the exercises 4

Database System Recovery

Solution (Undo)

The constraints are:

• Flush-Log(A) < Output(A)

• Flush-Log(B) < Output(B)

• Flush-Log(A) < Flush-Log(B) < Commit

• Output(A) < Commit

• Output(B) < Commit

Hence, the valid sequences are:

• Flush-Log(A),Output(A), Flush-Log(B),Output(B),Commit

• Flush-Log(A), Flush-Log(B),Output(A),Output(B),Commit

• Flush-Log(A), Flush-Log(B),Output(B),Output(A),Commit

Solution of the exercises 5

Database System Recovery

Solution (Redo)

The constraints are:

• Flush-Log(A) < Output(A)

• Flush-Log(B) < Output(B)

• Flush-Log(A) < Flush-Log(B) < Commit

• Commit < Output(A)

• Commit < Output(B)

Hence, the valid sequences are:

• Flush-Log(A), Flush-Log(B),Commit,Output(A),Output(B)

• Flush-Log(A), Flush-Log(B),Commit,Output(B),Output(A)

Solution of the exercises 6

Database System Recovery

Solution (Undo/Redo)

The constraints are:

• Flush-Log(A) < Output(A)

• Flush-Log(B) < Output(B)

• Flush-Log(A) < Flush-Log(B) < Commit

Hence, the valid sequences are:

• Flush-Log(A),Output(A), Flush-Log(B),Output(B),Commit

• Flush-Log(A), Flush-Log(B),Output(A),Output(B),Commit

• Flush-Log(A), Flush-Log(B),Output(B),Output(A),Commit

• Flush-Log(A), Flush-Log(B),Commit,Output(A),Output(B)

• Flush-Log(A), Flush-Log(B),Commit,Output(B),Output(A)

• Flush-Log(A),Output(A), Flush-Log(B),Commit,Output(B)

• Flush-Log(A), Flush-Log(B),Output(A),Commit,Output(B)

• Flush-Log(A), Flush-Log(B),Output(B),Commit,Output(A)

Solution of the exercises 7

Database System Recovery

Task:

Consider the following log, after a crash:

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉

•What values might/must have been changed?

• How does the recovery manager get the database back to a consistent state?

Discuss for Undo-, Redo-, and Undo/Redo-logging.

Solution of the exercises 8

Database System Recovery

Solution (Undo)

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉

We first identify the transactions that we need to undo. They are T , and U .

By reading the log we can conclude that:

• A might have had its value changed on disk.

Solution of the exercises 9

Database System Recovery

Solution (Undo)

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉

Starting from the end of the log, we undo as follows:

• Append 〈ABRTU〉 to the log.

•Write value 10 for A.

• Append 〈ABRTT 〉 to the log.

Solution of the exercises 10

Database System Recovery

Solution (Redo)

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉

We first identify the transactions that we need to redo. No transaction hascommitted, so we do not need to redo any transaction.

By reading the log we can conclude that:

• A cannot have had its value changed on disk.

Solution of the exercises 11

Database System Recovery

Solution (Redo)

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉

We do not need to redo any transaction.

• Append 〈ABRTU〉 to the log.

• Append 〈ABRTT 〉 to the log.

Solution of the exercises 12

Database System Recovery

Solution (Undo/Redo)

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉

We first identify the transactions that we need to undo or redo. No transactionhas committed, so we do not need to redo any transaction. We need to undotransactions T and U .

By reading the log we can conclude that:

• A might have had its value changed on disk.

We recover from the crash as with Undo.

Solution of the exercises 13

Database System Recovery

Solution (Undo/Redo)

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉

We do not need to redo any transaction.

Starting from the end of the log, we undo as follows:

• Append 〈ABRTU〉 to the log.

•Write value 10 for A.

• Append 〈ABRTT 〉 to the log.

Solution of the exercises 14

Database System Recovery

Task:

Consider the following log, after a crash:

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉〈U,B, 20, 21〉〈T,C, 30, 31〉〈U,D, 40, 41〉〈COMMITU〉

•What values might/must have been changed?

• How does the recovery manager get the database back to a consistent state?

Discuss for Undo-, Redo-, and Undo/Redo-logging.

Solution of the exercises 15

Database System Recovery

Solution (Undo)

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉〈U,B, 20, 21〉〈T,C, 30, 31〉〈U,D, 40, 41〉〈COMMITU〉

We first identify the transactions that we need to undo. Only transaction Tmust be undone.

By reading the log we can conclude that:

• A might have had its value changed on disk.

• B must have had its value changed on disk.

• C might have had its value changed on disk.

•D must have had its value changed on disk.

Solution of the exercises 16

Database System Recovery

Solution (Undo)

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉〈U,B, 20, 21〉〈T,C, 30, 31〉〈U,D, 40, 41〉〈COMMITU〉

Starting from the end of the log:

• Ignore changes of transaction U altogether.

•Write value 30 for C.

•Write value 10 for A.

• Append 〈ABRTT 〉 to the log.

Solution of the exercises 17

Database System Recovery

Solution (Redo)

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉〈U,B, 20, 21〉〈T,C, 30, 31〉〈U,D, 40, 41〉〈COMMITU〉

We first identify the transactions that we need to redo. Only transaction Umust be redone.

By reading the log we can conclude that:

• A cannot have had its value changed on disk.

• B might have had its value changed on disk.

• C cannot have had its value changed on disk.

•D might have had its value changed on disk.

Solution of the exercises 18

Database System Recovery

Solution (Redo)

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉〈U,B, 20, 21〉〈T,C, 30, 31〉〈U,D, 40, 41〉〈COMMITU〉

Starting from the beginning of the log:

• Ignore changes of transaction T altogether.

•Write value 21 for B.

•Write value 41 for D.

• Append 〈ABRTT 〉 to the log.

Solution of the exercises 19

Database System Recovery

Solution (Undo/Redo)

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉〈U,B, 20, 21〉〈T,C, 30, 31〉〈U,D, 40, 41〉〈COMMITU〉

We first identify the transactions that we need to redo and those that we needto undo. U must be redone, while T must be undone.

By reading the log we can conclude that:

• A might have had its value changed on disk.

• B might have had its value changed on disk.

• C might have had its value changed on disk.

•D might have had its value changed on disk.

Solution of the exercises 20

Database System Recovery

Solution (Undo/Redo)

〈STARTT 〉〈T,A, 10, 11〉〈STARTU〉〈U,B, 20, 21〉〈T,C, 30, 31〉〈U,D, 40, 41〉〈COMMITU〉

Starting from the end of the log (undo):

• Ignore changes of transaction U altogether.

•Write value 30 for C.

•Write value 10 for A.

• Append 〈ABRTT 〉 to the log.

Then, starting from the beginning of the log (redo):

• Ignore changes of transaction T altogether.

•Write value 21 for B.

•Write value 41 for D.

Solution of the exercises 21

Database System Recovery

Task:

Consider the following log, where a checkpoint start has been added:

〈STARTS〉〈S,A, 60〉〈COMMITS〉〈STARTT 〉〈T,A, 10〉〈CKPT START〉〈STARTU〉〈U,B, 20〉〈T,C, 30〉〈STARTV 〉〈U,D, 40〉〈V, F, 70〉〈COMMITU〉〈T,E, 50〉〈COMMITT 〉〈V,B, 80〉〈COMMITV 〉

•When is 〈CKPT END〉 written?

•What happens if a crash occurs? (for each possible point at which a crashcan occur)

Discuss for Undo-, Redo-, and Undo/Redo-logging.

Solution of the exercises 22

Database System Recovery

Solution (Undo)

〈STARTS〉〈S,A, 60〉〈COMMITS〉〈STARTT 〉〈T,A, 10〉〈CKPT START〉〈STARTU〉〈U,B, 20〉〈T,C, 30〉〈STARTV 〉〈U,D, 40〉〈V, F, 70〉〈COMMITU〉〈T,E, 50〉〈COMMITT 〉〈V,B, 80〉〈COMMITV 〉

• The checkpoint entry identifies the transactions that are currently active.Hence, the checkpoint entry is 〈CKPT START (T )〉.• Every one of these active transactions must commit before writing〈CKPT END〉. We can write 〈CKPT END〉 right after 〈COMMITT 〉.

Solution of the exercises 23

Database System Recovery

Solution (Undo)

〈STARTS〉〈S,A, 60〉〈COMMITS〉〈STARTT 〉〈T,A, 10〉〈CKPT START〉〈STARTU〉〈U,B, 20〉〈T,C, 30〉〈STARTV 〉〈U,D, 40〉〈V, F, 70〉〈COMMITU〉〈T,E, 50〉〈COMMITT 〉〈V,B, 80〉〈COMMITV 〉

The recovery depends on whether we first meet 〈CKPT END〉 or〈CKPT START〉:

• if 〈CKPT END〉 was written last, we only need to consider the log up to〈CKPT START (T )〉.• if 〈CKPT START (T )〉 was written last, we need to consider the log up to〈STARTT 〉, as it was the only active transaction.

Solution of the exercises 24

Database System Recovery

Solution (Redo)

〈STARTS〉〈S,A, 60〉〈COMMITS〉〈STARTT 〉〈T,A, 10〉〈CKPT START〉〈STARTU〉〈U,B, 20〉〈T,C, 30〉〈STARTV 〉〈U,D, 40〉〈V, F, 70〉〈COMMITU〉〈T,E, 50〉〈COMMITT 〉〈V,B, 80〉〈COMMITV 〉

• The checkpoint entry identifies the transactions that are currently active.Hence, the checkpoint entry is 〈CKPT START (T )〉.•We first write blocks from transaction that were commited at〈CKPT START〉 to the disk.

•We only write 〈CKPT END〉 after these blocks.

•We cannot predict when the dirty blocks will be written on disk:〈CKPT END〉 can occur anywhere after 〈CKPT START〉.

Solution of the exercises 25

Database System Recovery

Solution (Redo)

〈STARTS〉〈S,A, 60〉〈COMMITS〉〈STARTT 〉〈T,A, 10〉〈CKPT START〉〈STARTU〉〈U,B, 20〉〈T,C, 30〉〈STARTV 〉〈U,D, 40〉〈V, F, 70〉〈COMMITU〉〈T,E, 50〉〈COMMITT 〉〈V,B, 80〉〈COMMITV 〉

The recovery depends on whether we the last checkpoint entry was〈CKPT END〉 or 〈CKPT START (T )〉:

• if 〈CKPT END〉 was written last, we know that transaction S was fullywritten. The transactions that were active at 〈CKPT START (T )〉 or startedlater and that have committed must be redone (that is, T , U , and V ,depending on the place of the crash).

• if 〈CKPT START (T )〉 was written last, the checkpoint does not help. Weneed to go back to the previous 〈CKPT END〉, or to the beginning of the log.

Solution of the exercises 26

Database System Recovery

Solution (Undo/Redo)

〈STARTS〉〈S,A, 60〉〈COMMITS〉〈STARTT 〉〈T,A, 10〉〈CKPT START〉〈STARTU〉〈U,B, 20〉〈T,C, 30〉〈STARTV 〉〈U,D, 40〉〈V, F, 70〉〈COMMITU〉〈T,E, 50〉〈COMMITT 〉〈V,B, 80〉〈COMMITV 〉

• The checkpoint entry identifies the transactions that are currently active.Hence, the checkpoint entry is 〈CKPT START (T )〉.• All dirty blocks are written to disk first.

•We only write 〈CKPT END〉 after these blocks.

•We cannot predict when the dirty blocks will be written on disk:〈CKPT END〉 can occur anywhere after 〈CKPT START〉.

Solution of the exercises 27

Database System Recovery

Solution (Undo/Redo)

〈STARTS〉〈S,A, 60〉〈COMMITS〉〈STARTT 〉〈T,A, 10〉〈CKPT START〉〈STARTU〉〈U,B, 20〉〈T,C, 30〉〈STARTV 〉〈U,D, 40〉〈V, F, 70〉〈COMMITU〉〈T,E, 50〉〈COMMITT 〉〈V,B, 80〉〈COMMITV 〉

The recovery depends on whether we the last checkpoint entry was〈CKPT END〉 or 〈CKPT START (T )〉:

• if 〈CKPT END〉 was written last, we know that all the dirty buffers werewritten to disk. We only need to redo transactions from 〈CKPT START (T )〉,but we also need to undo transactions that were active at〈CKPT START (T )〉. Hence, we may need to go back to 〈STARTT 〉 whenundoing.

• if 〈CKPT START (T )〉 was written last, the checkpoint does not help. Weneed to go back to the previous 〈CKPT END〉, or to the beginning of the log.

Solution of the exercises 28