cs4432: database systems ii

41
cs4432 concurrency control 1 CS4432: Database Systems II Concurrency Control with Recovery

Upload: elaine-wade

Post on 30-Dec-2015

34 views

Category:

Documents


0 download

DESCRIPTION

CS4432: Database Systems II. Concurrency Control with Recovery. Concurrency control & recovery. Example:T i T o W i (A) r j (A) Commit T o Abort T i. …. …. …. …. …. …. Cascading rollback ( But already committed!). Note : Schedule is conflict serializable, - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS4432: Database Systems II

cs4432 concurrency control 1

CS4432: Database Systems II

Concurrency Control with Recovery

Page 2: CS4432: Database Systems II

cs4432 concurrency control 2

Example: Ti To

Wi(A) rj(A) Commit To

Abort Ti

Concurrency control & recovery

……

… ……

…Cascading rollback ( But already committed!)

Page 3: CS4432: Database Systems II

cs4432 concurrency control 3

Note :

Schedule is conflict serializable, But schedule is not recoverable.

Page 4: CS4432: Database Systems II

cs4432 concurrency control 4

• Need to make “final’ decision for each transaction:

– commit decision - system guarantees transaction will or has completed, no matter what (Ci)

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

(has no effect) (Ai)

Page 5: CS4432: Database Systems II

cs4432 concurrency control 6

...

...

...

...

Back to example:

Ti Tj

Wi(A)rj(A)

Cj can we commit

here?

Page 6: CS4432: Database Systems II

cs4432 concurrency control 7

Definition

Ti read from Tj in S (Tj S Tj) 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 7: CS4432: Database Systems II

cs4432 concurrency control 8

Definition

Intuition : A schedule S is recoverable,

if transactions only commit after all transactions they read from have already been committed first.

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

then Cj <S Ci

Page 8: CS4432: Database Systems II

cs4432 concurrency control 10

How to achieve recoverable schedules?

Page 9: CS4432: Database Systems II

cs4432 concurrency control 11

Strict 2PL : With 2PL, hold write locks until commit

Ti Tj

Wi(A)

Ciui(A)

rj(A)

...

...

...

...

...

...

...

Page 10: CS4432: Database Systems II

cs4432 concurrency control 12

• 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 11: CS4432: Database Systems II

cs4432 concurrency control 13

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

Avoids cascading rollback

RC

ACR

STSERIAL

Page 12: CS4432: Database Systems II

14

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 done

without reading

Page 13: CS4432: Database Systems II

CS4432 transaction management 15

Schedule with Deadlock

T1 T2l1(A); Read(A) l2(B);Read(B)A A+100;Write(A) B Bx2;Write(B)

ll11(B)(B) l l22(A)(A)delayeddelayed

Page 14: CS4432: Database Systems II

CS4432 transaction management 16

Deadlocks

• Detection– Wait-for graph

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

Page 15: CS4432: Database Systems II

CS4432 transaction management 17

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 16: CS4432: Database Systems II

CS4432 transaction management 18

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 17: CS4432: Database Systems II

CS4432 transaction management 19

Timeout

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

• Simple scheme• Hard to select L

Page 18: CS4432: Database Systems II

CS4432 transaction management 20

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 19: CS4432: Database Systems II

CS4432 transaction management 21

T1

(ts =10)

T2

(ts =20)

T3

(ts =25)

wait

wait

Example:

wait?

Page 20: CS4432: Database Systems II

CS4432 transaction management 22

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 21: CS4432: Database Systems II

CS4432 transaction management 23

Summary

• Cascading rollbackRecoverable schedule

• Deadlock– Prevention– Detection

Page 22: CS4432: Database Systems II

Overall Summary

cs4432 concurrency control 24

Page 23: CS4432: Database Systems II

25

Recovery: DB Dump + Log

backup

database

active

databaselog

• If active database is lost,

– restore active database from backup

– bring up-to-date using redo entries in log

Page 24: CS4432: Database Systems II

