Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 2: Enforcing Serializable Schedules Professor Chen Li.

Similar presentations


Presentation on theme: "1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 2: Enforcing Serializable Schedules Professor Chen Li."— Presentation transcript:

1 1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 2: Enforcing Serializable Schedules Professor Chen Li

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

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

4 ICS214BNotes 024 A locking protocol Two new actions: lock (exclusive):l i (A) unlock:u i (A) scheduler T 1 T 2 lock table

5 ICS214BNotes 025 Rule #1: Well-formed transactions T i : … l i (A) … p i (A) … u i (A)... 1) Lock an element before accessing it 2) Unlock the element after accessing it

6 ICS214BNotes 026 Rule #2 Legal scheduler S = …….. l i (A) ………... u i (A) ……... Once an element A is locked by one transaction Ti, the schedule will not grant any other lock requests on A no l j (A)

7 ICS214BNotes 027 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

8 ICS214BNotes 028 Another problem: schedule F is still allowed, but it’s bad! T1 T 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) A B

9 ICS214BNotes 029 Rule #3 Two phase locking (2PL) T i = ……. l i (A) ………... u i (A) ……... A transaction once releases a lock, it cannot request more locks. no unlocks no locks

10 ICS214BNotes 0210 # locks held by Ti Time Growing Shrinking Phase Phase

11 ICS214BNotes 0211 Schedule G request delayed

12 ICS214BNotes 0212 Schedule G request delayed

13 ICS214BNotes 0213 Schedule G request delayed

14 ICS214BNotes 0214 Schedule H (T 2 reversed) delayed

15 ICS214BNotes 0215 Assume deadlocked transactions are rolled back –They have no effect –They do not appear in schedule Solution

16 ICS214BNotes 0216 Next step: Show that rules #1,2,3  conflict- serializable schedules

17 ICS214BNotes 0217 New Conflict rules for l i (A), u i (A): Check the semantics of the operations, e.g., –l i (A), u j (A) conflict –l i (A), ri(A) conflict; l i (A), wi(A) conflict –ri(A), ui(A) conflict; wi(A), ui(A) conflict Some actions can never become adjacent after legal swaps –,,... Example: l1(A)r1(A)l1(B)u1(A)l2(A)w2(A)w1(B)u2(A)u1(B)

18 ICS214BNotes 0218 Theorem Rules #1,2,3  conflict (2PL) serializable schedule To help in proof: Definition Shrink(Ti) = SH(Ti) = first unlock action of Ti

19 ICS214BNotes 0219 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: ui(A) < S lj(A) S = … p i (A) … u i (A) … l j (A)... q j (A) … By rule 3: SH(Ti) SH(Tj) So, SH(Ti) < S SH(Tj)

20 ICS214BNotes 0220 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

21 ICS214BNotes 0221 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 concurrency control mechanisms

22 ICS214BNotes 0222 Shared locks S =...l 1 (A) r 1 (A) u 1 (A) … l 2 (A) r 2 (A) u 2 (A) … read only, no conflict Use “shared” lock: S=... ls 1 (A) r 1 (A) ls 2 (A) r 2 (A) …. us 1 (A) us 2 (A)

23 ICS214BNotes 0223 Lock actions l-t i (A): lock A in t mode (t is S or X) u-t i (A): unlock t mode (t is S or X) “S” = “Shared”, “X” = “Exclusive” Shorthand: u i (A): unlock whatever modes T i has locked A

24 ICS214BNotes 0224 Rule #1 Well formed transactions T i =... l-S 1 (A) … r 1 (A) … u 1 (A) … T i =... l-X 1 (A) … w 1 (A) … u 1 (A) … Lock an element before accessing it Release the lock afterwards

25 ICS214BNotes 0225 What about transactions that read and write same object? Option 1: Request exclusive lock T i =...l-X 1 (A) … r 1 (A)... w 1 (A)... u(A) …

26 ICS214BNotes 0226 Option 2: Upgrade (E.g., need to read, but don’t know if will write…) T i =... l-S 1 (A) … r 1 (A)... l-X 1 (A) …w 1 (A)...u(A)… Think of - Get 2nd lock on A, or - Drop S, get X lock

27 ICS214BNotes 0227 Rule #2 Legal scheduler S =....l-S i (A) … … u i (A) … l-S j (A) OK, but no l-X j (A) S =... l-X i (A) … … u i (A) … no l-X j (A) no l-S j (A)

28 ICS214BNotes 0228 A way to summarize Rule #2 Compatibility matrix: “comp” S X S true false Xfalse false

29 ICS214BNotes 0229 Rule # 3 2PL transactions No change: Locking must precede unlocking, i.e., No action sli(X) or xli(X) can be preceded by ui(X)

30 ICS214BNotes 0230 Theorem Rules 1,2,3  Conf.serializable for S/X locks schedules


Download ppt "1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 2: Enforcing Serializable Schedules Professor Chen Li."

Similar presentations


Ads by Google