Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation...

59
Concurrency Control Instructor: Matei Zaharia cs245.stanford.edu

Transcript of Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation...

Page 1: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Concurrency Control

Instructor: Matei Zahariacs245.stanford.edu

Page 2: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

OutlineWhat makes a schedule serializable?Conflict serializability

Precedence graphsEnforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking

Optimistic concurrency with validation

Concurrency control + recovery

CS 245 2

Page 3: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Lock Modes Beyond S/X

Examples:

(1) increment lock

(2) update lock

CS 245 3

Page 4: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

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 4

Page 5: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Compatibility Matrix

compat S X I

S T F F

X F F F

I F F T

CS 245 5

Page 6: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

A common deadlock problem with upgrades:

T1 T2l-S1(A)

l-S2(A)l-X1(A)

l-X2(A)--- Deadlock ---

CS 245 6

Update Locks

Page 7: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

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 7

Page 8: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

compat S X US T FX F FU

Lock alreadyheld in

CS 245 8

Compatibility MatrixNew request

Page 9: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

compat S X US T F TX F F FU F F F

Lock alreadyheld in

CS 245 9

Compatibility MatrixNew request

Note: asymmetric table!

Page 10: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

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 10

Page 11: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

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 11

#locks

time

Page 12: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

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 12

Page 13: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Which Objects Do We Lock?

?

CS 245 13

Table A

Table B

...

Tuple ATuple BTuple C

...

Disk block

A

Disk block

B

...

DB DB DB

Page 14: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Which Objects Do We Lock?

Locking works in any case, but should we choose small or large objects?

CS 245 14

Page 15: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Which Objects Do We Lock?

Locking works in any case, but should we choose small or large objects?

CS 245 15

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 16: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

We Can Have It Both Ways!

Ask any janitor to give you the solution...

CS 245 16

hall

Stall 1 Stall 2 Stall 3 Stall 4

restroom

Page 17: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Example

CS 245 17

R1

t1t2 t3 t4

Page 18: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Example

CS 245 18

R1

t1t2 t3 t4

T1(IS)

T1(S)

Page 19: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Example

CS 245 19

R1

t1t2 t3 t4

T1(IS)

T1(S)

, T2(S)

Page 20: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Example 2

CS 245 20

R1

t1t2 t3 t4

T1(IS)

T1(S)

Page 21: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Example 2

CS 245 21

R1

t1t2 t3 t4

T1(IS)

T1(S)

, T2(IX)

T2(X)

Page 22: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Example 3

CS 245 22

R1

t1t2 t3 t4

T1(IS)

T1(S)

, T2(S), T3(IX)?

Page 23: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

compat RequestorIS IX S SIX X

ISHolder IX

SSIX

X

T T T T FFFFFFFFF

FFFTFTFTFFTT

CS 245 23

Multiple Granularity Locks

Page 24: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

compat RequestorIS IX S SIX X

ISHolder IX

SSIX

X

T T T T FFFFFFFFF

FFFTFTFTFFTT

CS 245 24

Multiple Granularity Locks

Page 25: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Parent Child can be lockedlocked in by same transaction in

ISIXSSIXX

P

C

IS, SIS, S, IX, X, SIXnoneX, IX, SIXnone

CS 245 25

Rules Within A Transaction

Page 26: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Multi-Granularity 2PL Rules1. Follow multi-granularity compat function2. Lock root of tree first, any mode3. Node Q can be locked by Ti in S or IS only if

parent(Q) locked by Ti in IX or IS4. Node Q can be locked by Ti in X, SIX, IX only if

parent(Q) locked by Ti in IX, SIX5. Ti is two-phase6. Ti can unlock node Q only if none of Q’s

children are locked by Ti

CS 245 26

Page 27: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Exercise:Can T2 access object f2.2 in X mode? What locks will T2 get?

CS 245 27

R1

t1t2 t3 t4T1(IX)

f2.1 f2.2 f3.1 f3.2

T1(IX)

T1(X)

Page 28: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Exercise:Can T2 access object f2.2 in X mode? What locks will T2 get?

CS 245 28

R1

t1t2 t3 t4T1(X)

f2.1 f2.2 f3.1 f3.2

T1(IX)

Page 29: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Exercise:Can T2 access object f3.1 in X mode? What locks will T2 get?

CS 245 29

R1

t1t2 t3 t4T1(S)

f2.1 f2.2 f3.1 f3.2

T1(IS)

Page 30: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Exercise:Can T2 access object f2.2 in S mode? What locks will T2 get?

CS 245 30

R1

t1t2 t3 t4T1(IX)

f2.1 f2.2 f3.1 f3.2

T1(SIX)

T1(X)

Page 31: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Exercise:Can T2 access object f2.2 in X mode? What locks will T2 get?

CS 245 31

R1

t1t2 t3 t4T1(IX)

f2.1 f2.2 f3.1 f3.2

T1(SIX)

T1(X)

Page 32: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Insert + Delete Operations

Insert

CS 245 32

A

Za

...

Page 33: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

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 33

Page 34: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

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 34

Page 35: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

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 35

Page 36: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Solution

Use multiple granularity tree

Before insert of node N,lock parent(N) in X mode

CS 245 36

R1

t1 t2 t3

Page 37: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

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 37

Page 38: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Instead of Using R, Can Use Index Nodes for Ranges

Example:

CS 245 38

...

...

...

R

Index100<id≤200

Index0<id≤100

id=2 id=5 id=107 id=109

Page 39: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

OutlineWhat makes a schedule serializable?Conflict serializability

Precedence graphsEnforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking

