cs 4604: introducon to database management...

Post on 24-Dec-2020

9 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS4604:Introduc0ontoDatabaseManagementSystems

B.AdityaPrakashLecture#17:Transac0ons2:2PLand

Deadlocks

Announcement

§  HW6isout– DuenextWednesday(April13)

Prakash2016 VTCS4604 2

Review(lastlecture)

§  DBMSssupportACIDTransac0onseman0cs.§  ConcurrencycontrolandCrashRecoveryarekeycomponents

Prakash2016 VTCS4604 3

Review

§  ForIsola0onproperty,serialexecu0onoftransac0onsissafebutslow– Trytofindschedulesequivalenttoserialexecu0on

§  Onesolu0onfor“conflictserializable”schedulesisTwoPhaseLocking(2PL)

Prakash2016 VTCS4604 4

Outline

§  2PL/2PLC§  LockManagement§  Deadlocks– detec0on– Preven0on

§  SpecializedLocking

Prakash2016 VTCS4604 5

SerializabilityinPrac0ce

§  DBMSdoesnottestforconflictserializabilityofagivenschedule–  Imprac0calasinterleavingofopera0onsfromconcurrentXactscouldbedictatedbytheOS

§  Approach:– Usespecificprotocolsthatareknowntoproduceconflictserializableschedules

– Butmayreduceconcurrency

Prakash2016 VTCS4604 6

Solu0on?

§  Onesolu0onfor“conflictserializable”schedulesisTwoPhaseLocking(2PL)

Prakash2016 VTCS4604 7

Answer

§  (Fullanswer:)uselocks;keepthemun0lcommit(‘strict2phaselocking’)

§  Let’sseethedetails

Prakash2016 VTCS4604 8

Lostupdateproblem-nolocks

T1Read(N)N=N-1Write(N) Prakash2016 VTCS4604 9

T2Read(N)N=N-1Write(N)

Solu0on–part1

§  withlocks:§  lockmanager:grants/denieslockrequests

Prakash2016 VTCS4604 10

Lostupdateproblem–withlocks

0me

T1

lock(N)

Read(N)

N=N-1

Write(N)

Unlock(N)

T2

lock(N)

lockmanager

grantslock

denieslock

T2:waits

grantslocktoT2Read(N)...

Prakash2016 VTCS4604 11

Locks

§  Q:Ijustneedtoread‘N’-shouldIs0llgetalock?

Prakash2016 VTCS4604 12

Solu0on–part1

§  Locksandtheirflavors– exclusive(orwrite-)locks– shared(orread-)locks– <andmore...>

§  compa0bilitymatrix

X

S

XST2wantsT1has

Prakash2016 VTCS4604 13

Solu0on–part1

§  Locksandtheirflavors– exclusive(orwrite-)locks– shared(orread-)locks– <andmore...>

§  compa0bilitymatrix

X

S

XST2wantsT1has

Yes

Prakash2016 VTCS4604 14

Solu0on–part1

§  transac0onsrequestlocks(orupgrades)§  lockmanagergrantsorblocksrequests§  transac0onsreleaselocks§  lockmanagerupdateslock-table

Prakash2016 VTCS4604 15

Solu0on–part2

locksarenotenough–eg.,the‘inconsistentanalysis’problem

Prakash2016 VTCS4604 16

‘Inconsistentanalysis’

T1 Read(A) A=A-10 Write(A)

T2 Read(A) Sum = A

Read(B) Sum += B

Read(B) B=B+10 Write(B)

0me

Prakash2016 VTCS4604 17

‘Inconsistentanalysis’–w/locks

0me T1

L(A)

Read(A)

...

U(A)

T2

L(A)

....

L(B)

....

theproblemremains!

T2readsaninconsistentDBstate

Solu0on??

Prakash2016 VTCS4604 18

Generalsolu0on:

§  Protocol(s)§ Mostpopularprotocol:2PhaseLocking(2PL)

Prakash2016 VTCS4604 19

2PL

X-lockversion:transac0onsissuenolockrequests,aoerthefirst‘unlock’

THEOREM:ifALLtransac0onsinthesystemobey2PL-->allschedulesareserializable

