lecture 21: concurrency & locking - github pagesconcurrency, scheduling & anomalies 2....

58
Lecture 21: Concurrency & Locking Lecture 21

Upload: others

Post on 07-Apr-2020

18 views

Category:

Documents


0 download

TRANSCRIPT

Lecture21:Concurrency&Locking

Lecture21

Today’sLecture

1. Concurrency,scheduling&anomalies

2. Locking:2PL,conflictserializability,deadlockdetection

2

Lecture21

1.Concurrency,Scheduling&Anomalies

3

Lecture21>Section1

Whatyouwilllearnaboutinthissection

1. Interleaving&scheduling

2. Conflict&anomalytypes

4

Lecture21>Section1

Concurrency:Isolation&Consistency

Lecture21>Section1>Interleaving&scheduling

• TheDBMSmusthandleconcurrencysuchthat…

1. Isolation ismaintained:UsersmustbeabletoexecuteeachTXNasiftheyweretheonlyuser• DBMShandlesthedetailsofinterleaving variousTXNs

2. Consistency ismaintained:TXNsmustleavetheDBinaconsistentstate• DBMShandlesthedetailsofenforcingintegrityconstraints

ACID

ACID

Example- considertwoTXNs:

Lecture21>Section1>Interleaving&scheduling

T1: START TRANSACTIONUPDATE AccountsSET Amt = Amt + 100WHERE Name = ‘A’

UPDATE AccountsSET Amt = Amt - 100WHERE Name = ‘B’

COMMIT

T2: START TRANSACTIONUPDATE AccountsSET Amt = Amt * 1.06

COMMIT

T1transfers$100fromB’saccounttoA’saccount

T2creditsbothaccountswitha6%interestpayment

Example- considertwoTXNs:

Lecture21>Section1>Interleaving&scheduling

T1transfers$100fromB’saccounttoA’saccount

T2creditsbothaccountswitha6%interestpayment

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

Time

WecanlookattheTXNsinatimelineview- serialexecution:

Example- considertwoTXNs:

Lecture21>Section1>Interleaving&scheduling

T1transfers$100fromB’saccounttoA’saccount

T2creditsbothaccountswitha6%interestpayment

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

Time

TheTXNscouldoccurineitherorder…DBMSallows!

Example- considertwoTXNs:

Lecture21>Section1>Interleaving&scheduling

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

Time

TheDBMScanalsointerleave theTXNs

T2creditsA’saccountwith6%interestpayment,thenT1transfers$100toA’saccount…

T2creditsB’saccountwitha6%interestpayment,thenT1transfers$100fromB’saccount…

Example- considertwoTXNs:

Lecture21>Section1>Interleaving&scheduling

Whatgoeswronghere??(nothing--it’sT2FollowedbyT1)

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

Time

TheDBMScanalsointerleave theTXNs

Recall:ThreeTypesofRegionsofMemory

1. Local: InourmodeleachprocessinaDBMShasitsownlocalmemory,whereitstoresvaluesthatonlyit“sees”

2. Global:Eachprocesscanreadfrom/writetoshareddatainmainmemory

3. Disk:Globalmemorycanreadfrom/flushtodisk

4. Log:Assumeonstablediskstorage- spansbothmainmemoryanddisk…

Local GlobalMain

Memory(RAM)

Disk

“Flushing todisk”=writingtodisk.

1 2

3

Lecture21>Section1>Interleaving&scheduling

Logisasequence frommainmemory->disk

4

WhyInterleaveTXNs?

• InterleavingTXNsmightleadtoanomalousoutcomes…whydoit?

• Severalimportantreasons:• IndividualTXNsmightbeslow- don’twanttoblockotherusersduring!

• Diskaccessmaybeslow- letsomeTXNsuseCPUswhileothersaccessingdisk!

12

Lecture21>Section1>Interleaving&scheduling

Allconcernlargedifferencesinperformance

Interleaving&Isolation

• TheDBMShasfreedomtointerleaveTXNs

• However,itmustpickaninterleavingorschedulesuchthatisolationandconsistencyaremaintained

