Concurrency Control Two
Concurrency Control Two
Outline
1. A Model for Concurrency Control 2. Serializability Theory 3. Synchronization Requirements for Recoverability 4. Two-Phase Locking 5. Implementing Two-Phase Locking
6. Locking Performance 7. Multigranularity Locking (revisited) 8. Hot Spot Techniques 9. Query-Update Techniques 10. Phantoms 11. B-Trees 12. Tree locking
2/8/2012 2
You get an update lock by using the UPDLOCK hint in the FROM clause
Select Foo.A From Foo (UPDLOCK) Where Foo.B = 7
2/8/2012 4
Low
High
# of Active Txns
5
More on Thrashing
Its purely a blocking problem
It happens even when the abort rate is low.
2/8/2012
Avoiding Thrashing
Good heuristic:
If over 30% of active transactions are blocked, then the system is (nearly) thrashing so reduce the number of active transactions.
Interesting Sidelights
By getting all locks before transaction Start, you can increase throughput at the thrashing point because blocked transactions hold no locks.
But it assumes that you get exactly the locks you need and that retries of get-all-locks are cheap.
Pure restart policy - abort when theres a conflict and restart when the conflict disappears.
If aborts are cheap and theres low contention for other resources, then this policy produces higher throughput before thrashing than a blocking policy. But response time is greater than a blocking policy.
2/8/2012 8
Use record-level locking (i.e., choose a database system that supports it).
2/8/2012
10
12
DB1 A1
F1 R1.1 R1.2 F2
Area
File Record
A2
F3
Lock Type Lock Instance Graph Graph Before setting a read lock on R2.3, first set an intention-read lock on DB1, then A2, and then F2. Set locks root-to-leaf. Release locks leaf-to-root. 13
2/8/2012
r w ir iw riw
y n y n n
n n n n n
y n y y y
n n y y n
n n y n n
riw = read with intent to write, for a scan that updates some of the records it reads
E.g., ir conflicts with w because ir says theres a finegrained r-lock that conflicts with a w-lock on the container To r-lock an item, need an r-, ir- or riw-lock on its parent To w-lock an item, need a w-, iw- or riw-lock on its parent 14
2/8/2012
MGL Complexities
Relational DBMSs use MGL to lock SQL queries, short updates, and scans with updates. Use lock escalation - start locking at fine-grain and escalate to coarse grain after nth lock is set. The lock type graph is a directed acyclic graph, not a tree, to cope with indices. R-lock one path to an item. W-lock all paths to it.
2/8/2012
File
MS SQL Server
MS SQL Server can lock at table, page, and row level. Uses intention read (share) and intention write (exclusive) locks at the table and page level. Tries to avoid escalation by choosing the appropriate granularity when the scan is instantiated. Table
Index Range
Page
2/8/2012 16
Hot spots often create a convoy of transactions. The hot spot lock serializes transactions.
2/8/2012 17
2/8/2012
18
2/8/2012
19
r y n n n
w n n n n
inc dec n n n n y y y y
But if Inc and Dec have a threshold (e.g. a quantity of zero), then they conflict (when the threshold is near)
20
2/8/2012
22
Batching
Transactions add updates to a mini-batch and only periodically apply the mini-batch to shared data.
Each process has a private data entry file, in addition to a global shared data entry file Each transaction appends to its process file Periodically append the process file to the shared file.
2/8/2012
23
Partitioning
Split up inventory into partitions Each transaction only accesses one partition Example
Each ticket agency has a subset of the tickets If one agency sells out early, it needs a way to get more tickets from other agencies (partitions)
2/8/2012
24
2/8/2012
Data Warehouse
A data warehouse contains a snapshot of the DB which is periodically refreshed from the TP DB All queries run on the data warehouse All update transactions run on the TP DB Queries dont get absolutely up-to-date data How to refresh the data warehouse?
Stop processing transactions and copy the TP DB to the data warehouse. Possibly run queries while refreshing Treat the warehouse as a DB replica and use a replication technique.
2/8/2012 26
Degrees of Isolation
Serializability = Degree 3 Isolation Degree 2 Isolation (a.k.a. cursor stability)
Data manager holds read-lock(x) only while reading x, but holds write locks till commit (as in 2PL) E.g. when scanning records in a file, each get-next-record releases lock on current record and gets lock on next one read(x) is not repeatable within a transaction, e.g., rl1[x] r1[x] ru1[x] wl2[x] w2[x] wu2[x] c2 rl1[x] r1[x] ru1[x] Degree 2 is commonly used by ISAM file systems Degree 2 is often a DB systems default behavior! And customers seem to accept it!!!
2/8/2012 27
Degree 1 - no read locks; hold write locks to commit Unfortunately, SQL concurrency control standards have been stated in terms of repeatable reads and cursor stability instead of serializability, leading to much confusion.
28
2/8/2012
2/8/2012
29
MS SQL Server
Lock hints in SQL FROM clause
All the ANSI isolation levels, plus UPDLOCK - use update locks instead of read locks READPAST - ignore locked rows (if running read committed) PAGLOCK - use page lock when the system would otherwise use a table lock TABLOCK - shared table lock till end of command or transaction TABLOCKX - exclusive table lock till end of command or transaction
2/8/2012 30
Multiversion Data
Assume record granularity locking. Each write operation creates a new version instead of overwriting existing value. So each logical record has a sequence of versions. Tag each record with transaction id of the transaction that wrote that version.
Tid 123 175 134 199 227
2/8/2012
E# 1 1 2 2 27
Other fields
31
2/8/2012
32
2/8/2012
2/8/2012
34
2/8/2012
36
8.10 Phantoms
Problems when using 2PL with inserts and deletes Accounts Assets Acct# Location Balance Location Total 1 Seattle 400 Seattle 400 2 Tacoma 200 Tacoma 500 3 Tacoma 300 The phantom record T1: Read Accounts 1, 2, and 3 T2: Insert Accounts[4, Tacoma, 100] T2: Read Assets(Tacoma), returns 500 T2: Write Assets(Tacoma, 600) T1: Read Assets(Tacoma), returns 600 T1: Commit
2/8/2012 37
Precision Locks
Suppose update operations are on single records
8.11 B-Trees
An index maps field values to record ids.
Record id = [page-id, offset-within-page] Most common DB index structures: hashing and B-trees DB index structures are page-oriented
B-Tree Structure
Index node is a sequence of [pointer, key] pairs K1 < K2 < < Kn-1 < Kn P1 points to a node containing keys < K1 Pi points to a node containing keys in range [Ki-1, Ki) Pn+1 points to a node containing keys > Kn So, K 1 < K 2 < < K n-1 < K n
P1
K1 . . .
Pi
Ki Pi+1
. . . Kn Pn+1
Pn+1
42
P1 K1 . . .
2/8/2012
Pi Ki Pi+1 . . . Kn
Example n=3
127 14 83 496
221
352
521
352 353
690
487
Notice that leaves are sorted by key, left-to-right Search for value v by following path from the root If key = 8 bytes, ptr = 2 bytes, page = 4K, then n = 409 So 3-level index has up to 68M leaves (4093) At 20 records per leaf, thats 136M records
43
2/8/2012
Insertion
To insert key v, search for the leaf where v should appear If theres space on the leave, insert the record If no, split the leaf in half, and split the key range in its parent to point to the two leaves To insert key 15 19 - split the leaf X split the parents range [0, 19) 12 14 17 to [0, 15) and [15, 19) if the parent was full, youd split that too (not shown here) 15 19 X this automatically keeps the tree balanced 12 14 15 17
2/8/2012 44
B-Tree Observations
Delete algorithm merges adjacent nodes < 50% full, but rarely used in practice Root and most level-1 nodes are cached, to reduce disk accesses In a primary (clustered) index, leaves contain records In a secondary (non-clustered) index, leaves contain [key, record id] pairs or [key, primary-key] pairs. Use key prefix for long (string) key values
Drop prefix and add to suffix as you move down the tree
2/8/2012 45
Commonly used with multi-granularity locking Insert/delete locks record and intention-write locks range MGL tree defines a fixed set of predicates, and thereby avoids predicate satisfiability
2/8/2012 46
B
E F
B-tree Locking
Root lock on a B-tree is a bottleneck Use tree locking to relieve it Problem: node splits P If you unlock P before splitting C, 19 -then you have to back up and lock C X P again, which breaks the tree 12 14 17 locking protocol. So, dont unlock a node till youre sure its child wont split (i.e. has space for an insert) Implies different locking rules for different ops (search vs. insert/update)
2/8/2012 48
B-link Optimization
B-link tree - Each node has a side pointer to the next After searching a node, you can release its lock before locking its child r1[P] r2[P] r2[C] w2[C] w2[C] w2[P] r1[C] r1[C] P 19 -X P 15 19 C 15 17
C 12 14 17
C 12 14
Searching has the same behavior as if it locked the child before releasing the parent and ran later (after the insert)
2/8/2012 49