11 UW Concurrency
11 UW Concurrency
:
Database System Principles
1
Concepts
2
What about concurrent actions?
4
What about conflicting, concurrent
actions on same object?
start r1(A) end r1(A)
6
Example:
r1(A); w1(A); r2(A); w2(A); r1(B); w1(B); r2(B); w2(B);
7
• Conflict-serializability is a sufficient condition
for serializability i.e., a conflict-serializable
schedule is a serializable schedule.
8
Precedence graph P(S) (S is schedule)
9
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?
10
Another Exercise:
• What is P(S) for
S = w1(A) r2(A) r3(A) w4(A) ?
11
Lemma
S1, S2 conflict equivalent P(S1)=P(S2)
Proof:
Assume P(S1) P(S2)
Ti: Ti Tj in S1 and not in S2
S1 = …pi(A)... qj(A)… pi, qj
S2 = …qj(A)…pi(A)... conflict
Counter example:
13
Theorem
P(S1) acyclic S1 conflict serializable
14
Theorem
P(S1) acyclic S1 conflict serializable
T1
() Assume P(S1) is acyclic T2 T3
Transform S1 as follows:
(1) Take T1 to be transaction with no incident arcs
T4
(2) Move all T1 actions to the front
S1 = ……. qj(A)…….p1(A)…..
16
How to enforce serializable schedules?
DB
17
A locking protocol
18
Rule #1: Consistency of transactions
19
Rule #2 Legality of schedules
no lj(A)
Locks must have their intended meaning:
no two transactions may have locked the
same element without one having first
released the lock.
20
Exercise:
• What schedules are legal?
What transactions are consistent?
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)
21
Exercise:
• What schedules are legal?
What transactions are consistent?
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) (S1: not legal )
S2 = l1(A)r1(A)w1(B)u1(A)u1(B) (T1: not consistent)
l2(B)r2(B)w2(B)l3(B)r3(B)u3(B) (S2: not legal )
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)
(S3: legal schedule, T1,T2,T3: consistent transactions)
22
Schedule F (legal schedule of consistent transactions)
(still not equivalent to a serial schedule)
A B
T1 T2 25
25
l1(A);Read(A)
A A+100;Write(A);u1(A) 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 23
250
Rule #3 Two phase locking (2PL)
for transactions
no unlocks no locks
24
# locks
held by
Ti
Time
Growing Shrinking
Phase Phase
25
Schedule G
T1 T2
l1(A);Read(A)
A A+100;Write(A)
l1(B); u1(A)
delayed
l2(A);Read(A)
A Ax2;Write(A);l2(B)
26
Schedule G
T1 T2
l1(A);Read(A)
A A+100;Write(A)
l1(B); u1(A)
delayed
l2(A);Read(A)
A Ax2;Write(A);l2(B)
Read(B);B B+100
Write(B); u1(B)
27
Schedule G
T1 T2
l1(A);Read(A)
A A+100;Write(A)
l1(B); u1(A)
l2(A);Read(A) delayed
A Ax2;Write(A);l2(B)
Read(B);B B+100
Write(B); u1(B)
l2(B); u2(A);Read(B)
B Bx2;Write(B);u2(B);
28
Schedule G ”good” (equivalent to a serial)
A B
T1 T2 25
25
l1(A);Read(A); A A+100
Write(A); l1(B);u1(A) 125
l2(A);Read(A)
A Ax2;Write(A);
250
l2(B); delayed
Read(B);
B B+100;Write(B);u1(B)
125
l2(B);u2(A);Read(B) 29
B Bx2;Write(B);u2(B) 250
Theorem Rules #1,2,3 conflict
(consistency, legality, 2PL) serializable
schedule
30
Theorem Rules #1,2,3 conflict
(consistency, legality, 2PL) serializable
schedule
31
Theorem Rules #1,2,3 conflict
serializable
schedule
Proof:
BASIS: If n = 1, there is nothing to do; S is already a serial
schedule.
INDUCTION: Suppose S involves n transactions
T1,T2, ... , Tn, and let Ti be the transaction with the first
unlock action in the entire schedule S, say ui (x) .
We claim it is possible to move all the read and write
actions of Ti forward to the beginning of the schedule
without passing any conflicting reads or writes.
32
Theorem Rules #1,2,3 conflict
(2PL) serializable
schedule
To help in proof:
Definition Shrink(Ti) = SH(Ti) =
first unlock action
of Ti
33
Lemma
Ti Tj in S SH(Ti) <S SH(Tj)
Proof of lemma:
Ti Tj means that
S = … pi(A) … qj(A) …; p,q conflict
By rules 1,2:
S = … pi(A) … ui(A) … lj(A) ... qj(A) …
35
Schedule H (T2 reversed)
T1 T2
l1(A); Read(A) l2(B);Read(B)
A A+100;Write(A) B Bx2;Write(B)
l1(B) l2(A)
delayed delayed
Deadlock !!!
36
• Assume deadlocked transactions are
rolled back
– They have no effect
– They do not appear in schedule
E.g., Schedule H =
This space intentionally
left blank!
37
Deadlocks
• Detection
– Wait-for graph
• Prevention
– Resource ordering
– Timeout
– Wait-die
– Wound-wait
38
Deadlock Detection
• Build Wait-For graph (Ti -> Tj edge if Ti waits for Tj)
• Use lock table structures
• Build incrementally or periodically
• When cycle found, rollback victim
T1 T2 T5
T7
T4 T3 T6
39
Resource Ordering (prevention)
40
Timeout
• If transaction waits more than L sec.,
roll it back!
• Simple scheme
• Hard to select L
41
2PL subset of Serializable
(schedules that can be implemented by 2PL locks are subset of serializable schedules)
Serializable
2PL
42
S1: w1(x) w3(x) w2(y) w1(y)
43
If you need a bit more practice:
Are our schedules SC and SD 2PL schedules?
44
• 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
45
Shared locks
So far:
S = ...l1(A) r1(A) u1(A) … l2(A) r2(A) u2(A) …
Do not conflict
46
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)
47
Lock actions
l-ti(A): lock A in t mode (t is S or X)
u-ti(A): unlock t mode (t is S or X)
Shorthand:
ui(A): unlock whatever modes
Ti has locked A
48
Rule #1 Consistency of transactions
Ti =... l-S1(A) … r1(A) … u1 (A) …
Ti =... l-X1(A) … w1(A) … u1 (A) …
Legality of schedules:
An element may either be locked exclusively
by one transaction or by several in shared
mode, but not both.
50
• What about transactions that read and
write same object?
51
• What about transactions that read and
write same object?
Option 2: Upgrade
(E.g., need to read, but don’t know if it will write…)
Think of
- Get 2nd lock on A, or
- Drop S, get X lock
52
Rule #2 Legal scheduler
S = ....l-Si(A) … … ui(A) …
no l-Xj(A)
no l-Xj(A)
no l-Sj(A)
53
A way to summarize Rule #2
Compatibility matrix
Comp S X
S true false
X false false
54
A way to summarize Rule #2
Compatibility matrix
Comp S X
S true false
X false false
Detail:
l-ti(A), l-rj(A) (l-t and l-r can be a lock mode)
do not conflict if comp(t,r) = true
57
Lock types beyond S/X
Examples:
(1) increment lock
(2) update lock
58