• Mustbeasif theTXNshadexecutedserially!

13

Lecture21>Section1>Interleaving&scheduling

DBMSmustpickaschedulewhichmaintainsisolation&consistency

“Withgreatpowercomesgreatresponsibility”

ACID

Schedulingexamples

14

Lecture21>Section1>Interleaving&scheduling

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

A B

$50 $200

A B

$159 $106

A B

$159 $106

StartingBalance

Sameresult!

SerialscheduleT1,T2:

InterleavedscheduleA:

Schedulingexamples

15

Lecture21>Section1>Interleaving&scheduling

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

A B

$50 $200

A B

$159 $106

A B

$159 $112

StartingBalance

DifferentresultthanserialT1,T2!

SerialscheduleT1,T2:

InterleavedscheduleB:

Schedulingexamples

16

Lecture21>Section1>Interleaving&scheduling

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

A B

$50 $200

A B

$153 $112

A B

$159 $112

StartingBalance

DifferentresultthanserialT2,T1ALSO!

SerialscheduleT2,T1:

InterleavedscheduleB:

Schedulingexamples

17

Lecture21>Section1>Interleaving&scheduling

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

Thisscheduleisdifferentthananyserialorder! Wesaythatitisnot

serializable

InterleavedscheduleB:

Scheduling Definitions• Aserialschedule isonethatdoesnotinterleavetheactionsofdifferenttransactions

• AandBareequivalentschedules if, foranydatabasestate,theeffectonDBofexecutingAisidenticaltotheeffectofexecutingB

• Aserializableschedule isa schedulethatisequivalenttosome serialexecutionofthetransactions.

Theword“some”makesthisdefinitionpowerful& tricky!

Lecture21>Section1>Interleaving&scheduling

Serializable?

19

Lecture21>Section1>Interleaving&scheduling

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

SameasaserialscheduleforallpossiblevaluesofA,B=serializable

Serialschedules:

A BT1,T2 1.06*(A+100) 1.06*(B-100)

T2,T1 1.06*A+100 1.06*B- 100

A B1.06*(A+100) 1.06*(B-100)

Serializable?

20

Lecture21>Section1>Interleaving&scheduling

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

Notequivalent toanyserializableschedule =notserializable

Serialschedules:

A BT1,T2 1.06*(A+100) 1.06*(B-100)

T2,T1 1.06*A+100 1.06*B- 100

A B1.06*(A+100) 1.06*B- 100

Whatelsecangowrongwithinterleaving?

• Variousanomalieswhichbreakisolation/serializability

• Oftenreferredtobyname…

• Occurbecauseof/withcertain“conflicts”betweeninterleavedTXNs

21

Lecture21>Section1>Interleaving&scheduling

TheDBMS’sviewoftheschedule

22

Lecture21>Section1>Interleaving&scheduling

T1

T2

A+=100 B -=100

A*=1.06 B *=1.06

T1

T2

R(A)

R(A)

W(A)

W(A) R(B) W(B)

R(B) W(B)

EachactionintheTXNsreadsavaluefromglobalmemory andthenwritesonebacktoit

Schedulingordermatters!

ConflictTypes

• Thus,therearethreetypesofconflicts:• Read-Writeconflicts(RW)• Write-Readconflicts(WR)• Write-Writeconflicts(WW)

Whyno“RRConflict”?

Lecture21>Section1>Interleaving&scheduling

Twoactionsconflict iftheyarepartofdifferentTXNs,involvethesamevariable,andatleastoneofthemisawrite

Interleavinganomaliesoccurwith/becauseoftheseconflictsbetweenTXNs (buttheseconflictscanoccurwithoutcausinganomalies!)

Seenextsectionformore!

OccurringbecauseofaRWconflict

Lecture21>Section1>Interleaving&scheduling

ClassicAnomalieswithInterleavedExecution

“Unrepeatableread”:

T1

T2

R(A) R(A)

1. T1 reads somedata fromA

2. T2 writes toA

3. Then,T1 readsfromAagainandnowgetsadifferent/inconsistentvalue

R(A) W(A) C

Example:

