/
Concurrency Control Database System Implementation CSE 507 Concurrency Control Database System Implementation CSE 507

Concurrency Control Database System Implementation CSE 507 - PowerPoint Presentation

adah
adah . @adah
Follow
67 views
Uploaded On 2023-06-25

Concurrency Control Database System Implementation CSE 507 - PPT Presentation

Some slides adapted from Navathe et Al Silberchatz et Al and Hector GarciaMolina Lock Based Protocols A lock is a mechanism to control concurrent access to a data item Data items can be locked in two modes ID: 1003291

read lock write transaction lock read transaction write timestamp validation data item time phase unlock locking version concurrency transactions

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Concurrency Control Database System Impl..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. Concurrency ControlDatabase System Implementation CSE 507Some slides adapted from Navathe et. Al. , Silberchatz et. Al and Hector Garcia-Molina

2. Lock Based ProtocolsA lock is a mechanism to control concurrent access to a data itemData items can be locked in two modes : 1. exclusive (X) mode. Data item can be both read as well as written. X-lock is requested using lock-X instruction. 2. shared (S) mode. Data item can only be read. S-lock is requested using lock-S instruction.Lock requests are made to the concurrency-control manager by the programmer. Transaction can proceed only after request is granted.

3. Lock Compatibility MatrixLock-compatibility matrixLock granted if the requested lock is compatible with locks already held on the item by other transactionsMultiple shared locks on an itemBut only one exclusive on an itemIf a lock cannot be granted, then need to wait.

4. Lock Compatibility MatrixExample of a transaction performing locking: T2: lock-S(A); T3: lock-X(B); read (A); read (B); unlock(A); B = B – 50; lock-S(B); write(B); read (B); unlock(B); unlock(B); lock-X(A); display(A+B) read(A); A = A + 50; write(A); unlock(A);If T2 was executed here in midst

5. Lock Compatibility MatrixLocking as done in previous example is not sufficient to guarantee serializability.What if T2 was executed in the middle of T3?Locking protocol is a set of rules followed by all transactions while requesting and releasing locks. Locking protocols restrict the set of possible schedules.

6. 2 Phase Locking ProtocolPhase 1: Growing PhaseTransaction may obtain locks Transaction may not release locksPhase 2: Shrinking PhaseTransaction may release locksTransaction may not obtain locks

7. 2 Phase Locking Protocol: exampleT2: lock-S(A);read (A); unlock(A);lock-S(B); read (B); unlock (A)unlock(B); display(A+B)T3: lock-X(B); read (B); B = B – 50; unlock(B); Lock-X(A) read(A);A = A + 50;write(A); unlock(B)unlock(A);Does this guarantee serializability?

8. 2 Phase Locking ProtocolThis protocol ensures conflict-serializable schedules.It can be proved that the transactions can be serialized in the order of their lock points (i.e., the point where a transaction acquired its final lock).

9. Does 2 Phase Locking support recoverability And cascadeless?T2: lock-S(A);read (A); lock-S(B);read (B); unlock (A)unlock(B); display(A+B);Commit;T3: lock-X(B); read (B); B = B – 50; Lock-X(A) read(A);A = A + 50;write(A); unlock(B);unlock(A);Abort;Time

10. Strict 2 Phase Locking All exclusive locks to be held until transactions commits;Guarantees “Strict” schedules (recall discussion on recovery) TimeT2: lock-S(A);read (A); lock-S(B);read (B); unlock (A)unlock(B); display(A+B);Commit;T3: lock-X(B); read (B); B = B – 50; Lock-X(A) read(A);A = A + 50;write(A); unlock(B);unlock(A);Abort;

11. Rigorous 2 Phase Locking All locks to be held until transactions commits;The serializability order === the commit orderMore intuitive behavior for the usersTimeT2: lock-S(A);read (A); lock-S(B);read (B); unlock (A)unlock(B); display(A+B);Commit;T3: lock-X(B); read (B); B = B – 50; Lock-X(A) read(A);A = A + 50;write(A); unlock(B);unlock(A);Abort;

