Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database System Implementation CSE 507

Similar presentations


Presentation on theme: "Database System Implementation CSE 507"— 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 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); A = A + 50; write(A); unlock(A); If T2 was executed here in midst

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

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

11 Rigorous 2 Phase Locking
T3: lock-X(B); read (B); B = B – 50; Lock-X(A) read(A); A = A + 50; write(A); unlock(B); unlock(A); Abort; T2: lock-S(A); read (A); lock-S(B); read (B); unlock (A) unlock(B); display(A+B);Commit; 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

12 Lock Conversions Two-phase locking with lock conversions:
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: 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; 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 upgrade lock on D to lock-X else grant Ti a lock-X on 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
Granularity of 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
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
These locks are applied using the following compatibility matrix: Intention-shared (IS Intention-exclusive (IX) Shared-intention-exclusive (SIX) SIX Implicitly S Implicitly S X Consider SIX as an intermediate locking status between marking a node X and IX

21 Multiple granularity of Locking
Following rules to be followed for producing serializable schedule: The 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.

22 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.

23 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.

24 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.

25 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

26 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.

27 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.

28 Deadlock Detection Deadlocks 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 cycle Wait-for graph with a cycle

29 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

30 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.

31 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.

32 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) If TS(Ti) < W-timestamp(Q). If TS(Ti)  W-timestamp(Q)

33 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) If TS(Ti) < W-timestamp(Q). Read operation is rejected, and Ti is rolled back. If TS(Ti)  W-timestamp(Q), Read operation is executed R-timestamp(Q) is set to max(R-timestamp(Q), TS(Ti)).

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

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

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

37 TSO Algorithm Recoverability and Cascadeless
Problem with timestamp-ordering protocol: Suppose Ti aborts, but Tj has read a data item written by Ti Then 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 abort This can lead to cascading rollback --- that is, a chain of rollbacks

38 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

39 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.

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

41 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.

42 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’.

43 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.

44 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.

45 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.

46 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

47 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).

48 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.

49 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 overwritten else a new version of Q is created.

50 Multiversion Time Stamp Ordering
Observe that Reads always succeed A 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

51 Multiversion Two Phase Locking
Main 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.

52 Multiversion Two Phase Locking
Steps: 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

53 Multiversion Two Phase Locking
In 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.

54 Validation Based Concurrency Control
In this technique only at the time of commit serializability is checked Transactions are aborted in case of non-serializable schedules. Each Transaction has the following three phases: Read phase Validation phase Write phase

55 Validation Based Concurrency Control
Read 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.

56 Validation Based Concurrency Control
Key Idea: Make validation atomic If 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.

57 Validation Based Concurrency Control
To 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!

58 Validation Based Concurrency Control
Example of what validation must stop: =  RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C} T2 start T2 validated T3 is validating T3 start time T2 would be writing in this time

59 Validation Based Concurrency Control
Example of what validation must stop: Should T3 pass ? Does it follow the our definition of equivalent serial schedule? =  RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C} T2 start T2 validated T3 is validating T3 start time T2 would be writing in this time

60 Validation Based Concurrency Control
Example of what validation must pass: =  RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C} T2 start T2 validated T3 validated T3 start T2 finish phase 3 T3 start time

61 Validation Based Concurrency Control
Another thing validation must prevent: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C,D} T2 validated T3 Validating? finish T2 time

62 Validation Based Concurrency Control
Another thing validation must prevent: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C,D} T2 validated T3 Validating? BAD?? w3(D) w2(D) finish T2 time

63 Validation Based Concurrency Control
Another thing validation must allow: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C,D} T2 validated T3 validated finish T2 finish T2 time

64 Validation Based Concurrency Control
Validation 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} ]

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

66 Validation Based Concurrency Control
Improving 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;


Download ppt "Database System Implementation CSE 507"

Similar presentations


Ads by Google