Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme.

Similar presentations


Presentation on theme: "1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme."— Presentation transcript:

1 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

2 Database Implementation – Concurrency Control Yan Huang2 Learning Objectives Variations of two phase locking Dealing with Deadlock and Starvation Time Stamp Ordering Technique Validation

3 Database Implementation – Concurrency Control Yan Huang3 Schedules Interleaved (or non-interleaved) actions from several transactions A schedule is good if it can be transformed into one of the serial schedules by switching two consecutive non- conflict actions We’ve learned 2PL to achieve serializability It is a pessimistic scheme

4 Database Implementation – Concurrency Control Yan Huang4 Recall 2PL (two phase locking) Locks of Ti time growing shrinking Each transaction has to follow, no locking anymore after the first unlocking

5 Database Implementation – Concurrency Control Yan Huang5 Why 2PL serializability? Acyclic precedence graph = serializability Basic idea: No cycle in precedence graph Because if there is a arc from Ti to Tj in precedence graph, the first unlocking of Ti precede the first unlocking of Tj (proof details in ccI notes) If there is circle, you will have Ti < Ti

6 Database Implementation – Concurrency Control Yan Huang6 Who will follow 2PL in practice? Looks like it is DB application developers’ job. But, they can not be trusted and too much work Checking conformity of every transaction is costly In practice, CC subsystems of DBMS take over the responsibility

7 Database Implementation – Concurrency Control Yan Huang7 Variations of 2PL Basic 2PL Conservative 2PL Strict 2PL Rigorous 2PL

8 Database Implementation – Concurrency Control Yan Huang8 Basic 2PL 2PL with binary locks Covered in last class

9 Database Implementation – Concurrency Control Yan Huang9 Shared locks So far: S =...l 1 (A) r 1 (A) u 1 (A) … l 2 (A) r 2 (A) u 2 (A) … Do not conflict Instead: S=... ls 1 (A) r 1 (A) ls 2 (A) r 2 (A) …. us 1 (A) us 2 (A)

10 Database Implementation – Concurrency Control Yan Huang10 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) Shorthand: u i (A): unlock whatever modes T i has locked A

11 Database Implementation – Concurrency Control Yan Huang11 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) …

12 Database Implementation – Concurrency Control Yan Huang12 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 1 (A) …

13 Database Implementation – Concurrency Control Yan Huang13 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 1 (A)… Think of - Get 2nd lock on A, or - Drop S, get X lock What about transactions that read and write same object?

14 Database Implementation – Concurrency Control Yan Huang14 Compatibility matrix Comp S X S true false Xfalse false

15 Database Implementation – Concurrency Control Yan Huang15 Schedule delayed

16 Database Implementation – Concurrency Control Yan Huang16 Conservative 2PL Lock all items it needs then transaction starts execution If any locks can not be obtained, then do not lock anything Difficult but deadlock free growing shrinking locks time first action starts

17 Database Implementation – Concurrency Control Yan Huang17 Strict 2PL T does not release any write locks until it commits or aborts Good for recoverability Since reads or writes on what T writes Deadlock free? growing shrinking locks time T commits or aborts First write unlock

18 Database Implementation – Concurrency Control Yan Huang18 Rigorous 2PL T does not release any locks until it commits or aborts Easy to implement Deadlock free? growing shrinking locks time T commits or aborts

19 Database Implementation – Concurrency Control Yan Huang19 2PL Does basic 2PL guarantee serializability? Does conservative 2PL guarantee serializability? Does strict 2PL guarantee serializability? Does rigorous 2PL guarantee serializability?

20 Database Implementation – Concurrency Control Yan Huang20 Compare variations of 2PL Deadlock Only conservative 2PLis deadlock free Q: give a schedule of two transactions following 2PL but result in deadlock.

21 Database Implementation – Concurrency Control Yan Huang21 Exercises: S1: r1(y)r1(x)w1(x)w2(x)w2(y) S2: r1(y)r3(x)w1(x)w3(x)w2(y)w2(x) S3: r3(y)w1(x)w3(x)r1(z)w2(y)w2(x) Assuming binary lock right before read or write; and rigorous 2PL (release all locks right after last operation), are S1, S2,and S3 possible?

22 Database Implementation – Concurrency Control Yan Huang22 Deadlocks Detection Wait-for graph Prevention Resource ordering Timeout Wait-die Wound-wait

23 Database Implementation – Concurrency Control Yan Huang23 Deadlock Detection Build Wait-For graph Use lock table structures Build incrementally or periodically When cycle found, rollback victim T1T1 T3T3 T2T2 T6T6 T5T5 T4T4 T7T7

24 Database Implementation – Concurrency Control Yan Huang24 Resource Ordering Order all elements A 1, A 2, …, A n A transaction T can lock A i after A j only if i > j Problem : Ordered lock requests not realistic in most cases

25 Database Implementation – Concurrency Control Yan Huang25 Timeout If transaction waits more than L sec., roll it back! Simple scheme Hard to select L

26 Database Implementation – Concurrency Control Yan Huang26 Wait-die Transactions are given a timestamp when they arrive …. ts(T i ) T i can only wait for T j if ts(T i )< ts(T j )...else die

27 Database Implementation – Concurrency Control Yan Huang27 T 1 (ts =10) T 2 (ts =20) T 3 (ts =25) wait Example: wait? Very high level: only older ones have the privilege to wait, younger ones die if they attempt to wait for older ones

