ENCh19

download ENCh19

of 35

Transcript of ENCh19

  • 7/25/2019 ENCh19

    1/35

  • 7/25/2019 ENCh19

    2/35

    Chapter 19

    Database RecoveryTechniques

    Copyright 2004 Pearson Education, Inc.

  • 7/25/2019 ENCh19

    3/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-3

    Chapter 19 Outline

    Databases Recovery# Purpose o$ %ata&ase Reco"ery

    2 'ypes o$ (ailure

    ) 'ransaction *og

    4 %ata +pdates %ata Caching

    - 'ransaction Roll&ac /+ndo and Roll(or1ard

    Checpointing

    3 Reco"ery schemes

    5RIES Reco"ery Scheme

    #0 Reco"ery in 6ultidata&ase System

  • 7/25/2019 ENCh19

    4/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-4

    Database Recovery

    1 Purpose of Database Recovery

    7 'o &ring the data&ase into the last consistent state,1hich e8isted prior to the $ailure.

    7 'o preser"e transaction properties /5tomicity,Consistency, Isolation and %ura&ility.

    E8ample9 I$ the system crashes &e$ore a $und trans$ertransaction completes its e8ecution, then either one or &othaccounts may ha"e incorrect "alue. 'hus, the data&asemust &e restored to the state &e$ore the transaction modi$iedany o$ the accounts.

  • 7/25/2019 ENCh19

    5/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-5

    Database Recovery

    2 Types of Failure

    'he data&ase may &ecome una"aila&le $or use due to

    7'ransaction $ailure9 'ransactions may $ail &ecause o$incorrect input, deadloc, incorrect synchronization.

    7 System $ailure9 System may $ail &ecause o$ addressing

    error, application error, operating system $ault, R56

    $ailure, etc.

    7 6edia $ailure9 %is head crash, po1er disruption, etc.

  • 7/25/2019 ENCh19

    6/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-6

    Database Recovery

    3 Trasactio !o"

    (or reco"ery $rom any type o$ $ailure data "alues prior tomodi$ication /:(I6 :e(ore Image and the ne1 "alue a$termodi$ication /5(I6 ; 5(ter Image are re

    ?

    6

    !

    > @ 200

    ? @ #00

    6 @ 200

    ! @ 400

    > @ #00

    ? @ ,0

    6 @ 200

    ! @ 400

  • 7/25/2019 ENCh19

    7/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-,

    Database Recovery

    4 Data p.ate

    7 '))e.iate p.ate9 5s soon as a data item ismodi$ied in cache, the dis copy is updated.

    7 Deferre. p.ate9 5ll modi$ied data items in thecache is 1ritten either a$ter a transaction ends itse8ecution or a$ter a $i8ed num&er o$ transactionsha"e completed their e8ecution.

    7 /ha.o0 up.ate9 'he modi$ied "ersion o$ a dataitem does not o"er1rite its dis copy &ut is 1ritten at

    a separate dis location.7 '-place up.ate9 'he dis "ersion o$ the data item is

    o"er1ritten &y the cache "ersion.

  • 7/25/2019 ENCh19

    8/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-

    Database Recovery

    5 Data Cachi"

    %ata items to &e modi$ied are $irst stored into data&ase

    cache &y the Cache 6anager /C6 and a$ter

    modi$ication they are $lushed /1ritten to the dis. 'he$lushing is controlled &y *o.ifie.and Pi-pi&its.

    Pi-pi9 Instructs the operating system not to $lush

    the data item.

    *o.ifie.9 Indicates the 5(I6 o$ the data item.

  • 7/25/2019 ENCh19

    9/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-9

    Database Recovery

    6 Trasactio Roll-bac$ .o a. Roll-For0ar. Re.o

    'o maintain atomicity, a transactionAs operations are re.oe

    or u.oe.

    .o9 Restore all :(I6s on to dis /Remo"e all 5(I6s.

    Re.o9 Restore all 5(I6s on to dis.

    %ata&ase reco"ery is achie"ed either &y per$orming only

    +ndos or only Redos or &y a com&ination o$ the t1o. 'hese

    operations are recorded in the log as they happen.

  • 7/25/2019 ENCh19

    10/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-1

    Database Recovery

    Roll-bac$

    =e sho1 the process o$ roll&ac 1ith the help o$ the $ollo1ing three transactions'#, and '2 and ').

    '# '2 ')

    readBitem /5 readBitem /: readBitem /C

    readBitem /% 1riteBitem /: 1riteBitem /:

    1riteBitem /% readBitem /% readBitem /51riteBitem /5 1riteBitem /5

  • 7/25/2019 ENCh19

    11/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-11

    Database Recovery

    Roll-bac$ (e e&ecutio of T1 T2 a. T3 as recor.e. i the lo"7+ # C D

    3 15 4 2

    8starttrasactio T3:

    8rea.ite) T3 C:

    ; 80riteite) T3 # 15 12: 12

    8starttrasactioT2:8rea.ite) T2 #:

    ;; 80riteite) T2 # 12 1: 1

    8starttrasactioT1:

    8rea.ite) T1 +:

    8rea.ite) T1 D:

    80riteite) T1 D 2 25: 25

    8rea.ite) T2 D:

    ;; 80riteite) T2 D 25 26: 26

    8rea.ite) T3 +:

    ---- syste) crash ----

    ; T3 is rolle. bac$ because it .i. ot reach its co))it poit7

    ;; T2 is rolle. bac$ because it rea.s the value of ite) # 0ritte by T37

  • 7/25/2019 ENCh19

    12/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-12

    Database RecoveryRoll-bac$ (e e&ecutio of T1 T2 a. T3 as recor.e. i the lo"7

    READ(C) READ(A)

    READ(D)

    WRITE(B)T3

    BEGINREAD(B) WRITE(D)WRITE(B)T2

    BEGIN WRITE(D)READ(A) READ(D)T1

    BEGIN

    syste !rashTime

    'llustrati" casca.i" roll-bac$

  • 7/25/2019 ENCh19

    13/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-13

    Database Recovery

  • 7/25/2019 ENCh19

    14/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-14

    Database Recovery

    , Chec$poiti"

    'ime to time /randomly or under some criteria the data&ase

    $lushes its &u$$er to data&ase dis to minimize the tas o$

    reco"ery. 'he $ollo1ing steps de$ines a checpoint operation9#. Suspend e8ecution o$ transactions temporarily.

    2. (orce 1rite modi$ied &u$$er data to dis.

    ). =rite a checpointD record to the log, sa"e the log to dis.

    4. Resume normal transaction e8ecution.

    %uring reco"ery re.o or u.o is re

  • 7/25/2019 ENCh19

    15/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-15

    Database Recovery

    /teal=%o-/teal a. Force=%o-Force

    Possi&le 1ays $or $lushing data&ase cache to data&ase dis9

    /teal9 Cache can &e $lushed &e$ore transaction commits.

    %o-/teal9 Cache cannot &e $lushed &e$ore transaction commit.

    Force9 Cache is immediately $lushed /$orced to dis.

    %o-Force9 Cache is de$erred until transaction commits.

    'hese gi"e rise to $our di$$erent 1ays $or handling reco"ery9Steal!o(orce /+ndoRedo, Steal(orce /+ndo!oredo,

    !oSteal!o(orce /Redo!oundo and !oSteal(orce /!o

    undo!oredo.

  • 7/25/2019 ENCh19

    16/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-16

    Database Recovery

    Deferre. p.ate %o .o=Re.o

    'he data update goes as $ollo1s9

    #. 5 set o$ transactions records their updates in the log.

    2. 5t commit point under =5* scheme these updates are

    sa"ed on data&ase dis.

    5$ter re&oot $rom a $ailure the log is used to redo all the

    transactions a$$ected &y this $ailure. !o undo is re

  • 7/25/2019 ENCh19

    17/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-1,

    Database Recovery

    #. 5 set o$ transactions records their updates in the log.

    2. 5t commit point under =5* scheme these updates are

    sa"ed on data&ase dis.

    5$ter re&oot $rom a $ailure the log is used to redo all the

    transactions a$$ected &y this $ailure. !o undo is re

  • 7/25/2019 ENCh19

    18/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-1

    Database Recovery

    Deferre. p.ate i a si"le-user syste)

    /a '# '2

    readBitem /5 readBitem /:

    readBitem /% 1riteBitem /:

    1riteBitem /% readBitem /%

    1riteBitem /5

    /&

    startBtransaction, '#D

    1riteBitem, '#, %, 20D

    commit '#D

    startBtransaction, '#D

    1riteBitem, '2, :, #0D1riteBitem, '2, %, 2Dsystem crash

    'he 1riteBitem, FD operations o$ '# are redone.

    '2 log entries are ignored &y the reco"ery manager.

  • 7/25/2019 ENCh19

    19/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-19

    Database RecoveryDeferre. p.ate 0ith cocurret users

    'his en"ironment re

  • 7/25/2019 ENCh19

    20/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-2

    Database RecoveryDeferre. p.ate 0ith cocurret users

    b 8starttrasactio T1:

    80riteite) T1 D 2:

    8co))it T1:8chec$poit:

    8starttrasactio T4:

    80riteite) T4 # 15:

    80riteite) T4 + 2:

    8co))it T4:

    8starttrasactio T2:

    80riteite) T2 # 12:8starttrasactio T3:

    80riteite) T3 + 3:

    80riteite) T2 D 25: syste) crash

    T2 a. T3 are i"ore. because they .i. ot reach their co))it poits7

    T4 is re.oe because its co))it poit is after the last chec$poit7

    a T1 T2 T3 T4

    rea.ite) + rea.ite) # rea.ite) + rea.ite) #

    rea.ite) D 0riteite) # 0riteite) + 0riteite) #

    0riteite) D rea.ite) D rea.ite) C rea.ite) +

    0riteite) D 0riteite) C 0riteite) +

  • 7/25/2019 ENCh19

    21/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-21

    Database RecoveryDeferre. p.ate 0ith cocurret users

    '1o ta&les are re

  • 7/25/2019 ENCh19

    22/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-22

    Database Recovery

    Recovery Techi>ues #ase. o '))e.iate p.ate

    In this algorithm 5(I6s o$ a transaction are $lushed to the data&ase disunder =5* &e$ore it commits. (or this reason the reco"ery manager

    u.oesall transactions during reco"ery. !o transaction is re.oe. It is

    possi&le that a transaction might ha"e completed e8ecution and ready to

    commit &ut this transaction is also u.oe.

    .o=%o-re.o +l"orith)

  • 7/25/2019 ENCh19

    23/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-23

    Database RecoveryRecovery Techi>ues #ase. o '))e.iate p.ate

    Reco"ery schemes o$ this category apply u.o and also re.o $or

    reco"ery. In a singleuser en"ironment no concurrency control isre

  • 7/25/2019 ENCh19

    24/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-24

    Database RecoveryRecovery Techi>ues #ase. o '))e.iate p.ate

    Reco"ery schemes o$ this category applies undo and also redo to reco"er

    the data&ase $rom $ailure. In concurrent e8ecution en"ironment a

    concurrency control is re

  • 7/25/2019 ENCh19

    25/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-25

    Database Recovery/ha.o0 Pa"i"

    'he 5(I6 does not o"er1rite its :(I6 &ut recorded at another place on

    the dis. 'hus, at any time a data item has 5(I6 and :(I6 /Shado1

    copy o$ the data item at t1o di$$erent places on the dis.

    " #

    Database

    "$ #$

    > and ?9 Shado1 copies o$ data items

    > and ?9 Current copies o$ data items

  • 7/25/2019 ENCh19

    26/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-26

    Database Recovery/ha.o0 Pa"i"

    'o manage access o$ data items &y concurrent transactions t1o

    directories /current and shado1 are used. 'he directory arrangement is

    illustrated &elo1. Jere a page is a data item.

    %a&e ' (old)

    %a&e 1

    %a&e *

    %a&e + (old)

    %a&e

    %a&e -

    %a&e + (ne.)

    %a&e ' (ne.)

    1

    +

    *

    '

    -

    Current Directory

    (ater updatin& pa&es +, ')

    !hado. Directory

    (not updated)

    1

    +

    *

    '

    -

  • 7/25/2019 ENCh19

    27/35Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-2,

    Database Recovery

    9 The +R'?/ Recovery +l"orith)

    'he 5RIES Reco"ery 5lgorithm is &ased on9

    #. =5* /=rite 5head *ogging

    2. Repeating history during redo9 5RIES 1ill retrace all actions o$

    the data&ase system prior to the crash to reconstruct the data&ase

    state 1hen the crash occurred.

    ). *ogging changes during undo9 It 1ill pre"ent 5RIES $rom

    repeating the completed undo operations i$ a $ailure occurs

    during reco"ery, 1hich causes a restart o$ the reco"ery process.

  • 7/25/2019 ENCh19

    28/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-2

    Database RecoveryThe +R'?/ Recovery +l"orith)

    'he 5RIES reco"ery algorithm consists o$ three steps9

    17 +alysis9 step identi$ies the dirty /updated pages in the &u$$er

    and the set o$ transactions acti"e at the time o$ crash. 'he

    appropriate point in the log 1here redo is to start is alsodetermined.

    27 Re.o9 necessary redo operations are applied.

    37 .o9 log is scanned &ac1ards and the operations o$

    transactions acti"e at the time o$ crash are undone in re"erseorder.

  • 7/25/2019 ENCh19

    29/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-29

    Database RecoveryThe +R'?/ Recovery +l"orith)

    The !o" a. !o" /e>uece %u)ber !/%

    5 log record is 1ritten $or /a data update, /& transaction commit,

    /c transaction a&ort, /d undo, and /e transaction end. In the case

    o$ undo a compensating log record is 1ritten.

    5 uni

  • 7/25/2019 ENCh19

    30/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-3

    Database RecoveryThe +R'?/ Recovery +l"orith)

    The !o" a. !o" /e>uece %u)ber !/%

    5 log record stores9

    #. Pre"ious *S! o$ that transaction9 It lins the log record o$

    each transaction. It is lie a &ac pointer points to the pre"ious

    record o$ the same transaction.

    2. 'ransaction I%). 'ype o$ log record.

    (or a 1rite operation the $ollo1ing additional in$ormation is logged9

    4. Page I% $or the page that includes the item

    . *ength o$ the updated item

    -. Its o$$set $rom the &eginning o$ the page

    . :(I6 o$ the item

    3. 5(I6 o$ the item

  • 7/25/2019 ENCh19

    31/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-31

    Database RecoveryThe +R'?/ Recovery +l"orith)

    The Trasactio table a. the Dirty Pa"e table

    (or e$$icient reco"ery $ollo1ing ta&les are also stored in the log during

    checpointing9

    Trasactio table9 Contains an entry $or each acti"e transaction,

    1ith in$ormation such as transaction I%, transaction status and the

    *S! o$ the most recent log record $or the transaction.

    Dirty Pa"e table9 Contains an entry $or each dirty page in the

    &u$$er, 1hich includes the page I% and the *S! corresponding to the

    earliest update to that page.

  • 7/25/2019 ENCh19

    32/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-32

    Database RecoveryThe +R'?/ Recovery +l"orith)

    Chec$poiti"

    5 checpointing does the $ollo1ing9

    #. =rites a begin_checkpointrecord in the log

    2. =rites an end_checkpointrecord in the log. =ith this record the

    contents o$ transaction ta&le and dirty page ta&le are appended tothe end o$ the log.

    ). =rites the *S! o$ the begin_checkpointrecord to a special $ile.

    'his special $ile is accessed during reco"ery to locate the last

    checpoint in$ormation.

    'o reduce the cost o$ checpointing and allo1 the system to

    continue to e8ecute transactions, 5RIES uses G$uzzy

    checpointingH.

  • 7/25/2019 ENCh19

    33/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-33

    Database RecoveryThe +R'?/ Recovery +l"orith)

    'he $ollo1ing steps are per$ormed $or reco"ery17 +alysis phase9 Start at the &eginBchecpoint record and

    proceed to the endBchecpoint record. 5ccess transaction ta&le

    and dirty page ta&le are appended to the end o$ the log. !ote

    that during this phase some other log records may &e 1ritten to

    the log and transaction ta&le may &e modi$ied. 'he analysisphase compiles the set o$ redo and undo to &e per$ormed and

    ends.

    27 Re.o phase9 Starts $rom the point in the log up to 1here all dirty

    pages ha"e &een $lushed, and mo"e $or1ard to the end o$ the

    log. 5ny change that appears in the dirty page ta&le is redone.37 .o phase9 Starts $rom the end o$ the log and proceeds

    &ac1ard 1hile per$orming appropriate undo. (or each undo it

    1rites a compensating record in the log.

    'he reco"ery completes at the end o$ undo phase.

  • 7/25/2019 ENCh19

    34/35

    Copyright 2004 Ramez Elmasri and Shamant !a"athe

    Elmasri/Navathe, Fundamentals o Database !ystems, Fourth Edition Chapter 19-34

    Database Recovery+ e&a)ple of the 0or$i" of +R'?/ sche)e

    &'N &A'T&'N TRANID T*E *AGEID Other In+%,

    1 - T1 up.ate C

    2 - T2 up.ate B

    3 1 T1 !%it

    " /e0in !he!$p%int

    # en. !he!$p%int

    - T3 up.ate A

    2 T2 up.ate C

    T2 !%it

    (a)

    TRAN'ACTION ID &A'T &'N 'TAT4' *AGE ID &'N

    T1 3 !%it C 1

    T2 2 in pr%0ress B 2

    TRAN'ACTION ID &A'T &'N 'TAT4' *AGE ID &'N

    T1 3 !%it C 1

    T2 !%it B 2

    T3 in pr%0ress A

    (/)

    TR/N!/CT0N T/23E D0RT# %/4E T/23E

    (!)

    TR/N!/CT0N T/23E D0RT# %/4E T/23E

  • 7/25/2019 ENCh19

    35/35

    El i/N th F d t l D t b ! t F th Editi Ch t 19 35

    Database Recovery

    1 Recovery i )ulti.atabase syste)

    5 multidata&ase system is a special distri&uted data&ase system

    1here one node may &e running relational data&ase system under

    +ni8, another may &e running o&Kectoriented system under

    1indo1 and so on. 5 transaction may run in a distri&uted $ashion

    at multiple nodes. In this e8ecution scenario the transactioncommits only 1hen all these multiple nodes agree to commit

    indi"idually the part o$ the transaction they 1ere e8ecuting. 'his

    commit scheme is re$erred to as Gtwo-phase commitH /2PC. I$ any

    one o$ these nodes $ails or cannot commit the part o$ the

    transaction, then the transaction is a&orted. Each node reco"ers thetransaction under its o1n reco"ery protocol.