Concurrency Control

73
1 Concurrency Control Conflict serializability Two phase locking Optimistic concurrency control Source: slides by Hector Garcia-Molina

description

Concurrency Control. Conflict serializability Two phase locking Optimistic concurrency control. Source: slides by Hector Garcia-Molina. Concurrently Executing Transactions. T1T2…Tn. DB (consistency constraints). Example:. T1:Read(A)T2:Read(A) A  A+100A  A  2 - PowerPoint PPT Presentation

Transcript of Concurrency Control

Page 1: Concurrency Control

1

Concurrency ControlConflict serializability

Two phase lockingOptimistic concurrency

controlSource: slides by Hector Garcia-Molina

Page 2: Concurrency Control

2

Concurrently Executing Transactions

T1 T2 … Tn

DB(consistencyconstraints)

Page 3: Concurrency Control

3

Example:T1: Read(A) T2: Read(A)

A A+100 A A2Write(A) Write(A)Read(B) Read(B)B B+100 B B2Write(B) Write(B)

Constraint: A=B

Page 4: Concurrency Control

4

Schedule AT1T2Read(A); A A+100Write(A);Read(B); B B+100;Write(B);

Read(A);A A2;Write(A); Read(B);B B2;Write(B);

A B25 25

125

125

250

250250 250

Page 5: Concurrency Control

5

Schedule BT1T2

Read(A);A A2;Write(A);Read(B);B B2;Write(B);

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

A B25 25

50

50

150

150150 150

Page 6: Concurrency Control

6

Schedule CT1T2Read(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 7: Concurrency Control

7

Schedule DT1T2Read(A); A A+100Write(A);

Read(A);A A2;Write(A); Read(B);B B2;Write(B);

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

A B25 25

125

250

50

150250 150

Page 8: Concurrency Control

8

Schedule ET1T2’Read(A); A A+100Write(A);

Read(A);A A1;Write(A); Read(B);B B1;Write(B);

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

A B25 25

125

125

25

125125 125

Same as Schedule Dbut with new T2’

Page 9: Concurrency Control

9

Want schedules that are “good”,regardless of

initial state and transaction semantics

Only look at order of read and writes

Example: Sc = r1(A) w1(A) r2(A) w2(A) r1(B) w1(B) r2(B)

w2(B)

Page 10: Concurrency Control

10

Sc’ = r1(A) w1(A) r1(B) w1(B) r2(A) w2(A) r2(B) w2(B)

T1 T2

Example: Sc = r1(A) w1(A) r2(A) w2(A) r1(B) w1(B) r2(B) w2(B)

Page 11: Concurrency Control

11

However, for Sd:Sd = r1(A) w1(A) r2(A) w2(A) r2(B) w2(B) r1(B)

w1(B)

as a matter of fact, T2 must precede T1

in any equivalent schedule, i.e., T2 T1

Page 12: Concurrency Control

12

T1 T2 Sd cannot be rearrangedinto a serial scheduleSd is not “equivalent” toany serial scheduleSd is “bad”

T2 T1 Also, T1 T2

Page 13: Concurrency Control

13

Returning to ScSc=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B)

T1 T2 T1 T2

no cycles Sc is “equivalent” to aserial schedule(in this case T1,T2)

Page 14: Concurrency Control

14

ConceptsTransaction: sequence of ri(x), wi(x) actions

Conflicting actions: r1(A) w2(A) w1(A) w2(A) r1(A) w2(A)

Schedule: represents chronological order in which actions are executed

Serial schedule: no interleaving of actions or transactions

Page 15: Concurrency Control

15

What about concurrent actions?

T1 issues System Input(X) t XRead(X,t) issues completes

Input(X) time

T2 issuesWrite(B,s)

Systemissues

Input(B)

Input(B)completes

B s

Systemissues

Output(B)Output(B)completes

Page 16: Concurrency Control

16

So net effect is either

S=…r1(X)…w2(B)… or

S=…w2(B)…r1(X)…

Page 17: Concurrency Control

17

Assume equivalent to either r1(A) w2(A)or w2(A) r1(A)

low level synchronization mechanism Assumption called “atomic actions”

What about conflicting, concurrent actions on same object?

start r1(A) end r1(A)

start w2(A) end w2(A) time

Page 18: Concurrency Control

18

DefinitionS1, S2 are conflict equivalent schedules

if S1 can be transformed into S2 by a series of swaps on non-conflicting actions.

