concurrency control iii

35
1 Concurrency Control III Dead Lock Time Stamp Ordering Validation Scheme

Upload: taariq

Post on 23-Mar-2016

27 views

Category:

Documents


1 download

DESCRIPTION

Concurrency Control III. Dead Lock Time Stamp Ordering Validation Scheme. Learning Objectives. Dealing with Deadlock and Starvation Time Stamp Ordering Technique Validation. Deadlocks. Detection Wait-for graph Prevention Resource ordering Timeout Wait-die Wound-wait. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Concurrency Control III

1

Concurrency Control III

Dead LockTime Stamp Ordering

Validation Scheme

Page 2: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 2

Learning Objectives

Dealing with Deadlock and Starvation Time Stamp Ordering Technique Validation

Page 3: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 3

Deadlocks Detection

Wait-for graph Prevention

Resource ordering Timeout Wait-die Wound-wait

Page 4: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 4

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 5: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 5

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 6: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 6

Timeout

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

Simple scheme Hard to select L

Page 7: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 7

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 8: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 8

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 9: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 9

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 10: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 10

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 11: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 11

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 12: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 12

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 13: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 13

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

Denoted by ts(Ti)

Page 14: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 14

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 15: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 15

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300T1 T2 T3R(x); W(y);

R (y); W(z); R(x);

W(z); R(y); W(x);

x y zRS=-1 RS=-1 RS=-1WS=-1 WS=-1 WS=-1

Page 16: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 16

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300T1 T2 T3R(x); W(y);W(y);

R (y);R (y); W(z);W(z); R(x); R(x);

W(z);W(z); R(y); R(y); W(x);W(x);

x y zRS=100 RS=-1 RS=-1WS=-1 WS=-1 WS=-1

Page 17: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 17

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300T1 T2 T3R(x); W(y);

R (y);R (y); W(z);W(z); R(x); R(x);

W(z);W(z); R(y); R(y); W(x);W(x);

x y zRS=100 RS=-1 RS=-1WS=-1 WS=100 WS=-1

Page 18: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 18

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300T1 T2 T3R(x); W(y);

R (y); W(z);W(z); R(x); R(x);

W(z);W(z); R(y); R(y); W(x);W(x);

x y zRS=100 RS=200 RS=-1WS=-1 WS=100 WS=-1

Page 19: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 19

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300T1 T2 T3R(x); W(y);

R (y); W(z); R(x); R(x);

W(z);W(z); R(y); R(y); W(x);W(x);

x y zRS=100 RS=200 RS=-1WS=-1 WS=100 WS=300

Page 20: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 20

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300T1 T2 T3R(x); W(y);

R (y); W(z);

R(x);W(z);W(z); R(y); R(y); W(x);W(x);

x y zRS=200 RS=200 RS=-1WS=-1 WS=100 WS=300

Page 21: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 21

ExampleAssuming: ts(T1) = 100, ts(T2) = 200, ts(T3) = 300T1 T2 T3R(x); W(y);

R (y); W(z); R(x);

W(z); R(y); R(y); W(x);W(x);

x y zRS=200 RS=200 RS=-1WS=-1 WS=100 WS=300

T1 is rolled back

Page 22: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 22

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

Page 23: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 23

Validation

An optimistic schemeTransactions 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 24: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 24

Time stamps of a transaction Ti Start(Ti) Validation(Ti) Finish(Ti)

Page 25: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 25

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 26: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 26

Schedule

T1 T2Read(A)A A+100;

Read(A) A Ax2;

Read(B);B B+100validateWrite(A)Write(B); Read(B) B Bx2; validate Write(A) Write(B);

Page 27: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 27

Example of what validation must prevent:

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

time

T2start

T2validate

T3validateT3

start

=

T2finishes

T3finishes

Page 28: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 28

T2finish

phase 3

Example of what validation must prevent:

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

time

T2start

T2validated

T3validated

T3start

=

allow

T3start

Page 29: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 29

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

time

T2validated

T3validated

finishT2BAD: w3(D) w2(D)

Page 30: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 30

finishT2

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

time

T2validated

T3validated

allow

finishT2

Page 31: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 31

Validation Rule When start validating T

Check RS(T) WS(U) is empty for any 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 before T started validation)

Page 32: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 32

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

Page 33: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 33

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

Page 34: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 34

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

Page 35: Concurrency Control III

Database Implementation – Concurrency Control Yan

Huang 35

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

W is rolled bak