77
CS 245 Notes 10 1 CS 245: Database System Principles Notes 10: More TP Hector Garcia-Molina

CS 245: Database System Principles Notes 10: More TP

  • Upload
    dillan

  • View
    23

  • Download
    0

Embed Size (px)

DESCRIPTION

CS 245: Database System Principles Notes 10: More TP. Hector Garcia-Molina. Sections to Skim:. Section 18.8 [18.8] Sections 19.2 19.4, 19.5, 19.6 [none, i.e., read all Ch 19] [In the Second Edition, skip all of Chapter 20, and Sections 21.5, 21.6, 21.7, 22.2 through 22.7]. - PowerPoint PPT Presentation

Citation preview

Page 1: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 1

CS 245: Database System Principles

Notes 10: More TP

Hector Garcia-Molina

Page 2: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 2

Sections to Skim:

• Section 18.8 [18.8]• Sections 19.2 19.4, 19.5, 19.6

[none, i.e., read all Ch 19]• [In the Second Edition, skip all of Chapter 20,

and Sections 21.5, 21.6, 21.7, 22.2 through 22.7]

Page 3: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 3

Chapter 19 [19] More on transaction processingTopics:• Cascading rollback, recoverable

schedule• Deadlocks

– Prevention– Detection

• View serializability• Distributed transactions• Long transactions (nested,

compensation)

Page 4: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 4

Example: Tj Ti

Wj(A) ri(A) Commit Ti

Abort Tj

Concurrency control & recovery

……

……

Page 5: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 5

Example: Tj Ti

Wj(A) ri(A) Commit Ti

Abort Tj

Concurrency control & recovery

……

……

… Non-Persistent Commit (Bad!)

Page 6: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 6

Example: Tj Ti

Wj(A) ri(A) Commit Ti

Abort Tj

Concurrency control & recovery

……

……

… Non-Persistent Commit (Bad!)

avoided byrecoverableschedules

Page 7: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 7

Example: Tj Ti

Wj(A) ri(A) wi(B)

Abort Tj

[Commit Ti]

Concurrency control & recovery

……

……

Page 8: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 8

Example: Tj Ti

Wj(A) ri(A) wi(B)

Abort Tj

[Commit Ti]

Concurrency control & recovery

……

……

… Cascading rollback (Bad!)

Page 9: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 9

Example: Tj Ti

Wj(A) ri(A) wi(B)

Abort Tj

[Commit Ti]

Concurrency control & recovery

……

……

… Cascading rollback (Bad!)

avoided byavoids-cascading-rollback (ACR)schedules

Page 10: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 10

• Schedule is conflict serializable• Tj Ti

• But not recoverable

Page 11: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 11

• Need to make “final’ decision for each transaction:– commit decision - system guarantees

transaction will or has completed, no matter what

– abort decision - system guarantees transaction will or has been rolled back

(has no effect)

Page 12: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 12

To model this, two new actions:

• Ci - transaction Ti commits• Ai - transaction Ti aborts

Page 13: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 13

...

...

...

...

Back to example:

Tj Ti

Wj(A)ri(A)

Ci can we commit

here?

Page 14: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 14

Definition

Ti reads from Tj in S (Tj S Ti) if

(1) wj(A) <S ri(A)

(2) aj <S ri(A) (< : does not precede)

(3) If wj(A) <S wk(A) <S ri(A) then

ak <S ri(A)

Page 15: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 15

Definition

Schedule S is recoverable if whenever Tj S Ti and j i and Ci

Sthen Cj <S Ci

Page 16: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 16

Note: in transactions, reads and writes precede commit or abort

If Ci Ti, then ri(A) < Ci

wi(A) < Ci

If Ai Ti, then ri(A) < Ai

wi(A) < Ai

• Also, one of Ci, Ai per transaction

Page 17: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 17

How to achieve recoverable schedules?

Page 18: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 18

With 2PL, hold write locks to commit (strict 2PL)