Prakash2016 VTCS4604 20

2PL–example

§  ‘inconsistentanalysis’–howdoes2PLhelp?§  howwoulditbeunder2PL?

Prakash2016 VTCS4604 21

2PL–X/Slockversion

transac0onsissuenolock/upgraderequest,aoerthefirstunlock/downgrade

Ingeneral:‘growing’and‘shrinking’phase

0me

#locks

growingphase shrinkingphase

Prakash2016 VTCS4604 22

2PL–X/Slockversion

transac0onsissuenolock/upgraderequest,aoerthefirstunlock/downgrade

Ingeneral:‘growing’and‘shrinking’phase

0me

#locks

viola0onof2PL

Prakash2016 VTCS4604 23

Two-PhaseLocking(2PL),cont.

§  2PLonitsownissufficienttoguaranteeconflictserializability(i.e.,scheduleswhoseprecedencegraphisacyclic),but,itissubjecttoCascadingAborts.

time

# locks held release phase acquisition

phase

Prakash2016 VTCS4604 24

2PL

§  Problem:CascadingAborts§  Example:rollbackofT1requiresrollbackofT2!

§  Solu0on:Strict2PL,i.e,§  keepalllocks,un0l‘commit’

T1:R(A),W(A), R(B),W(B),AbortT2: R(A),W(A)

Prakash2016 VTCS4604 25

Strict2PL

§  Allowsonlyconflictserializableschedules,butitisactuallystrongerthanneededforthatpurpose.

# locks held

acquisition phase

time

release all locks at end of xact

Prakash2016 VTCS4604 26

Strict2PL==2PLC(2PL0llCommit)

§  Ineffect,“shrinkingphase”isdelayedun0l– Transac0oncommits(commitlogrecordondisk),or

– Aborts(thenlockscanbereleasedaoerrollback).

# locks held

acquisition phase

time

release all locks at end of xact

Prakash2016 VTCS4604 27

Prakash 2016 VT CS 4604 28

Lock_X(A) Read(A) A: = A-50 Write(A) Unlock(A)

Lock_S(A) Read(A) Unlock(A) Lock_S(B) Read(B) Unlock(B) PRINT(A+B)

Lock_X(B) Read(B) B := B +50 Write(B) Unlock(B)

Non-2PL, A= 1000, B=2000, Output =?

Prakash 2016 VT CS 4604 29

Lock_X(A) Read(A) A: = A-50 Write(A)

Lock_X(B) Unlock(A)

Lock_S(A) Read(A)

Read(B) B := B +50 Write(B) Unlock(B)

Lock_S(B) Unlock(A) Read(B) Unlock(B) PRINT(A+B)

2PL, A= 1000, B=2000, Output =?

Prakash 2016 VT CS 4604 30

Lock_X(A) Read(A) A: = A-50 Write(A)

Lock_X(B) Read(B) B := B +50 Write(B) Unlock(A) Unlock(B)

Lock_S(A) Read(A) Lock_S(B) Read(B) PRINT(A+B) Unlock(A) Unlock(B)

Strict 2PL, A= 1000, B=2000, Output =?

VennDiagramforSchedulesAll Schedules

Avoid Cascading Abort

Serial

Conflict Serializable

Prakash2016 VTCS4604 31

Q:WhichschedulesdoesStrict2PLallow?

All Schedules

Avoid Cascading Abort

Serial

Conflict Serializable

Prakash2016 VTCS4604 32

Q:WhichschedulesdoesStrict2PLallow?

All Schedules

Avoid Cascading Abort

Serial

Conflict Serializable

Prakash2016 VTCS4604 33

AnotherVenndiagram

2PLschedules

serializableschedules

serialsch’s2PLC

Prakash2016 VTCS4604 34

Outline

§  2PL/2PLC§  LockManagement§  Deadlocks– detec0on– Preven0on

§  SpecializedLocking

Prakash2016 VTCS4604 35

LockManagement

§  LockandunlockrequestshandledbytheLockManager(LM).

§  LMcontainsanentryforeachcurrentlyheldlock.

§  Q:structureofalocktableentry?

