Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs4432concurrency control1 CS4432: Database Systems II Concurrency Control.

Similar presentations


Presentation on theme: "Cs4432concurrency control1 CS4432: Database Systems II Concurrency Control."— Presentation transcript:

1 cs4432concurrency control1 CS4432: Database Systems II Concurrency Control

2 cs4432concurrency control2 Chapter 18Concurrency Control T1T2…Tn DB (consistency constraints)

3 cs4432concurrency control3 Example: T1:Read(A)T2:Read(A) A  A+100A  A  2Write(A)Read(B) B  B+100B  B  2Write(B) Constraint: A=B

4 cs4432concurrency control4 A schedule An ordering of operations inside one or more transactions over time Why interleave operations?

5 cs4432concurrency control5 Schedule A T1T2 Read(A); A  A+100 Write(A); Read(B); B  B+100; Write(B); Read(A);A  A  2; Write(A); Read(B);B  B  2; Write(B); AB25 125 250 250

6 cs4432concurrency control6 Schedule B T1T2 Read(A);A  A  2; Write(A); Read(B);B  B  2; Write(B); Read(A); A  A+100 Write(A); Read(B); B  B+100; Write(B); AB25 50 150 150

7 cs4432concurrency control7 Serial Schedules ! Any serial schedule is “good”.

8 cs4432concurrency control8 Interleave Transactions in a Schedule?

9 cs4432concurrency control9 Schedule C 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); AB25 125 250 125 250250

10 cs4432concurrency control10 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); AB25 125 250 50 150 250150

11 cs4432concurrency control11 Schedule E T1T2’ Read(A); A  A+100 Write(A); Read(A);A  A  1; Write(A); Read(B);B  B  1; Write(B); Read(B); B  B+100; Write(B); AB25 125 25 125125 Same as Schedule D but with new T2’

12 cs4432concurrency control12 What is a ‘good’ schedule?

13 cs4432concurrency control13 We want schedules that are “good” regardless of : initial state and transaction semantics Hence we consider only : – order of read/writes Example: Sc=r 1 (A)w 1 (A)r 2 (A)w 2 (A)r 1 (B)w 1 (B)r 2 (B)w 2 (B)

14 cs4432concurrency control14 Remember Schedule C 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);

15 cs4432concurrency control15 Sc’=r 1 (A)w 1 (A) r 1 (B)w 1 (B)r 2 (A)w 2 (A)r 2 (B)w 2 (B) T 1 T 2 Example: Sc=r 1 (A)w 1 (A)r 2 (A)w 2 (A)r 1 (B)w 1 (B)r 2 (B)w 2 (B)

16 cs4432concurrency control16 Now Let’s Try that for 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);

17 cs4432concurrency control17 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)

18 cs4432concurrency control18 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)

19 cs4432concurrency control19 In short, Schedule D cannot be “fixed” : 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) as a matter of fact, there seems to be no safe way to transform this Sd into an equivalent serial schedule !?

20 cs4432concurrency control20 Sd cannot be rearranged into a serial schedule Sd is not “equivalent” to any serial schedule Sd is “bad” We note about schedule D: T 2  T 1 T 1  T 2 T 1 T 2

21 cs4432concurrency control21 Returning to Sc Sc=r 1 (A)w 1 (A)r 2 (A)w 2 (A)r 1 (B)w 1 (B)r 2 (B)w 2 (B) T 1  T 2 T 1  T 2  no cycles  Sc is “equivalent” to a serial schedule, I.e., in this case (T 1,T 2 ).

22 cs4432concurrency control22 Concepts Transaction: sequence of r i (x), w i (x) actions Conflicting actions: read/write on same resource A: r 1(A) w 2(A) w 1(A) w 2(A) r 1(A) w 2(A) Schedule: represents chronological order in which actions are executed Serial schedule: no interleaving of trans/actions

