Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 181 Chapter 18: Concurrency Control (Slides by Hector Garcia-Molina,

Similar presentations


Presentation on theme: "Chapter 181 Chapter 18: Concurrency Control (Slides by Hector Garcia-Molina,"— Presentation transcript:

1 Chapter 181 Chapter 18: Concurrency Control (Slides by Hector Garcia-Molina, http://www-db.stanford.edu/~hector/cs245/notes.htm)

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

3 Chapter 183 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 Chapter 184 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

5 Chapter 185 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

6 Chapter 186 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

7 Chapter 187 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

8 Chapter 188 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’

9 Chapter 189 Want schedules that are “good”, regardless of –initial state and –transaction semantics Only look at order of read and 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)

10 Chapter 1810 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)

11 Chapter 1811 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)

12 Chapter 1812 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

13 Chapter 1813 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 )

14 Chapter 1814 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

15 Chapter 1815 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.

16 Chapter 1816 Definition A schedule is conflict serializable if it is conflict equivalent to some serial schedule.

17 Chapter 1817 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 )

18 Chapter 1818 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?

19 Chapter 1819 Lemma S 1, S 2 conflict equivalent  P(S 1 )=P(S 2 )

20 Chapter 1820 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)

21 Chapter 1821 Theorem P(S 1 ) acyclic  S 1 conflict serializable

22 Chapter 1822 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

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

24 Chapter 1824 A locking protocol Two new actions: lock (exclusive):l i (A) unlock:u i (A) scheduler T 1 T 2 lock table

25 Chapter 1825 Rule #1: Consistent transactions T i : … l i (A) … p i (A) … u i (A)...

26 Chapter 1826 Rule #2 Legal scheduler S = …….. l i (A) ………... u i (A) ……... no l j (A)

27 Chapter 1827 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:

28 Chapter 1828 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)

29 Chapter 1829 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

30 Chapter 1830 Rule #3 Two phase locking (2PL) for transactions T i = ……. l i (A) ………... u i (A) ……... no unlocks no locks

31 Chapter 1831 # locks held by Ti Time Growing Shrinking Phase Phase

32 Chapter 1832 Schedule G delayed

33 Chapter 1833 Schedule G delayed

34 Chapter 1834 Schedule G delayed

35 Chapter 1835 Schedule H (T 2 reversed) delayed deadlock

36 Chapter 1836 Assume deadlocked transactions are rolled back –They have no effect –They do not appear in schedule E.g., Schedule H on previous slide

37 Chapter 1837 We can show that rules #1,2,3  conflict-serializable schedules (see textbook)

38 Chapter 1838 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

39 Chapter 1839 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)

40 Chapter 1840 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

41 Chapter 1841 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) …

42 Chapter 1842 Option 2: Upgrade (E.g., need to read, but don’t know if will write…) Ti=... l-S1(A) … r1(A)... l-X1(A) … w1(A)... u1(A) … The same as: - Getting 2nd lock on A, or - Dropping S, getting X lock What about transactions that read and write same object?

43 Chapter 1843 Compatibility matrix Comp S X S true false Xfalse false

44 Chapter 1844 Lock types beyond S/X Examples: (1) increment lock (see textbook) (2) update lock

45 Chapter 1845 Update locks

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

47 Chapter 1847 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...

48 Chapter 1848 (1) Don’t trust transactions to request/release locks (2) Hold all locks until transaction commits # locks time Sample Locking System:

49 Chapter 1849 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

50 Chapter 1850 Lock table Conceptually A  B C ... Lock info for B Lock info for C If null, object is unlocked Every possible object

51 Chapter 1851 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

52 Chapter 1852 What are the objects we lock? ? Relation A Relation B... Tuple A Tuple B Tuple C... Disk block A Disk block B... DB

53 Chapter 1853 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

54 Chapter 1854 Summary Have looked at 2PL, a concurrency- control mechanism commonly used in practice Others (in the textbook): - Multiple granularity - Tree (index) protocols - Timestamping - Validation


Download ppt "Chapter 181 Chapter 18: Concurrency Control (Slides by Hector Garcia-Molina,"

Similar presentations


Ads by Google