OccurringbecauseofaWRconflict

Lecture21>Section1>Interleaving&scheduling

ClassicAnomalieswithInterleavedExecution

“Dirtyread”/Readinguncommitteddata:

T1

T2

W(A) A

1. T1 writes somedata toA

2. T2 reads fromA,thenwritesbacktoA&commits

3. T1 thenaborts- nowT2’sresultisbasedonanobsolete/inconsistentvalue

R(A) W(A) C

Example:

Lecture21>Section1>Interleaving&scheduling

ClassicAnomalieswithInterleavedExecution

“Inconsistentread”/Readingpartialcommits:

T1

T2

W(A)

1. T1 writes somedata toA

2. T2 reads fromAandB,andthenwritessomevaluewhichdependsonA&B

3. T1 thenwritestoB- nowT2’sresultisbasedonanincompletecommit

Example:

W(B) C

R(A) CR(B) W(C=A*B)

Again,occurringbecauseofaWRconflict

Lecture21>Section1>Interleaving&scheduling

ClassicAnomalieswithInterleavedExecution

Partially-lostupdate:

T1

T2

W(A)

1. T1 blindwrites somedata toA

2. T2 blindwrites toAandB

3. T1 thenblindwrites toB;nowwehaveT2’svalueforBandT1’svalueforA- notequivalenttoanyserialschedule!

Example:

W(B) C

W(A) CW(B)

OccurringbecauseofaWWconflict

2.ConflictSerializability,Locking&Deadlock

28

Lecture21>Section2

Whatyouwilllearnaboutinthissection

1. RECAP:Concurrency

2. ConflictSerializability

3. DAGs&TopologicalOrderings

4. Strict2PL

5. Deadlocks

29

Lecture21>Section2

Recall:ConcurrencyasInterleavingTXNs

• Forourpurposes,havingTXNsoccurconcurrentlymeansinterleavingtheircomponentactions(R/W)

30

Lecture21>Section2>Concurrency

Wecalltheparticularorderofinterleavingaschedule

T1T2

R(A) R(B)W(A) W(B)

SerialSchedule:

R(A) R(B)W(A) W(B)

T1T2

R(A) R(B)W(A) W(B)

InterleavedSchedule:

R(A) R(B)W(A) W(B)

Recall:“Good”vs.“bad”schedules

31

Wewanttodevelopwaysofdiscerning“good”vs.“bad”schedules

SerialSchedule:

T1

T2

R(A) R(B)W(A) W(B)

R(A) R(B)W(A) W(B)

T1

T2

R(A) R(B)W(A) W(B)

R(A) R(B)W(A) W(B)

T1

T2

R(A) R(B)W(A) W(B)

R(A) R(B)W(A) W(B)

X

InterleavedSchedules:

Why?

Lecture21>Section2>Concurrency

WaysofDefining“Good”vs.“Bad”Schedules• Recallfromlasttime:wecallascheduleserializable ifitisequivalenttosome serialschedule

• Weusedthisasanotionofa“good”interleavedschedule,sinceaserializableschedulewillmaintainisolation&consistency

• Now,we’lldefineastricter,butveryusefulvariant:

• Conflictserializability We’llneedtodefineconflicts first..

Lecture21>Section2>Concurrency

Conflicts

Twoactionsconflict iftheyarepartofdifferentTXNs,involvethesamevariable,andatleastoneofthemisawrite

T1

T2

R(A) R(B)W(A) W(B)

R(A) R(B)W(A) W(B)W-RConflict

W-WConflict

Lecture21>Section2>ConflictSerializability

Conflicts

Twoactionsconflict iftheyarepartofdifferentTXNs,involvethesamevariable,andatleastoneofthemisawrite

T1

T2

R(A) R(B)W(A) W(B)

R(A) R(B)W(A) W(B)

All“conflicts”!

Lecture21>Section2>ConflictSerializability

ConflictSerializability• Twoschedulesareconflictequivalentif:

• TheyinvolvethesameactionsofthesameTXNs

• Everypairofconflictingactions oftwoTXNsareorderedinthesameway