Page 19: Concurrency Control

19

DefinitionA schedule is conflict serializable if it

is conflict equivalent to some serial schedule.

Page 20: Concurrency Control

20

Nodes: transactions in SArcs: Ti Tj whenever

there is an action of Ti in S there is an action of Tj later in S both actions are on same database

element and at least one is a write

Precedence graph P(S) (S is schedule)

Page 21: Concurrency Control

21

Exercise: What is P(S) for

S = w3(A) w2(C) r1(A) w1(B) r1(C) w2(A) r4(A) w4(D)

Is S serializable?

Page 22: Concurrency Control

22

Another Exercise: What is P(S) for

S = w1(A) r2(A) r3(A) w4(A) ?

Page 23: Concurrency Control

23

Characterizing Conflict Serializability

Theorem:

A schedule is conflict serializableif and only if

its precedence graph has no cycles.

Page 24: Concurrency Control

24

How to enforce serializable schedules?Option 1: run system, recording P(S) at end of day, check for cycles in

P(S) declare execution good if no cycles

else bad

Page 25: Concurrency Control

25

Option 2: prevent cycles in P(S) from occurring

T1 T2 ….. Tn

Scheduler

DB

How to enforce serializable schedules?

Page 26: Concurrency Control

26

A locking protocol

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

unlock: ui (A)

schedulerT1 T2

locktable

Page 27: Concurrency Control

27

Rule #1: Well-formed ("consistent") transactions

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

pi(A) is either a read or a write

Page 28: Concurrency Control

28

Rule #2 Legal scheduler

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

no lj(A)

I.e., only one transaction has a DB elementlocked at any time.

Page 29: Concurrency Control

29

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:

What schedules are legal?

Page 30: Concurrency Control

30

Schedule FT1 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 31: Concurrency Control

31

Schedule F

T1 T2 25 25l1(A);Read(A)A A+100;Write(A);u1(A) 125

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

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

250 150

A B

Page 32: Concurrency Control

32

Rule #3 Two phase locking (2PL)

for transactionsTi = ……. li(A) ………... ui(A) ……...

no unlocks no locks

Page 33: Concurrency Control

33

# locksheld byTi

Time Growing Shrinking Phase Phase

Page 34: Concurrency Control

34

Schedule GT1 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 35: Concurrency Control

35

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 36: Concurrency Control

36

Schedule GT1 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 37: Concurrency Control

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

Deadlock!

Page 38: Concurrency Control

38

Assume deadlocked transactions are rolled back They have no effect They do not appear in schedule

E.g., Schedule H =

This space intentionallyleft blank!

Page 39: Concurrency Control

39

Next step:

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

Page 40: Concurrency Control

40

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

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

Note: no conflict < ui(A), uj(A)>, < li(A), rj(A)>,...

Page 41: Concurrency Control

41

2PL Ensures Conflict-Serializability

Theorem:Every schedule produced by two-phase locking, i.e., every read and write is preceded by a

lock and followed by an unlock no DB item is simultaneously locked by

more than one transaction for each transaction, the last lock

precedes the first unlockis conflict-serializable.

Page 42: Concurrency Control

42

Beyond this simple 2PL protocol, it is all a matter of improving performance and allowing more concurrency…. Shared locks Increment locks Multiple granularity Other types of concurrency control

mechanisms

Page 43: Concurrency Control

43

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 44: Concurrency Control

44

Lock actionsl-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 45: Concurrency Control

45

Rule #1 Well formed transactionsT1 =... l-S1(A) … r1(A) … u1 (A) …T1 =... l-X1(A) … w1(A) … u1 (A) …

I.e., must hold a shared lock to read and must hold an exclusive lock to write

Page 46: Concurrency Control

46

What about transactions that read and write same object?

Option 1: Request exclusive lockT1 = ...l-X1(A) … r1(A) ... w1(A) ... u(A)

Page 47: Concurrency Control

47

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

T1=... l-S1(A) … r1(A) ... l-X1(A) …w1(A) ...u(A)…

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

• What about transactions that read and write same object?

Page 48: Concurrency Control

48

Rule #2 Legal schedulerS = ....l-Si(A) … … ui(A) …

no l-Xj(A)S = ... l-Xi(A) … … ui(A) …

no l-Xj(A) no l-Sj(A)

I.e., an element can be locked exclusively by one transaction or by several in shared mode, but not both

Page 49: Concurrency Control

