Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jinze Liu. Option 1: run system, recording P(S); at end of day, check for P(S) cycles and declare if execution was good.

Similar presentations


Presentation on theme: "Jinze Liu. Option 1: run system, recording P(S); at end of day, check for P(S) cycles and declare if execution was good."— Presentation transcript:

1 Jinze Liu

2 Option 1: run system, recording P(S); at end of day, check for P(S) cycles and declare if execution was good

3 Option 2: prevent P(S) cycles from occurring T 1 T 2 …..T n Scheduler DB

4 Two new actions: lock (exclusive):l i (A) unlock:u i (A) scheduler T1 T2 Lock table

5 T i : … l i (A) … p i (A) … u i (A)...

6 S = …….. l i (A) ………... u i (A) ……... no l j (A)

7 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  CS 245  Notes 09 88 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:

9  Schedule F 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)

10 Schedule without two phase locking 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

11 T i = ……. l i (A) ………u i (A) ……... no unlocks no locks

12 # locks held by Ti Time Growing Shrinking Phase Phase

13 delayed

14 Schedule with two phase locking delayed

15 Schedule with two phase locking delayed

16

17 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!

18 Beyond this simple 2PL protocol, it is all a matter of improving performance and allowing more concurrency…. Multiple granularity Inserts, deletes and phantoms Other types of C.C. mechanisms

19 Examples: (1) increment lock (2) update lock

20 Atomic increment action: IN i (A) {Read(A); A  A+k; Write(A)} IN i (A), IN j (A) do not conflict! A=7 A=5A=17 IN i (A) +2 IN j (A) +10

21 Atomic increment action: IN i (A) {Read(A); A  A+k; Write(A)} IN i (A), IN j (A) do not conflict! A=7 A=5A=17 A=15 IN i (A) +2 IN j (A) +10 IN j (A) +2 IN i (A)

22 SXI STFF XFFF IFFT

23

24 If T i wants to read A and knows it may later want to write A, it requests update lock (not shared)

25 Comp SXU S X U Lock already held in

26 SXU STFT XFFF U T or FFF -> symmetric table? Lock already held in

27 Note: object A may be locked in different modes at the same time... S 1 =...l-S 1 (A)…l-S 2 (A)…l-U 3 (A)… l-S 4 (A)…? l-U 4 (A)…? To grant a lock in mode t, mode t must be compatible with all currently held locks on object

28 Every system is different (E.g., may not even provide CONFLICT-SERIALIZABLE schedules) But here is one (simplified) way...

29 (1) Don’t trust transactions to request/release locks (2) Hold all locks until transaction commits # locks time

30 Ti Read(A),Write(B) l(A),Read(A),l(B),Write(B)… Read(A),Write(B) Scheduler, part I Scheduler, part II DB Lock table

31 A  B C  Lock info for B Lock info for C If null, object is unlocked Every possible object

32 A If object not found in hash table, it is unlocked Lock info for A A..... H

33 tran mode wait? Nxt T_link Object:A Group mode:U Waiting:yes List: T1Sno T2Uno T3Xyes  To other T3 records

34 ? Relation A Relation B... Tuple A Tuple B Tuple C... Disk Block A Disk Block B... DB

35 Locking works in any case, but should we choose small or large objects? If we lock large objects (e.g., Relations) –Need few locks –Low concurrency If we lock small objects (e.g., tuples,fields) –Need more locks –More concurrency

36

37 Select * From Movie Where title = ‘King Kong’ Update Movie Set year = 1939 Where title = ‘Gone with the wind’

38 R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (S)

39 R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (IX) T 2 (IX)

40 Comp Requestor IS IX S SIX X IS Holder IX S SIX X TTTTF F F F FFFFF FFFT FTFT FFTT

41 ParentChild can belocked in IS IX S SIX X PP CC

42 ParentChild can be locked locked inby same transaction in IS IX S SIX X P C IS, S IS, S, IX, X, SIX [S, IS] not necessary X, IX, [SIX] none

43 (1) Follow multiple granularity comp function (2) Lock root of tree first, any mode (3) Node Q can be locked by T i in S or IS only if parent(Q) locked by Ti in IX or IS (4) Node Q can be locked by T i in X,SIX,IX only if parent(Q) locked by T i in IX,SIX (5) T i is two-phase (6) T i can unlock node Q only if none of Q’s children are locked by T i

44 Can T 2 access object f 2.2 in X mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (IX) f 2.1 f 2.2 f 3.1 f 3.2  =T 1 (IX) T 1 (X)

45 Can T 2 access object f 2.2 in X mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (IX) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (SIX) T 1 (X)

46 Insert A Z ...

47 (1) Get exclusive lock on A before deleting A (2) At insert A operation by T i, T i is given exclusive lock on A

48 Example: relation R (E#,name,…) constraint: E# is key use tuple locking RE#Name…. o155Smith o275Jones

49 T1 T2 S1(o1) S2(o1) S1(o2) S2(o2) Check Constraint Insert [08,Obama,..] Insert [08,Mccain,..]

50 Use multiple granularity tree Before insert of node Q, lock parent(Q) in X mode R1 t1t1 t2t2 t3t3

51 T 1 : Insert T 2 : Insert T 1 T 2 X 1 (R) Check constraint Insert U(R) X 2 (R) Check constraint Oops! e# = 08 already in R! delayed

52 Example: R Index 0<E#<100 Index 100<E#<200 E#=2E#=5 E#=107 E#=109...


Download ppt "Jinze Liu. Option 1: run system, recording P(S); at end of day, check for P(S) cycles and declare if execution was good."

Similar presentations


Ads by Google