51
1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Embed Size (px)

Citation preview

Page 1: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

1

Concurrency Control II

More on Two Phase Locking

Time Stamp Ordering

Validation Scheme

Page 2: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 2

Learning Objectives Variations of two phase locking Dealing with Deadlock and Starvation Time Stamp Ordering Technique Validation

Page 3: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 3

Schedules Interleaved (or non-interleaved) actions from several

transactions A schedule is good if it can be transformed into one of

the serial schedules by switching two consecutive non-conflict actions

We’ve learned 2PL to achieve serializability It is a pessimistic scheme

Page 4: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 4

Recall 2PL (two phase locking)

Locks of Ti

time

growing shrinking

Each transaction has to follow, no locking anymore after the first unlocking

Page 5: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 5

Why 2PL serializability? Acyclic precedence graph = serializability Basic idea:

No cycle in precedence graph Because

if there is a arc from Ti to Tj in precedence graph, the first unlocking of Ti precede the first unlocking of Tj (proof details in ccI notes)

If there is circle, you will have Ti < Ti

Page 6: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 6

Who will follow 2PL in practice? Looks like it is DB application developers’ job. But, they can not be trusted and too much work

Checking conformity of every transaction is costly In practice, CC subsystems of DBMS take over the

responsibility

Page 7: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 7

Variations of 2PL Basic 2PL Conservative 2PL Strict 2PL Rigorous 2PL

Page 8: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 8

Basic 2PL 2PL with binary locks Covered in last class

Page 9: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 9

Shared locksSo 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)

Page 10: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 10

Lock actions

l-ti(A): lock A in t mode (t is S or X)

u-ti(A): unlock t mode (t is S or X)

Shorthand:

ui(A): unlock whatever modes

Ti has locked A

Page 11: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 11

Well formed transactionsTi =... l-S1(A) … r1(A) … u1 (A) …

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

Page 12: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 12

What about transactions that read and write same object?

Option 1: Request exclusive lock

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

Page 13: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 13

Option 2: Upgrade (E.g., need to read, but don’t know if will write…)

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

Think of- Get 2nd lock on A, or- Drop S, get X lock

• What about transactions that read and write same object?

Page 14: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 14

Compatibility matrix

Comp S X

S true false

X false false

Page 15: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 15

Schedule

T1 T2

l-s1(A);Read(A)

A A+100;Write(A)

l-x1(B); u1(A)

l-s2(A);Read(A)

A Ax2;Write(A); l-x2(B)l-x2(B)

Read(B);B B+100

Write(B); u1(B)

l-x2(B); u2(A);Read(B)

B Bx2;Write(B);u2(B);

delayed

Page 16: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 16

Conservative 2PL Lock all items it needs then transaction starts execution

If any locks can not be obtained, then do not lock anything Difficult but deadlock free

growing shrinkinglocks

time

first action starts

Page 17: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 17

Strict 2PL T does not release any write locks until it commits or

aborts Good for recoverability Since reads or writes on what T writes Deadlock free?

growing shrinkinglocks

time

T commits or aborts

First write unlock

Page 18: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 18

Rigorous 2PL T does not release any locks until it commits or aborts

Easy to implement Deadlock free?

growing shrinkinglocks

time

T commits or aborts

Page 19: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 19

2PL Does basic 2PL guarantee serializability? Does conservative 2PL guarantee serializability? Does strict 2PL guarantee serializability? Does rigorous 2PL guarantee serializability?

Page 20: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 20

Compare variations of 2PL Deadlock

Only conservative 2PLis deadlock free Q: give a schedule of two transactions following 2PL but

result in deadlock.

Page 21: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 21

Exercises: S1: r1(y)r1(x)w1(x)w2(x)w2(y) S2: r1(y)r3(x)w1(x)w3(x)w2(y)w2(x) S3: r3(y)w1(x)w3(x)r1(z)w2(y)w2(x)

Assuming binary lock right before read or write; and rigorous 2PL (release all locks right after last operation), are S1, S2,and S3 possible?

Page 22: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 22

Deadlocks Detection

Wait-for graph Prevention

Resource ordering Timeout Wait-die Wound-wait

Page 23: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 23

Deadlock Detection

Build Wait-For graph Use lock table structures Build incrementally or periodically When cycle found, rollback victim

T1

T3

T2

T6

T5

T4T7

Page 24: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 24

Resource Ordering

Order all elements A1, A2, …, An

A transaction T can lock Ai after Aj only if i > j

Problem : Ordered lock requests not realistic in most cases

Page 25: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 25

Timeout

If transaction waits more than L sec., roll it back!

Simple scheme Hard to select L

Page 26: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 26

Wait-die Transactions are given a timestamp when they

arrive …. ts(Ti) Ti can only wait for Tj if ts(Ti)< ts(Tj)

...else die

Page 27: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 27

T1

(ts =10)

T2

(ts =20)

T3

(ts =25)

