1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner
2 Schedule D T1T2 Read(A); A A+100 Write(A); Read(A);A A 2; Write(A); Read(B);B B 2; Write(B); Read(B); B B+100; Write(B); AB
3 Now for Sd: Sd=r 1 (A)w 1 (A)r 2 (A)w 2 (A) r 2 (B)w 2 (B)r 1 (B)w 1 (B) Sd=r 1 (A)w 1 (A) r 1 (B)w 1 (B)r 2 (A)w 2 (A)r 2 (B)w 2 (B) T1 T2
4 Or, let’s try for Sd: Sd=r 1 (A)w 1 (A)r 2 (A)w 2 (A) r 2 (B)w 2 (B)r 1 (B)w 1 (B) Sd=r 2 (A)w 2 (A)r 2 (B)w 2 (B) r 1 (A)w 1 (A)r 1 (B)w 1 (B) T1 T2
5 For Schedule D: Sd=r 1 (A)w 1 (A)r 2 (A)w 2 (A) r 2 (B)w 2 (B)r 1 (B)w 1 (B) T 1 T 2 T 2 T 1 T 1 T 2 D cannot be rearranged into serial schedule
6 Idea: Swap non-conflicting operation pairs to see if you can go to a serial schedule.
7 Definition S 1, S 2 are conflict equivalent schedules if S 1 can be transformed into S 2 by a series of swaps on non-conflicting actions.
8 Definition A schedule is conflict serializable if it is conflict equivalent to some (any) serial schedule.
9 One Answer: A Precedence Graph ! How determine this ?
10 Nodes: transactions in S Arcs: Ti Tj whenever - p i (A), q j (A) are actions in S - p i (A) < S q j (A) - at least one of p i, q j is a write Precedence graph P(S) (S is schedule )
11 Exercise: What is P(S) for S = w 3 (A) w 2 (C) r 1 (A) w 1 (B) r 1 (C) w 2 (A) r 4 (A) w 4 (D) Is S conflict serializable?
12 Another Exercise: What is P(S) for S = w 1 (A) r 2 (A) r 3 (A) w 4 (A) ? Is S conflict serializable?
13 Lemma S 1, S 2 conflict equivalent P(S 1 )=P(S 2 ) Proof: Assume P(S 1 ) P(S 2 ) T i : T i T j in S 1 and not in S 2 S 1 = …p i (A)... q j (A)… p i, q j S 2 = …q j (A)…p i (A)... conflict S 1, S 2 not conflict equivalent
14 Counter example: S 1 =w 1 (A) r 2 (A) w 2 (B) r 1 (B) S 2 =r 2 (A) w 1 (A) r 1 (B) w 2 (B) P(S 1 )=P(S 2 ) S 1, S 2 conflict equivalent ?? Note: P(S 1 )=P(S 2 ) S 1, S 2 conflict equivalent
15 Theorem P(S 1 ) acyclic S 1 conflict serializable ( ) Assume S 1 is conflict serializable S s : S s, S 1 conflict equivalent P(S s ) = P(S 1 ) P(S 1 ) acyclic since P(S s ) is acyclic
16 ( ) Assume P(S 1 ) is acyclic Transform S 1 as follows: (1) Take T 1 to be transaction with no incident arcs (2) Move all T 1 actions to the front S 1 = ……. q j (A)……. p 1 (A)….. (3) we now have S 1 = (4) repeat above steps to serialize rest! T 1 T 2 T 3 T 4 Theorem P(S 1 ) acyclic S 1 conflict serializable
17 How to enforce serializable schedules?
18 How to enforce serializable schedules? Option 1 : try all possible swaps of non-conflicting operation pairs to determine if the schedule can be turned into a serial one, i.e., if the schedule is ‘good’.
19 How to enforce serializable schedules? Option 2: a) run system, recording P(S); b) at end of day, check for P(S) cycles c) and declare if execution was good
20 Option 3: prevent P(S) cycles from occurring T 1 T 2 …..T n Scheduler DB How to enforce serializable schedules?
21 A locking protocol Two new actions: lock (exclusive):l i (A) unlock:u i (A) scheduler T 1 T 2 lock table
22 Rule #1: Well-formed transactions T i : … l i (A) … p i (A) … u i (A)...
23 Rule #2 Legal scheduler S = …….. l i (A) ………... u i (A) ……... no l j (A)
24 What schedules are legal? What transactions are well-formed? S1 = l 1 (A)l 1 (B)r 1 (A)w 1 (B)l 2 (B)u 1 (A)u 1 (B) r 2 (B)w 2 (B)u 2 (B)l 3 (B)r 3 (B)u 3 (B) S2 = l 1 (A)r 1 (A)w 1 (B)u 1 (A)u 1 (B) l 2 (B)r 2 (B)w 2 (B)l 3 (B)r 3 (B)u 3 (B) S3 = l 1 (A)r 1 (A)u 1 (A)l 1 (B)w 1 (B)u 1 (B) l 2 (B)r 2 (B)w 2 (B)u 2 (B)l 3 (B)r 3 (B)u 3 (B) Exercise:
25 What schedules are legal? What transactions are well-formed? S1 = l 1 (A)l 1 (B)r 1 (A)w 1 (B)l 2 (B)u 1 (A)u 1 (B) r 2 (B)w 2 (B)u 2 (B)l 3 (B)r 3 (B)u 3 (B) S2 = l 1 (A)r 1 (A)w 1 (B)u 1 (A)u 1 (B) l 2 (B)r 2 (B)w 2 (B)l 3 (B)r 3 (B)u 3 (B) S3 = l 1 (A)r 1 (A)u 1 (A)l 1 (B)w 1 (B)u 1 (B) l 2 (B)r 2 (B)w 2 (B)u 2 (B)l 3 (B)r 3 (B)u 3 (B) Exercise:
26 Schedule F : Adding locking ! How? T1 T2 l 1 (A);Read(A) A A+100;Write(A);u 1 (A) l 2 (A);Read(A) A Ax2;Write(A);u 2 (A) l 2 (B);Read(B) B Bx2;Write(B);u 2 (B) l 1 (B);Read(B) B B+100;Write(B);u 1 (B) Legal ? Well-formed ?
27 Schedule F : Adding locking ! How? T1 T2 l 1 (A);Read(A) A A+100;Write(A);u 1 (A) l 2 (A);Read(A) A Ax2;Write(A);u 2 (A) l 2 (B);Read(B) B Bx2;Write(B);u 2 (B) l 1 (B);Read(B) B B+100;Write(B);u 1 (B) AB
28 Rule #3 Two phase locking (2PL) for transactions T i = ……. l i (A) ………... u i (A) ……... no unlocks no locks
29 # locks held by Ti Time Growing Shrinking Phase Phase 2 Phase Locking Protocol
30 Schedule F : Can 2PL fix it ? T1 T2 l 1 (A);Read(A) A A+100;Write(A);u 1 (A) l 2 (A);Read(A) A Ax2;Write(A);u 2 (A) l 2 (B);Read(B) B Bx2;Write(B);u 2 (B) l 1 (B);Read(B) B B+100;Write(B);u 1 (B)
31 Schedule G T1 T2 l 1 (A);Read(A) A A+100;Write(A) l 1 (B); u 1 (A) l 2 (A);Read(A) A Ax2;Write(A);
32 Schedule G delayed
33 Schedule G delayed
34 Schedule G delayed
35 We Got the GOOD Schedule !!!! T1T2 Read(A); A A+100 Write(A); Read(A);A A 2; Write(A); Read(B); B B+100; Write(B); Read(B);B B 2; Write(B); AB
36 Schedule H (T 2 reversed) What’s happening ?
37 Schedule H (T 2 reversed) delayed
38 Deadlocked transactions can arise in ‘good’ schedules (generated due to 2PL locking). We assume that when they are rolled back: –They have no effect –They do not appear in schedule E.g., Schedule H = This space intentionally left blank!
39 Correctness of 2PC Protocol : Show that rules #1,2,3 Conflict-serializable schedules
40 Conflict rules for l i (A), u i (A): l i (A), l j (A) conflict l i (A), u j (A) conflict
41 Theorem Rules #1,2,3 conflict (2PL) serializable schedule To help in proof: Definition Shrink(Ti) = SH(Ti) = first unlock action of Ti
42 Lemma Ti Tj in S SH(Ti) < S SH(Tj) Proof of lemma: Ti Tj means that S = … p i (A) … q j (A) …; p,q conflict By rules 1,2: S = … p i (A) … u i (A) … l j (A)... q j (A) … SH(Ti) SH(Tj) By rule 3: So, SH(Ti) < S SH(Tj)
43 Proof: (1) Assume P(S) has cycle T 1 T 2 …. T n T 1 (2) By lemma: SH(T 1 ) < SH(T 2 ) <... < SH(T 1 ) (3) Impossible, so P(S) acyclic (4) S is conflict serializable Theorem Rules #1,2,3 conflict (2PL) serializable schedule
44 Beyond this simple 2PL protocol, it is all a matter of improving performance and allowing yet more concurrency…. –Shared locks –Multiple granularity –Inserts, deletes and phantoms –Other types of C.C. mechanisms