49

A way to summarize Rule #2Compatibility matrix

S XS true falseX false false

Page 50: Concurrency Control

50

Rule # 3 2PL transactionsNo change except for upgrades:(I) If upgrade gets more locks

(e.g., S {S, X}) then no change!(II) If upgrade releases read (shared)

lock (e.g., S X)- can be allowed in growing phase

Page 51: Concurrency Control

51

2PL Ensures Conflict-Serializability With Shared

LocksTheorem:

Every schedule produced by two-phase locking, modified to handle shared locks,is conflict-serializable.

Page 52: Concurrency Control

52

Lock types beyond S/XExamples:

(1) increment lock(2) update lock

Page 53: Concurrency Control

53

Example (1): increment lock Atomic increment action: INi(A)

{Read(A); A A+k; Write(A)}

INi(A), INj(A) do not conflict!A=7

A=5 A=17A=15

INi(A)+2

INj(A)+10

+10INj(A)

+2INi(A)

Page 54: Concurrency Control

54

Compatibility S X IMatrix S

XI

Page 55: Concurrency Control

55

Compatibility S X IMatrix S T F F

X F F FI F F T

Page 56: Concurrency Control

56

Update locksA common deadlock problem with upgrades:T1 T2l-S1(A)

l-S2(A)l-Xl-X11(A)(A)

l-Xl-X22(A)(A) --- Deadlock ---

Page 57: Concurrency Control

57

SolutionIf Ti wants to read A and knows itmay later want to write A, it requestsupdate lock (not shared)

Page 58: Concurrency Control

58

Comp. Matrix S X USXU

New request

Lock alreadyheld in

Page 59: Concurrency Control

59

Comp. Matrix S X US T F TX F F FU TorF F F

-> symmetric table?

New request

Lock alreadyheld in

Page 60: Concurrency Control

60

Note: object A may be locked in different modes at the same time...

S1=...l-S1(A)…l-S2(A)…l-U3(A)… l-S4(A)…? l-U4(A)…? To grant a lock in mode t, mode t must be compatible with all currently held locks on object

Page 61: Concurrency Control

61

How does locking work in practice? Every system is different

(E.g., may not even provide CONFLICT-SERIALIZABLE schedules)

But here is one (simplified) way ...

Page 62: Concurrency Control

62

(1) Don’t trust transactions torequest/release

locks(2) Hold all locks until transaction

commits#locks

time

Sample Locking System:

Page 63: Concurrency Control

63

Ti

Read(A),Write(B)

l(A),Read(A),l(B),Write(B)…

Read(A),Write(B)

Scheduler, part I

Scheduler, part II

DB

locktable

Page 64: Concurrency Control

64

Lock table Conceptually

A

BC

...

Lock info for BLock info for C

If null, object is unlockedEv

ery

poss

ible

obj

ect

Page 65: Concurrency Control

65

But use hash table:

A

If object not found in hash table, it is unlocked

Lock info for AA

......

H

Page 66: Concurrency Control

66

Lock info for A - example tran mode wait? Nxt T_link

Object:AGroup mode:UWaiting:yesList:

T1 S no

T2 U no

T3 XX yes

To other T3 records

Page 67: Concurrency Control

67

What are the objects we lock?

?

Relation A

Relation B

...

Tuple ATuple BTuple C

...

Disk block

ADisk block

B

...

DB DB DB

Page 68: Concurrency Control

68

Locking works in any case, but should we choose small or large objects?

If we lock large objects (e.g., Relations) Need few locks Low concurrency

If we lock small objects (e.g., tuples,fields) Need more locks More concurrency

Page 69: Concurrency Control

69

We can have it both ways!!

Called multi-granularity locking.

Page 70: Concurrency Control

70

Optimistic Concurrency Control

Locking is pessimistic -- assumes something bad will try to happen and prevents it

Instead assume conflicts won't occur in the common case, then check at the end of a transaction timestamping validation

Page 71: Concurrency Control

71

Timestamping Assign a "timestamp" to each

transaction Keep track of the timestamp of the

last transaction to read and write each DB element

Compare these values to check for serializability

Abort transaction if there is a violation

Page 72: Concurrency Control

72

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 73: Concurrency Control

73

Comparison Space usage of optimistic CC is

about the same as locking: roughly proportional to number of DB elements accessed

Optimistic CC is useful when conflicts are rare system resources are plentiful there are real-time constraints