1 cs542 concurrency control: theory and protocol professor elke a. rundensteiner

41
1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

Upload: eustace-oneal

Post on 21-Jan-2016

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

1

CS542

Concurrency Control:Theory and Protocol

Professor Elke A. Rundensteiner

Page 2: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

5

For Schedule D:Sd=r1(A)w1(A)r2(A)w2(A)

r2(B)w2(B)r1(B)w1(B)

T1 T2 T2 T1

T1 T2

D cannot be rearranged into serial schedule

Page 3: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

6

Idea: Swap non-conflicting operation pairs to see if you can go to a serial

schedule.

Page 4: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

7

Definition

S1, S2 are conflict equivalent schedulesif S1 can be transformed into S2 by a series of swaps on non-conflicting actions.

Page 5: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

8

Definition

A schedule is conflict serializable if it is conflict equivalent to some (any) serial schedule.

Page 6: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

9

One Answer: A Precedence Graph !

How determine this ?

Page 7: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

10

Nodes: transactions in SArcs: Ti Tj whenever

- pi(A), qj(A) are actions in S- pi(A) <S qj(A)

- at least one of pi, qj is a write

Precedence graph P(S) (S is

schedule)

Page 8: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

11

Exercise:

• What is P(S) forS = w3(A) w2(C) r1(A) w1(B) r1(C) w2(A) r4(A) w4(D)

• Is S conflict serializable?

Page 9: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

12

Another Exercise:

• What is P(S) forS = w1(A) r2(A) r3(A) w4(A) ?

• Is S conflict serializable?

Page 10: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

13

Lemma

S1, S2 conflict equivalent P(S1)=P(S2)

Proof:Assume P(S1) P(S2) Ti: Ti Tj in S1 and not in S2

S1 = …pi(A)... qj(A)… pi, qj

S2 = …qj(A)…pi(A)... conflict

S1, S2 not conflict equivalent

Page 11: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

14

Counter example:

S1=w1(A) r2(A) w2(B) r1(B) S2=r2(A) w1(A) r1(B) w2(B)

P(S1)=P(S2) S1, S2 conflict equivalent ??

Note: P(S1)=P(S2) S1, S2 conflict equivalent

Page 12: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

15

Theorem

P(S1) acyclic S1 conflict serializable

() Assume S1 is conflict serializable Ss: Ss, S1 conflict equivalent P(Ss) = P(S1)

P(S1) acyclic since P(Ss) is acyclic

Page 13: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

16

() Assume P(S1) is acyclicTransform S1 as follows:(1) Take T1 to be transaction with no incident arcs(2) Move all T1 actions to the front

S1 = ……. qj(A)…….p1(A)…..

(3) we now have S1 = < T1 actions ><... rest ...>(4) repeat above steps to serialize rest!

T1

T2 T3

T4

Theorem

P(S1) acyclic S1 conflict serializable

Page 14: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

17

How to enforce serializable schedules?

Page 15: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

18

How to enforce serializable schedules?

Option 1 : try all possible swaps of non-

conflicting operation pairs to determine if the schedule can be turned into a serial one,

i.e., if the schedule is ‘good’.

Page 16: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

19

How to enforce serializable schedules?

Option 2: a) run system, recording P(S);

b) at end of day, check for P(S) cycles

c) and declare if execution was good

Page 17: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

20

Option 3: prevent P(S) cycles from occurring

T1 T2 ….. Tn

Scheduler

DB

How to enforce serializable schedules?

Page 18: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

21

A locking protocol

Two new actions:lock (exclusive): li (A)

unlock: ui (A)

scheduler

T1 T2

locktable

Page 19: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

22

Rule #1: Well-formed transactions

Ti: … li(A) … pi(A) … ui(A) ...

Page 20: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

23

Rule #2 Legal scheduler

S = …….. li(A) ………... ui(A) ……...

no lj(A)

Page 21: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

24

• What schedules are legal?What transactions are well-formed?S1 = l1(A)l1(B)r1(A)w1(B)l2(B)u1(A)u1(B)r2(B)w2(B)u2(B)l3(B)r3(B)u3(B)

S2 = l1(A)r1(A)w1(B)u1(A)u1(B)l2(B)r2(B)w2(B)l3(B)r3(B)u3(B)

S3 = l1(A)r1(A)u1(A)l1(B)w1(B)u1(B)l2(B)r2(B)w2(B)u2(B)l3(B)r3(B)u3(B)

Exercise:

Page 22: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

25

• What schedules are legal?What transactions are well-formed?S1 = l1(A)l1(B)r1(A)w1(B)l2(B)u1(A)u1(B)r2(B)w2(B)u2(B)l3(B)r3(B)u3(B)

S2 = l1(A)r1(A)w1(B)u1(A)u1(B)l2(B)r2(B)w2(B)l3(B)r3(B)u3(B)

S3 = l1(A)r1(A)u1(A)l1(B)w1(B)u1(B)l2(B)r2(B)w2(B)u2(B)l3(B)r3(B)u3(B)