Prakash2016 VTCS4604 36

LockManagement

§  LockandunlockrequestshandledbytheLockManager(LM).

§  LMcontainsanentryforeachcurrentlyheldlock.

§  Locktableentry:– Ptr.tolistoftransac0onscurrentlyholdingthelock

– Typeoflockheld(sharedorexclusive)– Pointertoqueueoflockrequests

Prakash2016 VTCS4604 37

LockManagement,cont.

§ Whenlockrequestarrivesseeifanyotherxactholdsaconflic0nglock.–  Ifnot,createanentryandgrantthelock– Else,puttherequestoronthewaitqueue

§  Lockupgrade:transac0onthatholdsasharedlockcanbeupgradedtoholdanexclusivelock

Prakash2016 VTCS4604 38

LockManagement,cont.

§  Two-phaselockingissimpleenough,right?§ We’renotdone.There’sanimportantwrinkle…

Prakash2016 VTCS4604 39

Prakash2016 VTCS4604 40

Example: Output = ?

Lock_X(A) Lock_S(B) Read(B) Lock_S(A)

Read(A) A: = A-50 Write(A) Lock_X(B)

Prakash2016 VTCS4604 41

Example: Output = ?

Lock_X(A) Lock_S(B) Read(B) Lock_S(A)

Read(A) A: = A-50 Write(A) Lock_X(B)

lock mgr: grant grant

wait

wait

Outline

§  LockManagement§  Deadlocks– detec0on– Preven0on

§  SpecializedLocking

Prakash2016 VTCS4604 42

Deadlocks

§  Deadlock:Cycleoftransac0onswai0ngforlockstobereleasedbyeachother.

§  Twowaysofdealingwithdeadlocks:– Deadlockpreven0on– Deadlockdetec0on

§ ManysystemsjustpuntanduseTimeouts– Whatarethedangerswiththisapproach?

Prakash2016 VTCS4604 43

DeadlockDetec0on

§  Createawaits-forgraph:– Nodesaretransac0ons– EdgefromTitoTjifTiiswai0ngforTjtoreleasealock

§  Periodicallycheckforcyclesinwaits-forgraph

Prakash2016 VTCS4604 44

DeadlockDetec0on(Con0nued)

Example: T1: S(A), S(D), S(B) T2: X(B) X(C) T3: S(D), S(C), X(A) T4: X(B)

T1 T2

T4 T3Prakash2016 VTCS4604 45

Anotherexample

T1 T2

T3 T4

•  is there a deadlock?

•  if yes, which xacts are involved?

Prakash2016 VTCS4604 46

Anotherexample

T1 T2

T3 T4

•  now, is there a deadlock?

•  if yes, which xacts are involved?

Prakash2016 VTCS4604 47

Deadlockdetec0on

§  howooenshouldwerunthealgo?§  howmanytransac0onsaretypicallyinvolved?

Prakash2016 VTCS4604 48

Deadlockhandling

T1 T2

T3 T4

•  Q: what to do?

Prakash2016 VTCS4604 49

Deadlockhandling

T1 T2

T3 T4

•  Q0: what to do?

•  A: select a ‘victim’ & ‘rollback’

•  Q1: which/how to choose?

Prakash2016 VTCS4604 50

Deadlockhandling

•  Q1: which/how to choose?

•  A1.1: by age

•  A1.2: by progress

•  A1.3: by # items locked already...

•  A1.4: by # xacts to rollback

•  Q2: How far to rollback?

T1 T2

T3 T4

Prakash2016 VTCS4604 51

Deadlockhandling

•  Q2: How far to rollback?

• A2.1: completely

• A2.2: minimally

•  Q3: Starvation??

T1 T2

T3 T4

Prakash2016 VTCS4604 52

Deadlockhandling

•  Q3: Starvation??

•  A3.1: include #rollbacks in victim selection criterion.

T1 T2

T3 T4

Prakash2016 VTCS4604 53

Outline

§  LockManagement§  Deadlocks– detec0on– Preven0on

§  SpecializedLocking

Prakash2016 VTCS4604 54

DeadlockPreven0on