Tj Ti

Wj(A)

Cj

uj(A)ri(A)

...

...

...

...

...

...

...

Page 19: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 19

With validation, no change!

Page 20: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 20

• S is recoverable if each transaction commits only after all transactions from which it read have committed.

Page 21: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 21

• S is recoverable if each transaction commits only after all transactions from which it read have committed.

• S avoids cascading rollback if each transaction may read only those values written by committed transactions.

Page 22: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 22

• S is recoverable if each transaction commits only after all transactions from which it read have committed.

• S avoids cascading rollback if each transaction may read only those values written by committed transactions.

• S is strict if each transaction may read and write only items previously written by committed transactions.

Page 23: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 23

• Relationship of RC, ACR, Strict

Avoids cascading rollback

RC

ACR

ST SERIAL

Page 24: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 24

Examples

• Recoverable:– w1(A) w1(B) w2(A) r2(B) c1 c2

• Avoids Cascading Rollback:– w1(A) w1(B) w2(A) c1 r2(B) c2

• Strict:– w1(A) w1(B) c1 w2(A) r2(B) c2

Assumes w2(A) is donewithout reading

Page 25: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 25

Where are serializable schedules?

Avoids cascading rollback

RC

ACR

ST SERIAL

Page 26: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 26

Deadlocks

• Detection– Wait-for graph

• Prevention– Resource ordering– Timeout– Wait-die– Wound-wait

Page 27: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 27

Deadlock Detection

• Build Wait-For graph• Use lock table structures• Build incrementally or periodically• When cycle found, rollback victim

T1

T3

T2

T6

T5

T4T7

Page 28: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 28

Resource Ordering

• Order all elements A1, A2, …, An

• A transaction T can lock Ai after Aj only if i > j

Page 29: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 29

Resource Ordering

• Order all elements A1, A2, …, An

• A transaction T can lock Ai after Aj only if i > j

Problem : Ordered lock requests not realistic in most cases

Page 30: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 30

Timeout

• If transaction waits more than L sec., roll it back!

• Simple scheme• Hard to select L

Page 31: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 31

Wait-die

• Transactions given a timestamp when they arrive …. ts(Ti)

• Ti can only wait for Tj if ts(Ti)< ts(Tj) ...else die

Page 32: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 32

T1

(ts =10)

T2

(ts =20)

T3

(ts =25)

wait

wait

Example:

Page 33: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 33

T1

(ts =10)

T2

(ts =20)

T3

(ts =25)

wait

wait

Example:

wait?

Page 34: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 34

T1

(ts =10)

T2

(ts =20)

T3

(ts =25)

wait

wait

Example:

wait?

Page 35: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 35

Starvation with Wait-Die

• When transaction dies, re-try laterwith what timestamp?– original timestamp– new timestamp (time of re-submit)

Page 36: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 36

Starvation with Wait-Die

• Resubmit with original timestamp• Guarantees no starvation

– Transaction with oldest ts never dies– A transaction that dies will eventually

have oldest ts and will complete...

Page 37: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 37

T1

(ts =22)

T2

(ts =20)

T3

(ts =25)

wait(A)

Second Example:

requests A: wait for T2 or T3?

Note: ts between20 and 25.

Page 38: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 38

T1

(ts =22)

T2

(ts =20)

T3

(ts =25)

wait(A)

Second Example (continued):

wait(A)

One option: T1 waits just for T3, transaction holding lock.But when T2 gets lock, T1 will have to die!

Page 39: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 39

T1

(ts =22)

T2

(ts =20)

T3

(ts =25)

wait(A)

Second Example (continued):

wait(A)

wait(A)

Another option: T1 only gets A lock after T2, T3 complete,

so T1 waits for both T2, T3 T1 dies right away!

Page 40: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 40

T1

(ts =22)

T2

(ts =20)

T3

(ts =25)

wait(A)

Second Example (continued):

wait(A)

