ics 214b: transaction processing and distributed data management

30
1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 2: Enforcing Serializable Schedules Professor Chen Li

Upload: vala

Post on 05-Feb-2016

26 views

Category:

Documents


0 download

DESCRIPTION

ICS 214B: Transaction Processing and Distributed Data Management. Lecture 2: Enforcing Serializable Schedules Professor Chen Li. How to enforce serializable schedules?. Option 1: run system, recording P(S); at end of day, check for P(S) cycles and declare if execution was good. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ICS 214B: Transaction Processing and Distributed Data Management

1

ICS 214B: Transaction Processing and Distributed Data Management

Lecture 2: Enforcing Serializable Schedules

Professor Chen Li

Page 2: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 2

How to enforce serializable schedules?

Option 1: run system, recording P(S); at end of day, check

for P(S) cycles and declare if execution was good

Page 3: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 3

Option 2: prevent P(S) cycles from occurring T1 T2 ….. Tn

Scheduler

DB

How to enforce serializable schedules?

Page 4: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 4

A locking protocol

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

unlock: ui (A)

scheduler

T1 T2

locktable

Page 5: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 5

Rule #1: Well-formed transactions

Ti: … li(A) … pi(A) … ui(A) ...1) Lock an element before accessing

it2) Unlock the element after

accessing it

Page 6: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 6

Rule #2 Legal scheduler

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

Once an element A is locked by one transaction Ti, the schedule will not grant any other lock requests on A

no lj(A)

Page 7: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 7

• 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 8: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 8

Another problem: schedule F is still allowed, but it’s bad!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 9: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 9

Rule #3 Two phase locking (2PL)

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

A transaction once releases a lock, it cannot request more locks.

no unlocks no locks

Page 10: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 10

# locksheld byTi

Time Growing Shrinking Phase Phase

Page 11: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 11

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)

request delayed

Page 12: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 12

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)

request delayed

Page 13: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 13

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);

request delayed

Page 14: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 14

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)

Deadlock!

delayeddelayed

Page 15: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 15

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

Solution

Page 16: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 16

Next step:

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

Page 17: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 17

New Conflict rules for li(A), ui(A):• Check the semantics of the operations, e.g.,

– li(A), uj(A) conflict– li(A), ri(A) conflict; li(A), wi(A) conflict– ri(A), ui(A) conflict; wi(A), ui(A) conflict

• Some actions can never become adjacent after legal swaps– <ui(A), uj(A)>, <li(A), rj(A)>,...

Example: l1(A)r1(A)l1(B)u1(A)l2(A)w2(A)w1(B)u2(A)u1(B)

Page 18: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 18

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

schedule

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

first unlock action of Ti

Page 19: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 19

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: ui(A) <S lj(A)

S = … pi(A) … ui(A) … lj(A) ... qj(A) …By rule 3: SH(Ti) SH(Tj)

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

Page 20: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 20

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 21: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 21

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

mechanisms

Page 22: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 22

Shared locks

S = ...l1(A) r1(A) u1(A) … l2(A) r2(A) u2(A) …

read only, no conflict

Use “shared” lock:S=... ls1(A) r1(A) ls2(A) r2(A) …. us1(A)

us2(A)

Page 23: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 23

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)

“S” = “Shared”, “X” = “Exclusive”Shorthand:ui(A): unlock whatever modes Ti has

locked A

Page 24: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 24

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

Lock an element before accessing itRelease the lock afterwards

Page 25: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 25

• What about transactions that read and write same object?

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

Page 26: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 26

Option 2: Upgrade

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

Ti=... 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

Page 27: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 27

Rule #2 Legal scheduler

S = ....l-Si(A) … … ui(A) …

l-Sj(A) OK, but no l-Xj(A)

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

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

Page 28: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 28

A way to summarize Rule #2

Compatibility matrix: “comp”

S XS true falseX false false

Page 29: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 29

Rule # 3 2PL transactions

No change:• Locking must precede unlocking,

i.e.,• No action sli(X) or xli(X) can be

preceded by ui(X)

Page 30: ICS 214B: Transaction Processing and Distributed Data Management

ICS214B Notes 02 30

Theorem Rules 1,2,3 Conf.serializablefor S/X locks schedules