§  Assignpriori0esbasedon0mestamps(older->higherpriority)

§  Weonlyallow‘old-wait-for-young’§  (oronlyallow‘young-wait-for-old’)§  androllbackviolators.Specifically:§  SayTiwantsalockthatTjholds-twopolicies:– Wait-Die:IfTihashigherpriority,TiwaitsforTj;otherwiseTiaborts(ie.,oldwaitforyoung)

– Wound-wait:IfTihashigherpriority,Tjaborts;otherwiseTiwaits(ie.,youngwaitforold)

Prakash2016 VTCS4604 55

DeadlockPreven0on

Wait-Die Wound-Wait Ti wants Tj has Ti wants Tj has

Priori0es

Prakash2016 VTCS4604 56

DeadlockPreven0on

§  Q:Whydotheseschemesguaranteenodeadlocks?

§  A:§  Q:Whenatransac0onrestarts,whatisits(new)priority?

§  A:

Prakash2016 VTCS4604 57

DeadlockPreven0on

§  Q:Whydotheseschemesguaranteenodeadlocks?

§  A:onlyone‘type’ofdirec0onallowed.§  Q:Whenatransac0onrestarts,whatisits(new)priority?

§  A:itsoriginal0mestamp.--Why?

Prakash2016 VTCS4604 58

SQLstatement

§  usually,conc.controlistransparenttotheuser,but

§  LOCK<table-name>[EXCLUSIVE|SHARED]

Prakash2016 VTCS4604 59

Quiz:

§  isthereaserialschedule(=interleaving)thatisnotserializable?

§  isthereaserializableschedulethatisnotserial?

§  can2PLproduceanon-serializableschedule?(assumenodeadlocks)

Prakash2016 VTCS4604 60

Quiz-cont’d

§  isthereaserializableschedulethatcannotbeproducedby2PL?

§  axactobeys2PL-canitbeinvolvedinanon-serializableschedule?

§  allxactsobey2PL-cantheyendupinadeadlock?

Prakash2016 VTCS4604 61

Outline

§  LockManagement§  Deadlocks– detec0on– Preven0on

§  SpecializedLocking

Prakash2016 VTCS4604 62

Thingswewillnotstudy§  Weassumed0llnowDBobjectsarefixedandindependent---nottrueinmanycases!§  Mul0-levellocking

–  Lockdborfileorpagesorrecord?§  Whataboutlockingindexes?

–  E.g.B+-trees–  CrabbingAlgorithm

§  Whataboutdynamicdatabases?–  ‘phantom’problem–  Solu0on:predicatelocking

§  Non-lockingbasedTechniques–  TimestampbasedConcurrencyControl

§  Alltheseareinthetextbookthough

SKIP

Prakash2016 VTCS4604 63

Transac0onSupportinSQL-92

§  SERIALIZABLE–Nophantoms,allreadsrepeatable,no“dirty”(uncommited)reads.

§  REPEATABLEREADS–phantomsmayhappen.§  READCOMMITTED–phantomsandunrepeatablereadsmayhappen

§  READUNCOMMITTED–allofthemmayhappen.

Prakash2016 VTCS4604 64

Transac0onSupportinSQL-92

§  SERIALIZABLE:obtainsalllocksfirst;plusindexlocks,plusstrict2PL

§  REPEATABLEREADS–asabove,butnoindexlocks

§  READCOMMITTED–asabove,butS-locksarereleasedimmediately

§  READUNCOMMITTED–asabove,butallowing‘dirtyreads’(noS-locks)

Prakash2016 VTCS4604 65

Transac0onSupportinSQL-92

§  SETTRANSACTIONISOLATIONLEVELSERIALIZABLEREADONLY

§  Defaults:§  SERIALIZABLE§  READWRITE

isolation level

access mode

Prakash2016 VTCS4604 66

Conclusions

§  2PL/2PL-C(=Strict2PL):extremelypopular§  Deadlockmays0llhappen– detec0on:wait-forgraph– preven0on:abortsomexacts,defensively

§  philosophically:concurrencycontroluses:–  locks– andaborts

Prakash2016 VTCS4604 67

top related