wait(A)

Yet another option: T1 preempts T2, so T1 only

waits for T3; T2 then waits for T3 and T1... T2 may starve?

redundant arc

Page 41: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 41

Wound-wait

• Transactions given a timestamp when they arrive … ts(Ti)

• Ti wounds Tj if ts(Ti)< ts(Tj) else Ti waits

“Wound”: Tj rolls back and gives lock to Ti

Page 42: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 42

T1

(ts =25)

T2

(ts =20)

T3

(ts =10)

wait

wait

Example:

Page 43: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 43

T1

(ts =25)

T2

(ts =20)

T3

(ts =10)

wait

wait

Example:

wait

Page 44: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 44

Starvation with Wound-Wait

• When transaction dies, re-try laterwith what timestamp?– original timestamp– new timestamp (time of re-submit)

Page 45: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 45

T1

(ts =15)

T2

(ts =20)

T3

(ts =10)

wait(A)

Second Example:

requests A: wait for T2 or T3?

Note: ts between10 and 20.

Page 46: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 46

T1

(ts =15)

T2

(ts =20)

T3

(ts =10)

wait(A)

Second Example (continued):

wait(A)

One option: T1 waits just for T3, transaction holding lock.But when T2 gets lock, T1 waits for T2 and wounds T2.

Page 47: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 47

T1

(ts =15)

T2

(ts =20)

T3

(ts =10)

wait(A)

Second Example (continued):

wait(A)

wait(A)

Another option: T1 only gets A lock after T2, T3 complete,

so T1 waits for both T2, T3 T2 wounded right away!

Page 48: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 48

T1

(ts =15)

T2

(ts =20)

T3

(ts =10)

wait(A)

Second Example (continued):

wait(A)

wait(A)

Yet another option: T1 preempts T2, so T1 only

waits for T3; T2 then waits for T3 and T1... T2 is spared!

Page 49: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 49

User/Program commands

Lots of variations, but in general• Begin_work• Commit_work• Abort_work

Page 50: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 50

Nested transactions

User program:

Begin_work;

If results_ok, then commit workelse abort_work

...

...

...

Page 51: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 51

Nested transactions

User program:

Begin_work;Begin_work;

If results_ok, then commit work else {abort_work; try something else…}

If results_ok, then commit workelse abort_work

...

...

...

Page 52: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 52

Parallel Nested TransactionsT1: begin-work

parallel:T11: begin_work

commit_work

T12: begin_work

commit_work

commit_work

......

......

Page 53: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 53

Parallel Nested TransactionsT1: begin-work

parallel:T11: begin_work

commit_work

T12: begin_work

commit_work

commit_work

......

......

T1

T11 T12

Page 54: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 54

Parallel Nested TransactionsT1: begin-work

parallel:T11: begin_work

commit_work

T12: begin_work

commit_work

commit_work

......

......

T1

T11 T12

T1

Page 55: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 55

LockingLocking

What are we really locking?

Page 56: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 56

Example:

Ti

Read record r1

Read record r1 do record

lockingModify record r3

...

...

...

...

Page 57: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 57

But underneath:

Disk

pages

R3

R1

R2

record id

Page 58: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 58

But underneath:

Disk

pages

R3

R1

R2

record id

If we lock all

data involved in read

of R1, we may prevent

an update to R2

(which may require

reorganization within

block)

Page 59: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 59

Solution: view DB at two levels

Top level: record actions record locks undo/redo actions — logical

e.g., Insert record(X,Y,Z) Redo: insert(X,Y,Z) Undo: delete

Page 60: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 60

Low level: deal with physical details latch page during action(release at end of action)

Page 61: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 61

Note: undo does not return physical DB to original state; only same logical state

e.g., Insert R3 Undo (delete R3)

R1 R1R2

R1R2

R2R3

Page 62: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 62

Logging Logical Actions

• Logical action typically span one block(physiological actions)

• Undo/redo log entry specifies undo/redo logical action