26

When can log be discarded?

check-

point

db

dump

last

needed

undo

not needed for

media recovery

not needed for undo

after system failure

not needed for

redo after system failure

log

time

Page 25: CS4432: Database Systems II

27

Multiple Mode Locks: Shared locksSo far:S = ...l1(A) r1(A) u1(A) … l2(A) r2(A) u2(A) …

Do not conflict

Instead:S=... ls1(A) r1(A) ls2(A) r2(A) …. us1(A)

us2(A)

Also, we have exclusive (X)/write locks

Page 26: CS4432: Database Systems II

28

Ti

Read(A),Write(B)

l(A),Read(A),l(B),Write(B)…

Read(A),Write(B)

Scheduler, part I

Scheduler, part II

DB

locktable

Locks in Practice

Page 27: CS4432: Database Systems II

29

Lock table Conceptually

A

BC

...

Lock info for B

Lock info for C

If null, object is unlocked

Every

poss

ible

obje

ct

Page 28: CS4432: Database Systems II

30

But use hash table:

A

If object not found in hash table, it is unlocked

Lock info for AA

......

H

Page 29: CS4432: Database Systems II

31

Lock info for A - example

tran mode wait? Nxt T_link

Object:AGroup mode:UWaiting:yesList:

T1 S no

T2 U no

T3 XX yes

To other T3

records

Page 30: CS4432: Database Systems II

32

What are the objects we lock?

?

Relation A

Relation B

...

Tuple A

Tuple BTuple C

...

Disk block

A

Disk block

B

...

DB DB DB

Page 31: CS4432: Database Systems II

33

• Locking works in any case, but should we choose small or large objects?

• If we lock large objects (e.g., Relations)– Need few locks– Low concurrency

• If we lock small objects (e.g., tuples,fields)– Need more locks– More concurrency

Page 32: CS4432: Database Systems II

34

We can have it both ways!!

Ask any janitor to give you the solution...

hall

Stall 1 Stall 2 Stall 3 Stall 4

restroom

Page 33: CS4432: Database Systems II

35

Example

R1

t1t2 t3

t4

T1(IS)

T1(S)

, T2(S)

Page 34: CS4432: Database Systems II

36

Example

R1

t1t2 t3

t4

T1(IS)

T1(S)

, T2(IX)

T2(IX)

Page 35: CS4432: Database Systems II

37

Multiple granularity

Comp Requestor

IS IX S SIX X IS

Holder IX S

SIX

X

Page 36: CS4432: Database Systems II

38

Multiple granularity

Comp Requestor

IS IX S SIX X IS

Holder IX S

SIX

X

T T T T F

F

F

F

FFFFF

FFFT

FTFT

FFTT

Page 37: CS4432: Database Systems II

39

Inserting Records: PhantomsPhantoms

Example: relation R (E#,name,…)constraint: E# is keyuse tuple locking

R E# Name ….o1 55 Smitho2 75 Jones

Page 38: CS4432: Database Systems II

40

T1: Insert <04,Kerry,…> into RT2: Insert <04,Bush,…> into R

T1 T2

S1(o1) S2(o1)S1(o2) S2(o2)Check Constraint Check Constraint

Insert o3[04,Kerry,..] Insert

o4[04,Bush,..]

... ...

Page 39: CS4432: Database Systems II

41

Solution

• Use multiple granularity tree• Before insert of node Q, lock parent(Q) in X mode R1

t1t2 t3

Page 40: CS4432: Database Systems II

42

Back to exampleT1: Insert<04,Kerry> T2: Insert<04,Bush>

T1 T2

X1(R)

Check constraintInsert<04,Kerry>U(R)

X2(R)Check constraintOops! e# = 04 already in

R!

XX22(R(R))

delayed

Page 41: CS4432: Database Systems II

43

Instead of using R, can use index on R:

Example: R

Index0<E#<100

Index100<E#<200

E#=2 E#=5 E#=107 E#=109...

...

...