Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 245Notes 091 CS 245: Database System Principles Notes 09: Concurrency Control Hector Garcia-Molina.

Similar presentations


Presentation on theme: "CS 245Notes 091 CS 245: Database System Principles Notes 09: Concurrency Control Hector Garcia-Molina."— Presentation transcript:

1 CS 245Notes 091 CS 245: Database System Principles Notes 09: Concurrency Control Hector Garcia-Molina

2 CS 245Notes 092 Chapter 18 [18] Concurrency Control T1T2…Tn DB (consistency constraints)

3 Interactions among concurrently executing transactions can cause the database state to become inconsistent, even when the transactions individually preserve correctness of the state, and there is no system failure. Thus, the timing of individual steps of different transactions needs to be regulated in some manner. This regulation is the job of the scheduler component of the DBMS, and the general process of assuring that transactions preserve consistency when executing simultaneously is called concurrency control. CS 245Notes 093

4 In most situations, the scheduler will execute the reads and writes directly, first calling on the buffer manager if the desired database element is not in a buffer. However, in some situations, it is not safe for the request to be executed immediately. The scheduler must delay the request. In some concurrency-control techniques, the scheduler may even abort the transaction that issued the request CS 245Notes 094

5 CS 245Notes 095 Example: T1:Read(A)T2:Read(A) A  A+100A  A  2Write(A)Read(B) B  B+100B  B  2Write(B) Constraint: A=B

6 A schedule is a sequence of the important actions taken by one or more transactions. When studying concurrency control, the important read and write actions take place in the main-memory buffers, not the disk. That is, a database element A that is brought to a buffer by some transaction T may be read or written in that buffer not only by T but by other transactions that access A. A schedule is serial if its actions consist of all the actions of one transaction, then all the actions of another transaction, and so on. No mixing of the actions is allowed. CS 245Notes 096

7 CS 245Notes 097 Schedule A (serial) 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

8 CS 245Notes 098 Schedule B (serial) 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

9 A schedule S is serializable if there is a serial schedule S' such that for every initial database state, the effects of S and S‘ are the same. CS 245Notes 099

10 CS 245Notes 0910 Schedule C (serializable) 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

11 CS 245Notes 0911 Schedule D (not serializable) 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

12 CS 245Notes 0912 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’

13 CS 245Notes 0913 Want schedules that are “good”, regardless of –initial state and –transaction semantics Only look at order of read and writes It is not realistic for the scheduler to concern itself with the details of computation undertaken by transactions. Any database element A that a transaction T writes is given a value that depends on the database state in such a way that no arithmetic coincidences occur.

14 CS 245Notes 0914 To make the notation precise: 1. An action is an expression of the form r i (X ) or W i (X), meaning that transaction T i, reads or writes, respectively, the database element X. 2. A transaction T i is a sequence of actions with subscript i. 3. A schedule S of a set of transactions T is a sequence of actions, in which for each transaction T i in T, the actions of T i appear in S in the same order that they appear in the definition of T i itself. We say that S is an interleaving of the actions of the transactions of which it is composed.

15 CS 245Notes 0915 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 CS 245Notes 0916 However, 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) as a matter of fact, T 2 must precede T 1 in any equivalent schedule, i.e., T 2  T 1

17 CS 245Notes 0917 T 1 T 2 Sd cannot be rearranged into a serial schedule Sd is not “equivalent” to any serial schedule Sd is “bad” T 2  T 1 Also, T 1  T 2

18 CS 245Notes 0918 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 (in this case T 1,T 2 )

19 CS 245Notes 0919 Concepts Transaction: sequence of r i (x), w i (x) actions Conflicting actions: 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 actions or transactions

20 CS 245Notes 0920 What about concurrent actions? Ti issuesSystemInput(X) t  X read(X,t)issuescompletes input(X) time T2 issues write(B,s) System issues input(B) completes B  s System issues output(B) completes

21 CS 245Notes 0921 So net effect is either S=…r 1 (X)…w 2 (B)… or S=…w 2 (B)…r 1 (X)…

22 CS 245Notes 0922 Assume equivalent to either r 1 (A) w 2 (A) orw 2 (A) r 1 (A)  low level synchronization mechanism Assumption called “atomic actions” What about conflicting, concurrent actions on same object? start r 1 (A)end r 1 (A) start w 2 (A) end w 2 (A) time

