Understanding Lock-Based Protocols in Database Concurrency Control

Slide Note
Embed
Share

Lock-based protocols are essential mechanisms for controlling concurrent access to data items in a database system. This involves granting locks in exclusive (X) or shared (S) modes to ensure data integrity and prevent conflicts. Lock compatibility matrices and locking protocols play a crucial role in managing transactions and guaranteeing serializability. The 2-Phase Locking Protocol follows a structured approach of growing and shrinking phases to manage lock acquisition and release effectively.


Uploaded on Aug 04, 2024 | 2 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


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

  2. Lock Based Protocols A lock is a mechanism to control concurrent access to a data item Data 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 Matrix Lock-compatibility matrix Lock granted if the requested lock is compatible with locks already held on the item by other transactions Multiple shared locks on an item But only one exclusive on an item If a lock cannot be granted, then need to wait.

  4. Lock Compatibility Matrix Example 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); If T2 was executed here in midst A = A + 50; write(A); unlock(A);

  5. Lock Compatibility Matrix Locking 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 Protocol Phase 1: Growing Phase Transaction may obtain locks Transaction may not release locks Phase 2: Shrinking Phase Transaction may release locks Transaction may not obtain locks

  7. 2 Phase Locking Protocol: example 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); T2: lock-S(A); read (A); unlock(A); lock-S(B); read (B); unlock (A) unlock(B); display(A+B) Does this guarantee serializability?

  8. 2 Phase Locking Protocol This 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. T3: lock-X(B); read (B); B = B 50; Lock-X(A) read(A); A = A + 50; write(A); unlock(B); unlock(A); T2: Does 2 Phase Locking support recoverability And cascadeless? Time lock-S(A); read (A); lock-S(B); read (B); unlock (A) unlock(B); display(A+B);Commit; Abort;

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

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

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

  13. Automatic Acquisition of Locks A transaction Ti issues the standard read/write instruction, without explicit locking calls. The operation read(D) is processed as: ifTi 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 Locks write(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 Locking Allow data items to be of various sizes and define a hierarchy of data granularities, Small granularities are nested within larger ones Represented 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 Locking Granularityof locking (level in tree where locking is done): fine granularity (lower in tree): high concurrency, high locking overhead coarse granularity (higher in tree): low locking overhead, low concurrency

  18. Multiple granularity of Locking DB f1 f2 p11 p12 ... p1n p11 p12 ... p1n r111 ... r11j r111 ... r11j r111 ... r11j r111 ... r11j r111 ... r11j r111 ... r11j Granularities Entire database Entire file A disk block A database record

  19. Multiple granularity of Locking To 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 Locking SIX Implicitly S Implicitly S X You 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.

  21. Multiple granularity of Locking These locks are applied using the following compatibility matrix: IS IX S SIX X yes yes yes yes no yes yes no no no yes no yes no no yes no no no no no no no no no IS -- Intention- shared IX -- Intention- exclusive SIX -- Shared- intention- exclusive IS IX S SIX X

  22. Multiple granularity of Locking --- Rules 1. The lock compatibility must adhered to. 2. The root of the tree must be locked first, in any mode.. 3. 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. 4. 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. 5. T can lock a node only if it has not unlocked any node (to enforce 2PL policy). 6. T can unlock a node, N, only if none of the children of N are currently locked by T. 7. locks need to released bottom first.

  23. Some Examples on Multi-granular Locking Query 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 SIX Now 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 Locking Query 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 locking Neither 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 locking 2PL 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 Handling Deadlock 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 Schemes Following schemes use transaction timestamps. wait-die scheme non-preemptive older 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 Schemes wound-wait scheme preemptive Oder 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 Schemes Both 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 ones Thus starvation is avoided. Timeout-Based Schemes: Transaction waits for a lock only for a specified amount of time.

  31. Deadlock Detection Deadlocks can be described as a wait-for graph, If Ti Tjis 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 cycle Wait-for graph with a cycle

  32. Deadlock Recovery When 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 transaction Total 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 Algorithm Each 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 Algorithm In 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 Algorithm This protocol ensures that any conflicting read and write operations are executed in timestamp order. Suppose a transaction Ti issues a read(Q) 1. If TS(Ti) < W-timestamp(Q). 2. If TS(Ti) W-timestamp(Q)

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

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

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

  39. Apply TSO Algorithm on following Schedule Transaction T1 Transaction T2 Read_item(Y) Write_item(Y) Assume Read_item(X) Write_item(X) T1 arrives at time t=3 T2 arrives at time t=1 Read_item(X) Time Read_item(Y) Write_item(Y) Write_item(X)

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

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

  42. TSO Algorithm Thomas Write Rule Modified 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 Rule Thomas' 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 viewserializable if it is viewequivalent 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, 1.Transaction Tireads the initial value of Q in both schedule S and S . 2.Transaction Ti should consume (read(Q)) the same output (write(Q)) of Tjin both S and S . 3.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 serializableif 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 Schemes Multiversion schemes keep old versions of data item to increase concurrency. Multiversion Timestamp Ordering Multiversion Two-Phase Locking Each successful write results in the creation of a new version of the data item written. Use timestamps to label versions.

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

  49. Multiversion Time Stamp Ordering Each 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 Qk R-timestamp(Qk) -- largest timestamp of a transaction that successfully read version Qk

  50. Multiversion Time Stamp Ordering When a transaction Ticreates 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).

More Related Content