Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurrency Control Chapter 17

Similar presentations


Presentation on theme: "Concurrency Control Chapter 17"— Presentation transcript:

1 Concurrency Control Chapter 17
The slides for this text are organized into chapters. This lecture covers Chapter 17. The slides present the material suitable at the level of a graduate course; Concurrency control in B+ Trees, optimistic concurrency control, and multiversion concurrency control are explained in detail. For an introductory undergraduate course, we usually cover only the material in slides 1 to 20. The foundational systems material on buffer management and disks should be covered first, as also the overview of transaction processing in Chapter 16. However, this material does not depend on the query processing and optimization material. The discussion of B+ tree concurrency control, of course, depends on an understanding of the earlier indexing chapters.

2 Review Transactions and ACID properties
Serial execution and serializable execution Conflict equivalence and view equivalence Serializability (dependency) graph Serializability theorem Properties of schedules: SR, RC, ACA, ST Schedulers 3 options to handle the request from TM What are the options? immediate, delay, reject optimistic (aggressive) vs pessimistic (conservative) 2PL Correctness of schedulers Correctness of 2PL

3 Review: Strict 2PL Strict Two-phase Locking (Strict 2PL) Protocol:
Each Xact must obtain a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing. All locks held by a transaction are released when the transaction completes If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object. Strict 2PL allows only schedules whose precedence graph is acyclic

4 Lock Management Lock and unlock requests are handled by the lock manager Lock table entry: Number of transactions currently holding a lock Type of lock held (shared or exclusive) Pointer to queue of lock requests Locking and unlocking have to be atomic operations Lock upgrade: transaction that holds a shared lock can be upgraded to hold an exclusive lock

5 Deadlocks Deadlock: Cycle of transactions waiting for locks to be released by each other. T1=r1(x)w1(y)C1; T2=r2(y)w2(x)C2 How can a deadlock occur? Four conditions for deadlock: Mutual exclusion Hold and wait No preemption Circular wait Are they necessary conditions, sufficient, or necessary and sufficient conditions?

6 Deadlocks Three ways of dealing with deadlocks:
Deadlock detection and resolution Deadlock prevention Deadlock avoidance Deadlock detection Time-out: no detection, just guessing Chances of aborting a transaction that is not involved in a deadlock Wait-for graph Precise detection Large overhead

7 Deadlock Detection by WFG
Create a waits-for graph: Nodes are transactions There is an edge from Ti to Tj if Ti is waiting for Tj to release a lock Periodically check for cycles in the waits-for graph --- How often?

8 Deadlock Detection using WFG
Example: T1: S(A), R(A), S(B) T2: X(B),W(B) X(C) T3: S(C), R(C) X(A) T4: X(B) T1 T2 T1 T2 T4 T3 T4 T3

9 Deadlock Prevention Priority-based scheme: allow Ti wait for Tj only if Ti has a higher priority than Tj. Otherwise, abort Ti. Deadlock is not possible. Why? If T1->T2-> … ->Tn->T1 in WFG, then P(T1)>P(T2) ..>P(Tn)>P(T1) Typically implemented by using timestamp -- Assign priorities based on timestamps. Timestamps A monotonically increasing number Finite number of smaller timestamps Priority of T is the inverse of its timestamp. Why?

10 Deadlock Prevention using Timestamps
Assign priorities based on timestamps. Assume Ti wants a lock that Tj holds. Two policies are possible: Wait-Die: It Ti has higher priority, Ti waits for Tj; otherwise Ti aborts Wound-wait: If Ti has higher priority, Tj aborts; otherwise Ti waits In both cases, younger transaction is aborted. Why? If a transaction re-starts, make sure it has its original timestamp. Why?

11 Performance Issues Thrashing Resource contention Data contention
Policy Blocking policy vs restart policy If low resource contention but severe data contention -- which policy will perform better? Restart policy – surprising? Blocking is selfish, restarting is self-sacrificing Impact of granularity on performance Tree locking – why important?

12 Multiple-Granularity Locks
Why consider it? Database consists of tables, pages, tuples (records) Hard to decide what granularity to lock (tuples vs. pages vs. tables). Shouldn’t have to decide. How? Data “containers” are nested: Tuples Tables Pages Database contains

13 Solution: New Lock Modes
How to ensure that a page is not locked if another T holds a conflicting lock on the table? Exploit hierarchical nature: lock at each level, using new “intention” locks. Before locking an item, T must set “intention locks” on all its ancestors. For unlock, go from specific to general (i.e., bottom-up). SIX mode: Like S & IX at the same time. -- IS IX Ö S X