• ScheduleSisconflictserializableifSisconflictequivalent tosomeserialschedule

Conflictserializable⇒ serializableSoifwehaveconflictserializable,wehaveconsistency&isolation!

Lecture21>Section2>ConflictSerializability

Recall:“Good”vs.“bad”schedules

36

Conflictserializability alsoprovidesuswithanoperativenotionof“good”vs.“bad”schedules!

SerialSchedule:

T1

T2

R(A) R(B)W(A) W(B)

R(A) R(B)W(A) W(B)

T1

T2

R(A) R(B)W(A) W(B)

R(A) R(B)W(A) W(B)

T1

T2

R(A) R(B)W(A) W(B)

R(A) R(B)W(A) W(B)

X

InterleavedSchedules:

Notethatinthe“bad”schedule,theorderofconflictingactionsisdifferentthantheabove(orany)serialschedule!

Lecture21>Section2>ConflictSerializability

Note:Conflictsvs.Anomalies

• Conflicts arethingswetalkabouttohelpuscharacterizedifferentschedules• Presentinboth“good”and“bad”schedules

• Anomalies areinstanceswhereisolationand/orconsistencyisbrokenbecauseofa“bad”schedule• Weoftencharacterizedifferentanomalytypesbywhattypesofconflictspredicatedthem

Lecture21>Section2>ConflictSerializability

TheConflictGraph

• Let’snowconsiderlookingatconflictsattheTXNlevel

• ConsideragraphwherethenodesareTXNs,andthereisanedgefromTi àTj ifanyactionsinTi precedeandconflictwith anyactionsinTj

T1 T2

T1

T2

R(A) R(B)W(A) W(B)

R(A) R(B)W(A) W(B)

Lecture21>Section2>ConflictSerializability

39

SerialSchedule:

T1

T2

R(A) R(B)W(A) W(B)

R(A) R(B)W(A) W(B)

T1

T2

R(A) R(B)W(A) W(B)

R(A) R(B)W(A) W(B)

T1

T2

R(A) R(B)W(A) W(B)

R(A) R(B)W(A) W(B)

X

InterleavedSchedules:

Whatcanwesayabout“good”vs.“bad”conflictgraphs?

Abitcomplicated…

Lecture21>Section2>ConflictSerializability

40

SerialSchedule:

X

InterleavedSchedules:

Whatcanwesayabout“good”vs.“bad”conflictgraphs?

T1 T2 T1 T2

T1 T2

Theorem:Scheduleisconflictserializable ifandonlyifitsconflictgraphisacyclic

Simple!

Lecture21>Section2>ConflictSerializability

Let’sunpackthisnotionofacyclicconflictgraphs…

Lecture21>Section2>ConflictSerializability

DAGs&TopologicalOrderings

• Atopologicalorderingofadirectedgraphisalinearorderingofitsverticesthatrespectsallthedirectededges

• Adirectedacyclic graph(DAG)alwayshasoneormoretopologicalorderings• (Andthereexistsatopologicalorderingifandonlyiftherearenodirectedcycles)

Lecture21>Section2>Topologicalorderings

DAGs&TopologicalOrderings

• Ex:Whatisonepossibletopologicalorderinghere?

1

32

0Ex:0,1,2,3(or:0,1,3,2)

Lecture21>Section2>Topologicalorderings

DAGs&TopologicalOrderings

• Ex:Whatisonepossibletopologicalorderinghere?

1

32

0

Thereisnone!

Lecture21>Section2>Topologicalorderings

Connectiontoconflictserializability

• Intheconflictgraph,atopologicalorderingofnodescorrespondstoaserialorderingofTXNs

• Thusanacyclic conflictgraphà conflictserializable!

Theorem:Scheduleisconflictserializable ifandonlyifitsconflictgraphisacyclic

Lecture21>Section2>Topologicalorderings

StrictTwo-PhaseLocking

• Weconsiderlocking- specifically,stricttwo-phaselocking- asawaytodealwithconcurrency,becauseisguaranteesconflictserializability(ifitcompletes- seeupcoming…)

• Also(conceptually)straightforwardtoimplement,andtransparenttotheuser!