28 Database Implementation – Concurrency Control Yan Huang28 Wound-wait Transactions are given a timestamp when they arrive … ts(T i ) T i wounds T j if ts(T i )< ts(T j ) else T i waits “Wound”: T j rolls back and gives lock to T i

29 Database Implementation – Concurrency Control Yan Huang29 T 1 (ts =25) T 2 (ts =20) T 3 (ts =10) wait Example: wait Very high level: younger ones wait; older ones kill (wound) younger ones who hold needed locks

30 Database Implementation – Concurrency Control Yan Huang30 Who die? Looks like it is always the younger ones either die automatically or killed What is the reason? Will the younger ones starve? Suggestions?

31 Database Implementation – Concurrency Control Yan Huang31 Timestamp Ordering Key idea: Transactions access variables according to an order decided by their time stamps when they enter the system No cycles are possible in the precedence graph

32 Database Implementation – Concurrency Control Yan Huang32 Timestamp System time when transactions starts An increasing unique number given to each stransaction Denoted by ts(Ti)

33 Database Implementation – Concurrency Control Yan Huang33 The way it works Two time stamps associated with each variable x RS(x): the largest time stamp of the transactions read it WS(x): the largest time stamp of the transactions write it Protocol: ri(x) is allowed if ts(Ti) >= WS(x) wi(x) is allowed if ts(Ti) >=WS(x) and ts(Ti) >=RS(x) Disallowed ri(x) or wi(x) will kill Ti, Ti will restart

34 Database Implementation – Concurrency Control Yan Huang34 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); R (y); W(z); R(x); W(z); R(y); W(x); x y z RS=-1 RS=-1 RS=-1 WS=-1 WS=-1 WS=-1

35 Database Implementation – Concurrency Control Yan Huang35 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); x y z RS=100 RS=-1 RS=-1 WS=-1 WS=-1 WS=-1

36 Database Implementation – Concurrency Control Yan Huang36 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); x y z RS=100 RS=-1 RS=-1 WS=-1 WS=100 WS=-1

37 Database Implementation – Concurrency Control Yan Huang37 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); R (y); x y z RS=100 RS=200 RS=-1 WS=-1 WS=100 WS=-1

38 Database Implementation – Concurrency Control Yan Huang38 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); R (y); W(z); x y z RS=100 RS=200 RS=-1 WS=-1 WS=100 WS=300

39 Database Implementation – Concurrency Control Yan Huang39 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); R (y); W(z); R(x); x y z RS=200 RS=200 RS=-1 WS=-1 WS=100 WS=300

40 Database Implementation – Concurrency Control Yan Huang40 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); R (y); W(z); R(x); W(z); x y z RS=200 RS=200 RS=-1 WS=-1 WS=100 WS=300 T1 is rolled back

41 Database Implementation – Concurrency Control Yan Huang41 Example Assuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300 T1 T2 T3 R(x); W(y); R (y); W(z); R(x); W(z); x y z RS=200 RS=200 RS=-1 WS=-1 WS=100 WS=300 T1 is rolled back What happen to RS and WS?

42 Database Implementation – Concurrency Control Yan Huang42 Net result of TO scheduling Conflict pairs of actions are taken in the order of their home transactions But the basic TO does not guarantee recoverability later

43 Database Implementation – Concurrency Control Yan Huang43 Validation An optimistic scheme Transactions have 3 phases: (1) Read all DB values read writes to temporary storage no locking (2) Validate check if schedule so far is serializable (3) Write if validate ok, write to DB

44 Database Implementation – Concurrency Control Yan Huang44 Key idea Make validation atomic If T 1, T 2, T 3, … is validation order, then resulting schedule will be conflict equivalent to S s = T 1 T 2 T 3...

45 Database Implementation – Concurrency Control Yan Huang45 To implement validation, system keeps two sets: FIN = transactions that have finished phase 3 (and are all done) VAL = transactions that have successfully finished phase 2 (validation)

46 Database Implementation – Concurrency Control Yan Huang46 Example of what validation must prevent: RS(T 2 )={B} RS(T 3 )={A,B} WS(T 2 )={B,D} WS(T 3 )={C} time T 2 start T 2 validated T 3 validated T 3 start  = 

47 Database Implementation – Concurrency Control Yan Huang47 T 2 finish phase 3 Example of what validation must prevent: RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C} time T 2 start T 2 validated T 3 validated T 3 start  =  allow T 3 start

48 Database Implementation – Concurrency Control Yan Huang48 Another thing validation must prevent: RS(T 2 )={A} RS(T 3 )={A,B} WS(T 2 )={D,E} WS(T 3 )={C,D} time T 2 validated T 3 validated finish T 2 BAD: w 3 (D) w 2 (D)

49 Database Implementation – Concurrency Control Yan Huang49 finish T 2 Another thing validation must prevent: RS(T 2 )={A} RS(T 3 )={A,B} WS(T 2 )={D,E} WS(T 3 )={C,D} time T 2 validated T 3 validated allow finish T 2

50 Database Implementation – Concurrency Control Yan Huang50 Validation Rule When start validating T Check RS(T)  WS(U) is empty for U that started but did not finish validation before T started Check WS(T)  WS(U) is empty for any U that started but did not finish validation T start validation

51 Database Implementation – Concurrency Control Yan Huang51 Exercise: T: RS(T)={A,B} WS(T)={A,C} V: RS(V)={B} WS(V)={D,E} U: RS(U)={B} WS(U)={D} W: RS(W)={A,D} WS(W)={A,C} start validate finish


Download ppt "1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme."

Similar presentations


Ads by Google