12. Lock ConversionsTwo-phase locking with lock conversions: – First Phase: can acquire a lock-S on itemcan acquire a lock-X on itemcan convert a lock-S to a lock-X (upgrade) – Second Phase:can release a lock-Scan release a lock-Xcan convert a lock-X to a lock-S (downgrade)T1: Read (A) read (B); Read(C);Read(D);Write(B)

13. Automatic Acquisition of LocksA transaction Ti issues the standard read/write instruction, without explicit locking calls.The operation read(D) is processed as: if Ti has a lock on D then read(D) else begin if necessary wait until no other transaction has a lock-X on D grant Ti a lock-S on D; read(D) end

14. Automatic Acquisition of Lockswrite(D) is processed as: if Ti has a lock-X on D then write(D) else begin if necessary wait until no other transaction has any lock on D, if Ti has a lock-S on D then upgrade lock on D to lock-X else grant Ti a lock-X on D write(D) end;All locks are released after commit or abort

15. Show application of 2PL on following:(a) r1 (X); r2 (X); w1(X); r3(X); w2(X)(b) r2 (X); r3 (X); w3(X); w1(X); w2(X)(c) r3 (X); r1 (X); w3(X); r2(X); w1(X)(d) r3 (X); r2 (X); r1(X); w3(X); w1(X)

16. Multiple granularity of LockingAllow data items to be of various sizes and define a hierarchy of data granularities, Small granularities are nested within larger onesRepresented graphically as a tree.When a transaction locks a node in the tree explicitly, it implicitly locks all the node's descendents in the same mode.

17. Multiple granularity of LockingGranularity of locking (level in tree where locking is done):fine granularity (lower in tree): high concurrency, high locking overheadcoarse granularity (higher in tree): low locking overhead, low concurrency

18. Multiple granularity of LockingGranularitiesEntire databaseEntire fileA disk blockA database record

19. Multiple granularity of LockingTo manage such hierarchy, in addition to read and write, three additional locking modes are defined:Intention-shared (IS): indicates that a shared lock(s) will be requested on some descendent nodes(s).Intention-exclusive (IX): indicates that an exclusive lock(s) will be requested on some descendent node(s).Shared-intention-exclusive (SIX): indicates that the current node is locked in shared mode but an exclusive lock(s) will be requested on some descendent nodes(s).

20. Multiple granularity of LockingSIXXYou can consider SIX as a combination of S and IX lock modes. It is the stricter of the two. A transaction can get the affect of SIX by getting S and IX modes. Implicitly SImplicitly S

21. Multiple granularity of LockingThese locks are applied using the following compatibility matrix:IS -- Intention-shared IX -- Intention-exclusive SIX -- Shared-intention-exclusive

22. Multiple granularity of Locking --- RulesThe lock compatibility must adhered to.The root of the tree must be locked first, in any mode..A node N can be locked by a transaction T in S or IX mode only if the parent node is already locked by T in either IS or IX mode.A node N can be locked by T in X, IX, or SIX mode only if the parent of N is already locked by T in either IX or SIX mode.T can lock a node only if it has not unlocked any node (to enforce 2PL policy).T can unlock a node, N, only if none of the children of N are currently locked by T.locks need to released bottom first.

23. Some Examples on Multi-granular LockingQuery 1: Read the entire Emp table to compute average salary Lock the root (database) in IS mode, then Emp table file in S mode.You can also lock the Emp table in IS mode; then pages in IS mode and then keep requesting for S lock on the records. Query 2: Modify the head Aadhar number of CSE Dept. Lock the root (database) in SIX mode, then Dept table file in SIXNow we can search through the pages till we get the record corresponding to CSE dept. On that page we can get an X lock. Or we can get an IX lock on that page and get an X lock on the record. If we had a B+ tree on Dept file, then we would first get the record address of CSE dept. Now we can get IX locks from root the corresponding page, and get an X lock on the record.

24. Some Examples on Multi-granular LockingQuery 3: Insert a record into Dept table at the end of file Lock the root (database) in IX mode, Then Dept table file in X mode.One can also lock the Dept table file in IX and then get an X lock on the page where record is being inserted but that may lead to incorrect summary problem.

25. Deadlocks in 2 phase lockingNeither T3 nor T4 can make progress.Such a situation is called a deadlock. To handle a deadlock one of T3 or T4 must be rolled back and its locks released.