Exercise:

Page 23: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

26

Schedule F : Adding locking ! How?

T1 T2l1(A);Read(A)A A+100;Write(A);u1(A)

l2(A);Read(A)A Ax2;Write(A);u2(A)l2(B);Read(B)B Bx2;Write(B);u2(B)

l1(B);Read(B)B B+100;Write(B);u1(B)

Legal ? Well-formed ?

Page 24: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

27

Schedule F : Adding locking ! How?

T1 T2l1(A);Read(A)A A+100;Write(A);u1(A)

l2(A);Read(A)A Ax2;Write(A);u2(A)l2(B);Read(B)B Bx2;Write(B);u2(B)

l1(B);Read(B)B B+100;Write(B);u1(B)

A B25 25

125

250

50

150250 150

Page 25: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

28

Rule #3 Two phase locking (2PL)

for transactions

Ti = ……. li(A) ………... ui(A) ……...

no unlocks no locks

Page 26: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

29

# locksheld byTi

Time Growing Shrinking Phase Phase

2 Phase Locking Protocol

Page 27: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

30

Schedule F : Can 2PL fix it ?

T1 T2l1(A);Read(A)A A+100;Write(A);u1(A)

l2(A);Read(A)A Ax2;Write(A);u2(A)l2(B);Read(B)B Bx2;Write(B);u2(B)

l1(B);Read(B)B B+100;Write(B);u1(B)

Page 28: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

31

Schedule G

T1 T2l1(A);Read(A)A A+100;Write(A)l1(B); u1(A)

l2(A);Read(A) A Ax2;Write(A);

Page 29: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

32

Schedule G

T1 T2l1(A);Read(A)A A+100;Write(A)l1(B); u1(A)

l2(A);Read(A) A Ax2;Write(A);ll22(B)(B)

delayed

Page 30: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

33

Schedule G

T1 T2l1(A);Read(A)A A+100;Write(A)l1(B); u1(A)

l2(A);Read(A) A Ax2;Write(A);ll22(B)(B)

Read(B);B B+100Write(B); u1(B)

delayed

Page 31: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

34

Schedule G

T1 T2l1(A);Read(A)A A+100;Write(A)l1(B); u1(A)

l2(A);Read(A) A Ax2;Write(A);ll22(B)(B)

Read(B);B B+100Write(B); u1(B)

l2(B); u2(A);Read(B) B Bx2;Write(B);u2(B);

delayed

Page 32: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

35

We Got the GOOD Schedule !!!!

T1 T2Read(A); A A+100Write(A);

Read(A);A A2;Write(A);

Read(B); B B+100;Write(B);

Read(B);B B2;Write(B);

A B25 25

125

250

125

250250 250

Page 33: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

36

Schedule H (T2 reversed)

T1 T2l1(A); Read(A) l2(B);Read(B)A A+100;Write(A) B Bx2;Write(B)

. . .. . . . . . . . .

What’s happening ?

Page 34: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

37

Schedule H (T2 reversed)

T1 T2l1(A); Read(A) l2(B);Read(B)A A+100;Write(A) B Bx2;Write(B)

ll11(B)(B) l l22(A)(A)delayeddelayed

Page 35: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

38

• Deadlocked transactions can arise in ‘good’ schedules (generated due to 2PL locking).

• We assume that when they are rolled back:– They have no effect– They do not appear in schedule

E.g., Schedule H =This space intentionallyleft blank!

Page 36: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

39

Correctness of 2PC Protocol :

Show that rules #1,2,3 Conflict-serializable schedules

Page 37: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

40

Conflict rules for li(A), ui(A):

• li(A), lj(A) conflict • li(A), uj(A) conflict

Page 38: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

41

Theorem Rules #1,2,3 conflict (2PL) serializable

schedule

To help in proof:Definition Shrink(Ti) = SH(Ti) =

first unlock action of Ti

Page 39: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

42

LemmaTi Tj in S SH(Ti) <S SH(Tj)

Proof of lemma:Ti Tj means that

S = … pi(A) … qj(A) …; p,q conflictBy rules 1,2:

S = … pi(A) … ui(A) … lj(A) ... qj(A) … SH(Ti) SH(Tj)

By rule 3:

So, SH(Ti) <S SH(Tj)

Page 40: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

43

Proof:(1) Assume P(S) has cycle

T1 T2 …. Tn T1

(2) By lemma: SH(T1) < SH(T2) < ... <

SH(T1)

(3) Impossible, so P(S) acyclic(4) S is conflict serializable

Theorem Rules #1,2,3 conflict (2PL) serializable

schedule

Page 41: 1 CS542 Concurrency Control: Theory and Protocol Professor Elke A. Rundensteiner

44

• Beyond this simple 2PL protocol, it is all a matter of improving

performance and allowing yet more concurrency….– Shared locks– Multiple granularity– Inserts, deletes and phantoms– Other types of C.C. mechanisms