wait

wait

Example:

wait?

Very high level: only older ones have the privilege to wait, younger ones die if they attempt to wait for older ones

Page 28: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 28

Wound-wait Transactions are given a timestamp when they

arrive … ts(Ti) Ti wounds Tj if ts(Ti)< ts(Tj)

else Ti waits

“Wound”: Tj rolls back and gives lock to Ti

Page 29: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 29

T1

(ts =25)

T2

(ts =20)

T3

(ts =10)

wait

wait

Example:

wait

Very high level: younger ones wait; older ones kill (wound) younger ones who hold needed locks

Page 30: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 30

Who die? Looks like it is always the younger ones

either die automatically or killed

What is the reason? Will the younger ones starve?

Suggestions?

Page 31: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 31

Timestamp Ordering Key idea:

Transactions access variables according to an order decided by their time stamps when they enter the system

No cycles are possible in the precedence graph

Page 32: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 32

Timestamp System time when transactions starts An increasing unique number given to each stransaction

Denoted by ts(Ti)

Page 33: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 33

The way it works Two time stamps associated with each variable x

RS(x): the largest time stamp of the transactions read it WS(x): the largest time stamp of the transactions write it

Protocol: ri(x) is allowed if ts(Ti) >= WS(x) wi(x) is allowed if ts(Ti) >=WS(x) and ts(Ti) >=RS(x) Disallowed ri(x) or wi(x) will kill Ti, Ti will restart

Page 34: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 34

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300

T1 T2 T3

R(x);

W(y);

R (y);

W(z);

R(x);

W(z);

R(y);

W(x);

x y z

RS=-1 RS=-1 RS=-1

WS=-1 WS=-1 WS=-1

Page 35: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 35

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300

T1 T2 T3

R(x);

x y z

RS=100 RS=-1 RS=-1

WS=-1 WS=-1 WS=-1

Page 36: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 36

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300

T1 T2 T3

R(x);

W(y);

x y z

RS=100 RS=-1 RS=-1

WS=-1 WS=100 WS=-1

Page 37: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 37

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300

T1 T2 T3

R(x);

W(y);

R (y);

x y z

RS=100 RS=200 RS=-1

WS=-1 WS=100 WS=-1

Page 38: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 38

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300

T1 T2 T3

R(x);

W(y);

R (y);

W(z);

x y z

RS=100 RS=200 RS=-1

WS=-1 WS=100 WS=300

Page 39: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 39

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300

T1 T2 T3

R(x);

W(y);

R (y);

W(z);

R(x);

x y z

RS=200 RS=200 RS=-1

WS=-1 WS=100 WS=300

Page 40: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 40

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300

T1 T2 T3

R(x);

W(y);

R (y);

W(z);

R(x);

W(z);

x y z

RS=200 RS=200 RS=-1

WS=-1 WS=100 WS=300

T1 is rolled back

Page 41: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 41

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300

T1 T2 T3

R(x);

W(y);

R (y);

W(z);

R(x);

W(z);

x y z

RS=200 RS=200 RS=-1

WS=-1 WS=100 WS=300

T1 is rolled back

What happen to RS and WS?

Page 42: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 42

Net result of TO scheduling Conflict pairs of actions are taken in the order of their

home transactions But the basic TO does not guarantee recoverability

later

Page 43: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 43

Validation

An optimistic scheme

Transactions have 3 phases:

(1) Read all DB values read writes to temporary storage no locking

(2) Validate check if schedule so far is serializable

(3) Write if validate ok, write to DB

Page 44: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 44

Key idea Make validation atomic If T1, T2, T3, … is validation order, then resulting

schedule will be conflict equivalent to Ss = T1 T2

T3...

Page 45: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 45

To implement validation, system keeps two sets: FIN = transactions that have finished

phase 3 (and are all done) VAL = transactions that have

successfully finished phase 2 (validation)

Page 46: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 46

Example of what validation must prevent:

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

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

time

T2

start

T2

validated

T3

validatedT3

start

=

Page 47: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 47

T2

finishphase 3

Example of what validation must prevent:

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

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

time

T2

start

T2

validated

T3

validatedT3

start

=

allow

T3

start

Page 48: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 48

Another thing validation must prevent:RS(T2)={A} RS(T3)={A,B}

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

time

T2

validatedT3

validated

finish

T2BAD: w3(D) w2(D)

Page 49: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 49

finish

T2

Another thing validation must prevent:RS(T2)={A} RS(T3)={A,B}

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

time

T2

validatedT3

validated

allow

finish

T2

Page 50: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 50

Validation Rule When start validating T

Check RS(T) WS(U) is empty for U that started but did not finish validation before T started

Check WS(T) WS(U) is empty for any U that started but did not finish validation T start validation

Page 51: 1 Concurrency Control II More on Two Phase Locking Time Stamp Ordering Validation Scheme

Database Implementation – Concurrency Control Yan

Huang 51

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