Lecture21>Section2>Strict2PL

StrictTwo-phaseLocking(Strict2PL)Protocol:

TXNsobtain:

• AnX(exclusive)lockonobjectbeforewriting.

• IfaTXNholds,nootherTXNcangeta lock(SorX)onthatobject.

• AnS(shared)lockonobjectbeforereading

• IfaTXNholds,nootherTXNcangetanXlock onthatobject

• AlllocksheldbyaTXNarereleasedwhenTXNcompletes.

Note:Terminologyhere- “exclusive”,“shared”- meanttobeintuitive- notricks!

Lecture21>Section2>Strict2PL

Pictureof2-PhaseLocking(2PL)

TimeStrict2PL

0locks

#LockstheTXNhas

LockAcquisition

LockReleaseOnTXNcommit!

Lecture21>Section2>Strict2PL

Strict2PLTheorem: Strict2PLallowsonlyscheduleswhosedependencygraphisacyclic

Therefore,Strict2PLonlyallowsconflictserializable⇒ serializableschedules

ProofIntuition:Instrict2PL,ifthereisanedgeTi à Tj (i.e.Ti andTjconflict)thenTj needstowaituntilTi isfinished– socannothaveanedgeTj à Ti

Lecture21>Section2>Strict2PL

Strict2PL

• Ifaschedulefollowsstrict2PL andlocking,itisconflictserializable…

• …andthusserializable• …andthusmaintainsisolation&consistency!

• Notallserializableschedulesareallowedbystrict2PL.

• Solet’susestrict2PL,whatcouldgowrong?

Lecture21>Section2>Strict2PL

DeadlockDetection:Example

First,T1 requestsasharedlockonAtoreadfromit

T1T2

S(A) R(A)

Waits-forgraph:

T1 T2

Lecture21>Section2>Deadlocks

DeadlockDetection:Example

Next,T2 requestsasharedlockonBtoreadfromit

T1T2 S(B) R(B)

S(A) R(A)

Waits-forgraph:

T1 T2

Lecture21>Section2>Deadlocks

DeadlockDetection:Example

T2 thenrequestsanexclusivelockonAtowritetoit- nowT2iswaitingonT1…

T1T2 X(A)S(B) R(B)

S(A) R(A)

Waits-forgraph:

T1 T2

W(A)Waiting…

Lecture21>Section2>Deadlocks

DeadlockDetection:Example

Finally,T1 requestsanexclusivelockonBtowritetoit- nowT1iswaitingonT2…DEADLOCK!

T1T2

X(B)

X(A)S(B) R(B)

S(A) R(A)

Waits-forgraph:

T1 T2

W(A)

W(B)

Cycle=DEADLOCK

Waiting…

Waiting…

Lecture21>Section2>Deadlocks

ERROR: deadlock detectedDETAIL: Process 321 waits for ExclusiveLock on tuple of relation 20 of database 12002; blocked by process 4924.Process 404 waits for ShareLock on transaction 689; blocked by process 552.HINT: See server log for query details.

Theproblem?Deadlock!??! T1 T2

Lecture21>Section2>Deadlocks

Deadlocks

• Deadlock:Cycleoftransactionswaitingforlockstobereleasedbyeachother.

• Twowaysofdealingwithdeadlocks:

1. Deadlockprevention

2. Deadlockdetection

Lecture21>Section2>Deadlocks

DeadlockDetection

• Createthewaits-forgraph:

• Nodesaretransactions

• ThereisanedgefromTi à Tj ifTi iswaitingforTj toreleasealock

• Periodicallycheckfor(andbreak)cyclesinthewaits-forgraph

Lecture21>Section2>Deadlocks

Summary

• ConcurrencyachievedbyinterleavingTXNssuchthatisolation&consistencyaremaintained• Weformalizedanotionofserializability thatcapturedsucha“good”interleavingschedule

• Wedefinedconflictserializability,whichimpliesserializability

• Lockingallowsonlyconflictserializableschedules• Iftheschedulecompletes…(itmaydeadlock!)

Lecture21>Section2