Optimistic concurrency with validation

Concurrency control + recovery

CS 245 39

Page 40: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Validation ApproachTransactions have 3 phases:

1. Read» Read all DB values needed» Write to temporary storage» No locking

2. Validate» Check whether schedule so far is serializable

3. Write» If validate OK, write to DB

CS 245 40

Page 41: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Key Idea

Make validation atomic

If T1, T2, T3, … is the validation order, then resulting schedule will be conflict equivalent to Ss = T1, T2, T3, …

CS 245 41

Page 42: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Implementing Validation

System keeps track of two sets:

FIN = transactions that have finished phase 3(write phase) and are all done

VAL = transactions that have successfullyfinished phase 2 (validation)

CS 245 42

Page 43: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Example That Validation Must Prevent:

RS(T2)={B} RS(T3)={A,B}

WS(T2)={B,D} WS(T3)={C}

CS 245 43

time

T2start

T2validated

T3validated

T3start

Ç≠ ∅

Page 44: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

T2finish

phase 3

Example That Validation Must Prevent:

RS(T2)={B} RS(T3)={A,B}

WS(T2)={B,D} WS(T3)={C}

CS 245 44

time

T2start

T2validated

T3validated

T3start

Allow

T3start

Ç≠ ∅

Page 45: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Another Thing Validation Must Prevent:

RS(T2)={A} RS(T3)={A,B}

WS(T2)={D,E} WS(T3)={C,D}

time

T2validated

T3validated

finishT2

CS 245 45

Page 46: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

RS(T2)={A} RS(T3)={A,B}

WS(T2)={D,E} WS(T3)={C,D}

time

T2validated

T3validated

finishT2

BAD: w3(D) w2(D)

CS 245 46

Another Thing Validation Must Prevent:

Page 47: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

finishT2

RS(T2)={A} RS(T3)={A,B}

WS(T2)={D,E} WS(T3)={C,D}

time

T2validated

T3validated

finishT2

CS 245 47

Another Thing Validation Must Prevent:Allow

Page 48: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Validation Rules for Tj:

when Tj starts phase 1: ignore(Tj) ¬ FIN

at Tj Validation:if Check(Tj) then

VAL ¬ VAL ∪ {Tj}do write phaseFIN ¬ FIN ∪ {Tj}

CS 245 48

Page 49: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Check(Tj)

for Ti Î VAL – ignore(Tj) doif (WS(Ti) ∩ RS(Tj) ≠ ∅ or

(Ti Ï FIN and WS(Ti) ∩ WS(Tj) ≠ ∅))then return false

return true

CS 245 49

Page 50: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Exercise

T: RS(T)={A,B}WS(T)={A,C}

V: RS(V)={B}WS(V)={D,E}

U: RS(U)={B}WS(U)={D}

W: RS(W)={A,D}WS(W)={A,C}

startvalidatefinish

CS 245 50

Page 51: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Is Validation = 2PL?

CS 245 51

2PLVal

2PLVal

2PLVal

Val2PL

Page 52: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

S: w2(y) w1(x) w2(x)

Achievable with 2PL?

Achievable with validation?

CS 245 52

Page 53: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

S: w2(y) w1(x) w2(x)

S can be achieved with 2PL:l2(y) w2 (y) l1(x) w1(x) u1(x) l2(x) w2(x) u2(x) u2(y)

S cannot be achieved by validation:The validation point of T2, val2, must occur before w2(y) since transactions do not write to the database until after validation. Because of the conflict on x, val1 < val2, so we must have something like:

S: val1 val2 w2(y) w1(x) w2(x)

With the validation protocol, the writes of T2 should not start until T1 is all done with writes, which is not the case.

CS 245 53

Page 54: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Validation Subset of 2PL?Possible proof (Check!):» Let S be validation schedule» For each T in S insert lock/unlocks, get S’:

• At T start: request read locks for all of RS(T)• At T validation: request write locks for WS(T);

release read locks for read-only objects• At T end: release all write locks

» Clearly transactions well-formed and 2PL» Must show S’ is legal (next slide)

CS 245 54

Page 55: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Say S’ not legal (due to w-r conflict):S’: ... l1(x) w2(x) r1(x) val1 u1(x) ...» At val1: T2 not in Ignore(T1); T2 in VAL» T1 does not validate: WS(T2) Ç RS(T1) ¹ Æ» contradiction!

Say S’ not legal (due to w-w conflict):S’: ... val1 l1(x) w2(x) w1(x) u1(x) ...» Say T2 validates first (proof similar if T1 validates first)» At val1: T2 not in Ignore(T1); T2 in VAL» T1 does not validate:

T2 Ï FIN AND WS(T1) Ç WS(T2) ¹ Æ)» contradiction!

CS 245 55

Validation Subset of 2PL?

Page 56: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Is Validation = 2PL?

CS 245 56

2PLVal

2PLVal

2PLVal

Val2PL

Page 57: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

When to Use Validation?

Validation performs better than locking when:» Conflicts are rare» System resources are plentiful» Have tight latency constraints

CS 245 57

Page 58: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

Summary

Have studied several concurrency control mechanisms used in practice» 2 PL» Multiple granularity» Validation

Next: how does concurrency control interact with failure recovery?

CS 245 58

Page 59: Concurrency Control - Stanford University · Concurrency control + recovery CS 245 39. Validation Approach Transactions have 3 phases: 1. Read »Read all DB values needed »Write

OutlineWhat makes a schedule serializable?Conflict serializability

Precedence graphsEnforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking

Optimistic concurrency with validation

Concurrency control + recovery

CS 245 59