ppt

advertisement
CS4432: Database Systems II
Concurrency Control with Recovery
cs4432
concurrency control
1
Ti
To
… …
… …
Wi(A)
Abort Ti
rj(A)
Commit To
…
Example:
…
Concurrency control & recovery
Cascading rollback ( But already committed!)
cs4432
concurrency control
2
Note :
Schedule is conflict serializable,
But schedule is not recoverable.
cs4432
concurrency control
3
• 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
4
Back to example:
rj(A)
...
...
Wi(A)
Tj
...
...
Ti
Cj 
cs4432
concurrency control
can we commit
here?
6
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
7
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
8
How to achieve recoverable schedules?
cs4432
concurrency control
10
Strict 2PL : With 2PL, hold write
Ti
Tj
...
... ... ... ...
locks until commit
...
Wi(A)
...
Ci
ui(A)
cs4432
rj(A)
concurrency control
11
• 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
12
• S is strict if each transaction may read
and write only items previously written
by committed transactions.
RC
ST
Avoids SERIAL
cascading rollback
ACR
cs4432
concurrency control
13
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:
Assumes w2(A) is done
without reading
– w1(A) w1(B) c1 w2(A) r2(B) c2
14
Schedule with Deadlock
T1
l1(A); Read(A)
A A+100;Write(A)
l1(B)
delayed
CS4432
T2
l2(B);Read(B)
B Bx2;Write(B)
l2(A)
delayed
transaction management
15
Deadlocks
• Detection
– Wait-for graph
• Prevention
– Resource ordering
– Timeout
– Wait-die
– Wound-wait
CS4432
transaction management
16
Deadlock Detection
•
•
•
•
Build Wait-For graph
Use lock table structures
Build incrementally or periodically
When cycle found, rollback victim
T2
T1
T4
CS4432
T3
T5
T6
transaction management
T7
17
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
18
Timeout
• If transaction waits more than L sec.,
roll it back!
• Simple scheme
• Hard to select L
CS4432
transaction management
19
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
20
Example:
T1
wait
(ts =10)
T2
wait?
wait
(ts =20)
T3
(ts =25)
CS4432
transaction management
21
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
22
Summary
• Cascading rollback
Recoverable schedule
• Deadlock
– Prevention
– Detection
CS4432
transaction management
23
Overall Summary
cs4432
concurrency control
24
Recovery: DB Dump + Log
backup
active
database
database
log
• If active database is lost,
– restore active database from backup
– bring up-to-date using redo entries in log
25
When can log be discarded?
log
db
last
check-
dump
needed
point
undo
time
not needed for
media recovery
not needed for undo
after system failure
not needed for
redo after system failure
26
Multiple Mode Locks: Shared locks
So 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
27
Locks in Practice
Ti
Read(A),Write(B)
lock
table
Scheduler, part I
l(A),Read(A),l(B),Write(B)…
Scheduler, part II
Read(A),Write(B)
DB
28
A
B
C
Conceptually
If null, object is unlocked

Lock info for B
Lock info for C

...
Every possible object
Lock table
29
But use hash table:
H
...
A
A
Lock info for A
...
If object not found in hash table, it is
unlocked
30
Lock info for A - example
tran mode wait? Nxt T_link
Object:A
Group mode:U
Waiting:yes
List:
T1
S
no
T2
U
no
T3
X
yes 
To other T3
records
31
What are the objects we lock?
Relation A
Relation B
Tuple A
Tuple B
Tuple C
Disk
block
A
...
...
Disk
block
B
?
...
DB
DB
DB
32
• 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
33
We can have it both ways!!
Ask any janitor to give you the solution...
Stall 1
Stall 2
Stall 3
Stall 4
restroom
hall
34
Example
T1(IS) , T2(S)
R1
t1
t2
t3
t4
T1(S)
35
Example
T1(IS) , T2(IX)
R1
t1
t2
T1(S)
t3
t4
T2(IX)
36
Multiple granularity
Comp
Requestor
IS IX S SIX X
IS
Holder IX
S
SIX
X
37
Multiple granularity
Comp
Requestor
IS IX S SIX X
IS T
Holder IX T
S T
SIX T
X F
T
T
F
F
F
T
F
T
F
F
T
F
F
F
F
F
F
F
F
F
38
Inserting Records: Phantoms
Example: relation R (E#,name,…)
constraint: E# is key
use tuple locking
R
o1
o2
E# Name
55 Smith
75 Jones
….
39
T1: Insert <04,Kerry,…> into R
T2: Insert <04,Bush,…> into R
T1
S1(o1)
S1(o2)
Check Constraint
S2(o1)
S2(o2)
Check Constraint
...
...
Insert o3[04,Kerry,..]
T2
Insert o4[04,Bush,..]
40
Solution
• Use multiple granularity tree
• Before insert of node Q,
lock parent(Q) in
R1
X mode
t1
t2
t3
41
Back to example
T1: Insert<04,Kerry>
T1
X1(R)
T2: Insert<04,Bush>
T2
X2(R)
delayed
Check constraint
Insert<04,Kerry>
U(R)
X2(R)
Check constraint
Oops! e# = 04 already in R!
42
Instead of using R, can use index on R:
Example:
R
E#=2
E#=5
...
Index
100<E#<200
Index
0<E#<100
...
E#=107
E#=109
...
43
Download