Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia...
Transcript of Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia...
![Page 2: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/2.jpg)
The Problem
T1 T2 … Tn
DB(consistencyconstraints)
Different transactions may need to access data items at the same time, violating constraints
CS 245 2
![Page 3: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/3.jpg)
The Problem
Even if each transaction maintains constraints by itself, interleaving their actions does not
Could try to run just one transaction at a time (serial schedule), but this has problems» Too slow! Especially with external clients & IO
CS 245 3
![Page 4: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/4.jpg)
High-Level Approach
Define isolation levels: sets of guarantees about what transactions may experience
Strongest level: serializability (result is same as some serial schedule)
Many others possible: snapshot isolation, read committed, read uncommitted, …
CS 245 4
![Page 5: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/5.jpg)
OutlineWhat makes a schedule serializable?
Conflict serializability
Precedence graphs
Enforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking
Optimistic concurrency with validationCS 245 5
![Page 6: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/6.jpg)
Example
T1: Read(A) T2: Read(A)A ¬ A+100 A ¬ A´2Write(A) Write(A)Read(B) Read(B)B ¬ B+100 B ¬ B´2Write(B) Write(B)
Constraint: A=B
CS 245 6
![Page 7: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/7.jpg)
Schedule CT1 T2Read(A); A ¬ A+100Write(A);
Read(A); A ¬ A´2;Write(A);
Read(B); B ¬ B+100;Write(B);
Read(B); B ¬ B´2;Write(B);
CS 245 7
![Page 8: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/8.jpg)
Schedule CT1 T2Read(A); A ¬ A+100Write(A);
Read(A); A ¬ A´2;Write(A);
Read(B); B ¬ B+100;Write(B);
Read(B); B ¬ B´2;Write(B);
A B25 25
125
250
125
250250 250
CS 245 8
![Page 9: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/9.jpg)
Schedule DT1 T2Read(A); A ¬ A+100Write(A);
Read(A); A ¬ A´2;Write(A);Read(B); B ¬ B´2;Write(B);
Read(B); B ¬ B+100;Write(B);
CS 245 9
![Page 10: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/10.jpg)
Schedule DT1 T2Read(A); A ¬ A+100Write(A);
Read(A); A ¬ A´2;Write(A);Read(B); B ¬ B´2;Write(B);
Read(B); B ¬ B+100;Write(B);
A B25 25
125
250
50
150250 150
CS 245 10
![Page 11: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/11.jpg)
Want schedules that are “good”, regardless of» initial state and» transaction semantics
Only look at order of read & write operations
Example:
SC = r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B)
Our Goal
We don’t know the logicin external client apps!
CS 245 11
![Page 12: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/12.jpg)
SC’ = r1(A)w1(A)r1(B)w1(B)r2(A)w2(A)r2(B)w2(B)
T1 T2
Example:
SC = r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B)
CS 245 12
![Page 13: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/13.jpg)
However, for SD:
SD = r1(A)w1(A)r2(A)w2(A) r2(B)w2(B)r1(B)w1(B)
Another way to view this:» r1(B) after w2(B) means T1 should be after T2 in an
equivalent serial schedule (T2 ® T1)» r2(A) after w1(A) means T2 should be after T1 in an
equivalent serial schedule (T1 ® T2)» Can’t have both of these!
CS 245 13
![Page 14: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/14.jpg)
OutlineWhat makes a schedule serializable?
Conflict serializability
Precedence graphs
Enforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking
Optimistic concurrency with validationCS 245 14
![Page 15: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/15.jpg)
Transaction: sequence of ri(x), wi(x) actions
Conflicting actions: r1(A) w1(A) w1(A)
w2(A) r2(A) w2(A)
Schedule: a chronological order in which all the transactions’ actions are executed
Serial schedule: no interleaving of actions from different transactionsCS 245 15
Concepts
![Page 16: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/16.jpg)
Question
Is it OK to model reads & writes as occurring at a single point in time in a schedule?
S = … r1(x) … w2(b) …
CS 245 16
![Page 17: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/17.jpg)
Question
What about conflicting, concurrent actions on same object?
start r1(A) end r1(A)
start w2(A) end w2(A)
CS 245 17
time
Assume “atomic actions” that only occur at one point in time (e.g. implement using locking)
![Page 18: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/18.jpg)
Definition
S1, S2 are conflict equivalent schedules if S1 can be transformed into S2 by a series of swaps of non-conflicting actions
(i.e., can reorder non-conflicting operations in S1 to obtain S2)
CS 245 18
![Page 19: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/19.jpg)
Definition
A schedule is conflict serializable if it is conflict equivalent to some serial schedule
CS 245 19
Key idea:» Conflicts “change” result of reads and writes» Conflict serializable means there exists
some equivalent serial execution that does not change the effects
How can we compute whether a schedule is conflict serializable?
![Page 20: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/20.jpg)
OutlineWhat makes a schedule serializable?
Conflict serializability
Precedence graphs
Enforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking
Optimistic concurrency with validationCS 245 20
![Page 21: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/21.jpg)
Precedence Graph P(S)
Nodes: transactions in a schedule S
Edges: Ti ® Tj whenever» pi(A), qj(A) are actions in S» pi(A) <S qj(A) (occurs earlier in schedule)» at least one of pi, qj is a write (i.e. conflict)
CS 245 21
![Page 22: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/22.jpg)
Exercise
What is P(S) for
S = w3(A) w2(C) r1(A) w1(B) r1(C) w2(A) r4(A) w4(D)
Is S serializable?
CS 245 22
![Page 23: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/23.jpg)
Another Exercise
What is P(S) for
S = w1(A) r2(A) r3(A) w4(A)
CS 245 23
![Page 24: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/24.jpg)
Lemma
S1, S2 conflict equivalent Þ P(S1)=P(S2)
CS 245 24
![Page 25: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/25.jpg)
S1, S2 conflict equivalent Þ P(S1)=P(S2)
Proof:Assume P(S1) ¹ P(S2)Þ $ Ti: Ti ® Tj in S1 and not in S2
Þ S1 = …pi(A)... qj(A)… pi, qj
S2 = …qj(A)… pi(A)... conflict
Þ S1, S2 not conflict equivalent CS 245 25
Lemma
![Page 26: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/26.jpg)
Note: P(S1)=P(S2) Þ S1, S2 conflict equivalent
CS 245 26
![Page 27: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/27.jpg)
Note: P(S1)=P(S2) Þ S1, S2 conflict equivalent
Counter example:
S1 = w1(A) r2(A) w2(B) r1(B)
S2 = r2(A) w1(A) r1(B) w2(B)
CS 245 27
![Page 28: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/28.jpg)
P(S1) acyclic ÜÞ S1 conflict serializable
(Ü) Assume S1 is conflict serializableÞ $ Ss (serial): Ss, S1 conflict equivalentÞ P(Ss) = P(S1) (by previous lemma)Þ P(S1) acyclic since P(Ss) is acyclic
CS 245 28
Theorem
![Page 29: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/29.jpg)
T1
T2 T3
T4
CS 245 29
P(S1) acyclic ÜÞ S1 conflict serializable
Theorem
![Page 30: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/30.jpg)
(Þ) Assume P(S1) is acyclicTransform S1 as follows:(1) Take T1 to be transaction with no inbound edges(2) Move all T1 actions to the front
S1 = ……. qj(A)…….p1(A)…..
(3) we now have S1 = <T1 actions><... rest ...>(4) repeat above steps to serialize rest!CS 245 30
P(S1) acyclic ÜÞ S1 conflict serializable
TheoremT1
T2 T3
T4
![Page 31: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/31.jpg)
OutlineWhat makes a schedule serializable?
Conflict serializability
Precedence graphs
Enforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking
Optimistic concurrency with validationCS 245 31
![Page 32: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/32.jpg)
How to Enforce Serializable Schedules?Option 1: run system, recording P(S); at end of day, check for cycles in P(S) and declare whether execution was good
CS 245 32
![Page 33: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/33.jpg)
How to Enforce Serializable Schedules?Option 2: prevent P(S) cycles from occurring
T1 T2 ….. Tn
CS 245 33
Scheduler
DB
![Page 34: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/34.jpg)
A Locking Protocol
Two new actions:
lock: li (A)
unlock: ui (A)
CS 245 34
scheduler
T1 T2
locktable
Transaction i locks object A
![Page 35: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/35.jpg)
Rule #1: Well-Formed Transactions
Ti: … li(A) … ri(A) … ui(A) ...
CS 245 35
Transactions can only operate on locked items
![Page 36: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/36.jpg)
Rule #2: Legal Scheduler
S = …….. li(A) ………... ui(A) ……...
CS 245 36
no lj(A)
Only one transaction can lock item at a time
![Page 37: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/37.jpg)
ExerciseWhich schedules are legal?Which transactions are well-formed?
S1 = l1(A) l1(B) r1(A) w1(B) l2(B) u1(A) u1(B)r2(B) w2(B) u2(B) l3(B) r3(B) u3(B)
S2 = l1(A) r1(A) w1(B) u1(A) u1(B) l2(B) r2(B)w2(B) l3(B) r3(B) u3(B)
S3 = l1(A) r1(A) u1(A) l1(B) w1(B) u1(B) l2(B)r2(B) w2(B) u2(B) l3(B) r3(B) u3(B)
CS 245 37
![Page 38: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/38.jpg)
ExerciseWhich schedules are legal?Which transactions are well-formed?
S1 = l1(A) l1(B) r1(A) w1(B) l2(B) u1(A) u1(B)r2(B) w2(B) u2(B) l3(B) r3(B) u3(B)
S2 = l1(A) r1(A) w1(B) u1(A) u1(B) l2(B) r2(B)w2(B) l3(B) r3(B) u3(B)
S3 = l1(A) r1(A) u1(A) l1(B) w1(B) u1(B)l2(B) r2(B) w2(B) u2(B) l3(B) r3(B) u3(B)
CS 245 38
u2(B) missing
![Page 39: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/39.jpg)
T1 T2l1(A);Read(A)A←A+100;Write(A);u1(A)
l2(A);Read(A)A←Ax2;Write(A);u2(A)l2(B);Read(B)B←Bx2;Write(B);u2(B)
l1(B);Read(B)B←B+100;Write(B);u1(B)
Schedule F
CS 245 39
A B25 25
125
250
50
150250 150
![Page 40: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/40.jpg)
Rule #3: 2-Phase Locking (2PL)
Ti = ……. li(A) ………... ui(A) ……...
CS 245 40
no unlocks no locks
Transactions first lock all items they need, then unlock them
![Page 41: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/41.jpg)
# locksheld byTi
Time
Growing ShrinkingPhase Phase
CS 245 41
2-Phase Locking (2PL)
![Page 42: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/42.jpg)
T1 T2l1(A);Read(A)A←A+100;Write(A)l1(B);u1(A)
CS 245 42
Schedule G
![Page 43: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/43.jpg)
T1 T2l1(A);Read(A)A←A+100;Write(A)l1(B);u1(A)
l2(A);Read(A)A←A⨯2;Write(A)l2(B) delayed
CS 245 43
Schedule G
![Page 44: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/44.jpg)
T1 T2l1(A);Read(A)A←A+100;Write(A)l1(B);u1(A)
l2(A);Read(A)A←A⨯2;Write(A)l2(B)
Read(B);B←B+100Write(B);u1(B)
delayed
CS 245 44
Schedule G
![Page 45: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/45.jpg)
T1 T2l1(A);Read(A)A←A+100;Write(A)l1(B);u1(A)
l2(A);Read(A)A←A⨯2;Write(A)l2(B)
Read(B);B←B+100Write(B);u1(B)
l2(B);u2(A);Read(B)B←B⨯2;Write(B);u2(B)
delayed
CS 245 45
Schedule G
![Page 46: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/46.jpg)
T1 T2l1(A); Read(A) l2(B); Read(B)A←A+100; Write(A) B←B⨯2; Write(B)l1(B) l2(A)
CS 245 46
Schedule H (T2 Ops Reversed)
delayed(T1 holds A)
delayed(T2 holds B)
Problem: Deadlock between transactions
![Page 47: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/47.jpg)
Dealing with Deadlock
Option 1: Detect deadlocks and roll back one of the deadlocked transactions» The rolled back transaction no longer appears
in our schedule
Option 2: Agree on an order to lock items in that prevents deadlocks» E.g. transactions acquire locks in key order» Must know which items Ti will need up front!
CS 245 47
![Page 48: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/48.jpg)
Is 2PL Correct?
Yes! We can prove that following rules #1,2,3 gives conflict-serializable schedules
CS 245 48
![Page 49: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/49.jpg)
Conflict Rules for Lock Ops
li(A), lj(A) conflict
li(A), uj(A) conflict
Note: no conflict <ui(A), uj(A)>, <li(A), rj(A)>,...
CS 245 49
![Page 50: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/50.jpg)
Theorem
Rules #1,2,3 Þ conflict-serializable schedule(2PL)
CS 245 50
To help in proof:Definition: Shrink(Ti) = SH(Ti) =
first unlock action of Ti
![Page 51: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/51.jpg)
LemmaTi ® Tj in S Þ SH(Ti) <S SH(Tj)
CS 245 51
Proof:Ti ® Tj means that
S = … pi(A) … qj(A) …; p,q conflictBy rules 1, 2:
S = … pi(A) … ui(A) … lj(A) ... qj(A) …
By rule 3: SH(Ti) SH(Tj)So, SH(Ti) <S SH(Tj)
![Page 52: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/52.jpg)
Theorem: Rules #1,2,3 ÞConflict Serializable ScheduleProof:
(1) Assume P(S) has cycle
T1 ® T2 ®…. Tn ® T1
(2) By lemma: SH(T1) < SH(T2) < ... < SH(T1)
(3) Impossible, so P(S) acyclic
(4) Þ S is conflict serializableCS 245 52
![Page 53: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/53.jpg)
2PL Subset of Serializable
CS 245 53
2PLSerializable
![Page 54: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/54.jpg)
S1: w1(X) w3(X) w2(Y) w1(Y)
CS 245 54
2PLSerializable
S1
S1 cannot be achieved via 2PL:The lock by T1 for Y must occur after w2(Y), so the unlock by T1 for X must occur after this point (and before w1(X)). Thus, w3(X) cannot occur under 2PL where shown in S1.
But S1 is serializable: equivalent to T2, T1, T3.
![Page 55: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/55.jpg)
SC: w1(A) w2(A) w1(B) w2(B)
Are our schedules SC and SD 2PL schedules?
SD: w1(A) w2(A) w2(B) w1(B)
CS 245 55
If You Need More Practice
![Page 56: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/56.jpg)
Optimizing Performance
Beyond this simple 2PL protocol, it is all a matter of improving performance and allowing more concurrency….» Shared locks» Multiple granularity» Inserts, deletes and phantoms» Other types of C.C. mechanisms
CS 245 57
![Page 57: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/57.jpg)
So far:
S = ...l1(A) r1(A) u1(A) … l2(A) r2(A) u2(A) …
Do not conflict
CS 245 58
Shared Locks
![Page 58: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/58.jpg)
So far:
S = ...l1(A) r1(A) u1(A) … l2(A) r2(A) u2(A) …
Do not conflict
Instead:S=... ls1(A) r1(A) ls2(A) r2(A) …. us1(A) us2(A)
CS 245 59
Shared Locks
![Page 59: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/59.jpg)
Multiple Lock Modes
Lock actionsl-mi(A): lock A in mode m (m is S or X)u-mi(A): unlock mode m (m is S or X)
Shorthand:ui(A): unlock whatever modes Ti has locked A
CS 245 60
![Page 60: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/60.jpg)
Ti =... l-S1(A) … r1(A) … u1 (A) …
Ti =... l-X1(A) … w1(A) … u1 (A) …
CS 245 61
Rule 1: Well-Formed Transactions
Transactions must acquire the right lock type for their actions (S for read only, X for r/w).
![Page 61: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/61.jpg)
Rule 1: Well-Formed TransactionsWhat about transactions that read and write same object?
Option 1: Request exclusive lock
T1 = ...l-X1(A) … r1(A) ... w1(A) ... u(A) …
CS 245 62
![Page 62: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/62.jpg)
Rule 1: Well-Formed TransactionsWhat about transactions that read and write same object?
Option 2: Upgrade lock to X on write
T1 = ...l-S1(A)…r1(A)...l-X1(A)…w1(A)...u1(A)…
CS 245 63
(Think of this as getting a 2nd lock, or dropping S to get X.)
![Page 63: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/63.jpg)
Rule 2: Legal Scheduler
S = ... l-Si(A) … … ui(A) …
no l-Xj(A)
S = ... l-Xi(A) … … ui(A) …
no l-Xj(A)no l-Sj(A)
CS 245 64
![Page 64: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/64.jpg)
A Way to Summarize Rule #2
Lock mode compatibility matrix
compat = S XS true falseX false false
CS 245 65
![Page 65: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/65.jpg)
Rule 3: 2PL Transactions
No change except for upgrades:
(I) If upgrade gets more locks
(e.g., S ® {S, X}) then no change!
(II) If upgrade releases read lock (e.g., S®X)
can be allowed in growing phase
CS 245 66
![Page 66: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/66.jpg)
Proof: similar to X locks case
Detail:
l-mi(A), l-nj(A) do not conflict if compat(m,n)
l-mi(A), u-nj(A) do not conflict if compat(m,n)
CS 245 67
Rules 1,2,3 Þ Conf. Serializable Schedules for S/X Locks
![Page 67: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/67.jpg)
Lock Modes Beyond S/X
Examples:
(1) increment lock
(2) update lock
CS 245 68
![Page 68: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/68.jpg)
Example 1: Increment Lock
Atomic addition action: INi(A)
{Read(A); A ¬ A+k; Write(A)}
INi(A), INj(A) do not conflict, because addition is commutative!
CS 245 69
![Page 69: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/69.jpg)
Compatibility Matrix
compat S X I
S
X
I
CS 245 70
![Page 70: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/70.jpg)
Compatibility Matrix
compat S X I
S T F F
X F F F
I F F T
CS 245 71
![Page 71: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/71.jpg)
A common deadlock problem with upgrades:
T1 T2l-S1(A)
l-S2(A)l-X1(A)
l-X2(A)--- Deadlock ---
CS 245 72
Update Locks
![Page 72: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/72.jpg)
Solution
If Ti wants to read A and knows it may later want to write A, it requests an update lock(not shared lock)
CS 245 73
![Page 73: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/73.jpg)
compat S X US T FX F FU
Lock alreadyheld in
CS 245 74
Compatibility MatrixNew request
![Page 74: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/74.jpg)
compat S X US T F TX F F FU F F F
Lock alreadyheld in
CS 245 75
Compatibility MatrixNew request
Note: asymmetric table!
![Page 75: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/75.jpg)
How Is Locking Implemented In Practice?Every system is different (e.g., may not even provide conflict serializable schedules)
But here is one (simplified) way ...
CS 245 76
![Page 76: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/76.jpg)
Sample Locking System
1. Don’t ask transactions to request/release locks: just get the weakest lock for each action they perform
2. Hold all locks until transaction commits
CS 245 77
#locks
time
![Page 77: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/77.jpg)
Sample Locking System
Under the hood: lock manager that keeps track of which objects are locked» E.g. hash table
Also need a good way to block transactions until locks are available, and find deadlocks
CS 245 78
![Page 78: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/78.jpg)
Which Objects Do We Lock?
?
CS 245 79
Table A
Table B
...
Tuple ATuple BTuple C
...
Disk block
A
Disk block
B
...
DB DB DB
![Page 79: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/79.jpg)
Which Objects Do We Lock?
Locking works in any case, but should we choose small or large objects?
CS 245 80
![Page 80: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/80.jpg)
Which Objects Do We Lock?
Locking works in any case, but should we choose small or large objects?
CS 245 81
If we lock large objects (e.g., relations)– Need few locks– Low concurrency
If we lock small objects (e.g., tuples, fields)– Need more locks– More concurrency
![Page 81: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/81.jpg)
We Can Have It Both Ways!
Ask any janitor to give you the solution...
CS 245 82
hall
Stall 1 Stall 2 Stall 3 Stall 4
restroom
![Page 82: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/82.jpg)
Example
CS 245 83
R1
t1t2 t3 t4
![Page 83: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/83.jpg)
Example
CS 245 84
R1
t1t2 t3 t4
T1(IS)
T1(S)
![Page 84: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/84.jpg)
Example
CS 245 85
R1
t1t2 t3 t4
T1(IS)
T1(S)
, T2(S)
![Page 85: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/85.jpg)
Example 2
CS 245 86
R1
t1t2 t3 t4
T1(IS)
T1(S)
![Page 86: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/86.jpg)
Example 2
CS 245 87
R1
t1t2 t3 t4
T1(IS)
T1(S)
, T2(IX)
T2(IX)
![Page 87: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/87.jpg)
compat RequestorIS IX S SIX X
ISHolder IX
SSIX
X
T T T T FFFFFFFFF
FFFTFTFTFFTT
CS 245 88
Multiple Granularity Locks
![Page 88: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/88.jpg)
compat RequestorIS IX S SIX X
ISHolder IX
SSIX
X
T T T T FFFFFFFFF
FFFTFTFTFFTT
CS 245 89
Multiple Granularity Locks
![Page 89: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/89.jpg)
Parent Child can be lockedlocked in by same transaction in
ISIXSSIXX
P
C
IS, SIS, S, IX, X, SIXnoneX, IX, SIXnone
CS 245 90
Rules Within A Transaction
![Page 90: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/90.jpg)
Rules(1) Follow multiple granularity comp function(2) Lock root of tree first, any mode(3) Node Q can be locked by Ti in S or IS only if
parent(Q) locked by Ti in IX or IS(4) Node Q can be locked by Ti in X,SIX,IX only
if parent(Q) locked by Ti in IX,SIX(5) Ti is two-phase(6) Ti can unlock node Q only if none of Q’s
children are locked by Ti
CS 245 91
![Page 91: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/91.jpg)
Exercise:Can T2 access object f2.2 in X mode? What locks will T2 get?
CS 245 92
R1
t1t2 t3 t4T1(IX)
f2.1 f2.2 f3.1 f3.2
T1(IX)
T1(X)
![Page 92: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/92.jpg)
Exercise:Can T2 access object f2.2 in X mode? What locks will T2 get?
CS 245 93
R1
t1t2 t3 t4T1(X)
f2.1 f2.2 f3.1 f3.2
T1(IX)
![Page 93: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/93.jpg)
Exercise:Can T2 access object f3.1 in X mode? What locks will T2 get?
CS 245 94
R1
t1t2 t3 t4T1(S)
f2.1 f2.2 f3.1 f3.2
T1(IS)
![Page 94: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/94.jpg)
Exercise:Can T2 access object f2.2 in S mode? What locks will T2 get?
CS 245 95
R1
t1t2 t3 t4T1(IX)
f2.1 f2.2 f3.1 f3.2
T1(SIX)
T1(X)
![Page 95: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/95.jpg)
Exercise:Can T2 access object f2.2 in X mode? What locks will T2 get?
CS 245 96
R1
t1t2 t3 t4T1(IX)
f2.1 f2.2 f3.1 f3.2
T1(SIX)
T1(X)
![Page 96: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/96.jpg)
Insert + delete operations
Insert
CS 245 97
A
Za
...
![Page 97: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/97.jpg)
Changes to Locking Rules:
1. Get exclusive lock on A before deleting A
2. At insert A operation by Ti, Ti is given exclusive lock on A
CS 245 98
![Page 98: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/98.jpg)
Still Have Problem: Phantoms
Example: relation R (id, name,…)constraint: id is unique keyuse tuple locking
R id Name ….o1 55 Smitho2 75 Jones
CS 245 99
![Page 99: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/99.jpg)
T1: Insert <12,Mary,…> into RT2: Insert <12,Sam,…> into R
T1 T2S1(o1) S2(o1)S1(o2) S2(o2)Check Constraint Check Constraint
Insert o3[12,Mary,..]Insert o4[12,Sam,..]
... ...
CS 245 100
![Page 100: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/100.jpg)
Solution
Use multiple granularity tree
Before insert of node N,lock parent(N) in X mode
CS 245 101
R1
t1t2 t3
![Page 101: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/101.jpg)
Back to exampleT1: Insert<12,Mary> T2: Insert<12,Sam>
T1 T2
X1(R)
Check constraintInsert<12,Mary>U1(R)
X2(R)Check constraintOops! e# = 12 already in R!
X2(R) delayed
CS 245 102
![Page 102: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/102.jpg)
Instead of Using R, Can Use Index Nodes for RangesExample:
CS 245 103
R
Index0<E#<100
Index100<E#<200
E#=2 E#=5 E#=107 E#=109...
...
...
![Page 103: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/103.jpg)
OutlineWhat makes a schedule serializable?
Conflict serializability
Precedence graphs
Enforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking
Optimistic concurrency with validationCS 245 104
![Page 104: Concurrency Control - Stanford University · Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu. The Problem T1 T2 … Tn DB (consistency constraints) Different transactions](https://reader034.fdocuments.net/reader034/viewer/2022042611/5f3bd1f260723072d52a5276/html5/thumbnails/104.jpg)
Next Class
Guest talk by Reynold Xin from Databricks:
Delta Lake: Making Cloud Data Lakes Transactional and Scalable
105
The same concurrency issues we saw happen in large data lakes with billions of files… how to offer transactions there?