23 CS 245Notes 0923 Definition S 1, S 2 are conflict equivalent schedules if S 1 can be transformed into S 2 by a series of non-conflicting swaps of adjacent actions. A schedule is conflict serializable if it is conflict equivalent to some serial schedule.

24 CS 245Notes 0924 Example: r 1 (A); w 1 (A); r 2 (A); w 2 (A); r 1 (B); w 1 (B); r 2 (B); w 2 (B); We claim this schedule is conflict-serializable. 1.r 1 (A) ; w 1 (A) ; r 2 (A) ; w 2 (A) ; r 1 (B) ; w 1 (B) ; r 2 (B) ; w 2 (B) ; 2.r 1 (A) ; w 1 (A) ; r 2 (A) ; r 1 (B) ; w 2 (A) ; w 1 (B) ; r 2 (B) ; w 2 (B) ; 3.r 1 (A) ; w 1 (A) ; r 1 (B) ; r 2 (A) ; w 2 (A) ; w 1 (B) ; r 2 (B) ; w 2 (B) ; 4.r 1 (A) ; w 1 (A) ; r 1 (B) ; r 2 (A) ; w 1 (B) ; w 2 (A) ; r 2 (B) ; w 2 (B) ; 5.r 1 (A) ; w 1 (A) ; r 1 (B) ; w 1 (B) ; r 2 (A) ; w 2 (A) ; r 2 (B) ; w 2 (B) ;

25 CS 245Notes 0925 Conflict-serializability is a sufficient condition for serializability i.e., a conflict-serializable schedule is a serializable schedule. Conflict-serializability is not required for a schedule to be serializable. Schedulers in commercial systems generally use conflict-serializability when they need to guarantee serializability.

26 CS 245Notes 0926 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 )

27 CS 245Notes 0927 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 serializable?

28 CS 245Notes 0928 Another Exercise: What is P(S) for S = w 1 (A) r 2 (A) r 3 (A) w 4 (A) ?

29 CS 245Notes 0929 Lemma S 1, S 2 conflict equivalent  P(S 1 )=P(S 2 )

30 CS 245Notes 0930 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 CS 245Notes 0931 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) (cannot swap) S 2 =r 2 (A) w 1 (A) r 1 (B) w 2 (B)

32 CS 245Notes 0932 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 CS 245Notes 0933 (  ) 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 CS 245Notes 0934 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

35 CS 245Notes 0935 Option 2: prevent P(S) cycles from occurring T 1 T 2 …..T n Scheduler DB How to enforce serializable schedules?

36 CS 245Notes 0936 A locking protocol Two new actions: lock (exclusive):l i (A) unlock:u i (A) scheduler T 1 T 2 lock table

37 CS 245Notes 0937 Rule #1: Consistency of transactions T i : … l i (A) … p i (A) … u i (A)... 1. A transaction can only read or write an element if it previously was granted a lock on that element and hasn’t yet released the lock. 2. If a transaction locks an element, it must later unlock that element.

38 CS 245Notes 0938 Rule #2 Legality of schedules S = …….. l i (A) ………... u i (A) ……... Locks must have their intended meaning: no two transactions may have locked the same element without one having first released the lock. no l j (A)

39 CS 245Notes 0939 What schedules are legal? What transactions are consistent? 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:

40 CS 245Notes 0940 What schedules are legal? What transactions are consistent? 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:

41 CS 245Notes 0941 Schedule F (legal schedule of consistent transactions) 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

42 CS 245Notes 0942 Rule #3 Two phase locking (2PL) for transactions T i = ……. l i (A) ………... u i (A) ……... no unlocks no locks

43 CS 245Notes 0943 # locks held by Ti Time Growing Shrinking Phase Phase

44 CS 245Notes 0944 Schedule G delayed

45 CS 245Notes 0945 Schedule G delayed

46 CS 245Notes 0946 Schedule G delayed

47 CS 245Notes 0947 Schedule G T1 T2 25 25 l 1 (A);Read(A); A A+100 Write(A); l 1 (B);u 1 (A) 125 l 2 (A);Read(A) A Ax2;Write(A);u 2 (A) 250 l 2 (B); delayed Read(B); B B+100;Write(B);u 1 (B) 125 l 2 (B);Read(B) B Bx2;Write(B);u 2 (B) 250 250 A B

48 CS 245Notes 0948 Schedule H (T 2 reversed) delayed

49 CS 245Notes 0949 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!

50 CS 245Notes 1050 Deadlocks Detection –Wait-for graph Prevention –Resource ordering –Timeout –Wait-die –Wound-wait

