Documente Academic
Documente Profesional
Documente Cultură
8: Concurrency Control
Pekka Kilpelinen
(after Stanford CS245 slide originals by Hector Garcia-Molina, Jeff Ullman and Jennifer Widom)
DBMS 2001 Notes 8: Concurrency 1
Chapter 9
T1 T2
Concurrency Control
Tn
How to prevent harmful interference btw transactions?
=> scheduling techniques based on - locks - timestamps and validation
Notes 8: Concurrency 2
DB (consistency constraints)
DBMS 2001
Example:
T1: Read(A) A n A+100 Write(A) Read(B) B n B+100 Write(B)
DBMS 2001
Notes 8: Concurrency
Correctness depends on scheduling of transactions A schedule - Chronological (possibly interleaving) order in which actions of transactions are executed - A correct schedule is equivalent to executing transactions one-at-a-time in some order
DBMS 2001 Notes 8: Concurrency 4
Schedule A
T1 Read(A); A n A+100; Write(A); Read(B); B n B+100; Write(B); T2 A 25 125 125 Read(A);A n Av2; Write(A); Read(B);B n Bv2; Write(B); 250 250 250
5
B 25
OK
DBMS 2001 Notes 8: Concurrency
250
Schedule B
T1 T2 Read(A);A n Av2; Write(A); Read(B);B n Bv2; Write(B); Read(A); A n A+100; Write(A); Read(B); B n B+100; Write(B); 150 150 150
6
A 25 50
B 25
50
OK
DBMS 2001 Notes 8: Concurrency
150
Schedule C
T1 Read(A); A n A+100 Write(A); T2 A 25 125 Read(A);A n Av2; Write(A); Read(B); B n B+100; Write(B); Read(B);B n Bv2; Write(B); 250 125 250 250
7
B 25
OK
DBMS 2001 Notes 8: Concurrency
250
Schedule D
T1 Read(A); A n A+100; Write(A); T2 A 25 125 Read(A);A n Av2; Write(A); Read(B);B n Bv2; Write(B); Read(B); B n B+100; Write(B); Constraint violation!
DBMS 2001 Notes 8: Concurrency
B 25
250
Schedule E
T2
A 25 125
B 25
OK
DBMS 2001 Notes 8: Concurrency
125
Want schedules that are good , regardless of initial state ( " good in any DB state) and transaction semantics
T2
10
Sc =r1(A)w1(A) r1(B)w1(B)r2(A)w2(A)r2(B)w2(B) T1
DBMS 2001 Notes 8: Concurrency
T2
12
DBMS 2001
Notes 8: Concurrency
13
Concepts
Transaction: sequence of ri(x), wi(x) actions Conflicting actions: rh(A) wh(A) wh(A) wk(A) rk(A) wk(A)
If schedule S contains conflicting actions , ph(A), , qk(A), ... [i.e., one of p, q is w], transaction Th must precede Tk in a corresponding serial schedule. Denote this by Th " Tk
DBMS 2001 Notes 8: Concurrency 14
Returning to Sc
Sc=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B) T1 p T2 T1 p T2
Definition
S1, S2 are conflict equivalent schedules if S1 can be transformed into S2 by a series of swaps on non-conflicting actions.
(=> effect of both S1 and S2 on the DB is the same)
DBMS 2001
Notes 8: Concurrency
16
Definition
A schedule is conflict serializable if it is conflict equivalent to some serial schedule.
NB: Conflict serializability is a sufficient (but not a necessary) condition for serializability (equivalence to some serial schedule) Easier to enforce than serializability, therefore generally assured by commercial systems
DBMS 2001 Notes 8: Concurrency 17
is schedule)
Nodes: transactions T1, T2, ... in S Arcs: Ti p Tj for i { j whenever - pi(A), qj(A) are conflicting actions in S,
(same element A, at least one of actions is a write)
DBMS 2001
Notes 8: Concurrency
18
Exercise:
What is P(S) for
S = w3(A) w2(C) r1(A) w1(B) r1(C) w2(A) r4(A) w4(D)
Is S serializable?
DBMS 2001 Notes 8: Concurrency 19
Note: P(S1)=P(S2) S1, S2 conflict equivalent Counter example: S1= w1(A) r2(A) w2(B) r1(B) S2= r2(A) w1(A) r1(B) w2(B)
DBMS 2001
Notes 8: Concurrency
21
Theorem I
P(S1) acyclic S1 conflict serializable () Assume S1 is conflict serializable serial Ss: Ss, S1 conflict equivalent P(Ss) = P(S1) [ Lemma] P(S1) acyclic since P(Ss) is acyclic
DBMS 2001
Notes 8: Concurrency
22
Theorem (cont.)
P(S1) acyclic S1 conflict serializable () Assume P(S1) is acyclic T1 Transform S1 as follows: T2 T4 T3
(1) Take T1 to be transaction with no incoming arcs (2) Move all T1 actions to the front S1 =
qj(X)
p1(A)
(3) we now have S1 = < T1 actions ><... rest ...> (4) repeat above steps to serialize rest!
DBMS 2001 Notes 8: Concurrency 23
DBMS 2001
Notes 8: Concurrency
24
A locking protocol
Two new actions: lock (exclusive): unlock: Tj
scheduler
DBMS 2001
Notes 8: Concurrency
26
(ui(A))
DBMS 2001
Notes 8: Concurrency
27
Rule #2
S=
Legal scheduler
... ui(A) ...
.. li(A)
no lj(A) for i { j
At most one transaction Ti can hold a lock on any element A
DBMS 2001
Notes 8: Concurrency
28
Exercise:
What schedules are legal? What transactions are well-formed?
S1 = l1(A)l1(B)r1(A)w1(B)l2(B)u1(A)u1(B) r2(B)w2(B)u2(B)l3(B)r3(B)u3(B) S2 = l1(A)r1(A)w1(B)u1(A)u1(B) l2(B)r2(B)w2(B)l3(B)r3(B)u3(B) S3 = l1(A)r1(A)u1(A)l1(B)w1(B)u1(B) l2(B)r2(B)w2(B)u2(B)l3(B)r3(B)u3(B)
DBMS 2001 Notes 8: Concurrency 29
A B
25 25 125
l2(A);Read(A) A:=Ax2;Write(A); u2(A) 250 l2(B); Read(B) B:=Bx2;Write(B); u2(B) 50 l1(B); Read(B) B:=B+100;Write(B); u1(B) 150 Constraint violation! 250 150
30
DBMS 2001
Notes 8: Concurrency
Simple-minded locking not sufficient to ensure serializability (i.e., correctness)! " More advanced protocol known as "two phase locking"
DBMS 2001
Notes 8: Concurrency
31
Ti =
. li(A)
... ui(A)
no locks
...
no unlocks
DBMS 2001
Notes 8: Concurrency
32
# locks held by Ti
DBMS 2001
Notes 8: Concurrency
33
delayed
Schedule H
(T2 reversed)
T2 l2(B); Read(B) B Bx2;Write(B) l2(A)
delayed
Next step:
Show that Rules #1,2,3 conflict (2PL) serializable schedule
DBMS 2001
Notes 8: Concurrency
36
SH(T1)
37
Notes 8: Concurrency
Lemma Let S be a 2PL schedule. Ti p Tj in P(S) SH(Ti) <S SH(Tj) Proof of lemma: Ti p Tj means that qj (A) ; p,q conflict S = pi(A) By rules 1,2: S = pi(A) ui(A) lj(A) ... qj(A) By rule 3:
DBMS 2001
SH(Ti)
SH(Tj)
38
conflict
serializable schedule Proof: Let S be a 2PL schedule. Assume P(S) has cycle T1 p T2 p . Tn p T1 By Lemma: SH(T1) < SH(T2) < ... < SH(T1) Impossible, so P(S) acyclic S is conflict serializable (by Th. I)
DBMS 2001 Notes 8: Concurrency 39
Beyond this simple 2PL protocol, it is all a matter of improving performance and allowing more concurrency .
Shared locks Multiple granularity Inserts, deletes and phantoms Other types of C.C. mechanisms
Timestamping Validation
DBMS 2001
Notes 8: Concurrency
40
Shared locks
So far only exclusive locks: S = ...l1(A) r1(A) u1(A) l2(A) r2(A) u2(A)
Do not conflict " locking unnecessary
Instead, use shared locks (S) for reading: S=... ls1(A) r1(A) ls2(A) r2(A) . u1(A)u2(A)
DBMS 2001
Notes 8: Concurrency
41
Write actions conflict " use exclusive (X) locks for writing Lock actions: l-mk(A): lock A in mode m (S or X) for Tk uk(A): release (whatever) lock(s) held by transaction Tk on element A
DBMS 2001
Notes 8: Concurrency
42
Rule #1
Well-formed transactions
r1(A) w1(A) u1(A) u1(A)
DBMS 2001
Notes 8: Concurrency
43
What about transactions that first read and later write the same element? Option 1: Request exclusive lock Ti = ...l-X1(A) r1(A) ... w1(A) ... u1(A) Option 2: Upgrade
(E.g., need to read, but don t know if will write )
Ti=... l-S1(A)
w1(A) ...u(A)
Think as getting 2nd lock on A
DBMS 2001
Notes 8: Concurrency
44
S X
X false false
DBMS 2001
Notes 8: Concurrency
46
Rule # 3
(2PL)
Only change to previous: Lock upgrades S(A) p {S(A), X(A)} or S(A) p X(A) are allowed only in the growing phase
DBMS 2001
Notes 8: Concurrency
47
Theorem Rules 1,2,3 Conf.serializable for S/X locks schedules Proof: Similar to the X locks case
Update locks
A common deadlock problem with upgrades: T1 T2 l-S1(A) l-S2(A) l-X1(A) l-X2(A) --- Deadlock --DBMS 2001 Notes 8: Concurrency 49
Solution
If Ti wants to read A and knows it may later want to write A, it requests an update lock (not shared)
DBMS 2001
Notes 8: Concurrency
50
S X U
S T F F
X F F F
U T F F
As before
DBMS 2001
Notes 8: Concurrency
51
Note: object A may be locked in different modes at the same time... S1=...l-S1(A) l-S2(A) l-U3(A) To grant a lock in mode m, mode m must be compatible with all currently held locks on object
DBMS 2001
Notes 8: Concurrency
52
DBMS 2001
Notes 8: Concurrency
53
time
DBMS 2001
Notes 8: Concurrency
54
Read(A),Write(B) DB
DBMS 2001
Notes 8: Concurrency
55
Lock table
Every possible object
Conceptually
If null, object is unlocked 0 Lock info for B Lock info for C 0
A B C
...
Notes 8: Concurrency 56
DBMS 2001
A ...
T1 T2 T3
S U X
no no yes 0
To other lock table entries of transaction T3
DBMS 2001
Notes 8: Concurrency
58
DB
DBMS 2001
...
DB
Notes 8: Concurrency
...
DB
59
...
Locking works in any case, but should we choose small or large objects? If we lock large objects (e.g., Relations)
Need few locks Get low concurrency
DBMS 2001
Notes 8: Concurrency
60
restroom
hall
DBMS 2001
Notes 8: Concurrency
61
Warning Protocol
Hierarchically nesting elements (e.g. relation/block/tuple) can be locked with intention locks IS and IX Idea
start locking at the root (relation) level to place an S or X lock on a subelement, first place a corresponding intention lock IS or IX the element itself
Warns others: "I'll be reading/writing some subelement of this element"
DBMS 2001 Notes 8: Concurrency 62
R1 t1 t2
T1(S)
t3
t4
DBMS 2001
Notes 8: Concurrency
63
R1 t1 t2
T1(S)
t3
t4
T2(X)
DBMS 2001
Notes 8: Concurrency
64
IS T Holder IX T S T X F
DBMS 2001
Notes 8: Concurrency
65
Parent locked in IS IX S X
P C
DBMS 2001
Notes 8: Concurrency
66
Rules
(1) Follow multiple granularity comp function (2) Lock root of tree first, any mode (3) Node Q can be locked by Ti in S or IS only if parent(Q) can be locked by Ti in IX or IS (4) Node Q can be locked by Ti in X,IX only if parent(Q) locked by Ti in IX (5) Ti is two-phase (6) Ti can unlock node Q only if none of Q s children are locked by Ti
DBMS 2001 Notes 8: Concurrency 67
Exercise:
Can T2 write element f2.2? What locks will T2 get?
T1(IX)
R1 t3 f3.1 f3.2
68
t1
T1(IX) t2 T1(X) f2.1
DBMS 2001
t4
f2.2
Notes 8: Concurrency
Exercise:
Can T2 write element f2.2? What locks will T2 get?
T1(IX)
R1 t3 f3.1 f3.2
69
t1
T1(X)
t2 f2.2
t4
f2.1
DBMS 2001
Notes 8: Concurrency
Exercise:
Can T2 write element f3.1? What locks will T2 get?
T1(IS)
R1 t3 f3.1 f3.2
70
t1
T1(S)
t2 f2.2
t4
f2.1
DBMS 2001
Notes 8: Concurrency
Exercise:
Can T2 read element f2.2? What locks will T2 get?
T1(S,IX)
R1 t3 f3.1 f3.2
71
t1
T1(IX) t2 T1(X) f2.1
DBMS 2001
t4
f2.2
Notes 8: Concurrency
Exercise:
Can T2 write element f2.2? What locks will T2 get?
T1(S,IX)
R1 t3 f3.1 f3.2
72
t1
T1(IX) t2 T1(X) f2.1
DBMS 2001
t4
f2.2
Notes 8: Concurrency
Phantom tuples:
tuples that should have been locked, but did not exist when the locks were taken
DBMS 2001 Notes 8: Concurrency 73
Phantom tuples
Example: relation R (E#,name, ) constraint: E# is key use tuple locking R t1 t2
DBMS 2001
FInit G W
Notes 8: Concurrency
74
T1: Insert <99,Gore,A, > into R T2: Insert <99,Bush,G, > into R
T1 l-S1(t1) l- S1(t2) Check Constraint Insert [99,Gore,A,..] Insert [99,Bush,G,..]
DBMS 2001 Notes 8: Concurrency 75
T2
l-S2(t1) l-S2(t2) Check Constraint ...
...
Solution
Use multiple granularity tree Before insert of node Q, lock parent(Q) in R1 X mode t1 t2 t3
DBMS 2001
Notes 8: Concurrency
76
Back to example
T1: Insert<99,Gore,A> T1 l-X1(R) T2: Insert<99,Bush,G> T2 L-X2(R) Check constraint Insert<99,Gore,A> u1(R) l-X2(R) Check constraint Oops! e# = 99 already in R!
delayed
DBMS 2001
Notes 8: Concurrency
77
Validation
Lock-based concurrency control is pessimistic: non-serializable schedules are prevented in advance Another, optimistic strategy: allow transaction Ti access data without locks, but record elements read or written by Ti (in read and write sets RS(Ti) and WS(Ti)) at the end validate that the actions correspond to some serial schedule
DBMS 2001 Notes 8: Concurrency 78
Validation
Transactions have 3 phases: (1) Read
all accessed DB elements read writes to temporary storage (no locking)
(2) Validate
check if schedule so far is serializable; if yes, then ...
(3) Write
write updated elements to DB
DBMS 2001 Notes 8: Concurrency 79
Key idea
Make validation an atomic operation
i.e., validate a single transaction at a time
If T1, T2, T3, is validation order, then resulting schedule will be conflict equivalent to Ss = T1 T2 T3...
DBMS 2001
Notes 8: Concurrency
80
To implement validation, system maintains two sets of transactions: FIN = transactions that have finished phase 3 (writing, and are completed) VAL = transactions that have successfully finished phase 2 (validation), but not yet completed
DBMS 2001
Notes 8: Concurrency
81
T3
start
T2
validated
T3
validating
T2 may have written B after T3 read B, contradicting the assumed serial order (T2, T3) " T3 is rolled back
DBMS 2001 Notes 8: Concurrency
time
82
allow Example of what validation must prevent: RS(T2)={B} RS(T3)={A, B} WS(T2)={B,D} { J WS(T3)={C}
T2
start
T3
start
T2
validated
T3
validated
T2 finish phase 3
T3
start
time
DBMS 2001
Notes 8: Concurrency
83
T3
validating finish
T2
time
DBMS 2001
Notes 8: Concurrency
84
allow Another thing validation must prevent: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C, D} {J
T2
validated finish
T3
validated finish
T2
T2
time
DBMS 2001
Notes 8: Concurrency
85
W: RS(W)={A,D} WS(W)={A,C}
T: RS(T)={A,B} WS(T)={A,C}
DBMS 2001
V: RS(V)={B} WS(V)={D,E}
88
Notes 8: Concurrency
DBMS 2001
Notes 8: Concurrency
89
Summary
Have studied C.C. mechanisms used in practice - 2 PL - Multiple granularity - Validation
DBMS 2001
Notes 8: Concurrency
90