26. Deadlocks in 2 phase locking2PL does not ensure freedom from deadlocks.In addition, there is a possibility of starvation.Starvation examples:A transaction may be waiting for an X-lock on an item, while a sequence of other transactions request and are granted an S-lock on the same item. The same transaction is repeatedly rolled back due to deadlocks.

27. Deadlock HandlingDeadlock prevention protocols ensure that the system will never enter into a deadlock state. Some prevention strategies :Transaction locks all its data items before it begins execution (predeclaration).Impose partial ordering of all data items and require that a transaction can lock data items only in the order specified by the partial order.

28. Deadlock Handling – Prevention SchemesFollowing schemes use transaction timestamps.wait-die scheme — non-preemptiveolder transaction may wait for younger one to release data item. (older means smaller timestamp) Younger transactions never wait for older ones; they are rolled back instead.a transaction may die several times before acquiring needed data item

29. Deadlock Handling – Prevention Schemeswound-wait scheme — preemptiveOder transaction wounds (forces rollback) of younger transaction instead of waiting for it. Younger transactions may wait for older ones.may be fewer rollbacks than wait-die scheme.

30. Deadlock Handling – Prevention SchemesBoth in wait-die and in wound-wait schemes, a rolled back transactions is restarted with its original timestamp. Older transactions thus have precedence over newer onesThus starvation is avoided.Timeout-Based Schemes:Transaction waits for a lock only for a specified amount of time.

31. Deadlock DetectionDeadlocks can be described as a wait-for graph, If Ti  Tj is in E, then there is a directed edge from Ti to Tj, implying that Ti is waiting for Tj to release a data item.Wait-for graph without a cycleWait-for graph with a cycle

32. Deadlock RecoveryWhen deadlock is detected :Some transaction will have to rolled back.Select that transaction as victim that will incur minimum cost.Rollback -- determine how far to roll back transactionTotal rollback: Abort the transaction and then restart it.More effective to roll back transaction only as far as necessary to break deadlock.Starvation happens if same transaction is always chosen as victim.Include #rollbacks in the cost factor to avoid starvation

33. Time Stamp Ordering AlgorithmEach transaction is issued a timestamp.Time stamp of an old transaction Ti TS(Ti) < TS(Tj) of a newer transaction. The protocol manages concurrent execution such that the time-stamps determine the serializability order.

34. Time Stamp Ordering AlgorithmIn order to assure such behavior, the protocol maintains for each data Q two timestamp values:W-timestamp(Q) is the largest time-stamp of any transaction that executed write(Q) successfully.R-timestamp(Q) is the largest time-stamp of any transaction that executed read(Q) successfully.

35. Time Stamp Ordering AlgorithmThis protocol ensures that any conflicting read and write operations are executed in timestamp order.Suppose a transaction Ti issues a read(Q)If TS(Ti) < W-timestamp(Q).If TS(Ti)  W-timestamp(Q)

36. Time Stamp Ordering AlgorithmThis protocol ensures that any conflicting read and write operations are executed in timestamp order.Suppose a transaction Ti issues a read(Q)If TS(Ti) < W-timestamp(Q).Read operation is rejected, and Ti is rolled back.If TS(Ti)  W-timestamp(Q), Read operation is executedR-timestamp(Q) is set to max(R-timestamp(Q), TS(Ti)).

37. Time Stamp Ordering: Write Operation by TiIf TS(Ti) < R-timestamp(Q)If TS(Ti) < W-timestamp(Q)Otherwise, the write operation is executed

38. Time Stamp Ordering: Write Operation by TiIf TS(Ti) < R-timestamp(Q)Write operation is rejected, and Ti is rolled back.If TS(Ti) < W-timestamp(Q)Write operation is rejected, and Ti is rolled back.Otherwise, the write operation is executedW-timestamp(Q) is set to TS(Ti).

39. Apply TSO Algorithm on following ScheduleTransaction T1Transaction T2Read_item(Y)Write_item(Y)Read_item(X)Write_item(X)Read_item(X)Read_item(Y)Write_item(Y)Write_item(X)TimeAssume T1 arrives at time t=3T2 arrives at time t=1