51 CS 245Notes 1051 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

52 CS 245Notes 1052 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

53 CS 245Notes 1053 Timeout If transaction waits more than L sec., roll it back! Simple scheme Hard to select L

54 CS 245Notes 0954 Theorem Rules #1,2,3  conflict (consistency, legality, 2PL) serializable schedule

55 CS 245Notes 0955 Intuitively, each two-phase-locked transaction may be thought to execute in its entirety at the instant it issues its first unlock request. In a conflict-equivalent serial schedule transactions appear in the same order as their first unlocks. Theorem Rules #1,2,3  conflict (consistency, legality, 2PL) serializable schedule

56 CS 245Notes 0956 Proof: BASIS: If n = 1, there is nothing to do; S is already a serial schedule. INDUCTION: Suppose S involves n transactions T 1,T 2,..., T n, and let T i be the transaction with the first unlock action in the entire schedule S, say u i (x). We claim it is possible to move all the read and write actions of T i forward to the beginning of the schedule without passing any conflicting reads or writes. Theorem Rules #1,2,3  conflict serializable schedule

57 CS 245Notes 0957 2PL subset of Serializable 2PL Serializable

58 CS 245Notes 0958 S1: w1(x) w3(x) w2(y) w1(y) S1 cannot be achieved via 2PL: The lock by T1 for y must occur after w2(y), so the unlock by T1 for x must occur after this point (and before w1(x)). Thus, w3(x) cannot occur under 2PL where shown in S1 because T1 holds the x lock at that point. However, S1 is serializable (equivalent to T2, T1, T3).

59 CS 245Notes 0959 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 C.C. mechanisms

60 CS 245Notes 0960 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

61 CS 245Notes 0961 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)

62 CS 245Notes 0962 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

63 CS 245Notes 0963 Rule #1 Consistency of 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) … A transaction may not write without holding an exclusive lock, and may not read without holding some lock. All locks must be followed by an unlock of the same element.

64 CS 245Notes 0964 Two-phase locking of transactions: Locking must precede unlocking. Legality of schedules: An element may either be locked exclusively by one transaction or by several in shared mode, but not both.

65 CS 245Notes 0965 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) …

66 CS 245Notes 0966 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 What about transactions that read and write same object?

67 CS 245Notes 0967 Rule #2 Legal scheduler S =....l-S i (A) … … u i (A) … no l-X j (A) S =... l-X i (A) … … u i (A) … no l-X j (A) no l-S j (A)

68 CS 245Notes 0968 A way to summarize Rule #2 Compatibility matrix Comp S X S true false Xfalse false

69 CS 245Notes 0969 Rule # 3 2PL transactions No change except for upgrades: (I) If upgrade gets more locks (e.g., S  {S, X}) then no change! (II) If upgrade releases read (shared) lock (e.g., S  X) - can be allowed in growing phase

70 CS 245Notes 0970 Proof: similar to X locks case Detail: l-t i (A), l-r j (A) do not conflict if comp(t,r) l-t i (A), u-r j (A) do not conflict if comp(t,r) Theorem Rules 1,2,3  Conf.serializable for S/X locks schedules

71 CS 245Notes 0971 Lock types beyond S/X Examples: (1) increment lock (2) update lock

72 CS 245Notes 0972 Example (1): increment lock 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)

73 CS 245Notes 0973 CompSXI S X I

74 CS 245Notes 0974 CompSXI STFF XFFF IFFT

75 CS 245Notes 0975 Update locks

76 CS 245Notes 0976 Solution If T i wants to read A and knows it may later want to write A, it requests update lock (not shared)

77 CS 245Notes 0977 CompSXU STFT XFFF U F FF -> symmetric table? New request Lock already held in

78 CS 245Notes 0978 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

79 CS 245Notes 0979 How does locking work in practice? Every system is different (E.g., may not even provide CONFLICT-SERIALIZABLE schedules) But here is one (simplified) way...

80 CS 245Notes 0980 (1) Don’t trust transactions to request/release locks (2) Hold all locks until transaction commits # locks time Sample Locking System:

81 CS 245Notes 0981 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

82 CS 245Notes 0982 Lock table Conceptually A  B C ... Lock info for B Lock info for C If null, object is unlocked Every possible object

83 CS 245Notes 0983 But use hash table: A If object not found in hash table, it is unlocked Lock info for A A... H

84 CS 245Notes 0984 Lock info for A - example tran mode wait? Nxt T_link Object:A Group mode:U Waiting:yes List: T1 S no T2 U no T3X yes  To other T3 records

