cs4432: database systems ii
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 PresentationTRANSCRIPT
cs4432 concurrency control 1
CS4432: Database Systems II
Concurrency Control with Recovery
cs4432 concurrency control 2
Example: Ti To
Wi(A) rj(A) Commit To
Abort Ti
Concurrency control & recovery
……
… ……
…Cascading rollback ( But already committed!)
cs4432 concurrency control 3
Note :
Schedule is conflict serializable, But schedule is not recoverable.
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)
cs4432 concurrency control 6
...
...
...
...
Back to example:
Ti Tj
Wi(A)rj(A)
Cj can we commit
here?
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)
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
cs4432 concurrency control 10
How to achieve recoverable schedules?
cs4432 concurrency control 11
Strict 2PL : With 2PL, hold write locks until commit
Ti Tj
Wi(A)
Ciui(A)
rj(A)
...
...
...
...
...
...
...
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.
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
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
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
CS4432 transaction management 16
Deadlocks
• Detection– Wait-for graph
• Prevention– Resource ordering– Timeout– Wait-die– Wound-wait
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
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
CS4432 transaction management 19
Timeout
• If transaction waits more than L sec., roll it back!
• Simple scheme• Hard to select L
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
CS4432 transaction management 21
T1
(ts =10)
T2
(ts =20)
T3
(ts =25)
wait
wait
Example:
wait?
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
CS4432 transaction management 23
Summary
• Cascading rollbackRecoverable schedule
• Deadlock– Prevention– Detection
Overall Summary
cs4432 concurrency control 24
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
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
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
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
29
Lock table Conceptually
A
BC
...
Lock info for B
Lock info for C
If null, object is unlocked
Every
poss
ible
obje
ct
30
But use hash table:
A
If object not found in hash table, it is unlocked
Lock info for AA
......
H
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
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
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
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
35
Example
R1
t1t2 t3
t4
T1(IS)
T1(S)
, T2(S)
36
Example
R1
t1t2 t3
t4
T1(IS)
T1(S)
, T2(IX)
T2(IX)
37
Multiple granularity
Comp Requestor
IS IX S SIX X IS
Holder IX S
SIX
X
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
39
Inserting Records: PhantomsPhantoms
Example: relation R (E#,name,…)constraint: E# is keyuse tuple locking
R E# Name ….o1 55 Smitho2 75 Jones
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,..]
... ...
41
Solution
• Use multiple granularity tree• Before insert of node Q, lock parent(Q) in X mode R1
t1t2 t3
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
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...
...
...