40. TSO Algorithm Recoverability and CascadelessProblem with timestamp-ordering protocol:Suppose Ti aborts, but Tj has read a data item written by TiThen Tj must abort; if Tj had been allowed to commit earlier, the schedule is not recoverable.Further, any transaction that has read a data item written by Tj must abortThis can lead to cascading rollback --- that is, a chain of rollbacks

41. TSO Algorithm Recoverability and CascadelessSolution 1:A transaction is structured such that its writes are all performed at the end of its processingAll writes of a transaction form an atomic action; no transaction may execute while a transaction is being writtenA transaction that aborts is restarted with a new timestampSolution 2: Limited form of locking: wait for data to be committed before reading itSolution 3: Track uncommitted writes to atleast ensure recoverability

42. TSO Algorithm Thomas Write RuleModified version of the timestamp-ordering protocol.Obsolete write operations may be ignored in some cases.When Ti attempts to write data item Q, if TS(Ti) < W-timestamp(Q), then Ti is attempting to write an obsolete value of {Q}. Rather than rolling back Ti (as TSO would do), this {write} operation can be ignored.Otherwise this protocol is the same as the TSO algorithm.

43. TSO Algorithm Thomas Write RuleThomas' Write Rule allows greater potential concurrency. Allows some view-serializable schedules that are not conflict-serializable.

44. Quick note on View Serializability View equivalence:A less restrictive definition of equivalence of schedules View serializability:Definition of serializability based on view equivalence. A schedule is view serializable if it is view equivalent to a serial schedule.

45. Quick note on View Serializability Let S and S´ be two schedules. Following three conditions are met, for each data item Q, Transaction Ti reads the initial value of Q in both schedule S and S’.Transaction Ti should consume (read(Q)) the same output (write(Q)) of Tj in both S and S’.The transaction (if any) that performs the final write(Q) operation must be same in both S and S’.

46. Quick note on View Serializability A schedule S is view serializable if it is view equivalent to a serial schedule.Every conflict serializable schedule is also view serializable.But not vice versa. What serial schedule is above equivalent to?Every view serializable schedule that is not conflict serializable has blind writes.

47. Multiversion SchemesMultiversion schemes keep old versions of data item to increase concurrency.Multiversion Timestamp OrderingMultiversion Two-Phase LockingEach successful write results in the creation of a new version of the data item written.Use timestamps to label versions.

48. Multiversion SchemesUse timestamps to label versions.When a read(Q) operation is issued, Select an appropriate version of Q based on the timestamp of the transactionReads never have to wait as an appropriate version is returned immediately.

49. Multiversion Time Stamp OrderingEach data item Q has a sequence of versions <Q1,..., Qm>. Each version Qk contains three data fields:Content -- the value of version Qk.W-timestamp(Qk) -- timestamp of the transaction that created (wrote) version QkR-timestamp(Qk) -- largest timestamp of a transaction that successfully read version Qk

50. Multiversion Time Stamp Ordering When a transaction Ti creates a new version Qk of Q, Qk's W-timestamp is initialized to TS(Ti)Qk's R-timestamp is initialized to TS(Ti). R-timestamp of Qk is updated whenever a transaction Tj reads Qk, and TS(Tj) > R-timestamp(Qk).

51. Multiversion Time Stamp Ordering Suppose that transaction Ti issues a read(Q) or write(Q) operation. Let Qk denote the version of Q whose write timestamp is the largest write timestamp less than or equal to TS(Ti).If transaction Ti issues a read(Q), then the value returned is the content of version Qk.If transaction Ti issues a write(Q)if TS(Ti) < R-timestamp(Qk), then …… if TS(Ti) = W-timestamp(Qk), then …….else a new version of Q is created.

52. Multiversion Time Stamp Ordering Suppose that transaction Ti issues a read(Q) or write(Q) operation. Let Qk denote the version of Q whose write timestamp is the largest write timestamp less than or equal to TS(Ti).If transaction Ti issues a read(Q), then the value returned is the content of version Qk.If transaction Ti issues a write(Q)if TS(Ti) < R-timestamp(Qk), then transaction Ti is rolled back. if TS(Ti) = W-timestamp(Qk), the contents of Qk are overwrittenelse a new version of Q is created.

