concurrency controlweb.stanford.edu/class/cs245/slides/13-concurrency-p2.pdfconcurrency control +...

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

Upload: others

Post on 21-May-2021

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Concurrency Control

Instructor: Matei Zahariacs245.stanford.edu

Page 2: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

OutlineWhat makes a schedule serializable?Conflict serializabilityPrecedence graphsEnforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking

Optimistic concurrency with validationConcurrency control + recoveryBeyond serializabilityCS 245 2

Page 3: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

# locksheld byTi

Time

Growing ShrinkingPhase Phase

CS 245 3

Recap: 2-Phase Locking (2PL)

Page 4: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

How Is 2PL Implemented In Practice?Every system is different, but we’ll show one simplified way

CS 245 4

Page 5: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Sample Locking System

1. Don’t ask transactions to request/release locks: just get a lock for each action they do

2. Hold all locks until a transaction commits

CS 245 5

#locks

time

Page 6: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Sample Locking System

Under the hood: lock manager that keeps track of which objects are locked» E.g., hash table

Also need ways to block transactions until locks are available, and to find deadlocks

CS 245 6

Page 7: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Optimizing Performance

Beyond the base 2PL protocol, many ways to improve performance & concurrency:» Shared locks» Multiple granularity» Inserts, deletes and phantoms» Other types of C.C. mechanisms

CS 245 7

Page 8: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

So far:

S = ...l1(A) r1(A) u1(A) … l2(A) r2(A) u2(A) …

Do not conflict

CS 245 8

Shared Locks

Page 9: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

So far:

S = ...l1(A) r1(A) u1(A) … l2(A) r2(A) u2(A) …

Do not conflict

Instead:S=... l-S1(A) r1(A) l-S2(A) r2(A) …. u1(A) u2(A)

CS 245 9

Shared Locks

Page 10: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 10

Page 11: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Ti =... l-S1(A) … r1(A) … u1(A) …

Ti =... l-X1(A) … w1(A) … u1(A) …

CS 245 11

Rule 1: Well-Formed Transactions

Transactions must acquire the right lock type for their actions (S for read only, X for r/w).

Page 12: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 12

Page 13: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 13

(Think of this as replacing S lock with X lock.)

Page 14: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 14

Page 15: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

A Way to Summarize Rule #2

Lock mode compatibility matrix

compat = S XS true falseX false false

CS 245 15

Lock alreadyheld in

New request

Page 16: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Rule 3: 2PL Transactions

No change except for upgrades: allow upgrades from S to X only in growing phase

CS 245 16

Page 17: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 17

Rules 1,2,3 Þ Conf. Serializable Schedules for S/X Locks

Page 18: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Lock Modes Beyond S/X

Examples:

(1) increment lock

(2) update lock

(3) hierarchical locks

CS 245 18

Page 19: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Increment Locks

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 19

Page 20: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Compatibility Matrix

compat S X I

S T F F

X F F F

I F F T

CS 245 20

Lock alreadyheld in

New request

Page 21: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

A common deadlock problem with upgrades:

T1 T2l-S1(A)

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

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

CS 245 21

Update Locks

Page 22: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 22

Page 23: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

compat S X US T FX F FU

Lock alreadyheld in

CS 245 23

Compatibility Matrix

New request

Page 24: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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

Lock alreadyheld in

CS 245 24

Compatibility Matrix

New request

Note: asymmetric table!

Page 25: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Which Objects Do We Lock?

?

CS 245 25

Table A

Table B

...

Tuple ATuple BTuple C

...

Disk block

A

Disk block

B

...

DB DB DB

Page 26: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Which Objects Do We Lock?

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

CS 245 26

Page 27: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Which Objects Do We Lock?

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

CS 245 27

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 28: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

We Can Have It Both Ways!

Ask any janitor to give you the solution...

CS 245 28

hall

Stall 1 Stall 2 Stall 3 Stall 4

restroom

Page 29: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Example

CS 245 29

R1

t1t2 t3

t4

Page 30: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Example

CS 245 30

R1

t1t2 t3

t4

T1(IS)

T1(S)

Page 31: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Example

CS 245 31

R1

t1t2 t3

t4

T1(IS)

T1(S)

, T2(S)

Page 32: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Example 2

CS 245 32

R1

t1t2 t3

t4

T1(IS)

T1(S)

Page 33: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Example 2

CS 245 33

R1

t1t2 t3

t4

T1(IS)

T1(S)

, T2(IX)

T2(X)

Page 34: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Example 3

CS 245 34

R1

t1t2 t3

t4

T1(IS)

T1(S)

, T2(S) , T3(IX)?

Page 35: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