Page 63: CS 245: Database System Principles Notes 10: More TP

Question

• How to deal with spanned record?

CS 245 Notes 10 63

part (a) part (b) part (c)

Page 64: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 64

Logging Logical Actions

• Logical action typically span one block(physiological actions)

• Undo/redo log entry specifies undo/redo logical action

• Challenge: making actions idempotent•Example (bad): redo insert

key inserted multiple times!

Page 65: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 65

Solution: Add Log Sequence Number

Log record:•LSN=26•OP=insert(5,v2) into P• ...

3, v1sem lsn=25 ...

3, v1sem lsn=26 ...

5, v2

Page 66: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 66

Still Have a Problem!

3, v1lsn=24 ...

4, v23, v1

lsn=25 ...

3, v1lsn=26 ...

5, v3

T1Del 4

T2Ins 5

Page 67: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 67

Still Have a Problem!

3, v1lsn=24 ...

4, v23, v1

lsn=25 ...

3, v1lsn=26 ...

5, v3

T1Del 4

T2Ins 5

3, v1lsn=?? ...

5, v34, v2

undoDel 4

Page 68: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 68

Still Have a Problem!

3, v1lsn=24 ...

4, v23, v1

lsn=25 ...

3, v1lsn=26 ...

5, v3

T1Del 4

T2Ins 5

3, v1lsn=?? ...

5, v34, v2

undoDel 4

Make log entryfor undo

lsn=27

Page 69: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 69

Compensation Log Records

• Log record to indicate undo (not redo) action performed

• Note: Compensation may not return page to exactly the initial state

Page 70: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 70

At Recovery: Example

lsn=21T1a1p1

lsn=35T1a2-1

p2

lsn=27T1a2p2

... ... ......

Log:

Page 71: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 71

What to do with p2 (during T1 rollback)?

• If lsn(p2)<27 then ... ?• If 27 lsn(p2) < 35 then ... ?• If lsn(p2) 35 then ... ?

Note: lsn(p2) is lsn of p copy on disk

Page 72: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 72

Recovery Strategy

[1] Reconstruct state at time of crash– Find latest valid checkpoint, Ck, and

let ac be its set of active transactions– Scan log from Ck to end:

• For each log entry [lsn, page] do:if lsn(page) < lsn then redo action

• If log entry is start or commit, update ac

Page 73: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 73

Recovery Strategy

[2] Abort uncommitted transactions– Set ac contains transactions to abort– Scan log from end to Ck :

• For each log entry (not undo) of an ac transaction,undo action (making log entry)

– For ac transactions not fully aborted,read their log entries older than Ck andundo their actions

Page 74: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 74

Example: What To Do After Crash

lsn=21T1a1p1

lsn=35T1a2-1

p2

lsn=27T1a2p2

... ... ......

Log:

...

lsn=29T1a3p3

lsn=31T1a3-1

p3 ...

chkpt

Page 75: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 75

During Undo: Skip Undo’s

lsn=21T1a1p1

lsn=35T1a2-1

p2

lsn=27T1a2p2

... ... ......

Log:

...

lsn=29T1a3p3

lsn=31T1a3-1

p3 ...

chkpt

pointer to forward action

pointer to previous T1 action

Page 76: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 76

Related idea: Sagas• Long running activity: T1, T2, ... Tn

• Each step/trasnaction Ti has a compensating transaction Ti-1

• Semantic atomicity: execute one of– T1, T2, ... Tn

– T1, T2, ... Tn-1 T-1n-1, T-1

n-2, ... T-11

– T1, T2, ... Tn-2 T-1n-2, T-1

n-3, ... T-11

– T1, T-11

– nothing

...

Page 77: CS 245: Database System Principles Notes 10: More TP

CS 245 Notes 10 77

Summary

• Cascading rollbackRecoverable schedule

• Deadlock– Prevention– Detectoin

• Nested transactions• Multi-level view