53. Multiversion Time Stamp Ordering Observe thatReads always succeedA write by Ti is rejected if:Some other transaction Tj that (in the serialization order defined by the timestamp values) should read Ti's write, has already read a version created by a transaction older than Ti.Protocol guarantees serializability

54. Multiversion Two Phase LockingMain Idea:Allow a transaction T’ to read a data item X while it is write locked by a conflicting transaction T.Accomplished by maintaining two versions of each data item X.Here, one version must always have been written by some committed transaction. This means a write operation always creates a new version of X.

55. Multiversion Two Phase LockingSteps:X is the committed version of a data item.T creates a second version X’ after obtaining a write lock on X.Other transactions continue to read X.T is ready to commit so it obtains a certify lock on X’.The committed version X becomes X’.T releases its certify lock on X’, which is X now.read/write locking scheme read/write/certify locking scheme

56. Multiversion Two Phase LockingIn multiversion 2PL read and write operations from conflicting transactions can be processed concurrently. This improves concurrency.But it may delay transaction commit because of obtaining certify locks on all its writes. It avoids cascading abort but like strict two phase locking scheme conflicting transactions may get deadlocked.

57. Validation Based Concurrency ControlIn this technique only at the time of commit serializability is checkedTransactions are aborted in case of non-serializable schedules.Each Transaction has the following three phases:Read phaseValidation phaseWrite phase

58. Validation Based Concurrency ControlRead Phase:A transaction can read values of committed data items. However, updates are applied only to local copies (versions) of the data items (in database cache).Validation Phase: Serializability is checked before transactions write their updates to the database.Write Phase:On a successful validation transactions’ updates are applied to the database; otherwise, transactions are restarted.

59. Validation Based Concurrency ControlKey Idea:Make validation atomicIf T1, T2, T3, … is validation order, then resulting schedule will be conflict equivalent to Ss = T1 T2 T3...Aim: get a schedule which is conflict equivalent to a serial schedule where transactions were executed according to the validation point.

60. Validation Based Concurrency ControlTo implement validation, system keeps three sets:START = transactions that have started, but not yet completed validation. START(T): time at which T started.VAL = transactions that have successfully finished phase 2 (validation). VAL(T): time at which T is validated.FIN = transactions that have finished phase 3 (and are all done). FIN(T): time at which T finished.FIN set is periodically purged!

61. Validation Based Concurrency ControlExample of what validation must stop: RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C}timeT2startT2validatedT3 isvalidatingT3start= T2 would be writing in this time

62. Validation Based Concurrency ControlExample of what validation must stop: RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C}timeT2startT2validatedT3 isvalidatingT3start= T2 would be writing in this timeShould T3 pass ? Does it follow the our definition of equivalent serial schedule?

63. Validation Based Concurrency ControlExample of what validation must pass: RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C}= T2finishphase 3timeT2startT2validatedT3validatedT3startT3start

64. Validation Based Concurrency ControlAnother thing validation must prevent: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C,D}timeT2validatedT3Validating?finishT2

65. Validation Based Concurrency ControlAnother thing validation must prevent: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C,D}timeT2validatedfinishT2BAD?? w3(D) w2(D)T3Validating?

66. Validation Based Concurrency ControlAnother thing validation must allow: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C,D}finishT2timeT2validatedT3validatedfinishT2

67. Validation Based Concurrency ControlValidation rules for Tj:(1) When Tj starts phase 1: ignore(Tj)  FIN(2) at Tj Validation: if check (Tj) then [ VAL  VAL U {Tj}; do write phase; FIN FIN U {Tj} ]

68. Validation Based Concurrency ControlCheck (Tj): For Ti  VAL - IGNORE (Tj) DO IF [ WS(Ti)  RS(Tj)   OR Ti  FIN ] THEN RETURN false; RETURN true;

69. Validation Based Concurrency ControlImproving Check(Tj)For Ti  VAL - IGNORE (Tj) DO IF [ WS(Ti)  RS(Tj)   OR (Ti  FIN AND WS(Ti)  WS(Tj)  )] THEN RETURN false;RETURN true;