compat RequesterIS IX S SIX X

ISHolder IX

SSIX

X

T T T T FFFFFFFFF

FFFTFTFTFFTT

CS 245 35

Multiple Granularity Locks

Page 36: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Parent Child can be lockedlocked in by same transaction in

ISIXSSIXX

P

C

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

CS 245 36

Rules Within A Transaction

Page 37: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 37

Page 38: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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

CS 245 38

R1

t1t2 t3

t4T1(IX)

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

T1(IX)

T1(X)

Page 39: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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

CS 245 39

R1

t1t2 t3

t4T1(X)

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

T1(IX)

Page 40: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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

CS 245 40

R1

t1t2 t3

t4T1(S)

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

T1(IS)

Page 41: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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

CS 245 41

R1

t1t2 t3

t4T1(IX)

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

T1(SIX)

T1(X)

Page 42: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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

CS 245 42

R1

t1t2 t3

t4T1(IX)

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

T1(SIX)

T1(X)

Page 43: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Insert + Delete Operations

Insert

CS 245 43

A

Za

...

Page 44: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Changes to Locking Rules:

1. Need exclusive lock on A to delete A

2. When Ti inserts an object A, Ti receives an exclusive lock on A

CS 245 44

Page 45: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 45

Page 46: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

T1: Insert <12,Mary,…> into RT2: Insert <12,Sam,…> into R

T1 T2l-S1(o1) l-S2(o1)l-S1(o2) l-S2(o2)Check Constraint Check Constraint

Insert o3[12,Mary,..]Insert o4[12,Sam,..]

... ...

CS 245 46

Page 47: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Solution

Use multiple granularity tree

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

CS 245 47

R1

t1 t2 t3

Page 48: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Back to ExampleT1: Insert<12,Mary> T2: Insert<12,Sam>

T1 T2l-X1(R)

Check constraintInsert<12,Mary>U1(R)

l-X2(R)Check constraintOops! id=12 already in R!

l-X2(R) delayed

CS 245 48

Page 49: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Instead of Locking All of R, Can Lock Ranges of Keys

Example:

CS 245 49

...

...

...

R

Index100<id≤200

Index0<id≤100

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

Page 50: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

OutlineWhat makes a schedule serializable?Conflict serializabilityPrecedence graphsEnforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking

Optimistic concurrency with validationConcurrency control + recoveryBeyond serializabilityCS 245 50

Page 51: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Validation OverviewTransactions 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 51

Page 52: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Key Idea

Make validation atomic

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

CS 245 52

Page 53: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Implementing Validation

System keeps track of two sets:

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

VAL = transactions that have successfullyfinished phase 2 (validation)

CS 245 53

Page 54: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Example That Validation Must Prevent:

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

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

CS 245 54

time

T2start

T2validated

T3validated

T3start

Ç≠ ∅

Page 55: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

T2finish

phase 3

Example That Validation Must Allow:

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

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

CS 245 55

time

T2start

T2validated

T3validated

T3start

Ç≠ ∅

Page 56: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 56

Page 57: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 57

Another Thing Validation Must Prevent:

Page 58: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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

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

time

T2validated

T3validated

finishT2

CS 245 58

Another Thing Validation Must Allow:

Page 59: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 59

Page 60: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 60

Page 61: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 61

Page 62: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Is Validation = 2PL?

CS 245 62

2PLVal

2PLVal

2PLVal

Val2PL

Page 63: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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

Achievable with 2PL?

Achievable with validation?

CS 245 63

Page 64: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 64

Page 65: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 65

Page 66: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

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 66

Validation Subset of 2PL?

Page 67: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Is Validation = 2PL?

CS 245 67

2PLVal

2PLVal

2PLVal

Val2PL

Page 68: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

When to Use Validation?

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

CS 245 68

Page 69: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

OutlineWhat makes a schedule serializable?Conflict serializabilityPrecedence graphsEnforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking

Optimistic concurrency with validationConcurrency control + recoveryBeyond serializabilityCS 245 70

Page 70: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Example: Tj Ti

wj(A)ri(A)

Commit Ti

Abort Tj

Concurrency Control & Recovery

……

… ……

CS 245 71

Non-persistent commit (bad!)avoided byrecoverableschedules

Page 71: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Example: Tj Ti

wj(A)ri(A)wi(B)

Abort Tj[Commit Ti]

……

……

CS 245 72

Concurrency Control & Recovery

Cascading rollback (bad!)avoided byavoids-cascading-rollback (ACR)schedules

Page 72: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Core Problem

Schedule is conflict serializable

Tj Ti

But not recoverable

CS 245 73

Page 73: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

To Resolve This