85 CS 245Notes 0985 What are the objects we lock? ? Relation A Relation B... Tuple A Tuple B Tuple C... Disk block A Disk block B... DB

86 CS 245Notes 0986 Locking works in any case, but should we choose small or large objects?

87 CS 245Notes 0987 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

88 CS 245Notes 0988 We can have it both ways!! Ask any janitor to give you the solution... hall Stall 1Stall 2Stall 3Stall 4 restroom

89 CS 245Notes 0989 Example R1 t1t1 t2t2 t3t3 t4t4

90 CS 245Notes 0990 Example R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S)

91 CS 245Notes 0991 Example R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (S)

92 CS 245Notes 0992 Example (b) R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S)

93 CS 245Notes 0993 Example R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (IX) T 2 (IX)

94 CS 245Notes 0994 Multiple granularity CompRequestor IS IX S SIX X IS Holder IX S SIX X

95 CS 245Notes 0995 Multiple granularity CompRequestor IS IX S SIX X IS Holder IX S SIX X TTTTF F F F FFFFF FFFT FTFT FFTT

96 CS 245Notes 0996 ParentChild can belocked in IS IX S SIX X P C

97 CS 245Notes 0997 ParentChild can be locked locked inby same transaction in IS IX S SIX X P C IS, S IS, S, IX, X, SIX none X, IX, [SIX] none not necessary

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

99 CS 245Notes 0999 Exercise: 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)

100 CS 245Notes 09100 Exercise: Can T 2 access object f 2.2 in X mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (X) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (IX)

101 CS 245Notes 09101 Exercise: Can T 2 access object f 3.1 in X mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (S) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (IS)

102 CS 245Notes 09102 Exercise: Can T 2 access object f 2.2 in S 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)

103 CS 245Notes 09103 Exercise: 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)

104 CS 245Notes 09104 Insert + delete operations Insert A Z ...

105 CS 245Notes 09105 Modifications to locking rules: (1) Get exclusive lock on A before deleting A (2) At insert A operation by Ti, Ti is given exclusive lock on A

106 CS 245Notes 09106 Phantoms Still have a problem: Phantoms Example: relation R (E#,name,…) constraint: E# is key use tuple locking RE#Name…. o155Smith o275Jones

107 CS 245Notes 09107 T 1 : Insert into R T 2 : Insert into R T 1 T 2 S 1 (o 1 ) S 2 (o 1 ) S 1 (o 2 ) S 2 (o 2 ) Check Constraint Insert o 3 [08,Obama,..] Insert o 4 [08,McCain,..]...

108 CS 245Notes 09108 Solution Use multiple granularity tree Before insert of node Q, lock parent(Q) in X mode R1 t1t1 t2t2 t3t3

109 CS 245Notes 09109 Back to example 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

110 CS 245Notes 09110 Instead of using R, can use index on R: Example: R Index 0<E#<100 Index 100<E#<200 E#=2E#=5 E#=107 E#=109...

111 CS 245Notes 09111 This approach can be generalized to multiple indexes...

112 CS 245Notes 09112 Next: Tree-based concurrency control Validation concurrency control

113 CS 245Notes 09113 Example A B C D EF all objects accessed through root, following pointers

114 CS 245Notes 09114 Example A B C D EF all objects accessed through root, following pointers T 1 lock

115 CS 245Notes 09115 Example A B C D EF all objects accessed through root, following pointers T 1 lock  can we release A lock if we no longer need A??

116 CS 245Notes 09116 Idea: traverse like “Monkey Bars” A B C D EF

117 CS 245Notes 09117 Idea: traverse like “Monkey Bars” A B C D EF T 1 lock

118 CS 245Notes 09118 Idea: traverse like “Monkey Bars” A B C D EF T 1 lock

119 CS 245Notes 09119 Why does this work? Assume all T i start at root; exclusive lock T i  T j  T i locks root before T j Actually works if we don’t always start at root Root Q T i  T j

120 CS 245Notes 09120 Rules: tree protocol (exclusive locks) (1) First lock by T i may be on any item (2) After that, item Q can be locked by T i only if parent(Q) locked by T i (3) Items may be unlocked at any time (4) After T i unlocks Q, it cannot relock Q

121 CS 245Notes 09121 Tree-like protocols are used typically for B-tree concurrency control E.g., during insert, do not release parent lock, until you are certain child does not have to split Root

122 CS 245Notes 09122 Tree Protocol with Shared Locks Rules for shared & exclusive locks? A B C D EF T 1 S lock(released) T 1 S lock (held) T 1 X lock (released) T 1 X lock (will get)

123 CS 245Notes 09123 Tree Protocol with Shared Locks Rules for shared & exclusive locks? A B C D EF T 1 S lock(released) T 1 S lock (held) T 1 X lock (released) T 1 X lock (will get) T 2 reads: B modified by T 1 F not yet modified by T 1

124 CS 245Notes 09124 Need more restrictive protocol Will this work?? –Once T 1 locks one object in X mode, all further locks down the tree must be in X mode Tree Protocol with Shared Locks

125 CS 245Notes 09125 Validation 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

126 CS 245Notes 09126 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...

127 CS 245Notes 09127 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)