14 Multiple Granularity Lock Protocol
Each T starts from the root of the hierarchy. To get S or IS lock on a node, must hold IS or IX on parent node. What if T holds SIX on parent? S on parent? To get X or IX or SIX on a node, must hold IX or SIX on parent node. When can a lock released? To ensure SR, multi-granularity locking must be used with 2PL, which dictates when a lock can be released Must release locks in bottom-up order.

15 Example T1 scans R, and updates a few tuples:
T1 gets an SIX lock on R, then repeatedly gets an S lock on tuples of R, and occasionally upgrades to X on the tuples. T2 uses an index to read only part of R: T2 gets an IS lock on R, and repeatedly gets an S lock on tuples of R. T3 reads all of R: T3 gets an S lock on R. OR, T3 could behave like T2; can use lock escalation. -- IS IX Ö S X

16 Dynamic Databases If we relax the assumption that the DB is a fixed collection of objects, even Strict 2PL will not assure serializability. Why? T1 locks all pages containing sailor records with rating = 1, and finds oldest sailor (say, age = 71). Next, T2 inserts a new sailor; rating = 1, age = 96. T2 also deletes oldest sailor with rating = 2 (and, say, age = 80), and commits. T1 now locks all pages containing sailor records with rating = 2, and finds oldest (say, age = 63). What’s the problem in this example? No consistent DB state where T1 is “correct”! For T1, it’s like dealing with a ghost/phantom

17 The Phantom Problem T1 implicitly assumes that it has locked the set of all sailor records with rating = 1. Assumption only holds if no sailor records are added while T1 is executing! Why did this problem happen? Example shows that conflict serializability guarantees serializability only if the set of objects is fixed! Need some mechanism to enforce this assumption -- index locking and predicate locking

18 Index Locking Index Data r=1 If there is a dense index on the rating field using Alternative (2), T1 should lock the index page containing the data entries with rating = 1. If there are no records with rating = 1, T1 must lock the index page where such a data entry would be, if it existed! What if there is no suitable index? T1 must lock all pages, and lock the file/table to prevent new pages from being added, to ensure that no new records with rating = 1 are added.

19 Predicate Locking Grant lock on all records that satisfy some logical predicate, e.g. age > 2*salary. Index locking is a special case of predicate locking for which an index supports efficient implementation of the predicate lock. What is the predicate in the sailor example? rating=1 Why not using predicate locks in commercial DBMS? In general, predicate locking has a significant locking overhead.

20 Locking in B+ Trees How can we efficiently lock a particular leaf node? Don’t confuse this with multiple granularity locking -- How are they different? One solution: Ignore the tree structure, just lock pages while traversing the tree, following 2PL -- What’s wrong? This has terrible performance! Root node (and many higher level nodes) becomes a bottleneck. Why? Because every tree access begins at the root.

21 Two Useful Observations
Higher levels of the tree only direct searches for leaf pages. For inserts, a node on a path from root to modified leaf must be locked (in X mode, of course), only if a split can propagate up to it from the modified leaf. (Similar point holds w.r.t. deletes.) We can exploit these observations to design efficient locking protocols that guarantee serializability even though they violate 2PL.

22 A Simple Tree Locking Algorithm
Search: Start at root and go down; repeatedly, S lock child then unlock parent. Insert/Delete: Start at root and go down, obtaining X locks as needed. Once child is locked, check if it is safe: If child is safe, release all locks on ancestors. Safe node: Node such that changes will not propagate up beyond this node. When is a node safe for inserts? Node is not full. When is a node safe for deletes? Node is not half-empty.

23 Example A B F C H G I D E ROOT 20 Do: 1) Search 38* 2) Insert 45*
3) Delete 38* B 35 F C 23 38 44 H G I D E 20* 22* 23* 24* 35* 36* 38* 41* 44*

24 A Better Tree Locking Algorithm
Search: As before. Insert/Delete: Set S locks as if for search, get to leaf, and set X lock on leaf. If leaf is not safe, release all locks, and restart T using previous Insert/Delete protocol. Gambles that only leaf node will be modified; if not, S locks set on the first pass to leaf are wasteful. In practice, better than previous algorithm

25 Example A B F C H G I D E ROOT 20 Do: 1) Delete 38* 2) Insert 25* 35
23 38 44 H G I D E 20* 22* 23* 24* 35* 36* 38* 41* 44*

26 Optimistic CC (Kung-Robinson)
Locking is a conservative approach in which conflicts are prevented. Disadvantages of locking? Lock management overhead. Deadlock detection/resolution. Lock contention for heavily used objects. Alternatives? Non-locking (optimistic) approach Idea: If conflicts are rare, we might be able to gain concurrency by not locking, and instead checking for conflicts just before transaction commits.

27 Kung-Robinson Model Transactions have three phases:
READ: transactions read from the database, but make changes to private copies of objects. VALIDATE: Check for conflicts. WRITE: Make local copies of changes public. old modified objects ROOT new