Need to mark the “final” decision for each transaction in our schedules:» Commit decision: system guarantees

transaction will or has completed» Abort decision: system guarantees

transaction will or has been rolled back

CS 245 74

Page 74: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Model This as 2 New Actions:

ci = transaction Ti commits

ai = transaction Ti aborts

CS 245 75

Page 75: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

......

......

Tj Ti

wj(A)ri(A)

ci ¬ can we commit here?

Back to Example

CS 245 76

Page 76: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

DefinitionTi reads from Tj in S (Tj ÞS Ti) if:

1. wj(A) <S ri(A)

2. aj <S r(A) (<S: does not precede)

3. If wj(A) <S wk(A) <S ri(A) then ak <S ri(A)

CS 245 77

Page 77: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Definition

Schedule S is recoverable if

whenever Tj ÞS Ti and j ¹ i and ci Î S

then cj <S ci

CS 245 78

Page 78: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Notes

In all transactions, reads and writes must precede commits or abortsó If ci Î Ti, then ri(A) < ai, wi(A) < ai

ó If ai Î Ti, then ri(A) < ai, wi(A) < ai

Also, just one of ci, ai per transaction

CS 245 79

Page 79: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

How to Achieve Recoverable Schedules?

CS 245 80

Page 80: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

With 2PL, Hold Write Locks Until Commit (“Strict 2PL”)

Tj Ti

Wj(A)

Cj

uj(A)ri(A)

CS 245 81

......

......

......

Page 81: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

With Validation, No Change!

Each transaction’s validation point is its commit point, and only write after

CS 245 82

Page 82: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

DefinitionsS is recoverable if each transaction commits only after all transactions from which it read have committed

S avoids cascading rollback if each transaction may read only those values written by committed transactions

S is strict if each transaction may read and write only items previously written by committed transactions (≡ strict 2PL)CS 245 83

Page 83: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Relationship of Recoverable, ACR & Strict Schedules

Avoids cascading rollback

Recoverable

ACR

Strict

Serial

CS 245 84

Page 84: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

ExamplesRecoverable:

w1(A) w1(B) w2(A) r2(B) c1 c2

Avoids Cascading Rollback:w1(A) w1(B) w2(A) c1 r2(B) c2

Strict:w1(A) w1(B) c1 w2(A) r2(B) c2

CS 245 85

Page 85: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Recoverability & Serializability

Every strict schedule is serializable

Proof: equivalent to serial schedule based on the order of commit points» Only read/write from previously committed

transactions

CS 245 86

Page 86: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Recoverability & Serializability

CS 245 87

Page 87: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

CS 245

OutlineWhat makes a schedule serializable?Conflict serializabilityPrecedence graphsEnforcing serializability via 2-phase locking» Shared and exclusive locks» Lock tables and multi-level locking

Optimistic concurrency with validationConcurrency control + recoveryBeyond serializability

88

Page 88: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Weaker Isolation Levels

Dirty reads: Let transactions read values written by other uncommitted transactions» Equivalent to having long-duration write locks,

but no read locks

Read committed: Can only read values from committed transactions, but they may change» Equivalent to having long-duration write locks

(X) and short-duration read locks (S)

CS 245 89

Page 89: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Weaker Isolation Levels

Repeatable reads: Can only read values from committed transactions, and each value will be the same if read again» Equivalent to having long-duration read &

write locks (X/S) but not table locks for insert

Remaining problem: phantoms!

CS 245 90

Page 90: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Weaker Isolation Levels

Snapshot isolation: Each transaction sees a consistent snapshot of the whole DB (as if we saved all committed values when it began)» Often implemented with multi-version

concurrency control (MVCC)

Still has some anomalies! Example?

CS 245 91

Page 91: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Weaker Isolation Levels

Snapshot isolation: Each transaction sees a consistent snapshot of the whole DB (as if we saved all committed values when it began)» Often implemented with multi-version

concurrency control (MVCC)

Write skew anomaly: txns write different values» Constraint: A+B ≥ 0» T1: read A, B; if A+B ≥ 1, subtract 1 from A» T2: read A, B; if A+B ≥ 1, subtract 1 from B» Problem: what if we started with A=1, B=0?

CS 245 92

Page 92: Concurrency Controlweb.stanford.edu/class/cs245/slides/13-Concurrency-p2.pdfConcurrency control + recovery Beyond serializability CS 245 50. Validation Overview Transactions have 3

Interesting Fact

Oracle calls their snapshot isolation level “serializable”, and doesn’t implement true serializable

Many other systems provide snapshot isolation as an option» MySQL, Postgres, MongoDB, SQL Server

CS 245 93