1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

49
1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing

description

3 Key idea Make validation atomic If T 1, T 2, T 3, … is validation order, then resulting schedule will be conflict equivalent to S s = T 1 T 2 T 3...

Transcript of 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

Page 1: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

1

CSE232A: Database System Principles

More Concurrency Control and Transaction

Processing

Page 2: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

2

ValidationTransactions 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 3: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

3

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 4: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

4

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 5: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

5

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

=

Page 6: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

6

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 7: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

7

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 8: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

8

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 9: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

9

Validation rules for Tj:(1) When Tj starts phase 1:

ignore(Tj) FIN(2) at Tj Validation:

if check (Tj) then [ VAL VAL U {Tj}; do write phase; FIN FIN U {Tj} ]

Page 10: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

10

Check (Tj):For Ti VAL - IGNORE (Tj) DO

IF [ WS(Ti) RS(Tj) OR Ti FIN ] THEN RETURN false;

RETURN true;

Is this check too restrictive ?

Page 11: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

11

Improving Check(Tj)

For Ti VAL - IGNORE (Tj) DO IF [ WS(Ti) RS(Tj) OR

(Ti FIN AND WS(Ti) WS(Tj) )]

THEN RETURN false;RETURN true;

Page 12: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

12

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 13: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

13

Validation (also called optimistic concurrency control) is useful in some cases:

- Conflicts rare- System resources plentiful- Have real time constraints

Page 14: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

14

SummaryHave studied C.C. mechanisms used

in practice- 2 PL- Multiple granularity- Tree (index) protocols- Validation

Page 15: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

15

Chapter 10 More on transaction processing

Topics (which I doubt we’ll get to all of them):

• Cascading rollback, recoverable schedule

• Deadlocks– Prevention– Detection

• View serializability• Long transactions (nested,

compensation)

Page 16: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

16

Example: Tj Ti

Wj(A) ri(A) Commit Ti

Abort Tj

Concurrency control & recovery

……

… ……

… Cascading rollback (Bad!)

Page 17: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

17

• Schedule is conflict serializable• Tj Ti

• But not recoverable

Page 18: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

18

• Need to make “final’ decision for each transaction:– commit decision - system guarantees

transaction will or has completed, no matter what

– abort decision - system guarantees transaction will or has been rolled back

(has no effect)

Page 19: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

19

To model this, two new actions:• Ci - transaction Ti commits• Ai - transaction Ti aborts

Page 20: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

20

......

......

Back to example:

Tj Ti

Wj(A)ri(A)

Ci can we commit here?

Page 21: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

21

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

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

(2) aj <S ri(A) (< : does not precede)

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

Page 22: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

22

Definition

Schedule S is recoverable if whenever Tj S Ti and j i and Ci

Sthen Cj <S Ci

Page 23: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

23

Note: in transactions, reads and writes precede commit or abort

If Ci Ti, then ri(A) < Ci wi(A) < Ci

If Ai Ti, then ri(A) < Ai wi(A) < Ai

• Also, one of Ci, Ai per transaction

Page 24: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

24

How to achieve recoverable schedules?

Page 25: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

25

With 2PL, hold write locks to commit (strict 2PL)

Tj Ti

Wj(A)

Cjuj(A)

ri(A)

......

...

......

......

Page 26: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

26

With validation, no change!

Page 27: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

27

• S 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.

Page 28: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

28

• S is strict if each transaction may read and write only items previously written by committed transactions.

Avoids cascading rollback

RC

ACR

ST SERIAL

Page 29: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

29

Where are serializable schedules?

Avoids cascading rollback

RC

ACR

ST SERIAL

Page 30: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

30

Examples• Recoverable:

– 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

Assumes w2(A) is donewithout reading

Page 31: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

31

Deadlocks• Detection

– Wait-for graph• Prevention

– Resource ordering– Timeout– Wait-die– Wound-wait

Page 32: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

32

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 33: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

33

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 34: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

34

Timeout• If transaction waits more than L

sec., roll it back!

• Simple scheme• Hard to select L

Page 35: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

35

Wait-die• Transactions given a timestamp

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

ts(Tj) ...else die

Page 36: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

36

T1(ts =10)

T2(ts =20)

T3 (ts =25)

wait

wait

Example:

wait?

Page 37: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

37

Wound-wait• Transactions 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 38: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

38

User/Program commands

Lots of variations, but in general• Begin_work• Commit_work• Abort_work

Page 39: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

39

Nested transactionsUser program:

Begin_work;

If results_ok, then commit workelse abort_work

......

...

Page 40: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

40

Nested transactionsUser program:

Begin_work;Begin_work;

If results_ok, then commit work else {abort_work; try something else…}

If results_ok, then commit workelse abort_work

...

...

...

Page 41: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

41

Parallel Nested TransactionsT1: begin-work

parallel:T11: begin_work

commit_workT12: begin_work

commit_workcommit_work

......

......

T1

T11 T12

T1

Page 42: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

42

LockingLocking

What are we really locking?

Page 43: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

43

Example:Ti

Read record r1

Read record r1 do recordlockingModify record r3

......

......

Page 44: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

44

But underneath:

Diskpages

R3

R1

R2

record idIf we lock alldata involved in read of R1, we may preventan update to R2(which may require reorganization withinblock)

Page 45: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

45

Solution: view DB at two levelsTop level: record actions

record locks undo/redo actions — logical

e.g., Insert record(X,Y,Z) Redo: insert(X,Y,Z) Undo: delete

Page 46: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

46

Low level: deal with physical details latch page during action(release at end of action)

Page 47: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

47

Note: undo does not return physical DB to original state; only same logical state

e.g., Insert R3 Undo (delete R3)

R1 R1R2

R1R2

R2 R3

Page 48: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

48

Related idea: Sagas• Long running activity: T1, T2, ... Tn

• Each step/trasnaction Ti has a compensating transaction Ti-1

• Semantic atomicity: execute one of– T1, T2, ... Tn – T1, T2, ... Tn-1 T-1

n-1, T-1n-2, ... T-1

1

– T1, T2, ... Tn-2 T-1n-2, T-1

n-3, ... T-11

– T1, T-11

– nothing

...

Page 49: 1 CSE232A: Database System Principles More Concurrency Control and Transaction Processing.

49

Summary• Cascading rollback

Recoverable schedule• Deadlock

– Prevention– Detectoin

• Nested transactions• Multi-level view