128 CS 245Notes 09128 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  = 

129 CS 245Notes 09129 T 2 finish phase 3 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  =  allow T 3 start

130 CS 245Notes 09130 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

131 CS 245Notes 09131 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)

132 CS 245Notes 09132 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

133 CS 245Notes 09133 Validation rules for T j : (1) When T j starts phase 1: ignore(T j )  FIN (2) at T j Validation: if check (T j ) then [ VAL  VAL U {T j }; do write phase; FIN  FIN U {T j } ]

134 CS 245Notes 09134 Check (T j ): For T i  VAL - IGNORE (T j ) DO IF [ WS(T i )  RS(T j )   OR T i  FIN ] THEN RETURN false; RETURN true;

135 CS 245Notes 09135 Check (T j ): For T i  VAL - IGNORE (T j ) DO IF [ WS(T i )  RS(T j )   OR T i  FIN ] THEN RETURN false; RETURN true; Is this check too restrictive ?

136 CS 245Notes 09136 Improving Check(T j ) For T i  VAL - IGNORE (T j ) DO IF [ WS(T i )  RS(T j )   OR ( T i  FIN AND WS(T i )  WS(T j )   )] THEN RETURN false; RETURN true;

137 CS 245Notes 09137 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

138 CS 245Notes 09138 Is Validation = 2PL? 2PL Val 2PL Val 2PL Val 2PL

139 CS 245Notes 09139 S2: w2(y) w1(x) w2(x) Achievable with 2PL? Achievable with validation?

140 CS 245Notes 09140 S2: w2(y) w1(x) w2(x) S2 can be achieved with 2PL: l2(y) w2(y) l1(x) w1(x) u1(x) l2(x) w2(x) u2(y) u2(x) S2 cannot be achieved by validation: The validation point of T2, val2 must occur before w2(y) since transactions do not write to the database until after validation. Because of the conflict on x, val1 < val2, so we must have something like S2: val1 val2 w2(y) w1(x) w2(x) With the validation protocol, the writes of T2 should not start until T1 is all done with its writes, which is not the case.

141 CS 245Notes 09141 Validation subset of 2PL? Possible proof (Check!): –Let S be validation schedule –For each T in S insert lock/unlocks, get S’: At T start: request read locks for all of RS(T) At T validation: request write locks for WS(T); release read locks for read-only objects At T end: release all write locks –Clearly transactions well-formed and 2PL –Must show S’ is legal (next page)

142 CS 245Notes 09142 Say S’ not legal (due to w-r conflict): S’:... l1(x) w2(x) r1(x) val1 u1(x)... –At val1: T2 not in Ignore(T1); T2 in VAL –T1 does not validate: WS(T2)  RS(T1)   –contradiction! Say S’ not legal (due to w-w conflict): S’:... val1 l1(x) w2(x) w1(x) u1(x)... –Say T2 validates first (proof similar if T1 validates first) –At val1: T2 not in Ignore(T1); T2 in VAL –T1 does not validate: T2  FIN AND WS(T1)  WS(T2)   ) –contradiction!

143 CS 245Notes 09143 Conclusion: Validation subset 2PL 2PL Val 2PL Val 2PL Val 2PL

144 CS 245Notes 09144 Validation (also called optimistic concurrency control) is useful in some cases: - Conflicts rare - System resources plentiful - Have real time constraints

145 CS 245Notes 09145 Summary Have studied C.C. mechanisms used in practice - 2 PL - Multiple granularity - Tree (index) protocols - Validation


Download ppt "CS 245Notes 091 CS 245: Database System Principles Notes 09: Concurrency Control Hector Garcia-Molina."

Similar presentations


Ads by Google