Lesson04-Concurrency Control
Lesson04-Concurrency Control
Email: [email protected]
Twitter: @EricUmuhoza
1 / 76
Concurrency Control
Concurrency Control
Concurrency is fundamental
2 / 76
Concurrency Control
Concurrency Control
3 / 76
Concurrency Control
Concurrent Executions
4 / 76
Concurrency Control
Concurrency Control
5 / 76
Concurrency Control
6 / 76
Concurrency Control
Example of Concurrency
• Two transactions:
T1:
UPDATE a c c o u n t
SET b a l a n c e = b a l a n c e + 3
WHERE c l i e n t = ’ Smith ’
T2:
UPDATE a c c o u n t
SET b a l a n c e = b a l a n c e + 6
WHERE c l i e n t = ’ Smith ’
7 / 76
Concurrency Control
Serial Executions
b o t ( T1 ) b o t ( T2 )
r (D, x ) r (D, y )
x=x+3 y=y+6
w( x ,D) w( y , D) D0 = 0
commit commit
e o t ( T1 ) e o t ( T2 )
8 / 76
Concurrency Control
9 / 76
Concurrency Control
S=100
1. T1: R(S,V1) T1: UPDATE account
SET balance = balance + 3
2. V1 = V1 + 3
WHERE client = ’Smith’
3. T2: R(S,V2)
T2: UPDATE account
4. V2 = V2 + 6 SET balance = balance + 6
WHERE client = ’Smith’
5. T1: W(V1,S) S=103
6. T2: W(V2,S) S=106!
10 / 76
Concurrency Control
11 / 76
Concurrency Control
Dirty Read
S=100
1. T1: R(S,V1)
T1: UPDATE account
2. T1: V1 = V1 + 3 SET balance = balance + 3
3. T1: W(V1,S) S=103 WHERE client = ’Smith’
12 / 76
Concurrency Control
Non-repeatable Read
S=100
T1: SELECT balance FROM account
1. T1: R(S,V1) S=100 WHERE client = ’Smith’
UPDATE account
2. T2: R(S,V2) S=100 SET balance = balance + 3
3. T2: V2 = V2 + 6 WHERE client = ’Smith’
4. T2: W(V2,S) S=106 T2: UPDATE account
SET balance = balance + 6
5. T1: R(S,V3) V36=V1 WHERE client = ’Smith’
6. ...
For T1, S assumes the value 100 after the first read and the value
106 after the second read. Instead, it is convenient that a transaction
that accesses the database twice finds exactly the same value for
each piece of data read, and is not affected by the other transaction.
13 / 76
Concurrency Control
Phantom Update
• Consider a DB with three objects, X, Y and Z which satisfy an
integrity constraint: X+Y+Z=100
• Initial state: X=50, Y=30, Z=20
T1: R(X,V1), R(Y,V2)
T2: R(Y,V3), R(Z,V4)
T2: V3 = V3 + 10, V4=V4-10
T2: W(V3,Y), W(V4,Z) (Y=40, Z=10)
T1: R(Z,V5) (But for T1, X+Y+Z=90!)
T1 observes only some of the effects of T2, and thus observes a state
that does not satisfy the integrity constraints. This anomaly is called
a Phantom or Ghost update.
14 / 76
Concurrency Control
Anomalies
15 / 76
Concurrency Control
Preliminary Definitions
Schedule
• A Schedule is sequence of input/output operations
performed by concurrent transactions.
• E.g., S1 : r1 (x) r2 (z) w1 (x) w2 (z)
16 / 76
Concurrency Control
S2 : r0 (x) r0 (y) w0 (z) r1 (y) r1 (x) w1 (y) r2 (x) r2 (y) r2 (z) w2 (z)
17 / 76
Concurrency Control
18 / 76
Concurrency Control
• Note
– Efficiency: cost of equivalence checking
– Efficacy: classes of acceptable schedules
The larger the acceptable classes of schedules the higher the costs
of equivalence checking
19 / 76
Concurrency Control
Assumption
20 / 76
Concurrency Control
View-serializability
Preliminary definition
21 / 76
Concurrency Control
Examples of View-serializability
22 / 76
Concurrency Control
Examples of View-serializability 2
23 / 76
Concurrency Control
• W0 (x),r1 (x),w0 (z),r1 (z),r2 (x),w0 (y),r3 (z),w3 (z),w2 (y),w1 (x),w3 (y)
• W0 (x),w0 (z),w0 (y),r1 (x),r1 (z),w1 (x),r2 (x),w2 (y),r3 (z),w3 (z),w3 (y)
24 / 76
Concurrency Control
• W0 (x),r1 (x),w0 (z),r1 (z),r2 (x),w0 (y),r3 (z),w3 (z),w2 (y),w1 (x),w3 (y)
• W0 (x),w0 (z),w0 (y),r2 (x),w2 (y),r1 (x),r1 (z),w1 (x),r3 (z),w3 (z),w3 (y)
25 / 76
Concurrency Control
Complexity of View-serializability
26 / 76
Concurrency Control
Conflict-serializability
Preliminary definition
27 / 76
Concurrency Control
Conflict-serializability
28 / 76
Concurrency Control
View-serializable: view-equivalent to
r1 (x) w1 (x) w2 (x) w3 (x)
29 / 76
Concurrency Control
30 / 76
Concurrency Control
Testing Conflict-serializability
Theorem
A schedule is in CSR iff its conflict graph is acyclic.
31 / 76
Concurrency Control
Example Test
w0 (x),r1 (x),w0 (z),r1 (z),r2 (x),w0 (y),r3 (z),w3 (z),w2 (y), w1 (x),w3 (y)
32 / 76
Concurrency Control
33 / 76
Concurrency Control
34 / 76
Concurrency Control
35 / 76
Concurrency Control
Locking
36 / 76
Concurrency Control
Lock Primitives
Primitives
• r lock: read lock
• w lock: write lock
• unlock
37 / 76
Concurrency Control
38 / 76
Concurrency Control
Two-Phase Locking
• Requirements:
– A transaction cannot acquire any other lock after releasing a lock
– Locks on a transaction can be released only after commit/abort
operations
39 / 76
Concurrency Control
Serializability
• If a scheduler
1 uses well-formed transactions;
2 grants locks according to conflicts; and
3 is two-phase
• Then it produces the schedule class called 2PL
40 / 76
Concurrency Control
T3 < T1 < T2
41 / 76
Concurrency Control
42 / 76
Concurrency Control
Strict 2PL
43 / 76
Concurrency Control
44 / 76
Concurrency Control
45 / 76
Concurrency Control
• Writes are always applied strict 2PL (so update loss is avoided)
• READ UNCOMMITTED allows dirty reads, non repeatable reads
and phantoms:
– No read lock (and ignores locks of other transactions)
• READ COMMITTED avoids dirty reads but allows non
repeatable reads and phantoms:
– Read locks (and complies with locks of other transactions), but
without 2PL
• REPEATABLE READ avoids dirty reads, non repeatable reads
and phantom updates, but allows phantom inserts:
– 2PL also for reads, with data locks
• SERIALIZABLE avoids all anomalies (default):
– 2PL with predicate locks
On the entire relation
On the index
46 / 76
Concurrency Control
Predicate Lock
47 / 76
Concurrency Control
Hierarchical Locking
48 / 76
Concurrency Control
Hierarchical Locking
• Concept:
– Locking can be done upon objects at various levels of granularity.
• Objectives:
– Setting the minimum number of lockings
– Recognizing conflicts as soon as possible
• Organization: asking locking upon resources organized as a
hierarchy
– Requesting resources top-down until the right level is obtained
– Releasing locks bottom-up.
49 / 76
Concurrency Control
50 / 76
Concurrency Control
51 / 76
Concurrency Control
52 / 76
Concurrency Control
1 Lock requests are done starting from the root and going down in
the hierarchy.
2 Locks are released from the lowest level and then going up in the
hierarchy.
3 To request an SL or ISL lock on a non-root node, a transaction
must hold an ISL or IXL lock on its parent node.
4 To request an IXL, XL or SIXL lock on a non-root node, a
transaction must hold a SIXL or IXL lock on its parent node.
53 / 76
Concurrency Control
54 / 76
Concurrency Control
Example
• Page 1: T1,T2,T3,T4
• Page 2: T5,T6,T7,T8
• Transaction 1:
1 read(P1)
2 write(T3)
3 read(T8)
• Transaction 2:
1 read(T2)
2 read(T4)
3 write(T5)
4 write(T6)
They are NOT in conflict!
55 / 76
Concurrency Control
Lock Sequences
• Transaction 2:
1 IXL(root) – OK, (IXL,IXL)
2 ISL(P1) – OK, (ISL,SIXL)
3 SL(T2) – OK, Free
4 SL(T4) – OK, Free
5 IXL(P2) – OK, (IXL, ISL)
6 XL(T5) – OK, Free
7 XL(T6) – OK, Free
56 / 76
Concurrency Control
Deadlock
57 / 76
Concurrency Control
Deadlock
58 / 76
Concurrency Control
• Timeout
– Transactions killed after a long wait
• Deadlock prevention
– Transactions killed when they could be in deadlock
• Deadlock detection
– Transactions killed when they are in deadlock
59 / 76
Concurrency Control
Timeout Method
60 / 76
Concurrency Control
Deadlock Prevention
61 / 76
Concurrency Control
Deadlock Detection
62 / 76
Concurrency Control
Deadlocks in Practice
63 / 76
Concurrency Control
Update Lock
Request Resource
SL UL XL
SL OK OK No
UL OK No No
XL No No No
64 / 76
Concurrency Control
• Alternative to 2PL
• Timestamp: identifier defining a total ordering of the events of
a system
• Each transaction has a timestamp representing the time at which
the transaction begins
• A schedule is accepted only if it reflects the serial ordering of the
transactions induced by their timestamps
65 / 76
Concurrency Control
Timestamp Mechanism
• The scheduler has two counters for each object: RTM(x) and
WTM(x)
• The scheduler receives read and write requests with timestamp:
– read(x,ts):
If ts < WTM(x) the request is rejected and the transaction killed
Else, the request is granted and RTM(x) is set to max(RTM(x), ts)
– write(x,ts):
If ts < WTM(x) or ts < RTM(x) the request is rejected and the
transaction killed
Else, the request is granted and WTM(x) is set to ts
66 / 76
Concurrency Control
Timestamp: Problems
67 / 76
Concurrency Control
Timestamp: Example
Assume RTM(x) = 7
WTM(x) = 4
68 / 76
Concurrency Control
2PL vs. TS
69 / 76
Concurrency Control
70 / 76
Concurrency Control
2PL vs. TS
71 / 76
Concurrency Control
Idea
Writes generate new copies, reads access the right copy.
• Writes generate new copies, each one with a new WTM. Each
object x always has N > 1 active copies with WTMN (x). There
is a unique global RTM(x)
72 / 76
Concurrency Control
Mechanism
• Read(x,ts) is always accepted. A copy xk is selected for
reading such that:
• If ts > WTMN (x), then k = N
• Else take k such that WTMk (x) < ts < WTMk+1 (x)
• Write(x,ts):
• If ts < RTM(x), the request is rejected
• Else a new version is created (N is incremented)
with WTMN (x) = ts
73 / 76
Concurrency Control
Example
Assume RTM(x) = 7
N=1 WTM(x1 ) = 4
Request Response New value
read(x,6) OK
read(x,8) OK RTM(x) = 8
read(x,9) OK RTM(x) = 9
write(x,8) No t8 killed
write(x,11) OK N=2, WTM(x2 ) = 11
read(x,10) OK on 1 RTM(x) = 10
read(x,12) OK on 2 RTM(x) = 12
write(x,13) OK N=3, WTM(x3 ) = 13
74 / 76
Concurrency Control
75 / 76
Concurrency Control
The End
76 / 76