1 15-214
SchoolofComputerScience
PrinciplesofSo3wareConstruc9onSerializabilityandTransac9onsJoshBloch CharlieGarrod
2 15-214
Administrivia
• Homework6checkpointdueFriday5p.m.• FinalexamFriday,Dec16th5:30-8:30p.m.,GHC4401
– ReviewsessionWednesday,Dec14th7-9:30p.m.,DH1112
3 15-214
“MadMax”
publicclassMax{publicstaticdoublemax(double...vals){if(vals.length==0)thrownewIllegalArgumentException("Novalues!");doubleresult=Double.MIN_VALUE;for(doubleval:vals)if(val>result)result=val;returnresult;}publicstaticvoidmain(String[]arguments){System.out.println(max(-1,0,-2.718281828));}}
4 15-214
Whatdoesitprint?
publicclassMax{publicstaticdoublemax(double...vals){if(vals.length==0)thrownewIllegalArgumentException("Novalues!");doubleresult=Double.MIN_VALUE;for(doubleval:vals)if(val>result)result=val;returnresult;}publicstaticvoidmain(String[]arguments){System.out.println(max(-1,0,-2.718281828));}}
(a) 0.0 (b) 4.9E-324 (c) Throwsexcep:on(d) Noneoftheabove
5 15-214
(a) 0.0 (b) 4.9E-324 (c) Throwsexcep9on(d) Noneoftheabove Double.MIN_VALUEisverydifferentfromInteger.MIN_VALUE
Whatdoesitprint?
6 15-214
AnotherlookpublicclassMax{publicstaticdoublemax(double...vals){if(vals.length==0)thrownewIllegalArgumentException("Novalues!");doubleresult=Double.MIN_VALUE;for(doubleval:vals)if(val>result)result=val;returnresult;}publicstaticvoidmain(String[]arguments){System.out.println(max(-1,0,-2.718281828));}}
Integer.MIN_VALUEismostnega+veint.Double.MIN_VALUEisthesmallestposi+vedouble.
7 15-214
Youcouldfixitlikethis...
publicclassMax{publicstaticdoublemax(double...vals){if(vals.length==0)thrownewIllegalArgumentException("Novalues!");doubleresult=Double.NEGATIVE_INFINITY;//Mindoublevalfor(doubleval:vals)if(val>result)result=val;returnresult;}publicstaticvoidmain(String[]arguments){System.out.println(max(-1,0,-2.718281828));}}
Prints0.0
8 15-214
Butthisfixismuchbe`er
publicclassMax{publicstaticdoublemax(doublefirst,double...rest){doubleresult=first;for(doubleval:rest)if(val>result)result=val;returnresult;}publicstaticvoidmain(String[]arguments){System.out.println(max(-1,0,-2.718281828));}}
Prints0.0
9 15-214
• TheleastdoublevalisDouble.NEGATIVE_INFINITY,notDouble.MIN_VALUE– ThesameistrueofFloat
• Ifamethodrequiresoneormorearguments,declarewith(Tfirst,T...rest)– Thetechniquegeneralizestonormorevalues,foranyn
• ForAPIdesigners– Don’tviolatetheprincipleofleastastonishment– Useconsistentnames
Themoral
10 15-214
Last9me:MapReduce
• Master– Assigntaskstoworkers– Pingworkerstotestforfailures
• Mapworkers– Mapforeachkey/valuepair– Emitintermediatekey/valuepairs
• Reduceworkers
– Sortdatabyintermediatekeyandaggregatebykey
– Reduceforeachkey
The shuffle:
11 15-214
• Foreachpairofpeopleinasocialnetwork,countmutualfriends§ ForMap:key1isaperson,valueisthelistoftheirfriends§ ForReduce:key2isapairofpeople,valuesisalistof1s,foreachmutualfriendthatpairhas
MapReducetocountmutualfriendsandetc…
f1(String key1, String value): for each pair of friends
in value: EmitIntermediate(pair, 1);
f2(String key2, Iterator values): int result = 0; for each v in values: result += v; Emit(key2, result);
MapReduce: (person, friends)* à (pair of people, count of mutual friends)*
12 15-214
Today:Serializabilityandtransac9ons
• Aformaldefini9onofconsistency• Introduc9ontotransac9ons• Concurrencycontrolandserializability• Distributedconcurrencycontrol(9mepermikng)
– Two-phasecommit
13 15-214
Anaside:Double-entrybookkeeping
• Astyleofaccoun9ngwhereeveryeventconsistsoftwoseparateentries:acreditandadebit
voidtransfer(AccountfromAcct,AccounttoAcct,intval){fromAccount.debit(val);toAccount.credit(val);}staticfinalAccountBANK_LIABILITIES=…;voiddeposit(AccounttoAcct,intval){transfer(BANK_LIABILITIES,toAcct,val);}booleanwithdraw(AccountfromAcct,intval){if(fromAcct.getBalance()<val)returnfalse;transfer(fromAcct,BANK_LIABILITIES,val);returntrue;}
14 15-214
Someproper9esofdouble-entrybookkeeping
• Redundancy!• Sumofallaccountsissta9c
– Canbe0
15 15-214
Dataconsistencyofanapplica9on
• SupposeDisthedatabaseforsomeapplica9onandϕ isafunc9onfromdatabasestatesto{true,false}– Wecallϕ an integrity constraint for the application if ϕ(D)is
trueifthestateDis"good" – WesayadatabasestateDisconsistentifϕ(D)istrueforallintegrity
constraintsϕ– WesayDisinconsistentifϕ(D)isfalseforanyintegrityconstraintϕ
16 15-214
Dataconsistencyofanapplica9on
• SupposeDisthedatabaseforsomeapplica9onandϕ isafunc9onfromdatabasestatesto{true,false}– Wecallϕ an integrity constraint for the application if ϕ(D)is
trueifthestateDis"good" – WesayadatabasestateDisconsistentifϕ(D)istrueforallintegrity
constraintsϕ– WesayDisinconsistentifϕ(D)isfalseforanyintegrityconstraintϕ
• E.g.,forabankusingdouble-entrybookkeepingonepossibleintegrityconstraintis:def IsConsistent(D): If sum(all account balances in D) == 0: Return True Else: Return False
17 15-214
Database transactions
• Atransac+onisanatomicsequenceofreadandwriteopera9ons(alongwithanycomputa9onalsteps)thattakesadatabasefromonestatetoanother– "Atomic"~indivisible
• Transac9onsalwaysterminatewitheither:– Commit:completetransac9on'schangessuccessfully– Abort:undoanypar9alworkofthetransac9on
18 15-214
Database transactions
• Atransac+onisanatomicsequenceofreadandwriteopera9ons(alongwithanycomputa9onalsteps)thattakesadatabasefromonestatetoanother– "Atomic"~indivisible
• Transac9onsalwaysterminatewitheither:– Commit:completetransac9on'schangessuccessfully– Abort:undoanypar9alworkofthetransac9on
booleanwithdraw(AccountfromAcct,intval){begin_transaction();if(fromAcct.getBalance()<val){abort_transaction();returnfalse;}transfer(fromAcct,BANK_LIABILITIES,val);commit_transaction();returntrue;}
19 15-214
Afunc9onalviewoftransac9ons
• Atransac9onT isafunc9onthattakesthedatabasefromonestateD toanotherstateT(D)
• Inacorrectapplica9on,ifDisconsistentthenT(D)isconsistentforalltransac9onsT
20 15-214
Afunc9onalviewoftransac9ons
• Atransac9onT isafunc9onthattakesthedatabasefromonestateD toanotherstateT(D)
• Inacorrectapplica9on,ifDisconsistentthenT(D)isconsistentforalltransac9onsT– E.g.,inacorrectapplica9onanyserialexecu9onofmul9pletransac9ons
takesthedatabasefromoneconsistentstatetoanotherconsistentstate
21 15-214
Databasetransac9onsinprac9ce
• Theapplica9onrequestscommitorabort,butthedatabasemayarbitrarilyabortanytransac9on– Applica9oncanrestartanabortedtransac9on
• Transac9onACIDproper9es:– Atomicity: Allornothing– Consistency: Applica9on-dependentasbefore– Isola9on: Eachtransac9onrunsasifalone– Durability: Databasewillnotabortorundoworkof
atransac9ona3eritconfirmsthecommit
22 15-214
Concurrenttransac9onsandserializability
• Forgoodperformance,databaseinterleavesopera9onsofconcurrenttransac9ons
23 15-214
Concurrenttransac9onsandserializability
• Forgoodperformance,databaseinterleavesopera9onsofconcurrenttransac9ons
• Problemstoavoid:– Lostupdates
• Anothertransac9onoverwritesyourupdate,basedonolddata– Inconsistentretrievals
• Readingpar9alwritesbyanothertransac9on• Readingwritesbyanothertransac9onthatsubsequentlyaborts
• Ascheduleoftransac9onopera9onsisserializableifitisequivalenttosomeserialorderingofthetransac9ons
24 15-214
Concurrencycontrolforadatabase
• Two-phaselocking(2PL)– Phase1:acquirelocks– Phase2:releaselocks
• E.g.,– Lockanobjectbeforereadingorwri9ngit– Don'treleaseanylocksun9lcommitorabort
25 15-214
Summary
• Distributedsystemsareagreatsourceofcomplexity– Abstrac9onstoreducecomplexity:
• Protocols• RPCandcomputa9onalframeworks• Commonbuildingblocks