23 Anomalies with Interleaving Reading Uncommitted Data (WR Conflicts, “dirty reads”): e.g. T1: A+100, B+100, T2: A*1.06, B*1.06 T1: R(A), W(A), R(B), W(B), Abort T2:R(A), W(A), C T1:R(A), R(A), W(A), C T2:R(A), W(A), C Unrepeatable Reads (RW Conflicts): E.g., T1: R(A), check if A >0, decrement, T2: R(A), decrement Overwriting Uncommitted Data (WW Conflicts): T1:W(A), W(B), C T2:W(A), W(B), C

24 cs4432concurrency control24 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.

25 cs4432concurrency control25 Definition A schedule is conflict serializable if it is conflict equivalent to some serial schedule.

26 cs4432concurrency control26 Answer: A Precedence Graph ! How determine this ?

27 cs4432concurrency control27 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 )

28 cs4432concurrency control28 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?

29 cs4432concurrency control29 Another Exercise: What is P(S) for S = w 1 (A) r 2 (A) r 3 (A) w 4 (A) ? Is S conflict-serializable?

30 cs4432concurrency control30 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

31 cs4432concurrency control31 Note: P(S 1 )=P(S 2 )  S 1, S 2 conflict equivalent 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)

32 cs4432concurrency control32 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

33 cs4432concurrency control33 (  ) 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

34 cs4432concurrency control34 How to enforce serializable schedules?

35 cs4432concurrency control35 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’.

36 cs4432concurrency control36 How to enforce serializable schedules? Option 2: run system, recording P(S); at end of day, check for P(S) cycles and declare if execution was good

37 cs4432concurrency control37 Option 3: prevent P(S) cycles from occurring T 1 T 2 …..T n Scheduler DB How to enforce serializable schedules?

38 cs4432concurrency control38 A locking protocol Two new actions: lock (exclusive):l i (A) unlock:u i (A) scheduler T 1 T 2 lock table

39 cs4432concurrency control39 Rule #1: Well-formed transactions T i : … l i (A) … p i (A) … u i (A)...

40 cs4432concurrency control40 Rule #2 Legal scheduler S = …….. l i (A) ………... u i (A) ……... no l j (A)

41 cs4432concurrency control41 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:

42 cs4432concurrency control42 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:

43 cs4432concurrency control43 Schedule F : Adding locking ??? 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)

44 cs4432concurrency control44 Schedule F T1 T2 25 25 l 1 (A);Read(A) A A+100;Write(A);u 1 (A) 125 l 2 (A);Read(A) A Ax2;Write(A);u 2 (A) 250 l 2 (B);Read(B) B Bx2;Write(B);u 2 (B) 50 l 1 (B);Read(B) B B+100;Write(B);u 1 (B) 150 250 150 A B

45 cs4432concurrency control45 Rule #3 Two phase locking (2PL) for transactions T i = ……. l i (A) ………... u i (A) ……... no unlocks no locks

46 cs4432concurrency control46 # locks held by Ti Time Growing Shrinking Phase Phase

47 cs4432concurrency control47 Schedule F : Does it follow 2PL ? 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)

48 cs4432concurrency control48 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);

49 cs4432concurrency control49 Schedule G delayed

50 cs4432concurrency control50 Schedule G delayed

51 cs4432concurrency control51 Schedule G delayed

52 cs4432concurrency control52 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); AB25 125 250 125 250250

53 cs4432concurrency control53 Schedule H (T 2 reversed) T1T2 l 1 (A); Read(A) l 2 (B);Read(B) A A+100;Write(A) B Bx2;Write(B) …

54 cs4432concurrency control54 Schedule H (T 2 reversed) delayed

55 cs4432concurrency control55 SUMMARY: –2PL is commonly used solution for concurrency NOTE: –But 2PL does not prevent deadlocks NEXT: –What are we going to do about deadlocks?


Download ppt "Cs4432concurrency control1 CS4432: Database Systems II Concurrency Control."

Similar presentations


Ads by Google