transaction_test.cc 174 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #ifndef ROCKSDB_LITE
  6. #include "utilities/transactions/transaction_test.h"
  7. #include <algorithm>
  8. #include <functional>
  9. #include <string>
  10. #include <thread>
  11. #include "db/db_impl/db_impl.h"
  12. #include "rocksdb/db.h"
  13. #include "rocksdb/options.h"
  14. #include "rocksdb/perf_context.h"
  15. #include "rocksdb/utilities/transaction.h"
  16. #include "rocksdb/utilities/transaction_db.h"
  17. #include "table/mock_table.h"
  18. #include "test_util/fault_injection_test_env.h"
  19. #include "test_util/sync_point.h"
  20. #include "test_util/testharness.h"
  21. #include "test_util/testutil.h"
  22. #include "test_util/transaction_test_util.h"
  23. #include "util/random.h"
  24. #include "util/string_util.h"
  25. #include "utilities/merge_operators.h"
  26. #include "utilities/merge_operators/string_append/stringappend.h"
  27. #include "utilities/transactions/pessimistic_transaction_db.h"
  28. #include "port/port.h"
  29. using std::string;
  30. namespace ROCKSDB_NAMESPACE {
  31. INSTANTIATE_TEST_CASE_P(
  32. DBAsBaseDB, TransactionTest,
  33. ::testing::Values(
  34. std::make_tuple(false, false, WRITE_COMMITTED, kOrderedWrite),
  35. std::make_tuple(false, true, WRITE_COMMITTED, kOrderedWrite),
  36. std::make_tuple(false, false, WRITE_PREPARED, kOrderedWrite),
  37. std::make_tuple(false, true, WRITE_PREPARED, kOrderedWrite),
  38. std::make_tuple(false, true, WRITE_PREPARED, kUnorderedWrite),
  39. std::make_tuple(false, false, WRITE_UNPREPARED, kOrderedWrite),
  40. std::make_tuple(false, true, WRITE_UNPREPARED, kOrderedWrite)));
  41. INSTANTIATE_TEST_CASE_P(
  42. DBAsBaseDB, TransactionStressTest,
  43. ::testing::Values(
  44. std::make_tuple(false, false, WRITE_COMMITTED, kOrderedWrite),
  45. std::make_tuple(false, true, WRITE_COMMITTED, kOrderedWrite),
  46. std::make_tuple(false, false, WRITE_PREPARED, kOrderedWrite),
  47. std::make_tuple(false, true, WRITE_PREPARED, kOrderedWrite),
  48. std::make_tuple(false, true, WRITE_PREPARED, kUnorderedWrite),
  49. std::make_tuple(false, false, WRITE_UNPREPARED, kOrderedWrite),
  50. std::make_tuple(false, true, WRITE_UNPREPARED, kOrderedWrite)));
  51. INSTANTIATE_TEST_CASE_P(
  52. StackableDBAsBaseDB, TransactionTest,
  53. ::testing::Values(
  54. std::make_tuple(true, true, WRITE_COMMITTED, kOrderedWrite),
  55. std::make_tuple(true, true, WRITE_PREPARED, kOrderedWrite),
  56. std::make_tuple(true, true, WRITE_UNPREPARED, kOrderedWrite)));
  57. // MySQLStyleTransactionTest takes far too long for valgrind to run.
  58. #ifndef ROCKSDB_VALGRIND_RUN
  59. INSTANTIATE_TEST_CASE_P(
  60. MySQLStyleTransactionTest, MySQLStyleTransactionTest,
  61. ::testing::Values(
  62. std::make_tuple(false, false, WRITE_COMMITTED, kOrderedWrite, false),
  63. std::make_tuple(false, true, WRITE_COMMITTED, kOrderedWrite, false),
  64. std::make_tuple(false, false, WRITE_PREPARED, kOrderedWrite, false),
  65. std::make_tuple(false, false, WRITE_PREPARED, kOrderedWrite, true),
  66. std::make_tuple(false, true, WRITE_PREPARED, kOrderedWrite, false),
  67. std::make_tuple(false, true, WRITE_PREPARED, kOrderedWrite, true),
  68. std::make_tuple(false, false, WRITE_UNPREPARED, kOrderedWrite, false),
  69. std::make_tuple(false, false, WRITE_UNPREPARED, kOrderedWrite, true),
  70. std::make_tuple(false, true, WRITE_UNPREPARED, kOrderedWrite, false),
  71. std::make_tuple(false, true, WRITE_UNPREPARED, kOrderedWrite, true),
  72. std::make_tuple(false, true, WRITE_PREPARED, kUnorderedWrite, false),
  73. std::make_tuple(false, true, WRITE_PREPARED, kUnorderedWrite, true)));
  74. #endif // ROCKSDB_VALGRIND_RUN
  75. TEST_P(TransactionTest, DoubleEmptyWrite) {
  76. WriteOptions write_options;
  77. write_options.sync = true;
  78. write_options.disableWAL = false;
  79. WriteBatch batch;
  80. ASSERT_OK(db->Write(write_options, &batch));
  81. ASSERT_OK(db->Write(write_options, &batch));
  82. // Also test committing empty transactions in 2PC
  83. TransactionOptions txn_options;
  84. Transaction* txn0 = db->BeginTransaction(write_options, txn_options);
  85. ASSERT_OK(txn0->SetName("xid"));
  86. ASSERT_OK(txn0->Prepare());
  87. ASSERT_OK(txn0->Commit());
  88. delete txn0;
  89. // Also test that it works during recovery
  90. txn0 = db->BeginTransaction(write_options, txn_options);
  91. ASSERT_OK(txn0->SetName("xid2"));
  92. txn0->Put(Slice("foo0"), Slice("bar0a"));
  93. ASSERT_OK(txn0->Prepare());
  94. delete txn0;
  95. reinterpret_cast<PessimisticTransactionDB*>(db)->TEST_Crash();
  96. ASSERT_OK(ReOpenNoDelete());
  97. assert(db != nullptr);
  98. txn0 = db->GetTransactionByName("xid2");
  99. ASSERT_OK(txn0->Commit());
  100. delete txn0;
  101. }
  102. TEST_P(TransactionTest, SuccessTest) {
  103. ASSERT_OK(db->ResetStats());
  104. WriteOptions write_options;
  105. ReadOptions read_options;
  106. std::string value;
  107. ASSERT_OK(db->Put(write_options, Slice("foo"), Slice("bar")));
  108. ASSERT_OK(db->Put(write_options, Slice("foo2"), Slice("bar")));
  109. Transaction* txn = db->BeginTransaction(write_options, TransactionOptions());
  110. ASSERT_TRUE(txn);
  111. ASSERT_EQ(0, txn->GetNumPuts());
  112. ASSERT_LE(0, txn->GetID());
  113. ASSERT_OK(txn->GetForUpdate(read_options, "foo", &value));
  114. ASSERT_EQ(value, "bar");
  115. ASSERT_OK(txn->Put(Slice("foo"), Slice("bar2")));
  116. ASSERT_EQ(1, txn->GetNumPuts());
  117. ASSERT_OK(txn->GetForUpdate(read_options, "foo", &value));
  118. ASSERT_EQ(value, "bar2");
  119. ASSERT_OK(txn->Commit());
  120. ASSERT_OK(db->Get(read_options, "foo", &value));
  121. ASSERT_EQ(value, "bar2");
  122. delete txn;
  123. }
  124. // The test clarifies the contract of do_validate and assume_tracked
  125. // in GetForUpdate and Put/Merge/Delete
  126. TEST_P(TransactionTest, AssumeExclusiveTracked) {
  127. WriteOptions write_options;
  128. ReadOptions read_options;
  129. std::string value;
  130. Status s;
  131. TransactionOptions txn_options;
  132. txn_options.lock_timeout = 1;
  133. const bool EXCLUSIVE = true;
  134. const bool DO_VALIDATE = true;
  135. const bool ASSUME_LOCKED = true;
  136. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  137. ASSERT_TRUE(txn);
  138. txn->SetSnapshot();
  139. // commit a value after the snapshot is taken
  140. ASSERT_OK(db->Put(write_options, Slice("foo"), Slice("bar")));
  141. // By default write should fail to the commit after our snapshot
  142. s = txn->GetForUpdate(read_options, "foo", &value, EXCLUSIVE);
  143. ASSERT_TRUE(s.IsBusy());
  144. // But the user could direct the db to skip validating the snapshot. The read
  145. // value then should be the most recently committed
  146. ASSERT_OK(
  147. txn->GetForUpdate(read_options, "foo", &value, EXCLUSIVE, !DO_VALIDATE));
  148. ASSERT_EQ(value, "bar");
  149. // Although ValidateSnapshot is skipped the key must have still got locked
  150. s = db->Put(write_options, Slice("foo"), Slice("bar"));
  151. ASSERT_TRUE(s.IsTimedOut());
  152. // By default the write operations should fail due to the commit after the
  153. // snapshot
  154. s = txn->Put(Slice("foo"), Slice("bar1"));
  155. ASSERT_TRUE(s.IsBusy());
  156. s = txn->Put(db->DefaultColumnFamily(), Slice("foo"), Slice("bar1"),
  157. !ASSUME_LOCKED);
  158. ASSERT_TRUE(s.IsBusy());
  159. // But the user could direct the db that it already assumes exclusive lock on
  160. // the key due to the previous GetForUpdate call.
  161. ASSERT_OK(txn->Put(db->DefaultColumnFamily(), Slice("foo"), Slice("bar1"),
  162. ASSUME_LOCKED));
  163. ASSERT_OK(txn->Merge(db->DefaultColumnFamily(), Slice("foo"), Slice("bar2"),
  164. ASSUME_LOCKED));
  165. ASSERT_OK(
  166. txn->Delete(db->DefaultColumnFamily(), Slice("foo"), ASSUME_LOCKED));
  167. ASSERT_OK(txn->SingleDelete(db->DefaultColumnFamily(), Slice("foo"),
  168. ASSUME_LOCKED));
  169. txn->Rollback();
  170. delete txn;
  171. }
  172. // This test clarifies the contract of ValidateSnapshot
  173. TEST_P(TransactionTest, ValidateSnapshotTest) {
  174. for (bool with_flush : {true}) {
  175. for (bool with_2pc : {true}) {
  176. ASSERT_OK(ReOpen());
  177. WriteOptions write_options;
  178. ReadOptions read_options;
  179. std::string value;
  180. assert(db != nullptr);
  181. Transaction* txn1 =
  182. db->BeginTransaction(write_options, TransactionOptions());
  183. ASSERT_TRUE(txn1);
  184. ASSERT_OK(txn1->Put(Slice("foo"), Slice("bar1")));
  185. if (with_2pc) {
  186. ASSERT_OK(txn1->SetName("xid1"));
  187. ASSERT_OK(txn1->Prepare());
  188. }
  189. if (with_flush) {
  190. auto db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  191. db_impl->TEST_FlushMemTable(true);
  192. // Make sure the flushed memtable is not kept in memory
  193. int max_memtable_in_history =
  194. std::max(
  195. options.max_write_buffer_number,
  196. static_cast<int>(options.max_write_buffer_size_to_maintain) /
  197. static_cast<int>(options.write_buffer_size)) +
  198. 1;
  199. for (int i = 0; i < max_memtable_in_history; i++) {
  200. db->Put(write_options, Slice("key"), Slice("value"));
  201. db_impl->TEST_FlushMemTable(true);
  202. }
  203. }
  204. Transaction* txn2 =
  205. db->BeginTransaction(write_options, TransactionOptions());
  206. ASSERT_TRUE(txn2);
  207. txn2->SetSnapshot();
  208. ASSERT_OK(txn1->Commit());
  209. delete txn1;
  210. auto pes_txn2 = dynamic_cast<PessimisticTransaction*>(txn2);
  211. // Test the simple case where the key is not tracked yet
  212. auto trakced_seq = kMaxSequenceNumber;
  213. auto s = pes_txn2->ValidateSnapshot(db->DefaultColumnFamily(), "foo",
  214. &trakced_seq);
  215. ASSERT_TRUE(s.IsBusy());
  216. delete txn2;
  217. }
  218. }
  219. }
  220. TEST_P(TransactionTest, WaitingTxn) {
  221. WriteOptions write_options;
  222. ReadOptions read_options;
  223. TransactionOptions txn_options;
  224. string value;
  225. Status s;
  226. txn_options.lock_timeout = 1;
  227. s = db->Put(write_options, Slice("foo"), Slice("bar"));
  228. ASSERT_OK(s);
  229. /* create second cf */
  230. ColumnFamilyHandle* cfa;
  231. ColumnFamilyOptions cf_options;
  232. s = db->CreateColumnFamily(cf_options, "CFA", &cfa);
  233. ASSERT_OK(s);
  234. s = db->Put(write_options, cfa, Slice("foo"), Slice("bar"));
  235. ASSERT_OK(s);
  236. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  237. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  238. TransactionID id1 = txn1->GetID();
  239. ASSERT_TRUE(txn1);
  240. ASSERT_TRUE(txn2);
  241. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
  242. "TransactionLockMgr::AcquireWithTimeout:WaitingTxn", [&](void* /*arg*/) {
  243. std::string key;
  244. uint32_t cf_id;
  245. std::vector<TransactionID> wait = txn2->GetWaitingTxns(&cf_id, &key);
  246. ASSERT_EQ(key, "foo");
  247. ASSERT_EQ(wait.size(), 1);
  248. ASSERT_EQ(wait[0], id1);
  249. ASSERT_EQ(cf_id, 0U);
  250. });
  251. get_perf_context()->Reset();
  252. // lock key in default cf
  253. s = txn1->GetForUpdate(read_options, "foo", &value);
  254. ASSERT_OK(s);
  255. ASSERT_EQ(value, "bar");
  256. ASSERT_EQ(get_perf_context()->key_lock_wait_count, 0);
  257. // lock key in cfa
  258. s = txn1->GetForUpdate(read_options, cfa, "foo", &value);
  259. ASSERT_OK(s);
  260. ASSERT_EQ(value, "bar");
  261. ASSERT_EQ(get_perf_context()->key_lock_wait_count, 0);
  262. auto lock_data = db->GetLockStatusData();
  263. // Locked keys exist in both column family.
  264. ASSERT_EQ(lock_data.size(), 2);
  265. auto cf_iterator = lock_data.begin();
  266. // The iterator points to an unordered_multimap
  267. // thus the test can not assume any particular order.
  268. // Column family is 1 or 0 (cfa).
  269. if (cf_iterator->first != 1 && cf_iterator->first != 0) {
  270. FAIL();
  271. }
  272. // The locked key is "foo" and is locked by txn1
  273. ASSERT_EQ(cf_iterator->second.key, "foo");
  274. ASSERT_EQ(cf_iterator->second.ids.size(), 1);
  275. ASSERT_EQ(cf_iterator->second.ids[0], txn1->GetID());
  276. cf_iterator++;
  277. // Column family is 0 (default) or 1.
  278. if (cf_iterator->first != 1 && cf_iterator->first != 0) {
  279. FAIL();
  280. }
  281. // The locked key is "foo" and is locked by txn1
  282. ASSERT_EQ(cf_iterator->second.key, "foo");
  283. ASSERT_EQ(cf_iterator->second.ids.size(), 1);
  284. ASSERT_EQ(cf_iterator->second.ids[0], txn1->GetID());
  285. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  286. s = txn2->GetForUpdate(read_options, "foo", &value);
  287. ASSERT_TRUE(s.IsTimedOut());
  288. ASSERT_EQ(s.ToString(), "Operation timed out: Timeout waiting to lock key");
  289. ASSERT_EQ(get_perf_context()->key_lock_wait_count, 1);
  290. ASSERT_GE(get_perf_context()->key_lock_wait_time, 0);
  291. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  292. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
  293. delete cfa;
  294. delete txn1;
  295. delete txn2;
  296. }
  297. TEST_P(TransactionTest, SharedLocks) {
  298. WriteOptions write_options;
  299. ReadOptions read_options;
  300. TransactionOptions txn_options;
  301. Status s;
  302. txn_options.lock_timeout = 1;
  303. s = db->Put(write_options, Slice("foo"), Slice("bar"));
  304. ASSERT_OK(s);
  305. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  306. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  307. Transaction* txn3 = db->BeginTransaction(write_options, txn_options);
  308. ASSERT_TRUE(txn1);
  309. ASSERT_TRUE(txn2);
  310. ASSERT_TRUE(txn3);
  311. // Test shared access between txns
  312. s = txn1->GetForUpdate(read_options, "foo", nullptr, false /* exclusive */);
  313. ASSERT_OK(s);
  314. s = txn2->GetForUpdate(read_options, "foo", nullptr, false /* exclusive */);
  315. ASSERT_OK(s);
  316. s = txn3->GetForUpdate(read_options, "foo", nullptr, false /* exclusive */);
  317. ASSERT_OK(s);
  318. auto lock_data = db->GetLockStatusData();
  319. ASSERT_EQ(lock_data.size(), 1);
  320. auto cf_iterator = lock_data.begin();
  321. ASSERT_EQ(cf_iterator->second.key, "foo");
  322. // We compare whether the set of txns locking this key is the same. To do
  323. // this, we need to sort both vectors so that the comparison is done
  324. // correctly.
  325. std::vector<TransactionID> expected_txns = {txn1->GetID(), txn2->GetID(),
  326. txn3->GetID()};
  327. std::vector<TransactionID> lock_txns = cf_iterator->second.ids;
  328. ASSERT_EQ(expected_txns, lock_txns);
  329. ASSERT_FALSE(cf_iterator->second.exclusive);
  330. txn1->Rollback();
  331. txn2->Rollback();
  332. txn3->Rollback();
  333. // Test txn1 and txn2 sharing a lock and txn3 trying to obtain it.
  334. s = txn1->GetForUpdate(read_options, "foo", nullptr, false /* exclusive */);
  335. ASSERT_OK(s);
  336. s = txn2->GetForUpdate(read_options, "foo", nullptr, false /* exclusive */);
  337. ASSERT_OK(s);
  338. s = txn3->GetForUpdate(read_options, "foo", nullptr);
  339. ASSERT_TRUE(s.IsTimedOut());
  340. ASSERT_EQ(s.ToString(), "Operation timed out: Timeout waiting to lock key");
  341. txn1->UndoGetForUpdate("foo");
  342. s = txn3->GetForUpdate(read_options, "foo", nullptr);
  343. ASSERT_TRUE(s.IsTimedOut());
  344. ASSERT_EQ(s.ToString(), "Operation timed out: Timeout waiting to lock key");
  345. txn2->UndoGetForUpdate("foo");
  346. s = txn3->GetForUpdate(read_options, "foo", nullptr);
  347. ASSERT_OK(s);
  348. txn1->Rollback();
  349. txn2->Rollback();
  350. txn3->Rollback();
  351. // Test txn1 and txn2 sharing a lock and txn2 trying to upgrade lock.
  352. s = txn1->GetForUpdate(read_options, "foo", nullptr, false /* exclusive */);
  353. ASSERT_OK(s);
  354. s = txn2->GetForUpdate(read_options, "foo", nullptr, false /* exclusive */);
  355. ASSERT_OK(s);
  356. s = txn2->GetForUpdate(read_options, "foo", nullptr);
  357. ASSERT_TRUE(s.IsTimedOut());
  358. ASSERT_EQ(s.ToString(), "Operation timed out: Timeout waiting to lock key");
  359. txn1->UndoGetForUpdate("foo");
  360. s = txn2->GetForUpdate(read_options, "foo", nullptr);
  361. ASSERT_OK(s);
  362. ASSERT_OK(txn1->Rollback());
  363. ASSERT_OK(txn2->Rollback());
  364. // Test txn1 trying to downgrade its lock.
  365. s = txn1->GetForUpdate(read_options, "foo", nullptr, true /* exclusive */);
  366. ASSERT_OK(s);
  367. s = txn2->GetForUpdate(read_options, "foo", nullptr, false /* exclusive */);
  368. ASSERT_TRUE(s.IsTimedOut());
  369. ASSERT_EQ(s.ToString(), "Operation timed out: Timeout waiting to lock key");
  370. // Should still fail after "downgrading".
  371. s = txn1->GetForUpdate(read_options, "foo", nullptr, false /* exclusive */);
  372. ASSERT_OK(s);
  373. s = txn2->GetForUpdate(read_options, "foo", nullptr, false /* exclusive */);
  374. ASSERT_TRUE(s.IsTimedOut());
  375. ASSERT_EQ(s.ToString(), "Operation timed out: Timeout waiting to lock key");
  376. txn1->Rollback();
  377. txn2->Rollback();
  378. // Test txn1 holding an exclusive lock and txn2 trying to obtain shared
  379. // access.
  380. s = txn1->GetForUpdate(read_options, "foo", nullptr);
  381. ASSERT_OK(s);
  382. s = txn2->GetForUpdate(read_options, "foo", nullptr, false /* exclusive */);
  383. ASSERT_TRUE(s.IsTimedOut());
  384. ASSERT_EQ(s.ToString(), "Operation timed out: Timeout waiting to lock key");
  385. txn1->UndoGetForUpdate("foo");
  386. s = txn2->GetForUpdate(read_options, "foo", nullptr, false /* exclusive */);
  387. ASSERT_OK(s);
  388. delete txn1;
  389. delete txn2;
  390. delete txn3;
  391. }
  392. TEST_P(TransactionTest, DeadlockCycleShared) {
  393. WriteOptions write_options;
  394. ReadOptions read_options;
  395. TransactionOptions txn_options;
  396. txn_options.lock_timeout = 1000000;
  397. txn_options.deadlock_detect = true;
  398. // Set up a wait for chain like this:
  399. //
  400. // Tn -> T(n*2)
  401. // Tn -> T(n*2 + 1)
  402. //
  403. // So we have:
  404. // T1 -> T2 -> T4 ...
  405. // | |> T5 ...
  406. // |> T3 -> T6 ...
  407. // |> T7 ...
  408. // up to T31, then T[16 - 31] -> T1.
  409. // Note that Tn holds lock on floor(n / 2).
  410. std::vector<Transaction*> txns(31);
  411. for (uint32_t i = 0; i < 31; i++) {
  412. txns[i] = db->BeginTransaction(write_options, txn_options);
  413. ASSERT_TRUE(txns[i]);
  414. auto s = txns[i]->GetForUpdate(read_options, ToString((i + 1) / 2), nullptr,
  415. false /* exclusive */);
  416. ASSERT_OK(s);
  417. }
  418. std::atomic<uint32_t> checkpoints(0);
  419. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
  420. "TransactionLockMgr::AcquireWithTimeout:WaitingTxn",
  421. [&](void* /*arg*/) { checkpoints.fetch_add(1); });
  422. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  423. // We want the leaf transactions to block and hold everyone back.
  424. std::vector<port::Thread> threads;
  425. for (uint32_t i = 0; i < 15; i++) {
  426. std::function<void()> blocking_thread = [&, i] {
  427. auto s = txns[i]->GetForUpdate(read_options, ToString(i + 1), nullptr,
  428. true /* exclusive */);
  429. ASSERT_OK(s);
  430. txns[i]->Rollback();
  431. delete txns[i];
  432. };
  433. threads.emplace_back(blocking_thread);
  434. }
  435. // Wait until all threads are waiting on each other.
  436. while (checkpoints.load() != 15) {
  437. /* sleep override */
  438. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  439. }
  440. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  441. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
  442. // Complete the cycle T[16 - 31] -> T1
  443. for (uint32_t i = 15; i < 31; i++) {
  444. auto s =
  445. txns[i]->GetForUpdate(read_options, "0", nullptr, true /* exclusive */);
  446. ASSERT_TRUE(s.IsDeadlock());
  447. // Calculate next buffer len, plateau at 5 when 5 records are inserted.
  448. const uint32_t curr_dlock_buffer_len_ =
  449. (i - 14 > kInitialMaxDeadlocks) ? kInitialMaxDeadlocks : (i - 14);
  450. auto dlock_buffer = db->GetDeadlockInfoBuffer();
  451. ASSERT_EQ(dlock_buffer.size(), curr_dlock_buffer_len_);
  452. auto dlock_entry = dlock_buffer[0].path;
  453. ASSERT_EQ(dlock_entry.size(), kInitialMaxDeadlocks);
  454. int64_t pre_deadlock_time = dlock_buffer[0].deadlock_time;
  455. int64_t cur_deadlock_time = 0;
  456. for (auto const& dl_path_rec : dlock_buffer) {
  457. cur_deadlock_time = dl_path_rec.deadlock_time;
  458. ASSERT_NE(cur_deadlock_time, 0);
  459. ASSERT_TRUE(cur_deadlock_time <= pre_deadlock_time);
  460. pre_deadlock_time = cur_deadlock_time;
  461. }
  462. int64_t curr_waiting_key = 0;
  463. // Offset of each txn id from the root of the shared dlock tree's txn id.
  464. int64_t offset_root = dlock_entry[0].m_txn_id - 1;
  465. // Offset of the final entry in the dlock path from the root's txn id.
  466. TransactionID leaf_id =
  467. dlock_entry[dlock_entry.size() - 1].m_txn_id - offset_root;
  468. for (auto it = dlock_entry.rbegin(); it != dlock_entry.rend(); ++it) {
  469. auto dl_node = *it;
  470. ASSERT_EQ(dl_node.m_txn_id, offset_root + leaf_id);
  471. ASSERT_EQ(dl_node.m_cf_id, 0U);
  472. ASSERT_EQ(dl_node.m_waiting_key, ToString(curr_waiting_key));
  473. ASSERT_EQ(dl_node.m_exclusive, true);
  474. if (curr_waiting_key == 0) {
  475. curr_waiting_key = leaf_id;
  476. }
  477. curr_waiting_key /= 2;
  478. leaf_id /= 2;
  479. }
  480. }
  481. // Rollback the leaf transaction.
  482. for (uint32_t i = 15; i < 31; i++) {
  483. txns[i]->Rollback();
  484. delete txns[i];
  485. }
  486. for (auto& t : threads) {
  487. t.join();
  488. }
  489. // Downsize the buffer and verify the 3 latest deadlocks are preserved.
  490. auto dlock_buffer_before_resize = db->GetDeadlockInfoBuffer();
  491. db->SetDeadlockInfoBufferSize(3);
  492. auto dlock_buffer_after_resize = db->GetDeadlockInfoBuffer();
  493. ASSERT_EQ(dlock_buffer_after_resize.size(), 3);
  494. for (uint32_t i = 0; i < dlock_buffer_after_resize.size(); i++) {
  495. for (uint32_t j = 0; j < dlock_buffer_after_resize[i].path.size(); j++) {
  496. ASSERT_EQ(dlock_buffer_after_resize[i].path[j].m_txn_id,
  497. dlock_buffer_before_resize[i].path[j].m_txn_id);
  498. }
  499. }
  500. // Upsize the buffer and verify the 3 latest dealocks are preserved.
  501. dlock_buffer_before_resize = db->GetDeadlockInfoBuffer();
  502. db->SetDeadlockInfoBufferSize(5);
  503. dlock_buffer_after_resize = db->GetDeadlockInfoBuffer();
  504. ASSERT_EQ(dlock_buffer_after_resize.size(), 3);
  505. for (uint32_t i = 0; i < dlock_buffer_before_resize.size(); i++) {
  506. for (uint32_t j = 0; j < dlock_buffer_before_resize[i].path.size(); j++) {
  507. ASSERT_EQ(dlock_buffer_after_resize[i].path[j].m_txn_id,
  508. dlock_buffer_before_resize[i].path[j].m_txn_id);
  509. }
  510. }
  511. // Downsize to 0 and verify the size is consistent.
  512. dlock_buffer_before_resize = db->GetDeadlockInfoBuffer();
  513. db->SetDeadlockInfoBufferSize(0);
  514. dlock_buffer_after_resize = db->GetDeadlockInfoBuffer();
  515. ASSERT_EQ(dlock_buffer_after_resize.size(), 0);
  516. // Upsize from 0 to verify the size is persistent.
  517. dlock_buffer_before_resize = db->GetDeadlockInfoBuffer();
  518. db->SetDeadlockInfoBufferSize(3);
  519. dlock_buffer_after_resize = db->GetDeadlockInfoBuffer();
  520. ASSERT_EQ(dlock_buffer_after_resize.size(), 0);
  521. // Contrived case of shared lock of cycle size 2 to verify that a shared
  522. // lock causing a deadlock is correctly reported as "shared" in the buffer.
  523. std::vector<Transaction*> txns_shared(2);
  524. // Create a cycle of size 2.
  525. for (uint32_t i = 0; i < 2; i++) {
  526. txns_shared[i] = db->BeginTransaction(write_options, txn_options);
  527. ASSERT_TRUE(txns_shared[i]);
  528. auto s = txns_shared[i]->GetForUpdate(read_options, ToString(i), nullptr);
  529. ASSERT_OK(s);
  530. }
  531. std::atomic<uint32_t> checkpoints_shared(0);
  532. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
  533. "TransactionLockMgr::AcquireWithTimeout:WaitingTxn",
  534. [&](void* /*arg*/) { checkpoints_shared.fetch_add(1); });
  535. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  536. std::vector<port::Thread> threads_shared;
  537. for (uint32_t i = 0; i < 1; i++) {
  538. std::function<void()> blocking_thread = [&, i] {
  539. auto s =
  540. txns_shared[i]->GetForUpdate(read_options, ToString(i + 1), nullptr);
  541. ASSERT_OK(s);
  542. txns_shared[i]->Rollback();
  543. delete txns_shared[i];
  544. };
  545. threads_shared.emplace_back(blocking_thread);
  546. }
  547. // Wait until all threads are waiting on each other.
  548. while (checkpoints_shared.load() != 1) {
  549. /* sleep override */
  550. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  551. }
  552. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  553. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
  554. // Complete the cycle T2 -> T1 with a shared lock.
  555. auto s = txns_shared[1]->GetForUpdate(read_options, "0", nullptr, false);
  556. ASSERT_TRUE(s.IsDeadlock());
  557. auto dlock_buffer = db->GetDeadlockInfoBuffer();
  558. // Verify the size of the buffer and the single path.
  559. ASSERT_EQ(dlock_buffer.size(), 1);
  560. ASSERT_EQ(dlock_buffer[0].path.size(), 2);
  561. // Verify the exclusivity field of the transactions in the deadlock path.
  562. ASSERT_TRUE(dlock_buffer[0].path[0].m_exclusive);
  563. ASSERT_FALSE(dlock_buffer[0].path[1].m_exclusive);
  564. txns_shared[1]->Rollback();
  565. delete txns_shared[1];
  566. for (auto& t : threads_shared) {
  567. t.join();
  568. }
  569. }
  570. #ifndef ROCKSDB_VALGRIND_RUN
  571. TEST_P(TransactionStressTest, DeadlockCycle) {
  572. WriteOptions write_options;
  573. ReadOptions read_options;
  574. TransactionOptions txn_options;
  575. // offset by 2 from the max depth to test edge case
  576. const uint32_t kMaxCycleLength = 52;
  577. txn_options.lock_timeout = 1000000;
  578. txn_options.deadlock_detect = true;
  579. for (uint32_t len = 2; len < kMaxCycleLength; len++) {
  580. // Set up a long wait for chain like this:
  581. //
  582. // T1 -> T2 -> T3 -> ... -> Tlen
  583. std::vector<Transaction*> txns(len);
  584. for (uint32_t i = 0; i < len; i++) {
  585. txns[i] = db->BeginTransaction(write_options, txn_options);
  586. ASSERT_TRUE(txns[i]);
  587. auto s = txns[i]->GetForUpdate(read_options, ToString(i), nullptr);
  588. ASSERT_OK(s);
  589. }
  590. std::atomic<uint32_t> checkpoints(0);
  591. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
  592. "TransactionLockMgr::AcquireWithTimeout:WaitingTxn",
  593. [&](void* /*arg*/) { checkpoints.fetch_add(1); });
  594. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  595. // We want the last transaction in the chain to block and hold everyone
  596. // back.
  597. std::vector<port::Thread> threads;
  598. for (uint32_t i = 0; i < len - 1; i++) {
  599. std::function<void()> blocking_thread = [&, i] {
  600. auto s = txns[i]->GetForUpdate(read_options, ToString(i + 1), nullptr);
  601. ASSERT_OK(s);
  602. txns[i]->Rollback();
  603. delete txns[i];
  604. };
  605. threads.emplace_back(blocking_thread);
  606. }
  607. // Wait until all threads are waiting on each other.
  608. while (checkpoints.load() != len - 1) {
  609. /* sleep override */
  610. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  611. }
  612. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  613. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
  614. // Complete the cycle Tlen -> T1
  615. auto s = txns[len - 1]->GetForUpdate(read_options, "0", nullptr);
  616. ASSERT_TRUE(s.IsDeadlock());
  617. const uint32_t dlock_buffer_size_ = (len - 1 > 5) ? 5 : (len - 1);
  618. uint32_t curr_waiting_key = 0;
  619. TransactionID curr_txn_id = txns[0]->GetID();
  620. auto dlock_buffer = db->GetDeadlockInfoBuffer();
  621. ASSERT_EQ(dlock_buffer.size(), dlock_buffer_size_);
  622. uint32_t check_len = len;
  623. bool check_limit_flag = false;
  624. // Special case for a deadlock path that exceeds the maximum depth.
  625. if (len > 50) {
  626. check_len = 0;
  627. check_limit_flag = true;
  628. }
  629. auto dlock_entry = dlock_buffer[0].path;
  630. ASSERT_EQ(dlock_entry.size(), check_len);
  631. ASSERT_EQ(dlock_buffer[0].limit_exceeded, check_limit_flag);
  632. int64_t pre_deadlock_time = dlock_buffer[0].deadlock_time;
  633. int64_t cur_deadlock_time = 0;
  634. for (auto const& dl_path_rec : dlock_buffer) {
  635. cur_deadlock_time = dl_path_rec.deadlock_time;
  636. ASSERT_NE(cur_deadlock_time, 0);
  637. ASSERT_TRUE(cur_deadlock_time <= pre_deadlock_time);
  638. pre_deadlock_time = cur_deadlock_time;
  639. }
  640. // Iterates backwards over path verifying decreasing txn_ids.
  641. for (auto it = dlock_entry.rbegin(); it != dlock_entry.rend(); ++it) {
  642. auto dl_node = *it;
  643. ASSERT_EQ(dl_node.m_txn_id, len + curr_txn_id - 1);
  644. ASSERT_EQ(dl_node.m_cf_id, 0u);
  645. ASSERT_EQ(dl_node.m_waiting_key, ToString(curr_waiting_key));
  646. ASSERT_EQ(dl_node.m_exclusive, true);
  647. curr_txn_id--;
  648. if (curr_waiting_key == 0) {
  649. curr_waiting_key = len;
  650. }
  651. curr_waiting_key--;
  652. }
  653. // Rollback the last transaction.
  654. txns[len - 1]->Rollback();
  655. delete txns[len - 1];
  656. for (auto& t : threads) {
  657. t.join();
  658. }
  659. }
  660. }
  661. TEST_P(TransactionStressTest, DeadlockStress) {
  662. const uint32_t NUM_TXN_THREADS = 10;
  663. const uint32_t NUM_KEYS = 100;
  664. const uint32_t NUM_ITERS = 10000;
  665. WriteOptions write_options;
  666. ReadOptions read_options;
  667. TransactionOptions txn_options;
  668. txn_options.lock_timeout = 1000000;
  669. txn_options.deadlock_detect = true;
  670. std::vector<std::string> keys;
  671. for (uint32_t i = 0; i < NUM_KEYS; i++) {
  672. db->Put(write_options, Slice(ToString(i)), Slice(""));
  673. keys.push_back(ToString(i));
  674. }
  675. size_t tid = std::hash<std::thread::id>()(std::this_thread::get_id());
  676. Random rnd(static_cast<uint32_t>(tid));
  677. std::function<void(uint32_t)> stress_thread = [&](uint32_t seed) {
  678. std::default_random_engine g(seed);
  679. Transaction* txn;
  680. for (uint32_t i = 0; i < NUM_ITERS; i++) {
  681. txn = db->BeginTransaction(write_options, txn_options);
  682. auto random_keys = keys;
  683. std::shuffle(random_keys.begin(), random_keys.end(), g);
  684. // Lock keys in random order.
  685. for (const auto& k : random_keys) {
  686. // Lock mostly for shared access, but exclusive 1/4 of the time.
  687. auto s =
  688. txn->GetForUpdate(read_options, k, nullptr, txn->GetID() % 4 == 0);
  689. if (!s.ok()) {
  690. ASSERT_TRUE(s.IsDeadlock());
  691. txn->Rollback();
  692. break;
  693. }
  694. }
  695. delete txn;
  696. }
  697. };
  698. std::vector<port::Thread> threads;
  699. for (uint32_t i = 0; i < NUM_TXN_THREADS; i++) {
  700. threads.emplace_back(stress_thread, rnd.Next());
  701. }
  702. for (auto& t : threads) {
  703. t.join();
  704. }
  705. }
  706. #endif // ROCKSDB_VALGRIND_RUN
  707. TEST_P(TransactionTest, CommitTimeBatchFailTest) {
  708. WriteOptions write_options;
  709. TransactionOptions txn_options;
  710. std::string value;
  711. Status s;
  712. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  713. ASSERT_TRUE(txn1);
  714. ASSERT_OK(txn1->GetCommitTimeWriteBatch()->Put("cat", "dog"));
  715. s = txn1->Put("foo", "bar");
  716. ASSERT_OK(s);
  717. // fails due to non-empty commit-time batch
  718. s = txn1->Commit();
  719. ASSERT_EQ(s, Status::InvalidArgument());
  720. delete txn1;
  721. }
  722. TEST_P(TransactionTest, LogMarkLeakTest) {
  723. TransactionOptions txn_options;
  724. WriteOptions write_options;
  725. options.write_buffer_size = 1024;
  726. ASSERT_OK(ReOpenNoDelete());
  727. assert(db != nullptr);
  728. Random rnd(47);
  729. std::vector<Transaction*> txns;
  730. DBImpl* db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  731. // At the beginning there should be no log containing prepare data
  732. ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(), 0);
  733. for (size_t i = 0; i < 100; i++) {
  734. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  735. ASSERT_OK(txn->SetName("xid" + ToString(i)));
  736. ASSERT_OK(txn->Put(Slice("foo" + ToString(i)), Slice("bar")));
  737. ASSERT_OK(txn->Prepare());
  738. ASSERT_GT(db_impl->TEST_FindMinLogContainingOutstandingPrep(), 0);
  739. if (rnd.OneIn(5)) {
  740. txns.push_back(txn);
  741. } else {
  742. ASSERT_OK(txn->Commit());
  743. delete txn;
  744. }
  745. db_impl->TEST_FlushMemTable(true);
  746. }
  747. for (auto txn : txns) {
  748. ASSERT_OK(txn->Commit());
  749. delete txn;
  750. }
  751. // At the end there should be no log left containing prepare data
  752. ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(), 0);
  753. // Make sure that the underlying data structures are properly truncated and
  754. // cause not leak
  755. ASSERT_EQ(db_impl->TEST_PreparedSectionCompletedSize(), 0);
  756. ASSERT_EQ(db_impl->TEST_LogsWithPrepSize(), 0);
  757. }
  758. TEST_P(TransactionTest, SimpleTwoPhaseTransactionTest) {
  759. for (bool cwb4recovery : {true, false}) {
  760. ASSERT_OK(ReOpen());
  761. WriteOptions write_options;
  762. ReadOptions read_options;
  763. TransactionOptions txn_options;
  764. txn_options.use_only_the_last_commit_time_batch_for_recovery = cwb4recovery;
  765. string value;
  766. Status s;
  767. DBImpl* db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  768. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  769. s = txn->SetName("xid");
  770. ASSERT_OK(s);
  771. ASSERT_EQ(db->GetTransactionByName("xid"), txn);
  772. // transaction put
  773. s = txn->Put(Slice("foo"), Slice("bar"));
  774. ASSERT_OK(s);
  775. ASSERT_EQ(1, txn->GetNumPuts());
  776. // regular db put
  777. s = db->Put(write_options, Slice("foo2"), Slice("bar2"));
  778. ASSERT_OK(s);
  779. ASSERT_EQ(1, txn->GetNumPuts());
  780. // regular db read
  781. db->Get(read_options, "foo2", &value);
  782. ASSERT_EQ(value, "bar2");
  783. // commit time put
  784. txn->GetCommitTimeWriteBatch()->Put(Slice("gtid"), Slice("dogs"));
  785. txn->GetCommitTimeWriteBatch()->Put(Slice("gtid2"), Slice("cats"));
  786. // nothing has been prepped yet
  787. ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(), 0);
  788. s = txn->Prepare();
  789. ASSERT_OK(s);
  790. // data not im mem yet
  791. s = db->Get(read_options, Slice("foo"), &value);
  792. ASSERT_TRUE(s.IsNotFound());
  793. s = db->Get(read_options, Slice("gtid"), &value);
  794. ASSERT_TRUE(s.IsNotFound());
  795. // find trans in list of prepared transactions
  796. std::vector<Transaction*> prepared_trans;
  797. db->GetAllPreparedTransactions(&prepared_trans);
  798. ASSERT_EQ(prepared_trans.size(), 1);
  799. ASSERT_EQ(prepared_trans.front()->GetName(), "xid");
  800. auto log_containing_prep =
  801. db_impl->TEST_FindMinLogContainingOutstandingPrep();
  802. ASSERT_GT(log_containing_prep, 0);
  803. // make commit
  804. s = txn->Commit();
  805. ASSERT_OK(s);
  806. // value is now available
  807. s = db->Get(read_options, "foo", &value);
  808. ASSERT_OK(s);
  809. ASSERT_EQ(value, "bar");
  810. if (!cwb4recovery) {
  811. s = db->Get(read_options, "gtid", &value);
  812. ASSERT_OK(s);
  813. ASSERT_EQ(value, "dogs");
  814. s = db->Get(read_options, "gtid2", &value);
  815. ASSERT_OK(s);
  816. ASSERT_EQ(value, "cats");
  817. }
  818. // we already committed
  819. s = txn->Commit();
  820. ASSERT_EQ(s, Status::InvalidArgument());
  821. // no longer is prepared results
  822. db->GetAllPreparedTransactions(&prepared_trans);
  823. ASSERT_EQ(prepared_trans.size(), 0);
  824. ASSERT_EQ(db->GetTransactionByName("xid"), nullptr);
  825. // heap should not care about prepared section anymore
  826. ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(), 0);
  827. switch (txn_db_options.write_policy) {
  828. case WRITE_COMMITTED:
  829. // but now our memtable should be referencing the prep section
  830. ASSERT_GE(log_containing_prep, db_impl->MinLogNumberToKeep());
  831. ASSERT_EQ(log_containing_prep,
  832. db_impl->TEST_FindMinPrepLogReferencedByMemTable());
  833. break;
  834. case WRITE_PREPARED:
  835. case WRITE_UNPREPARED:
  836. // In these modes memtable do not ref the prep sections
  837. ASSERT_EQ(0, db_impl->TEST_FindMinPrepLogReferencedByMemTable());
  838. break;
  839. default:
  840. assert(false);
  841. }
  842. db_impl->TEST_FlushMemTable(true);
  843. // After flush the recoverable state must be visible
  844. if (cwb4recovery) {
  845. s = db->Get(read_options, "gtid", &value);
  846. ASSERT_OK(s);
  847. ASSERT_EQ(value, "dogs");
  848. s = db->Get(read_options, "gtid2", &value);
  849. ASSERT_OK(s);
  850. ASSERT_EQ(value, "cats");
  851. }
  852. // after memtable flush we can now relese the log
  853. ASSERT_GT(db_impl->MinLogNumberToKeep(), log_containing_prep);
  854. ASSERT_EQ(0, db_impl->TEST_FindMinPrepLogReferencedByMemTable());
  855. delete txn;
  856. if (cwb4recovery) {
  857. // kill and reopen to trigger recovery
  858. s = ReOpenNoDelete();
  859. ASSERT_OK(s);
  860. assert(db != nullptr);
  861. s = db->Get(read_options, "gtid", &value);
  862. ASSERT_OK(s);
  863. ASSERT_EQ(value, "dogs");
  864. s = db->Get(read_options, "gtid2", &value);
  865. ASSERT_OK(s);
  866. ASSERT_EQ(value, "cats");
  867. }
  868. }
  869. }
  870. TEST_P(TransactionTest, TwoPhaseNameTest) {
  871. Status s;
  872. WriteOptions write_options;
  873. TransactionOptions txn_options;
  874. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  875. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  876. Transaction* txn3 = db->BeginTransaction(write_options, txn_options);
  877. ASSERT_TRUE(txn3);
  878. delete txn3;
  879. // cant prepare txn without name
  880. s = txn1->Prepare();
  881. ASSERT_EQ(s, Status::InvalidArgument());
  882. // name too short
  883. s = txn1->SetName("");
  884. ASSERT_EQ(s, Status::InvalidArgument());
  885. // name too long
  886. s = txn1->SetName(std::string(513, 'x'));
  887. ASSERT_EQ(s, Status::InvalidArgument());
  888. // valid set name
  889. s = txn1->SetName("name1");
  890. ASSERT_OK(s);
  891. // cant have duplicate name
  892. s = txn2->SetName("name1");
  893. ASSERT_EQ(s, Status::InvalidArgument());
  894. // shouldn't be able to prepare
  895. s = txn2->Prepare();
  896. ASSERT_EQ(s, Status::InvalidArgument());
  897. // valid name set
  898. s = txn2->SetName("name2");
  899. ASSERT_OK(s);
  900. // cant reset name
  901. s = txn2->SetName("name3");
  902. ASSERT_EQ(s, Status::InvalidArgument());
  903. ASSERT_EQ(txn1->GetName(), "name1");
  904. ASSERT_EQ(txn2->GetName(), "name2");
  905. s = txn1->Prepare();
  906. ASSERT_OK(s);
  907. // can't rename after prepare
  908. s = txn1->SetName("name4");
  909. ASSERT_EQ(s, Status::InvalidArgument());
  910. txn1->Rollback();
  911. txn2->Rollback();
  912. delete txn1;
  913. delete txn2;
  914. }
  915. TEST_P(TransactionTest, TwoPhaseEmptyWriteTest) {
  916. for (bool cwb4recovery : {true, false}) {
  917. for (bool test_with_empty_wal : {true, false}) {
  918. if (!cwb4recovery && test_with_empty_wal) {
  919. continue;
  920. }
  921. ASSERT_OK(ReOpen());
  922. Status s;
  923. std::string value;
  924. WriteOptions write_options;
  925. ReadOptions read_options;
  926. TransactionOptions txn_options;
  927. txn_options.use_only_the_last_commit_time_batch_for_recovery =
  928. cwb4recovery;
  929. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  930. ASSERT_TRUE(txn1);
  931. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  932. ASSERT_TRUE(txn2);
  933. s = txn1->SetName("joe");
  934. ASSERT_OK(s);
  935. s = txn2->SetName("bob");
  936. ASSERT_OK(s);
  937. s = txn1->Prepare();
  938. ASSERT_OK(s);
  939. s = txn1->Commit();
  940. ASSERT_OK(s);
  941. delete txn1;
  942. txn2->GetCommitTimeWriteBatch()->Put(Slice("foo"), Slice("bar"));
  943. s = txn2->Prepare();
  944. ASSERT_OK(s);
  945. s = txn2->Commit();
  946. ASSERT_OK(s);
  947. delete txn2;
  948. if (!cwb4recovery) {
  949. s = db->Get(read_options, "foo", &value);
  950. ASSERT_OK(s);
  951. ASSERT_EQ(value, "bar");
  952. } else {
  953. if (test_with_empty_wal) {
  954. DBImpl* db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  955. db_impl->TEST_FlushMemTable(true);
  956. // After flush the state must be visible
  957. s = db->Get(read_options, "foo", &value);
  958. ASSERT_OK(s);
  959. ASSERT_EQ(value, "bar");
  960. }
  961. db->FlushWAL(true);
  962. // kill and reopen to trigger recovery
  963. s = ReOpenNoDelete();
  964. ASSERT_OK(s);
  965. assert(db != nullptr);
  966. s = db->Get(read_options, "foo", &value);
  967. ASSERT_OK(s);
  968. ASSERT_EQ(value, "bar");
  969. }
  970. }
  971. }
  972. }
  973. #ifndef ROCKSDB_VALGRIND_RUN
  974. TEST_P(TransactionStressTest, TwoPhaseExpirationTest) {
  975. Status s;
  976. WriteOptions write_options;
  977. TransactionOptions txn_options;
  978. txn_options.expiration = 500; // 500ms
  979. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  980. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  981. ASSERT_TRUE(txn1);
  982. ASSERT_TRUE(txn1);
  983. s = txn1->SetName("joe");
  984. ASSERT_OK(s);
  985. s = txn2->SetName("bob");
  986. ASSERT_OK(s);
  987. s = txn1->Prepare();
  988. ASSERT_OK(s);
  989. /* sleep override */
  990. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  991. s = txn1->Commit();
  992. ASSERT_OK(s);
  993. s = txn2->Prepare();
  994. ASSERT_EQ(s, Status::Expired());
  995. delete txn1;
  996. delete txn2;
  997. }
  998. TEST_P(TransactionTest, TwoPhaseRollbackTest) {
  999. WriteOptions write_options;
  1000. ReadOptions read_options;
  1001. TransactionOptions txn_options;
  1002. std::string value;
  1003. Status s;
  1004. DBImpl* db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  1005. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  1006. s = txn->SetName("xid");
  1007. ASSERT_OK(s);
  1008. // transaction put
  1009. s = txn->Put(Slice("tfoo"), Slice("tbar"));
  1010. ASSERT_OK(s);
  1011. // value is readable form txn
  1012. s = txn->Get(read_options, Slice("tfoo"), &value);
  1013. ASSERT_OK(s);
  1014. ASSERT_EQ(value, "tbar");
  1015. // issue rollback
  1016. s = txn->Rollback();
  1017. ASSERT_OK(s);
  1018. // value is nolonger readable
  1019. s = txn->Get(read_options, Slice("tfoo"), &value);
  1020. ASSERT_TRUE(s.IsNotFound());
  1021. ASSERT_EQ(txn->GetNumPuts(), 0);
  1022. // put new txn values
  1023. s = txn->Put(Slice("tfoo2"), Slice("tbar2"));
  1024. ASSERT_OK(s);
  1025. // new value is readable from txn
  1026. s = txn->Get(read_options, Slice("tfoo2"), &value);
  1027. ASSERT_OK(s);
  1028. ASSERT_EQ(value, "tbar2");
  1029. s = txn->Prepare();
  1030. ASSERT_OK(s);
  1031. // flush to next wal
  1032. s = db->Put(write_options, Slice("foo"), Slice("bar"));
  1033. ASSERT_OK(s);
  1034. db_impl->TEST_FlushMemTable(true);
  1035. // issue rollback (marker written to WAL)
  1036. s = txn->Rollback();
  1037. ASSERT_OK(s);
  1038. // value is nolonger readable
  1039. s = txn->Get(read_options, Slice("tfoo2"), &value);
  1040. ASSERT_TRUE(s.IsNotFound());
  1041. ASSERT_EQ(txn->GetNumPuts(), 0);
  1042. // make commit
  1043. s = txn->Commit();
  1044. ASSERT_EQ(s, Status::InvalidArgument());
  1045. // try rollback again
  1046. s = txn->Rollback();
  1047. ASSERT_EQ(s, Status::InvalidArgument());
  1048. delete txn;
  1049. }
  1050. TEST_P(TransactionTest, PersistentTwoPhaseTransactionTest) {
  1051. WriteOptions write_options;
  1052. write_options.sync = true;
  1053. write_options.disableWAL = false;
  1054. ReadOptions read_options;
  1055. TransactionOptions txn_options;
  1056. std::string value;
  1057. Status s;
  1058. DBImpl* db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  1059. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  1060. s = txn->SetName("xid");
  1061. ASSERT_OK(s);
  1062. ASSERT_EQ(db->GetTransactionByName("xid"), txn);
  1063. // transaction put
  1064. s = txn->Put(Slice("foo"), Slice("bar"));
  1065. ASSERT_OK(s);
  1066. ASSERT_EQ(1, txn->GetNumPuts());
  1067. // txn read
  1068. s = txn->Get(read_options, "foo", &value);
  1069. ASSERT_OK(s);
  1070. ASSERT_EQ(value, "bar");
  1071. // regular db put
  1072. s = db->Put(write_options, Slice("foo2"), Slice("bar2"));
  1073. ASSERT_OK(s);
  1074. ASSERT_EQ(1, txn->GetNumPuts());
  1075. db_impl->TEST_FlushMemTable(true);
  1076. // regular db read
  1077. db->Get(read_options, "foo2", &value);
  1078. ASSERT_EQ(value, "bar2");
  1079. // nothing has been prepped yet
  1080. ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(), 0);
  1081. // prepare
  1082. s = txn->Prepare();
  1083. ASSERT_OK(s);
  1084. // still not available to db
  1085. s = db->Get(read_options, Slice("foo"), &value);
  1086. ASSERT_TRUE(s.IsNotFound());
  1087. db->FlushWAL(false);
  1088. delete txn;
  1089. // kill and reopen
  1090. reinterpret_cast<PessimisticTransactionDB*>(db)->TEST_Crash();
  1091. s = ReOpenNoDelete();
  1092. ASSERT_OK(s);
  1093. assert(db != nullptr);
  1094. db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  1095. // find trans in list of prepared transactions
  1096. std::vector<Transaction*> prepared_trans;
  1097. db->GetAllPreparedTransactions(&prepared_trans);
  1098. ASSERT_EQ(prepared_trans.size(), 1);
  1099. txn = prepared_trans.front();
  1100. ASSERT_TRUE(txn);
  1101. ASSERT_EQ(txn->GetName(), "xid");
  1102. ASSERT_EQ(db->GetTransactionByName("xid"), txn);
  1103. // log has been marked
  1104. auto log_containing_prep =
  1105. db_impl->TEST_FindMinLogContainingOutstandingPrep();
  1106. ASSERT_GT(log_containing_prep, 0);
  1107. // value is readable from txn
  1108. s = txn->Get(read_options, "foo", &value);
  1109. ASSERT_OK(s);
  1110. ASSERT_EQ(value, "bar");
  1111. // make commit
  1112. s = txn->Commit();
  1113. ASSERT_OK(s);
  1114. // value is now available
  1115. db->Get(read_options, "foo", &value);
  1116. ASSERT_EQ(value, "bar");
  1117. // we already committed
  1118. s = txn->Commit();
  1119. ASSERT_EQ(s, Status::InvalidArgument());
  1120. // no longer is prepared results
  1121. prepared_trans.clear();
  1122. db->GetAllPreparedTransactions(&prepared_trans);
  1123. ASSERT_EQ(prepared_trans.size(), 0);
  1124. // transaction should no longer be visible
  1125. ASSERT_EQ(db->GetTransactionByName("xid"), nullptr);
  1126. // heap should not care about prepared section anymore
  1127. ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(), 0);
  1128. switch (txn_db_options.write_policy) {
  1129. case WRITE_COMMITTED:
  1130. // but now our memtable should be referencing the prep section
  1131. ASSERT_EQ(log_containing_prep,
  1132. db_impl->TEST_FindMinPrepLogReferencedByMemTable());
  1133. ASSERT_GE(log_containing_prep, db_impl->MinLogNumberToKeep());
  1134. break;
  1135. case WRITE_PREPARED:
  1136. case WRITE_UNPREPARED:
  1137. // In these modes memtable do not ref the prep sections
  1138. ASSERT_EQ(0, db_impl->TEST_FindMinPrepLogReferencedByMemTable());
  1139. break;
  1140. default:
  1141. assert(false);
  1142. }
  1143. // Add a dummy record to memtable before a flush. Otherwise, the
  1144. // memtable will be empty and flush will be skipped.
  1145. s = db->Put(write_options, Slice("foo3"), Slice("bar3"));
  1146. ASSERT_OK(s);
  1147. db_impl->TEST_FlushMemTable(true);
  1148. // after memtable flush we can now release the log
  1149. ASSERT_GT(db_impl->MinLogNumberToKeep(), log_containing_prep);
  1150. ASSERT_EQ(0, db_impl->TEST_FindMinPrepLogReferencedByMemTable());
  1151. delete txn;
  1152. // deleting transaction should unregister transaction
  1153. ASSERT_EQ(db->GetTransactionByName("xid"), nullptr);
  1154. }
  1155. #endif // ROCKSDB_VALGRIND_RUN
  1156. // TODO this test needs to be updated with serial commits
  1157. TEST_P(TransactionTest, DISABLED_TwoPhaseMultiThreadTest) {
  1158. // mix transaction writes and regular writes
  1159. const uint32_t NUM_TXN_THREADS = 50;
  1160. std::atomic<uint32_t> txn_thread_num(0);
  1161. std::function<void()> txn_write_thread = [&]() {
  1162. uint32_t id = txn_thread_num.fetch_add(1);
  1163. WriteOptions write_options;
  1164. write_options.sync = true;
  1165. write_options.disableWAL = false;
  1166. TransactionOptions txn_options;
  1167. txn_options.lock_timeout = 1000000;
  1168. if (id % 2 == 0) {
  1169. txn_options.expiration = 1000000;
  1170. }
  1171. TransactionName name("xid_" + std::string(1, 'A' + static_cast<char>(id)));
  1172. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  1173. ASSERT_OK(txn->SetName(name));
  1174. for (int i = 0; i < 10; i++) {
  1175. std::string key(name + "_" + std::string(1, static_cast<char>('A' + i)));
  1176. ASSERT_OK(txn->Put(key, "val"));
  1177. }
  1178. ASSERT_OK(txn->Prepare());
  1179. ASSERT_OK(txn->Commit());
  1180. delete txn;
  1181. };
  1182. // assure that all thread are in the same write group
  1183. std::atomic<uint32_t> t_wait_on_prepare(0);
  1184. std::atomic<uint32_t> t_wait_on_commit(0);
  1185. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
  1186. "WriteThread::JoinBatchGroup:Wait", [&](void* arg) {
  1187. auto* writer = reinterpret_cast<WriteThread::Writer*>(arg);
  1188. if (writer->ShouldWriteToWAL()) {
  1189. t_wait_on_prepare.fetch_add(1);
  1190. // wait for friends
  1191. while (t_wait_on_prepare.load() < NUM_TXN_THREADS) {
  1192. env->SleepForMicroseconds(10);
  1193. }
  1194. } else if (writer->ShouldWriteToMemtable()) {
  1195. t_wait_on_commit.fetch_add(1);
  1196. // wait for friends
  1197. while (t_wait_on_commit.load() < NUM_TXN_THREADS) {
  1198. env->SleepForMicroseconds(10);
  1199. }
  1200. } else {
  1201. FAIL();
  1202. }
  1203. });
  1204. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  1205. // do all the writes
  1206. std::vector<port::Thread> threads;
  1207. for (uint32_t i = 0; i < NUM_TXN_THREADS; i++) {
  1208. threads.emplace_back(txn_write_thread);
  1209. }
  1210. for (auto& t : threads) {
  1211. t.join();
  1212. }
  1213. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  1214. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
  1215. ReadOptions read_options;
  1216. std::string value;
  1217. Status s;
  1218. for (uint32_t t = 0; t < NUM_TXN_THREADS; t++) {
  1219. TransactionName name("xid_" + std::string(1, 'A' + static_cast<char>(t)));
  1220. for (int i = 0; i < 10; i++) {
  1221. std::string key(name + "_" + std::string(1, static_cast<char>('A' + i)));
  1222. s = db->Get(read_options, key, &value);
  1223. ASSERT_OK(s);
  1224. ASSERT_EQ(value, "val");
  1225. }
  1226. }
  1227. }
  1228. TEST_P(TransactionStressTest, TwoPhaseLongPrepareTest) {
  1229. WriteOptions write_options;
  1230. write_options.sync = true;
  1231. write_options.disableWAL = false;
  1232. ReadOptions read_options;
  1233. TransactionOptions txn_options;
  1234. std::string value;
  1235. Status s;
  1236. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  1237. s = txn->SetName("bob");
  1238. ASSERT_OK(s);
  1239. // transaction put
  1240. s = txn->Put(Slice("foo"), Slice("bar"));
  1241. ASSERT_OK(s);
  1242. // prepare
  1243. s = txn->Prepare();
  1244. ASSERT_OK(s);
  1245. delete txn;
  1246. for (int i = 0; i < 1000; i++) {
  1247. std::string key(i, 'k');
  1248. std::string val(1000, 'v');
  1249. assert(db != nullptr);
  1250. s = db->Put(write_options, key, val);
  1251. ASSERT_OK(s);
  1252. if (i % 29 == 0) {
  1253. // crash
  1254. env->SetFilesystemActive(false);
  1255. reinterpret_cast<PessimisticTransactionDB*>(db)->TEST_Crash();
  1256. ReOpenNoDelete();
  1257. } else if (i % 37 == 0) {
  1258. // close
  1259. ReOpenNoDelete();
  1260. }
  1261. }
  1262. // commit old txn
  1263. txn = db->GetTransactionByName("bob");
  1264. ASSERT_TRUE(txn);
  1265. s = txn->Commit();
  1266. ASSERT_OK(s);
  1267. // verify data txn data
  1268. s = db->Get(read_options, "foo", &value);
  1269. ASSERT_EQ(s, Status::OK());
  1270. ASSERT_EQ(value, "bar");
  1271. // verify non txn data
  1272. for (int i = 0; i < 1000; i++) {
  1273. std::string key(i, 'k');
  1274. std::string val(1000, 'v');
  1275. s = db->Get(read_options, key, &value);
  1276. ASSERT_EQ(s, Status::OK());
  1277. ASSERT_EQ(value, val);
  1278. }
  1279. delete txn;
  1280. }
  1281. TEST_P(TransactionTest, TwoPhaseSequenceTest) {
  1282. WriteOptions write_options;
  1283. write_options.sync = true;
  1284. write_options.disableWAL = false;
  1285. ReadOptions read_options;
  1286. TransactionOptions txn_options;
  1287. std::string value;
  1288. Status s;
  1289. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  1290. s = txn->SetName("xid");
  1291. ASSERT_OK(s);
  1292. // transaction put
  1293. s = txn->Put(Slice("foo"), Slice("bar"));
  1294. ASSERT_OK(s);
  1295. s = txn->Put(Slice("foo2"), Slice("bar2"));
  1296. ASSERT_OK(s);
  1297. s = txn->Put(Slice("foo3"), Slice("bar3"));
  1298. ASSERT_OK(s);
  1299. s = txn->Put(Slice("foo4"), Slice("bar4"));
  1300. ASSERT_OK(s);
  1301. // prepare
  1302. s = txn->Prepare();
  1303. ASSERT_OK(s);
  1304. // make commit
  1305. s = txn->Commit();
  1306. ASSERT_OK(s);
  1307. delete txn;
  1308. // kill and reopen
  1309. env->SetFilesystemActive(false);
  1310. ReOpenNoDelete();
  1311. assert(db != nullptr);
  1312. // value is now available
  1313. s = db->Get(read_options, "foo4", &value);
  1314. ASSERT_EQ(s, Status::OK());
  1315. ASSERT_EQ(value, "bar4");
  1316. }
  1317. TEST_P(TransactionTest, TwoPhaseDoubleRecoveryTest) {
  1318. WriteOptions write_options;
  1319. write_options.sync = true;
  1320. write_options.disableWAL = false;
  1321. ReadOptions read_options;
  1322. TransactionOptions txn_options;
  1323. std::string value;
  1324. Status s;
  1325. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  1326. s = txn->SetName("a");
  1327. ASSERT_OK(s);
  1328. // transaction put
  1329. s = txn->Put(Slice("foo"), Slice("bar"));
  1330. ASSERT_OK(s);
  1331. // prepare
  1332. s = txn->Prepare();
  1333. ASSERT_OK(s);
  1334. delete txn;
  1335. // kill and reopen
  1336. env->SetFilesystemActive(false);
  1337. reinterpret_cast<PessimisticTransactionDB*>(db)->TEST_Crash();
  1338. ReOpenNoDelete();
  1339. // commit old txn
  1340. txn = db->GetTransactionByName("a");
  1341. s = txn->Commit();
  1342. ASSERT_OK(s);
  1343. s = db->Get(read_options, "foo", &value);
  1344. ASSERT_EQ(s, Status::OK());
  1345. ASSERT_EQ(value, "bar");
  1346. delete txn;
  1347. txn = db->BeginTransaction(write_options, txn_options);
  1348. s = txn->SetName("b");
  1349. ASSERT_OK(s);
  1350. s = txn->Put(Slice("foo2"), Slice("bar2"));
  1351. ASSERT_OK(s);
  1352. s = txn->Prepare();
  1353. ASSERT_OK(s);
  1354. s = txn->Commit();
  1355. ASSERT_OK(s);
  1356. delete txn;
  1357. // kill and reopen
  1358. env->SetFilesystemActive(false);
  1359. ReOpenNoDelete();
  1360. assert(db != nullptr);
  1361. // value is now available
  1362. s = db->Get(read_options, "foo", &value);
  1363. ASSERT_EQ(s, Status::OK());
  1364. ASSERT_EQ(value, "bar");
  1365. s = db->Get(read_options, "foo2", &value);
  1366. ASSERT_EQ(s, Status::OK());
  1367. ASSERT_EQ(value, "bar2");
  1368. }
  1369. TEST_P(TransactionTest, TwoPhaseLogRollingTest) {
  1370. DBImpl* db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  1371. Status s;
  1372. std::string v;
  1373. ColumnFamilyHandle *cfa, *cfb;
  1374. // Create 2 new column families
  1375. ColumnFamilyOptions cf_options;
  1376. s = db->CreateColumnFamily(cf_options, "CFA", &cfa);
  1377. ASSERT_OK(s);
  1378. s = db->CreateColumnFamily(cf_options, "CFB", &cfb);
  1379. ASSERT_OK(s);
  1380. WriteOptions wopts;
  1381. wopts.disableWAL = false;
  1382. wopts.sync = true;
  1383. TransactionOptions topts1;
  1384. Transaction* txn1 = db->BeginTransaction(wopts, topts1);
  1385. s = txn1->SetName("xid1");
  1386. ASSERT_OK(s);
  1387. TransactionOptions topts2;
  1388. Transaction* txn2 = db->BeginTransaction(wopts, topts2);
  1389. s = txn2->SetName("xid2");
  1390. ASSERT_OK(s);
  1391. // transaction put in two column families
  1392. s = txn1->Put(cfa, "ka1", "va1");
  1393. ASSERT_OK(s);
  1394. // transaction put in two column families
  1395. s = txn2->Put(cfa, "ka2", "va2");
  1396. ASSERT_OK(s);
  1397. s = txn2->Put(cfb, "kb2", "vb2");
  1398. ASSERT_OK(s);
  1399. // write prep section to wal
  1400. s = txn1->Prepare();
  1401. ASSERT_OK(s);
  1402. // our log should be in the heap
  1403. ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(),
  1404. txn1->GetLogNumber());
  1405. ASSERT_EQ(db_impl->TEST_LogfileNumber(), txn1->GetLastLogNumber());
  1406. // flush default cf to crate new log
  1407. s = db->Put(wopts, "foo", "bar");
  1408. ASSERT_OK(s);
  1409. s = db_impl->TEST_FlushMemTable(true);
  1410. ASSERT_OK(s);
  1411. // make sure we are on a new log
  1412. ASSERT_GT(db_impl->TEST_LogfileNumber(), txn1->GetLastLogNumber());
  1413. // put txn2 prep section in this log
  1414. s = txn2->Prepare();
  1415. ASSERT_OK(s);
  1416. ASSERT_EQ(db_impl->TEST_LogfileNumber(), txn2->GetLastLogNumber());
  1417. // heap should still see first log
  1418. ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(),
  1419. txn1->GetLogNumber());
  1420. // commit txn1
  1421. s = txn1->Commit();
  1422. ASSERT_OK(s);
  1423. // heap should now show txn2s log
  1424. ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(),
  1425. txn2->GetLogNumber());
  1426. switch (txn_db_options.write_policy) {
  1427. case WRITE_COMMITTED:
  1428. // we should see txn1s log refernced by the memtables
  1429. ASSERT_EQ(txn1->GetLogNumber(),
  1430. db_impl->TEST_FindMinPrepLogReferencedByMemTable());
  1431. break;
  1432. case WRITE_PREPARED:
  1433. case WRITE_UNPREPARED:
  1434. // In these modes memtable do not ref the prep sections
  1435. ASSERT_EQ(0, db_impl->TEST_FindMinPrepLogReferencedByMemTable());
  1436. break;
  1437. default:
  1438. assert(false);
  1439. }
  1440. // flush default cf to crate new log
  1441. s = db->Put(wopts, "foo", "bar2");
  1442. ASSERT_OK(s);
  1443. s = db_impl->TEST_FlushMemTable(true);
  1444. ASSERT_OK(s);
  1445. // make sure we are on a new log
  1446. ASSERT_GT(db_impl->TEST_LogfileNumber(), txn2->GetLastLogNumber());
  1447. // commit txn2
  1448. s = txn2->Commit();
  1449. ASSERT_OK(s);
  1450. // heap should not show any logs
  1451. ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(), 0);
  1452. switch (txn_db_options.write_policy) {
  1453. case WRITE_COMMITTED:
  1454. // should show the first txn log
  1455. ASSERT_EQ(txn1->GetLogNumber(),
  1456. db_impl->TEST_FindMinPrepLogReferencedByMemTable());
  1457. break;
  1458. case WRITE_PREPARED:
  1459. case WRITE_UNPREPARED:
  1460. // In these modes memtable do not ref the prep sections
  1461. ASSERT_EQ(0, db_impl->TEST_FindMinPrepLogReferencedByMemTable());
  1462. break;
  1463. default:
  1464. assert(false);
  1465. }
  1466. // flush only cfa memtable
  1467. s = db_impl->TEST_FlushMemTable(true, false, cfa);
  1468. ASSERT_OK(s);
  1469. switch (txn_db_options.write_policy) {
  1470. case WRITE_COMMITTED:
  1471. // should show the first txn log
  1472. ASSERT_EQ(txn2->GetLogNumber(),
  1473. db_impl->TEST_FindMinPrepLogReferencedByMemTable());
  1474. break;
  1475. case WRITE_PREPARED:
  1476. case WRITE_UNPREPARED:
  1477. // In these modes memtable do not ref the prep sections
  1478. ASSERT_EQ(0, db_impl->TEST_FindMinPrepLogReferencedByMemTable());
  1479. break;
  1480. default:
  1481. assert(false);
  1482. }
  1483. // flush only cfb memtable
  1484. s = db_impl->TEST_FlushMemTable(true, false, cfb);
  1485. ASSERT_OK(s);
  1486. // should show not dependency on logs
  1487. ASSERT_EQ(db_impl->TEST_FindMinPrepLogReferencedByMemTable(), 0);
  1488. ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(), 0);
  1489. delete txn1;
  1490. delete txn2;
  1491. delete cfa;
  1492. delete cfb;
  1493. }
  1494. TEST_P(TransactionTest, TwoPhaseLogRollingTest2) {
  1495. DBImpl* db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  1496. Status s;
  1497. ColumnFamilyHandle *cfa, *cfb;
  1498. ColumnFamilyOptions cf_options;
  1499. s = db->CreateColumnFamily(cf_options, "CFA", &cfa);
  1500. ASSERT_OK(s);
  1501. s = db->CreateColumnFamily(cf_options, "CFB", &cfb);
  1502. ASSERT_OK(s);
  1503. WriteOptions wopts;
  1504. wopts.disableWAL = false;
  1505. wopts.sync = true;
  1506. auto cfh_a = reinterpret_cast<ColumnFamilyHandleImpl*>(cfa);
  1507. auto cfh_b = reinterpret_cast<ColumnFamilyHandleImpl*>(cfb);
  1508. TransactionOptions topts1;
  1509. Transaction* txn1 = db->BeginTransaction(wopts, topts1);
  1510. s = txn1->SetName("xid1");
  1511. ASSERT_OK(s);
  1512. s = txn1->Put(cfa, "boys", "girls1");
  1513. ASSERT_OK(s);
  1514. Transaction* txn2 = db->BeginTransaction(wopts, topts1);
  1515. s = txn2->SetName("xid2");
  1516. ASSERT_OK(s);
  1517. s = txn2->Put(cfb, "up", "down1");
  1518. ASSERT_OK(s);
  1519. // prepre transaction in LOG A
  1520. s = txn1->Prepare();
  1521. ASSERT_OK(s);
  1522. // prepre transaction in LOG A
  1523. s = txn2->Prepare();
  1524. ASSERT_OK(s);
  1525. // regular put so that mem table can actually be flushed for log rolling
  1526. s = db->Put(wopts, "cats", "dogs1");
  1527. ASSERT_OK(s);
  1528. auto prepare_log_no = txn1->GetLastLogNumber();
  1529. // roll to LOG B
  1530. s = db_impl->TEST_FlushMemTable(true);
  1531. ASSERT_OK(s);
  1532. // now we pause background work so that
  1533. // imm()s are not flushed before we can check their status
  1534. s = db_impl->PauseBackgroundWork();
  1535. ASSERT_OK(s);
  1536. ASSERT_GT(db_impl->TEST_LogfileNumber(), prepare_log_no);
  1537. switch (txn_db_options.write_policy) {
  1538. case WRITE_COMMITTED:
  1539. // This cf is empty and should ref the latest log
  1540. ASSERT_GT(cfh_a->cfd()->GetLogNumber(), prepare_log_no);
  1541. ASSERT_EQ(cfh_a->cfd()->GetLogNumber(), db_impl->TEST_LogfileNumber());
  1542. break;
  1543. case WRITE_PREPARED:
  1544. case WRITE_UNPREPARED:
  1545. // This cf is not flushed yet and should ref the log that has its data
  1546. ASSERT_EQ(cfh_a->cfd()->GetLogNumber(), prepare_log_no);
  1547. break;
  1548. default:
  1549. assert(false);
  1550. }
  1551. ASSERT_EQ(db_impl->TEST_FindMinLogContainingOutstandingPrep(),
  1552. txn1->GetLogNumber());
  1553. ASSERT_EQ(db_impl->TEST_FindMinPrepLogReferencedByMemTable(), 0);
  1554. // commit in LOG B
  1555. s = txn1->Commit();
  1556. ASSERT_OK(s);
  1557. switch (txn_db_options.write_policy) {
  1558. case WRITE_COMMITTED:
  1559. ASSERT_EQ(db_impl->TEST_FindMinPrepLogReferencedByMemTable(),
  1560. prepare_log_no);
  1561. break;
  1562. case WRITE_PREPARED:
  1563. case WRITE_UNPREPARED:
  1564. // In these modes memtable do not ref the prep sections
  1565. ASSERT_EQ(db_impl->TEST_FindMinPrepLogReferencedByMemTable(), 0);
  1566. break;
  1567. default:
  1568. assert(false);
  1569. }
  1570. ASSERT_TRUE(!db_impl->TEST_UnableToReleaseOldestLog());
  1571. // request a flush for all column families such that the earliest
  1572. // alive log file can be killed
  1573. db_impl->TEST_SwitchWAL();
  1574. // log cannot be flushed because txn2 has not been commited
  1575. ASSERT_TRUE(!db_impl->TEST_IsLogGettingFlushed());
  1576. ASSERT_TRUE(db_impl->TEST_UnableToReleaseOldestLog());
  1577. // assert that cfa has a flush requested
  1578. ASSERT_TRUE(cfh_a->cfd()->imm()->HasFlushRequested());
  1579. switch (txn_db_options.write_policy) {
  1580. case WRITE_COMMITTED:
  1581. // cfb should not be flushed becuse it has no data from LOG A
  1582. ASSERT_TRUE(!cfh_b->cfd()->imm()->HasFlushRequested());
  1583. break;
  1584. case WRITE_PREPARED:
  1585. case WRITE_UNPREPARED:
  1586. // cfb should be flushed becuse it has prepared data from LOG A
  1587. ASSERT_TRUE(cfh_b->cfd()->imm()->HasFlushRequested());
  1588. break;
  1589. default:
  1590. assert(false);
  1591. }
  1592. // cfb now has data from LOG A
  1593. s = txn2->Commit();
  1594. ASSERT_OK(s);
  1595. db_impl->TEST_SwitchWAL();
  1596. ASSERT_TRUE(!db_impl->TEST_UnableToReleaseOldestLog());
  1597. // we should see that cfb now has a flush requested
  1598. ASSERT_TRUE(cfh_b->cfd()->imm()->HasFlushRequested());
  1599. // all data in LOG A resides in a memtable that has been
  1600. // requested for a flush
  1601. ASSERT_TRUE(db_impl->TEST_IsLogGettingFlushed());
  1602. delete txn1;
  1603. delete txn2;
  1604. delete cfa;
  1605. delete cfb;
  1606. }
  1607. /*
  1608. * 1) use prepare to keep first log around to determine starting sequence
  1609. * during recovery.
  1610. * 2) insert many values, skipping wal, to increase seqid.
  1611. * 3) insert final value into wal
  1612. * 4) recover and see that final value was properly recovered - not
  1613. * hidden behind improperly summed sequence ids
  1614. */
  1615. TEST_P(TransactionTest, TwoPhaseOutOfOrderDelete) {
  1616. DBImpl* db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  1617. WriteOptions wal_on, wal_off;
  1618. wal_on.sync = true;
  1619. wal_on.disableWAL = false;
  1620. wal_off.disableWAL = true;
  1621. ReadOptions read_options;
  1622. TransactionOptions txn_options;
  1623. std::string value;
  1624. Status s;
  1625. Transaction* txn1 = db->BeginTransaction(wal_on, txn_options);
  1626. s = txn1->SetName("1");
  1627. ASSERT_OK(s);
  1628. s = db->Put(wal_on, "first", "first");
  1629. ASSERT_OK(s);
  1630. s = txn1->Put(Slice("dummy"), Slice("dummy"));
  1631. ASSERT_OK(s);
  1632. s = txn1->Prepare();
  1633. ASSERT_OK(s);
  1634. s = db->Put(wal_off, "cats", "dogs1");
  1635. ASSERT_OK(s);
  1636. s = db->Put(wal_off, "cats", "dogs2");
  1637. ASSERT_OK(s);
  1638. s = db->Put(wal_off, "cats", "dogs3");
  1639. ASSERT_OK(s);
  1640. s = db_impl->TEST_FlushMemTable(true);
  1641. ASSERT_OK(s);
  1642. s = db->Put(wal_on, "cats", "dogs4");
  1643. ASSERT_OK(s);
  1644. db->FlushWAL(false);
  1645. // kill and reopen
  1646. env->SetFilesystemActive(false);
  1647. reinterpret_cast<PessimisticTransactionDB*>(db)->TEST_Crash();
  1648. ReOpenNoDelete();
  1649. assert(db != nullptr);
  1650. s = db->Get(read_options, "first", &value);
  1651. ASSERT_OK(s);
  1652. ASSERT_EQ(value, "first");
  1653. s = db->Get(read_options, "cats", &value);
  1654. ASSERT_OK(s);
  1655. ASSERT_EQ(value, "dogs4");
  1656. }
  1657. TEST_P(TransactionTest, FirstWriteTest) {
  1658. WriteOptions write_options;
  1659. // Test conflict checking against the very first write to a db.
  1660. // The transaction's snapshot will have seq 1 and the following write
  1661. // will have sequence 1.
  1662. Status s = db->Put(write_options, "A", "a");
  1663. Transaction* txn = db->BeginTransaction(write_options);
  1664. txn->SetSnapshot();
  1665. ASSERT_OK(s);
  1666. s = txn->Put("A", "b");
  1667. ASSERT_OK(s);
  1668. delete txn;
  1669. }
  1670. TEST_P(TransactionTest, FirstWriteTest2) {
  1671. WriteOptions write_options;
  1672. Transaction* txn = db->BeginTransaction(write_options);
  1673. txn->SetSnapshot();
  1674. // Test conflict checking against the very first write to a db.
  1675. // The transaction's snapshot is a seq 0 while the following write
  1676. // will have sequence 1.
  1677. Status s = db->Put(write_options, "A", "a");
  1678. ASSERT_OK(s);
  1679. s = txn->Put("A", "b");
  1680. ASSERT_TRUE(s.IsBusy());
  1681. delete txn;
  1682. }
  1683. TEST_P(TransactionTest, WriteOptionsTest) {
  1684. WriteOptions write_options;
  1685. write_options.sync = true;
  1686. write_options.disableWAL = true;
  1687. Transaction* txn = db->BeginTransaction(write_options);
  1688. ASSERT_TRUE(txn);
  1689. ASSERT_TRUE(txn->GetWriteOptions()->sync);
  1690. write_options.sync = false;
  1691. txn->SetWriteOptions(write_options);
  1692. ASSERT_FALSE(txn->GetWriteOptions()->sync);
  1693. ASSERT_TRUE(txn->GetWriteOptions()->disableWAL);
  1694. delete txn;
  1695. }
  1696. TEST_P(TransactionTest, WriteConflictTest) {
  1697. WriteOptions write_options;
  1698. ReadOptions read_options;
  1699. string value;
  1700. Status s;
  1701. db->Put(write_options, "foo", "A");
  1702. db->Put(write_options, "foo2", "B");
  1703. Transaction* txn = db->BeginTransaction(write_options);
  1704. ASSERT_TRUE(txn);
  1705. s = txn->Put("foo", "A2");
  1706. ASSERT_OK(s);
  1707. s = txn->Put("foo2", "B2");
  1708. ASSERT_OK(s);
  1709. // This Put outside of a transaction will conflict with the previous write
  1710. s = db->Put(write_options, "foo", "xxx");
  1711. ASSERT_TRUE(s.IsTimedOut());
  1712. s = db->Get(read_options, "foo", &value);
  1713. ASSERT_EQ(value, "A");
  1714. s = txn->Commit();
  1715. ASSERT_OK(s);
  1716. db->Get(read_options, "foo", &value);
  1717. ASSERT_EQ(value, "A2");
  1718. db->Get(read_options, "foo2", &value);
  1719. ASSERT_EQ(value, "B2");
  1720. delete txn;
  1721. }
  1722. TEST_P(TransactionTest, WriteConflictTest2) {
  1723. WriteOptions write_options;
  1724. ReadOptions read_options;
  1725. TransactionOptions txn_options;
  1726. std::string value;
  1727. Status s;
  1728. db->Put(write_options, "foo", "bar");
  1729. txn_options.set_snapshot = true;
  1730. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  1731. ASSERT_TRUE(txn);
  1732. // This Put outside of a transaction will conflict with a later write
  1733. s = db->Put(write_options, "foo", "barz");
  1734. ASSERT_OK(s);
  1735. s = txn->Put("foo2", "X");
  1736. ASSERT_OK(s);
  1737. s = txn->Put("foo",
  1738. "bar2"); // Conflicts with write done after snapshot taken
  1739. ASSERT_TRUE(s.IsBusy());
  1740. s = txn->Put("foo3", "Y");
  1741. ASSERT_OK(s);
  1742. s = db->Get(read_options, "foo", &value);
  1743. ASSERT_EQ(value, "barz");
  1744. ASSERT_EQ(2, txn->GetNumKeys());
  1745. s = txn->Commit();
  1746. ASSERT_OK(s); // Txn should commit, but only write foo2 and foo3
  1747. // Verify that transaction wrote foo2 and foo3 but not foo
  1748. db->Get(read_options, "foo", &value);
  1749. ASSERT_EQ(value, "barz");
  1750. db->Get(read_options, "foo2", &value);
  1751. ASSERT_EQ(value, "X");
  1752. db->Get(read_options, "foo3", &value);
  1753. ASSERT_EQ(value, "Y");
  1754. delete txn;
  1755. }
  1756. TEST_P(TransactionTest, ReadConflictTest) {
  1757. WriteOptions write_options;
  1758. ReadOptions read_options, snapshot_read_options;
  1759. TransactionOptions txn_options;
  1760. std::string value;
  1761. Status s;
  1762. db->Put(write_options, "foo", "bar");
  1763. db->Put(write_options, "foo2", "bar");
  1764. txn_options.set_snapshot = true;
  1765. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  1766. ASSERT_TRUE(txn);
  1767. txn->SetSnapshot();
  1768. snapshot_read_options.snapshot = txn->GetSnapshot();
  1769. txn->GetForUpdate(snapshot_read_options, "foo", &value);
  1770. ASSERT_EQ(value, "bar");
  1771. // This Put outside of a transaction will conflict with the previous read
  1772. s = db->Put(write_options, "foo", "barz");
  1773. ASSERT_TRUE(s.IsTimedOut());
  1774. s = db->Get(read_options, "foo", &value);
  1775. ASSERT_EQ(value, "bar");
  1776. s = txn->Get(read_options, "foo", &value);
  1777. ASSERT_EQ(value, "bar");
  1778. s = txn->Commit();
  1779. ASSERT_OK(s);
  1780. delete txn;
  1781. }
  1782. TEST_P(TransactionTest, TxnOnlyTest) {
  1783. // Test to make sure transactions work when there are no other writes in an
  1784. // empty db.
  1785. WriteOptions write_options;
  1786. ReadOptions read_options;
  1787. std::string value;
  1788. Status s;
  1789. Transaction* txn = db->BeginTransaction(write_options);
  1790. ASSERT_TRUE(txn);
  1791. s = txn->Put("x", "y");
  1792. ASSERT_OK(s);
  1793. s = txn->Commit();
  1794. ASSERT_OK(s);
  1795. delete txn;
  1796. }
  1797. TEST_P(TransactionTest, FlushTest) {
  1798. WriteOptions write_options;
  1799. ReadOptions read_options, snapshot_read_options;
  1800. std::string value;
  1801. Status s;
  1802. db->Put(write_options, Slice("foo"), Slice("bar"));
  1803. db->Put(write_options, Slice("foo2"), Slice("bar"));
  1804. Transaction* txn = db->BeginTransaction(write_options);
  1805. ASSERT_TRUE(txn);
  1806. snapshot_read_options.snapshot = txn->GetSnapshot();
  1807. txn->GetForUpdate(snapshot_read_options, "foo", &value);
  1808. ASSERT_EQ(value, "bar");
  1809. s = txn->Put(Slice("foo"), Slice("bar2"));
  1810. ASSERT_OK(s);
  1811. txn->GetForUpdate(snapshot_read_options, "foo", &value);
  1812. ASSERT_EQ(value, "bar2");
  1813. // Put a random key so we have a memtable to flush
  1814. s = db->Put(write_options, "dummy", "dummy");
  1815. ASSERT_OK(s);
  1816. // force a memtable flush
  1817. FlushOptions flush_ops;
  1818. db->Flush(flush_ops);
  1819. s = txn->Commit();
  1820. // txn should commit since the flushed table is still in MemtableList History
  1821. ASSERT_OK(s);
  1822. db->Get(read_options, "foo", &value);
  1823. ASSERT_EQ(value, "bar2");
  1824. delete txn;
  1825. }
  1826. TEST_P(TransactionTest, FlushTest2) {
  1827. const size_t num_tests = 3;
  1828. for (size_t n = 0; n < num_tests; n++) {
  1829. // Test different table factories
  1830. switch (n) {
  1831. case 0:
  1832. break;
  1833. case 1:
  1834. options.table_factory.reset(new mock::MockTableFactory());
  1835. break;
  1836. case 2: {
  1837. PlainTableOptions pt_opts;
  1838. pt_opts.hash_table_ratio = 0;
  1839. options.table_factory.reset(NewPlainTableFactory(pt_opts));
  1840. break;
  1841. }
  1842. }
  1843. Status s = ReOpen();
  1844. ASSERT_OK(s);
  1845. assert(db != nullptr);
  1846. WriteOptions write_options;
  1847. ReadOptions read_options, snapshot_read_options;
  1848. TransactionOptions txn_options;
  1849. string value;
  1850. DBImpl* db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  1851. db->Put(write_options, Slice("foo"), Slice("bar"));
  1852. db->Put(write_options, Slice("foo2"), Slice("bar2"));
  1853. db->Put(write_options, Slice("foo3"), Slice("bar3"));
  1854. txn_options.set_snapshot = true;
  1855. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  1856. ASSERT_TRUE(txn);
  1857. snapshot_read_options.snapshot = txn->GetSnapshot();
  1858. txn->GetForUpdate(snapshot_read_options, "foo", &value);
  1859. ASSERT_EQ(value, "bar");
  1860. s = txn->Put(Slice("foo"), Slice("bar2"));
  1861. ASSERT_OK(s);
  1862. txn->GetForUpdate(snapshot_read_options, "foo", &value);
  1863. ASSERT_EQ(value, "bar2");
  1864. // verify foo is locked by txn
  1865. s = db->Delete(write_options, "foo");
  1866. ASSERT_TRUE(s.IsTimedOut());
  1867. s = db->Put(write_options, "Z", "z");
  1868. ASSERT_OK(s);
  1869. s = db->Put(write_options, "dummy", "dummy");
  1870. ASSERT_OK(s);
  1871. s = db->Put(write_options, "S", "s");
  1872. ASSERT_OK(s);
  1873. s = db->SingleDelete(write_options, "S");
  1874. ASSERT_OK(s);
  1875. s = txn->Delete("S");
  1876. // Should fail after encountering a write to S in memtable
  1877. ASSERT_TRUE(s.IsBusy());
  1878. // force a memtable flush
  1879. s = db_impl->TEST_FlushMemTable(true);
  1880. ASSERT_OK(s);
  1881. // Put a random key so we have a MemTable to flush
  1882. s = db->Put(write_options, "dummy", "dummy2");
  1883. ASSERT_OK(s);
  1884. // force a memtable flush
  1885. ASSERT_OK(db_impl->TEST_FlushMemTable(true));
  1886. s = db->Put(write_options, "dummy", "dummy3");
  1887. ASSERT_OK(s);
  1888. // force a memtable flush
  1889. // Since our test db has max_write_buffer_number=2, this flush will cause
  1890. // the first memtable to get purged from the MemtableList history.
  1891. ASSERT_OK(db_impl->TEST_FlushMemTable(true));
  1892. s = txn->Put("X", "Y");
  1893. // Should succeed after verifying there is no write to X in SST file
  1894. ASSERT_OK(s);
  1895. s = txn->Put("Z", "zz");
  1896. // Should fail after encountering a write to Z in SST file
  1897. ASSERT_TRUE(s.IsBusy());
  1898. s = txn->GetForUpdate(read_options, "foo2", &value);
  1899. // should succeed since key was written before txn started
  1900. ASSERT_OK(s);
  1901. // verify foo2 is locked by txn
  1902. s = db->Delete(write_options, "foo2");
  1903. ASSERT_TRUE(s.IsTimedOut());
  1904. s = txn->Delete("S");
  1905. // Should fail after encountering a write to S in SST file
  1906. ASSERT_TRUE(s.IsBusy());
  1907. // Write a bunch of keys to db to force a compaction
  1908. Random rnd(47);
  1909. for (int i = 0; i < 1000; i++) {
  1910. s = db->Put(write_options, std::to_string(i),
  1911. test::CompressibleString(&rnd, 0.8, 100, &value));
  1912. ASSERT_OK(s);
  1913. }
  1914. s = txn->Put("X", "yy");
  1915. // Should succeed after verifying there is no write to X in SST file
  1916. ASSERT_OK(s);
  1917. s = txn->Put("Z", "zzz");
  1918. // Should fail after encountering a write to Z in SST file
  1919. ASSERT_TRUE(s.IsBusy());
  1920. s = txn->Delete("S");
  1921. // Should fail after encountering a write to S in SST file
  1922. ASSERT_TRUE(s.IsBusy());
  1923. s = txn->GetForUpdate(read_options, "foo3", &value);
  1924. // should succeed since key was written before txn started
  1925. ASSERT_OK(s);
  1926. // verify foo3 is locked by txn
  1927. s = db->Delete(write_options, "foo3");
  1928. ASSERT_TRUE(s.IsTimedOut());
  1929. db_impl->TEST_WaitForCompact();
  1930. s = txn->Commit();
  1931. ASSERT_OK(s);
  1932. // Transaction should only write the keys that succeeded.
  1933. s = db->Get(read_options, "foo", &value);
  1934. ASSERT_EQ(value, "bar2");
  1935. s = db->Get(read_options, "X", &value);
  1936. ASSERT_OK(s);
  1937. ASSERT_EQ("yy", value);
  1938. s = db->Get(read_options, "Z", &value);
  1939. ASSERT_OK(s);
  1940. ASSERT_EQ("z", value);
  1941. delete txn;
  1942. }
  1943. }
  1944. TEST_P(TransactionTest, NoSnapshotTest) {
  1945. WriteOptions write_options;
  1946. ReadOptions read_options;
  1947. std::string value;
  1948. Status s;
  1949. db->Put(write_options, "AAA", "bar");
  1950. Transaction* txn = db->BeginTransaction(write_options);
  1951. ASSERT_TRUE(txn);
  1952. // Modify key after transaction start
  1953. db->Put(write_options, "AAA", "bar1");
  1954. // Read and write without a snap
  1955. txn->GetForUpdate(read_options, "AAA", &value);
  1956. ASSERT_EQ(value, "bar1");
  1957. s = txn->Put("AAA", "bar2");
  1958. ASSERT_OK(s);
  1959. // Should commit since read/write was done after data changed
  1960. s = txn->Commit();
  1961. ASSERT_OK(s);
  1962. txn->GetForUpdate(read_options, "AAA", &value);
  1963. ASSERT_EQ(value, "bar2");
  1964. delete txn;
  1965. }
  1966. TEST_P(TransactionTest, MultipleSnapshotTest) {
  1967. WriteOptions write_options;
  1968. ReadOptions read_options, snapshot_read_options;
  1969. std::string value;
  1970. Status s;
  1971. ASSERT_OK(db->Put(write_options, "AAA", "bar"));
  1972. ASSERT_OK(db->Put(write_options, "BBB", "bar"));
  1973. ASSERT_OK(db->Put(write_options, "CCC", "bar"));
  1974. Transaction* txn = db->BeginTransaction(write_options);
  1975. ASSERT_TRUE(txn);
  1976. db->Put(write_options, "AAA", "bar1");
  1977. // Read and write without a snapshot
  1978. ASSERT_OK(txn->GetForUpdate(read_options, "AAA", &value));
  1979. ASSERT_EQ(value, "bar1");
  1980. s = txn->Put("AAA", "bar2");
  1981. ASSERT_OK(s);
  1982. // Modify BBB before snapshot is taken
  1983. ASSERT_OK(db->Put(write_options, "BBB", "bar1"));
  1984. txn->SetSnapshot();
  1985. snapshot_read_options.snapshot = txn->GetSnapshot();
  1986. // Read and write with snapshot
  1987. ASSERT_OK(txn->GetForUpdate(snapshot_read_options, "BBB", &value));
  1988. ASSERT_EQ(value, "bar1");
  1989. s = txn->Put("BBB", "bar2");
  1990. ASSERT_OK(s);
  1991. ASSERT_OK(db->Put(write_options, "CCC", "bar1"));
  1992. // Set a new snapshot
  1993. txn->SetSnapshot();
  1994. snapshot_read_options.snapshot = txn->GetSnapshot();
  1995. // Read and write with snapshot
  1996. txn->GetForUpdate(snapshot_read_options, "CCC", &value);
  1997. ASSERT_EQ(value, "bar1");
  1998. s = txn->Put("CCC", "bar2");
  1999. ASSERT_OK(s);
  2000. s = txn->GetForUpdate(read_options, "AAA", &value);
  2001. ASSERT_OK(s);
  2002. ASSERT_EQ(value, "bar2");
  2003. s = txn->GetForUpdate(read_options, "BBB", &value);
  2004. ASSERT_OK(s);
  2005. ASSERT_EQ(value, "bar2");
  2006. s = txn->GetForUpdate(read_options, "CCC", &value);
  2007. ASSERT_OK(s);
  2008. ASSERT_EQ(value, "bar2");
  2009. s = db->Get(read_options, "AAA", &value);
  2010. ASSERT_OK(s);
  2011. ASSERT_EQ(value, "bar1");
  2012. s = db->Get(read_options, "BBB", &value);
  2013. ASSERT_OK(s);
  2014. ASSERT_EQ(value, "bar1");
  2015. s = db->Get(read_options, "CCC", &value);
  2016. ASSERT_OK(s);
  2017. ASSERT_EQ(value, "bar1");
  2018. s = txn->Commit();
  2019. ASSERT_OK(s);
  2020. s = db->Get(read_options, "AAA", &value);
  2021. ASSERT_OK(s);
  2022. ASSERT_EQ(value, "bar2");
  2023. s = db->Get(read_options, "BBB", &value);
  2024. ASSERT_OK(s);
  2025. ASSERT_EQ(value, "bar2");
  2026. s = db->Get(read_options, "CCC", &value);
  2027. ASSERT_OK(s);
  2028. ASSERT_EQ(value, "bar2");
  2029. // verify that we track multiple writes to the same key at different snapshots
  2030. delete txn;
  2031. txn = db->BeginTransaction(write_options);
  2032. // Potentially conflicting writes
  2033. db->Put(write_options, "ZZZ", "zzz");
  2034. db->Put(write_options, "XXX", "xxx");
  2035. txn->SetSnapshot();
  2036. TransactionOptions txn_options;
  2037. txn_options.set_snapshot = true;
  2038. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  2039. txn2->SetSnapshot();
  2040. // This should not conflict in txn since the snapshot is later than the
  2041. // previous write (spoiler alert: it will later conflict with txn2).
  2042. s = txn->Put("ZZZ", "zzzz");
  2043. ASSERT_OK(s);
  2044. s = txn->Commit();
  2045. ASSERT_OK(s);
  2046. delete txn;
  2047. // This will conflict since the snapshot is earlier than another write to ZZZ
  2048. s = txn2->Put("ZZZ", "xxxxx");
  2049. ASSERT_TRUE(s.IsBusy());
  2050. s = txn2->Commit();
  2051. ASSERT_OK(s);
  2052. s = db->Get(read_options, "ZZZ", &value);
  2053. ASSERT_OK(s);
  2054. ASSERT_EQ(value, "zzzz");
  2055. delete txn2;
  2056. }
  2057. TEST_P(TransactionTest, ColumnFamiliesTest) {
  2058. WriteOptions write_options;
  2059. ReadOptions read_options, snapshot_read_options;
  2060. TransactionOptions txn_options;
  2061. string value;
  2062. Status s;
  2063. ColumnFamilyHandle *cfa, *cfb;
  2064. ColumnFamilyOptions cf_options;
  2065. // Create 2 new column families
  2066. s = db->CreateColumnFamily(cf_options, "CFA", &cfa);
  2067. ASSERT_OK(s);
  2068. s = db->CreateColumnFamily(cf_options, "CFB", &cfb);
  2069. ASSERT_OK(s);
  2070. delete cfa;
  2071. delete cfb;
  2072. delete db;
  2073. db = nullptr;
  2074. // open DB with three column families
  2075. std::vector<ColumnFamilyDescriptor> column_families;
  2076. // have to open default column family
  2077. column_families.push_back(
  2078. ColumnFamilyDescriptor(kDefaultColumnFamilyName, ColumnFamilyOptions()));
  2079. // open the new column families
  2080. column_families.push_back(
  2081. ColumnFamilyDescriptor("CFA", ColumnFamilyOptions()));
  2082. column_families.push_back(
  2083. ColumnFamilyDescriptor("CFB", ColumnFamilyOptions()));
  2084. std::vector<ColumnFamilyHandle*> handles;
  2085. ASSERT_OK(ReOpenNoDelete(column_families, &handles));
  2086. assert(db != nullptr);
  2087. Transaction* txn = db->BeginTransaction(write_options);
  2088. ASSERT_TRUE(txn);
  2089. txn->SetSnapshot();
  2090. snapshot_read_options.snapshot = txn->GetSnapshot();
  2091. txn_options.set_snapshot = true;
  2092. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  2093. ASSERT_TRUE(txn2);
  2094. // Write some data to the db
  2095. WriteBatch batch;
  2096. batch.Put("foo", "foo");
  2097. batch.Put(handles[1], "AAA", "bar");
  2098. batch.Put(handles[1], "AAAZZZ", "bar");
  2099. s = db->Write(write_options, &batch);
  2100. ASSERT_OK(s);
  2101. db->Delete(write_options, handles[1], "AAAZZZ");
  2102. // These keys do not conflict with existing writes since they're in
  2103. // different column families
  2104. s = txn->Delete("AAA");
  2105. ASSERT_OK(s);
  2106. s = txn->GetForUpdate(snapshot_read_options, handles[1], "foo", &value);
  2107. ASSERT_TRUE(s.IsNotFound());
  2108. Slice key_slice("AAAZZZ");
  2109. Slice value_slices[2] = {Slice("bar"), Slice("bar")};
  2110. s = txn->Put(handles[2], SliceParts(&key_slice, 1),
  2111. SliceParts(value_slices, 2));
  2112. ASSERT_OK(s);
  2113. ASSERT_EQ(3, txn->GetNumKeys());
  2114. s = txn->Commit();
  2115. ASSERT_OK(s);
  2116. s = db->Get(read_options, "AAA", &value);
  2117. ASSERT_TRUE(s.IsNotFound());
  2118. s = db->Get(read_options, handles[2], "AAAZZZ", &value);
  2119. ASSERT_EQ(value, "barbar");
  2120. Slice key_slices[3] = {Slice("AAA"), Slice("ZZ"), Slice("Z")};
  2121. Slice value_slice("barbarbar");
  2122. s = txn2->Delete(handles[2], "XXX");
  2123. ASSERT_OK(s);
  2124. s = txn2->Delete(handles[1], "XXX");
  2125. ASSERT_OK(s);
  2126. // This write will cause a conflict with the earlier batch write
  2127. s = txn2->Put(handles[1], SliceParts(key_slices, 3),
  2128. SliceParts(&value_slice, 1));
  2129. ASSERT_TRUE(s.IsBusy());
  2130. s = txn2->Commit();
  2131. ASSERT_OK(s);
  2132. // In the above the latest change to AAAZZZ in handles[1] is delete.
  2133. s = db->Get(read_options, handles[1], "AAAZZZ", &value);
  2134. ASSERT_TRUE(s.IsNotFound());
  2135. delete txn;
  2136. delete txn2;
  2137. txn = db->BeginTransaction(write_options, txn_options);
  2138. snapshot_read_options.snapshot = txn->GetSnapshot();
  2139. txn2 = db->BeginTransaction(write_options, txn_options);
  2140. ASSERT_TRUE(txn);
  2141. std::vector<ColumnFamilyHandle*> multiget_cfh = {handles[1], handles[2],
  2142. handles[0], handles[2]};
  2143. std::vector<Slice> multiget_keys = {"AAA", "AAAZZZ", "foo", "foo"};
  2144. std::vector<std::string> values(4);
  2145. std::vector<Status> results = txn->MultiGetForUpdate(
  2146. snapshot_read_options, multiget_cfh, multiget_keys, &values);
  2147. ASSERT_OK(results[0]);
  2148. ASSERT_OK(results[1]);
  2149. ASSERT_OK(results[2]);
  2150. ASSERT_TRUE(results[3].IsNotFound());
  2151. ASSERT_EQ(values[0], "bar");
  2152. ASSERT_EQ(values[1], "barbar");
  2153. ASSERT_EQ(values[2], "foo");
  2154. s = txn->SingleDelete(handles[2], "ZZZ");
  2155. ASSERT_OK(s);
  2156. s = txn->Put(handles[2], "ZZZ", "YYY");
  2157. ASSERT_OK(s);
  2158. s = txn->Put(handles[2], "ZZZ", "YYYY");
  2159. ASSERT_OK(s);
  2160. s = txn->Delete(handles[2], "ZZZ");
  2161. ASSERT_OK(s);
  2162. s = txn->Put(handles[2], "AAAZZZ", "barbarbar");
  2163. ASSERT_OK(s);
  2164. ASSERT_EQ(5, txn->GetNumKeys());
  2165. // Txn should commit
  2166. s = txn->Commit();
  2167. ASSERT_OK(s);
  2168. s = db->Get(read_options, handles[2], "ZZZ", &value);
  2169. ASSERT_TRUE(s.IsNotFound());
  2170. // Put a key which will conflict with the next txn using the previous snapshot
  2171. db->Put(write_options, handles[2], "foo", "000");
  2172. results = txn2->MultiGetForUpdate(snapshot_read_options, multiget_cfh,
  2173. multiget_keys, &values);
  2174. // All results should fail since there was a conflict
  2175. ASSERT_TRUE(results[0].IsBusy());
  2176. ASSERT_TRUE(results[1].IsBusy());
  2177. ASSERT_TRUE(results[2].IsBusy());
  2178. ASSERT_TRUE(results[3].IsBusy());
  2179. s = db->Get(read_options, handles[2], "foo", &value);
  2180. ASSERT_EQ(value, "000");
  2181. s = txn2->Commit();
  2182. ASSERT_OK(s);
  2183. s = db->DropColumnFamily(handles[1]);
  2184. ASSERT_OK(s);
  2185. s = db->DropColumnFamily(handles[2]);
  2186. ASSERT_OK(s);
  2187. delete txn;
  2188. delete txn2;
  2189. for (auto handle : handles) {
  2190. delete handle;
  2191. }
  2192. }
  2193. TEST_P(TransactionTest, MultiGetBatchedTest) {
  2194. WriteOptions write_options;
  2195. ReadOptions read_options, snapshot_read_options;
  2196. TransactionOptions txn_options;
  2197. string value;
  2198. Status s;
  2199. ColumnFamilyHandle* cf;
  2200. ColumnFamilyOptions cf_options;
  2201. // Create a new column families
  2202. s = db->CreateColumnFamily(cf_options, "CF", &cf);
  2203. ASSERT_OK(s);
  2204. delete cf;
  2205. delete db;
  2206. db = nullptr;
  2207. // open DB with three column families
  2208. std::vector<ColumnFamilyDescriptor> column_families;
  2209. // have to open default column family
  2210. column_families.push_back(
  2211. ColumnFamilyDescriptor(kDefaultColumnFamilyName, ColumnFamilyOptions()));
  2212. // open the new column families
  2213. cf_options.merge_operator = MergeOperators::CreateStringAppendOperator();
  2214. column_families.push_back(ColumnFamilyDescriptor("CF", cf_options));
  2215. std::vector<ColumnFamilyHandle*> handles;
  2216. options.merge_operator = MergeOperators::CreateStringAppendOperator();
  2217. ASSERT_OK(ReOpenNoDelete(column_families, &handles));
  2218. assert(db != nullptr);
  2219. // Write some data to the db
  2220. WriteBatch batch;
  2221. batch.Put(handles[1], "aaa", "val1");
  2222. batch.Put(handles[1], "bbb", "val2");
  2223. batch.Put(handles[1], "ccc", "val3");
  2224. batch.Put(handles[1], "ddd", "foo");
  2225. batch.Put(handles[1], "eee", "val5");
  2226. batch.Put(handles[1], "fff", "val6");
  2227. batch.Merge(handles[1], "ggg", "foo");
  2228. s = db->Write(write_options, &batch);
  2229. ASSERT_OK(s);
  2230. Transaction* txn = db->BeginTransaction(write_options);
  2231. ASSERT_TRUE(txn);
  2232. txn->SetSnapshot();
  2233. snapshot_read_options.snapshot = txn->GetSnapshot();
  2234. txn_options.set_snapshot = true;
  2235. // Write some data to the db
  2236. s = txn->Delete(handles[1], "bbb");
  2237. ASSERT_OK(s);
  2238. s = txn->Put(handles[1], "ccc", "val3_new");
  2239. ASSERT_OK(s);
  2240. s = txn->Merge(handles[1], "ddd", "bar");
  2241. ASSERT_OK(s);
  2242. std::vector<Slice> keys = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg"};
  2243. std::vector<PinnableSlice> values(keys.size());
  2244. std::vector<Status> statuses(keys.size());
  2245. txn->MultiGet(snapshot_read_options, handles[1], keys.size(), keys.data(),
  2246. values.data(), statuses.data());
  2247. ASSERT_TRUE(statuses[0].ok());
  2248. ASSERT_EQ(values[0], "val1");
  2249. ASSERT_TRUE(statuses[1].IsNotFound());
  2250. ASSERT_TRUE(statuses[2].ok());
  2251. ASSERT_EQ(values[2], "val3_new");
  2252. ASSERT_TRUE(statuses[3].IsMergeInProgress());
  2253. ASSERT_TRUE(statuses[4].ok());
  2254. ASSERT_EQ(values[4], "val5");
  2255. ASSERT_TRUE(statuses[5].ok());
  2256. ASSERT_EQ(values[5], "val6");
  2257. ASSERT_TRUE(statuses[6].ok());
  2258. ASSERT_EQ(values[6], "foo");
  2259. delete txn;
  2260. for (auto handle : handles) {
  2261. delete handle;
  2262. }
  2263. }
  2264. // This test calls WriteBatchWithIndex::MultiGetFromBatchAndDB with a large
  2265. // number of keys, i.e greater than MultiGetContext::MAX_BATCH_SIZE, which is
  2266. // is 32. This forces autovector allocations in the MultiGet code paths
  2267. // to use std::vector in addition to stack allocations. The MultiGet keys
  2268. // includes Merges, which are handled specially in MultiGetFromBatchAndDB by
  2269. // allocating an autovector of MergeContexts
  2270. TEST_P(TransactionTest, MultiGetLargeBatchedTest) {
  2271. WriteOptions write_options;
  2272. ReadOptions read_options, snapshot_read_options;
  2273. string value;
  2274. Status s;
  2275. ColumnFamilyHandle* cf;
  2276. ColumnFamilyOptions cf_options;
  2277. std::vector<std::string> key_str;
  2278. for (int i = 0; i < 100; ++i) {
  2279. key_str.emplace_back(std::to_string(i));
  2280. }
  2281. // Create a new column families
  2282. s = db->CreateColumnFamily(cf_options, "CF", &cf);
  2283. ASSERT_OK(s);
  2284. delete cf;
  2285. delete db;
  2286. db = nullptr;
  2287. // open DB with three column families
  2288. std::vector<ColumnFamilyDescriptor> column_families;
  2289. // have to open default column family
  2290. column_families.push_back(
  2291. ColumnFamilyDescriptor(kDefaultColumnFamilyName, ColumnFamilyOptions()));
  2292. // open the new column families
  2293. cf_options.merge_operator = MergeOperators::CreateStringAppendOperator();
  2294. column_families.push_back(ColumnFamilyDescriptor("CF", cf_options));
  2295. std::vector<ColumnFamilyHandle*> handles;
  2296. options.merge_operator = MergeOperators::CreateStringAppendOperator();
  2297. ASSERT_OK(ReOpenNoDelete(column_families, &handles));
  2298. assert(db != nullptr);
  2299. // Write some data to the db
  2300. WriteBatch batch;
  2301. for (int i = 0; i < 3 * MultiGetContext::MAX_BATCH_SIZE; ++i) {
  2302. std::string val = "val" + std::to_string(i);
  2303. batch.Put(handles[1], key_str[i], val);
  2304. }
  2305. s = db->Write(write_options, &batch);
  2306. ASSERT_OK(s);
  2307. WriteBatchWithIndex wb;
  2308. // Write some data to the db
  2309. s = wb.Delete(handles[1], std::to_string(1));
  2310. ASSERT_OK(s);
  2311. s = wb.Put(handles[1], std::to_string(2), "new_val" + std::to_string(2));
  2312. ASSERT_OK(s);
  2313. // Write a lot of merges so when we call MultiGetFromBatchAndDB later on,
  2314. // it is forced to use std::vector in ROCKSDB_NAMESPACE::autovector to
  2315. // allocate MergeContexts. The number of merges needs to be >
  2316. // MultiGetContext::MAX_BATCH_SIZE
  2317. for (int i = 8; i < MultiGetContext::MAX_BATCH_SIZE + 24; ++i) {
  2318. s = wb.Merge(handles[1], std::to_string(i), "merge");
  2319. ASSERT_OK(s);
  2320. }
  2321. // MultiGet a lot of keys in order to force std::vector reallocations
  2322. std::vector<Slice> keys;
  2323. for (int i = 0; i < MultiGetContext::MAX_BATCH_SIZE + 32; ++i) {
  2324. keys.emplace_back(key_str[i]);
  2325. }
  2326. std::vector<PinnableSlice> values(keys.size());
  2327. std::vector<Status> statuses(keys.size());
  2328. wb.MultiGetFromBatchAndDB(db, snapshot_read_options, handles[1], keys.size(), keys.data(),
  2329. values.data(), statuses.data(), false);
  2330. for (size_t i =0; i < keys.size(); ++i) {
  2331. if (i == 1) {
  2332. ASSERT_TRUE(statuses[1].IsNotFound());
  2333. } else if (i == 2) {
  2334. ASSERT_TRUE(statuses[2].ok());
  2335. ASSERT_EQ(values[2], "new_val" + std::to_string(2));
  2336. } else if (i >= 8 && i < 56) {
  2337. ASSERT_TRUE(statuses[i].ok());
  2338. ASSERT_EQ(values[i], "val" + std::to_string(i) + ",merge");
  2339. } else {
  2340. ASSERT_TRUE(statuses[i].ok());
  2341. if (values[i] != "val" + std::to_string(i)) {
  2342. ASSERT_EQ(values[i], "val" + std::to_string(i));
  2343. }
  2344. }
  2345. }
  2346. for (auto handle : handles) {
  2347. delete handle;
  2348. }
  2349. }
  2350. TEST_P(TransactionTest, ColumnFamiliesTest2) {
  2351. WriteOptions write_options;
  2352. ReadOptions read_options, snapshot_read_options;
  2353. string value;
  2354. Status s;
  2355. ColumnFamilyHandle *one, *two;
  2356. ColumnFamilyOptions cf_options;
  2357. // Create 2 new column families
  2358. s = db->CreateColumnFamily(cf_options, "ONE", &one);
  2359. ASSERT_OK(s);
  2360. s = db->CreateColumnFamily(cf_options, "TWO", &two);
  2361. ASSERT_OK(s);
  2362. Transaction* txn1 = db->BeginTransaction(write_options);
  2363. ASSERT_TRUE(txn1);
  2364. Transaction* txn2 = db->BeginTransaction(write_options);
  2365. ASSERT_TRUE(txn2);
  2366. s = txn1->Put(one, "X", "1");
  2367. ASSERT_OK(s);
  2368. s = txn1->Put(two, "X", "2");
  2369. ASSERT_OK(s);
  2370. s = txn1->Put("X", "0");
  2371. ASSERT_OK(s);
  2372. s = txn2->Put(one, "X", "11");
  2373. ASSERT_TRUE(s.IsTimedOut());
  2374. s = txn1->Commit();
  2375. ASSERT_OK(s);
  2376. // Drop first column family
  2377. s = db->DropColumnFamily(one);
  2378. ASSERT_OK(s);
  2379. // Should fail since column family was dropped.
  2380. s = txn2->Commit();
  2381. ASSERT_OK(s);
  2382. delete txn1;
  2383. txn1 = db->BeginTransaction(write_options);
  2384. ASSERT_TRUE(txn1);
  2385. // Should fail since column family was dropped
  2386. s = txn1->Put(one, "X", "111");
  2387. ASSERT_TRUE(s.IsInvalidArgument());
  2388. s = txn1->Put(two, "X", "222");
  2389. ASSERT_OK(s);
  2390. s = txn1->Put("X", "000");
  2391. ASSERT_OK(s);
  2392. s = txn1->Commit();
  2393. ASSERT_OK(s);
  2394. s = db->Get(read_options, two, "X", &value);
  2395. ASSERT_OK(s);
  2396. ASSERT_EQ("222", value);
  2397. s = db->Get(read_options, "X", &value);
  2398. ASSERT_OK(s);
  2399. ASSERT_EQ("000", value);
  2400. s = db->DropColumnFamily(two);
  2401. ASSERT_OK(s);
  2402. delete txn1;
  2403. delete txn2;
  2404. delete one;
  2405. delete two;
  2406. }
  2407. TEST_P(TransactionTest, EmptyTest) {
  2408. WriteOptions write_options;
  2409. ReadOptions read_options;
  2410. string value;
  2411. Status s;
  2412. s = db->Put(write_options, "aaa", "aaa");
  2413. ASSERT_OK(s);
  2414. Transaction* txn = db->BeginTransaction(write_options);
  2415. s = txn->Commit();
  2416. ASSERT_OK(s);
  2417. delete txn;
  2418. txn = db->BeginTransaction(write_options);
  2419. txn->Rollback();
  2420. delete txn;
  2421. txn = db->BeginTransaction(write_options);
  2422. s = txn->GetForUpdate(read_options, "aaa", &value);
  2423. ASSERT_EQ(value, "aaa");
  2424. s = txn->Commit();
  2425. ASSERT_OK(s);
  2426. delete txn;
  2427. txn = db->BeginTransaction(write_options);
  2428. txn->SetSnapshot();
  2429. s = txn->GetForUpdate(read_options, "aaa", &value);
  2430. ASSERT_EQ(value, "aaa");
  2431. // Conflicts with previous GetForUpdate
  2432. s = db->Put(write_options, "aaa", "xxx");
  2433. ASSERT_TRUE(s.IsTimedOut());
  2434. // transaction expired!
  2435. s = txn->Commit();
  2436. ASSERT_OK(s);
  2437. delete txn;
  2438. }
  2439. TEST_P(TransactionTest, PredicateManyPreceders) {
  2440. WriteOptions write_options;
  2441. ReadOptions read_options1, read_options2;
  2442. TransactionOptions txn_options;
  2443. string value;
  2444. Status s;
  2445. txn_options.set_snapshot = true;
  2446. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  2447. read_options1.snapshot = txn1->GetSnapshot();
  2448. Transaction* txn2 = db->BeginTransaction(write_options);
  2449. txn2->SetSnapshot();
  2450. read_options2.snapshot = txn2->GetSnapshot();
  2451. std::vector<Slice> multiget_keys = {"1", "2", "3"};
  2452. std::vector<std::string> multiget_values;
  2453. std::vector<Status> results =
  2454. txn1->MultiGetForUpdate(read_options1, multiget_keys, &multiget_values);
  2455. ASSERT_TRUE(results[1].IsNotFound());
  2456. s = txn2->Put("2", "x"); // Conflict's with txn1's MultiGetForUpdate
  2457. ASSERT_TRUE(s.IsTimedOut());
  2458. txn2->Rollback();
  2459. multiget_values.clear();
  2460. results =
  2461. txn1->MultiGetForUpdate(read_options1, multiget_keys, &multiget_values);
  2462. ASSERT_TRUE(results[1].IsNotFound());
  2463. s = txn1->Commit();
  2464. ASSERT_OK(s);
  2465. delete txn1;
  2466. delete txn2;
  2467. txn1 = db->BeginTransaction(write_options, txn_options);
  2468. read_options1.snapshot = txn1->GetSnapshot();
  2469. txn2 = db->BeginTransaction(write_options, txn_options);
  2470. read_options2.snapshot = txn2->GetSnapshot();
  2471. s = txn1->Put("4", "x");
  2472. ASSERT_OK(s);
  2473. s = txn2->Delete("4"); // conflict
  2474. ASSERT_TRUE(s.IsTimedOut());
  2475. s = txn1->Commit();
  2476. ASSERT_OK(s);
  2477. s = txn2->GetForUpdate(read_options2, "4", &value);
  2478. ASSERT_TRUE(s.IsBusy());
  2479. txn2->Rollback();
  2480. delete txn1;
  2481. delete txn2;
  2482. }
  2483. TEST_P(TransactionTest, LostUpdate) {
  2484. WriteOptions write_options;
  2485. ReadOptions read_options, read_options1, read_options2;
  2486. TransactionOptions txn_options;
  2487. std::string value;
  2488. Status s;
  2489. // Test 2 transactions writing to the same key in multiple orders and
  2490. // with/without snapshots
  2491. Transaction* txn1 = db->BeginTransaction(write_options);
  2492. Transaction* txn2 = db->BeginTransaction(write_options);
  2493. s = txn1->Put("1", "1");
  2494. ASSERT_OK(s);
  2495. s = txn2->Put("1", "2"); // conflict
  2496. ASSERT_TRUE(s.IsTimedOut());
  2497. s = txn2->Commit();
  2498. ASSERT_OK(s);
  2499. s = txn1->Commit();
  2500. ASSERT_OK(s);
  2501. s = db->Get(read_options, "1", &value);
  2502. ASSERT_OK(s);
  2503. ASSERT_EQ("1", value);
  2504. delete txn1;
  2505. delete txn2;
  2506. txn_options.set_snapshot = true;
  2507. txn1 = db->BeginTransaction(write_options, txn_options);
  2508. read_options1.snapshot = txn1->GetSnapshot();
  2509. txn2 = db->BeginTransaction(write_options, txn_options);
  2510. read_options2.snapshot = txn2->GetSnapshot();
  2511. s = txn1->Put("1", "3");
  2512. ASSERT_OK(s);
  2513. s = txn2->Put("1", "4"); // conflict
  2514. ASSERT_TRUE(s.IsTimedOut());
  2515. s = txn1->Commit();
  2516. ASSERT_OK(s);
  2517. s = txn2->Commit();
  2518. ASSERT_OK(s);
  2519. s = db->Get(read_options, "1", &value);
  2520. ASSERT_OK(s);
  2521. ASSERT_EQ("3", value);
  2522. delete txn1;
  2523. delete txn2;
  2524. txn1 = db->BeginTransaction(write_options, txn_options);
  2525. read_options1.snapshot = txn1->GetSnapshot();
  2526. txn2 = db->BeginTransaction(write_options, txn_options);
  2527. read_options2.snapshot = txn2->GetSnapshot();
  2528. s = txn1->Put("1", "5");
  2529. ASSERT_OK(s);
  2530. s = txn1->Commit();
  2531. ASSERT_OK(s);
  2532. s = txn2->Put("1", "6");
  2533. ASSERT_TRUE(s.IsBusy());
  2534. s = txn2->Commit();
  2535. ASSERT_OK(s);
  2536. s = db->Get(read_options, "1", &value);
  2537. ASSERT_OK(s);
  2538. ASSERT_EQ("5", value);
  2539. delete txn1;
  2540. delete txn2;
  2541. txn1 = db->BeginTransaction(write_options, txn_options);
  2542. read_options1.snapshot = txn1->GetSnapshot();
  2543. txn2 = db->BeginTransaction(write_options, txn_options);
  2544. read_options2.snapshot = txn2->GetSnapshot();
  2545. s = txn1->Put("1", "7");
  2546. ASSERT_OK(s);
  2547. s = txn1->Commit();
  2548. ASSERT_OK(s);
  2549. txn2->SetSnapshot();
  2550. s = txn2->Put("1", "8");
  2551. ASSERT_OK(s);
  2552. s = txn2->Commit();
  2553. ASSERT_OK(s);
  2554. s = db->Get(read_options, "1", &value);
  2555. ASSERT_OK(s);
  2556. ASSERT_EQ("8", value);
  2557. delete txn1;
  2558. delete txn2;
  2559. txn1 = db->BeginTransaction(write_options);
  2560. txn2 = db->BeginTransaction(write_options);
  2561. s = txn1->Put("1", "9");
  2562. ASSERT_OK(s);
  2563. s = txn1->Commit();
  2564. ASSERT_OK(s);
  2565. s = txn2->Put("1", "10");
  2566. ASSERT_OK(s);
  2567. s = txn2->Commit();
  2568. ASSERT_OK(s);
  2569. delete txn1;
  2570. delete txn2;
  2571. s = db->Get(read_options, "1", &value);
  2572. ASSERT_OK(s);
  2573. ASSERT_EQ(value, "10");
  2574. }
  2575. TEST_P(TransactionTest, UntrackedWrites) {
  2576. if (txn_db_options.write_policy == WRITE_UNPREPARED) {
  2577. // TODO(lth): For WriteUnprepared, validate that untracked writes are
  2578. // not supported.
  2579. return;
  2580. }
  2581. WriteOptions write_options;
  2582. ReadOptions read_options;
  2583. std::string value;
  2584. Status s;
  2585. // Verify transaction rollback works for untracked keys.
  2586. Transaction* txn = db->BeginTransaction(write_options);
  2587. txn->SetSnapshot();
  2588. s = txn->PutUntracked("untracked", "0");
  2589. ASSERT_OK(s);
  2590. txn->Rollback();
  2591. s = db->Get(read_options, "untracked", &value);
  2592. ASSERT_TRUE(s.IsNotFound());
  2593. delete txn;
  2594. txn = db->BeginTransaction(write_options);
  2595. txn->SetSnapshot();
  2596. s = db->Put(write_options, "untracked", "x");
  2597. ASSERT_OK(s);
  2598. // Untracked writes should succeed even though key was written after snapshot
  2599. s = txn->PutUntracked("untracked", "1");
  2600. ASSERT_OK(s);
  2601. s = txn->MergeUntracked("untracked", "2");
  2602. ASSERT_OK(s);
  2603. s = txn->DeleteUntracked("untracked");
  2604. ASSERT_OK(s);
  2605. // Conflict
  2606. s = txn->Put("untracked", "3");
  2607. ASSERT_TRUE(s.IsBusy());
  2608. s = txn->Commit();
  2609. ASSERT_OK(s);
  2610. s = db->Get(read_options, "untracked", &value);
  2611. ASSERT_TRUE(s.IsNotFound());
  2612. delete txn;
  2613. }
  2614. TEST_P(TransactionTest, ExpiredTransaction) {
  2615. WriteOptions write_options;
  2616. ReadOptions read_options;
  2617. TransactionOptions txn_options;
  2618. string value;
  2619. Status s;
  2620. // Set txn expiration timeout to 0 microseconds (expires instantly)
  2621. txn_options.expiration = 0;
  2622. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  2623. s = txn1->Put("X", "1");
  2624. ASSERT_OK(s);
  2625. s = txn1->Put("Y", "1");
  2626. ASSERT_OK(s);
  2627. Transaction* txn2 = db->BeginTransaction(write_options);
  2628. // txn2 should be able to write to X since txn1 has expired
  2629. s = txn2->Put("X", "2");
  2630. ASSERT_OK(s);
  2631. s = txn2->Commit();
  2632. ASSERT_OK(s);
  2633. s = db->Get(read_options, "X", &value);
  2634. ASSERT_OK(s);
  2635. ASSERT_EQ("2", value);
  2636. s = txn1->Put("Z", "1");
  2637. ASSERT_OK(s);
  2638. // txn1 should fail to commit since it is expired
  2639. s = txn1->Commit();
  2640. ASSERT_TRUE(s.IsExpired());
  2641. s = db->Get(read_options, "Y", &value);
  2642. ASSERT_TRUE(s.IsNotFound());
  2643. s = db->Get(read_options, "Z", &value);
  2644. ASSERT_TRUE(s.IsNotFound());
  2645. delete txn1;
  2646. delete txn2;
  2647. }
  2648. TEST_P(TransactionTest, ReinitializeTest) {
  2649. WriteOptions write_options;
  2650. ReadOptions read_options;
  2651. TransactionOptions txn_options;
  2652. std::string value;
  2653. Status s;
  2654. // Set txn expiration timeout to 0 microseconds (expires instantly)
  2655. txn_options.expiration = 0;
  2656. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  2657. // Reinitialize transaction to no long expire
  2658. txn_options.expiration = -1;
  2659. txn1 = db->BeginTransaction(write_options, txn_options, txn1);
  2660. s = txn1->Put("Z", "z");
  2661. ASSERT_OK(s);
  2662. // Should commit since not expired
  2663. s = txn1->Commit();
  2664. ASSERT_OK(s);
  2665. txn1 = db->BeginTransaction(write_options, txn_options, txn1);
  2666. s = txn1->Put("Z", "zz");
  2667. ASSERT_OK(s);
  2668. // Reinitilize txn1 and verify that Z gets unlocked
  2669. txn1 = db->BeginTransaction(write_options, txn_options, txn1);
  2670. Transaction* txn2 = db->BeginTransaction(write_options, txn_options, nullptr);
  2671. s = txn2->Put("Z", "zzz");
  2672. ASSERT_OK(s);
  2673. s = txn2->Commit();
  2674. ASSERT_OK(s);
  2675. delete txn2;
  2676. s = db->Get(read_options, "Z", &value);
  2677. ASSERT_OK(s);
  2678. ASSERT_EQ(value, "zzz");
  2679. // Verify snapshots get reinitialized correctly
  2680. txn1->SetSnapshot();
  2681. s = txn1->Put("Z", "zzzz");
  2682. ASSERT_OK(s);
  2683. s = txn1->Commit();
  2684. ASSERT_OK(s);
  2685. s = db->Get(read_options, "Z", &value);
  2686. ASSERT_OK(s);
  2687. ASSERT_EQ(value, "zzzz");
  2688. txn1 = db->BeginTransaction(write_options, txn_options, txn1);
  2689. const Snapshot* snapshot = txn1->GetSnapshot();
  2690. ASSERT_FALSE(snapshot);
  2691. txn_options.set_snapshot = true;
  2692. txn1 = db->BeginTransaction(write_options, txn_options, txn1);
  2693. snapshot = txn1->GetSnapshot();
  2694. ASSERT_TRUE(snapshot);
  2695. s = txn1->Put("Z", "a");
  2696. ASSERT_OK(s);
  2697. txn1->Rollback();
  2698. s = txn1->Put("Y", "y");
  2699. ASSERT_OK(s);
  2700. txn_options.set_snapshot = false;
  2701. txn1 = db->BeginTransaction(write_options, txn_options, txn1);
  2702. snapshot = txn1->GetSnapshot();
  2703. ASSERT_FALSE(snapshot);
  2704. s = txn1->Put("X", "x");
  2705. ASSERT_OK(s);
  2706. s = txn1->Commit();
  2707. ASSERT_OK(s);
  2708. s = db->Get(read_options, "Z", &value);
  2709. ASSERT_OK(s);
  2710. ASSERT_EQ(value, "zzzz");
  2711. s = db->Get(read_options, "Y", &value);
  2712. ASSERT_TRUE(s.IsNotFound());
  2713. txn1 = db->BeginTransaction(write_options, txn_options, txn1);
  2714. s = txn1->SetName("name");
  2715. ASSERT_OK(s);
  2716. s = txn1->Prepare();
  2717. ASSERT_OK(s);
  2718. s = txn1->Commit();
  2719. ASSERT_OK(s);
  2720. txn1 = db->BeginTransaction(write_options, txn_options, txn1);
  2721. s = txn1->SetName("name");
  2722. ASSERT_OK(s);
  2723. delete txn1;
  2724. }
  2725. TEST_P(TransactionTest, Rollback) {
  2726. WriteOptions write_options;
  2727. ReadOptions read_options;
  2728. TransactionOptions txn_options;
  2729. std::string value;
  2730. Status s;
  2731. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  2732. ASSERT_OK(s);
  2733. s = txn1->Put("X", "1");
  2734. ASSERT_OK(s);
  2735. Transaction* txn2 = db->BeginTransaction(write_options);
  2736. // txn2 should not be able to write to X since txn1 has it locked
  2737. s = txn2->Put("X", "2");
  2738. ASSERT_TRUE(s.IsTimedOut());
  2739. txn1->Rollback();
  2740. delete txn1;
  2741. // txn2 should now be able to write to X
  2742. s = txn2->Put("X", "3");
  2743. ASSERT_OK(s);
  2744. s = txn2->Commit();
  2745. ASSERT_OK(s);
  2746. s = db->Get(read_options, "X", &value);
  2747. ASSERT_OK(s);
  2748. ASSERT_EQ("3", value);
  2749. delete txn2;
  2750. }
  2751. TEST_P(TransactionTest, LockLimitTest) {
  2752. WriteOptions write_options;
  2753. ReadOptions read_options, snapshot_read_options;
  2754. TransactionOptions txn_options;
  2755. string value;
  2756. Status s;
  2757. delete db;
  2758. db = nullptr;
  2759. // Open DB with a lock limit of 3
  2760. txn_db_options.max_num_locks = 3;
  2761. ASSERT_OK(ReOpen());
  2762. assert(db != nullptr);
  2763. ASSERT_OK(s);
  2764. // Create a txn and verify we can only lock up to 3 keys
  2765. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  2766. ASSERT_TRUE(txn);
  2767. s = txn->Put("X", "x");
  2768. ASSERT_OK(s);
  2769. s = txn->Put("Y", "y");
  2770. ASSERT_OK(s);
  2771. s = txn->Put("Z", "z");
  2772. ASSERT_OK(s);
  2773. // lock limit reached
  2774. s = txn->Put("W", "w");
  2775. ASSERT_TRUE(s.IsBusy());
  2776. // re-locking same key shouldn't put us over the limit
  2777. s = txn->Put("X", "xx");
  2778. ASSERT_OK(s);
  2779. s = txn->GetForUpdate(read_options, "W", &value);
  2780. ASSERT_TRUE(s.IsBusy());
  2781. s = txn->GetForUpdate(read_options, "V", &value);
  2782. ASSERT_TRUE(s.IsBusy());
  2783. // re-locking same key shouldn't put us over the limit
  2784. s = txn->GetForUpdate(read_options, "Y", &value);
  2785. ASSERT_OK(s);
  2786. ASSERT_EQ("y", value);
  2787. s = txn->Get(read_options, "W", &value);
  2788. ASSERT_TRUE(s.IsNotFound());
  2789. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  2790. ASSERT_TRUE(txn2);
  2791. // "X" currently locked
  2792. s = txn2->Put("X", "x");
  2793. ASSERT_TRUE(s.IsTimedOut());
  2794. // lock limit reached
  2795. s = txn2->Put("M", "m");
  2796. ASSERT_TRUE(s.IsBusy());
  2797. s = txn->Commit();
  2798. ASSERT_OK(s);
  2799. s = db->Get(read_options, "X", &value);
  2800. ASSERT_OK(s);
  2801. ASSERT_EQ("xx", value);
  2802. s = db->Get(read_options, "W", &value);
  2803. ASSERT_TRUE(s.IsNotFound());
  2804. // Committing txn should release its locks and allow txn2 to proceed
  2805. s = txn2->Put("X", "x2");
  2806. ASSERT_OK(s);
  2807. s = txn2->Delete("X");
  2808. ASSERT_OK(s);
  2809. s = txn2->Put("M", "m");
  2810. ASSERT_OK(s);
  2811. s = txn2->Put("Z", "z2");
  2812. ASSERT_OK(s);
  2813. // lock limit reached
  2814. s = txn2->Delete("Y");
  2815. ASSERT_TRUE(s.IsBusy());
  2816. s = txn2->Commit();
  2817. ASSERT_OK(s);
  2818. s = db->Get(read_options, "Z", &value);
  2819. ASSERT_OK(s);
  2820. ASSERT_EQ("z2", value);
  2821. s = db->Get(read_options, "Y", &value);
  2822. ASSERT_OK(s);
  2823. ASSERT_EQ("y", value);
  2824. s = db->Get(read_options, "X", &value);
  2825. ASSERT_TRUE(s.IsNotFound());
  2826. delete txn;
  2827. delete txn2;
  2828. }
  2829. TEST_P(TransactionTest, IteratorTest) {
  2830. // This test does writes without snapshot validation, and then tries to create
  2831. // iterator later, which is unsupported in write unprepared.
  2832. if (txn_db_options.write_policy == WRITE_UNPREPARED) {
  2833. return;
  2834. }
  2835. WriteOptions write_options;
  2836. ReadOptions read_options, snapshot_read_options;
  2837. std::string value;
  2838. Status s;
  2839. // Write some keys to the db
  2840. s = db->Put(write_options, "A", "a");
  2841. ASSERT_OK(s);
  2842. s = db->Put(write_options, "G", "g");
  2843. ASSERT_OK(s);
  2844. s = db->Put(write_options, "F", "f");
  2845. ASSERT_OK(s);
  2846. s = db->Put(write_options, "C", "c");
  2847. ASSERT_OK(s);
  2848. s = db->Put(write_options, "D", "d");
  2849. ASSERT_OK(s);
  2850. Transaction* txn = db->BeginTransaction(write_options);
  2851. ASSERT_TRUE(txn);
  2852. // Write some keys in a txn
  2853. s = txn->Put("B", "b");
  2854. ASSERT_OK(s);
  2855. s = txn->Put("H", "h");
  2856. ASSERT_OK(s);
  2857. s = txn->Delete("D");
  2858. ASSERT_OK(s);
  2859. s = txn->Put("E", "e");
  2860. ASSERT_OK(s);
  2861. txn->SetSnapshot();
  2862. const Snapshot* snapshot = txn->GetSnapshot();
  2863. // Write some keys to the db after the snapshot
  2864. s = db->Put(write_options, "BB", "xx");
  2865. ASSERT_OK(s);
  2866. s = db->Put(write_options, "C", "xx");
  2867. ASSERT_OK(s);
  2868. read_options.snapshot = snapshot;
  2869. Iterator* iter = txn->GetIterator(read_options);
  2870. ASSERT_OK(iter->status());
  2871. iter->SeekToFirst();
  2872. // Read all keys via iter and lock them all
  2873. std::string results[] = {"a", "b", "c", "e", "f", "g", "h"};
  2874. for (int i = 0; i < 7; i++) {
  2875. ASSERT_OK(iter->status());
  2876. ASSERT_TRUE(iter->Valid());
  2877. ASSERT_EQ(results[i], iter->value().ToString());
  2878. s = txn->GetForUpdate(read_options, iter->key(), nullptr);
  2879. if (i == 2) {
  2880. // "C" was modified after txn's snapshot
  2881. ASSERT_TRUE(s.IsBusy());
  2882. } else {
  2883. ASSERT_OK(s);
  2884. }
  2885. iter->Next();
  2886. }
  2887. ASSERT_FALSE(iter->Valid());
  2888. iter->Seek("G");
  2889. ASSERT_OK(iter->status());
  2890. ASSERT_TRUE(iter->Valid());
  2891. ASSERT_EQ("g", iter->value().ToString());
  2892. iter->Prev();
  2893. ASSERT_OK(iter->status());
  2894. ASSERT_TRUE(iter->Valid());
  2895. ASSERT_EQ("f", iter->value().ToString());
  2896. iter->Seek("D");
  2897. ASSERT_OK(iter->status());
  2898. ASSERT_TRUE(iter->Valid());
  2899. ASSERT_EQ("e", iter->value().ToString());
  2900. iter->Seek("C");
  2901. ASSERT_OK(iter->status());
  2902. ASSERT_TRUE(iter->Valid());
  2903. ASSERT_EQ("c", iter->value().ToString());
  2904. iter->Next();
  2905. ASSERT_OK(iter->status());
  2906. ASSERT_TRUE(iter->Valid());
  2907. ASSERT_EQ("e", iter->value().ToString());
  2908. iter->Seek("");
  2909. ASSERT_OK(iter->status());
  2910. ASSERT_TRUE(iter->Valid());
  2911. ASSERT_EQ("a", iter->value().ToString());
  2912. iter->Seek("X");
  2913. ASSERT_OK(iter->status());
  2914. ASSERT_FALSE(iter->Valid());
  2915. iter->SeekToLast();
  2916. ASSERT_OK(iter->status());
  2917. ASSERT_TRUE(iter->Valid());
  2918. ASSERT_EQ("h", iter->value().ToString());
  2919. s = txn->Commit();
  2920. ASSERT_OK(s);
  2921. delete iter;
  2922. delete txn;
  2923. }
  2924. TEST_P(TransactionTest, DisableIndexingTest) {
  2925. // Skip this test for write unprepared. It does not solely rely on WBWI for
  2926. // read your own writes, so depending on whether batches are flushed or not,
  2927. // only some writes will be visible.
  2928. //
  2929. // Also, write unprepared does not support creating iterators if there has
  2930. // been txn->Put() without snapshot validation.
  2931. if (txn_db_options.write_policy == WRITE_UNPREPARED) {
  2932. return;
  2933. }
  2934. WriteOptions write_options;
  2935. ReadOptions read_options;
  2936. std::string value;
  2937. Status s;
  2938. Transaction* txn = db->BeginTransaction(write_options);
  2939. ASSERT_TRUE(txn);
  2940. s = txn->Put("A", "a");
  2941. ASSERT_OK(s);
  2942. s = txn->Get(read_options, "A", &value);
  2943. ASSERT_OK(s);
  2944. ASSERT_EQ("a", value);
  2945. txn->DisableIndexing();
  2946. s = txn->Put("B", "b");
  2947. ASSERT_OK(s);
  2948. s = txn->Get(read_options, "B", &value);
  2949. ASSERT_TRUE(s.IsNotFound());
  2950. Iterator* iter = txn->GetIterator(read_options);
  2951. ASSERT_OK(iter->status());
  2952. iter->Seek("B");
  2953. ASSERT_OK(iter->status());
  2954. ASSERT_FALSE(iter->Valid());
  2955. s = txn->Delete("A");
  2956. s = txn->Get(read_options, "A", &value);
  2957. ASSERT_OK(s);
  2958. ASSERT_EQ("a", value);
  2959. txn->EnableIndexing();
  2960. s = txn->Put("B", "bb");
  2961. ASSERT_OK(s);
  2962. iter->Seek("B");
  2963. ASSERT_OK(iter->status());
  2964. ASSERT_TRUE(iter->Valid());
  2965. ASSERT_EQ("bb", iter->value().ToString());
  2966. s = txn->Get(read_options, "B", &value);
  2967. ASSERT_OK(s);
  2968. ASSERT_EQ("bb", value);
  2969. s = txn->Put("A", "aa");
  2970. ASSERT_OK(s);
  2971. s = txn->Get(read_options, "A", &value);
  2972. ASSERT_OK(s);
  2973. ASSERT_EQ("aa", value);
  2974. delete iter;
  2975. delete txn;
  2976. }
  2977. TEST_P(TransactionTest, SavepointTest) {
  2978. WriteOptions write_options;
  2979. ReadOptions read_options, snapshot_read_options;
  2980. std::string value;
  2981. Status s;
  2982. Transaction* txn = db->BeginTransaction(write_options);
  2983. ASSERT_TRUE(txn);
  2984. ASSERT_EQ(0, txn->GetNumPuts());
  2985. s = txn->RollbackToSavePoint();
  2986. ASSERT_TRUE(s.IsNotFound());
  2987. txn->SetSavePoint(); // 1
  2988. ASSERT_OK(txn->RollbackToSavePoint()); // Rollback to beginning of txn
  2989. s = txn->RollbackToSavePoint();
  2990. ASSERT_TRUE(s.IsNotFound());
  2991. s = txn->Put("B", "b");
  2992. ASSERT_OK(s);
  2993. ASSERT_EQ(1, txn->GetNumPuts());
  2994. ASSERT_EQ(0, txn->GetNumDeletes());
  2995. s = txn->Commit();
  2996. ASSERT_OK(s);
  2997. s = db->Get(read_options, "B", &value);
  2998. ASSERT_OK(s);
  2999. ASSERT_EQ("b", value);
  3000. delete txn;
  3001. txn = db->BeginTransaction(write_options);
  3002. ASSERT_TRUE(txn);
  3003. s = txn->Put("A", "a");
  3004. ASSERT_OK(s);
  3005. s = txn->Put("B", "bb");
  3006. ASSERT_OK(s);
  3007. s = txn->Put("C", "c");
  3008. ASSERT_OK(s);
  3009. txn->SetSavePoint(); // 2
  3010. s = txn->Delete("B");
  3011. ASSERT_OK(s);
  3012. s = txn->Put("C", "cc");
  3013. ASSERT_OK(s);
  3014. s = txn->Put("D", "d");
  3015. ASSERT_OK(s);
  3016. ASSERT_EQ(5, txn->GetNumPuts());
  3017. ASSERT_EQ(1, txn->GetNumDeletes());
  3018. ASSERT_OK(txn->RollbackToSavePoint()); // Rollback to 2
  3019. ASSERT_EQ(3, txn->GetNumPuts());
  3020. ASSERT_EQ(0, txn->GetNumDeletes());
  3021. s = txn->Get(read_options, "A", &value);
  3022. ASSERT_OK(s);
  3023. ASSERT_EQ("a", value);
  3024. s = txn->Get(read_options, "B", &value);
  3025. ASSERT_OK(s);
  3026. ASSERT_EQ("bb", value);
  3027. s = txn->Get(read_options, "C", &value);
  3028. ASSERT_OK(s);
  3029. ASSERT_EQ("c", value);
  3030. s = txn->Get(read_options, "D", &value);
  3031. ASSERT_TRUE(s.IsNotFound());
  3032. s = txn->Put("A", "a");
  3033. ASSERT_OK(s);
  3034. s = txn->Put("E", "e");
  3035. ASSERT_OK(s);
  3036. ASSERT_EQ(5, txn->GetNumPuts());
  3037. ASSERT_EQ(0, txn->GetNumDeletes());
  3038. // Rollback to beginning of txn
  3039. s = txn->RollbackToSavePoint();
  3040. ASSERT_TRUE(s.IsNotFound());
  3041. txn->Rollback();
  3042. ASSERT_EQ(0, txn->GetNumPuts());
  3043. ASSERT_EQ(0, txn->GetNumDeletes());
  3044. s = txn->Get(read_options, "A", &value);
  3045. ASSERT_TRUE(s.IsNotFound());
  3046. s = txn->Get(read_options, "B", &value);
  3047. ASSERT_OK(s);
  3048. ASSERT_EQ("b", value);
  3049. s = txn->Get(read_options, "D", &value);
  3050. ASSERT_TRUE(s.IsNotFound());
  3051. s = txn->Get(read_options, "D", &value);
  3052. ASSERT_TRUE(s.IsNotFound());
  3053. s = txn->Get(read_options, "E", &value);
  3054. ASSERT_TRUE(s.IsNotFound());
  3055. s = txn->Put("A", "aa");
  3056. ASSERT_OK(s);
  3057. s = txn->Put("F", "f");
  3058. ASSERT_OK(s);
  3059. ASSERT_EQ(2, txn->GetNumPuts());
  3060. ASSERT_EQ(0, txn->GetNumDeletes());
  3061. txn->SetSavePoint(); // 3
  3062. txn->SetSavePoint(); // 4
  3063. s = txn->Put("G", "g");
  3064. ASSERT_OK(s);
  3065. s = txn->SingleDelete("F");
  3066. ASSERT_OK(s);
  3067. s = txn->Delete("B");
  3068. ASSERT_OK(s);
  3069. s = txn->Get(read_options, "A", &value);
  3070. ASSERT_OK(s);
  3071. ASSERT_EQ("aa", value);
  3072. s = txn->Get(read_options, "F", &value);
  3073. // According to db.h, doing a SingleDelete on a key that has been
  3074. // overwritten will have undefinied behavior. So it is unclear what the
  3075. // result of fetching "F" should be. The current implementation will
  3076. // return NotFound in this case.
  3077. ASSERT_TRUE(s.IsNotFound());
  3078. s = txn->Get(read_options, "B", &value);
  3079. ASSERT_TRUE(s.IsNotFound());
  3080. ASSERT_EQ(3, txn->GetNumPuts());
  3081. ASSERT_EQ(2, txn->GetNumDeletes());
  3082. ASSERT_OK(txn->RollbackToSavePoint()); // Rollback to 3
  3083. ASSERT_EQ(2, txn->GetNumPuts());
  3084. ASSERT_EQ(0, txn->GetNumDeletes());
  3085. s = txn->Get(read_options, "F", &value);
  3086. ASSERT_OK(s);
  3087. ASSERT_EQ("f", value);
  3088. s = txn->Get(read_options, "G", &value);
  3089. ASSERT_TRUE(s.IsNotFound());
  3090. s = txn->Commit();
  3091. ASSERT_OK(s);
  3092. s = db->Get(read_options, "F", &value);
  3093. ASSERT_OK(s);
  3094. ASSERT_EQ("f", value);
  3095. s = db->Get(read_options, "G", &value);
  3096. ASSERT_TRUE(s.IsNotFound());
  3097. s = db->Get(read_options, "A", &value);
  3098. ASSERT_OK(s);
  3099. ASSERT_EQ("aa", value);
  3100. s = db->Get(read_options, "B", &value);
  3101. ASSERT_OK(s);
  3102. ASSERT_EQ("b", value);
  3103. s = db->Get(read_options, "C", &value);
  3104. ASSERT_TRUE(s.IsNotFound());
  3105. s = db->Get(read_options, "D", &value);
  3106. ASSERT_TRUE(s.IsNotFound());
  3107. s = db->Get(read_options, "E", &value);
  3108. ASSERT_TRUE(s.IsNotFound());
  3109. delete txn;
  3110. }
  3111. TEST_P(TransactionTest, SavepointTest2) {
  3112. WriteOptions write_options;
  3113. ReadOptions read_options;
  3114. TransactionOptions txn_options;
  3115. Status s;
  3116. txn_options.lock_timeout = 1; // 1 ms
  3117. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  3118. ASSERT_TRUE(txn1);
  3119. s = txn1->Put("A", "");
  3120. ASSERT_OK(s);
  3121. txn1->SetSavePoint(); // 1
  3122. s = txn1->Put("A", "a");
  3123. ASSERT_OK(s);
  3124. s = txn1->Put("C", "c");
  3125. ASSERT_OK(s);
  3126. txn1->SetSavePoint(); // 2
  3127. s = txn1->Put("A", "a");
  3128. ASSERT_OK(s);
  3129. s = txn1->Put("B", "b");
  3130. ASSERT_OK(s);
  3131. ASSERT_OK(txn1->RollbackToSavePoint()); // Rollback to 2
  3132. // Verify that "A" and "C" is still locked while "B" is not
  3133. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  3134. ASSERT_TRUE(txn2);
  3135. s = txn2->Put("A", "a2");
  3136. ASSERT_TRUE(s.IsTimedOut());
  3137. s = txn2->Put("C", "c2");
  3138. ASSERT_TRUE(s.IsTimedOut());
  3139. s = txn2->Put("B", "b2");
  3140. ASSERT_OK(s);
  3141. s = txn1->Put("A", "aa");
  3142. ASSERT_OK(s);
  3143. s = txn1->Put("B", "bb");
  3144. ASSERT_TRUE(s.IsTimedOut());
  3145. s = txn2->Commit();
  3146. ASSERT_OK(s);
  3147. delete txn2;
  3148. s = txn1->Put("A", "aaa");
  3149. ASSERT_OK(s);
  3150. s = txn1->Put("B", "bbb");
  3151. ASSERT_OK(s);
  3152. s = txn1->Put("C", "ccc");
  3153. ASSERT_OK(s);
  3154. txn1->SetSavePoint(); // 3
  3155. ASSERT_OK(txn1->RollbackToSavePoint()); // Rollback to 3
  3156. // Verify that "A", "B", "C" are still locked
  3157. txn2 = db->BeginTransaction(write_options, txn_options);
  3158. ASSERT_TRUE(txn2);
  3159. s = txn2->Put("A", "a2");
  3160. ASSERT_TRUE(s.IsTimedOut());
  3161. s = txn2->Put("B", "b2");
  3162. ASSERT_TRUE(s.IsTimedOut());
  3163. s = txn2->Put("C", "c2");
  3164. ASSERT_TRUE(s.IsTimedOut());
  3165. ASSERT_OK(txn1->RollbackToSavePoint()); // Rollback to 1
  3166. // Verify that only "A" is locked
  3167. s = txn2->Put("A", "a3");
  3168. ASSERT_TRUE(s.IsTimedOut());
  3169. s = txn2->Put("B", "b3");
  3170. ASSERT_OK(s);
  3171. s = txn2->Put("C", "c3po");
  3172. ASSERT_OK(s);
  3173. s = txn1->Commit();
  3174. ASSERT_OK(s);
  3175. delete txn1;
  3176. // Verify "A" "C" "B" are no longer locked
  3177. s = txn2->Put("A", "a4");
  3178. ASSERT_OK(s);
  3179. s = txn2->Put("B", "b4");
  3180. ASSERT_OK(s);
  3181. s = txn2->Put("C", "c4");
  3182. ASSERT_OK(s);
  3183. s = txn2->Commit();
  3184. ASSERT_OK(s);
  3185. delete txn2;
  3186. }
  3187. TEST_P(TransactionTest, SavepointTest3) {
  3188. WriteOptions write_options;
  3189. ReadOptions read_options;
  3190. TransactionOptions txn_options;
  3191. Status s;
  3192. txn_options.lock_timeout = 1; // 1 ms
  3193. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  3194. ASSERT_TRUE(txn1);
  3195. s = txn1->PopSavePoint(); // No SavePoint present
  3196. ASSERT_TRUE(s.IsNotFound());
  3197. s = txn1->Put("A", "");
  3198. ASSERT_OK(s);
  3199. s = txn1->PopSavePoint(); // Still no SavePoint present
  3200. ASSERT_TRUE(s.IsNotFound());
  3201. txn1->SetSavePoint(); // 1
  3202. s = txn1->Put("A", "a");
  3203. ASSERT_OK(s);
  3204. s = txn1->PopSavePoint(); // Remove 1
  3205. ASSERT_TRUE(txn1->RollbackToSavePoint().IsNotFound());
  3206. // Verify that "A" is still locked
  3207. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  3208. ASSERT_TRUE(txn2);
  3209. s = txn2->Put("A", "a2");
  3210. ASSERT_TRUE(s.IsTimedOut());
  3211. delete txn2;
  3212. txn1->SetSavePoint(); // 2
  3213. s = txn1->Put("B", "b");
  3214. ASSERT_OK(s);
  3215. txn1->SetSavePoint(); // 3
  3216. s = txn1->Put("B", "b2");
  3217. ASSERT_OK(s);
  3218. ASSERT_OK(txn1->RollbackToSavePoint()); // Roll back to 2
  3219. s = txn1->PopSavePoint();
  3220. ASSERT_OK(s);
  3221. s = txn1->PopSavePoint();
  3222. ASSERT_TRUE(s.IsNotFound());
  3223. s = txn1->Commit();
  3224. ASSERT_OK(s);
  3225. delete txn1;
  3226. std::string value;
  3227. // tnx1 should have modified "A" to "a"
  3228. s = db->Get(read_options, "A", &value);
  3229. ASSERT_OK(s);
  3230. ASSERT_EQ("a", value);
  3231. // tnx1 should have set "B" to just "b"
  3232. s = db->Get(read_options, "B", &value);
  3233. ASSERT_OK(s);
  3234. ASSERT_EQ("b", value);
  3235. s = db->Get(read_options, "C", &value);
  3236. ASSERT_TRUE(s.IsNotFound());
  3237. }
  3238. TEST_P(TransactionTest, SavepointTest4) {
  3239. WriteOptions write_options;
  3240. ReadOptions read_options;
  3241. TransactionOptions txn_options;
  3242. Status s;
  3243. txn_options.lock_timeout = 1; // 1 ms
  3244. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  3245. ASSERT_TRUE(txn1);
  3246. txn1->SetSavePoint(); // 1
  3247. s = txn1->Put("A", "a");
  3248. ASSERT_OK(s);
  3249. txn1->SetSavePoint(); // 2
  3250. s = txn1->Put("B", "b");
  3251. ASSERT_OK(s);
  3252. s = txn1->PopSavePoint(); // Remove 2
  3253. ASSERT_OK(s);
  3254. // Verify that A/B still exists.
  3255. std::string value;
  3256. ASSERT_OK(txn1->Get(read_options, "A", &value));
  3257. ASSERT_EQ("a", value);
  3258. ASSERT_OK(txn1->Get(read_options, "B", &value));
  3259. ASSERT_EQ("b", value);
  3260. ASSERT_OK(txn1->RollbackToSavePoint()); // Rollback to 1
  3261. // Verify that everything was rolled back.
  3262. s = txn1->Get(read_options, "A", &value);
  3263. ASSERT_TRUE(s.IsNotFound());
  3264. s = txn1->Get(read_options, "B", &value);
  3265. ASSERT_TRUE(s.IsNotFound());
  3266. // Nothing should be locked
  3267. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  3268. ASSERT_TRUE(txn2);
  3269. s = txn2->Put("A", "");
  3270. ASSERT_OK(s);
  3271. s = txn2->Put("B", "");
  3272. ASSERT_OK(s);
  3273. delete txn2;
  3274. delete txn1;
  3275. }
  3276. TEST_P(TransactionTest, UndoGetForUpdateTest) {
  3277. WriteOptions write_options;
  3278. ReadOptions read_options;
  3279. TransactionOptions txn_options;
  3280. std::string value;
  3281. Status s;
  3282. txn_options.lock_timeout = 1; // 1 ms
  3283. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  3284. ASSERT_TRUE(txn1);
  3285. txn1->UndoGetForUpdate("A");
  3286. s = txn1->Commit();
  3287. ASSERT_OK(s);
  3288. delete txn1;
  3289. txn1 = db->BeginTransaction(write_options, txn_options);
  3290. txn1->UndoGetForUpdate("A");
  3291. s = txn1->GetForUpdate(read_options, "A", &value);
  3292. ASSERT_TRUE(s.IsNotFound());
  3293. // Verify that A is locked
  3294. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  3295. s = txn2->Put("A", "a");
  3296. ASSERT_TRUE(s.IsTimedOut());
  3297. txn1->UndoGetForUpdate("A");
  3298. // Verify that A is now unlocked
  3299. s = txn2->Put("A", "a2");
  3300. ASSERT_OK(s);
  3301. txn2->Commit();
  3302. delete txn2;
  3303. s = db->Get(read_options, "A", &value);
  3304. ASSERT_OK(s);
  3305. ASSERT_EQ("a2", value);
  3306. s = txn1->Delete("A");
  3307. ASSERT_OK(s);
  3308. s = txn1->GetForUpdate(read_options, "A", &value);
  3309. ASSERT_TRUE(s.IsNotFound());
  3310. s = txn1->Put("B", "b3");
  3311. ASSERT_OK(s);
  3312. s = txn1->GetForUpdate(read_options, "B", &value);
  3313. ASSERT_OK(s);
  3314. txn1->UndoGetForUpdate("A");
  3315. txn1->UndoGetForUpdate("B");
  3316. // Verify that A and B are still locked
  3317. txn2 = db->BeginTransaction(write_options, txn_options);
  3318. s = txn2->Put("A", "a4");
  3319. ASSERT_TRUE(s.IsTimedOut());
  3320. s = txn2->Put("B", "b4");
  3321. ASSERT_TRUE(s.IsTimedOut());
  3322. txn1->Rollback();
  3323. delete txn1;
  3324. // Verify that A and B are no longer locked
  3325. s = txn2->Put("A", "a5");
  3326. ASSERT_OK(s);
  3327. s = txn2->Put("B", "b5");
  3328. ASSERT_OK(s);
  3329. s = txn2->Commit();
  3330. delete txn2;
  3331. ASSERT_OK(s);
  3332. txn1 = db->BeginTransaction(write_options, txn_options);
  3333. s = txn1->GetForUpdate(read_options, "A", &value);
  3334. ASSERT_OK(s);
  3335. s = txn1->GetForUpdate(read_options, "A", &value);
  3336. ASSERT_OK(s);
  3337. s = txn1->GetForUpdate(read_options, "C", &value);
  3338. ASSERT_TRUE(s.IsNotFound());
  3339. s = txn1->GetForUpdate(read_options, "A", &value);
  3340. ASSERT_OK(s);
  3341. s = txn1->GetForUpdate(read_options, "C", &value);
  3342. ASSERT_TRUE(s.IsNotFound());
  3343. s = txn1->GetForUpdate(read_options, "B", &value);
  3344. ASSERT_OK(s);
  3345. s = txn1->Put("B", "b5");
  3346. s = txn1->GetForUpdate(read_options, "B", &value);
  3347. ASSERT_OK(s);
  3348. txn1->UndoGetForUpdate("A");
  3349. txn1->UndoGetForUpdate("B");
  3350. txn1->UndoGetForUpdate("C");
  3351. txn1->UndoGetForUpdate("X");
  3352. // Verify A,B,C are locked
  3353. txn2 = db->BeginTransaction(write_options, txn_options);
  3354. s = txn2->Put("A", "a6");
  3355. ASSERT_TRUE(s.IsTimedOut());
  3356. s = txn2->Delete("B");
  3357. ASSERT_TRUE(s.IsTimedOut());
  3358. s = txn2->Put("C", "c6");
  3359. ASSERT_TRUE(s.IsTimedOut());
  3360. s = txn2->Put("X", "x6");
  3361. ASSERT_OK(s);
  3362. txn1->UndoGetForUpdate("A");
  3363. txn1->UndoGetForUpdate("B");
  3364. txn1->UndoGetForUpdate("C");
  3365. txn1->UndoGetForUpdate("X");
  3366. // Verify A,B are locked and C is not
  3367. s = txn2->Put("A", "a6");
  3368. ASSERT_TRUE(s.IsTimedOut());
  3369. s = txn2->Delete("B");
  3370. ASSERT_TRUE(s.IsTimedOut());
  3371. s = txn2->Put("C", "c6");
  3372. ASSERT_OK(s);
  3373. s = txn2->Put("X", "x6");
  3374. ASSERT_OK(s);
  3375. txn1->UndoGetForUpdate("A");
  3376. txn1->UndoGetForUpdate("B");
  3377. txn1->UndoGetForUpdate("C");
  3378. txn1->UndoGetForUpdate("X");
  3379. // Verify B is locked and A and C are not
  3380. s = txn2->Put("A", "a7");
  3381. ASSERT_OK(s);
  3382. s = txn2->Delete("B");
  3383. ASSERT_TRUE(s.IsTimedOut());
  3384. s = txn2->Put("C", "c7");
  3385. ASSERT_OK(s);
  3386. s = txn2->Put("X", "x7");
  3387. ASSERT_OK(s);
  3388. s = txn2->Commit();
  3389. ASSERT_OK(s);
  3390. delete txn2;
  3391. s = txn1->Commit();
  3392. ASSERT_OK(s);
  3393. delete txn1;
  3394. }
  3395. TEST_P(TransactionTest, UndoGetForUpdateTest2) {
  3396. WriteOptions write_options;
  3397. ReadOptions read_options;
  3398. TransactionOptions txn_options;
  3399. std::string value;
  3400. Status s;
  3401. s = db->Put(write_options, "A", "");
  3402. ASSERT_OK(s);
  3403. txn_options.lock_timeout = 1; // 1 ms
  3404. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  3405. ASSERT_TRUE(txn1);
  3406. s = txn1->GetForUpdate(read_options, "A", &value);
  3407. ASSERT_OK(s);
  3408. s = txn1->GetForUpdate(read_options, "B", &value);
  3409. ASSERT_TRUE(s.IsNotFound());
  3410. s = txn1->Put("F", "f");
  3411. ASSERT_OK(s);
  3412. txn1->SetSavePoint(); // 1
  3413. txn1->UndoGetForUpdate("A");
  3414. s = txn1->GetForUpdate(read_options, "C", &value);
  3415. ASSERT_TRUE(s.IsNotFound());
  3416. s = txn1->GetForUpdate(read_options, "D", &value);
  3417. ASSERT_TRUE(s.IsNotFound());
  3418. s = txn1->Put("E", "e");
  3419. ASSERT_OK(s);
  3420. s = txn1->GetForUpdate(read_options, "E", &value);
  3421. ASSERT_OK(s);
  3422. s = txn1->GetForUpdate(read_options, "F", &value);
  3423. ASSERT_OK(s);
  3424. // Verify A,B,C,D,E,F are still locked
  3425. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  3426. s = txn2->Put("A", "a1");
  3427. ASSERT_TRUE(s.IsTimedOut());
  3428. s = txn2->Put("B", "b1");
  3429. ASSERT_TRUE(s.IsTimedOut());
  3430. s = txn2->Put("C", "c1");
  3431. ASSERT_TRUE(s.IsTimedOut());
  3432. s = txn2->Put("D", "d1");
  3433. ASSERT_TRUE(s.IsTimedOut());
  3434. s = txn2->Put("E", "e1");
  3435. ASSERT_TRUE(s.IsTimedOut());
  3436. s = txn2->Put("F", "f1");
  3437. ASSERT_TRUE(s.IsTimedOut());
  3438. txn1->UndoGetForUpdate("C");
  3439. txn1->UndoGetForUpdate("E");
  3440. // Verify A,B,D,E,F are still locked and C is not.
  3441. s = txn2->Put("A", "a2");
  3442. ASSERT_TRUE(s.IsTimedOut());
  3443. s = txn2->Put("B", "b2");
  3444. ASSERT_TRUE(s.IsTimedOut());
  3445. s = txn2->Put("D", "d2");
  3446. ASSERT_TRUE(s.IsTimedOut());
  3447. s = txn2->Put("E", "e2");
  3448. ASSERT_TRUE(s.IsTimedOut());
  3449. s = txn2->Put("F", "f2");
  3450. ASSERT_TRUE(s.IsTimedOut());
  3451. s = txn2->Put("C", "c2");
  3452. ASSERT_OK(s);
  3453. txn1->SetSavePoint(); // 2
  3454. s = txn1->Put("H", "h");
  3455. ASSERT_OK(s);
  3456. txn1->UndoGetForUpdate("A");
  3457. txn1->UndoGetForUpdate("B");
  3458. txn1->UndoGetForUpdate("C");
  3459. txn1->UndoGetForUpdate("D");
  3460. txn1->UndoGetForUpdate("E");
  3461. txn1->UndoGetForUpdate("F");
  3462. txn1->UndoGetForUpdate("G");
  3463. txn1->UndoGetForUpdate("H");
  3464. // Verify A,B,D,E,F,H are still locked and C,G are not.
  3465. s = txn2->Put("A", "a3");
  3466. ASSERT_TRUE(s.IsTimedOut());
  3467. s = txn2->Put("B", "b3");
  3468. ASSERT_TRUE(s.IsTimedOut());
  3469. s = txn2->Put("D", "d3");
  3470. ASSERT_TRUE(s.IsTimedOut());
  3471. s = txn2->Put("E", "e3");
  3472. ASSERT_TRUE(s.IsTimedOut());
  3473. s = txn2->Put("F", "f3");
  3474. ASSERT_TRUE(s.IsTimedOut());
  3475. s = txn2->Put("H", "h3");
  3476. ASSERT_TRUE(s.IsTimedOut());
  3477. s = txn2->Put("C", "c3");
  3478. ASSERT_OK(s);
  3479. s = txn2->Put("G", "g3");
  3480. ASSERT_OK(s);
  3481. txn1->RollbackToSavePoint(); // rollback to 2
  3482. // Verify A,B,D,E,F are still locked and C,G,H are not.
  3483. s = txn2->Put("A", "a3");
  3484. ASSERT_TRUE(s.IsTimedOut());
  3485. s = txn2->Put("B", "b3");
  3486. ASSERT_TRUE(s.IsTimedOut());
  3487. s = txn2->Put("D", "d3");
  3488. ASSERT_TRUE(s.IsTimedOut());
  3489. s = txn2->Put("E", "e3");
  3490. ASSERT_TRUE(s.IsTimedOut());
  3491. s = txn2->Put("F", "f3");
  3492. ASSERT_TRUE(s.IsTimedOut());
  3493. s = txn2->Put("C", "c3");
  3494. ASSERT_OK(s);
  3495. s = txn2->Put("G", "g3");
  3496. ASSERT_OK(s);
  3497. s = txn2->Put("H", "h3");
  3498. ASSERT_OK(s);
  3499. txn1->UndoGetForUpdate("A");
  3500. txn1->UndoGetForUpdate("B");
  3501. txn1->UndoGetForUpdate("C");
  3502. txn1->UndoGetForUpdate("D");
  3503. txn1->UndoGetForUpdate("E");
  3504. txn1->UndoGetForUpdate("F");
  3505. txn1->UndoGetForUpdate("G");
  3506. txn1->UndoGetForUpdate("H");
  3507. // Verify A,B,E,F are still locked and C,D,G,H are not.
  3508. s = txn2->Put("A", "a3");
  3509. ASSERT_TRUE(s.IsTimedOut());
  3510. s = txn2->Put("B", "b3");
  3511. ASSERT_TRUE(s.IsTimedOut());
  3512. s = txn2->Put("E", "e3");
  3513. ASSERT_TRUE(s.IsTimedOut());
  3514. s = txn2->Put("F", "f3");
  3515. ASSERT_TRUE(s.IsTimedOut());
  3516. s = txn2->Put("C", "c3");
  3517. ASSERT_OK(s);
  3518. s = txn2->Put("D", "d3");
  3519. ASSERT_OK(s);
  3520. s = txn2->Put("G", "g3");
  3521. ASSERT_OK(s);
  3522. s = txn2->Put("H", "h3");
  3523. ASSERT_OK(s);
  3524. txn1->RollbackToSavePoint(); // rollback to 1
  3525. // Verify A,B,F are still locked and C,D,E,G,H are not.
  3526. s = txn2->Put("A", "a3");
  3527. ASSERT_TRUE(s.IsTimedOut());
  3528. s = txn2->Put("B", "b3");
  3529. ASSERT_TRUE(s.IsTimedOut());
  3530. s = txn2->Put("F", "f3");
  3531. ASSERT_TRUE(s.IsTimedOut());
  3532. s = txn2->Put("C", "c3");
  3533. ASSERT_OK(s);
  3534. s = txn2->Put("D", "d3");
  3535. ASSERT_OK(s);
  3536. s = txn2->Put("E", "e3");
  3537. ASSERT_OK(s);
  3538. s = txn2->Put("G", "g3");
  3539. ASSERT_OK(s);
  3540. s = txn2->Put("H", "h3");
  3541. ASSERT_OK(s);
  3542. txn1->UndoGetForUpdate("A");
  3543. txn1->UndoGetForUpdate("B");
  3544. txn1->UndoGetForUpdate("C");
  3545. txn1->UndoGetForUpdate("D");
  3546. txn1->UndoGetForUpdate("E");
  3547. txn1->UndoGetForUpdate("F");
  3548. txn1->UndoGetForUpdate("G");
  3549. txn1->UndoGetForUpdate("H");
  3550. // Verify F is still locked and A,B,C,D,E,G,H are not.
  3551. s = txn2->Put("F", "f3");
  3552. ASSERT_TRUE(s.IsTimedOut());
  3553. s = txn2->Put("A", "a3");
  3554. ASSERT_OK(s);
  3555. s = txn2->Put("B", "b3");
  3556. ASSERT_OK(s);
  3557. s = txn2->Put("C", "c3");
  3558. ASSERT_OK(s);
  3559. s = txn2->Put("D", "d3");
  3560. ASSERT_OK(s);
  3561. s = txn2->Put("E", "e3");
  3562. ASSERT_OK(s);
  3563. s = txn2->Put("G", "g3");
  3564. ASSERT_OK(s);
  3565. s = txn2->Put("H", "h3");
  3566. ASSERT_OK(s);
  3567. s = txn1->Commit();
  3568. ASSERT_OK(s);
  3569. s = txn2->Commit();
  3570. ASSERT_OK(s);
  3571. delete txn1;
  3572. delete txn2;
  3573. }
  3574. TEST_P(TransactionTest, TimeoutTest) {
  3575. WriteOptions write_options;
  3576. ReadOptions read_options;
  3577. std::string value;
  3578. Status s;
  3579. delete db;
  3580. db = nullptr;
  3581. // transaction writes have an infinite timeout,
  3582. // but we will override this when we start a txn
  3583. // db writes have infinite timeout
  3584. txn_db_options.transaction_lock_timeout = -1;
  3585. txn_db_options.default_lock_timeout = -1;
  3586. s = TransactionDB::Open(options, txn_db_options, dbname, &db);
  3587. assert(db != nullptr);
  3588. ASSERT_OK(s);
  3589. s = db->Put(write_options, "aaa", "aaa");
  3590. ASSERT_OK(s);
  3591. TransactionOptions txn_options0;
  3592. txn_options0.expiration = 100; // 100ms
  3593. txn_options0.lock_timeout = 50; // txn timeout no longer infinite
  3594. Transaction* txn1 = db->BeginTransaction(write_options, txn_options0);
  3595. s = txn1->GetForUpdate(read_options, "aaa", nullptr);
  3596. ASSERT_OK(s);
  3597. // Conflicts with previous GetForUpdate.
  3598. // Since db writes do not have a timeout, this should eventually succeed when
  3599. // the transaction expires.
  3600. s = db->Put(write_options, "aaa", "xxx");
  3601. ASSERT_OK(s);
  3602. ASSERT_GE(txn1->GetElapsedTime(),
  3603. static_cast<uint64_t>(txn_options0.expiration));
  3604. s = txn1->Commit();
  3605. ASSERT_TRUE(s.IsExpired()); // expired!
  3606. s = db->Get(read_options, "aaa", &value);
  3607. ASSERT_OK(s);
  3608. ASSERT_EQ("xxx", value);
  3609. delete txn1;
  3610. delete db;
  3611. // transaction writes have 10ms timeout,
  3612. // db writes have infinite timeout
  3613. txn_db_options.transaction_lock_timeout = 50;
  3614. txn_db_options.default_lock_timeout = -1;
  3615. s = TransactionDB::Open(options, txn_db_options, dbname, &db);
  3616. ASSERT_OK(s);
  3617. s = db->Put(write_options, "aaa", "aaa");
  3618. ASSERT_OK(s);
  3619. TransactionOptions txn_options;
  3620. txn_options.expiration = 100; // 100ms
  3621. txn1 = db->BeginTransaction(write_options, txn_options);
  3622. s = txn1->GetForUpdate(read_options, "aaa", nullptr);
  3623. ASSERT_OK(s);
  3624. // Conflicts with previous GetForUpdate.
  3625. // Since db writes do not have a timeout, this should eventually succeed when
  3626. // the transaction expires.
  3627. s = db->Put(write_options, "aaa", "xxx");
  3628. ASSERT_OK(s);
  3629. s = txn1->Commit();
  3630. ASSERT_NOK(s); // expired!
  3631. s = db->Get(read_options, "aaa", &value);
  3632. ASSERT_OK(s);
  3633. ASSERT_EQ("xxx", value);
  3634. delete txn1;
  3635. txn_options.expiration = 6000000; // 100 minutes
  3636. txn_options.lock_timeout = 1; // 1ms
  3637. txn1 = db->BeginTransaction(write_options, txn_options);
  3638. txn1->SetLockTimeout(100);
  3639. TransactionOptions txn_options2;
  3640. txn_options2.expiration = 10; // 10ms
  3641. Transaction* txn2 = db->BeginTransaction(write_options, txn_options2);
  3642. ASSERT_OK(s);
  3643. s = txn2->Put("a", "2");
  3644. ASSERT_OK(s);
  3645. // txn1 has a lock timeout longer than txn2's expiration, so it will win
  3646. s = txn1->Delete("a");
  3647. ASSERT_OK(s);
  3648. s = txn1->Commit();
  3649. ASSERT_OK(s);
  3650. // txn2 should be expired out since txn1 waiting until its timeout expired.
  3651. s = txn2->Commit();
  3652. ASSERT_TRUE(s.IsExpired());
  3653. delete txn1;
  3654. delete txn2;
  3655. txn_options.expiration = 6000000; // 100 minutes
  3656. txn1 = db->BeginTransaction(write_options, txn_options);
  3657. txn_options2.expiration = 100000000;
  3658. txn2 = db->BeginTransaction(write_options, txn_options2);
  3659. s = txn1->Delete("asdf");
  3660. ASSERT_OK(s);
  3661. // txn2 has a smaller lock timeout than txn1's expiration, so it will time out
  3662. s = txn2->Delete("asdf");
  3663. ASSERT_TRUE(s.IsTimedOut());
  3664. ASSERT_EQ(s.ToString(), "Operation timed out: Timeout waiting to lock key");
  3665. s = txn1->Commit();
  3666. ASSERT_OK(s);
  3667. s = txn2->Put("asdf", "asdf");
  3668. ASSERT_OK(s);
  3669. s = txn2->Commit();
  3670. ASSERT_OK(s);
  3671. s = db->Get(read_options, "asdf", &value);
  3672. ASSERT_OK(s);
  3673. ASSERT_EQ("asdf", value);
  3674. delete txn1;
  3675. delete txn2;
  3676. }
  3677. TEST_P(TransactionTest, SingleDeleteTest) {
  3678. WriteOptions write_options;
  3679. ReadOptions read_options;
  3680. std::string value;
  3681. Status s;
  3682. Transaction* txn = db->BeginTransaction(write_options);
  3683. ASSERT_TRUE(txn);
  3684. s = txn->SingleDelete("A");
  3685. ASSERT_OK(s);
  3686. s = txn->Get(read_options, "A", &value);
  3687. ASSERT_TRUE(s.IsNotFound());
  3688. s = txn->Commit();
  3689. ASSERT_OK(s);
  3690. delete txn;
  3691. txn = db->BeginTransaction(write_options);
  3692. s = txn->SingleDelete("A");
  3693. ASSERT_OK(s);
  3694. s = txn->Put("A", "a");
  3695. ASSERT_OK(s);
  3696. s = txn->Get(read_options, "A", &value);
  3697. ASSERT_OK(s);
  3698. ASSERT_EQ("a", value);
  3699. s = txn->Commit();
  3700. ASSERT_OK(s);
  3701. delete txn;
  3702. s = db->Get(read_options, "A", &value);
  3703. ASSERT_OK(s);
  3704. ASSERT_EQ("a", value);
  3705. txn = db->BeginTransaction(write_options);
  3706. s = txn->SingleDelete("A");
  3707. ASSERT_OK(s);
  3708. s = txn->Get(read_options, "A", &value);
  3709. ASSERT_TRUE(s.IsNotFound());
  3710. s = txn->Commit();
  3711. ASSERT_OK(s);
  3712. delete txn;
  3713. s = db->Get(read_options, "A", &value);
  3714. ASSERT_TRUE(s.IsNotFound());
  3715. txn = db->BeginTransaction(write_options);
  3716. Transaction* txn2 = db->BeginTransaction(write_options);
  3717. txn2->SetSnapshot();
  3718. s = txn->Put("A", "a");
  3719. ASSERT_OK(s);
  3720. s = txn->Put("A", "a2");
  3721. ASSERT_OK(s);
  3722. s = txn->SingleDelete("A");
  3723. ASSERT_OK(s);
  3724. s = txn->SingleDelete("B");
  3725. ASSERT_OK(s);
  3726. // According to db.h, doing a SingleDelete on a key that has been
  3727. // overwritten will have undefinied behavior. So it is unclear what the
  3728. // result of fetching "A" should be. The current implementation will
  3729. // return NotFound in this case.
  3730. s = txn->Get(read_options, "A", &value);
  3731. ASSERT_TRUE(s.IsNotFound());
  3732. s = txn2->Put("B", "b");
  3733. ASSERT_TRUE(s.IsTimedOut());
  3734. s = txn2->Commit();
  3735. ASSERT_OK(s);
  3736. delete txn2;
  3737. s = txn->Commit();
  3738. ASSERT_OK(s);
  3739. delete txn;
  3740. // According to db.h, doing a SingleDelete on a key that has been
  3741. // overwritten will have undefinied behavior. So it is unclear what the
  3742. // result of fetching "A" should be. The current implementation will
  3743. // return NotFound in this case.
  3744. s = db->Get(read_options, "A", &value);
  3745. ASSERT_TRUE(s.IsNotFound());
  3746. s = db->Get(read_options, "B", &value);
  3747. ASSERT_TRUE(s.IsNotFound());
  3748. }
  3749. TEST_P(TransactionTest, MergeTest) {
  3750. WriteOptions write_options;
  3751. ReadOptions read_options;
  3752. std::string value;
  3753. Status s;
  3754. Transaction* txn = db->BeginTransaction(write_options, TransactionOptions());
  3755. ASSERT_TRUE(txn);
  3756. s = db->Put(write_options, "A", "a0");
  3757. ASSERT_OK(s);
  3758. s = txn->Merge("A", "1");
  3759. ASSERT_OK(s);
  3760. s = txn->Merge("A", "2");
  3761. ASSERT_OK(s);
  3762. s = txn->Get(read_options, "A", &value);
  3763. ASSERT_TRUE(s.IsMergeInProgress());
  3764. s = txn->Put("A", "a");
  3765. ASSERT_OK(s);
  3766. s = txn->Get(read_options, "A", &value);
  3767. ASSERT_OK(s);
  3768. ASSERT_EQ("a", value);
  3769. s = txn->Merge("A", "3");
  3770. ASSERT_OK(s);
  3771. s = txn->Get(read_options, "A", &value);
  3772. ASSERT_TRUE(s.IsMergeInProgress());
  3773. TransactionOptions txn_options;
  3774. txn_options.lock_timeout = 1; // 1 ms
  3775. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  3776. ASSERT_TRUE(txn2);
  3777. // verify that txn has "A" locked
  3778. s = txn2->Merge("A", "4");
  3779. ASSERT_TRUE(s.IsTimedOut());
  3780. s = txn2->Commit();
  3781. ASSERT_OK(s);
  3782. delete txn2;
  3783. s = txn->Commit();
  3784. ASSERT_OK(s);
  3785. delete txn;
  3786. s = db->Get(read_options, "A", &value);
  3787. ASSERT_OK(s);
  3788. ASSERT_EQ("a,3", value);
  3789. }
  3790. TEST_P(TransactionTest, DeferSnapshotTest) {
  3791. WriteOptions write_options;
  3792. ReadOptions read_options;
  3793. std::string value;
  3794. Status s;
  3795. s = db->Put(write_options, "A", "a0");
  3796. ASSERT_OK(s);
  3797. Transaction* txn1 = db->BeginTransaction(write_options);
  3798. Transaction* txn2 = db->BeginTransaction(write_options);
  3799. txn1->SetSnapshotOnNextOperation();
  3800. auto snapshot = txn1->GetSnapshot();
  3801. ASSERT_FALSE(snapshot);
  3802. s = txn2->Put("A", "a2");
  3803. ASSERT_OK(s);
  3804. s = txn2->Commit();
  3805. ASSERT_OK(s);
  3806. delete txn2;
  3807. s = txn1->GetForUpdate(read_options, "A", &value);
  3808. // Should not conflict with txn2 since snapshot wasn't set until
  3809. // GetForUpdate was called.
  3810. ASSERT_OK(s);
  3811. ASSERT_EQ("a2", value);
  3812. s = txn1->Put("A", "a1");
  3813. ASSERT_OK(s);
  3814. s = db->Put(write_options, "B", "b0");
  3815. ASSERT_OK(s);
  3816. // Cannot lock B since it was written after the snapshot was set
  3817. s = txn1->Put("B", "b1");
  3818. ASSERT_TRUE(s.IsBusy());
  3819. s = txn1->Commit();
  3820. ASSERT_OK(s);
  3821. delete txn1;
  3822. s = db->Get(read_options, "A", &value);
  3823. ASSERT_OK(s);
  3824. ASSERT_EQ("a1", value);
  3825. s = db->Get(read_options, "B", &value);
  3826. ASSERT_OK(s);
  3827. ASSERT_EQ("b0", value);
  3828. }
  3829. TEST_P(TransactionTest, DeferSnapshotTest2) {
  3830. WriteOptions write_options;
  3831. ReadOptions read_options, snapshot_read_options;
  3832. std::string value;
  3833. Status s;
  3834. Transaction* txn1 = db->BeginTransaction(write_options);
  3835. txn1->SetSnapshot();
  3836. s = txn1->Put("A", "a1");
  3837. ASSERT_OK(s);
  3838. s = db->Put(write_options, "C", "c0");
  3839. ASSERT_OK(s);
  3840. s = db->Put(write_options, "D", "d0");
  3841. ASSERT_OK(s);
  3842. snapshot_read_options.snapshot = txn1->GetSnapshot();
  3843. txn1->SetSnapshotOnNextOperation();
  3844. s = txn1->Get(snapshot_read_options, "C", &value);
  3845. // Snapshot was set before C was written
  3846. ASSERT_TRUE(s.IsNotFound());
  3847. s = txn1->Get(snapshot_read_options, "D", &value);
  3848. // Snapshot was set before D was written
  3849. ASSERT_TRUE(s.IsNotFound());
  3850. // Snapshot should not have changed yet.
  3851. snapshot_read_options.snapshot = txn1->GetSnapshot();
  3852. s = txn1->Get(snapshot_read_options, "C", &value);
  3853. // Snapshot was set before C was written
  3854. ASSERT_TRUE(s.IsNotFound());
  3855. s = txn1->Get(snapshot_read_options, "D", &value);
  3856. // Snapshot was set before D was written
  3857. ASSERT_TRUE(s.IsNotFound());
  3858. s = txn1->GetForUpdate(read_options, "C", &value);
  3859. ASSERT_OK(s);
  3860. ASSERT_EQ("c0", value);
  3861. s = db->Put(write_options, "D", "d00");
  3862. ASSERT_OK(s);
  3863. // Snapshot is now set
  3864. snapshot_read_options.snapshot = txn1->GetSnapshot();
  3865. s = txn1->Get(snapshot_read_options, "D", &value);
  3866. ASSERT_OK(s);
  3867. ASSERT_EQ("d0", value);
  3868. s = txn1->Commit();
  3869. ASSERT_OK(s);
  3870. delete txn1;
  3871. }
  3872. TEST_P(TransactionTest, DeferSnapshotSavePointTest) {
  3873. WriteOptions write_options;
  3874. ReadOptions read_options, snapshot_read_options;
  3875. std::string value;
  3876. Status s;
  3877. Transaction* txn1 = db->BeginTransaction(write_options);
  3878. txn1->SetSavePoint(); // 1
  3879. s = db->Put(write_options, "T", "1");
  3880. ASSERT_OK(s);
  3881. txn1->SetSnapshotOnNextOperation();
  3882. s = db->Put(write_options, "T", "2");
  3883. ASSERT_OK(s);
  3884. txn1->SetSavePoint(); // 2
  3885. s = db->Put(write_options, "T", "3");
  3886. ASSERT_OK(s);
  3887. s = txn1->Put("A", "a");
  3888. ASSERT_OK(s);
  3889. txn1->SetSavePoint(); // 3
  3890. s = db->Put(write_options, "T", "4");
  3891. ASSERT_OK(s);
  3892. txn1->SetSnapshot();
  3893. txn1->SetSnapshotOnNextOperation();
  3894. txn1->SetSavePoint(); // 4
  3895. s = db->Put(write_options, "T", "5");
  3896. ASSERT_OK(s);
  3897. snapshot_read_options.snapshot = txn1->GetSnapshot();
  3898. s = txn1->Get(snapshot_read_options, "T", &value);
  3899. ASSERT_OK(s);
  3900. ASSERT_EQ("4", value);
  3901. s = txn1->Put("A", "a1");
  3902. ASSERT_OK(s);
  3903. snapshot_read_options.snapshot = txn1->GetSnapshot();
  3904. s = txn1->Get(snapshot_read_options, "T", &value);
  3905. ASSERT_OK(s);
  3906. ASSERT_EQ("5", value);
  3907. s = txn1->RollbackToSavePoint(); // Rollback to 4
  3908. ASSERT_OK(s);
  3909. snapshot_read_options.snapshot = txn1->GetSnapshot();
  3910. s = txn1->Get(snapshot_read_options, "T", &value);
  3911. ASSERT_OK(s);
  3912. ASSERT_EQ("4", value);
  3913. s = txn1->RollbackToSavePoint(); // Rollback to 3
  3914. ASSERT_OK(s);
  3915. snapshot_read_options.snapshot = txn1->GetSnapshot();
  3916. s = txn1->Get(snapshot_read_options, "T", &value);
  3917. ASSERT_OK(s);
  3918. ASSERT_EQ("3", value);
  3919. s = txn1->Get(read_options, "T", &value);
  3920. ASSERT_OK(s);
  3921. ASSERT_EQ("5", value);
  3922. s = txn1->RollbackToSavePoint(); // Rollback to 2
  3923. ASSERT_OK(s);
  3924. snapshot_read_options.snapshot = txn1->GetSnapshot();
  3925. ASSERT_FALSE(snapshot_read_options.snapshot);
  3926. s = txn1->Get(snapshot_read_options, "T", &value);
  3927. ASSERT_OK(s);
  3928. ASSERT_EQ("5", value);
  3929. s = txn1->Delete("A");
  3930. ASSERT_OK(s);
  3931. snapshot_read_options.snapshot = txn1->GetSnapshot();
  3932. ASSERT_TRUE(snapshot_read_options.snapshot);
  3933. s = txn1->Get(snapshot_read_options, "T", &value);
  3934. ASSERT_OK(s);
  3935. ASSERT_EQ("5", value);
  3936. s = txn1->RollbackToSavePoint(); // Rollback to 1
  3937. ASSERT_OK(s);
  3938. s = txn1->Delete("A");
  3939. ASSERT_OK(s);
  3940. snapshot_read_options.snapshot = txn1->GetSnapshot();
  3941. ASSERT_FALSE(snapshot_read_options.snapshot);
  3942. s = txn1->Get(snapshot_read_options, "T", &value);
  3943. ASSERT_OK(s);
  3944. ASSERT_EQ("5", value);
  3945. s = txn1->Commit();
  3946. ASSERT_OK(s);
  3947. delete txn1;
  3948. }
  3949. TEST_P(TransactionTest, SetSnapshotOnNextOperationWithNotification) {
  3950. WriteOptions write_options;
  3951. ReadOptions read_options;
  3952. std::string value;
  3953. class Notifier : public TransactionNotifier {
  3954. private:
  3955. const Snapshot** snapshot_ptr_;
  3956. public:
  3957. explicit Notifier(const Snapshot** snapshot_ptr)
  3958. : snapshot_ptr_(snapshot_ptr) {}
  3959. void SnapshotCreated(const Snapshot* newSnapshot) override {
  3960. *snapshot_ptr_ = newSnapshot;
  3961. }
  3962. };
  3963. std::shared_ptr<Notifier> notifier =
  3964. std::make_shared<Notifier>(&read_options.snapshot);
  3965. Status s;
  3966. s = db->Put(write_options, "B", "0");
  3967. ASSERT_OK(s);
  3968. Transaction* txn1 = db->BeginTransaction(write_options);
  3969. txn1->SetSnapshotOnNextOperation(notifier);
  3970. ASSERT_FALSE(read_options.snapshot);
  3971. s = db->Put(write_options, "B", "1");
  3972. ASSERT_OK(s);
  3973. // A Get does not generate the snapshot
  3974. s = txn1->Get(read_options, "B", &value);
  3975. ASSERT_OK(s);
  3976. ASSERT_FALSE(read_options.snapshot);
  3977. ASSERT_EQ(value, "1");
  3978. // Any other operation does
  3979. s = txn1->Put("A", "0");
  3980. ASSERT_OK(s);
  3981. // Now change "B".
  3982. s = db->Put(write_options, "B", "2");
  3983. ASSERT_OK(s);
  3984. // The original value should still be read
  3985. s = txn1->Get(read_options, "B", &value);
  3986. ASSERT_OK(s);
  3987. ASSERT_TRUE(read_options.snapshot);
  3988. ASSERT_EQ(value, "1");
  3989. s = txn1->Commit();
  3990. ASSERT_OK(s);
  3991. delete txn1;
  3992. }
  3993. TEST_P(TransactionTest, ClearSnapshotTest) {
  3994. WriteOptions write_options;
  3995. ReadOptions read_options, snapshot_read_options;
  3996. std::string value;
  3997. Status s;
  3998. s = db->Put(write_options, "foo", "0");
  3999. ASSERT_OK(s);
  4000. Transaction* txn = db->BeginTransaction(write_options);
  4001. ASSERT_TRUE(txn);
  4002. s = db->Put(write_options, "foo", "1");
  4003. ASSERT_OK(s);
  4004. snapshot_read_options.snapshot = txn->GetSnapshot();
  4005. ASSERT_FALSE(snapshot_read_options.snapshot);
  4006. // No snapshot created yet
  4007. s = txn->Get(snapshot_read_options, "foo", &value);
  4008. ASSERT_EQ(value, "1");
  4009. txn->SetSnapshot();
  4010. snapshot_read_options.snapshot = txn->GetSnapshot();
  4011. ASSERT_TRUE(snapshot_read_options.snapshot);
  4012. s = db->Put(write_options, "foo", "2");
  4013. ASSERT_OK(s);
  4014. // Snapshot was created before change to '2'
  4015. s = txn->Get(snapshot_read_options, "foo", &value);
  4016. ASSERT_EQ(value, "1");
  4017. txn->ClearSnapshot();
  4018. snapshot_read_options.snapshot = txn->GetSnapshot();
  4019. ASSERT_FALSE(snapshot_read_options.snapshot);
  4020. // Snapshot has now been cleared
  4021. s = txn->Get(snapshot_read_options, "foo", &value);
  4022. ASSERT_EQ(value, "2");
  4023. s = txn->Commit();
  4024. ASSERT_OK(s);
  4025. delete txn;
  4026. }
  4027. TEST_P(TransactionTest, ToggleAutoCompactionTest) {
  4028. Status s;
  4029. ColumnFamilyHandle *cfa, *cfb;
  4030. ColumnFamilyOptions cf_options;
  4031. // Create 2 new column families
  4032. s = db->CreateColumnFamily(cf_options, "CFA", &cfa);
  4033. ASSERT_OK(s);
  4034. s = db->CreateColumnFamily(cf_options, "CFB", &cfb);
  4035. ASSERT_OK(s);
  4036. delete cfa;
  4037. delete cfb;
  4038. delete db;
  4039. // open DB with three column families
  4040. std::vector<ColumnFamilyDescriptor> column_families;
  4041. // have to open default column family
  4042. column_families.push_back(
  4043. ColumnFamilyDescriptor(kDefaultColumnFamilyName, ColumnFamilyOptions()));
  4044. // open the new column families
  4045. column_families.push_back(
  4046. ColumnFamilyDescriptor("CFA", ColumnFamilyOptions()));
  4047. column_families.push_back(
  4048. ColumnFamilyDescriptor("CFB", ColumnFamilyOptions()));
  4049. ColumnFamilyOptions* cf_opt_default = &column_families[0].options;
  4050. ColumnFamilyOptions* cf_opt_cfa = &column_families[1].options;
  4051. ColumnFamilyOptions* cf_opt_cfb = &column_families[2].options;
  4052. cf_opt_default->disable_auto_compactions = false;
  4053. cf_opt_cfa->disable_auto_compactions = true;
  4054. cf_opt_cfb->disable_auto_compactions = false;
  4055. std::vector<ColumnFamilyHandle*> handles;
  4056. s = TransactionDB::Open(options, txn_db_options, dbname, column_families,
  4057. &handles, &db);
  4058. ASSERT_OK(s);
  4059. auto cfh_default = reinterpret_cast<ColumnFamilyHandleImpl*>(handles[0]);
  4060. auto opt_default = *cfh_default->cfd()->GetLatestMutableCFOptions();
  4061. auto cfh_a = reinterpret_cast<ColumnFamilyHandleImpl*>(handles[1]);
  4062. auto opt_a = *cfh_a->cfd()->GetLatestMutableCFOptions();
  4063. auto cfh_b = reinterpret_cast<ColumnFamilyHandleImpl*>(handles[2]);
  4064. auto opt_b = *cfh_b->cfd()->GetLatestMutableCFOptions();
  4065. ASSERT_EQ(opt_default.disable_auto_compactions, false);
  4066. ASSERT_EQ(opt_a.disable_auto_compactions, true);
  4067. ASSERT_EQ(opt_b.disable_auto_compactions, false);
  4068. for (auto handle : handles) {
  4069. delete handle;
  4070. }
  4071. }
  4072. TEST_P(TransactionStressTest, ExpiredTransactionDataRace1) {
  4073. // In this test, txn1 should succeed committing,
  4074. // as the callback is called after txn1 starts committing.
  4075. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
  4076. {{"TransactionTest::ExpirableTransactionDataRace:1"}});
  4077. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
  4078. "TransactionTest::ExpirableTransactionDataRace:1", [&](void* /*arg*/) {
  4079. WriteOptions write_options;
  4080. TransactionOptions txn_options;
  4081. // Force txn1 to expire
  4082. /* sleep override */
  4083. std::this_thread::sleep_for(std::chrono::milliseconds(150));
  4084. Transaction* txn2 = db->BeginTransaction(write_options, txn_options);
  4085. Status s;
  4086. s = txn2->Put("X", "2");
  4087. ASSERT_TRUE(s.IsTimedOut());
  4088. s = txn2->Commit();
  4089. ASSERT_OK(s);
  4090. delete txn2;
  4091. });
  4092. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  4093. WriteOptions write_options;
  4094. TransactionOptions txn_options;
  4095. txn_options.expiration = 100;
  4096. Transaction* txn1 = db->BeginTransaction(write_options, txn_options);
  4097. Status s;
  4098. s = txn1->Put("X", "1");
  4099. ASSERT_OK(s);
  4100. s = txn1->Commit();
  4101. ASSERT_OK(s);
  4102. ReadOptions read_options;
  4103. string value;
  4104. s = db->Get(read_options, "X", &value);
  4105. ASSERT_EQ("1", value);
  4106. delete txn1;
  4107. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  4108. }
  4109. #ifndef ROCKSDB_VALGRIND_RUN
  4110. namespace {
  4111. // cmt_delay_ms is the delay between prepare and commit
  4112. // first_id is the id of the first transaction
  4113. Status TransactionStressTestInserter(
  4114. TransactionDB* db, const size_t num_transactions, const size_t num_sets,
  4115. const size_t num_keys_per_set, Random64* rand,
  4116. const uint64_t cmt_delay_ms = 0, const uint64_t first_id = 0) {
  4117. WriteOptions write_options;
  4118. ReadOptions read_options;
  4119. TransactionOptions txn_options;
  4120. if (rand->OneIn(2)) {
  4121. txn_options.use_only_the_last_commit_time_batch_for_recovery = true;
  4122. }
  4123. // Inside the inserter we might also retake the snapshot. We do both since two
  4124. // separte functions are engaged for each.
  4125. txn_options.set_snapshot = rand->OneIn(2);
  4126. RandomTransactionInserter inserter(
  4127. rand, write_options, read_options, num_keys_per_set,
  4128. static_cast<uint16_t>(num_sets), cmt_delay_ms, first_id);
  4129. for (size_t t = 0; t < num_transactions; t++) {
  4130. bool success = inserter.TransactionDBInsert(db, txn_options);
  4131. if (!success) {
  4132. // unexpected failure
  4133. return inserter.GetLastStatus();
  4134. }
  4135. }
  4136. // Make sure at least some of the transactions succeeded. It's ok if
  4137. // some failed due to write-conflicts.
  4138. if (num_transactions != 1 &&
  4139. inserter.GetFailureCount() > num_transactions / 2) {
  4140. return Status::TryAgain("Too many transactions failed! " +
  4141. std::to_string(inserter.GetFailureCount()) + " / " +
  4142. std::to_string(num_transactions));
  4143. }
  4144. return Status::OK();
  4145. }
  4146. } // namespace
  4147. // Worker threads add a number to a key from each set of keys. The checker
  4148. // threads verify that the sum of all keys in each set are equal.
  4149. TEST_P(MySQLStyleTransactionTest, TransactionStressTest) {
  4150. // Small write buffer to trigger more compactions
  4151. options.write_buffer_size = 1024;
  4152. ReOpenNoDelete();
  4153. const size_t num_workers = 4; // worker threads count
  4154. const size_t num_checkers = 2; // checker threads count
  4155. const size_t num_slow_checkers = 2; // checker threads emulating backups
  4156. const size_t num_slow_workers = 1; // slow worker threads count
  4157. const size_t num_transactions_per_thread = 10000;
  4158. const uint16_t num_sets = 3;
  4159. const size_t num_keys_per_set = 100;
  4160. // Setting the key-space to be 100 keys should cause enough write-conflicts
  4161. // to make this test interesting.
  4162. std::vector<port::Thread> threads;
  4163. std::atomic<uint32_t> finished = {0};
  4164. bool TAKE_SNAPSHOT = true;
  4165. uint64_t time_seed = env->NowMicros();
  4166. printf("time_seed is %" PRIu64 "\n", time_seed); // would help to reproduce
  4167. std::function<void()> call_inserter = [&] {
  4168. size_t thd_seed = std::hash<std::thread::id>()(std::this_thread::get_id());
  4169. Random64 rand(time_seed * thd_seed);
  4170. ASSERT_OK(TransactionStressTestInserter(db, num_transactions_per_thread,
  4171. num_sets, num_keys_per_set, &rand));
  4172. finished++;
  4173. };
  4174. std::function<void()> call_checker = [&] {
  4175. size_t thd_seed = std::hash<std::thread::id>()(std::this_thread::get_id());
  4176. Random64 rand(time_seed * thd_seed);
  4177. // Verify that data is consistent
  4178. while (finished < num_workers) {
  4179. Status s = RandomTransactionInserter::Verify(
  4180. db, num_sets, num_keys_per_set, TAKE_SNAPSHOT, &rand);
  4181. ASSERT_OK(s);
  4182. }
  4183. };
  4184. std::function<void()> call_slow_checker = [&] {
  4185. size_t thd_seed = std::hash<std::thread::id>()(std::this_thread::get_id());
  4186. Random64 rand(time_seed * thd_seed);
  4187. // Verify that data is consistent
  4188. while (finished < num_workers) {
  4189. uint64_t delay_ms = rand.Uniform(100) + 1;
  4190. Status s = RandomTransactionInserter::Verify(
  4191. db, num_sets, num_keys_per_set, TAKE_SNAPSHOT, &rand, delay_ms);
  4192. ASSERT_OK(s);
  4193. }
  4194. };
  4195. std::function<void()> call_slow_inserter = [&] {
  4196. size_t thd_seed = std::hash<std::thread::id>()(std::this_thread::get_id());
  4197. Random64 rand(time_seed * thd_seed);
  4198. uint64_t id = 0;
  4199. // Verify that data is consistent
  4200. while (finished < num_workers) {
  4201. uint64_t delay_ms = rand.Uniform(500) + 1;
  4202. ASSERT_OK(TransactionStressTestInserter(db, 1, num_sets, num_keys_per_set,
  4203. &rand, delay_ms, id++));
  4204. }
  4205. };
  4206. for (uint32_t i = 0; i < num_workers; i++) {
  4207. threads.emplace_back(call_inserter);
  4208. }
  4209. for (uint32_t i = 0; i < num_checkers; i++) {
  4210. threads.emplace_back(call_checker);
  4211. }
  4212. if (with_slow_threads_) {
  4213. for (uint32_t i = 0; i < num_slow_checkers; i++) {
  4214. threads.emplace_back(call_slow_checker);
  4215. }
  4216. for (uint32_t i = 0; i < num_slow_workers; i++) {
  4217. threads.emplace_back(call_slow_inserter);
  4218. }
  4219. }
  4220. // Wait for all threads to finish
  4221. for (auto& t : threads) {
  4222. t.join();
  4223. }
  4224. // Verify that data is consistent
  4225. Status s = RandomTransactionInserter::Verify(db, num_sets, num_keys_per_set,
  4226. !TAKE_SNAPSHOT);
  4227. ASSERT_OK(s);
  4228. }
  4229. #endif // ROCKSDB_VALGRIND_RUN
  4230. TEST_P(TransactionTest, MemoryLimitTest) {
  4231. TransactionOptions txn_options;
  4232. // Header (12 bytes) + NOOP (1 byte) + 2 * 8 bytes for data.
  4233. txn_options.max_write_batch_size = 29;
  4234. // Set threshold to unlimited so that the write batch does not get flushed,
  4235. // and can hit the memory limit.
  4236. txn_options.write_batch_flush_threshold = 0;
  4237. std::string value;
  4238. Status s;
  4239. Transaction* txn = db->BeginTransaction(WriteOptions(), txn_options);
  4240. ASSERT_TRUE(txn);
  4241. ASSERT_EQ(0, txn->GetNumPuts());
  4242. ASSERT_LE(0, txn->GetID());
  4243. s = txn->Put(Slice("a"), Slice("...."));
  4244. ASSERT_OK(s);
  4245. ASSERT_EQ(1, txn->GetNumPuts());
  4246. s = txn->Put(Slice("b"), Slice("...."));
  4247. ASSERT_OK(s);
  4248. ASSERT_EQ(2, txn->GetNumPuts());
  4249. s = txn->Put(Slice("b"), Slice("...."));
  4250. ASSERT_TRUE(s.IsMemoryLimit());
  4251. ASSERT_EQ(2, txn->GetNumPuts());
  4252. txn->Rollback();
  4253. delete txn;
  4254. }
  4255. // This test clarifies the existing expectation from the sequence number
  4256. // algorithm. It could detect mistakes in updating the code but it is not
  4257. // necessarily the one acceptable way. If the algorithm is legitimately changed,
  4258. // this unit test should be updated as well.
  4259. TEST_P(TransactionStressTest, SeqAdvanceTest) {
  4260. // TODO(myabandeh): must be test with false before new releases
  4261. const bool short_test = true;
  4262. WriteOptions wopts;
  4263. FlushOptions fopt;
  4264. options.disable_auto_compactions = true;
  4265. ASSERT_OK(ReOpen());
  4266. // Do the test with NUM_BRANCHES branches in it. Each run of a test takes some
  4267. // of the branches. This is the same as counting a binary number where i-th
  4268. // bit represents whether we take branch i in the represented by the number.
  4269. const size_t NUM_BRANCHES = short_test ? 6 : 10;
  4270. // Helper function that shows if the branch is to be taken in the run
  4271. // represented by the number n.
  4272. auto branch_do = [&](size_t n, size_t* branch) {
  4273. assert(*branch < NUM_BRANCHES);
  4274. const size_t filter = static_cast<size_t>(1) << *branch;
  4275. return n & filter;
  4276. };
  4277. const size_t max_n = static_cast<size_t>(1) << NUM_BRANCHES;
  4278. for (size_t n = 0; n < max_n; n++) {
  4279. DBImpl* db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  4280. size_t branch = 0;
  4281. auto seq = db_impl->GetLatestSequenceNumber();
  4282. exp_seq = seq;
  4283. txn_t0(0);
  4284. seq = db_impl->TEST_GetLastVisibleSequence();
  4285. ASSERT_EQ(exp_seq, seq);
  4286. if (branch_do(n, &branch)) {
  4287. ASSERT_OK(db_impl->Flush(fopt));
  4288. seq = db_impl->TEST_GetLastVisibleSequence();
  4289. ASSERT_EQ(exp_seq, seq);
  4290. }
  4291. if (!short_test && branch_do(n, &branch)) {
  4292. ASSERT_OK(db_impl->FlushWAL(true));
  4293. ASSERT_OK(ReOpenNoDelete());
  4294. db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  4295. seq = db_impl->GetLatestSequenceNumber();
  4296. ASSERT_EQ(exp_seq, seq);
  4297. }
  4298. // Doing it twice might detect some bugs
  4299. txn_t0(1);
  4300. seq = db_impl->TEST_GetLastVisibleSequence();
  4301. ASSERT_EQ(exp_seq, seq);
  4302. txn_t1(0);
  4303. seq = db_impl->TEST_GetLastVisibleSequence();
  4304. ASSERT_EQ(exp_seq, seq);
  4305. if (branch_do(n, &branch)) {
  4306. ASSERT_OK(db_impl->Flush(fopt));
  4307. seq = db_impl->TEST_GetLastVisibleSequence();
  4308. ASSERT_EQ(exp_seq, seq);
  4309. }
  4310. if (!short_test && branch_do(n, &branch)) {
  4311. ASSERT_OK(db_impl->FlushWAL(true));
  4312. ASSERT_OK(ReOpenNoDelete());
  4313. db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  4314. seq = db_impl->GetLatestSequenceNumber();
  4315. ASSERT_EQ(exp_seq, seq);
  4316. }
  4317. txn_t3(0);
  4318. seq = db_impl->TEST_GetLastVisibleSequence();
  4319. ASSERT_EQ(exp_seq, seq);
  4320. if (branch_do(n, &branch)) {
  4321. ASSERT_OK(db_impl->Flush(fopt));
  4322. seq = db_impl->TEST_GetLastVisibleSequence();
  4323. ASSERT_EQ(exp_seq, seq);
  4324. }
  4325. if (!short_test && branch_do(n, &branch)) {
  4326. ASSERT_OK(db_impl->FlushWAL(true));
  4327. ASSERT_OK(ReOpenNoDelete());
  4328. db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  4329. seq = db_impl->GetLatestSequenceNumber();
  4330. ASSERT_EQ(exp_seq, seq);
  4331. }
  4332. txn_t4(0);
  4333. seq = db_impl->TEST_GetLastVisibleSequence();
  4334. ASSERT_EQ(exp_seq, seq);
  4335. if (branch_do(n, &branch)) {
  4336. ASSERT_OK(db_impl->Flush(fopt));
  4337. seq = db_impl->TEST_GetLastVisibleSequence();
  4338. ASSERT_EQ(exp_seq, seq);
  4339. }
  4340. if (!short_test && branch_do(n, &branch)) {
  4341. ASSERT_OK(db_impl->FlushWAL(true));
  4342. ASSERT_OK(ReOpenNoDelete());
  4343. db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  4344. seq = db_impl->GetLatestSequenceNumber();
  4345. ASSERT_EQ(exp_seq, seq);
  4346. }
  4347. txn_t2(0);
  4348. seq = db_impl->TEST_GetLastVisibleSequence();
  4349. ASSERT_EQ(exp_seq, seq);
  4350. if (branch_do(n, &branch)) {
  4351. ASSERT_OK(db_impl->Flush(fopt));
  4352. seq = db_impl->TEST_GetLastVisibleSequence();
  4353. ASSERT_EQ(exp_seq, seq);
  4354. }
  4355. if (!short_test && branch_do(n, &branch)) {
  4356. ASSERT_OK(db_impl->FlushWAL(true));
  4357. ASSERT_OK(ReOpenNoDelete());
  4358. db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  4359. seq = db_impl->GetLatestSequenceNumber();
  4360. ASSERT_EQ(exp_seq, seq);
  4361. }
  4362. ASSERT_OK(ReOpen());
  4363. }
  4364. }
  4365. // Verify that the optimization would not compromize the correctness
  4366. TEST_P(TransactionTest, Optimizations) {
  4367. size_t comb_cnt = size_t(1) << 2; // 2 is number of optimization vars
  4368. for (size_t new_comb = 0; new_comb < comb_cnt; new_comb++) {
  4369. TransactionDBWriteOptimizations optimizations;
  4370. optimizations.skip_concurrency_control = IsInCombination(0, new_comb);
  4371. optimizations.skip_duplicate_key_check = IsInCombination(1, new_comb);
  4372. ASSERT_OK(ReOpen());
  4373. WriteOptions write_options;
  4374. WriteBatch batch;
  4375. batch.Put(Slice("k"), Slice("v1"));
  4376. ASSERT_OK(db->Write(write_options, &batch));
  4377. ReadOptions ropt;
  4378. PinnableSlice pinnable_val;
  4379. ASSERT_OK(db->Get(ropt, db->DefaultColumnFamily(), "k", &pinnable_val));
  4380. ASSERT_TRUE(pinnable_val == ("v1"));
  4381. }
  4382. }
  4383. // A comparator that uses only the first three bytes
  4384. class ThreeBytewiseComparator : public Comparator {
  4385. public:
  4386. ThreeBytewiseComparator() {}
  4387. const char* Name() const override { return "test.ThreeBytewiseComparator"; }
  4388. int Compare(const Slice& a, const Slice& b) const override {
  4389. Slice na = Slice(a.data(), a.size() < 3 ? a.size() : 3);
  4390. Slice nb = Slice(b.data(), b.size() < 3 ? b.size() : 3);
  4391. return na.compare(nb);
  4392. }
  4393. bool Equal(const Slice& a, const Slice& b) const override {
  4394. Slice na = Slice(a.data(), a.size() < 3 ? a.size() : 3);
  4395. Slice nb = Slice(b.data(), b.size() < 3 ? b.size() : 3);
  4396. return na == nb;
  4397. }
  4398. // This methods below dont seem relevant to this test. Implement them if
  4399. // proven othersize.
  4400. void FindShortestSeparator(std::string* start,
  4401. const Slice& limit) const override {
  4402. const Comparator* bytewise_comp = BytewiseComparator();
  4403. bytewise_comp->FindShortestSeparator(start, limit);
  4404. }
  4405. void FindShortSuccessor(std::string* key) const override {
  4406. const Comparator* bytewise_comp = BytewiseComparator();
  4407. bytewise_comp->FindShortSuccessor(key);
  4408. }
  4409. };
  4410. #ifndef ROCKSDB_VALGRIND_RUN
  4411. TEST_P(TransactionTest, GetWithoutSnapshot) {
  4412. WriteOptions write_options;
  4413. std::atomic<bool> finish = {false};
  4414. db->Put(write_options, "key", "value");
  4415. ROCKSDB_NAMESPACE::port::Thread commit_thread([&]() {
  4416. for (int i = 0; i < 100; i++) {
  4417. TransactionOptions txn_options;
  4418. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  4419. ASSERT_OK(txn->SetName("xid"));
  4420. ASSERT_OK(txn->Put("key", "overridedvalue"));
  4421. ASSERT_OK(txn->Put("key", "value"));
  4422. ASSERT_OK(txn->Prepare());
  4423. ASSERT_OK(txn->Commit());
  4424. delete txn;
  4425. }
  4426. finish = true;
  4427. });
  4428. ROCKSDB_NAMESPACE::port::Thread read_thread([&]() {
  4429. while (!finish) {
  4430. ReadOptions ropt;
  4431. PinnableSlice pinnable_val;
  4432. ASSERT_OK(db->Get(ropt, db->DefaultColumnFamily(), "key", &pinnable_val));
  4433. ASSERT_TRUE(pinnable_val == ("value"));
  4434. }
  4435. });
  4436. commit_thread.join();
  4437. read_thread.join();
  4438. }
  4439. #endif // ROCKSDB_VALGRIND_RUN
  4440. // Test that the transactional db can handle duplicate keys in the write batch
  4441. TEST_P(TransactionTest, DuplicateKeys) {
  4442. ColumnFamilyOptions cf_options;
  4443. std::string cf_name = "two";
  4444. ColumnFamilyHandle* cf_handle = nullptr;
  4445. {
  4446. ASSERT_OK(db->CreateColumnFamily(cf_options, cf_name, &cf_handle));
  4447. WriteOptions write_options;
  4448. WriteBatch batch;
  4449. batch.Put(Slice("key"), Slice("value"));
  4450. batch.Put(Slice("key2"), Slice("value2"));
  4451. // duplicate the keys
  4452. batch.Put(Slice("key"), Slice("value3"));
  4453. // duplicate the 2nd key. It should not be counted duplicate since a
  4454. // sub-patch is cut after the last duplicate.
  4455. batch.Put(Slice("key2"), Slice("value4"));
  4456. // duplicate the keys but in a different cf. It should not be counted as
  4457. // duplicate keys
  4458. batch.Put(cf_handle, Slice("key"), Slice("value5"));
  4459. ASSERT_OK(db->Write(write_options, &batch));
  4460. ReadOptions ropt;
  4461. PinnableSlice pinnable_val;
  4462. auto s = db->Get(ropt, db->DefaultColumnFamily(), "key", &pinnable_val);
  4463. ASSERT_OK(s);
  4464. ASSERT_TRUE(pinnable_val == ("value3"));
  4465. s = db->Get(ropt, db->DefaultColumnFamily(), "key2", &pinnable_val);
  4466. ASSERT_OK(s);
  4467. ASSERT_TRUE(pinnable_val == ("value4"));
  4468. s = db->Get(ropt, cf_handle, "key", &pinnable_val);
  4469. ASSERT_OK(s);
  4470. ASSERT_TRUE(pinnable_val == ("value5"));
  4471. delete cf_handle;
  4472. }
  4473. // Test with non-bytewise comparator
  4474. {
  4475. ASSERT_OK(ReOpen());
  4476. std::unique_ptr<const Comparator> comp_gc(new ThreeBytewiseComparator());
  4477. cf_options.comparator = comp_gc.get();
  4478. ASSERT_OK(db->CreateColumnFamily(cf_options, cf_name, &cf_handle));
  4479. WriteOptions write_options;
  4480. WriteBatch batch;
  4481. batch.Put(cf_handle, Slice("key"), Slice("value"));
  4482. // The first three bytes are the same, do it must be counted as duplicate
  4483. batch.Put(cf_handle, Slice("key2"), Slice("value2"));
  4484. // check for 2nd duplicate key in cf with non-default comparator
  4485. batch.Put(cf_handle, Slice("key2b"), Slice("value2b"));
  4486. ASSERT_OK(db->Write(write_options, &batch));
  4487. // The value must be the most recent value for all the keys equal to "key",
  4488. // including "key2"
  4489. ReadOptions ropt;
  4490. PinnableSlice pinnable_val;
  4491. ASSERT_OK(db->Get(ropt, cf_handle, "key", &pinnable_val));
  4492. ASSERT_TRUE(pinnable_val == ("value2b"));
  4493. // Test duplicate keys with rollback
  4494. TransactionOptions txn_options;
  4495. Transaction* txn0 = db->BeginTransaction(write_options, txn_options);
  4496. ASSERT_OK(txn0->SetName("xid"));
  4497. ASSERT_OK(txn0->Put(cf_handle, Slice("key3"), Slice("value3")));
  4498. ASSERT_OK(txn0->Merge(cf_handle, Slice("key4"), Slice("value4")));
  4499. ASSERT_OK(txn0->Rollback());
  4500. ASSERT_OK(db->Get(ropt, cf_handle, "key5", &pinnable_val));
  4501. ASSERT_TRUE(pinnable_val == ("value2b"));
  4502. delete txn0;
  4503. delete cf_handle;
  4504. cf_options.comparator = BytewiseComparator();
  4505. }
  4506. for (bool do_prepare : {true, false}) {
  4507. for (bool do_rollback : {true, false}) {
  4508. for (bool with_commit_batch : {true, false}) {
  4509. if (with_commit_batch && !do_prepare) {
  4510. continue;
  4511. }
  4512. if (with_commit_batch && do_rollback) {
  4513. continue;
  4514. }
  4515. ASSERT_OK(ReOpen());
  4516. ASSERT_OK(db->CreateColumnFamily(cf_options, cf_name, &cf_handle));
  4517. TransactionOptions txn_options;
  4518. txn_options.use_only_the_last_commit_time_batch_for_recovery = false;
  4519. WriteOptions write_options;
  4520. Transaction* txn0 = db->BeginTransaction(write_options, txn_options);
  4521. auto s = txn0->SetName("xid");
  4522. ASSERT_OK(s);
  4523. s = txn0->Put(Slice("foo0"), Slice("bar0a"));
  4524. ASSERT_OK(s);
  4525. s = txn0->Put(Slice("foo0"), Slice("bar0b"));
  4526. ASSERT_OK(s);
  4527. s = txn0->Put(Slice("foo1"), Slice("bar1"));
  4528. ASSERT_OK(s);
  4529. s = txn0->Merge(Slice("foo2"), Slice("bar2a"));
  4530. ASSERT_OK(s);
  4531. // Repeat a key after the start of a sub-patch. This should not cause a
  4532. // duplicate in the most recent sub-patch and hence not creating a new
  4533. // sub-patch.
  4534. s = txn0->Put(Slice("foo0"), Slice("bar0c"));
  4535. ASSERT_OK(s);
  4536. s = txn0->Merge(Slice("foo2"), Slice("bar2b"));
  4537. ASSERT_OK(s);
  4538. // duplicate the keys but in a different cf. It should not be counted as
  4539. // duplicate.
  4540. s = txn0->Put(cf_handle, Slice("foo0"), Slice("bar0-cf1"));
  4541. ASSERT_OK(s);
  4542. s = txn0->Put(Slice("foo3"), Slice("bar3"));
  4543. ASSERT_OK(s);
  4544. s = txn0->Merge(Slice("foo3"), Slice("bar3"));
  4545. ASSERT_OK(s);
  4546. s = txn0->Put(Slice("foo4"), Slice("bar4"));
  4547. ASSERT_OK(s);
  4548. s = txn0->Delete(Slice("foo4"));
  4549. ASSERT_OK(s);
  4550. s = txn0->SingleDelete(Slice("foo4"));
  4551. ASSERT_OK(s);
  4552. if (do_prepare) {
  4553. s = txn0->Prepare();
  4554. ASSERT_OK(s);
  4555. }
  4556. if (do_rollback) {
  4557. // Test rolling back the batch with duplicates
  4558. s = txn0->Rollback();
  4559. ASSERT_OK(s);
  4560. } else {
  4561. if (with_commit_batch) {
  4562. assert(do_prepare);
  4563. auto cb = txn0->GetCommitTimeWriteBatch();
  4564. // duplicate a key in the original batch
  4565. // TODO(myabandeh): the behavior of GetCommitTimeWriteBatch
  4566. // conflicting with the prepared batch is currently undefined and
  4567. // gives different results in different implementations.
  4568. // s = cb->Put(Slice("foo0"), Slice("bar0d"));
  4569. // ASSERT_OK(s);
  4570. // add a new duplicate key
  4571. s = cb->Put(Slice("foo6"), Slice("bar6a"));
  4572. ASSERT_OK(s);
  4573. s = cb->Put(Slice("foo6"), Slice("bar6b"));
  4574. ASSERT_OK(s);
  4575. // add a duplicate key that is removed in the same batch
  4576. s = cb->Put(Slice("foo7"), Slice("bar7a"));
  4577. ASSERT_OK(s);
  4578. s = cb->Delete(Slice("foo7"));
  4579. ASSERT_OK(s);
  4580. }
  4581. s = txn0->Commit();
  4582. ASSERT_OK(s);
  4583. }
  4584. delete txn0;
  4585. ReadOptions ropt;
  4586. PinnableSlice pinnable_val;
  4587. if (do_rollback) {
  4588. s = db->Get(ropt, db->DefaultColumnFamily(), "foo0", &pinnable_val);
  4589. ASSERT_TRUE(s.IsNotFound());
  4590. s = db->Get(ropt, cf_handle, "foo0", &pinnable_val);
  4591. ASSERT_TRUE(s.IsNotFound());
  4592. s = db->Get(ropt, db->DefaultColumnFamily(), "foo1", &pinnable_val);
  4593. ASSERT_TRUE(s.IsNotFound());
  4594. s = db->Get(ropt, db->DefaultColumnFamily(), "foo2", &pinnable_val);
  4595. ASSERT_TRUE(s.IsNotFound());
  4596. s = db->Get(ropt, db->DefaultColumnFamily(), "foo3", &pinnable_val);
  4597. ASSERT_TRUE(s.IsNotFound());
  4598. s = db->Get(ropt, db->DefaultColumnFamily(), "foo4", &pinnable_val);
  4599. ASSERT_TRUE(s.IsNotFound());
  4600. } else {
  4601. s = db->Get(ropt, db->DefaultColumnFamily(), "foo0", &pinnable_val);
  4602. ASSERT_OK(s);
  4603. ASSERT_TRUE(pinnable_val == ("bar0c"));
  4604. s = db->Get(ropt, cf_handle, "foo0", &pinnable_val);
  4605. ASSERT_OK(s);
  4606. ASSERT_TRUE(pinnable_val == ("bar0-cf1"));
  4607. s = db->Get(ropt, db->DefaultColumnFamily(), "foo1", &pinnable_val);
  4608. ASSERT_OK(s);
  4609. ASSERT_TRUE(pinnable_val == ("bar1"));
  4610. s = db->Get(ropt, db->DefaultColumnFamily(), "foo2", &pinnable_val);
  4611. ASSERT_OK(s);
  4612. ASSERT_TRUE(pinnable_val == ("bar2a,bar2b"));
  4613. s = db->Get(ropt, db->DefaultColumnFamily(), "foo3", &pinnable_val);
  4614. ASSERT_OK(s);
  4615. ASSERT_TRUE(pinnable_val == ("bar3,bar3"));
  4616. s = db->Get(ropt, db->DefaultColumnFamily(), "foo4", &pinnable_val);
  4617. ASSERT_TRUE(s.IsNotFound());
  4618. if (with_commit_batch) {
  4619. s = db->Get(ropt, db->DefaultColumnFamily(), "foo6", &pinnable_val);
  4620. ASSERT_OK(s);
  4621. ASSERT_TRUE(pinnable_val == ("bar6b"));
  4622. s = db->Get(ropt, db->DefaultColumnFamily(), "foo7", &pinnable_val);
  4623. ASSERT_TRUE(s.IsNotFound());
  4624. }
  4625. }
  4626. delete cf_handle;
  4627. } // with_commit_batch
  4628. } // do_rollback
  4629. } // do_prepare
  4630. if (!options.unordered_write) {
  4631. // Also test with max_successive_merges > 0. max_successive_merges will not
  4632. // affect our algorithm for duplicate key insertion but we add the test to
  4633. // verify that.
  4634. cf_options.max_successive_merges = 2;
  4635. cf_options.merge_operator = MergeOperators::CreateStringAppendOperator();
  4636. ASSERT_OK(ReOpen());
  4637. db->CreateColumnFamily(cf_options, cf_name, &cf_handle);
  4638. WriteOptions write_options;
  4639. // Ensure one value for the key
  4640. ASSERT_OK(db->Put(write_options, cf_handle, Slice("key"), Slice("value")));
  4641. WriteBatch batch;
  4642. // Merge more than max_successive_merges times
  4643. batch.Merge(cf_handle, Slice("key"), Slice("1"));
  4644. batch.Merge(cf_handle, Slice("key"), Slice("2"));
  4645. batch.Merge(cf_handle, Slice("key"), Slice("3"));
  4646. batch.Merge(cf_handle, Slice("key"), Slice("4"));
  4647. ASSERT_OK(db->Write(write_options, &batch));
  4648. ReadOptions read_options;
  4649. string value;
  4650. ASSERT_OK(db->Get(read_options, cf_handle, "key", &value));
  4651. ASSERT_EQ(value, "value,1,2,3,4");
  4652. delete cf_handle;
  4653. }
  4654. {
  4655. // Test that the duplicate detection is not compromised after rolling back
  4656. // to a save point
  4657. TransactionOptions txn_options;
  4658. WriteOptions write_options;
  4659. Transaction* txn0 = db->BeginTransaction(write_options, txn_options);
  4660. ASSERT_OK(txn0->Put(Slice("foo0"), Slice("bar0a")));
  4661. ASSERT_OK(txn0->Put(Slice("foo0"), Slice("bar0b")));
  4662. txn0->SetSavePoint();
  4663. ASSERT_OK(txn0->RollbackToSavePoint());
  4664. ASSERT_OK(txn0->Commit());
  4665. delete txn0;
  4666. }
  4667. // Test sucessfull recovery after a crash
  4668. {
  4669. ASSERT_OK(ReOpen());
  4670. TransactionOptions txn_options;
  4671. WriteOptions write_options;
  4672. ReadOptions ropt;
  4673. Transaction* txn0;
  4674. PinnableSlice pinnable_val;
  4675. Status s;
  4676. std::unique_ptr<const Comparator> comp_gc(new ThreeBytewiseComparator());
  4677. cf_options.comparator = comp_gc.get();
  4678. cf_options.merge_operator = MergeOperators::CreateStringAppendOperator();
  4679. ASSERT_OK(db->CreateColumnFamily(cf_options, cf_name, &cf_handle));
  4680. delete cf_handle;
  4681. std::vector<ColumnFamilyDescriptor> cfds{
  4682. ColumnFamilyDescriptor(kDefaultColumnFamilyName,
  4683. ColumnFamilyOptions(options)),
  4684. ColumnFamilyDescriptor(cf_name, cf_options),
  4685. };
  4686. std::vector<ColumnFamilyHandle*> handles;
  4687. ASSERT_OK(ReOpenNoDelete(cfds, &handles));
  4688. ASSERT_OK(db->Put(write_options, "foo0", "init"));
  4689. ASSERT_OK(db->Put(write_options, "foo1", "init"));
  4690. ASSERT_OK(db->Put(write_options, handles[1], "foo0", "init"));
  4691. ASSERT_OK(db->Put(write_options, handles[1], "foo1", "init"));
  4692. // one entry
  4693. txn0 = db->BeginTransaction(write_options, txn_options);
  4694. ASSERT_OK(txn0->SetName("xid"));
  4695. ASSERT_OK(txn0->Put(Slice("foo0"), Slice("bar0a")));
  4696. ASSERT_OK(txn0->Prepare());
  4697. delete txn0;
  4698. // This will check the asserts inside recovery code
  4699. ASSERT_OK(db->FlushWAL(true));
  4700. reinterpret_cast<PessimisticTransactionDB*>(db)->TEST_Crash();
  4701. ASSERT_OK(ReOpenNoDelete(cfds, &handles));
  4702. txn0 = db->GetTransactionByName("xid");
  4703. ASSERT_TRUE(txn0 != nullptr);
  4704. ASSERT_OK(txn0->Commit());
  4705. delete txn0;
  4706. s = db->Get(ropt, db->DefaultColumnFamily(), "foo0", &pinnable_val);
  4707. ASSERT_OK(s);
  4708. ASSERT_TRUE(pinnable_val == ("bar0a"));
  4709. // two entries, no duplicate
  4710. txn0 = db->BeginTransaction(write_options, txn_options);
  4711. ASSERT_OK(txn0->SetName("xid"));
  4712. ASSERT_OK(txn0->Put(handles[1], Slice("foo0"), Slice("bar0b")));
  4713. ASSERT_OK(txn0->Put(handles[1], Slice("fol1"), Slice("bar1b")));
  4714. ASSERT_OK(txn0->Put(Slice("foo0"), Slice("bar0b")));
  4715. ASSERT_OK(txn0->Put(Slice("foo1"), Slice("bar1b")));
  4716. ASSERT_OK(txn0->Prepare());
  4717. delete txn0;
  4718. // This will check the asserts inside recovery code
  4719. db->FlushWAL(true);
  4720. // Flush only cf 1
  4721. reinterpret_cast<DBImpl*>(db->GetRootDB())
  4722. ->TEST_FlushMemTable(true, false, handles[1]);
  4723. reinterpret_cast<PessimisticTransactionDB*>(db)->TEST_Crash();
  4724. ASSERT_OK(ReOpenNoDelete(cfds, &handles));
  4725. txn0 = db->GetTransactionByName("xid");
  4726. ASSERT_TRUE(txn0 != nullptr);
  4727. ASSERT_OK(txn0->Commit());
  4728. delete txn0;
  4729. pinnable_val.Reset();
  4730. s = db->Get(ropt, db->DefaultColumnFamily(), "foo0", &pinnable_val);
  4731. ASSERT_OK(s);
  4732. ASSERT_TRUE(pinnable_val == ("bar0b"));
  4733. pinnable_val.Reset();
  4734. s = db->Get(ropt, db->DefaultColumnFamily(), "foo1", &pinnable_val);
  4735. ASSERT_OK(s);
  4736. ASSERT_TRUE(pinnable_val == ("bar1b"));
  4737. pinnable_val.Reset();
  4738. s = db->Get(ropt, handles[1], "foo0", &pinnable_val);
  4739. ASSERT_OK(s);
  4740. ASSERT_TRUE(pinnable_val == ("bar0b"));
  4741. pinnable_val.Reset();
  4742. s = db->Get(ropt, handles[1], "fol1", &pinnable_val);
  4743. ASSERT_OK(s);
  4744. ASSERT_TRUE(pinnable_val == ("bar1b"));
  4745. // one duplicate with ::Put
  4746. txn0 = db->BeginTransaction(write_options, txn_options);
  4747. ASSERT_OK(txn0->SetName("xid"));
  4748. ASSERT_OK(txn0->Put(handles[1], Slice("key-nonkey0"), Slice("bar0c")));
  4749. ASSERT_OK(txn0->Put(handles[1], Slice("key-nonkey1"), Slice("bar1d")));
  4750. ASSERT_OK(txn0->Put(Slice("foo0"), Slice("bar0c")));
  4751. ASSERT_OK(txn0->Put(Slice("foo1"), Slice("bar1c")));
  4752. ASSERT_OK(txn0->Put(Slice("foo0"), Slice("bar0d")));
  4753. ASSERT_OK(txn0->Prepare());
  4754. delete txn0;
  4755. // This will check the asserts inside recovery code
  4756. ASSERT_OK(db->FlushWAL(true));
  4757. // Flush only cf 1
  4758. reinterpret_cast<DBImpl*>(db->GetRootDB())
  4759. ->TEST_FlushMemTable(true, false, handles[1]);
  4760. reinterpret_cast<PessimisticTransactionDB*>(db)->TEST_Crash();
  4761. ASSERT_OK(ReOpenNoDelete(cfds, &handles));
  4762. txn0 = db->GetTransactionByName("xid");
  4763. ASSERT_TRUE(txn0 != nullptr);
  4764. ASSERT_OK(txn0->Commit());
  4765. delete txn0;
  4766. pinnable_val.Reset();
  4767. s = db->Get(ropt, db->DefaultColumnFamily(), "foo0", &pinnable_val);
  4768. ASSERT_OK(s);
  4769. ASSERT_TRUE(pinnable_val == ("bar0d"));
  4770. pinnable_val.Reset();
  4771. s = db->Get(ropt, db->DefaultColumnFamily(), "foo1", &pinnable_val);
  4772. ASSERT_OK(s);
  4773. ASSERT_TRUE(pinnable_val == ("bar1c"));
  4774. pinnable_val.Reset();
  4775. s = db->Get(ropt, handles[1], "key-nonkey2", &pinnable_val);
  4776. ASSERT_OK(s);
  4777. ASSERT_TRUE(pinnable_val == ("bar1d"));
  4778. // Duplicate with ::Put, ::Delete
  4779. txn0 = db->BeginTransaction(write_options, txn_options);
  4780. ASSERT_OK(txn0->SetName("xid"));
  4781. ASSERT_OK(txn0->Put(handles[1], Slice("key-nonkey0"), Slice("bar0e")));
  4782. ASSERT_OK(txn0->Delete(handles[1], Slice("key-nonkey1")));
  4783. ASSERT_OK(txn0->Put(Slice("foo0"), Slice("bar0e")));
  4784. ASSERT_OK(txn0->Delete(Slice("foo0")));
  4785. ASSERT_OK(txn0->Prepare());
  4786. delete txn0;
  4787. // This will check the asserts inside recovery code
  4788. ASSERT_OK(db->FlushWAL(true));
  4789. // Flush only cf 1
  4790. reinterpret_cast<DBImpl*>(db->GetRootDB())
  4791. ->TEST_FlushMemTable(true, false, handles[1]);
  4792. reinterpret_cast<PessimisticTransactionDB*>(db)->TEST_Crash();
  4793. ASSERT_OK(ReOpenNoDelete(cfds, &handles));
  4794. txn0 = db->GetTransactionByName("xid");
  4795. ASSERT_TRUE(txn0 != nullptr);
  4796. ASSERT_OK(txn0->Commit());
  4797. delete txn0;
  4798. pinnable_val.Reset();
  4799. s = db->Get(ropt, db->DefaultColumnFamily(), "foo0", &pinnable_val);
  4800. ASSERT_TRUE(s.IsNotFound());
  4801. pinnable_val.Reset();
  4802. s = db->Get(ropt, handles[1], "key-nonkey2", &pinnable_val);
  4803. ASSERT_TRUE(s.IsNotFound());
  4804. // Duplicate with ::Put, ::SingleDelete
  4805. txn0 = db->BeginTransaction(write_options, txn_options);
  4806. ASSERT_OK(txn0->SetName("xid"));
  4807. ASSERT_OK(txn0->Put(handles[1], Slice("key-nonkey0"), Slice("bar0g")));
  4808. ASSERT_OK(txn0->SingleDelete(handles[1], Slice("key-nonkey1")));
  4809. ASSERT_OK(txn0->Put(Slice("foo0"), Slice("bar0e")));
  4810. ASSERT_OK(txn0->SingleDelete(Slice("foo0")));
  4811. ASSERT_OK(txn0->Prepare());
  4812. delete txn0;
  4813. // This will check the asserts inside recovery code
  4814. ASSERT_OK(db->FlushWAL(true));
  4815. // Flush only cf 1
  4816. reinterpret_cast<DBImpl*>(db->GetRootDB())
  4817. ->TEST_FlushMemTable(true, false, handles[1]);
  4818. reinterpret_cast<PessimisticTransactionDB*>(db)->TEST_Crash();
  4819. ASSERT_OK(ReOpenNoDelete(cfds, &handles));
  4820. txn0 = db->GetTransactionByName("xid");
  4821. ASSERT_TRUE(txn0 != nullptr);
  4822. ASSERT_OK(txn0->Commit());
  4823. delete txn0;
  4824. pinnable_val.Reset();
  4825. s = db->Get(ropt, db->DefaultColumnFamily(), "foo0", &pinnable_val);
  4826. ASSERT_TRUE(s.IsNotFound());
  4827. pinnable_val.Reset();
  4828. s = db->Get(ropt, handles[1], "key-nonkey2", &pinnable_val);
  4829. ASSERT_TRUE(s.IsNotFound());
  4830. // Duplicate with ::Put, ::Merge
  4831. txn0 = db->BeginTransaction(write_options, txn_options);
  4832. ASSERT_OK(txn0->SetName("xid"));
  4833. ASSERT_OK(txn0->Put(handles[1], Slice("key-nonkey0"), Slice("bar1i")));
  4834. ASSERT_OK(txn0->Merge(handles[1], Slice("key-nonkey1"), Slice("bar1j")));
  4835. ASSERT_OK(txn0->Put(Slice("foo0"), Slice("bar0f")));
  4836. ASSERT_OK(txn0->Merge(Slice("foo0"), Slice("bar0g")));
  4837. ASSERT_OK(txn0->Prepare());
  4838. delete txn0;
  4839. // This will check the asserts inside recovery code
  4840. ASSERT_OK(db->FlushWAL(true));
  4841. // Flush only cf 1
  4842. reinterpret_cast<DBImpl*>(db->GetRootDB())
  4843. ->TEST_FlushMemTable(true, false, handles[1]);
  4844. reinterpret_cast<PessimisticTransactionDB*>(db)->TEST_Crash();
  4845. ASSERT_OK(ReOpenNoDelete(cfds, &handles));
  4846. txn0 = db->GetTransactionByName("xid");
  4847. ASSERT_TRUE(txn0 != nullptr);
  4848. ASSERT_OK(txn0->Commit());
  4849. delete txn0;
  4850. pinnable_val.Reset();
  4851. s = db->Get(ropt, db->DefaultColumnFamily(), "foo0", &pinnable_val);
  4852. ASSERT_OK(s);
  4853. ASSERT_TRUE(pinnable_val == ("bar0f,bar0g"));
  4854. pinnable_val.Reset();
  4855. s = db->Get(ropt, handles[1], "key-nonkey2", &pinnable_val);
  4856. ASSERT_OK(s);
  4857. ASSERT_TRUE(pinnable_val == ("bar1i,bar1j"));
  4858. for (auto h : handles) {
  4859. delete h;
  4860. }
  4861. delete db;
  4862. db = nullptr;
  4863. }
  4864. }
  4865. // Test that the reseek optimization in iterators will not result in an infinite
  4866. // loop if there are too many uncommitted entries before the snapshot.
  4867. TEST_P(TransactionTest, ReseekOptimization) {
  4868. WriteOptions write_options;
  4869. write_options.sync = true;
  4870. write_options.disableWAL = false;
  4871. ColumnFamilyDescriptor cfd;
  4872. db->DefaultColumnFamily()->GetDescriptor(&cfd);
  4873. auto max_skip = cfd.options.max_sequential_skip_in_iterations;
  4874. ASSERT_OK(db->Put(write_options, Slice("foo0"), Slice("initv")));
  4875. TransactionOptions txn_options;
  4876. Transaction* txn0 = db->BeginTransaction(write_options, txn_options);
  4877. ASSERT_OK(txn0->SetName("xid"));
  4878. // Duplicate keys will result into separate sequence numbers in WritePrepared
  4879. // and WriteUnPrepared
  4880. for (size_t i = 0; i < 2 * max_skip; i++) {
  4881. ASSERT_OK(txn0->Put(Slice("foo1"), Slice("bar")));
  4882. }
  4883. ASSERT_OK(txn0->Prepare());
  4884. ASSERT_OK(db->Put(write_options, Slice("foo2"), Slice("initv")));
  4885. ReadOptions read_options;
  4886. // To avoid loops
  4887. read_options.max_skippable_internal_keys = 10 * max_skip;
  4888. Iterator* iter = db->NewIterator(read_options);
  4889. ASSERT_OK(iter->status());
  4890. size_t cnt = 0;
  4891. iter->SeekToFirst();
  4892. while (iter->Valid()) {
  4893. iter->Next();
  4894. ASSERT_OK(iter->status());
  4895. cnt++;
  4896. }
  4897. ASSERT_EQ(cnt, 2);
  4898. cnt = 0;
  4899. iter->SeekToLast();
  4900. while (iter->Valid()) {
  4901. iter->Prev();
  4902. ASSERT_OK(iter->status());
  4903. cnt++;
  4904. }
  4905. ASSERT_EQ(cnt, 2);
  4906. delete iter;
  4907. txn0->Rollback();
  4908. delete txn0;
  4909. }
  4910. // After recovery in kPointInTimeRecovery mode, the corrupted log file remains
  4911. // there. The new log files should be still read succesfully during recovery of
  4912. // the 2nd crash.
  4913. TEST_P(TransactionTest, DoubleCrashInRecovery) {
  4914. for (const bool manual_wal_flush : {false, true}) {
  4915. for (const bool write_after_recovery : {false, true}) {
  4916. options.wal_recovery_mode = WALRecoveryMode::kPointInTimeRecovery;
  4917. options.manual_wal_flush = manual_wal_flush;
  4918. ReOpen();
  4919. std::string cf_name = "two";
  4920. ColumnFamilyOptions cf_options;
  4921. ColumnFamilyHandle* cf_handle = nullptr;
  4922. ASSERT_OK(db->CreateColumnFamily(cf_options, cf_name, &cf_handle));
  4923. // Add a prepare entry to prevent the older logs from being deleted.
  4924. WriteOptions write_options;
  4925. TransactionOptions txn_options;
  4926. Transaction* txn = db->BeginTransaction(write_options, txn_options);
  4927. ASSERT_OK(txn->SetName("xid"));
  4928. ASSERT_OK(txn->Put(Slice("foo-prepare"), Slice("bar-prepare")));
  4929. ASSERT_OK(txn->Prepare());
  4930. FlushOptions flush_ops;
  4931. db->Flush(flush_ops);
  4932. // Now we have a log that cannot be deleted
  4933. ASSERT_OK(db->Put(write_options, cf_handle, "foo1", "bar1"));
  4934. // Flush only the 2nd cf
  4935. db->Flush(flush_ops, cf_handle);
  4936. // The value is large enough to be touched by the corruption we ingest
  4937. // below.
  4938. std::string large_value(400, ' ');
  4939. // key/value not touched by corruption
  4940. ASSERT_OK(db->Put(write_options, "foo2", "bar2"));
  4941. // key/value touched by corruption
  4942. ASSERT_OK(db->Put(write_options, "foo3", large_value));
  4943. // key/value not touched by corruption
  4944. ASSERT_OK(db->Put(write_options, "foo4", "bar4"));
  4945. db->FlushWAL(true);
  4946. DBImpl* db_impl = reinterpret_cast<DBImpl*>(db->GetRootDB());
  4947. uint64_t wal_file_id = db_impl->TEST_LogfileNumber();
  4948. std::string fname = LogFileName(dbname, wal_file_id);
  4949. reinterpret_cast<PessimisticTransactionDB*>(db)->TEST_Crash();
  4950. delete txn;
  4951. delete cf_handle;
  4952. delete db;
  4953. db = nullptr;
  4954. // Corrupt the last log file in the middle, so that it is not corrupted
  4955. // in the tail.
  4956. std::string file_content;
  4957. ASSERT_OK(ReadFileToString(env, fname, &file_content));
  4958. file_content[400] = 'h';
  4959. file_content[401] = 'a';
  4960. ASSERT_OK(env->DeleteFile(fname));
  4961. ASSERT_OK(WriteStringToFile(env, file_content, fname, true));
  4962. // Recover from corruption
  4963. std::vector<ColumnFamilyHandle*> handles;
  4964. std::vector<ColumnFamilyDescriptor> column_families;
  4965. column_families.push_back(ColumnFamilyDescriptor(kDefaultColumnFamilyName,
  4966. ColumnFamilyOptions()));
  4967. column_families.push_back(
  4968. ColumnFamilyDescriptor("two", ColumnFamilyOptions()));
  4969. ASSERT_OK(ReOpenNoDelete(column_families, &handles));
  4970. if (write_after_recovery) {
  4971. // Write data to the log right after the corrupted log
  4972. ASSERT_OK(db->Put(write_options, "foo5", large_value));
  4973. }
  4974. // Persist data written to WAL during recovery or by the last Put
  4975. db->FlushWAL(true);
  4976. // 2nd crash to recover while having a valid log after the corrupted one.
  4977. ASSERT_OK(ReOpenNoDelete(column_families, &handles));
  4978. assert(db != nullptr);
  4979. txn = db->GetTransactionByName("xid");
  4980. ASSERT_TRUE(txn != nullptr);
  4981. ASSERT_OK(txn->Commit());
  4982. delete txn;
  4983. for (auto handle : handles) {
  4984. delete handle;
  4985. }
  4986. }
  4987. }
  4988. }
  4989. } // namespace ROCKSDB_NAMESPACE
  4990. int main(int argc, char** argv) {
  4991. ::testing::InitGoogleTest(&argc, argv);
  4992. return RUN_ALL_TESTS();
  4993. }
  4994. #else
  4995. #include <stdio.h>
  4996. int main(int /*argc*/, char** /*argv*/) {
  4997. fprintf(stderr,
  4998. "SKIPPED as Transactions are not supported in ROCKSDB_LITE\n");
  4999. return 0;
  5000. }
  5001. #endif // ROCKSDB_LITE