350 likes | 463 Views
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.
E N D
Concurrency Control III Dead Lock Time Stamp Ordering Validation Scheme
Learning Objectives • Dealing with Deadlock and Starvation • Time Stamp Ordering Technique • Validation Database Implementation – Concurrency Control Yan Huang
Deadlocks • Detection • Wait-for graph • Prevention • Resource ordering • Timeout • Wait-die • Wound-wait Database Implementation – Concurrency Control Yan Huang
Deadlock Detection • Build Wait-For graph • Use lock table structures • Build incrementally or periodically • When cycle found, rollback victim T5 T2 T1 T7 T4 T6 T3 Database Implementation – Concurrency Control Yan Huang
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 Database Implementation – Concurrency Control Yan Huang
Timeout • If transaction waits more than L sec., roll it back! • Simple scheme • Hard to select L Database Implementation – Concurrency Control Yan Huang
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 Database Implementation – Concurrency Control Yan Huang
wait? Example: T1 (ts =10) T2 (ts =20) T3 (ts =25) wait wait Very high level: only older ones have the privilege to wait, younger ones die if they attempt to wait for older ones Database Implementation – Concurrency Control Yan Huang
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 Database Implementation – Concurrency Control Yan Huang
wait Example: T1 (ts =25) T2 (ts =20) T3 (ts =10) wait wait Very high level: younger ones wait; older ones kill (wound) younger ones who hold needed locks Database Implementation – Concurrency Control Yan Huang
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? Database Implementation – Concurrency Control Yan Huang
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 Database Implementation – Concurrency Control Yan Huang
Timestamp • System time when transactions starts • An increasing unique number given to each stransaction • Denoted by ts(Ti) Database Implementation – Concurrency Control Yan Huang
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 Database Implementation – Concurrency Control Yan Huang
x y z RS=-1 RS=-1 RS=-1 WS=-1 WS=-1 WS=-1 Example Assuming: 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); Database Implementation – Concurrency Control Yan Huang
x y z RS=100 RS=-1 RS=-1 WS=-1 WS=-1 WS=-1 Example Assuming: 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); Database Implementation – Concurrency Control Yan Huang
x y z RS=100 RS=-1 RS=-1 WS=-1 WS=100 WS=-1 Example Assuming: 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); Database Implementation – Concurrency Control Yan Huang
x y z RS=100 RS=200 RS=-1 WS=-1 WS=100 WS=-1 Example Assuming: 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); Database Implementation – Concurrency Control Yan Huang
x y z RS=100 RS=200 RS=-1 WS=-1 WS=100 WS=300 Example Assuming: 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); Database Implementation – Concurrency Control Yan Huang
x y z RS=200 RS=200 RS=-1 WS=-1 WS=100 WS=300 Example Assuming: 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); Database Implementation – Concurrency Control Yan Huang
x y z RS=200 RS=200 RS=-1 WS=-1 WS=100 WS=300 Example Assuming: 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); T1 is rolled back Database Implementation – Concurrency Control Yan Huang
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 Database Implementation – Concurrency Control Yan Huang
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 Database Implementation – Concurrency Control Yan Huang
Time stamps of a transaction Ti • Start(Ti) • Validation(Ti) • Finish(Ti) Database Implementation – Concurrency Control Yan Huang
Key idea • Make validation atomic • If T1, T2, T3, … is validation order, then resulting schedule will be conflict equivalent to Ss = T1 T2 T3... Database Implementation – Concurrency Control Yan Huang
Schedule T1 T2 Read(A) A A+100; Read(A) A Ax2; Read(B);B B+100 validate Write(A) Write(B); Read(B) B Bx2; validate Write(A) Write(B); Database Implementation – Concurrency Control Yan Huang
= Example of what validation must prevent: RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C} T2 validate T3 validate T3 finishes T2 finishes T2 start T3 start time Database Implementation – Concurrency Control Yan Huang
= allow Example of what validation must prevent: RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C} T2 validated T3 validated T2 start T3 start T3 start T2 finish phase 3 time Database Implementation – Concurrency Control Yan Huang
BAD: w3(D) w2(D) Another thing validation must prevent: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C,D} T2 validated T3 validated finish T2 time Database Implementation – Concurrency Control Yan Huang
allow Another thing validation must prevent: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C,D} T2 validated T3 validated finish T2 finish T2 time Database Implementation – Concurrency Control Yan Huang
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) Database Implementation – Concurrency Control Yan Huang
start validate finish Exercise: U: RS(U)={B} WS(U)={D} W: RS(W)={A,D} WS(W)={A,C} V: RS(V)={B} WS(V)={D,E} T: RS(T)={A,B} WS(T)={A,C} Database Implementation – Concurrency Control Yan Huang
start validate finish Exercise: U: RS(U)={B} WS(U)={D} W: RS(W)={A,D} WS(W)={A,C} V: RS(V)={B} WS(V)={D,E} T: RS(T)={A,B} WS(T)={A,C} Database Implementation – Concurrency Control Yan Huang
start validate finish Exercise: U: RS(U)={B} WS(U)={D} W: RS(W)={A,D} WS(W)={A,C} V: RS(V)={B} WS(V)={D,E} T: RS(T)={A,B} WS(T)={A,C} Database Implementation – Concurrency Control Yan Huang
start validate finish W is rolled bak Exercise: U: RS(U)={B} WS(U)={D} W: RS(W)={A,D} WS(W)={A,C} V: RS(V)={B} WS(V)={D,E} T: RS(T)={A,B} WS(T)={A,C} Database Implementation – Concurrency Control Yan Huang