28 Validation Test conditions that are sufficient to ensure that no conflict occurred. Each T is assigned a numeric id. Typically, use a timestamp. Transaction ids are assigned at end of READ phase, just before validation begins. ReadSet(Ti): Set of objects read by Ti. WriteSet(Ti): Set of objects modified by Ti. To validate Tj, check to see that 1 of 3 conditions holds with each committed transaction Ti such that TS(Ti) < TS(Tj)

29 Test 1 For all i and j such that Ti < Tj, check that Ti completes before Tj begins. Ti Tj R V W R V W

30 Test 2 Ti Tj For all i and j such that Ti < Tj, check that:
Ti completes before Tj begins its Write phase + WriteSet(Ti) ReadSet(Tj) is empty. Ti R V W Tj R V W Does Tj read while Ti writes? Is it a problem? Does Tj overwrite Ti’s writes? Is it a problem?

31 Test 3 Ti Tj For all i and j such that Ti < Tj, check that:
Ti completes Read phase before Tj does + WriteSet(Ti) ReadSet(Tj) is empty + WriteSet(Ti) WriteSet(Tj) is empty. Ti R V W Tj R V W Does Tj read while Ti writes? Is it a problem? Does Tj overwrite Ti’s writes? Is it a problem?

32 Validation < foreach Ts in S do { To validate transaction T:
valid = true; // S = set of Xacts that committed after Begin(T) < foreach Ts in S do { if ReadSet(T) intersect WriteSet(Ts) then valid = false; } if valid then { install updates; // Write phase Commit T } > else Restart T end of critical section

33 Comments on Serial Validation
Applies Test 2, with T playing the role of Tj and each Xact in Ts (in turn) being Ti. Assignment of Xact id, validation, and the Write phase are inside a critical section! I.e., Nothing else goes on concurrently. If Write phase is long, major drawback. Optimization for Read-only transactions: Don’t need critical section (because there is no Write phase).

34 Overheads in Optimistic CC
Must record read/write activity in ReadSet and WriteSet per transaction. Must create and destroy these sets as needed. Must check for conflicts during validation, and must make validated writes ``global’’. Critical section can reduce concurrency. Scheme for making writes global can reduce clustering of objects. Optimistic CC restarts Xacts that fail validation. Work done so far is wasted; requires clean-up.

35 ``Optimistic’’ 2PL If desired, we can do the following:
Set S locks as usual. Make changes to private copies of objects. Obtain all X locks at end of T, make writes global, then release all locks. In contrast to Optimistic CC as in Kung-Robinson, this scheme results in transactions being blocked, waiting for locks. However, no validation phase, no restarts (modulo deadlocks).

36 Timestamp Ordering Idea: Any conflicting operations are executed in their timestamp order Simple and aggressive Schedule immediately and reject requests that arrive too late How do you know a request has arrived too late? Give each object a read-timestamp (RTS) and a write-timestamp (WTS), give each transaction a timestamp (TS) when it begins

37 Timestamp Ordering Timestamp ordering rule:
If Oi(x) and Oj(x) are conflicting operations, Oi(x) is processed before Oj(x), if and only if TS(Ti) < TS(Tj). Request arriving too late: Oi(x) arrives after the scheduler has sent conflicting operation Oj(x) with TS(Tj) > TS(Ti)

38 Basic Timestamp Ordering
Ri(x): if TS(Ti) < WTS(x), reject it; otherwise (TS(Ti) >=WTS(x)), then schedule it and set RTS(x) to max (RTS(x), TS(Ti)) Wi(x): if TS(Ti) < RTS(x) or TS(Ti) <WTS(x), reject it; otherwise (TS(Ti) >=WTS(x)), then schedule it and set WTS(x) to max (WTS(x), TS(Ti)) When restarted, Ti is assigned a new timestamp Thomas Write Rule: For wi(x), if TS(Ti) < WTS(x) and TS(Ti) >= RTS(x), then wi(x) can be ignored, rather than being rejected. Why is it correct? Deleting obsolete write Let’s see each of them in more detail

39 When T wants to read Object O
If TS(T) < WTS(O), this violates timestamp order of T w.r.t. writer of O. So, abort T and restart it with a new, larger TS. If restarted with same TS, what will happen? T will fail again! Contrast use of timestamps in 2PL for deadlock prevention. If TS(T) > WTS(O): Allow T to read O. Set RTS(O) to max(RTS(O), TS(T)) Change to RTS(O) on reads must be written to disk -- overheads.

40 When T wants to Write Object O
If TS(T) < RTS(O), this violates timestamp order of T w.r.t. writer of O; abort and restart T. If TS(T) < WTS(O), violates timestamp order of T w.r.t. writer of O. Else, allow T to write O.

41 Thomas Write Rule If TS(T) > RTS(O) and TS(T) < WTS(O), we can ignore this write operation, rather than rejecting it, causing the abort of T. T’s write is effectively followed by another write, with no intervening reads RTS(X): 6; WTS(X): 9 W8(X) from T8. X

42 Timestamp CC and Recoverability
Unfortunately, unrecoverable schedules are allowed: w1(x)r2(x)w2(y) C2 T T2 W(x) R(x) W(y) Commit Timestamp CC can be modified to become strict: It does not schedule any operations conflicting with wi(x) until Ti terminates. Similar to writers holding X locks until commit – Is it same as 2PL?

43 Exercise: Non-equivalence of 2PL and TO
H1=r2(x) w3(x) C3 w1(y)C1 r2(y) w2(y) C2 Is this schedule possible to timestamp ordering? Is it possible with 2PL? H1 is legal with strict timestamp ordering. What is the equivalent serial schedule? T1 T2 T3 It is not possible with 2PL T2 must release lock on x for T3, but then gets lock on y – violation of two-phaseness

44 Relationship between 2PL and TO
Schedules generated by 2PL and TO They are all correct (serializable) They are not the same set: H1 shows that Is the relationship inclusive? S {schedules by 2PL} subset of S {schedules by TO}? S {schedules by TO} subset of S {schedules by 2PL}? Consider w3(x) C3 w2(x) C2 r1(x) Is it legal with TO? Is it legal with 2PL? Two sets of schedules are intersecting, but subset of SR 2PL SR TO

45 Multi-version Data Objects
Objectives Improve system responsiveness by providing multiple versions (increased degree of concurrency) Reduce the probability of conflicts and rejection of tardy transactions by successive versions of data Maintenance of multiple versions Each write generates a new version System selects an appropriate version to read Potential problems Coordination for consistency Storage and processing overhead

46 Multi-version Timestamp Ordering
MVTO translates operation on data objects into version operations ri(X) => ri(Xk) Where Xk is the version of X with largest timestamp not greater than TS(Ti) Example: w10(X) C10 w15(X) C15 r12(X) wi(X) Case 1: if rj(Xk) with TS(Tk)<TS(Ti)<TS(Tj) already processed, reject wi(X). Why? Case 2: otherwise, wi(X)=>wi(Xi) Example: w10(X) C10 r15(X) w12(X) – what to do with w12(x)?

47 Multiversion Timestamp Ordering
Idea: Let writers make a “new” copy while readers use an appropriate “old” copy: MAIN SEGMENT (Current versions of DB objects) VERSION POOL (Older versions that may be useful for some active readers.) O’ O O’’ Readers are always allowed to proceed.

48 Reader Xact T For each object to be read:
old new WTS timeline Reader Xact T For each object to be read: Finds newest version with WTS < TS(T). (Starts with current version in the main segment and chains backward through earlier versions.) Assuming that some version of every object exists from the beginning of time, Reader Xacts are never restarted. However, might block until writer of the appropriate version commits.

49 Writer Xact To read an object, follows reader protocol.
To write an object: Finds newest version V s.t. WTS < TS(T). If RTS(V) < TS(T), T makes a copy CV of V, with a pointer to V, with WTS(CV) = TS(T), RTS(CV) = TS(T). (Write is buffered until T commits; other Xacts can see TS values but can’t read version CV.) Else, reject write. old new WTS CV V T RTS(V)

50 Summary There are several lock-based concurrency control schemes (Strict 2PL, 2PL). Conflicts between transactions can be detected in the dependency graph The lock manager keeps track of the locks issued. Deadlocks can either be prevented or detected. Naïve locking strategies may have the phantom problem

51 Summary (Contd.) Index locking is common, and affects performance significantly. Needed when accessing records via index. Needed for locking logical sets of records (index locking/predicate locking). Tree-structured indexes: Straightforward use of 2PL very inefficient. There is potential for improvement. In practice, better techniques have been developed; do record-level, rather than page-level locking.

52 Summary (Contd.) Multiple granularity locking reduces the overhead involved in setting locks for nested collections of objects (e.g., a file of pages); should not be confused with tree index locking! Optimistic CC aims to minimize CC overheads in an ``optimistic’’ environment where reads are common and writes are rare. Optimistic CC has its own overheads however; real systems typically use locking. Exception: Real-time databases SQL provides different isolation levels that control the degree of concurrency

53 Summary (Contd.) Timestamp CC is another alternative to 2PL; allows some serializable schedules that 2PL does not (converse is also true). Ensuring recoverability with Timestamp CC requires ability to block Xacts, which is similar to locking. Multiversion Timestamp CC is a variant which ensures that read-only Xacts are never restarted; they can always read a suitable older version. Additional overhead of version maintenance.


Download ppt "Concurrency Control Chapter 17"

Similar presentations


Ads by Google