compaction_picker_test.cc 197 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772
  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. #include <limits>
  6. #include <string>
  7. #include <utility>
  8. #include "db/compaction/compaction.h"
  9. #include "db/compaction/compaction_picker_fifo.h"
  10. #include "db/compaction/compaction_picker_level.h"
  11. #include "db/compaction/compaction_picker_universal.h"
  12. #include "db/compaction/file_pri.h"
  13. #include "rocksdb/advanced_options.h"
  14. #include "table/mock_table.h"
  15. #include "table/unique_id_impl.h"
  16. #include "test_util/testharness.h"
  17. #include "test_util/testutil.h"
  18. #include "util/string_util.h"
  19. namespace ROCKSDB_NAMESPACE {
  20. class CountingLogger : public Logger {
  21. public:
  22. using Logger::Logv;
  23. void Logv(const char* /*format*/, va_list /*ap*/) override { log_count++; }
  24. size_t log_count;
  25. };
  26. class CompactionPickerTestBase : public testing::Test {
  27. public:
  28. const Comparator* ucmp_;
  29. InternalKeyComparator icmp_;
  30. Options options_;
  31. ImmutableOptions ioptions_;
  32. MutableCFOptions mutable_cf_options_;
  33. MutableDBOptions mutable_db_options_;
  34. LevelCompactionPicker level_compaction_picker;
  35. std::string cf_name_;
  36. CountingLogger logger_;
  37. LogBuffer log_buffer_;
  38. uint32_t file_num_;
  39. CompactionOptionsFIFO fifo_options_;
  40. std::unique_ptr<VersionStorageInfo> vstorage_;
  41. std::vector<std::unique_ptr<FileMetaData>> files_;
  42. // does not own FileMetaData
  43. std::unordered_map<uint32_t, std::pair<FileMetaData*, int>> file_map_;
  44. // input files to compaction process.
  45. std::vector<CompactionInputFiles> input_files_;
  46. int compaction_level_start_;
  47. explicit CompactionPickerTestBase(const Comparator* _ucmp)
  48. : ucmp_(_ucmp),
  49. icmp_(ucmp_),
  50. options_(CreateOptions(ucmp_)),
  51. ioptions_(options_),
  52. mutable_cf_options_(options_),
  53. mutable_db_options_(),
  54. level_compaction_picker(ioptions_, &icmp_),
  55. cf_name_("dummy"),
  56. log_buffer_(InfoLogLevel::INFO_LEVEL, &logger_),
  57. file_num_(1),
  58. vstorage_(nullptr) {
  59. mutable_cf_options_.ttl = 0;
  60. mutable_cf_options_.periodic_compaction_seconds = 0;
  61. // ioptions_.compaction_pri = kMinOverlappingRatio has its own set of
  62. // tests to cover.
  63. ioptions_.compaction_pri = kByCompensatedSize;
  64. fifo_options_.max_table_files_size = 1;
  65. mutable_cf_options_.RefreshDerivedOptions(ioptions_);
  66. ioptions_.cf_paths.emplace_back("dummy",
  67. std::numeric_limits<uint64_t>::max());
  68. // When the default value of this option is true, universal compaction
  69. // tests can encounter assertion failure since SanitizeOption() is
  70. // not run to set this option to false. So we do the sanitization
  71. // here. Tests that test this option set this option to true explicitly.
  72. ioptions_.level_compaction_dynamic_level_bytes = false;
  73. }
  74. ~CompactionPickerTestBase() override { ClearFiles(); }
  75. void NewVersionStorage(int num_levels, CompactionStyle style) {
  76. DeleteVersionStorage();
  77. options_.num_levels = num_levels;
  78. vstorage_.reset(new VersionStorageInfo(
  79. &icmp_, ucmp_, options_.num_levels, style, nullptr, false,
  80. EpochNumberRequirement::kMustPresent, ioptions_.clock,
  81. options_.bottommost_file_compaction_delay,
  82. OffpeakTimeOption(mutable_db_options_.daily_offpeak_time_utc)));
  83. vstorage_->PrepareForVersionAppend(ioptions_, mutable_cf_options_);
  84. }
  85. // Create a new VersionStorageInfo object so we can add mode files and then
  86. // merge it with the existing VersionStorageInfo
  87. void AddVersionStorage() {
  88. temp_vstorage_.reset(new VersionStorageInfo(
  89. &icmp_, ucmp_, options_.num_levels, ioptions_.compaction_style,
  90. vstorage_.get(), false, EpochNumberRequirement::kMustPresent,
  91. ioptions_.clock, options_.bottommost_file_compaction_delay,
  92. OffpeakTimeOption(mutable_db_options_.daily_offpeak_time_utc)));
  93. }
  94. void DeleteVersionStorage() {
  95. vstorage_.reset();
  96. temp_vstorage_.reset();
  97. ClearFiles();
  98. file_map_.clear();
  99. input_files_.clear();
  100. }
  101. // REQUIRES: smallest and largest are c-style strings ending with '\0'
  102. void Add(int level, uint32_t file_number, const char* smallest,
  103. const char* largest, uint64_t file_size = 1, uint32_t path_id = 0,
  104. SequenceNumber smallest_seq = 100, SequenceNumber largest_seq = 100,
  105. size_t compensated_file_size = 0, bool marked_for_compact = false,
  106. Temperature temperature = Temperature::kUnknown,
  107. uint64_t oldest_ancestor_time = kUnknownOldestAncesterTime,
  108. uint64_t newest_key_time = kUnknownNewestKeyTime,
  109. Slice ts_of_smallest = Slice(), Slice ts_of_largest = Slice(),
  110. uint64_t epoch_number = kUnknownEpochNumber) {
  111. assert(ts_of_smallest.size() == ucmp_->timestamp_size());
  112. assert(ts_of_largest.size() == ucmp_->timestamp_size());
  113. VersionStorageInfo* vstorage;
  114. if (temp_vstorage_) {
  115. vstorage = temp_vstorage_.get();
  116. } else {
  117. vstorage = vstorage_.get();
  118. }
  119. assert(level < vstorage->num_levels());
  120. char* smallest_key_buf = nullptr;
  121. char* largest_key_buf = nullptr;
  122. if (!ts_of_smallest.empty()) {
  123. smallest_key_buf = new char[strlen(smallest) + ucmp_->timestamp_size()];
  124. memcpy(smallest_key_buf, smallest, strlen(smallest));
  125. memcpy(smallest_key_buf + strlen(smallest), ts_of_smallest.data(),
  126. ucmp_->timestamp_size());
  127. largest_key_buf = new char[strlen(largest) + ucmp_->timestamp_size()];
  128. memcpy(largest_key_buf, largest, strlen(largest));
  129. memcpy(largest_key_buf + strlen(largest), ts_of_largest.data(),
  130. ucmp_->timestamp_size());
  131. }
  132. InternalKey smallest_ikey = InternalKey(
  133. smallest_key_buf ? Slice(smallest_key_buf,
  134. ucmp_->timestamp_size() + strlen(smallest))
  135. : smallest,
  136. smallest_seq, kTypeValue);
  137. InternalKey largest_ikey = InternalKey(
  138. largest_key_buf
  139. ? Slice(largest_key_buf, ucmp_->timestamp_size() + strlen(largest))
  140. : largest,
  141. largest_seq, kTypeValue);
  142. FileMetaData* f = new FileMetaData(
  143. file_number, path_id, file_size, smallest_ikey, largest_ikey,
  144. smallest_seq, largest_seq, marked_for_compact, temperature,
  145. kInvalidBlobFileNumber, kUnknownOldestAncesterTime,
  146. kUnknownFileCreationTime, epoch_number, kUnknownFileChecksum,
  147. kUnknownFileChecksumFuncName, kNullUniqueId64x2, 0, 0,
  148. true /* user_defined_timestamps_persisted */);
  149. f->compensated_file_size =
  150. (compensated_file_size != 0) ? compensated_file_size : file_size;
  151. // oldest_ancester_time is only used if newest_key_time is not available
  152. f->oldest_ancester_time = oldest_ancestor_time;
  153. TableProperties tp;
  154. tp.newest_key_time = newest_key_time;
  155. f->fd.table_reader = new mock::MockTableReader(mock::KVVector{}, tp);
  156. vstorage->AddFile(level, f);
  157. files_.emplace_back(f);
  158. file_map_.insert({file_number, {f, level}});
  159. delete[] smallest_key_buf;
  160. delete[] largest_key_buf;
  161. }
  162. void SetCompactionInputFilesLevels(int level_count, int start_level) {
  163. input_files_.resize(level_count);
  164. for (int i = 0; i < level_count; ++i) {
  165. input_files_[i].level = start_level + i;
  166. }
  167. compaction_level_start_ = start_level;
  168. }
  169. void AddToCompactionFiles(uint32_t file_number) {
  170. auto iter = file_map_.find(file_number);
  171. assert(iter != file_map_.end());
  172. int level = iter->second.second;
  173. assert(level < vstorage_->num_levels());
  174. input_files_[level - compaction_level_start_].files.emplace_back(
  175. iter->second.first);
  176. }
  177. void UpdateVersionStorageInfo() {
  178. if (temp_vstorage_) {
  179. VersionBuilder builder(FileOptions(), &ioptions_, nullptr,
  180. vstorage_.get(), nullptr);
  181. ASSERT_OK(builder.SaveTo(temp_vstorage_.get()));
  182. vstorage_ = std::move(temp_vstorage_);
  183. }
  184. vstorage_->PrepareForVersionAppend(ioptions_, mutable_cf_options_);
  185. vstorage_->ComputeCompactionScore(ioptions_, mutable_cf_options_);
  186. vstorage_->SetFinalized();
  187. }
  188. private:
  189. Options CreateOptions(const Comparator* ucmp) const {
  190. Options opts;
  191. opts.comparator = ucmp;
  192. return opts;
  193. }
  194. void ClearFiles() {
  195. for (auto& file : files_) {
  196. if (file->fd.table_reader != nullptr) {
  197. delete file->fd.table_reader;
  198. }
  199. }
  200. files_.clear();
  201. }
  202. std::unique_ptr<VersionStorageInfo> temp_vstorage_;
  203. };
  204. class CompactionPickerTest : public CompactionPickerTestBase {
  205. public:
  206. explicit CompactionPickerTest()
  207. : CompactionPickerTestBase(BytewiseComparator()) {}
  208. ~CompactionPickerTest() override {
  209. SyncPoint::GetInstance()->ClearAllCallBacks();
  210. SyncPoint::GetInstance()->DisableProcessing();
  211. }
  212. };
  213. class CompactionPickerU64TsTest : public CompactionPickerTestBase {
  214. public:
  215. explicit CompactionPickerU64TsTest()
  216. : CompactionPickerTestBase(test::BytewiseComparatorWithU64TsWrapper()) {}
  217. ~CompactionPickerU64TsTest() override = default;
  218. };
  219. TEST_F(CompactionPickerTest, Empty) {
  220. NewVersionStorage(6, kCompactionStyleLevel);
  221. UpdateVersionStorageInfo();
  222. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  223. cf_name_, mutable_cf_options_, mutable_db_options_,
  224. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  225. vstorage_.get(), &log_buffer_));
  226. ASSERT_TRUE(compaction.get() == nullptr);
  227. }
  228. TEST_F(CompactionPickerTest, Single) {
  229. NewVersionStorage(6, kCompactionStyleLevel);
  230. mutable_cf_options_.level0_file_num_compaction_trigger = 2;
  231. Add(0, 1U, "p", "q");
  232. UpdateVersionStorageInfo();
  233. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  234. cf_name_, mutable_cf_options_, mutable_db_options_,
  235. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  236. vstorage_.get(), &log_buffer_));
  237. ASSERT_TRUE(compaction.get() == nullptr);
  238. }
  239. TEST_F(CompactionPickerTest, Level0Trigger) {
  240. NewVersionStorage(6, kCompactionStyleLevel);
  241. mutable_cf_options_.level0_file_num_compaction_trigger = 2;
  242. Add(0, 1U, "150", "200");
  243. Add(0, 2U, "200", "250");
  244. UpdateVersionStorageInfo();
  245. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  246. cf_name_, mutable_cf_options_, mutable_db_options_,
  247. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  248. vstorage_.get(), &log_buffer_));
  249. ASSERT_TRUE(compaction.get() != nullptr);
  250. ASSERT_EQ(2U, compaction->num_input_files(0));
  251. ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
  252. ASSERT_EQ(2U, compaction->input(0, 1)->fd.GetNumber());
  253. }
  254. TEST_F(CompactionPickerTest, Level1Trigger) {
  255. NewVersionStorage(6, kCompactionStyleLevel);
  256. Add(1, 66U, "150", "200", 1000000000U);
  257. UpdateVersionStorageInfo();
  258. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  259. cf_name_, mutable_cf_options_, mutable_db_options_,
  260. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  261. vstorage_.get(), &log_buffer_));
  262. ASSERT_TRUE(compaction.get() != nullptr);
  263. ASSERT_EQ(1U, compaction->num_input_files(0));
  264. ASSERT_EQ(66U, compaction->input(0, 0)->fd.GetNumber());
  265. }
  266. TEST_F(CompactionPickerTest, Level1Trigger2) {
  267. mutable_cf_options_.target_file_size_base = 10000000000;
  268. mutable_cf_options_.RefreshDerivedOptions(ioptions_);
  269. NewVersionStorage(6, kCompactionStyleLevel);
  270. Add(1, 66U, "150", "200", 1000000001U);
  271. Add(1, 88U, "201", "300", 1000000000U);
  272. Add(2, 6U, "150", "179", 1000000000U);
  273. Add(2, 7U, "180", "220", 1000000000U);
  274. Add(2, 8U, "221", "300", 1000000000U);
  275. UpdateVersionStorageInfo();
  276. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  277. cf_name_, mutable_cf_options_, mutable_db_options_,
  278. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  279. vstorage_.get(), &log_buffer_));
  280. ASSERT_TRUE(compaction.get() != nullptr);
  281. ASSERT_EQ(1U, compaction->num_input_files(0));
  282. ASSERT_EQ(2U, compaction->num_input_files(1));
  283. ASSERT_EQ(66U, compaction->input(0, 0)->fd.GetNumber());
  284. ASSERT_EQ(6U, compaction->input(1, 0)->fd.GetNumber());
  285. ASSERT_EQ(7U, compaction->input(1, 1)->fd.GetNumber());
  286. ASSERT_EQ(uint64_t{1073741824}, compaction->OutputFilePreallocationSize());
  287. }
  288. TEST_F(CompactionPickerTest, LevelMaxScore) {
  289. NewVersionStorage(6, kCompactionStyleLevel);
  290. mutable_cf_options_.target_file_size_base = 10000000;
  291. mutable_cf_options_.max_bytes_for_level_base = 10 * 1024 * 1024;
  292. mutable_cf_options_.RefreshDerivedOptions(ioptions_);
  293. Add(0, 1U, "150", "200", 1000000U);
  294. // Level 1 score 1.2
  295. Add(1, 66U, "150", "200", 6000000U);
  296. Add(1, 88U, "201", "300", 6000000U);
  297. // Level 2 score 1.8. File 7 is the largest. Should be picked
  298. Add(2, 6U, "150", "179", 60000000U);
  299. Add(2, 7U, "180", "220", 60000001U);
  300. Add(2, 8U, "221", "300", 60000000U);
  301. // Level 3 score slightly larger than 1
  302. Add(3, 26U, "150", "170", 260000000U);
  303. Add(3, 27U, "171", "179", 260000000U);
  304. Add(3, 28U, "191", "220", 260000000U);
  305. Add(3, 29U, "221", "300", 260000000U);
  306. UpdateVersionStorageInfo();
  307. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  308. cf_name_, mutable_cf_options_, mutable_db_options_,
  309. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  310. vstorage_.get(), &log_buffer_));
  311. ASSERT_TRUE(compaction.get() != nullptr);
  312. ASSERT_EQ(1U, compaction->num_input_files(0));
  313. ASSERT_EQ(7U, compaction->input(0, 0)->fd.GetNumber());
  314. ASSERT_EQ(mutable_cf_options_.target_file_size_base +
  315. mutable_cf_options_.target_file_size_base / 10,
  316. compaction->OutputFilePreallocationSize());
  317. }
  318. TEST_F(CompactionPickerTest, NeedsCompactionLevel) {
  319. const int kLevels = 6;
  320. const int kFileCount = 20;
  321. for (int level = 0; level < kLevels - 1; ++level) {
  322. NewVersionStorage(kLevels, kCompactionStyleLevel);
  323. uint64_t file_size = vstorage_->MaxBytesForLevel(level) * 2 / kFileCount;
  324. for (int file_count = 1; file_count <= kFileCount; ++file_count) {
  325. // start a brand new version in each test.
  326. NewVersionStorage(kLevels, kCompactionStyleLevel);
  327. for (int i = 0; i < file_count; ++i) {
  328. Add(level, i, std::to_string((i + 100) * 1000).c_str(),
  329. std::to_string((i + 100) * 1000 + 999).c_str(), file_size, 0,
  330. i * 100, i * 100 + 99);
  331. }
  332. UpdateVersionStorageInfo();
  333. ASSERT_EQ(vstorage_->CompactionScoreLevel(0), level);
  334. ASSERT_EQ(level_compaction_picker.NeedsCompaction(vstorage_.get()),
  335. vstorage_->CompactionScore(0) >= 1);
  336. // release the version storage
  337. DeleteVersionStorage();
  338. }
  339. }
  340. }
  341. TEST_F(CompactionPickerTest, Level0TriggerDynamic) {
  342. int num_levels = ioptions_.num_levels;
  343. ioptions_.level_compaction_dynamic_level_bytes = true;
  344. mutable_cf_options_.level0_file_num_compaction_trigger = 2;
  345. mutable_cf_options_.max_bytes_for_level_base = 200;
  346. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  347. NewVersionStorage(num_levels, kCompactionStyleLevel);
  348. Add(0, 1U, "150", "200");
  349. Add(0, 2U, "200", "250");
  350. UpdateVersionStorageInfo();
  351. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  352. cf_name_, mutable_cf_options_, mutable_db_options_,
  353. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  354. vstorage_.get(), &log_buffer_));
  355. ASSERT_TRUE(compaction.get() != nullptr);
  356. ASSERT_EQ(2U, compaction->num_input_files(0));
  357. ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
  358. ASSERT_EQ(2U, compaction->input(0, 1)->fd.GetNumber());
  359. ASSERT_EQ(1, static_cast<int>(compaction->num_input_levels()));
  360. ASSERT_EQ(num_levels - 1, compaction->output_level());
  361. }
  362. TEST_F(CompactionPickerTest, Level0TriggerDynamic2) {
  363. int num_levels = ioptions_.num_levels;
  364. ioptions_.level_compaction_dynamic_level_bytes = true;
  365. mutable_cf_options_.level0_file_num_compaction_trigger = 2;
  366. mutable_cf_options_.max_bytes_for_level_base = 200;
  367. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  368. NewVersionStorage(num_levels, kCompactionStyleLevel);
  369. Add(0, 1U, "150", "200");
  370. Add(0, 2U, "200", "250");
  371. Add(num_levels - 1, 3U, "200", "250", 300U);
  372. UpdateVersionStorageInfo();
  373. ASSERT_EQ(vstorage_->base_level(), num_levels - 2);
  374. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  375. cf_name_, mutable_cf_options_, mutable_db_options_,
  376. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  377. vstorage_.get(), &log_buffer_));
  378. ASSERT_TRUE(compaction.get() != nullptr);
  379. ASSERT_EQ(2U, compaction->num_input_files(0));
  380. ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
  381. ASSERT_EQ(2U, compaction->input(0, 1)->fd.GetNumber());
  382. ASSERT_EQ(1, static_cast<int>(compaction->num_input_levels()));
  383. ASSERT_EQ(num_levels - 2, compaction->output_level());
  384. }
  385. TEST_F(CompactionPickerTest, Level0TriggerDynamic3) {
  386. int num_levels = ioptions_.num_levels;
  387. ioptions_.level_compaction_dynamic_level_bytes = true;
  388. mutable_cf_options_.level0_file_num_compaction_trigger = 2;
  389. mutable_cf_options_.max_bytes_for_level_base = 200;
  390. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  391. NewVersionStorage(num_levels, kCompactionStyleLevel);
  392. Add(0, 1U, "150", "200");
  393. Add(0, 2U, "200", "250");
  394. Add(num_levels - 1, 3U, "200", "250", 300U);
  395. Add(num_levels - 1, 4U, "300", "350", 3000U);
  396. UpdateVersionStorageInfo();
  397. ASSERT_EQ(vstorage_->base_level(), num_levels - 3);
  398. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  399. cf_name_, mutable_cf_options_, mutable_db_options_,
  400. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  401. vstorage_.get(), &log_buffer_));
  402. ASSERT_TRUE(compaction.get() != nullptr);
  403. ASSERT_EQ(2U, compaction->num_input_files(0));
  404. ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
  405. ASSERT_EQ(2U, compaction->input(0, 1)->fd.GetNumber());
  406. ASSERT_EQ(1, static_cast<int>(compaction->num_input_levels()));
  407. ASSERT_EQ(num_levels - 3, compaction->output_level());
  408. }
  409. TEST_F(CompactionPickerTest, Level0TriggerDynamic4) {
  410. int num_levels = ioptions_.num_levels;
  411. ioptions_.level_compaction_dynamic_level_bytes = true;
  412. mutable_cf_options_.level0_file_num_compaction_trigger = 2;
  413. mutable_cf_options_.max_bytes_for_level_base = 200;
  414. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  415. NewVersionStorage(num_levels, kCompactionStyleLevel);
  416. Add(0, 1U, "150", "200");
  417. Add(0, 2U, "200", "250");
  418. Add(num_levels - 1, 3U, "200", "250", 300U);
  419. Add(num_levels - 1, 4U, "300", "350", 3000U);
  420. Add(num_levels - 3, 5U, "150", "180", 3U);
  421. Add(num_levels - 3, 6U, "181", "300", 3U);
  422. Add(num_levels - 3, 7U, "400", "450", 3U);
  423. UpdateVersionStorageInfo();
  424. ASSERT_EQ(vstorage_->base_level(), num_levels - 3);
  425. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  426. cf_name_, mutable_cf_options_, mutable_db_options_,
  427. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  428. vstorage_.get(), &log_buffer_));
  429. ASSERT_TRUE(compaction.get() != nullptr);
  430. ASSERT_EQ(2U, compaction->num_input_files(0));
  431. ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
  432. ASSERT_EQ(2U, compaction->input(0, 1)->fd.GetNumber());
  433. ASSERT_EQ(2U, compaction->num_input_files(1));
  434. ASSERT_EQ(num_levels - 3, compaction->level(1));
  435. ASSERT_EQ(5U, compaction->input(1, 0)->fd.GetNumber());
  436. ASSERT_EQ(6U, compaction->input(1, 1)->fd.GetNumber());
  437. ASSERT_EQ(2, static_cast<int>(compaction->num_input_levels()));
  438. ASSERT_EQ(num_levels - 3, compaction->output_level());
  439. }
  440. TEST_F(CompactionPickerTest, LevelTriggerDynamic4) {
  441. int num_levels = ioptions_.num_levels;
  442. ioptions_.level_compaction_dynamic_level_bytes = true;
  443. ioptions_.compaction_pri = kMinOverlappingRatio;
  444. mutable_cf_options_.level0_file_num_compaction_trigger = 2;
  445. mutable_cf_options_.max_bytes_for_level_base = 200;
  446. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  447. NewVersionStorage(num_levels, kCompactionStyleLevel);
  448. Add(0, 1U, "150", "200");
  449. Add(num_levels - 1, 2U, "200", "250", 300U);
  450. Add(num_levels - 1, 3U, "300", "350", 3000U);
  451. Add(num_levels - 1, 4U, "400", "450", 3U);
  452. Add(num_levels - 2, 5U, "150", "180", 300U);
  453. Add(num_levels - 2, 6U, "181", "350", 500U);
  454. Add(num_levels - 2, 7U, "400", "450", 200U);
  455. UpdateVersionStorageInfo();
  456. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  457. cf_name_, mutable_cf_options_, mutable_db_options_,
  458. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  459. vstorage_.get(), &log_buffer_));
  460. ASSERT_TRUE(compaction.get() != nullptr);
  461. ASSERT_EQ(1U, compaction->num_input_files(0));
  462. ASSERT_EQ(5U, compaction->input(0, 0)->fd.GetNumber());
  463. ASSERT_EQ(0, compaction->num_input_files(1));
  464. ASSERT_EQ(1U, compaction->num_input_levels());
  465. ASSERT_EQ(num_levels - 1, compaction->output_level());
  466. }
  467. TEST_F(CompactionPickerTest, NeedsCompactionUniversal) {
  468. NewVersionStorage(1, kCompactionStyleUniversal);
  469. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  470. UpdateVersionStorageInfo();
  471. // must return false when there's no files.
  472. ASSERT_EQ(universal_compaction_picker.NeedsCompaction(vstorage_.get()),
  473. false);
  474. // verify the trigger given different number of L0 files.
  475. for (int i = 1;
  476. i <= mutable_cf_options_.level0_file_num_compaction_trigger * 2; ++i) {
  477. NewVersionStorage(1, kCompactionStyleUniversal);
  478. Add(0, i, std::to_string((i + 100) * 1000).c_str(),
  479. std::to_string((i + 100) * 1000 + 999).c_str(), 1000000, 0, i * 100,
  480. i * 100 + 99);
  481. UpdateVersionStorageInfo();
  482. ASSERT_EQ(level_compaction_picker.NeedsCompaction(vstorage_.get()),
  483. vstorage_->CompactionScore(0) >= 1);
  484. }
  485. }
  486. TEST_F(CompactionPickerTest, CompactionUniversalIngestBehindReservedLevel) {
  487. for (bool cf_option : {false, true}) {
  488. SCOPED_TRACE("cf_option = " + std::to_string(cf_option));
  489. const uint64_t kFileSize = 100000;
  490. NewVersionStorage(3 /* num_levels */, kCompactionStyleUniversal);
  491. if (cf_option) {
  492. ioptions_.cf_allow_ingest_behind = true;
  493. } else {
  494. ioptions_.allow_ingest_behind = true;
  495. }
  496. ioptions_.num_levels = 3;
  497. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  498. UpdateVersionStorageInfo();
  499. // must return false when there's no files.
  500. ASSERT_EQ(universal_compaction_picker.NeedsCompaction(vstorage_.get()),
  501. false);
  502. NewVersionStorage(3, kCompactionStyleUniversal);
  503. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
  504. Add(0, 2U, "201", "250", kFileSize, 0, 401, 450);
  505. Add(0, 4U, "260", "300", kFileSize, 0, 260, 300);
  506. Add(1, 5U, "100", "151", kFileSize, 0, 200, 251);
  507. Add(1, 3U, "301", "350", kFileSize, 0, 101, 150);
  508. Add(2, 6U, "120", "200", kFileSize, 0, 20, 100);
  509. UpdateVersionStorageInfo();
  510. std::unique_ptr<Compaction> compaction(
  511. universal_compaction_picker.PickCompaction(
  512. cf_name_, mutable_cf_options_, mutable_db_options_,
  513. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  514. vstorage_.get(), &log_buffer_));
  515. // output level should be the one above the bottom-most
  516. ASSERT_EQ(1, compaction->output_level());
  517. // input should not include the reserved level
  518. const std::vector<CompactionInputFiles>* inputs = compaction->inputs();
  519. for (const auto& compaction_input : *inputs) {
  520. if (!compaction_input.empty()) {
  521. ASSERT_LT(compaction_input.level, 2);
  522. }
  523. }
  524. }
  525. }
  526. // Tests if the files can be trivially moved in multi level
  527. // universal compaction when allow_trivial_move option is set
  528. // In this test as the input files overlaps, they cannot
  529. // be trivially moved.
  530. TEST_F(CompactionPickerTest, CannotTrivialMoveUniversal) {
  531. const uint64_t kFileSize = 100000;
  532. mutable_cf_options_.compaction_options_universal.allow_trivial_move = true;
  533. NewVersionStorage(1, kCompactionStyleUniversal);
  534. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  535. UpdateVersionStorageInfo();
  536. // must return false when there's no files.
  537. ASSERT_EQ(universal_compaction_picker.NeedsCompaction(vstorage_.get()),
  538. false);
  539. NewVersionStorage(3, kCompactionStyleUniversal);
  540. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
  541. Add(0, 2U, "201", "250", kFileSize, 0, 401, 450);
  542. Add(0, 4U, "260", "300", kFileSize, 0, 260, 300);
  543. Add(1, 5U, "100", "151", kFileSize, 0, 200, 251);
  544. Add(1, 3U, "301", "350", kFileSize, 0, 101, 150);
  545. Add(2, 6U, "120", "200", kFileSize, 0, 20, 100);
  546. UpdateVersionStorageInfo();
  547. std::unique_ptr<Compaction> compaction(
  548. universal_compaction_picker.PickCompaction(
  549. cf_name_, mutable_cf_options_, mutable_db_options_,
  550. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  551. vstorage_.get(), &log_buffer_));
  552. ASSERT_TRUE(!compaction->is_trivial_move());
  553. }
  554. // Tests if the files can be trivially moved in multi level
  555. // universal compaction when allow_trivial_move option is set
  556. // In this test as the input files doesn't overlaps, they should
  557. // be trivially moved.
  558. TEST_F(CompactionPickerTest, AllowsTrivialMoveUniversal) {
  559. const uint64_t kFileSize = 100000;
  560. mutable_cf_options_.compaction_options_universal.allow_trivial_move = true;
  561. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  562. NewVersionStorage(3, kCompactionStyleUniversal);
  563. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
  564. Add(0, 2U, "201", "250", kFileSize, 0, 401, 450);
  565. Add(0, 4U, "260", "300", kFileSize, 0, 260, 300);
  566. Add(1, 5U, "010", "080", kFileSize, 0, 200, 251);
  567. Add(2, 3U, "301", "350", kFileSize, 0, 101, 150);
  568. UpdateVersionStorageInfo();
  569. std::unique_ptr<Compaction> compaction(
  570. universal_compaction_picker.PickCompaction(
  571. cf_name_, mutable_cf_options_, mutable_db_options_,
  572. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  573. vstorage_.get(), &log_buffer_));
  574. ASSERT_TRUE(compaction->is_trivial_move());
  575. }
  576. TEST_F(CompactionPickerTest, UniversalPeriodicCompaction1) {
  577. // The case where universal periodic compaction can be picked
  578. // with some newer files being compacted.
  579. const uint64_t kFileSize = 100000;
  580. mutable_cf_options_.periodic_compaction_seconds = 1000;
  581. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  582. NewVersionStorage(5, kCompactionStyleUniversal);
  583. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
  584. Add(0, 2U, "201", "250", kFileSize, 0, 401, 450);
  585. Add(0, 4U, "260", "300", kFileSize, 0, 260, 300);
  586. Add(3, 5U, "010", "080", kFileSize, 0, 200, 251);
  587. Add(4, 3U, "301", "350", kFileSize, 0, 101, 150);
  588. Add(4, 6U, "501", "750", kFileSize, 0, 101, 150);
  589. file_map_[2].first->being_compacted = true;
  590. UpdateVersionStorageInfo();
  591. vstorage_->TEST_AddFileMarkedForPeriodicCompaction(4, file_map_[3].first);
  592. std::unique_ptr<Compaction> compaction(
  593. universal_compaction_picker.PickCompaction(
  594. cf_name_, mutable_cf_options_, mutable_db_options_,
  595. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  596. vstorage_.get(), &log_buffer_));
  597. ASSERT_TRUE(compaction);
  598. ASSERT_EQ(4, compaction->output_level());
  599. ASSERT_EQ(0, compaction->start_level());
  600. ASSERT_EQ(1U, compaction->num_input_files(0));
  601. }
  602. TEST_F(CompactionPickerTest, UniversalPeriodicCompaction2) {
  603. // The case where universal periodic compaction does not
  604. // pick up only level to compact if it doesn't cover
  605. // any file marked as periodic compaction.
  606. const uint64_t kFileSize = 100000;
  607. mutable_cf_options_.periodic_compaction_seconds = 1000;
  608. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  609. NewVersionStorage(5, kCompactionStyleUniversal);
  610. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
  611. Add(3, 5U, "010", "080", kFileSize, 0, 200, 251);
  612. Add(4, 3U, "301", "350", kFileSize, 0, 101, 150);
  613. Add(4, 6U, "501", "750", kFileSize, 0, 101, 150);
  614. file_map_[5].first->being_compacted = true;
  615. UpdateVersionStorageInfo();
  616. vstorage_->TEST_AddFileMarkedForPeriodicCompaction(0, file_map_[1].first);
  617. std::unique_ptr<Compaction> compaction(
  618. universal_compaction_picker.PickCompaction(
  619. cf_name_, mutable_cf_options_, mutable_db_options_,
  620. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  621. vstorage_.get(), &log_buffer_));
  622. ASSERT_FALSE(compaction);
  623. }
  624. TEST_F(CompactionPickerTest, UniversalPeriodicCompaction3) {
  625. // The case where universal periodic compaction does not
  626. // pick up only the last sorted run which is an L0 file if it isn't
  627. // marked as periodic compaction.
  628. const uint64_t kFileSize = 100000;
  629. mutable_cf_options_.periodic_compaction_seconds = 1000;
  630. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  631. NewVersionStorage(5, kCompactionStyleUniversal);
  632. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
  633. Add(0, 5U, "010", "080", kFileSize, 0, 200, 251);
  634. Add(0, 6U, "501", "750", kFileSize, 0, 101, 150);
  635. file_map_[5].first->being_compacted = true;
  636. UpdateVersionStorageInfo();
  637. vstorage_->TEST_AddFileMarkedForPeriodicCompaction(0, file_map_[1].first);
  638. std::unique_ptr<Compaction> compaction(
  639. universal_compaction_picker.PickCompaction(
  640. cf_name_, mutable_cf_options_, mutable_db_options_,
  641. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  642. vstorage_.get(), &log_buffer_));
  643. ASSERT_FALSE(compaction);
  644. }
  645. TEST_F(CompactionPickerTest, UniversalPeriodicCompaction4) {
  646. // The case where universal periodic compaction couldn't form
  647. // a compaction that includes any file marked for periodic compaction.
  648. // Right now we form the compaction anyway if it is more than one
  649. // sorted run. Just put the case here to validate that it doesn't
  650. // crash.
  651. const uint64_t kFileSize = 100000;
  652. mutable_cf_options_.periodic_compaction_seconds = 1000;
  653. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  654. NewVersionStorage(5, kCompactionStyleUniversal);
  655. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
  656. Add(2, 2U, "010", "080", kFileSize, 0, 200, 251);
  657. Add(3, 5U, "010", "080", kFileSize, 0, 200, 251);
  658. Add(4, 3U, "301", "350", kFileSize, 0, 101, 150);
  659. Add(4, 6U, "501", "750", kFileSize, 0, 101, 150);
  660. file_map_[2].first->being_compacted = true;
  661. UpdateVersionStorageInfo();
  662. vstorage_->TEST_AddFileMarkedForPeriodicCompaction(0, file_map_[2].first);
  663. std::unique_ptr<Compaction> compaction(
  664. universal_compaction_picker.PickCompaction(
  665. cf_name_, mutable_cf_options_, mutable_db_options_,
  666. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  667. vstorage_.get(), &log_buffer_));
  668. ASSERT_TRUE(!compaction ||
  669. compaction->start_level() != compaction->output_level());
  670. }
  671. TEST_F(CompactionPickerTest, UniversalPeriodicCompaction5) {
  672. // Test single L0 file periodic compaction triggering.
  673. const uint64_t kFileSize = 100000;
  674. mutable_cf_options_.periodic_compaction_seconds = 1000;
  675. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  676. NewVersionStorage(5, kCompactionStyleUniversal);
  677. Add(0, 6U, "150", "200", kFileSize, 0, 500, 550);
  678. UpdateVersionStorageInfo();
  679. vstorage_->TEST_AddFileMarkedForPeriodicCompaction(0, file_map_[6].first);
  680. std::unique_ptr<Compaction> compaction(
  681. universal_compaction_picker.PickCompaction(
  682. cf_name_, mutable_cf_options_, mutable_db_options_,
  683. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  684. vstorage_.get(), &log_buffer_));
  685. ASSERT_TRUE(compaction);
  686. ASSERT_EQ(0, compaction->start_level());
  687. ASSERT_EQ(1U, compaction->num_input_files(0));
  688. ASSERT_EQ(6U, compaction->input(0, 0)->fd.GetNumber());
  689. ASSERT_EQ(4, compaction->output_level());
  690. }
  691. TEST_F(CompactionPickerTest, UniversalPeriodicCompaction6) {
  692. // Test single sorted run non-L0 periodic compaction
  693. const uint64_t kFileSize = 100000;
  694. mutable_cf_options_.periodic_compaction_seconds = 1000;
  695. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  696. NewVersionStorage(5, kCompactionStyleUniversal);
  697. Add(4, 5U, "150", "200", kFileSize, 0, 500, 550);
  698. Add(4, 6U, "350", "400", kFileSize, 0, 500, 550);
  699. UpdateVersionStorageInfo();
  700. vstorage_->TEST_AddFileMarkedForPeriodicCompaction(4, file_map_[6].first);
  701. std::unique_ptr<Compaction> compaction(
  702. universal_compaction_picker.PickCompaction(
  703. cf_name_, mutable_cf_options_, mutable_db_options_,
  704. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  705. vstorage_.get(), &log_buffer_));
  706. ASSERT_TRUE(compaction);
  707. ASSERT_EQ(4, compaction->start_level());
  708. ASSERT_EQ(2U, compaction->num_input_files(0));
  709. ASSERT_EQ(5U, compaction->input(0, 0)->fd.GetNumber());
  710. ASSERT_EQ(6U, compaction->input(0, 1)->fd.GetNumber());
  711. ASSERT_EQ(4, compaction->output_level());
  712. }
  713. TEST_F(CompactionPickerTest, UniversalIncrementalSpace1) {
  714. const uint64_t kFileSize = 100000;
  715. mutable_cf_options_.max_compaction_bytes = 555555;
  716. mutable_cf_options_.compaction_options_universal.incremental = true;
  717. mutable_cf_options_.compaction_options_universal
  718. .max_size_amplification_percent = 30;
  719. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  720. NewVersionStorage(5, kCompactionStyleUniversal);
  721. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
  722. Add(2, 2U, "010", "080", kFileSize, 0, 200, 251);
  723. Add(3, 5U, "310", "380", kFileSize, 0, 200, 251);
  724. Add(3, 6U, "410", "880", kFileSize, 0, 200, 251);
  725. Add(3, 7U, "910", "980", 1, 0, 200, 251);
  726. Add(4, 10U, "201", "250", kFileSize, 0, 101, 150);
  727. Add(4, 11U, "301", "350", kFileSize, 0, 101, 150);
  728. Add(4, 12U, "401", "450", kFileSize, 0, 101, 150);
  729. Add(4, 13U, "501", "750", kFileSize, 0, 101, 150);
  730. Add(4, 14U, "801", "850", kFileSize, 0, 101, 150);
  731. Add(4, 15U, "901", "950", kFileSize, 0, 101, 150);
  732. // Add(4, 15U, "960", "970", kFileSize, 0, 101, 150);
  733. UpdateVersionStorageInfo();
  734. std::unique_ptr<Compaction> compaction(
  735. universal_compaction_picker.PickCompaction(
  736. cf_name_, mutable_cf_options_, mutable_db_options_,
  737. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  738. vstorage_.get(), &log_buffer_));
  739. ASSERT_TRUE(compaction);
  740. ASSERT_EQ(4, compaction->output_level());
  741. ASSERT_EQ(3, compaction->start_level());
  742. ASSERT_EQ(2U, compaction->num_input_files(0));
  743. ASSERT_EQ(5U, compaction->input(0, 0)->fd.GetNumber());
  744. ASSERT_EQ(6U, compaction->input(0, 1)->fd.GetNumber());
  745. // ASSERT_EQ(4U, compaction->num_input_files(1));
  746. ASSERT_EQ(11U, compaction->input(1, 0)->fd.GetNumber());
  747. ASSERT_EQ(12U, compaction->input(1, 1)->fd.GetNumber());
  748. ASSERT_EQ(13U, compaction->input(1, 2)->fd.GetNumber());
  749. ASSERT_EQ(14U, compaction->input(1, 3)->fd.GetNumber());
  750. }
  751. TEST_F(CompactionPickerTest, UniversalIncrementalSpace2) {
  752. const uint64_t kFileSize = 100000;
  753. mutable_cf_options_.max_compaction_bytes = 400000;
  754. mutable_cf_options_.compaction_options_universal.incremental = true;
  755. mutable_cf_options_.compaction_options_universal
  756. .max_size_amplification_percent = 30;
  757. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  758. NewVersionStorage(5, kCompactionStyleUniversal);
  759. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
  760. Add(1, 2U, "010", "080", kFileSize, 0, 200, 251);
  761. Add(2, 5U, "310", "380", kFileSize, 0, 200, 251);
  762. Add(2, 6U, "410", "880", kFileSize, 0, 200, 251);
  763. Add(2, 7U, "910", "980", kFileSize, 0, 200, 251);
  764. Add(4, 10U, "201", "250", kFileSize, 0, 101, 150);
  765. Add(4, 11U, "301", "350", kFileSize, 0, 101, 150);
  766. Add(4, 12U, "401", "450", kFileSize, 0, 101, 150);
  767. Add(4, 13U, "501", "750", kFileSize, 0, 101, 150);
  768. Add(4, 14U, "801", "850", kFileSize, 0, 101, 150);
  769. Add(4, 15U, "901", "950", kFileSize, 0, 101, 150);
  770. UpdateVersionStorageInfo();
  771. std::unique_ptr<Compaction> compaction(
  772. universal_compaction_picker.PickCompaction(
  773. cf_name_, mutable_cf_options_, mutable_db_options_,
  774. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  775. vstorage_.get(), &log_buffer_));
  776. ASSERT_TRUE(compaction);
  777. ASSERT_EQ(4, compaction->output_level());
  778. ASSERT_EQ(2, compaction->start_level());
  779. ASSERT_EQ(1U, compaction->num_input_files(0));
  780. ASSERT_EQ(7U, compaction->input(0, 0)->fd.GetNumber());
  781. ASSERT_EQ(1U, compaction->num_input_files(1));
  782. ASSERT_EQ(15U, compaction->input(1, 0)->fd.GetNumber());
  783. }
  784. TEST_F(CompactionPickerTest, UniversalIncrementalSpace3) {
  785. // Test bottom level files falling between gaps between two upper level
  786. // files
  787. const uint64_t kFileSize = 100000;
  788. mutable_cf_options_.max_compaction_bytes = 300000;
  789. mutable_cf_options_.compaction_options_universal.incremental = true;
  790. mutable_cf_options_.compaction_options_universal
  791. .max_size_amplification_percent = 30;
  792. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  793. NewVersionStorage(5, kCompactionStyleUniversal);
  794. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
  795. Add(2, 2U, "010", "080", kFileSize, 0, 200, 251);
  796. Add(3, 5U, "000", "180", kFileSize, 0, 200, 251);
  797. Add(3, 6U, "181", "190", kFileSize, 0, 200, 251);
  798. Add(3, 7U, "710", "810", kFileSize, 0, 200, 251);
  799. Add(3, 8U, "820", "830", kFileSize, 0, 200, 251);
  800. Add(3, 9U, "900", "991", kFileSize, 0, 200, 251);
  801. Add(4, 10U, "201", "250", kFileSize, 0, 101, 150);
  802. Add(4, 11U, "301", "350", kFileSize, 0, 101, 150);
  803. Add(4, 12U, "401", "450", kFileSize, 0, 101, 150);
  804. Add(4, 13U, "501", "750", kFileSize, 0, 101, 150);
  805. Add(4, 14U, "801", "850", kFileSize, 0, 101, 150);
  806. Add(4, 15U, "901", "950", kFileSize, 0, 101, 150);
  807. UpdateVersionStorageInfo();
  808. std::unique_ptr<Compaction> compaction(
  809. universal_compaction_picker.PickCompaction(
  810. cf_name_, mutable_cf_options_, mutable_db_options_,
  811. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  812. vstorage_.get(), &log_buffer_));
  813. ASSERT_TRUE(compaction);
  814. ASSERT_EQ(4, compaction->output_level());
  815. ASSERT_EQ(2, compaction->start_level());
  816. ASSERT_EQ(1U, compaction->num_input_files(0));
  817. ASSERT_EQ(2U, compaction->input(0, 0)->fd.GetNumber());
  818. ASSERT_EQ(2U, compaction->num_input_files(1));
  819. ASSERT_EQ(5U, compaction->input(1, 0)->fd.GetNumber());
  820. ASSERT_EQ(6U, compaction->input(1, 1)->fd.GetNumber());
  821. ASSERT_EQ(0, compaction->num_input_files(2));
  822. }
  823. TEST_F(CompactionPickerTest, UniversalIncrementalSpace4) {
  824. // Test compaction candidates always cover many files.
  825. const uint64_t kFileSize = 100000;
  826. mutable_cf_options_.max_compaction_bytes = 3200000;
  827. mutable_cf_options_.compaction_options_universal.incremental = true;
  828. mutable_cf_options_.compaction_options_universal
  829. .max_size_amplification_percent = 30;
  830. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  831. NewVersionStorage(5, kCompactionStyleUniversal);
  832. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
  833. Add(2, 2U, "010", "080", kFileSize, 0, 200, 251);
  834. // Generate files like following:
  835. // L3: (1101, 1180) (1201, 1280) ... (7901, 7908)
  836. // L4: (1130, 1150) (1160, 1210) (1230, 1250) (1260 1310) ... (7960, 8010)
  837. for (int i = 11; i < 79; i++) {
  838. Add(3, 100 + i * 3, std::to_string(i * 100).c_str(),
  839. std::to_string(i * 100 + 80).c_str(), kFileSize, 0, 200, 251);
  840. // Add a tie breaker
  841. if (i == 66) {
  842. Add(3, 10000U, "6690", "6699", kFileSize, 0, 200, 251);
  843. }
  844. Add(4, 100 + i * 3 + 1, std::to_string(i * 100 + 30).c_str(),
  845. std::to_string(i * 100 + 50).c_str(), kFileSize, 0, 200, 251);
  846. Add(4, 100 + i * 3 + 2, std::to_string(i * 100 + 60).c_str(),
  847. std::to_string(i * 100 + 110).c_str(), kFileSize, 0, 200, 251);
  848. }
  849. UpdateVersionStorageInfo();
  850. std::unique_ptr<Compaction> compaction(
  851. universal_compaction_picker.PickCompaction(
  852. cf_name_, mutable_cf_options_, mutable_db_options_,
  853. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  854. vstorage_.get(), &log_buffer_));
  855. ASSERT_TRUE(compaction);
  856. ASSERT_EQ(4, compaction->output_level());
  857. ASSERT_EQ(3, compaction->start_level());
  858. ASSERT_EQ(6U, compaction->num_input_files(0));
  859. ASSERT_EQ(100 + 62U * 3, compaction->input(0, 0)->fd.GetNumber());
  860. ASSERT_EQ(10000U, compaction->input(0, 5)->fd.GetNumber());
  861. ASSERT_EQ(11, compaction->num_input_files(1));
  862. }
  863. TEST_F(CompactionPickerTest, UniversalIncrementalSpace5) {
  864. // Test compaction candidates always cover many files with some single
  865. // files larger than size threshold.
  866. const uint64_t kFileSize = 100000;
  867. mutable_cf_options_.max_compaction_bytes = 3200000;
  868. mutable_cf_options_.compaction_options_universal.incremental = true;
  869. mutable_cf_options_.compaction_options_universal
  870. .max_size_amplification_percent = 30;
  871. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  872. NewVersionStorage(5, kCompactionStyleUniversal);
  873. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550);
  874. Add(2, 2U, "010", "080", kFileSize, 0, 200, 251);
  875. // Generate files like following:
  876. // L3: (1101, 1180) (1201, 1280) ... (7901, 7908)
  877. // L4: (1130, 1150) (1160, 1210) (1230, 1250) (1260 1310) ... (7960, 8010)
  878. for (int i = 11; i < 70; i++) {
  879. Add(3, 100 + i * 3, std::to_string(i * 100).c_str(),
  880. std::to_string(i * 100 + 80).c_str(),
  881. i % 10 == 9 ? kFileSize * 100 : kFileSize, 0, 200, 251);
  882. Add(4, 100 + i * 3 + 1, std::to_string(i * 100 + 30).c_str(),
  883. std::to_string(i * 100 + 50).c_str(), kFileSize, 0, 200, 251);
  884. Add(4, 100 + i * 3 + 2, std::to_string(i * 100 + 60).c_str(),
  885. std::to_string(i * 100 + 110).c_str(), kFileSize, 0, 200, 251);
  886. }
  887. UpdateVersionStorageInfo();
  888. std::unique_ptr<Compaction> compaction(
  889. universal_compaction_picker.PickCompaction(
  890. cf_name_, mutable_cf_options_, mutable_db_options_,
  891. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  892. vstorage_.get(), &log_buffer_));
  893. ASSERT_TRUE(compaction);
  894. ASSERT_EQ(4, compaction->output_level());
  895. ASSERT_EQ(3, compaction->start_level());
  896. ASSERT_EQ(6U, compaction->num_input_files(0));
  897. ASSERT_EQ(100 + 14 * 3, compaction->input(0, 0)->fd.GetNumber());
  898. ASSERT_EQ(100 + 19 * 3, compaction->input(0, 5)->fd.GetNumber());
  899. ASSERT_EQ(13, compaction->num_input_files(1));
  900. }
  901. TEST_F(CompactionPickerTest,
  902. PartiallyExcludeL0ToReduceWriteStopForSizeAmpCompaction) {
  903. const uint64_t kFileSize = 100000;
  904. const uint64_t kL0FileCount = 30;
  905. const uint64_t kLastLevelFileCount = 1;
  906. const uint64_t kNumLevels = 5;
  907. for (const uint64_t test_no_exclusion : {false, true}) {
  908. const uint64_t kExpectedNumExcludedL0 =
  909. test_no_exclusion ? 0 : kL0FileCount * 1 / 10;
  910. mutable_cf_options_.level0_stop_writes_trigger = 36;
  911. mutable_cf_options_.compaction_options_universal
  912. .max_size_amplification_percent = 1;
  913. mutable_cf_options_.compaction_options_universal.max_merge_width =
  914. test_no_exclusion
  915. // In universal compaction, sorted runs from non L0 levels are
  916. // counted toward `level0_stop_writes_trigger`. Therefore we need to
  917. // subtract the total number of sorted runs picked originally for
  918. // this compaction (i.e, kL0FileCount + kLastLevelFileCount) from
  919. // `level0_stop_writes_trigger` to calculate `max_merge_width` that
  920. // results in no L0 exclusion for testing purpose.
  921. ? mutable_cf_options_.level0_stop_writes_trigger -
  922. (kL0FileCount + kLastLevelFileCount)
  923. : UINT_MAX;
  924. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  925. NewVersionStorage(kNumLevels, kCompactionStyleUniversal);
  926. for (uint64_t i = 1; i <= kL0FileCount + kLastLevelFileCount; ++i) {
  927. Add(i <= kL0FileCount ? 0 : kNumLevels - 1, static_cast<uint32_t>(i),
  928. std::to_string((i + 100) * 1000).c_str(),
  929. std::to_string((i + 100) * 1000 + 999).c_str(), kFileSize, 0, i * 100,
  930. i * 100 + 99);
  931. }
  932. UpdateVersionStorageInfo();
  933. ASSERT_TRUE(universal_compaction_picker.NeedsCompaction(vstorage_.get()));
  934. std::unique_ptr<Compaction> compaction(
  935. universal_compaction_picker.PickCompaction(
  936. cf_name_, mutable_cf_options_, mutable_db_options_,
  937. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  938. vstorage_.get(), &log_buffer_));
  939. ASSERT_TRUE(compaction.get() != nullptr);
  940. ASSERT_EQ(compaction->compaction_reason(),
  941. CompactionReason::kUniversalSizeAmplification);
  942. ASSERT_EQ(compaction->num_input_files(0),
  943. kL0FileCount - kExpectedNumExcludedL0);
  944. ASSERT_EQ(compaction->num_input_files(kNumLevels - 1), kLastLevelFileCount);
  945. for (uint64_t level = 1; level <= kNumLevels - 2; level++) {
  946. ASSERT_EQ(compaction->num_input_files(level), 0);
  947. }
  948. }
  949. }
  950. TEST_F(CompactionPickerTest, NeedsCompactionFIFO) {
  951. NewVersionStorage(1, kCompactionStyleFIFO);
  952. const int kFileCount =
  953. mutable_cf_options_.level0_file_num_compaction_trigger * 3;
  954. const uint64_t kFileSize = 100000;
  955. const uint64_t kMaxSize = kFileSize * kFileCount / 2;
  956. fifo_options_.max_table_files_size = kMaxSize;
  957. mutable_cf_options_.compaction_options_fifo = fifo_options_;
  958. FIFOCompactionPicker fifo_compaction_picker(ioptions_, &icmp_);
  959. UpdateVersionStorageInfo();
  960. // must return false when there's no files.
  961. ASSERT_EQ(fifo_compaction_picker.NeedsCompaction(vstorage_.get()), false);
  962. // verify whether compaction is needed based on the current
  963. // size of L0 files.
  964. for (int i = 1; i <= kFileCount; ++i) {
  965. NewVersionStorage(1, kCompactionStyleFIFO);
  966. Add(0, i, std::to_string((i + 100) * 1000).c_str(),
  967. std::to_string((i + 100) * 1000 + 999).c_str(), kFileSize, 0, i * 100,
  968. i * 100 + 99);
  969. UpdateVersionStorageInfo();
  970. ASSERT_EQ(fifo_compaction_picker.NeedsCompaction(vstorage_.get()),
  971. vstorage_->CompactionScore(0) >= 1);
  972. }
  973. }
  974. TEST_F(CompactionPickerTest, FIFOToCold1) {
  975. // Test fallback behavior from newest_key_time to oldest_ancestor_time
  976. for (bool newestKeyTimeKnown : {false, true}) {
  977. NewVersionStorage(1, kCompactionStyleFIFO);
  978. const uint64_t kFileSize = 100000;
  979. const uint64_t kMaxSize = kFileSize * 100000;
  980. uint64_t kColdThreshold = 2000;
  981. fifo_options_.max_table_files_size = kMaxSize;
  982. fifo_options_.file_temperature_age_thresholds = {
  983. {Temperature::kCold, kColdThreshold}};
  984. fifo_options_.allow_trivial_copy_when_change_temperature = true;
  985. fifo_options_.trivial_copy_buffer_size = 16 * 1024 * 1024;
  986. mutable_cf_options_.compaction_options_fifo = fifo_options_;
  987. mutable_cf_options_.level0_file_num_compaction_trigger = 100;
  988. mutable_cf_options_.max_compaction_bytes = kFileSize * 100;
  989. auto copiedIOptions = ioptions_;
  990. copiedIOptions.compaction_style = kCompactionStyleFIFO;
  991. FIFOCompactionPicker fifo_compaction_picker(copiedIOptions, &icmp_);
  992. int64_t current_time = 0;
  993. ASSERT_OK(Env::Default()->GetCurrentTime(&current_time));
  994. uint64_t threshold_time =
  995. static_cast<uint64_t>(current_time) - kColdThreshold;
  996. Add(0 /* level */, 4U /* file_number */, "260", "300", 1 * kFileSize, 0,
  997. 2500, 2600, 0, true, Temperature::kUnknown,
  998. newestKeyTimeKnown ? kUnknownOldestAncesterTime
  999. : threshold_time - 2000 /* oldest_ancestor_time */,
  1000. newestKeyTimeKnown ? threshold_time - 2000
  1001. : kUnknownNewestKeyTime /* newest_key_time */);
  1002. // Qualifies for compaction to kCold.
  1003. Add(0, 3U, "200", "300", 4 * kFileSize, 0, 2300, 2400, 0, true,
  1004. Temperature::kUnknown,
  1005. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 3000,
  1006. newestKeyTimeKnown ? threshold_time - 3000
  1007. : kUnknownNewestKeyTime /* newest_key_time */);
  1008. UpdateVersionStorageInfo();
  1009. ASSERT_EQ(fifo_compaction_picker.NeedsCompaction(vstorage_.get()), true);
  1010. std::unique_ptr<Compaction> compaction(
  1011. fifo_compaction_picker.PickCompaction(
  1012. cf_name_, mutable_cf_options_, mutable_db_options_,
  1013. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1014. vstorage_.get(), &log_buffer_));
  1015. ASSERT_TRUE(compaction.get() != nullptr);
  1016. ASSERT_EQ(compaction->compaction_reason(),
  1017. CompactionReason::kChangeTemperature);
  1018. ASSERT_EQ(compaction->GetOutputTemperature(), Temperature::kCold);
  1019. ASSERT_EQ(1U, compaction->num_input_files(0));
  1020. ASSERT_EQ(3U, compaction->input(0, 0)->fd.GetNumber());
  1021. }
  1022. }
  1023. TEST_F(CompactionPickerTest, FIFOToColdMaxCompactionSize) {
  1024. // Test fallback behavior from newest_key_time to oldest_ancestor_time
  1025. for (bool newestKeyTimeKnown : {false, true}) {
  1026. NewVersionStorage(1, kCompactionStyleFIFO);
  1027. const uint64_t kFileSize = 100000;
  1028. const uint64_t kMaxSize = kFileSize * 100000;
  1029. uint64_t kColdThreshold = 2000;
  1030. fifo_options_.max_table_files_size = kMaxSize;
  1031. fifo_options_.file_temperature_age_thresholds = {
  1032. {Temperature::kCold, kColdThreshold}};
  1033. mutable_cf_options_.compaction_options_fifo = fifo_options_;
  1034. mutable_cf_options_.level0_file_num_compaction_trigger = 100;
  1035. mutable_cf_options_.max_compaction_bytes = kFileSize * 9;
  1036. auto copiedIOptions = ioptions_;
  1037. copiedIOptions.compaction_style = kCompactionStyleFIFO;
  1038. FIFOCompactionPicker fifo_compaction_picker(copiedIOptions, &icmp_);
  1039. int64_t current_time = 0;
  1040. ASSERT_OK(Env::Default()->GetCurrentTime(&current_time));
  1041. uint64_t threshold_time =
  1042. static_cast<uint64_t>(current_time) - kColdThreshold;
  1043. Add(0, 6U, "240", "290", 2 * kFileSize, 0, 2900, 3000, 0, true,
  1044. Temperature::kUnknown,
  1045. newestKeyTimeKnown ? kUnknownOldestAncesterTime
  1046. : static_cast<uint64_t>(current_time) - 100,
  1047. newestKeyTimeKnown ? static_cast<uint64_t>(current_time) - 100
  1048. : kUnknownNewestKeyTime);
  1049. Add(0, 5U, "240", "290", 2 * kFileSize, 0, 2700, 2800, 0, true,
  1050. Temperature::kUnknown,
  1051. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time + 100,
  1052. newestKeyTimeKnown ? threshold_time + 100 : kUnknownNewestKeyTime);
  1053. Add(0, 4U, "260", "300", 1 * kFileSize, 0, 2500, 2600, 0, true,
  1054. Temperature::kUnknown,
  1055. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 2000,
  1056. newestKeyTimeKnown ? threshold_time - 2000 : kUnknownNewestKeyTime);
  1057. // The following two files qualify for compaction to kCold.
  1058. // But only the last two should be included to respect
  1059. // `max_compaction_bytes`.
  1060. Add(0, 3U, "200", "300", 4 * kFileSize, 0, 2300, 2400, 0, true,
  1061. Temperature::kUnknown,
  1062. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 3000,
  1063. newestKeyTimeKnown ? threshold_time - 3000 : kUnknownNewestKeyTime);
  1064. Add(0, 2U, "200", "300", 4 * kFileSize, 0, 2100, 2200, 0, true,
  1065. Temperature::kUnknown,
  1066. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 4000,
  1067. newestKeyTimeKnown ? threshold_time - 4000 : kUnknownNewestKeyTime);
  1068. Add(0, 1U, "200", "300", 4 * kFileSize, 0, 2000, 2100, 0, true,
  1069. Temperature::kUnknown,
  1070. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 5000,
  1071. newestKeyTimeKnown ? threshold_time - 5000 : kUnknownNewestKeyTime);
  1072. UpdateVersionStorageInfo();
  1073. ASSERT_EQ(fifo_compaction_picker.NeedsCompaction(vstorage_.get()), true);
  1074. std::unique_ptr<Compaction> compaction(
  1075. fifo_compaction_picker.PickCompaction(
  1076. cf_name_, mutable_cf_options_, mutable_db_options_,
  1077. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1078. vstorage_.get(), &log_buffer_));
  1079. ASSERT_TRUE(compaction.get() != nullptr);
  1080. ASSERT_EQ(compaction->compaction_reason(),
  1081. CompactionReason::kChangeTemperature);
  1082. // Compaction picker picks older files first and picks one file at a time.
  1083. ASSERT_EQ(compaction->GetOutputTemperature(), Temperature::kCold);
  1084. ASSERT_EQ(1U, compaction->num_input_files(0));
  1085. ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
  1086. }
  1087. }
  1088. TEST_F(CompactionPickerTest, FIFOToColdWithExistingCold) {
  1089. // Test fallback behavior from newest_key_time to oldest_ancestor_time
  1090. for (bool newestKeyTimeKnown : {false, true}) {
  1091. NewVersionStorage(1, kCompactionStyleFIFO);
  1092. const uint64_t kFileSize = 100000;
  1093. const uint64_t kMaxSize = kFileSize * 100000;
  1094. uint64_t kColdThreshold = 2000;
  1095. fifo_options_.max_table_files_size = kMaxSize;
  1096. fifo_options_.file_temperature_age_thresholds = {
  1097. {Temperature::kCold, kColdThreshold}};
  1098. mutable_cf_options_.compaction_options_fifo = fifo_options_;
  1099. mutable_cf_options_.level0_file_num_compaction_trigger = 100;
  1100. mutable_cf_options_.max_compaction_bytes = kFileSize * 100;
  1101. auto copiedIOptions = ioptions_;
  1102. copiedIOptions.compaction_style = kCompactionStyleFIFO;
  1103. FIFOCompactionPicker fifo_compaction_picker(copiedIOptions, &icmp_);
  1104. int64_t current_time = 0;
  1105. ASSERT_OK(Env::Default()->GetCurrentTime(&current_time));
  1106. uint64_t threshold_time =
  1107. static_cast<uint64_t>(current_time) - kColdThreshold;
  1108. Add(0, 6U, "240", "290", 2 * kFileSize, 0, 2900, 3000, 0, true,
  1109. Temperature::kUnknown,
  1110. newestKeyTimeKnown ? kUnknownOldestAncesterTime
  1111. : static_cast<uint64_t>(current_time) - 100,
  1112. newestKeyTimeKnown ? static_cast<uint64_t>(current_time) - 100
  1113. : kUnknownNewestKeyTime);
  1114. Add(0, 5U, "240", "290", 2 * kFileSize, 0, 2700, 2800, 0, true,
  1115. Temperature::kUnknown,
  1116. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time + 100,
  1117. newestKeyTimeKnown ? threshold_time + 100 : kUnknownNewestKeyTime);
  1118. Add(0, 4U, "260", "300", 1 * kFileSize, 0, 2500, 2600, 0, true,
  1119. Temperature::kUnknown,
  1120. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 2000,
  1121. newestKeyTimeKnown ? threshold_time - 2000 : kUnknownNewestKeyTime);
  1122. // The following two files qualify for compaction to kCold.
  1123. Add(0, 3U, "200", "300", 4 * kFileSize, 0, 2300, 2400, 0, true,
  1124. Temperature::kUnknown,
  1125. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 3000,
  1126. newestKeyTimeKnown ? threshold_time - 3000 : kUnknownNewestKeyTime);
  1127. Add(0, 2U, "200", "300", 4 * kFileSize, 0, 2100, 2200, 0, true,
  1128. Temperature::kUnknown,
  1129. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 4000,
  1130. newestKeyTimeKnown ? threshold_time - 4000 : kUnknownNewestKeyTime);
  1131. Add(0, 1U, "200", "300", 4 * kFileSize, 0, 2000, 2100, 0, true,
  1132. Temperature::kCold,
  1133. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 5000,
  1134. newestKeyTimeKnown ? threshold_time - 5000 : kUnknownNewestKeyTime);
  1135. UpdateVersionStorageInfo();
  1136. ASSERT_EQ(fifo_compaction_picker.NeedsCompaction(vstorage_.get()), true);
  1137. std::unique_ptr<Compaction> compaction(
  1138. fifo_compaction_picker.PickCompaction(
  1139. cf_name_, mutable_cf_options_, mutable_db_options_,
  1140. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1141. vstorage_.get(), &log_buffer_));
  1142. ASSERT_TRUE(compaction.get() != nullptr);
  1143. ASSERT_EQ(compaction->compaction_reason(),
  1144. CompactionReason::kChangeTemperature);
  1145. // Compaction picker picks older files first and picks one file at a time.
  1146. ASSERT_EQ(compaction->GetOutputTemperature(), Temperature::kCold);
  1147. ASSERT_EQ(1U, compaction->num_input_files(0));
  1148. ASSERT_EQ(2U, compaction->input(0, 0)->fd.GetNumber());
  1149. }
  1150. }
  1151. TEST_F(CompactionPickerTest, FIFOToColdWithHotBetweenCold) {
  1152. // Test fallback behavior from newest_key_time to oldest_ancestor_time
  1153. for (bool newestKeyTimeKnown : {false, true}) {
  1154. NewVersionStorage(1, kCompactionStyleFIFO);
  1155. const uint64_t kFileSize = 100000;
  1156. const uint64_t kMaxSize = kFileSize * 100000;
  1157. uint64_t kColdThreshold = 2000;
  1158. fifo_options_.max_table_files_size = kMaxSize;
  1159. fifo_options_.file_temperature_age_thresholds = {
  1160. {Temperature::kCold, kColdThreshold}};
  1161. mutable_cf_options_.compaction_options_fifo = fifo_options_;
  1162. mutable_cf_options_.level0_file_num_compaction_trigger = 100;
  1163. mutable_cf_options_.max_compaction_bytes = kFileSize * 100;
  1164. auto copiedIOptions = ioptions_;
  1165. copiedIOptions.compaction_style = kCompactionStyleFIFO;
  1166. FIFOCompactionPicker fifo_compaction_picker(copiedIOptions, &icmp_);
  1167. int64_t current_time = 0;
  1168. ASSERT_OK(Env::Default()->GetCurrentTime(&current_time));
  1169. uint64_t threshold_time =
  1170. static_cast<uint64_t>(current_time) - kColdThreshold;
  1171. Add(0, 6U, "240", "290", 2 * kFileSize, 0, 2900, 3000, 0, true,
  1172. Temperature::kUnknown,
  1173. newestKeyTimeKnown ? kUnknownOldestAncesterTime
  1174. : static_cast<uint64_t>(current_time) - 100,
  1175. newestKeyTimeKnown ? static_cast<uint64_t>(current_time) - 100
  1176. : kUnknownNewestKeyTime);
  1177. Add(0, 5U, "240", "290", 2 * kFileSize, 0, 2700, 2800, 0, true,
  1178. Temperature::kUnknown,
  1179. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time + 100,
  1180. newestKeyTimeKnown ? threshold_time + 100 : kUnknownNewestKeyTime);
  1181. Add(0, 4U, "260", "300", 1 * kFileSize, 0, 2500, 2600, 0, true,
  1182. Temperature::kUnknown,
  1183. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 2000,
  1184. newestKeyTimeKnown ? threshold_time - 2000 : kUnknownNewestKeyTime);
  1185. Add(0, 3U, "200", "300", 4 * kFileSize, 0, 2300, 2400, 0, true,
  1186. Temperature::kCold,
  1187. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 3000,
  1188. newestKeyTimeKnown ? threshold_time - 3000 : kUnknownNewestKeyTime);
  1189. // Qualifies for compaction to kCold.
  1190. Add(0, 2U, "200", "300", 4 * kFileSize, 0, 2100, 2200, 0, true,
  1191. Temperature::kUnknown,
  1192. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 4000,
  1193. newestKeyTimeKnown ? threshold_time - 4000 : kUnknownNewestKeyTime);
  1194. Add(0, 1U, "200", "300", 4 * kFileSize, 0, 2000, 2100, 0, true,
  1195. Temperature::kCold,
  1196. newestKeyTimeKnown ? kUnknownOldestAncesterTime : threshold_time - 5000,
  1197. newestKeyTimeKnown ? threshold_time - 5000 : kUnknownNewestKeyTime);
  1198. UpdateVersionStorageInfo();
  1199. ASSERT_EQ(fifo_compaction_picker.NeedsCompaction(vstorage_.get()), true);
  1200. std::unique_ptr<Compaction> compaction(
  1201. fifo_compaction_picker.PickCompaction(
  1202. cf_name_, mutable_cf_options_, mutable_db_options_,
  1203. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1204. vstorage_.get(), &log_buffer_));
  1205. ASSERT_TRUE(compaction.get() != nullptr);
  1206. ASSERT_EQ(compaction->compaction_reason(),
  1207. CompactionReason::kChangeTemperature);
  1208. ASSERT_EQ(compaction->GetOutputTemperature(), Temperature::kCold);
  1209. ASSERT_EQ(1U, compaction->num_input_files(0));
  1210. ASSERT_EQ(2U, compaction->input(0, 0)->fd.GetNumber());
  1211. }
  1212. }
  1213. TEST_F(CompactionPickerTest, FIFOToHotAndWarm) {
  1214. // Test fallback behavior from newest_key_time to oldest_ancestor_time
  1215. for (bool newestKeyTimeKnown : {false, true}) {
  1216. NewVersionStorage(1, kCompactionStyleFIFO);
  1217. const uint64_t kFileSize = 100000;
  1218. const uint64_t kMaxSize = kFileSize * 100000;
  1219. uint64_t kWarmThreshold = 10000;
  1220. uint64_t kHotThreshold = 2000;
  1221. fifo_options_.max_table_files_size = kMaxSize;
  1222. // Test that multiple threshold works.
  1223. fifo_options_.file_temperature_age_thresholds = {
  1224. {Temperature::kHot, kHotThreshold},
  1225. {Temperature::kWarm, kWarmThreshold}};
  1226. mutable_cf_options_.compaction_options_fifo = fifo_options_;
  1227. mutable_cf_options_.level0_file_num_compaction_trigger = 100;
  1228. mutable_cf_options_.max_compaction_bytes = kFileSize * 100;
  1229. auto copiedIOptions = ioptions_;
  1230. copiedIOptions.compaction_style = kCompactionStyleFIFO;
  1231. FIFOCompactionPicker fifo_compaction_picker(copiedIOptions, &icmp_);
  1232. int64_t current_time = 0;
  1233. ASSERT_OK(Env::Default()->GetCurrentTime(&current_time));
  1234. uint64_t hot_threshold_time =
  1235. static_cast<uint64_t>(current_time) - kHotThreshold;
  1236. uint64_t warm_threshold_time =
  1237. static_cast<uint64_t>(current_time) - kWarmThreshold;
  1238. Add(0, 6U, "240", "290", 2 * kFileSize, 0, 2900, 3000, 0, true,
  1239. Temperature::kUnknown,
  1240. newestKeyTimeKnown ? kUnknownOldestAncesterTime
  1241. : static_cast<uint64_t>(current_time) - 100,
  1242. newestKeyTimeKnown ? static_cast<uint64_t>(current_time) - 100
  1243. : kUnknownNewestKeyTime);
  1244. Add(0, 5U, "240", "290", 2 * kFileSize, 0, 2700, 2800, 0, true,
  1245. Temperature::kUnknown,
  1246. newestKeyTimeKnown ? kUnknownOldestAncesterTime
  1247. : hot_threshold_time + 100,
  1248. newestKeyTimeKnown ? hot_threshold_time + 100 : kUnknownNewestKeyTime);
  1249. Add(0, 4U, "260", "300", 1 * kFileSize, 0, 2500, 2600, 0, true,
  1250. Temperature::kUnknown,
  1251. newestKeyTimeKnown ? kUnknownOldestAncesterTime
  1252. : hot_threshold_time - 200,
  1253. newestKeyTimeKnown ? hot_threshold_time - 200 : kUnknownNewestKeyTime);
  1254. // Qualifies for Hot
  1255. Add(0, 3U, "200", "300", 4 * kFileSize, 0, 2300, 2400, 0, true,
  1256. Temperature::kUnknown,
  1257. newestKeyTimeKnown ? kUnknownOldestAncesterTime
  1258. : warm_threshold_time - 100,
  1259. newestKeyTimeKnown ? warm_threshold_time - 100 : kUnknownNewestKeyTime);
  1260. // Qualifies for Warm
  1261. Add(0, 2U, "200", "300", 4 * kFileSize, 0, 2100, 2200, 0, true,
  1262. Temperature::kUnknown,
  1263. newestKeyTimeKnown ? kUnknownOldestAncesterTime
  1264. : warm_threshold_time - 4000,
  1265. newestKeyTimeKnown ? warm_threshold_time - 4000
  1266. : kUnknownNewestKeyTime);
  1267. Add(0, 1U, "200", "300", 4 * kFileSize, 0, 2000, 2100, 0, true,
  1268. Temperature::kUnknown,
  1269. newestKeyTimeKnown ? kUnknownOldestAncesterTime
  1270. : warm_threshold_time - 5000,
  1271. newestKeyTimeKnown ? warm_threshold_time - 5000
  1272. : kUnknownNewestKeyTime);
  1273. UpdateVersionStorageInfo();
  1274. ASSERT_EQ(fifo_compaction_picker.NeedsCompaction(vstorage_.get()), true);
  1275. std::unique_ptr<Compaction> compaction(
  1276. fifo_compaction_picker.PickCompaction(
  1277. cf_name_, mutable_cf_options_, mutable_db_options_,
  1278. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1279. vstorage_.get(), &log_buffer_));
  1280. ASSERT_TRUE(compaction.get() != nullptr);
  1281. ASSERT_EQ(compaction->compaction_reason(),
  1282. CompactionReason::kChangeTemperature);
  1283. // Compaction picker picks older files first and picks one file at a time.
  1284. ASSERT_EQ(compaction->GetOutputTemperature(), Temperature::kWarm);
  1285. ASSERT_EQ(1U, compaction->num_input_files(0));
  1286. ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
  1287. }
  1288. }
  1289. TEST_F(CompactionPickerTest, CompactFilesOutputTemperature) {
  1290. NewVersionStorage(6, kCompactionStyleLevel);
  1291. auto file_number = 66U;
  1292. Add(0, file_number, "150", "200", 1000000000U);
  1293. UpdateVersionStorageInfo();
  1294. std::unordered_set<uint64_t> input{file_number};
  1295. std::vector<CompactionInputFiles> input_files;
  1296. ASSERT_OK(level_compaction_picker.GetCompactionInputsFromFileNumbers(
  1297. &input_files, &input, vstorage_.get(), CompactionOptions()));
  1298. auto compaction_options = CompactionOptions();
  1299. compaction_options.output_temperature_override = Temperature::kCold;
  1300. std::unique_ptr<Compaction> compaction(
  1301. level_compaction_picker.PickCompactionForCompactFiles(
  1302. compaction_options, input_files, 1, vstorage_.get(),
  1303. mutable_cf_options_, mutable_db_options_, /*output_path_id=*/0));
  1304. ASSERT_TRUE(compaction.get() != nullptr);
  1305. ASSERT_EQ(compaction->GetOutputTemperature(), Temperature::kCold);
  1306. }
  1307. TEST_F(CompactionPickerTest, CompactionPriMinOverlapping1) {
  1308. NewVersionStorage(6, kCompactionStyleLevel);
  1309. ioptions_.compaction_pri = kMinOverlappingRatio;
  1310. mutable_cf_options_.target_file_size_base = 100000000000;
  1311. mutable_cf_options_.target_file_size_multiplier = 10;
  1312. mutable_cf_options_.max_bytes_for_level_base = 10 * 1024 * 1024;
  1313. mutable_cf_options_.RefreshDerivedOptions(ioptions_);
  1314. Add(2, 6U, "150", "179", 50000000U);
  1315. Add(2, 7U, "180", "220", 50000000U);
  1316. Add(2, 8U, "321", "400", 50000000U); // File not overlapping
  1317. Add(2, 9U, "721", "800", 50000000U);
  1318. Add(3, 26U, "150", "170", 260000000U);
  1319. Add(3, 27U, "171", "179", 260000000U);
  1320. Add(3, 28U, "191", "220", 260000000U);
  1321. Add(3, 29U, "221", "300", 260000000U);
  1322. Add(3, 30U, "750", "900", 260000000U);
  1323. UpdateVersionStorageInfo();
  1324. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1325. cf_name_, mutable_cf_options_, mutable_db_options_,
  1326. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1327. vstorage_.get(), &log_buffer_));
  1328. ASSERT_TRUE(compaction.get() != nullptr);
  1329. ASSERT_EQ(1U, compaction->num_input_files(0));
  1330. // Pick file 8 because it overlaps with 0 files on level 3.
  1331. ASSERT_EQ(8U, compaction->input(0, 0)->fd.GetNumber());
  1332. // Compaction input size * 1.1
  1333. ASSERT_GE(uint64_t{55000000}, compaction->OutputFilePreallocationSize());
  1334. }
  1335. TEST_F(CompactionPickerTest, CompactionPriMinOverlapping2) {
  1336. NewVersionStorage(6, kCompactionStyleLevel);
  1337. ioptions_.compaction_pri = kMinOverlappingRatio;
  1338. mutable_cf_options_.target_file_size_base = 10000000;
  1339. mutable_cf_options_.target_file_size_multiplier = 10;
  1340. mutable_cf_options_.max_bytes_for_level_base = 10 * 1024 * 1024;
  1341. Add(2, 6U, "150", "175",
  1342. 60000000U); // Overlaps with file 26, 27, total size 521M
  1343. Add(2, 7U, "176", "200", 60000000U); // Overlaps with file 27, 28, total size
  1344. // 520M, the smallest overlapping
  1345. Add(2, 8U, "201", "300",
  1346. 60000000U); // Overlaps with file 28, 29, total size 521M
  1347. Add(3, 25U, "100", "110", 261000000U);
  1348. Add(3, 26U, "150", "170", 261000000U);
  1349. Add(3, 27U, "171", "179", 260000000U);
  1350. Add(3, 28U, "191", "220", 260000000U);
  1351. Add(3, 29U, "221", "300", 261000000U);
  1352. Add(3, 30U, "321", "400", 261000000U);
  1353. UpdateVersionStorageInfo();
  1354. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1355. cf_name_, mutable_cf_options_, mutable_db_options_,
  1356. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1357. vstorage_.get(), &log_buffer_));
  1358. ASSERT_TRUE(compaction.get() != nullptr);
  1359. ASSERT_EQ(1U, compaction->num_input_files(0));
  1360. // Picking file 7 because overlapping ratio is the biggest.
  1361. ASSERT_EQ(7U, compaction->input(0, 0)->fd.GetNumber());
  1362. }
  1363. TEST_F(CompactionPickerTest, CompactionPriMinOverlapping3) {
  1364. NewVersionStorage(6, kCompactionStyleLevel);
  1365. ioptions_.compaction_pri = kMinOverlappingRatio;
  1366. mutable_cf_options_.max_bytes_for_level_base = 10000000;
  1367. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  1368. // file 7 and 8 over lap with the same file, but file 8 is smaller so
  1369. // it will be picked.
  1370. Add(2, 6U, "150", "167", 60000000U); // Overlaps with file 26, 27
  1371. Add(2, 7U, "168", "169", 60000000U); // Overlaps with file 27
  1372. Add(2, 8U, "201", "300", 61000000U); // Overlaps with file 28, but the file
  1373. // itself is larger. Should be picked.
  1374. Add(3, 26U, "160", "165", 260000000U);
  1375. Add(3, 27U, "166", "170", 260000000U);
  1376. Add(3, 28U, "180", "400", 260000000U);
  1377. Add(3, 29U, "401", "500", 260000000U);
  1378. UpdateVersionStorageInfo();
  1379. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1380. cf_name_, mutable_cf_options_, mutable_db_options_,
  1381. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1382. vstorage_.get(), &log_buffer_));
  1383. ASSERT_TRUE(compaction.get() != nullptr);
  1384. ASSERT_EQ(1U, compaction->num_input_files(0));
  1385. // Picking file 8 because overlapping ratio is the biggest.
  1386. ASSERT_EQ(8U, compaction->input(0, 0)->fd.GetNumber());
  1387. }
  1388. TEST_F(CompactionPickerTest, CompactionPriMinOverlapping4) {
  1389. NewVersionStorage(6, kCompactionStyleLevel);
  1390. ioptions_.compaction_pri = kMinOverlappingRatio;
  1391. mutable_cf_options_.max_bytes_for_level_base = 10000000;
  1392. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  1393. // Overlaps with file 26, 27, ratio is
  1394. // (60000000U + 60000000U / 180000000U) = 0.67
  1395. Add(2, 6U, "150", "167", 60000000U, 0, 100, 100, 180000000U);
  1396. Add(2, 7U, "178", "189", 60000000U); // Overlaps with file 28
  1397. Add(2, 8U, "401", "500", 61000000U); // Overlaps with file 29
  1398. Add(3, 26U, "160", "165", 60000000U);
  1399. // Boosted file size in output level is not considered.
  1400. Add(3, 27U, "166", "170", 60000000U, 0, 100, 100, 260000000U);
  1401. Add(3, 28U, "180", "400", 60000000U);
  1402. Add(3, 29U, "401", "500", 60000000U);
  1403. UpdateVersionStorageInfo();
  1404. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1405. cf_name_, mutable_cf_options_, mutable_db_options_,
  1406. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1407. vstorage_.get(), &log_buffer_));
  1408. ASSERT_TRUE(compaction.get() != nullptr);
  1409. ASSERT_EQ(1U, compaction->num_input_files(0));
  1410. // Picking file 6 because overlapping ratio is the biggest.
  1411. ASSERT_EQ(6U, compaction->input(0, 0)->fd.GetNumber());
  1412. }
  1413. TEST_F(CompactionPickerTest, CompactionPriRoundRobin) {
  1414. std::vector<InternalKey> test_cursors = {InternalKey("249", 100, kTypeValue),
  1415. InternalKey("600", 100, kTypeValue),
  1416. InternalKey()};
  1417. std::vector<uint32_t> selected_files = {8U, 6U, 6U};
  1418. ioptions_.compaction_pri = kRoundRobin;
  1419. mutable_cf_options_.max_bytes_for_level_base = 12000000;
  1420. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  1421. for (size_t i = 0; i < test_cursors.size(); i++) {
  1422. // start a brand new version in each test.
  1423. NewVersionStorage(6, kCompactionStyleLevel);
  1424. vstorage_->ResizeCompactCursors(6);
  1425. // Set the cursor
  1426. vstorage_->AddCursorForOneLevel(2, test_cursors[i]);
  1427. Add(2, 6U, "150", "199", 50000000U); // Overlap with 26U, 27U
  1428. Add(2, 7U, "200", "249", 50000000U); // File not overlapping
  1429. Add(2, 8U, "300", "600", 50000000U); // Overlap with 28U, 29U
  1430. Add(3, 26U, "130", "165", 60000000U);
  1431. Add(3, 27U, "166", "170", 60000000U);
  1432. Add(3, 28U, "270", "340", 60000000U);
  1433. Add(3, 29U, "401", "500", 60000000U);
  1434. UpdateVersionStorageInfo();
  1435. LevelCompactionPicker local_level_compaction_picker =
  1436. LevelCompactionPicker(ioptions_, &icmp_);
  1437. std::unique_ptr<Compaction> compaction(
  1438. local_level_compaction_picker.PickCompaction(
  1439. cf_name_, mutable_cf_options_, mutable_db_options_,
  1440. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1441. vstorage_.get(), &log_buffer_));
  1442. ASSERT_TRUE(compaction.get() != nullptr);
  1443. // Since the max bytes for level 2 is 120M, picking one file to compact
  1444. // makes the post-compaction level size less than 120M, there is exactly one
  1445. // file picked for round-robin compaction
  1446. ASSERT_EQ(1U, compaction->num_input_files(0));
  1447. ASSERT_EQ(selected_files[i], compaction->input(0, 0)->fd.GetNumber());
  1448. // release the version storage
  1449. DeleteVersionStorage();
  1450. }
  1451. }
  1452. TEST_F(CompactionPickerTest, CompactionPriMultipleFilesRoundRobin1) {
  1453. ioptions_.compaction_pri = kRoundRobin;
  1454. mutable_cf_options_.max_compaction_bytes = 100000000u;
  1455. mutable_cf_options_.max_bytes_for_level_base = 120;
  1456. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  1457. // start a brand new version in each test.
  1458. NewVersionStorage(6, kCompactionStyleLevel);
  1459. vstorage_->ResizeCompactCursors(6);
  1460. // Set the cursor (file picking should start with 7U)
  1461. vstorage_->AddCursorForOneLevel(2, InternalKey("199", 100, kTypeValue));
  1462. Add(2, 6U, "150", "199", 500U);
  1463. Add(2, 7U, "200", "249", 500U);
  1464. Add(2, 8U, "300", "600", 500U);
  1465. Add(2, 9U, "700", "800", 500U);
  1466. Add(2, 10U, "850", "950", 500U);
  1467. Add(3, 26U, "130", "165", 600U);
  1468. Add(3, 27U, "166", "170", 600U);
  1469. Add(3, 28U, "270", "340", 600U);
  1470. Add(3, 29U, "401", "500", 600U);
  1471. Add(3, 30U, "601", "800", 600U);
  1472. Add(3, 31U, "830", "890", 600U);
  1473. UpdateVersionStorageInfo();
  1474. LevelCompactionPicker local_level_compaction_picker =
  1475. LevelCompactionPicker(ioptions_, &icmp_);
  1476. std::unique_ptr<Compaction> compaction(
  1477. local_level_compaction_picker.PickCompaction(
  1478. cf_name_, mutable_cf_options_, mutable_db_options_,
  1479. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1480. vstorage_.get(), &log_buffer_));
  1481. ASSERT_TRUE(compaction.get() != nullptr);
  1482. // The maximum compaction bytes is very large in this case so we can igore its
  1483. // constraint in this test case. The maximum bytes for level 2 is 1200
  1484. // bytes, and thus at least 3 files should be picked so that the bytes in
  1485. // level 2 is less than the maximum
  1486. ASSERT_EQ(3U, compaction->num_input_files(0));
  1487. ASSERT_EQ(7U, compaction->input(0, 0)->fd.GetNumber());
  1488. ASSERT_EQ(8U, compaction->input(0, 1)->fd.GetNumber());
  1489. ASSERT_EQ(9U, compaction->input(0, 2)->fd.GetNumber());
  1490. // release the version storage
  1491. DeleteVersionStorage();
  1492. }
  1493. TEST_F(CompactionPickerTest, CompactionPriMultipleFilesRoundRobin2) {
  1494. ioptions_.compaction_pri = kRoundRobin;
  1495. mutable_cf_options_.max_compaction_bytes = 2500u;
  1496. mutable_cf_options_.max_bytes_for_level_base = 120;
  1497. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  1498. // start a brand new version in each test.
  1499. NewVersionStorage(6, kCompactionStyleLevel);
  1500. vstorage_->ResizeCompactCursors(6);
  1501. // Set the cursor (file picking should start with 6U)
  1502. vstorage_->AddCursorForOneLevel(2, InternalKey("1000", 100, kTypeValue));
  1503. Add(2, 6U, "150", "199", 500U); // Overlap with 26U, 27U
  1504. Add(2, 7U, "200", "249", 500U); // Overlap with 27U
  1505. Add(2, 8U, "300", "600", 500U); // Overlap with 28U, 29U
  1506. Add(2, 9U, "700", "800", 500U);
  1507. Add(2, 10U, "850", "950", 500U);
  1508. Add(3, 26U, "130", "165", 600U);
  1509. Add(3, 27U, "166", "230", 600U);
  1510. Add(3, 28U, "270", "340", 600U);
  1511. Add(3, 29U, "401", "500", 600U);
  1512. Add(3, 30U, "601", "800", 600U);
  1513. Add(3, 31U, "830", "890", 600U);
  1514. UpdateVersionStorageInfo();
  1515. LevelCompactionPicker local_level_compaction_picker =
  1516. LevelCompactionPicker(ioptions_, &icmp_);
  1517. std::unique_ptr<Compaction> compaction(
  1518. local_level_compaction_picker.PickCompaction(
  1519. cf_name_, mutable_cf_options_, mutable_db_options_,
  1520. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1521. vstorage_.get(), &log_buffer_));
  1522. ASSERT_TRUE(compaction.get() != nullptr);
  1523. // The maximum compaction bytes is only 2500 bytes now. Even though we are
  1524. // required to choose 3 files so that the post-compaction level size is less
  1525. // than 1200 bytes. We cannot pick 3 files to compact since the maximum
  1526. // compaction size is 2500. After picking files 6U and 7U, the number of
  1527. // compaction bytes has reached 2200, and thus no more space to add another
  1528. // input file with 50M bytes.
  1529. ASSERT_EQ(2U, compaction->num_input_files(0));
  1530. ASSERT_EQ(6U, compaction->input(0, 0)->fd.GetNumber());
  1531. ASSERT_EQ(7U, compaction->input(0, 1)->fd.GetNumber());
  1532. // release the version storage
  1533. DeleteVersionStorage();
  1534. }
  1535. TEST_F(CompactionPickerTest, CompactionPriMultipleFilesRoundRobin3) {
  1536. ioptions_.compaction_pri = kRoundRobin;
  1537. mutable_cf_options_.max_compaction_bytes = 1000000u;
  1538. mutable_cf_options_.max_bytes_for_level_base = 120;
  1539. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  1540. // start a brand new version in each test.
  1541. NewVersionStorage(6, kCompactionStyleLevel);
  1542. vstorage_->ResizeCompactCursors(6);
  1543. // Set the cursor (file picking should start with 9U)
  1544. vstorage_->AddCursorForOneLevel(2, InternalKey("700", 100, kTypeValue));
  1545. Add(2, 6U, "150", "199", 500U);
  1546. Add(2, 7U, "200", "249", 500U);
  1547. Add(2, 8U, "300", "600", 500U);
  1548. Add(2, 9U, "700", "800", 500U);
  1549. Add(2, 10U, "850", "950", 500U);
  1550. Add(3, 26U, "130", "165", 600U);
  1551. Add(3, 27U, "166", "170", 600U);
  1552. Add(3, 28U, "270", "340", 600U);
  1553. Add(3, 29U, "401", "500", 600U);
  1554. Add(3, 30U, "601", "800", 600U);
  1555. Add(3, 31U, "830", "890", 600U);
  1556. UpdateVersionStorageInfo();
  1557. LevelCompactionPicker local_level_compaction_picker =
  1558. LevelCompactionPicker(ioptions_, &icmp_);
  1559. std::unique_ptr<Compaction> compaction(
  1560. local_level_compaction_picker.PickCompaction(
  1561. cf_name_, mutable_cf_options_, mutable_db_options_,
  1562. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1563. vstorage_.get(), &log_buffer_));
  1564. ASSERT_TRUE(compaction.get() != nullptr);
  1565. // Cannot pick more files since we reach the last file in level 2
  1566. ASSERT_EQ(2U, compaction->num_input_files(0));
  1567. ASSERT_EQ(9U, compaction->input(0, 0)->fd.GetNumber());
  1568. ASSERT_EQ(10U, compaction->input(0, 1)->fd.GetNumber());
  1569. // release the version storage
  1570. DeleteVersionStorage();
  1571. }
  1572. TEST_F(CompactionPickerTest, CompactionPriMinOverlappingManyFiles) {
  1573. NewVersionStorage(6, kCompactionStyleLevel);
  1574. ioptions_.compaction_pri = kMinOverlappingRatio;
  1575. mutable_cf_options_.max_bytes_for_level_base = 15000000;
  1576. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  1577. // file 7 and 8 over lap with the same file, but file 8 is smaller so
  1578. // it will be picked.
  1579. Add(2, 13U, "010", "011",
  1580. 6100U); // Overlaps with a large file. Not picked
  1581. Add(2, 14U, "020", "021",
  1582. 6100U); // Overlaps with a large file. Not picked
  1583. Add(2, 15U, "030", "031",
  1584. 6100U); // Overlaps with a large file. Not picked
  1585. Add(2, 16U, "040", "041",
  1586. 6100U); // Overlaps with a large file. Not picked
  1587. Add(2, 17U, "050", "051",
  1588. 6100U); // Overlaps with a large file. Not picked
  1589. Add(2, 18U, "060", "061",
  1590. 6100U); // Overlaps with a large file. Not picked
  1591. Add(2, 19U, "070", "071",
  1592. 6100U); // Overlaps with a large file. Not picked
  1593. Add(2, 20U, "080", "081",
  1594. 6100U); // Overlaps with a large file. Not picked
  1595. Add(2, 6U, "150", "167", 60000000U); // Overlaps with file 26, 27
  1596. Add(2, 7U, "168", "169", 60000000U); // Overlaps with file 27
  1597. Add(2, 8U, "201", "300", 61000000U); // Overlaps with file 28, but the file
  1598. // itself is larger. Should be picked.
  1599. Add(2, 9U, "610", "611",
  1600. 6100U); // Overlaps with a large file. Not picked
  1601. Add(2, 10U, "620", "621",
  1602. 6100U); // Overlaps with a large file. Not picked
  1603. Add(2, 11U, "630", "631",
  1604. 6100U); // Overlaps with a large file. Not picked
  1605. Add(2, 12U, "640", "641",
  1606. 6100U); // Overlaps with a large file. Not picked
  1607. Add(3, 31U, "001", "100", 260000000U);
  1608. Add(3, 26U, "160", "165", 260000000U);
  1609. Add(3, 27U, "166", "170", 260000000U);
  1610. Add(3, 28U, "180", "400", 260000000U);
  1611. Add(3, 29U, "401", "500", 260000000U);
  1612. Add(3, 30U, "601", "700", 260000000U);
  1613. UpdateVersionStorageInfo();
  1614. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1615. cf_name_, mutable_cf_options_, mutable_db_options_,
  1616. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1617. vstorage_.get(), &log_buffer_));
  1618. ASSERT_TRUE(compaction.get() != nullptr);
  1619. ASSERT_EQ(1U, compaction->num_input_files(0));
  1620. // Picking file 8 because overlapping ratio is the biggest.
  1621. ASSERT_EQ(8U, compaction->input(0, 0)->fd.GetNumber());
  1622. }
  1623. // This test exhibits the bug where we don't properly reset parent_index in
  1624. // PickCompaction()
  1625. TEST_F(CompactionPickerTest, ParentIndexResetBug) {
  1626. int num_levels = ioptions_.num_levels;
  1627. mutable_cf_options_.level0_file_num_compaction_trigger = 2;
  1628. mutable_cf_options_.max_bytes_for_level_base = 200;
  1629. NewVersionStorage(num_levels, kCompactionStyleLevel);
  1630. Add(0, 1U, "150", "200"); // <- marked for compaction
  1631. Add(1, 3U, "400", "500", 600); // <- this one needs compacting
  1632. Add(2, 4U, "150", "200");
  1633. Add(2, 5U, "201", "210");
  1634. Add(2, 6U, "300", "310");
  1635. Add(2, 7U, "400", "500"); // <- being compacted
  1636. vstorage_->LevelFiles(2)[3]->being_compacted = true;
  1637. vstorage_->LevelFiles(0)[0]->marked_for_compaction = true;
  1638. UpdateVersionStorageInfo();
  1639. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1640. cf_name_, mutable_cf_options_, mutable_db_options_,
  1641. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1642. vstorage_.get(), &log_buffer_));
  1643. }
  1644. // This test checks ExpandWhileOverlapping() by having overlapping user keys
  1645. // ranges (with different sequence numbers) in the input files.
  1646. TEST_F(CompactionPickerTest, OverlappingUserKeys) {
  1647. NewVersionStorage(6, kCompactionStyleLevel);
  1648. ioptions_.compaction_pri = kByCompensatedSize;
  1649. Add(1, 1U, "100", "150", 1U);
  1650. // Overlapping user keys
  1651. Add(1, 2U, "200", "400", 1U);
  1652. Add(1, 3U, "400", "500", 1000000000U, 0, 0);
  1653. Add(2, 4U, "600", "700", 1U);
  1654. UpdateVersionStorageInfo();
  1655. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1656. cf_name_, mutable_cf_options_, mutable_db_options_,
  1657. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1658. vstorage_.get(), &log_buffer_));
  1659. ASSERT_TRUE(compaction.get() != nullptr);
  1660. ASSERT_EQ(1U, compaction->num_input_levels());
  1661. ASSERT_EQ(2U, compaction->num_input_files(0));
  1662. ASSERT_EQ(2U, compaction->input(0, 0)->fd.GetNumber());
  1663. ASSERT_EQ(3U, compaction->input(0, 1)->fd.GetNumber());
  1664. }
  1665. TEST_F(CompactionPickerTest, OverlappingUserKeys2) {
  1666. NewVersionStorage(6, kCompactionStyleLevel);
  1667. // Overlapping user keys on same level and output level
  1668. Add(1, 1U, "200", "400", 1000000000U);
  1669. Add(1, 2U, "400", "500", 1U, 0, 0);
  1670. Add(2, 3U, "000", "100", 1U);
  1671. Add(2, 4U, "100", "600", 1U, 0, 0);
  1672. Add(2, 5U, "600", "700", 1U, 0, 0);
  1673. UpdateVersionStorageInfo();
  1674. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1675. cf_name_, mutable_cf_options_, mutable_db_options_,
  1676. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1677. vstorage_.get(), &log_buffer_));
  1678. ASSERT_TRUE(compaction.get() != nullptr);
  1679. ASSERT_EQ(2U, compaction->num_input_levels());
  1680. ASSERT_EQ(2U, compaction->num_input_files(0));
  1681. ASSERT_EQ(3U, compaction->num_input_files(1));
  1682. ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
  1683. ASSERT_EQ(2U, compaction->input(0, 1)->fd.GetNumber());
  1684. ASSERT_EQ(3U, compaction->input(1, 0)->fd.GetNumber());
  1685. ASSERT_EQ(4U, compaction->input(1, 1)->fd.GetNumber());
  1686. ASSERT_EQ(5U, compaction->input(1, 2)->fd.GetNumber());
  1687. }
  1688. TEST_F(CompactionPickerTest, OverlappingUserKeys3) {
  1689. NewVersionStorage(6, kCompactionStyleLevel);
  1690. // Chain of overlapping user key ranges (forces ExpandWhileOverlapping() to
  1691. // expand multiple times)
  1692. Add(1, 1U, "100", "150", 1U);
  1693. Add(1, 2U, "150", "200", 1U, 0, 0);
  1694. Add(1, 3U, "200", "250", 1000000000U, 0, 0);
  1695. Add(1, 4U, "250", "300", 1U, 0, 0);
  1696. Add(1, 5U, "300", "350", 1U, 0, 0);
  1697. // Output level overlaps with the beginning and the end of the chain
  1698. Add(2, 6U, "050", "100", 1U);
  1699. Add(2, 7U, "350", "400", 1U);
  1700. UpdateVersionStorageInfo();
  1701. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1702. cf_name_, mutable_cf_options_, mutable_db_options_,
  1703. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1704. vstorage_.get(), &log_buffer_));
  1705. ASSERT_TRUE(compaction.get() != nullptr);
  1706. ASSERT_EQ(2U, compaction->num_input_levels());
  1707. ASSERT_EQ(5U, compaction->num_input_files(0));
  1708. ASSERT_EQ(2U, compaction->num_input_files(1));
  1709. ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
  1710. ASSERT_EQ(2U, compaction->input(0, 1)->fd.GetNumber());
  1711. ASSERT_EQ(3U, compaction->input(0, 2)->fd.GetNumber());
  1712. ASSERT_EQ(4U, compaction->input(0, 3)->fd.GetNumber());
  1713. ASSERT_EQ(5U, compaction->input(0, 4)->fd.GetNumber());
  1714. ASSERT_EQ(6U, compaction->input(1, 0)->fd.GetNumber());
  1715. ASSERT_EQ(7U, compaction->input(1, 1)->fd.GetNumber());
  1716. }
  1717. TEST_F(CompactionPickerTest, OverlappingUserKeys4) {
  1718. NewVersionStorage(6, kCompactionStyleLevel);
  1719. mutable_cf_options_.max_bytes_for_level_base = 1000000;
  1720. Add(1, 1U, "100", "150", 1U);
  1721. Add(1, 2U, "150", "199", 1U, 0, 0);
  1722. Add(1, 3U, "200", "250", 1100000U, 0, 0);
  1723. Add(1, 4U, "251", "300", 1U, 0, 0);
  1724. Add(1, 5U, "300", "350", 1U, 0, 0);
  1725. Add(2, 6U, "100", "115", 1U);
  1726. Add(2, 7U, "125", "325", 1U);
  1727. Add(2, 8U, "350", "400", 1U);
  1728. UpdateVersionStorageInfo();
  1729. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1730. cf_name_, mutable_cf_options_, mutable_db_options_,
  1731. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1732. vstorage_.get(), &log_buffer_));
  1733. ASSERT_TRUE(compaction.get() != nullptr);
  1734. ASSERT_EQ(2U, compaction->num_input_levels());
  1735. ASSERT_EQ(1U, compaction->num_input_files(0));
  1736. ASSERT_EQ(1U, compaction->num_input_files(1));
  1737. ASSERT_EQ(3U, compaction->input(0, 0)->fd.GetNumber());
  1738. ASSERT_EQ(7U, compaction->input(1, 0)->fd.GetNumber());
  1739. }
  1740. TEST_F(CompactionPickerTest, OverlappingUserKeys5) {
  1741. NewVersionStorage(6, kCompactionStyleLevel);
  1742. // Overlapping user keys on same level and output level
  1743. Add(1, 1U, "200", "400", 1000000000U);
  1744. Add(1, 2U, "400", "500", 1U, 0, 0);
  1745. Add(2, 3U, "000", "100", 1U);
  1746. Add(2, 4U, "100", "600", 1U, 0, 0);
  1747. Add(2, 5U, "600", "700", 1U, 0, 0);
  1748. vstorage_->LevelFiles(2)[2]->being_compacted = true;
  1749. UpdateVersionStorageInfo();
  1750. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1751. cf_name_, mutable_cf_options_, mutable_db_options_,
  1752. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1753. vstorage_.get(), &log_buffer_));
  1754. ASSERT_TRUE(compaction.get() == nullptr);
  1755. }
  1756. TEST_F(CompactionPickerTest, OverlappingUserKeys6) {
  1757. NewVersionStorage(6, kCompactionStyleLevel);
  1758. // Overlapping user keys on same level and output level
  1759. Add(1, 1U, "200", "400", 1U, 0, 0);
  1760. Add(1, 2U, "401", "500", 1U, 0, 0);
  1761. Add(2, 3U, "000", "100", 1U);
  1762. Add(2, 4U, "100", "300", 1U, 0, 0);
  1763. Add(2, 5U, "305", "450", 1U, 0, 0);
  1764. Add(2, 6U, "460", "600", 1U, 0, 0);
  1765. Add(2, 7U, "600", "700", 1U, 0, 0);
  1766. vstorage_->LevelFiles(1)[0]->marked_for_compaction = true;
  1767. vstorage_->LevelFiles(1)[1]->marked_for_compaction = true;
  1768. UpdateVersionStorageInfo();
  1769. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1770. cf_name_, mutable_cf_options_, mutable_db_options_,
  1771. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1772. vstorage_.get(), &log_buffer_));
  1773. ASSERT_TRUE(compaction.get() != nullptr);
  1774. ASSERT_EQ(2U, compaction->num_input_levels());
  1775. ASSERT_EQ(1U, compaction->num_input_files(0));
  1776. ASSERT_EQ(3U, compaction->num_input_files(1));
  1777. }
  1778. TEST_F(CompactionPickerTest, OverlappingUserKeys7) {
  1779. NewVersionStorage(6, kCompactionStyleLevel);
  1780. mutable_cf_options_.max_compaction_bytes = 100000000000u;
  1781. // Overlapping user keys on same level and output level
  1782. Add(1, 1U, "200", "400", 1U, 0, 0);
  1783. Add(1, 2U, "401", "500", 1000000000U, 0, 0);
  1784. Add(2, 3U, "100", "250", 1U);
  1785. Add(2, 4U, "300", "600", 1U, 0, 0);
  1786. Add(2, 5U, "600", "800", 1U, 0, 0);
  1787. UpdateVersionStorageInfo();
  1788. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1789. cf_name_, mutable_cf_options_, mutable_db_options_,
  1790. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1791. vstorage_.get(), &log_buffer_));
  1792. ASSERT_TRUE(compaction.get() != nullptr);
  1793. ASSERT_EQ(2U, compaction->num_input_levels());
  1794. ASSERT_GE(1U, compaction->num_input_files(0));
  1795. ASSERT_GE(2U, compaction->num_input_files(1));
  1796. // File 5 has to be included in the compaction
  1797. ASSERT_EQ(5U, compaction->inputs(1)->back()->fd.GetNumber());
  1798. }
  1799. TEST_F(CompactionPickerTest, OverlappingUserKeys8) {
  1800. NewVersionStorage(6, kCompactionStyleLevel);
  1801. mutable_cf_options_.max_compaction_bytes = 100000000000u;
  1802. // grow the number of inputs in "level" without
  1803. // changing the number of "level+1" files we pick up
  1804. // Expand input level as much as possible
  1805. // no overlapping case
  1806. Add(1, 1U, "101", "150", 1U);
  1807. Add(1, 2U, "151", "200", 1U);
  1808. Add(1, 3U, "201", "300", 1000000000U);
  1809. Add(1, 4U, "301", "400", 1U);
  1810. Add(1, 5U, "401", "500", 1U);
  1811. Add(2, 6U, "150", "200", 1U);
  1812. Add(2, 7U, "200", "450", 1U, 0, 0);
  1813. Add(2, 8U, "500", "600", 1U);
  1814. UpdateVersionStorageInfo();
  1815. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1816. cf_name_, mutable_cf_options_, mutable_db_options_,
  1817. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1818. vstorage_.get(), &log_buffer_));
  1819. ASSERT_TRUE(compaction.get() != nullptr);
  1820. ASSERT_EQ(2U, compaction->num_input_levels());
  1821. ASSERT_EQ(3U, compaction->num_input_files(0));
  1822. ASSERT_EQ(2U, compaction->num_input_files(1));
  1823. ASSERT_EQ(2U, compaction->input(0, 0)->fd.GetNumber());
  1824. ASSERT_EQ(3U, compaction->input(0, 1)->fd.GetNumber());
  1825. ASSERT_EQ(4U, compaction->input(0, 2)->fd.GetNumber());
  1826. ASSERT_EQ(6U, compaction->input(1, 0)->fd.GetNumber());
  1827. ASSERT_EQ(7U, compaction->input(1, 1)->fd.GetNumber());
  1828. }
  1829. TEST_F(CompactionPickerTest, OverlappingUserKeys9) {
  1830. NewVersionStorage(6, kCompactionStyleLevel);
  1831. mutable_cf_options_.max_compaction_bytes = 100000000000u;
  1832. // grow the number of inputs in "level" without
  1833. // changing the number of "level+1" files we pick up
  1834. // Expand input level as much as possible
  1835. // overlapping case
  1836. Add(1, 1U, "121", "150", 1U);
  1837. Add(1, 2U, "151", "200", 1U);
  1838. Add(1, 3U, "201", "300", 1000000000U);
  1839. Add(1, 4U, "301", "400", 1U);
  1840. Add(1, 5U, "401", "500", 1U);
  1841. Add(2, 6U, "100", "120", 1U);
  1842. Add(2, 7U, "150", "200", 1U);
  1843. Add(2, 8U, "200", "450", 1U, 0, 0);
  1844. Add(2, 9U, "501", "600", 1U);
  1845. UpdateVersionStorageInfo();
  1846. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1847. cf_name_, mutable_cf_options_, mutable_db_options_,
  1848. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1849. vstorage_.get(), &log_buffer_));
  1850. ASSERT_TRUE(compaction.get() != nullptr);
  1851. ASSERT_EQ(2U, compaction->num_input_levels());
  1852. ASSERT_EQ(5U, compaction->num_input_files(0));
  1853. ASSERT_EQ(2U, compaction->num_input_files(1));
  1854. ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
  1855. ASSERT_EQ(2U, compaction->input(0, 1)->fd.GetNumber());
  1856. ASSERT_EQ(3U, compaction->input(0, 2)->fd.GetNumber());
  1857. ASSERT_EQ(4U, compaction->input(0, 3)->fd.GetNumber());
  1858. ASSERT_EQ(7U, compaction->input(1, 0)->fd.GetNumber());
  1859. ASSERT_EQ(8U, compaction->input(1, 1)->fd.GetNumber());
  1860. }
  1861. TEST_F(CompactionPickerTest, OverlappingUserKeys10) {
  1862. // Locked file encountered when pulling in extra input-level files with same
  1863. // user keys. Verify we pick the next-best file from the same input level.
  1864. NewVersionStorage(6, kCompactionStyleLevel);
  1865. mutable_cf_options_.max_compaction_bytes = 100000000000u;
  1866. // file_number 2U is largest and thus first choice. But it overlaps with
  1867. // file_number 1U which is being compacted. So instead we pick the next-
  1868. // biggest file, 3U, which is eligible for compaction.
  1869. Add(1 /* level */, 1U /* file_number */, "100" /* smallest */,
  1870. "150" /* largest */, 1U /* file_size */);
  1871. file_map_[1U].first->being_compacted = true;
  1872. Add(1 /* level */, 2U /* file_number */, "150" /* smallest */,
  1873. "200" /* largest */, 1000000000U /* file_size */, 0 /* smallest_seq */,
  1874. 0 /* largest_seq */);
  1875. Add(1 /* level */, 3U /* file_number */, "201" /* smallest */,
  1876. "250" /* largest */, 900000000U /* file_size */);
  1877. Add(2 /* level */, 4U /* file_number */, "100" /* smallest */,
  1878. "150" /* largest */, 1U /* file_size */);
  1879. Add(2 /* level */, 5U /* file_number */, "151" /* smallest */,
  1880. "200" /* largest */, 1U /* file_size */);
  1881. Add(2 /* level */, 6U /* file_number */, "201" /* smallest */,
  1882. "250" /* largest */, 1U /* file_size */);
  1883. UpdateVersionStorageInfo();
  1884. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1885. cf_name_, mutable_cf_options_, mutable_db_options_,
  1886. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1887. vstorage_.get(), &log_buffer_));
  1888. ASSERT_TRUE(compaction.get() != nullptr);
  1889. ASSERT_EQ(2U, compaction->num_input_levels());
  1890. ASSERT_EQ(1U, compaction->num_input_files(0));
  1891. ASSERT_EQ(1U, compaction->num_input_files(1));
  1892. ASSERT_EQ(3U, compaction->input(0, 0)->fd.GetNumber());
  1893. ASSERT_EQ(6U, compaction->input(1, 0)->fd.GetNumber());
  1894. }
  1895. TEST_F(CompactionPickerTest, OverlappingUserKeys11) {
  1896. // Locked file encountered when pulling in extra output-level files with same
  1897. // user keys. Expected to skip that compaction and pick the next-best choice.
  1898. NewVersionStorage(6, kCompactionStyleLevel);
  1899. mutable_cf_options_.max_compaction_bytes = 100000000000u;
  1900. // score(L1) = 3.7
  1901. // score(L2) = 1.85
  1902. // There is no eligible file in L1 to compact since both candidates pull in
  1903. // file_number 5U, which overlaps with a file pending compaction (6U). The
  1904. // first eligible compaction is from L2->L3.
  1905. Add(1 /* level */, 2U /* file_number */, "151" /* smallest */,
  1906. "200" /* largest */, 1000000000U /* file_size */);
  1907. Add(1 /* level */, 3U /* file_number */, "201" /* smallest */,
  1908. "250" /* largest */, 1U /* file_size */);
  1909. Add(2 /* level */, 4U /* file_number */, "100" /* smallest */,
  1910. "149" /* largest */, 5000000000U /* file_size */);
  1911. Add(2 /* level */, 5U /* file_number */, "150" /* smallest */,
  1912. "201" /* largest */, 1U /* file_size */);
  1913. Add(2 /* level */, 6U /* file_number */, "201" /* smallest */,
  1914. "249" /* largest */, 1U /* file_size */, 0 /* smallest_seq */,
  1915. 0 /* largest_seq */);
  1916. file_map_[6U].first->being_compacted = true;
  1917. Add(3 /* level */, 7U /* file_number */, "100" /* smallest */,
  1918. "149" /* largest */, 1U /* file_size */);
  1919. UpdateVersionStorageInfo();
  1920. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  1921. cf_name_, mutable_cf_options_, mutable_db_options_,
  1922. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  1923. vstorage_.get(), &log_buffer_));
  1924. ASSERT_TRUE(compaction.get() != nullptr);
  1925. ASSERT_EQ(2U, compaction->num_input_levels());
  1926. ASSERT_EQ(1U, compaction->num_input_files(0));
  1927. ASSERT_EQ(1U, compaction->num_input_files(1));
  1928. ASSERT_EQ(4U, compaction->input(0, 0)->fd.GetNumber());
  1929. ASSERT_EQ(7U, compaction->input(1, 0)->fd.GetNumber());
  1930. }
  1931. TEST_F(CompactionPickerTest, FileTtlBoosterLargeNumLevels) {
  1932. const uint64_t kCurrentTime = 1000000;
  1933. FileTtlBooster booster(kCurrentTime, /*ttl=*/2048,
  1934. /*num_non_empty_levels=*/100, /*level=*/1);
  1935. FileMetaData meta;
  1936. meta.oldest_ancester_time = kCurrentTime - 1023;
  1937. ASSERT_EQ(1, booster.GetBoostScore(&meta));
  1938. }
  1939. TEST_F(CompactionPickerTest, FileTtlBooster) {
  1940. // Set TTL to 2048
  1941. // TTL boosting for all levels starts at 1024,
  1942. // Whole TTL range is 2048 * 31 / 32 - 1024 = 1984 - 1024 = 960.
  1943. // From second last level (L5), range starts at
  1944. // 1024 + 480, 1024 + 240, 1024 + 120 (which is L3).
  1945. // Boosting step 124 / 16 = 7.75 -> 7
  1946. //
  1947. const uint64_t kCurrentTime = 1000000;
  1948. FileMetaData meta;
  1949. {
  1950. FileTtlBooster booster(kCurrentTime, 2048, 7, 3);
  1951. // Not triggering if the file is younger than ttl/2
  1952. meta.oldest_ancester_time = kCurrentTime - 1023;
  1953. ASSERT_EQ(1, booster.GetBoostScore(&meta));
  1954. meta.oldest_ancester_time = kCurrentTime - 1024;
  1955. ASSERT_EQ(1, booster.GetBoostScore(&meta));
  1956. meta.oldest_ancester_time = kCurrentTime + 10;
  1957. ASSERT_EQ(1, booster.GetBoostScore(&meta));
  1958. // Within one boosting step
  1959. meta.oldest_ancester_time = kCurrentTime - (1024 + 120 + 6);
  1960. ASSERT_EQ(1, booster.GetBoostScore(&meta));
  1961. // One boosting step
  1962. meta.oldest_ancester_time = kCurrentTime - (1024 + 120 + 7);
  1963. ASSERT_EQ(2, booster.GetBoostScore(&meta));
  1964. meta.oldest_ancester_time = kCurrentTime - (1024 + 120 + 8);
  1965. ASSERT_EQ(2, booster.GetBoostScore(&meta));
  1966. // Multiple boosting steps
  1967. meta.oldest_ancester_time = kCurrentTime - (1024 + 120 + 30);
  1968. ASSERT_EQ(5, booster.GetBoostScore(&meta));
  1969. // Very high boosting steps
  1970. meta.oldest_ancester_time = kCurrentTime - (1024 + 120 + 700);
  1971. ASSERT_EQ(101, booster.GetBoostScore(&meta));
  1972. }
  1973. {
  1974. // Test second last level
  1975. FileTtlBooster booster(kCurrentTime, 2048, 7, 5);
  1976. meta.oldest_ancester_time = kCurrentTime - (1024 + 480);
  1977. ASSERT_EQ(1, booster.GetBoostScore(&meta));
  1978. meta.oldest_ancester_time = kCurrentTime - (1024 + 480 + 60);
  1979. ASSERT_EQ(3, booster.GetBoostScore(&meta));
  1980. }
  1981. {
  1982. // Test last level
  1983. FileTtlBooster booster(kCurrentTime, 2048, 7, 6);
  1984. meta.oldest_ancester_time = kCurrentTime - (1024 + 480);
  1985. ASSERT_EQ(1, booster.GetBoostScore(&meta));
  1986. meta.oldest_ancester_time = kCurrentTime - (1024 + 480 + 60);
  1987. ASSERT_EQ(1, booster.GetBoostScore(&meta));
  1988. meta.oldest_ancester_time = kCurrentTime - 3000;
  1989. ASSERT_EQ(1, booster.GetBoostScore(&meta));
  1990. }
  1991. }
  1992. TEST_F(CompactionPickerTest, NotScheduleL1IfL0WithHigherPri1) {
  1993. NewVersionStorage(6, kCompactionStyleLevel);
  1994. mutable_cf_options_.level0_file_num_compaction_trigger = 2;
  1995. mutable_cf_options_.max_bytes_for_level_base = 900000000U;
  1996. // 6 L0 files, score 3.
  1997. Add(0, 1U, "000", "400", 1U);
  1998. Add(0, 2U, "001", "400", 1U, 0, 0);
  1999. Add(0, 3U, "001", "400", 1000000000U, 0, 0);
  2000. Add(0, 31U, "001", "400", 1000000000U, 0, 0);
  2001. Add(0, 32U, "001", "400", 1000000000U, 0, 0);
  2002. Add(0, 33U, "001", "400", 1000000000U, 0, 0);
  2003. // L1 total size 2GB, score 2.2. If one file being compacted, score 1.1.
  2004. Add(1, 4U, "050", "300", 1000000000U, 0, 0);
  2005. file_map_[4u].first->being_compacted = true;
  2006. Add(1, 5U, "301", "350", 1000000000U, 0, 0);
  2007. // Output level overlaps with the beginning and the end of the chain
  2008. Add(2, 6U, "050", "100", 1U);
  2009. Add(2, 7U, "300", "400", 1U);
  2010. // No compaction should be scheduled, if L0 has higher priority than L1
  2011. // but L0->L1 compaction is blocked by a file in L1 being compacted.
  2012. UpdateVersionStorageInfo();
  2013. ASSERT_EQ(0, vstorage_->CompactionScoreLevel(0));
  2014. ASSERT_EQ(1, vstorage_->CompactionScoreLevel(1));
  2015. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2016. cf_name_, mutable_cf_options_, mutable_db_options_,
  2017. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2018. vstorage_.get(), &log_buffer_));
  2019. ASSERT_TRUE(compaction.get() == nullptr);
  2020. }
  2021. TEST_F(CompactionPickerTest, NotScheduleL1IfL0WithHigherPri2) {
  2022. NewVersionStorage(6, kCompactionStyleLevel);
  2023. mutable_cf_options_.level0_file_num_compaction_trigger = 2;
  2024. mutable_cf_options_.max_bytes_for_level_base = 900000000U;
  2025. // 6 L0 files, score 3.
  2026. Add(0, 1U, "000", "400", 1U);
  2027. Add(0, 2U, "001", "400", 1U, 0, 0);
  2028. Add(0, 3U, "001", "400", 1000000000U, 0, 0);
  2029. Add(0, 31U, "001", "400", 1000000000U, 0, 0);
  2030. Add(0, 32U, "001", "400", 1000000000U, 0, 0);
  2031. Add(0, 33U, "001", "400", 1000000000U, 0, 0);
  2032. // L1 total size 2GB, score 2.2. If one file being compacted, score 1.1.
  2033. Add(1, 4U, "050", "300", 1000000000U, 0, 0);
  2034. Add(1, 5U, "301", "350", 1000000000U, 0, 0);
  2035. // Output level overlaps with the beginning and the end of the chain
  2036. Add(2, 6U, "050", "100", 1U);
  2037. Add(2, 7U, "300", "400", 1U);
  2038. // If no file in L1 being compacted, L0->L1 compaction will be scheduled.
  2039. UpdateVersionStorageInfo(); // being_compacted flag is cleared here.
  2040. ASSERT_EQ(0, vstorage_->CompactionScoreLevel(0));
  2041. ASSERT_EQ(1, vstorage_->CompactionScoreLevel(1));
  2042. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2043. cf_name_, mutable_cf_options_, mutable_db_options_,
  2044. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2045. vstorage_.get(), &log_buffer_));
  2046. ASSERT_TRUE(compaction.get() != nullptr);
  2047. }
  2048. TEST_F(CompactionPickerTest, NotScheduleL1IfL0WithHigherPri3) {
  2049. NewVersionStorage(6, kCompactionStyleLevel);
  2050. mutable_cf_options_.level0_file_num_compaction_trigger = 2;
  2051. mutable_cf_options_.max_bytes_for_level_base = 900000000U;
  2052. // 6 L0 files, score 3.
  2053. Add(0, 1U, "000", "400", 1U);
  2054. Add(0, 2U, "001", "400", 1U, 0, 0);
  2055. Add(0, 3U, "001", "400", 1000000000U, 0, 0);
  2056. Add(0, 31U, "001", "400", 1000000000U, 0, 0);
  2057. Add(0, 32U, "001", "400", 1000000000U, 0, 0);
  2058. Add(0, 33U, "001", "400", 1000000000U, 0, 0);
  2059. // L1 score more than 6.
  2060. Add(1, 4U, "050", "300", 1000000000U, 0, 0);
  2061. file_map_[4u].first->being_compacted = true;
  2062. Add(1, 5U, "301", "350", 1000000000U, 0, 0);
  2063. Add(1, 51U, "351", "400", 6000000000U, 0, 0);
  2064. // Output level overlaps with the beginning and the end of the chain
  2065. Add(2, 6U, "050", "100", 1U);
  2066. Add(2, 7U, "300", "400", 1U);
  2067. // If score in L1 is larger than L0, L1 compaction goes through despite
  2068. // there is pending L0 compaction.
  2069. UpdateVersionStorageInfo();
  2070. ASSERT_EQ(1, vstorage_->CompactionScoreLevel(0));
  2071. ASSERT_EQ(0, vstorage_->CompactionScoreLevel(1));
  2072. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2073. cf_name_, mutable_cf_options_, mutable_db_options_,
  2074. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2075. vstorage_.get(), &log_buffer_));
  2076. ASSERT_TRUE(compaction.get() != nullptr);
  2077. }
  2078. TEST_F(CompactionPickerTest, EstimateCompactionBytesNeeded1) {
  2079. int num_levels = ioptions_.num_levels;
  2080. ioptions_.level_compaction_dynamic_level_bytes = false;
  2081. mutable_cf_options_.level0_file_num_compaction_trigger = 4;
  2082. mutable_cf_options_.max_bytes_for_level_base = 1000;
  2083. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  2084. NewVersionStorage(num_levels, kCompactionStyleLevel);
  2085. Add(0, 1U, "150", "200", 200);
  2086. Add(0, 2U, "150", "200", 200);
  2087. Add(0, 3U, "150", "200", 200);
  2088. // Level 1 is over target by 200
  2089. Add(1, 4U, "400", "500", 600);
  2090. Add(1, 5U, "600", "700", 600);
  2091. // Level 2 is less than target 10000 even added size of level 1
  2092. // Size ratio of L2/L1 is 9600 / 1200 = 8
  2093. Add(2, 6U, "150", "200", 2500);
  2094. Add(2, 7U, "201", "210", 2000);
  2095. Add(2, 8U, "300", "310", 2600);
  2096. Add(2, 9U, "400", "500", 2500);
  2097. // Level 3 exceeds target 100,000 of 1000
  2098. Add(3, 10U, "400", "500", 101000);
  2099. // Level 4 exceeds target 1,000,000 by 900 after adding size from level 3
  2100. // Size ratio L4/L3 is 9.9
  2101. // After merge from L3, L4 size is 1000900
  2102. Add(4, 11U, "400", "500", 999900);
  2103. Add(5, 12U, "400", "500", 8007200);
  2104. UpdateVersionStorageInfo();
  2105. ASSERT_EQ(200u * 9u + 10900u + 900u * 9,
  2106. vstorage_->estimated_compaction_needed_bytes());
  2107. }
  2108. TEST_F(CompactionPickerTest, EstimateCompactionBytesNeeded2) {
  2109. int num_levels = ioptions_.num_levels;
  2110. ioptions_.level_compaction_dynamic_level_bytes = false;
  2111. mutable_cf_options_.level0_file_num_compaction_trigger = 3;
  2112. mutable_cf_options_.max_bytes_for_level_base = 1000;
  2113. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  2114. NewVersionStorage(num_levels, kCompactionStyleLevel);
  2115. Add(0, 1U, "150", "200", 200);
  2116. Add(0, 2U, "150", "200", 200);
  2117. Add(0, 4U, "150", "200", 200);
  2118. Add(0, 5U, "150", "200", 200);
  2119. Add(0, 6U, "150", "200", 200);
  2120. // Level 1 size will be 1400 after merging with L0
  2121. Add(1, 7U, "400", "500", 200);
  2122. Add(1, 8U, "600", "700", 200);
  2123. // Level 2 is less than target 10000 even added size of level 1
  2124. Add(2, 9U, "150", "200", 9100);
  2125. // Level 3 over the target, but since level 4 is empty, we assume it will be
  2126. // a trivial move.
  2127. Add(3, 10U, "400", "500", 101000);
  2128. UpdateVersionStorageInfo();
  2129. // estimated L1->L2 merge: 400 * (9100.0 / 1400.0 + 1.0)
  2130. ASSERT_EQ(1400u + 3000u, vstorage_->estimated_compaction_needed_bytes());
  2131. }
  2132. TEST_F(CompactionPickerTest, EstimateCompactionBytesNeeded3) {
  2133. int num_levels = ioptions_.num_levels;
  2134. ioptions_.level_compaction_dynamic_level_bytes = false;
  2135. mutable_cf_options_.level0_file_num_compaction_trigger = 3;
  2136. mutable_cf_options_.max_bytes_for_level_base = 1000;
  2137. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  2138. NewVersionStorage(num_levels, kCompactionStyleLevel);
  2139. Add(0, 1U, "150", "200", 2000);
  2140. Add(0, 2U, "150", "200", 2000);
  2141. Add(0, 4U, "150", "200", 2000);
  2142. Add(0, 5U, "150", "200", 2000);
  2143. Add(0, 6U, "150", "200", 1000);
  2144. // Level 1 size will be 10000 after merging with L0
  2145. Add(1, 7U, "400", "500", 500);
  2146. Add(1, 8U, "600", "700", 500);
  2147. Add(2, 9U, "150", "200", 10000);
  2148. UpdateVersionStorageInfo();
  2149. ASSERT_EQ(10000u + 18000u, vstorage_->estimated_compaction_needed_bytes());
  2150. }
  2151. TEST_F(CompactionPickerTest, EstimateCompactionBytesNeededDynamicLevel) {
  2152. int num_levels = ioptions_.num_levels;
  2153. ioptions_.level_compaction_dynamic_level_bytes = true;
  2154. mutable_cf_options_.level0_file_num_compaction_trigger = 3;
  2155. mutable_cf_options_.max_bytes_for_level_base = 1000;
  2156. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  2157. NewVersionStorage(num_levels, kCompactionStyleLevel);
  2158. // Set Last level size 50000
  2159. // num_levels - 1 target 5000
  2160. // num_levels - 2 is base level with target 1000 (rounded up to
  2161. // max_bytes_for_level_base).
  2162. Add(num_levels - 1, 10U, "400", "500", 50000);
  2163. Add(0, 1U, "150", "200", 200);
  2164. Add(0, 2U, "150", "200", 200);
  2165. Add(0, 4U, "150", "200", 200);
  2166. Add(0, 5U, "150", "200", 200);
  2167. Add(0, 6U, "150", "200", 200);
  2168. // num_levels - 3 is over target by 100 + 1000
  2169. Add(num_levels - 3, 7U, "400", "500", 550);
  2170. Add(num_levels - 3, 8U, "600", "700", 550);
  2171. // num_levels - 2 is over target by 1100 + 200
  2172. Add(num_levels - 2, 9U, "150", "200", 5200);
  2173. UpdateVersionStorageInfo();
  2174. // Merging to the second last level: (5200 / 2100 + 1) * 1100
  2175. // Merging to the last level: (50000 / 6300 + 1) * 1300
  2176. ASSERT_EQ(2100u + 3823u + 11617u,
  2177. vstorage_->estimated_compaction_needed_bytes());
  2178. }
  2179. TEST_F(CompactionPickerTest, IsBottommostLevelTest) {
  2180. // case 1: Higher levels are empty
  2181. NewVersionStorage(6, kCompactionStyleLevel);
  2182. Add(0, 1U, "a", "m");
  2183. Add(0, 2U, "c", "z");
  2184. Add(1, 3U, "d", "e");
  2185. Add(1, 4U, "l", "p");
  2186. Add(2, 5U, "g", "i");
  2187. Add(2, 6U, "x", "z");
  2188. UpdateVersionStorageInfo();
  2189. SetCompactionInputFilesLevels(2, 1);
  2190. AddToCompactionFiles(3U);
  2191. AddToCompactionFiles(5U);
  2192. bool result =
  2193. Compaction::TEST_IsBottommostLevel(2, vstorage_.get(), input_files_);
  2194. ASSERT_TRUE(result);
  2195. // case 2: Higher levels have no overlap
  2196. NewVersionStorage(6, kCompactionStyleLevel);
  2197. Add(0, 1U, "a", "m");
  2198. Add(0, 2U, "c", "z");
  2199. Add(1, 3U, "d", "e");
  2200. Add(1, 4U, "l", "p");
  2201. Add(2, 5U, "g", "i");
  2202. Add(2, 6U, "x", "z");
  2203. Add(3, 7U, "k", "p");
  2204. Add(3, 8U, "t", "w");
  2205. Add(4, 9U, "a", "b");
  2206. Add(5, 10U, "c", "cc");
  2207. UpdateVersionStorageInfo();
  2208. SetCompactionInputFilesLevels(2, 1);
  2209. AddToCompactionFiles(3U);
  2210. AddToCompactionFiles(5U);
  2211. result = Compaction::TEST_IsBottommostLevel(2, vstorage_.get(), input_files_);
  2212. ASSERT_TRUE(result);
  2213. // case 3.1: Higher levels (level 3) have overlap
  2214. NewVersionStorage(6, kCompactionStyleLevel);
  2215. Add(0, 1U, "a", "m");
  2216. Add(0, 2U, "c", "z");
  2217. Add(1, 3U, "d", "e");
  2218. Add(1, 4U, "l", "p");
  2219. Add(2, 5U, "g", "i");
  2220. Add(2, 6U, "x", "z");
  2221. Add(3, 7U, "e", "g");
  2222. Add(3, 8U, "h", "k");
  2223. Add(4, 9U, "a", "b");
  2224. Add(5, 10U, "c", "cc");
  2225. UpdateVersionStorageInfo();
  2226. SetCompactionInputFilesLevels(2, 1);
  2227. AddToCompactionFiles(3U);
  2228. AddToCompactionFiles(5U);
  2229. result = Compaction::TEST_IsBottommostLevel(2, vstorage_.get(), input_files_);
  2230. ASSERT_FALSE(result);
  2231. // case 3.2: Higher levels (level 5) have overlap
  2232. DeleteVersionStorage();
  2233. NewVersionStorage(6, kCompactionStyleLevel);
  2234. Add(0, 1U, "a", "m");
  2235. Add(0, 2U, "c", "z");
  2236. Add(1, 3U, "d", "e");
  2237. Add(1, 4U, "l", "p");
  2238. Add(2, 5U, "g", "i");
  2239. Add(2, 6U, "x", "z");
  2240. Add(3, 7U, "j", "k");
  2241. Add(3, 8U, "l", "m");
  2242. Add(4, 9U, "a", "b");
  2243. Add(5, 10U, "c", "cc");
  2244. Add(5, 11U, "h", "k");
  2245. Add(5, 12U, "y", "yy");
  2246. Add(5, 13U, "z", "zz");
  2247. UpdateVersionStorageInfo();
  2248. SetCompactionInputFilesLevels(2, 1);
  2249. AddToCompactionFiles(3U);
  2250. AddToCompactionFiles(5U);
  2251. result = Compaction::TEST_IsBottommostLevel(2, vstorage_.get(), input_files_);
  2252. ASSERT_FALSE(result);
  2253. // case 3.3: Higher levels (level 5) have overlap, but it's only overlapping
  2254. // one key ("d")
  2255. NewVersionStorage(6, kCompactionStyleLevel);
  2256. Add(0, 1U, "a", "m");
  2257. Add(0, 2U, "c", "z");
  2258. Add(1, 3U, "d", "e");
  2259. Add(1, 4U, "l", "p");
  2260. Add(2, 5U, "g", "i");
  2261. Add(2, 6U, "x", "z");
  2262. Add(3, 7U, "j", "k");
  2263. Add(3, 8U, "l", "m");
  2264. Add(4, 9U, "a", "b");
  2265. Add(5, 10U, "c", "cc");
  2266. Add(5, 11U, "ccc", "d");
  2267. Add(5, 12U, "y", "yy");
  2268. Add(5, 13U, "z", "zz");
  2269. UpdateVersionStorageInfo();
  2270. SetCompactionInputFilesLevels(2, 1);
  2271. AddToCompactionFiles(3U);
  2272. AddToCompactionFiles(5U);
  2273. result = Compaction::TEST_IsBottommostLevel(2, vstorage_.get(), input_files_);
  2274. ASSERT_FALSE(result);
  2275. // Level 0 files overlap
  2276. NewVersionStorage(6, kCompactionStyleLevel);
  2277. Add(0, 1U, "s", "t");
  2278. Add(0, 2U, "a", "m");
  2279. Add(0, 3U, "b", "z");
  2280. Add(0, 4U, "e", "f");
  2281. Add(5, 10U, "y", "z");
  2282. UpdateVersionStorageInfo();
  2283. SetCompactionInputFilesLevels(1, 0);
  2284. AddToCompactionFiles(1U);
  2285. AddToCompactionFiles(2U);
  2286. AddToCompactionFiles(3U);
  2287. AddToCompactionFiles(4U);
  2288. result = Compaction::TEST_IsBottommostLevel(2, vstorage_.get(), input_files_);
  2289. ASSERT_FALSE(result);
  2290. // Level 0 files don't overlap
  2291. NewVersionStorage(6, kCompactionStyleLevel);
  2292. Add(0, 1U, "s", "t");
  2293. Add(0, 2U, "a", "m");
  2294. Add(0, 3U, "b", "k");
  2295. Add(0, 4U, "e", "f");
  2296. Add(5, 10U, "y", "z");
  2297. UpdateVersionStorageInfo();
  2298. SetCompactionInputFilesLevels(1, 0);
  2299. AddToCompactionFiles(1U);
  2300. AddToCompactionFiles(2U);
  2301. AddToCompactionFiles(3U);
  2302. AddToCompactionFiles(4U);
  2303. result = Compaction::TEST_IsBottommostLevel(2, vstorage_.get(), input_files_);
  2304. ASSERT_TRUE(result);
  2305. // Level 1 files overlap
  2306. NewVersionStorage(6, kCompactionStyleLevel);
  2307. Add(0, 1U, "s", "t");
  2308. Add(0, 2U, "a", "m");
  2309. Add(0, 3U, "b", "k");
  2310. Add(0, 4U, "e", "f");
  2311. Add(1, 5U, "a", "m");
  2312. Add(1, 6U, "n", "o");
  2313. Add(1, 7U, "w", "y");
  2314. Add(5, 10U, "y", "z");
  2315. UpdateVersionStorageInfo();
  2316. SetCompactionInputFilesLevels(2, 0);
  2317. AddToCompactionFiles(1U);
  2318. AddToCompactionFiles(2U);
  2319. AddToCompactionFiles(3U);
  2320. AddToCompactionFiles(4U);
  2321. AddToCompactionFiles(5U);
  2322. AddToCompactionFiles(6U);
  2323. AddToCompactionFiles(7U);
  2324. result = Compaction::TEST_IsBottommostLevel(2, vstorage_.get(), input_files_);
  2325. ASSERT_FALSE(result);
  2326. DeleteVersionStorage();
  2327. }
  2328. TEST_F(CompactionPickerTest, CompactionLimitWhenAddFileFromInputLevel) {
  2329. mutable_cf_options_.max_bytes_for_level_base = 1000000u;
  2330. mutable_cf_options_.max_compaction_bytes = 800000u;
  2331. ioptions_.level_compaction_dynamic_level_bytes = false;
  2332. NewVersionStorage(6, kCompactionStyleLevel);
  2333. // A compaction should be triggered and pick file 2 and 5.
  2334. // It pulls in other compaction input file from the input level L1
  2335. // without pulling in more output level files.
  2336. // Files 1, 3, 4 are eligible.
  2337. // File 6 is excluded since it overlaps with file 7.
  2338. // It can expand input level since in this case, the limit on compaction size
  2339. // is 2 * max_compaction_bytes.
  2340. Add(1, 1U, "100", "150", 300000U);
  2341. Add(1, 2U, "151", "200", 300001U, 0, 0);
  2342. Add(1, 3U, "201", "250", 300000U, 0, 0);
  2343. Add(1, 4U, "251", "300", 300000U, 0, 0);
  2344. Add(1, 6U, "325", "400", 300000U, 0, 0);
  2345. Add(2, 5U, "100", "350", 1U);
  2346. Add(2, 7U, "375", "425", 1U);
  2347. UpdateVersionStorageInfo();
  2348. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2349. cf_name_, mutable_cf_options_, mutable_db_options_,
  2350. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2351. vstorage_.get(), &log_buffer_));
  2352. ASSERT_TRUE(compaction.get() != nullptr);
  2353. ASSERT_EQ(2U, compaction->num_input_levels());
  2354. ASSERT_EQ(4U, compaction->num_input_files(0));
  2355. ASSERT_EQ(1U, compaction->num_input_files(1));
  2356. ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
  2357. ASSERT_EQ(2U, compaction->input(0, 1)->fd.GetNumber());
  2358. ASSERT_EQ(3U, compaction->input(0, 2)->fd.GetNumber());
  2359. ASSERT_EQ(4U, compaction->input(0, 3)->fd.GetNumber());
  2360. ASSERT_EQ(5U, compaction->input(1, 0)->fd.GetNumber());
  2361. }
  2362. TEST_F(CompactionPickerTest, HitCompactionLimitWhenAddFileFromInputLevel) {
  2363. mutable_cf_options_.max_bytes_for_level_base = 1000000u;
  2364. mutable_cf_options_.max_compaction_bytes = 800000u;
  2365. ioptions_.level_compaction_dynamic_level_bytes = false;
  2366. NewVersionStorage(6, kCompactionStyleLevel);
  2367. // A compaction should be triggered and pick file 2 and 5.
  2368. // It pulls in other compaction input file from the input level L1
  2369. // without pulling in more output level files.
  2370. // Files 1, 3, 4 are eligible.
  2371. // File 6 is excluded since it overlaps with file 7.
  2372. // It can not expand input level since total compaction size hit the limit
  2373. // 2 * max_compaction_bytes.
  2374. Add(1, 1U, "100", "150", 400000U);
  2375. Add(1, 2U, "151", "200", 400001U, 0, 0);
  2376. Add(1, 3U, "201", "250", 400000U, 0, 0);
  2377. Add(1, 4U, "251", "300", 400000U, 0, 0);
  2378. Add(1, 6U, "325", "400", 400000U, 0, 0);
  2379. Add(2, 5U, "100", "350", 1U);
  2380. Add(2, 7U, "375", "425", 1U);
  2381. UpdateVersionStorageInfo();
  2382. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2383. cf_name_, mutable_cf_options_, mutable_db_options_,
  2384. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2385. vstorage_.get(), &log_buffer_));
  2386. ASSERT_TRUE(compaction.get() != nullptr);
  2387. ASSERT_EQ(2U, compaction->num_input_levels());
  2388. ASSERT_EQ(1U, compaction->num_input_files(0));
  2389. ASSERT_EQ(1U, compaction->num_input_files(1));
  2390. ASSERT_EQ(2U, compaction->input(0, 0)->fd.GetNumber());
  2391. ASSERT_EQ(5U, compaction->input(1, 0)->fd.GetNumber());
  2392. }
  2393. TEST_F(CompactionPickerTest, CompactRangeMaxCompactionBytes) {
  2394. mutable_cf_options_.max_compaction_bytes = 800000U;
  2395. NewVersionStorage(6, kCompactionStyleLevel);
  2396. // We will first pick file 1 and 2 and then stop before file 3.
  2397. // Since picking file 3 will pull in file 4 and 5 from L2 and
  2398. // exceed max_compaction_bytes.
  2399. Add(1, 1U, "100", "110", 10000U);
  2400. Add(1, 2U, "200", "210", 10000U, 0, 0);
  2401. Add(1, 3U, "400", "410", 10000U, 0, 0);
  2402. Add(2, 4U, "300", "310", 400000U);
  2403. Add(2, 5U, "320", "330", 400000U);
  2404. UpdateVersionStorageInfo();
  2405. bool manual_conflict = false;
  2406. InternalKey manual_end;
  2407. InternalKey* manual_end_ptr = &manual_end;
  2408. std::unique_ptr<Compaction> compaction(
  2409. level_compaction_picker.PickCompactionForCompactRange(
  2410. cf_name_, mutable_cf_options_, mutable_db_options_, vstorage_.get(),
  2411. /*input_level=*/1, /*output_level=*/2,
  2412. /*compact_range_options*/ {}, /*begin=*/nullptr, /*end=*/nullptr,
  2413. &manual_end_ptr, &manual_conflict,
  2414. /*max_file_num_to_ignore=*/std::numeric_limits<uint64_t>::max(),
  2415. /*trim_ts=*/""));
  2416. ASSERT_TRUE(compaction.get() != nullptr);
  2417. ASSERT_EQ(1U, compaction->num_input_levels());
  2418. ASSERT_EQ(2, compaction->output_level());
  2419. ASSERT_EQ(2U, compaction->num_input_files(0));
  2420. ASSERT_EQ(1U, compaction->input(0, 0)->fd.GetNumber());
  2421. ASSERT_EQ(2U, compaction->input(0, 1)->fd.GetNumber());
  2422. }
  2423. TEST_F(CompactionPickerTest, IsTrivialMoveOn) {
  2424. mutable_cf_options_.max_bytes_for_level_base = 10000u;
  2425. mutable_cf_options_.max_compaction_bytes = 10001u;
  2426. ioptions_.level_compaction_dynamic_level_bytes = false;
  2427. NewVersionStorage(6, kCompactionStyleLevel);
  2428. // A compaction should be triggered and pick file 2
  2429. Add(1, 1U, "100", "150", 3000U);
  2430. Add(1, 2U, "151", "200", 3001U);
  2431. Add(1, 3U, "201", "250", 3000U);
  2432. Add(1, 4U, "251", "300", 3000U);
  2433. Add(3, 5U, "120", "130", 7000U);
  2434. Add(3, 6U, "170", "180", 7000U);
  2435. Add(3, 7U, "220", "230", 7000U);
  2436. Add(3, 8U, "270", "280", 7000U);
  2437. UpdateVersionStorageInfo();
  2438. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2439. cf_name_, mutable_cf_options_, mutable_db_options_,
  2440. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2441. vstorage_.get(), &log_buffer_));
  2442. ASSERT_TRUE(compaction.get() != nullptr);
  2443. ASSERT_TRUE(compaction->IsTrivialMove());
  2444. }
  2445. TEST_F(CompactionPickerTest, L0TrivialMove1) {
  2446. mutable_cf_options_.max_bytes_for_level_base = 10000000u;
  2447. mutable_cf_options_.level0_file_num_compaction_trigger = 4;
  2448. mutable_cf_options_.max_compaction_bytes = 10000000u;
  2449. ioptions_.level_compaction_dynamic_level_bytes = false;
  2450. NewVersionStorage(6, kCompactionStyleLevel);
  2451. Add(0, 1U, "100", "150", 3000U, 0, 710, 800);
  2452. Add(0, 2U, "151", "200", 3001U, 0, 610, 700);
  2453. Add(0, 3U, "301", "350", 3000U, 0, 510, 600);
  2454. Add(0, 4U, "451", "400", 3000U, 0, 410, 500);
  2455. Add(1, 5U, "120", "130", 7000U);
  2456. Add(1, 6U, "170", "180", 7000U);
  2457. Add(1, 7U, "220", "230", 7000U);
  2458. Add(1, 8U, "270", "280", 7000U);
  2459. UpdateVersionStorageInfo();
  2460. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2461. cf_name_, mutable_cf_options_, mutable_db_options_,
  2462. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2463. vstorage_.get(), &log_buffer_));
  2464. ASSERT_TRUE(compaction.get() != nullptr);
  2465. ASSERT_EQ(1, compaction->num_input_levels());
  2466. ASSERT_EQ(2, compaction->num_input_files(0));
  2467. ASSERT_EQ(3, compaction->input(0, 0)->fd.GetNumber());
  2468. ASSERT_EQ(4, compaction->input(0, 1)->fd.GetNumber());
  2469. ASSERT_TRUE(compaction->IsTrivialMove());
  2470. }
  2471. TEST_F(CompactionPickerTest, L0TrivialMoveOneFile) {
  2472. mutable_cf_options_.max_bytes_for_level_base = 10000000u;
  2473. mutable_cf_options_.level0_file_num_compaction_trigger = 4;
  2474. mutable_cf_options_.max_compaction_bytes = 10000000u;
  2475. ioptions_.level_compaction_dynamic_level_bytes = false;
  2476. NewVersionStorage(6, kCompactionStyleLevel);
  2477. Add(0, 1U, "100", "150", 3000U, 0, 710, 800);
  2478. Add(0, 2U, "551", "600", 3001U, 0, 610, 700);
  2479. Add(0, 3U, "101", "150", 3000U, 0, 510, 600);
  2480. Add(0, 4U, "451", "400", 3000U, 0, 410, 500);
  2481. Add(1, 5U, "120", "130", 7000U);
  2482. Add(1, 6U, "170", "180", 7000U);
  2483. Add(1, 7U, "220", "230", 7000U);
  2484. Add(1, 8U, "270", "280", 7000U);
  2485. UpdateVersionStorageInfo();
  2486. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2487. cf_name_, mutable_cf_options_, mutable_db_options_,
  2488. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2489. vstorage_.get(), &log_buffer_));
  2490. ASSERT_TRUE(compaction.get() != nullptr);
  2491. ASSERT_EQ(1, compaction->num_input_levels());
  2492. ASSERT_EQ(1, compaction->num_input_files(0));
  2493. ASSERT_EQ(4, compaction->input(0, 0)->fd.GetNumber());
  2494. ASSERT_TRUE(compaction->IsTrivialMove());
  2495. }
  2496. TEST_F(CompactionPickerTest, L0TrivialMoveWholeL0) {
  2497. mutable_cf_options_.max_bytes_for_level_base = 10000000u;
  2498. mutable_cf_options_.level0_file_num_compaction_trigger = 4;
  2499. mutable_cf_options_.max_compaction_bytes = 10000000u;
  2500. ioptions_.level_compaction_dynamic_level_bytes = false;
  2501. NewVersionStorage(6, kCompactionStyleLevel);
  2502. Add(0, 1U, "300", "350", 3000U, 0, 710, 800);
  2503. Add(0, 2U, "651", "600", 3001U, 0, 610, 700);
  2504. Add(0, 3U, "501", "550", 3000U, 0, 510, 600);
  2505. Add(0, 4U, "451", "400", 3000U, 0, 410, 500);
  2506. Add(1, 5U, "120", "130", 7000U);
  2507. Add(1, 6U, "970", "980", 7000U);
  2508. UpdateVersionStorageInfo();
  2509. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2510. cf_name_, mutable_cf_options_, mutable_db_options_,
  2511. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2512. vstorage_.get(), &log_buffer_));
  2513. ASSERT_TRUE(compaction.get() != nullptr);
  2514. ASSERT_EQ(1, compaction->num_input_levels());
  2515. ASSERT_EQ(4, compaction->num_input_files(0));
  2516. ASSERT_EQ(1, compaction->input(0, 0)->fd.GetNumber());
  2517. ASSERT_EQ(4, compaction->input(0, 1)->fd.GetNumber());
  2518. ASSERT_EQ(3, compaction->input(0, 2)->fd.GetNumber());
  2519. ASSERT_EQ(2, compaction->input(0, 3)->fd.GetNumber());
  2520. ASSERT_TRUE(compaction->IsTrivialMove());
  2521. }
  2522. TEST_F(CompactionPickerTest, NonL0TrivialMoveExtendBothDirection) {
  2523. mutable_cf_options_.max_bytes_for_level_base = 5000;
  2524. mutable_cf_options_.level0_file_num_compaction_trigger = 4;
  2525. mutable_cf_options_.max_compaction_bytes = 10000000u;
  2526. ioptions_.level_compaction_dynamic_level_bytes = false;
  2527. NewVersionStorage(6, kCompactionStyleLevel);
  2528. Add(1, 1U, "300", "350", 3000U, 0, 710, 800, 3000U);
  2529. Add(1, 2U, "600", "651", 3001U, 0, 610, 700, 3001U);
  2530. Add(1, 3U, "700", "750", 3000U, 0, 500, 550, 3000U);
  2531. Add(2, 4U, "800", "850", 4000U, 0, 150, 200, 4000U);
  2532. UpdateVersionStorageInfo();
  2533. // File #2 should be picked first, and expand both directions to include
  2534. // files #1 and #3.
  2535. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2536. cf_name_, mutable_cf_options_, mutable_db_options_,
  2537. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2538. vstorage_.get(), &log_buffer_));
  2539. ASSERT_TRUE(compaction.get() != nullptr);
  2540. ASSERT_EQ(1, compaction->num_input_levels());
  2541. ASSERT_EQ(3, compaction->num_input_files(0));
  2542. ASSERT_EQ(1, compaction->input(0, 0)->fd.GetNumber());
  2543. ASSERT_EQ(2, compaction->input(0, 1)->fd.GetNumber());
  2544. ASSERT_EQ(3, compaction->input(0, 2)->fd.GetNumber());
  2545. ASSERT_TRUE(compaction->IsTrivialMove());
  2546. }
  2547. TEST_F(CompactionPickerTest, L0TrivialMoveToEmptyLevel) {
  2548. mutable_cf_options_.max_bytes_for_level_base = 5000;
  2549. mutable_cf_options_.level0_file_num_compaction_trigger = 4;
  2550. mutable_cf_options_.max_compaction_bytes = 10000000u;
  2551. ioptions_.level_compaction_dynamic_level_bytes = false;
  2552. NewVersionStorage(6, kCompactionStyleLevel);
  2553. // File 2 will be picked first, which by itself is trivial movable.
  2554. // There was a bug before where compaction also picks file 3 and 4,
  2555. // (and then file 1 since it overlaps with the key range),
  2556. // which makes the compaction not trivial movable.
  2557. Add(0, 1U, "450", "599", 3000U, 0, 710, 800, 3000U);
  2558. Add(0, 2U, "600", "651", 3001U, 0, 610, 700, 3001U);
  2559. Add(0, 3U, "300", "350", 3000U, 0, 500, 550, 3000U);
  2560. Add(0, 4U, "500", "550", 2999U, 0, 300, 350, 2999U);
  2561. UpdateVersionStorageInfo();
  2562. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2563. cf_name_, mutable_cf_options_, mutable_db_options_,
  2564. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2565. vstorage_.get(), &log_buffer_));
  2566. ASSERT_TRUE(compaction.get() != nullptr);
  2567. ASSERT_EQ(1, compaction->num_input_levels());
  2568. ASSERT_EQ(1, compaction->num_input_files(0));
  2569. ASSERT_EQ(2, compaction->input(0, 0)->fd.GetNumber());
  2570. ASSERT_TRUE(compaction->IsTrivialMove());
  2571. }
  2572. TEST_F(CompactionPickerTest, IsTrivialMoveOffSstPartitioned) {
  2573. mutable_cf_options_.max_bytes_for_level_base = 10000u;
  2574. mutable_cf_options_.max_compaction_bytes = 10001u;
  2575. ioptions_.level_compaction_dynamic_level_bytes = false;
  2576. ioptions_.sst_partitioner_factory = NewSstPartitionerFixedPrefixFactory(1);
  2577. NewVersionStorage(6, kCompactionStyleLevel);
  2578. // A compaction should be triggered and pick file 2
  2579. Add(1, 1U, "100", "150", 3000U);
  2580. Add(1, 2U, "151", "200", 3001U);
  2581. Add(1, 3U, "201", "250", 3000U);
  2582. Add(1, 4U, "251", "300", 3000U);
  2583. Add(3, 5U, "120", "130", 7000U);
  2584. Add(3, 6U, "170", "180", 7000U);
  2585. Add(3, 7U, "220", "230", 7000U);
  2586. Add(3, 8U, "270", "280", 7000U);
  2587. UpdateVersionStorageInfo();
  2588. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2589. cf_name_, mutable_cf_options_, mutable_db_options_,
  2590. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2591. vstorage_.get(), &log_buffer_));
  2592. ASSERT_TRUE(compaction.get() != nullptr);
  2593. // No trivial move, because partitioning is applied
  2594. ASSERT_TRUE(!compaction->IsTrivialMove());
  2595. }
  2596. TEST_F(CompactionPickerTest, IsTrivialMoveOff) {
  2597. mutable_cf_options_.max_bytes_for_level_base = 1000000u;
  2598. mutable_cf_options_.max_compaction_bytes = 10000u;
  2599. ioptions_.level_compaction_dynamic_level_bytes = false;
  2600. NewVersionStorage(6, kCompactionStyleLevel);
  2601. // A compaction should be triggered and pick all files from level 1
  2602. Add(1, 1U, "100", "150", 300000U, 0, 0);
  2603. Add(1, 2U, "150", "200", 300000U, 0, 0);
  2604. Add(1, 3U, "200", "250", 300000U, 0, 0);
  2605. Add(1, 4U, "250", "300", 300000U, 0, 0);
  2606. Add(3, 5U, "120", "130", 6000U);
  2607. Add(3, 6U, "140", "150", 6000U);
  2608. UpdateVersionStorageInfo();
  2609. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2610. cf_name_, mutable_cf_options_, mutable_db_options_,
  2611. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2612. vstorage_.get(), &log_buffer_));
  2613. ASSERT_TRUE(compaction.get() != nullptr);
  2614. ASSERT_FALSE(compaction->IsTrivialMove());
  2615. }
  2616. TEST_F(CompactionPickerTest, TrivialMoveMultipleFiles1) {
  2617. mutable_cf_options_.max_bytes_for_level_base = 1000u;
  2618. mutable_cf_options_.max_compaction_bytes = 10000001u;
  2619. ioptions_.level_compaction_dynamic_level_bytes = false;
  2620. ioptions_.compaction_pri = kMinOverlappingRatio;
  2621. NewVersionStorage(6, kCompactionStyleLevel);
  2622. Add(2, 1U, "100", "150", 3000U);
  2623. Add(2, 2U, "151", "200", 3001U);
  2624. Add(2, 3U, "301", "350", 3000U);
  2625. Add(2, 4U, "451", "400", 3000U);
  2626. Add(2, 5U, "551", "500", 3000U);
  2627. Add(2, 6U, "651", "600", 3000U);
  2628. Add(2, 7U, "751", "700", 3000U);
  2629. Add(2, 8U, "851", "900", 3000U);
  2630. Add(3, 15U, "120", "130", 700U);
  2631. Add(3, 16U, "170", "180", 700U);
  2632. Add(3, 17U, "220", "230", 700U);
  2633. Add(3, 18U, "870", "880", 700U);
  2634. UpdateVersionStorageInfo();
  2635. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2636. cf_name_, mutable_cf_options_, mutable_db_options_,
  2637. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2638. vstorage_.get(), &log_buffer_));
  2639. ASSERT_TRUE(compaction.get() != nullptr);
  2640. ASSERT_TRUE(compaction->IsTrivialMove());
  2641. ASSERT_EQ(1, compaction->num_input_levels());
  2642. ASSERT_EQ(4, compaction->num_input_files(0));
  2643. ASSERT_EQ(3, compaction->input(0, 0)->fd.GetNumber());
  2644. ASSERT_EQ(4, compaction->input(0, 1)->fd.GetNumber());
  2645. ASSERT_EQ(5, compaction->input(0, 2)->fd.GetNumber());
  2646. ASSERT_EQ(6, compaction->input(0, 3)->fd.GetNumber());
  2647. }
  2648. TEST_F(CompactionPickerTest, TrivialMoveMultipleFiles2) {
  2649. mutable_cf_options_.max_bytes_for_level_base = 1000u;
  2650. mutable_cf_options_.max_compaction_bytes = 10000001u;
  2651. ioptions_.level_compaction_dynamic_level_bytes = false;
  2652. ioptions_.compaction_pri = kMinOverlappingRatio;
  2653. NewVersionStorage(6, kCompactionStyleLevel);
  2654. Add(2, 1U, "100", "150", 3000U);
  2655. Add(2, 2U, "151", "160", 3001U);
  2656. Add(2, 3U, "161", "179", 3000U);
  2657. Add(2, 4U, "220", "400", 3000U);
  2658. Add(2, 5U, "551", "500", 3000U);
  2659. Add(2, 6U, "651", "600", 3000U);
  2660. Add(2, 7U, "751", "700", 3000U);
  2661. Add(2, 8U, "851", "900", 3000U);
  2662. Add(3, 15U, "120", "130", 700U);
  2663. Add(3, 17U, "220", "230", 700U);
  2664. Add(3, 18U, "870", "880", 700U);
  2665. UpdateVersionStorageInfo();
  2666. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2667. cf_name_, mutable_cf_options_, mutable_db_options_,
  2668. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2669. vstorage_.get(), &log_buffer_));
  2670. ASSERT_TRUE(compaction.get() != nullptr);
  2671. ASSERT_TRUE(compaction->IsTrivialMove());
  2672. ASSERT_EQ(1, compaction->num_input_levels());
  2673. ASSERT_EQ(2, compaction->num_input_files(0));
  2674. ASSERT_EQ(2, compaction->input(0, 0)->fd.GetNumber());
  2675. ASSERT_EQ(3, compaction->input(0, 1)->fd.GetNumber());
  2676. }
  2677. TEST_F(CompactionPickerTest, TrivialMoveMultipleFiles3) {
  2678. mutable_cf_options_.max_bytes_for_level_base = 1000u;
  2679. mutable_cf_options_.max_compaction_bytes = 10000001u;
  2680. ioptions_.level_compaction_dynamic_level_bytes = false;
  2681. ioptions_.compaction_pri = kMinOverlappingRatio;
  2682. NewVersionStorage(6, kCompactionStyleLevel);
  2683. // Even if consecutive files can be trivial moved, we don't pick them
  2684. // since in case trivial move can't be issued for a reason, we cannot
  2685. // fall back to normal compactions.
  2686. Add(2, 1U, "100", "150", 3000U);
  2687. Add(2, 2U, "151", "160", 3001U);
  2688. Add(2, 5U, "551", "500", 3000U);
  2689. Add(2, 6U, "651", "600", 3000U);
  2690. Add(2, 7U, "751", "700", 3000U);
  2691. Add(2, 8U, "851", "900", 3000U);
  2692. Add(3, 15U, "120", "130", 700U);
  2693. Add(3, 17U, "220", "230", 700U);
  2694. Add(3, 18U, "870", "880", 700U);
  2695. UpdateVersionStorageInfo();
  2696. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2697. cf_name_, mutable_cf_options_, mutable_db_options_,
  2698. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2699. vstorage_.get(), &log_buffer_));
  2700. ASSERT_TRUE(compaction.get() != nullptr);
  2701. ASSERT_TRUE(compaction->IsTrivialMove());
  2702. ASSERT_EQ(1, compaction->num_input_levels());
  2703. ASSERT_EQ(1, compaction->num_input_files(0));
  2704. ASSERT_EQ(2, compaction->input(0, 0)->fd.GetNumber());
  2705. }
  2706. TEST_F(CompactionPickerTest, TrivialMoveMultipleFiles4) {
  2707. mutable_cf_options_.max_bytes_for_level_base = 1000u;
  2708. mutable_cf_options_.max_compaction_bytes = 10000001u;
  2709. ioptions_.level_compaction_dynamic_level_bytes = false;
  2710. ioptions_.compaction_pri = kMinOverlappingRatio;
  2711. NewVersionStorage(6, kCompactionStyleLevel);
  2712. Add(2, 1U, "100", "150", 4000U);
  2713. Add(2, 2U, "151", "160", 4001U);
  2714. Add(2, 3U, "161", "179", 4000U);
  2715. Add(3, 15U, "120", "130", 700U);
  2716. Add(3, 17U, "220", "230", 700U);
  2717. Add(3, 18U, "870", "880", 700U);
  2718. UpdateVersionStorageInfo();
  2719. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2720. cf_name_, mutable_cf_options_, mutable_db_options_,
  2721. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2722. vstorage_.get(), &log_buffer_));
  2723. ASSERT_TRUE(compaction.get() != nullptr);
  2724. ASSERT_TRUE(compaction->IsTrivialMove());
  2725. ASSERT_EQ(1, compaction->num_input_levels());
  2726. ASSERT_EQ(2, compaction->num_input_files(0));
  2727. ASSERT_EQ(2, compaction->input(0, 0)->fd.GetNumber());
  2728. ASSERT_EQ(3, compaction->input(0, 1)->fd.GetNumber());
  2729. }
  2730. TEST_F(CompactionPickerTest, TrivialMoveMultipleFiles5) {
  2731. mutable_cf_options_.max_bytes_for_level_base = 1000u;
  2732. mutable_cf_options_.max_compaction_bytes = 10000001u;
  2733. ioptions_.level_compaction_dynamic_level_bytes = false;
  2734. ioptions_.compaction_pri = kMinOverlappingRatio;
  2735. NewVersionStorage(6, kCompactionStyleLevel);
  2736. // File 4 and 5 aren't clean cut, so only 2 and 3 are picked.
  2737. Add(2, 1U, "100", "150", 4000U);
  2738. Add(2, 2U, "151", "160", 4001U);
  2739. Add(2, 3U, "161", "179", 4000U);
  2740. Add(2, 4U, "180", "185", 4000U);
  2741. Add(2, 5U, "185", "190", 4000U);
  2742. Add(3, 15U, "120", "130", 700U);
  2743. Add(3, 17U, "220", "230", 700U);
  2744. Add(3, 18U, "870", "880", 700U);
  2745. UpdateVersionStorageInfo();
  2746. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2747. cf_name_, mutable_cf_options_, mutable_db_options_,
  2748. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2749. vstorage_.get(), &log_buffer_));
  2750. ASSERT_TRUE(compaction.get() != nullptr);
  2751. ASSERT_TRUE(compaction->IsTrivialMove());
  2752. ASSERT_EQ(1, compaction->num_input_levels());
  2753. ASSERT_EQ(2, compaction->num_input_files(0));
  2754. ASSERT_EQ(2, compaction->input(0, 0)->fd.GetNumber());
  2755. ASSERT_EQ(3, compaction->input(0, 1)->fd.GetNumber());
  2756. }
  2757. TEST_F(CompactionPickerTest, TrivialMoveMultipleFiles6) {
  2758. mutable_cf_options_.max_bytes_for_level_base = 1000u;
  2759. mutable_cf_options_.max_compaction_bytes = 10000001u;
  2760. ioptions_.level_compaction_dynamic_level_bytes = false;
  2761. ioptions_.compaction_pri = kMinOverlappingRatio;
  2762. NewVersionStorage(6, kCompactionStyleLevel);
  2763. Add(2, 1U, "100", "150", 3000U);
  2764. Add(2, 2U, "151", "200", 3001U);
  2765. Add(2, 3U, "301", "350", 3000U);
  2766. Add(2, 4U, "451", "400", 3000U);
  2767. Add(2, 5U, "551", "500", 3000U);
  2768. file_map_[5U].first->being_compacted = true;
  2769. Add(2, 6U, "651", "600", 3000U);
  2770. Add(2, 7U, "751", "700", 3000U);
  2771. Add(2, 8U, "851", "900", 3000U);
  2772. Add(3, 15U, "120", "130", 700U);
  2773. Add(3, 16U, "170", "180", 700U);
  2774. Add(3, 17U, "220", "230", 700U);
  2775. Add(3, 18U, "870", "880", 700U);
  2776. UpdateVersionStorageInfo();
  2777. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2778. cf_name_, mutable_cf_options_, mutable_db_options_,
  2779. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2780. vstorage_.get(), &log_buffer_));
  2781. ASSERT_TRUE(compaction.get() != nullptr);
  2782. ASSERT_TRUE(compaction->IsTrivialMove());
  2783. ASSERT_EQ(1, compaction->num_input_levels());
  2784. // Since the next file is being compacted. Stopping at 3 and 4.
  2785. ASSERT_EQ(2, compaction->num_input_files(0));
  2786. ASSERT_EQ(3, compaction->input(0, 0)->fd.GetNumber());
  2787. ASSERT_EQ(4, compaction->input(0, 1)->fd.GetNumber());
  2788. }
  2789. TEST_F(CompactionPickerTest, CacheNextCompactionIndex) {
  2790. NewVersionStorage(6, kCompactionStyleLevel);
  2791. mutable_cf_options_.max_compaction_bytes = 100000000000u;
  2792. Add(1 /* level */, 1U /* file_number */, "100" /* smallest */,
  2793. "149" /* largest */, 1000000000U /* file_size */);
  2794. file_map_[1U].first->being_compacted = true;
  2795. Add(1 /* level */, 2U /* file_number */, "150" /* smallest */,
  2796. "199" /* largest */, 900000000U /* file_size */);
  2797. Add(1 /* level */, 3U /* file_number */, "200" /* smallest */,
  2798. "249" /* largest */, 800000000U /* file_size */);
  2799. Add(1 /* level */, 4U /* file_number */, "250" /* smallest */,
  2800. "299" /* largest */, 700000000U /* file_size */);
  2801. Add(2 /* level */, 5U /* file_number */, "150" /* smallest */,
  2802. "199" /* largest */, 100U /* file_size */);
  2803. Add(2 /* level */, 6U /* file_number */, "200" /* smallest */,
  2804. "240" /* largest */, 1U /* file_size */);
  2805. Add(2 /* level */, 7U /* file_number */, "260" /* smallest */,
  2806. "270" /* largest */, 1U /* file_size */);
  2807. file_map_[5U].first->being_compacted = true;
  2808. UpdateVersionStorageInfo();
  2809. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2810. cf_name_, mutable_cf_options_, mutable_db_options_,
  2811. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2812. vstorage_.get(), &log_buffer_));
  2813. ASSERT_TRUE(compaction.get() != nullptr);
  2814. ASSERT_EQ(2U, compaction->num_input_levels());
  2815. ASSERT_EQ(1U, compaction->num_input_files(0));
  2816. ASSERT_EQ(1U, compaction->num_input_files(1));
  2817. ASSERT_EQ(3U, compaction->input(0, 0)->fd.GetNumber());
  2818. ASSERT_EQ(2, vstorage_->NextCompactionIndex(1 /* level */));
  2819. compaction.reset(level_compaction_picker.PickCompaction(
  2820. cf_name_, mutable_cf_options_, mutable_db_options_,
  2821. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2822. vstorage_.get(), &log_buffer_));
  2823. ASSERT_TRUE(compaction.get() != nullptr);
  2824. ASSERT_EQ(2U, compaction->num_input_levels());
  2825. ASSERT_EQ(1U, compaction->num_input_files(0));
  2826. ASSERT_EQ(1U, compaction->num_input_files(1));
  2827. ASSERT_EQ(4U, compaction->input(0, 0)->fd.GetNumber());
  2828. ASSERT_EQ(3, vstorage_->NextCompactionIndex(1 /* level */));
  2829. compaction.reset(level_compaction_picker.PickCompaction(
  2830. cf_name_, mutable_cf_options_, mutable_db_options_,
  2831. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2832. vstorage_.get(), &log_buffer_));
  2833. ASSERT_TRUE(compaction.get() == nullptr);
  2834. ASSERT_EQ(4, vstorage_->NextCompactionIndex(1 /* level */));
  2835. }
  2836. TEST_F(CompactionPickerTest, IntraL0MaxCompactionBytesNotHit) {
  2837. // Intra L0 compaction triggers only if there are at least
  2838. // level0_file_num_compaction_trigger + 2 L0 files.
  2839. mutable_cf_options_.level0_file_num_compaction_trigger = 3;
  2840. mutable_cf_options_.max_compaction_bytes = 1000000u;
  2841. NewVersionStorage(6, kCompactionStyleLevel);
  2842. // All 5 L0 files will be picked for intra L0 compaction. The one L1 file
  2843. // spans entire L0 key range and is marked as being compacted to avoid
  2844. // L0->L1 compaction.
  2845. Add(0, 1U, "100", "150", 200000U, 0, 100, 101);
  2846. Add(0, 2U, "151", "200", 200000U, 0, 102, 103);
  2847. Add(0, 3U, "201", "250", 200000U, 0, 104, 105);
  2848. Add(0, 4U, "251", "300", 200000U, 0, 106, 107);
  2849. Add(0, 5U, "301", "350", 200000U, 0, 108, 109);
  2850. Add(1, 6U, "100", "350", 200000U, 0, 110, 111);
  2851. vstorage_->LevelFiles(1)[0]->being_compacted = true;
  2852. UpdateVersionStorageInfo();
  2853. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2854. cf_name_, mutable_cf_options_, mutable_db_options_,
  2855. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2856. vstorage_.get(), &log_buffer_));
  2857. ASSERT_TRUE(compaction.get() != nullptr);
  2858. ASSERT_EQ(1U, compaction->num_input_levels());
  2859. ASSERT_EQ(5U, compaction->num_input_files(0));
  2860. ASSERT_EQ(CompactionReason::kLevelL0FilesNum,
  2861. compaction->compaction_reason());
  2862. ASSERT_EQ(0, compaction->output_level());
  2863. }
  2864. TEST_F(CompactionPickerTest, IntraL0MaxCompactionBytesHit) {
  2865. // Intra L0 compaction triggers only if there are at least
  2866. // level0_file_num_compaction_trigger + 2 L0 files.
  2867. mutable_cf_options_.level0_file_num_compaction_trigger = 3;
  2868. mutable_cf_options_.max_compaction_bytes = 999999u;
  2869. NewVersionStorage(6, kCompactionStyleLevel);
  2870. // 4 out of 5 L0 files will be picked for intra L0 compaction due to
  2871. // max_compaction_bytes limit (the minimum number of files for triggering
  2872. // intra L0 compaction is 4). The one L1 file spans entire L0 key range and
  2873. // is marked as being compacted to avoid L0->L1 compaction.
  2874. Add(0, 1U, "100", "150", 200000U, 0, 100, 101);
  2875. Add(0, 2U, "151", "200", 200000U, 0, 102, 103);
  2876. Add(0, 3U, "201", "250", 200000U, 0, 104, 105);
  2877. Add(0, 4U, "251", "300", 200000U, 0, 106, 107);
  2878. Add(0, 5U, "301", "350", 200000U, 0, 108, 109);
  2879. Add(1, 6U, "100", "350", 200000U, 0, 109, 110);
  2880. vstorage_->LevelFiles(1)[0]->being_compacted = true;
  2881. UpdateVersionStorageInfo();
  2882. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  2883. cf_name_, mutable_cf_options_, mutable_db_options_,
  2884. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2885. vstorage_.get(), &log_buffer_));
  2886. ASSERT_TRUE(compaction.get() != nullptr);
  2887. ASSERT_EQ(1U, compaction->num_input_levels());
  2888. ASSERT_EQ(4U, compaction->num_input_files(0));
  2889. ASSERT_EQ(CompactionReason::kLevelL0FilesNum,
  2890. compaction->compaction_reason());
  2891. ASSERT_EQ(0, compaction->output_level());
  2892. }
  2893. TEST_F(CompactionPickerTest, UniversalMarkedCompactionFullOverlap) {
  2894. const uint64_t kFileSize = 100000;
  2895. ioptions_.compaction_style = kCompactionStyleUniversal;
  2896. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  2897. // This test covers the case where a "regular" universal compaction is
  2898. // scheduled first, followed by a delete triggered compaction. The latter
  2899. // should fail
  2900. NewVersionStorage(5, kCompactionStyleUniversal);
  2901. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550, /*compensated_file_size*/ 0,
  2902. /*marked_for_compact*/ false, /* temperature*/ Temperature::kUnknown,
  2903. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  2904. /*newest_key_time*/ kUnknownNewestKeyTime,
  2905. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  2906. /*epoch_number*/ 3);
  2907. Add(0, 2U, "201", "250", 2 * kFileSize, 0, 401, 450,
  2908. /*compensated_file_size*/ 0, /*marked_for_compact*/ false,
  2909. /* temperature*/ Temperature::kUnknown,
  2910. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  2911. /*newest_key_time*/ kUnknownNewestKeyTime,
  2912. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  2913. /*epoch_number*/ 2);
  2914. Add(0, 4U, "260", "300", 4 * kFileSize, 0, 260, 300,
  2915. /*compensated_file_size*/ 0, /*marked_for_compact*/ false,
  2916. /* temperature*/ Temperature::kUnknown,
  2917. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  2918. /*newest_key_time*/ kUnknownNewestKeyTime,
  2919. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  2920. /*epoch_number*/ 1);
  2921. Add(3, 5U, "010", "080", 8 * kFileSize, 0, 200, 251);
  2922. Add(4, 3U, "301", "350", 8 * kFileSize, 0, 101, 150);
  2923. Add(4, 6U, "501", "750", 8 * kFileSize, 0, 101, 150);
  2924. UpdateVersionStorageInfo();
  2925. std::unique_ptr<Compaction> compaction(
  2926. universal_compaction_picker.PickCompaction(
  2927. cf_name_, mutable_cf_options_, mutable_db_options_,
  2928. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2929. vstorage_.get(), &log_buffer_));
  2930. ASSERT_TRUE(compaction);
  2931. // Validate that its a compaction to reduce sorted runs
  2932. ASSERT_EQ(CompactionReason::kUniversalSortedRunNum,
  2933. compaction->compaction_reason());
  2934. ASSERT_EQ(0, compaction->output_level());
  2935. ASSERT_EQ(0, compaction->start_level());
  2936. ASSERT_EQ(2U, compaction->num_input_files(0));
  2937. AddVersionStorage();
  2938. // Simulate a flush and mark the file for compaction
  2939. Add(0, 7U, "150", "200", kFileSize, 0, 551, 600, 0, true,
  2940. /* temperature*/ Temperature::kUnknown,
  2941. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  2942. /*newest_key_time*/ kUnknownNewestKeyTime,
  2943. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  2944. /*epoch_number*/ 4);
  2945. UpdateVersionStorageInfo();
  2946. std::unique_ptr<Compaction> compaction2(
  2947. universal_compaction_picker.PickCompaction(
  2948. cf_name_, mutable_cf_options_, mutable_db_options_,
  2949. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2950. vstorage_.get(), &log_buffer_));
  2951. ASSERT_FALSE(compaction2);
  2952. }
  2953. TEST_F(CompactionPickerTest, UniversalMarkedCompactionFullOverlap2) {
  2954. const uint64_t kFileSize = 100000;
  2955. ioptions_.compaction_style = kCompactionStyleUniversal;
  2956. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  2957. // This test covers the case where a delete triggered compaction is
  2958. // scheduled first, followed by a "regular" compaction. The latter
  2959. // should fail
  2960. NewVersionStorage(5, kCompactionStyleUniversal);
  2961. // Mark file number 4 for compaction
  2962. Add(0, 4U, "260", "300", 4 * kFileSize, 0, 260, 300, 0, true,
  2963. /* temperature*/ Temperature::kUnknown,
  2964. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  2965. /*newest_key_time*/ kUnknownNewestKeyTime,
  2966. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  2967. /*epoch_number*/ 1);
  2968. Add(3, 5U, "240", "290", 8 * kFileSize, 0, 201, 250);
  2969. Add(4, 3U, "301", "350", 8 * kFileSize, 0, 101, 150);
  2970. Add(4, 6U, "501", "750", 8 * kFileSize, 0, 101, 150);
  2971. UpdateVersionStorageInfo();
  2972. std::unique_ptr<Compaction> compaction(
  2973. universal_compaction_picker.PickCompaction(
  2974. cf_name_, mutable_cf_options_, mutable_db_options_,
  2975. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  2976. vstorage_.get(), &log_buffer_));
  2977. ASSERT_TRUE(compaction);
  2978. // Validate that its a delete triggered compaction
  2979. ASSERT_EQ(CompactionReason::kFilesMarkedForCompaction,
  2980. compaction->compaction_reason());
  2981. ASSERT_EQ(3, compaction->output_level());
  2982. ASSERT_EQ(0, compaction->start_level());
  2983. ASSERT_EQ(1U, compaction->num_input_files(0));
  2984. ASSERT_EQ(1U, compaction->num_input_files(1));
  2985. AddVersionStorage();
  2986. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550, /*compensated_file_size*/ 0,
  2987. /*marked_for_compact*/ false, /* temperature*/ Temperature::kUnknown,
  2988. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  2989. /*newest_key_time*/ kUnknownNewestKeyTime,
  2990. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  2991. /*epoch_number*/ 3);
  2992. Add(0, 2U, "201", "250", 2 * kFileSize, 0, 401, 450,
  2993. /*compensated_file_size*/ 0, /*marked_for_compact*/ false,
  2994. /* temperature*/ Temperature::kUnknown,
  2995. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  2996. /*newest_key_time*/ kUnknownNewestKeyTime,
  2997. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  2998. /*epoch_number*/ 2);
  2999. UpdateVersionStorageInfo();
  3000. std::unique_ptr<Compaction> compaction2(
  3001. universal_compaction_picker.PickCompaction(
  3002. cf_name_, mutable_cf_options_, mutable_db_options_,
  3003. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3004. vstorage_.get(), &log_buffer_));
  3005. ASSERT_FALSE(compaction2);
  3006. }
  3007. TEST_F(CompactionPickerTest, UniversalMarkedCompactionStartOutputOverlap) {
  3008. // The case where universal periodic compaction can be picked
  3009. // with some newer files being compacted.
  3010. const uint64_t kFileSize = 100000;
  3011. ioptions_.compaction_style = kCompactionStyleUniversal;
  3012. bool input_level_overlap = false;
  3013. bool output_level_overlap = false;
  3014. // Let's mark 2 files in 2 different levels for compaction. The
  3015. // compaction picker will randomly pick one, so use the sync point to
  3016. // ensure a deterministic order. Loop until both cases are covered
  3017. size_t random_index = 0;
  3018. SyncPoint::GetInstance()->SetCallBack(
  3019. "CompactionPicker::PickFilesMarkedForCompaction", [&](void* arg) {
  3020. size_t* index = static_cast<size_t*>(arg);
  3021. *index = random_index;
  3022. });
  3023. SyncPoint::GetInstance()->EnableProcessing();
  3024. while (!input_level_overlap || !output_level_overlap) {
  3025. // Ensure that the L0 file gets picked first
  3026. random_index = !input_level_overlap ? 0 : 1;
  3027. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3028. NewVersionStorage(5, kCompactionStyleUniversal);
  3029. Add(0, 1U, "260", "300", 4 * kFileSize, 0, 260, 300, 0, true);
  3030. Add(3, 2U, "010", "020", 2 * kFileSize, 0, 201, 248);
  3031. Add(3, 3U, "250", "270", 2 * kFileSize, 0, 202, 249);
  3032. Add(3, 4U, "290", "310", 2 * kFileSize, 0, 203, 250);
  3033. Add(3, 5U, "310", "320", 2 * kFileSize, 0, 204, 251, 0, true);
  3034. Add(4, 6U, "301", "350", 8 * kFileSize, 0, 101, 150);
  3035. Add(4, 7U, "501", "750", 8 * kFileSize, 0, 101, 150);
  3036. UpdateVersionStorageInfo();
  3037. std::unique_ptr<Compaction> compaction(
  3038. universal_compaction_picker.PickCompaction(
  3039. cf_name_, mutable_cf_options_, mutable_db_options_,
  3040. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3041. vstorage_.get(), &log_buffer_));
  3042. ASSERT_TRUE(compaction);
  3043. // Validate that its a delete triggered compaction
  3044. ASSERT_EQ(CompactionReason::kFilesMarkedForCompaction,
  3045. compaction->compaction_reason());
  3046. ASSERT_TRUE(compaction->start_level() == 0 ||
  3047. compaction->start_level() == 3);
  3048. if (compaction->start_level() == 0) {
  3049. // The L0 file was picked. The next compaction will detect an
  3050. // overlap on its input level
  3051. input_level_overlap = true;
  3052. ASSERT_EQ(3, compaction->output_level());
  3053. ASSERT_EQ(1U, compaction->num_input_files(0));
  3054. ASSERT_EQ(3U, compaction->num_input_files(1));
  3055. } else {
  3056. // The level 3 file was picked. The next compaction will pick
  3057. // the L0 file and will detect overlap when adding output
  3058. // level inputs
  3059. output_level_overlap = true;
  3060. ASSERT_EQ(4, compaction->output_level());
  3061. ASSERT_EQ(2U, compaction->num_input_files(0));
  3062. ASSERT_EQ(1U, compaction->num_input_files(1));
  3063. }
  3064. vstorage_->ComputeCompactionScore(ioptions_, mutable_cf_options_);
  3065. // After recomputing the compaction score, only one marked file will remain
  3066. random_index = 0;
  3067. std::unique_ptr<Compaction> compaction2(
  3068. universal_compaction_picker.PickCompaction(
  3069. cf_name_, mutable_cf_options_, mutable_db_options_,
  3070. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3071. vstorage_.get(), &log_buffer_));
  3072. ASSERT_FALSE(compaction2);
  3073. DeleteVersionStorage();
  3074. }
  3075. }
  3076. TEST_F(CompactionPickerTest, UniversalMarkedL0NoOverlap) {
  3077. const uint64_t kFileSize = 100000;
  3078. ioptions_.compaction_style = kCompactionStyleUniversal;
  3079. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3080. // This test covers the case where a delete triggered compaction is
  3081. // scheduled and should result in a full compaction
  3082. NewVersionStorage(1, kCompactionStyleUniversal);
  3083. // Mark file number 4 for compaction
  3084. Add(0, 4U, "260", "300", 1 * kFileSize, 0, 260, 300, 0, true);
  3085. Add(0, 5U, "240", "290", 2 * kFileSize, 0, 201, 250);
  3086. Add(0, 3U, "301", "350", 4 * kFileSize, 0, 101, 150);
  3087. Add(0, 6U, "501", "750", 8 * kFileSize, 0, 50, 100);
  3088. UpdateVersionStorageInfo();
  3089. std::unique_ptr<Compaction> compaction(
  3090. universal_compaction_picker.PickCompaction(
  3091. cf_name_, mutable_cf_options_, mutable_db_options_,
  3092. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3093. vstorage_.get(), &log_buffer_));
  3094. ASSERT_TRUE(compaction);
  3095. // Validate that its a delete triggered compaction
  3096. ASSERT_EQ(CompactionReason::kFilesMarkedForCompaction,
  3097. compaction->compaction_reason());
  3098. ASSERT_EQ(0, compaction->output_level());
  3099. ASSERT_EQ(0, compaction->start_level());
  3100. ASSERT_EQ(4U, compaction->num_input_files(0));
  3101. ASSERT_TRUE(file_map_[4].first->being_compacted);
  3102. ASSERT_TRUE(file_map_[5].first->being_compacted);
  3103. ASSERT_TRUE(file_map_[3].first->being_compacted);
  3104. ASSERT_TRUE(file_map_[6].first->being_compacted);
  3105. }
  3106. TEST_F(CompactionPickerTest, UniversalMarkedL0WithOverlap) {
  3107. const uint64_t kFileSize = 100000;
  3108. ioptions_.compaction_style = kCompactionStyleUniversal;
  3109. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3110. // This test covers the case where a file is being compacted, and a
  3111. // delete triggered compaction is then scheduled. The latter should stop
  3112. // at the first file being compacted
  3113. NewVersionStorage(1, kCompactionStyleUniversal);
  3114. // Mark file number 4 for compaction
  3115. Add(0, 4U, "260", "300", 1 * kFileSize, 0, 260, 300, 0, true);
  3116. Add(0, 5U, "240", "290", 2 * kFileSize, 0, 201, 250);
  3117. Add(0, 3U, "301", "350", 4 * kFileSize, 0, 101, 150);
  3118. Add(0, 6U, "501", "750", 8 * kFileSize, 0, 50, 100);
  3119. UpdateVersionStorageInfo();
  3120. file_map_[3].first->being_compacted = true;
  3121. std::unique_ptr<Compaction> compaction(
  3122. universal_compaction_picker.PickCompaction(
  3123. cf_name_, mutable_cf_options_, mutable_db_options_,
  3124. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3125. vstorage_.get(), &log_buffer_));
  3126. ASSERT_TRUE(compaction);
  3127. // Validate that its a delete triggered compaction
  3128. ASSERT_EQ(CompactionReason::kFilesMarkedForCompaction,
  3129. compaction->compaction_reason());
  3130. ASSERT_EQ(0, compaction->output_level());
  3131. ASSERT_EQ(0, compaction->start_level());
  3132. ASSERT_EQ(2U, compaction->num_input_files(0));
  3133. ASSERT_TRUE(file_map_[4].first->being_compacted);
  3134. ASSERT_TRUE(file_map_[5].first->being_compacted);
  3135. }
  3136. TEST_F(CompactionPickerTest, UniversalMarkedL0Overlap2) {
  3137. const uint64_t kFileSize = 100000;
  3138. ioptions_.compaction_style = kCompactionStyleUniversal;
  3139. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3140. // This test covers the case where a delete triggered compaction is
  3141. // scheduled first, followed by a "regular" compaction. The latter
  3142. // should fail
  3143. NewVersionStorage(1, kCompactionStyleUniversal);
  3144. // Mark file number 5 for compaction
  3145. Add(0, 4U, "260", "300", 1 * kFileSize, 0, 260, 300,
  3146. /*compensated_file_size*/ 0, /*marked_for_compact*/ false,
  3147. /* temperature*/ Temperature::kUnknown,
  3148. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  3149. /*newest_key_time*/ kUnknownNewestKeyTime,
  3150. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  3151. /*epoch_number*/ 4);
  3152. Add(0, 5U, "240", "290", 2 * kFileSize, 0, 201, 250, 0, true,
  3153. /* temperature*/ Temperature::kUnknown,
  3154. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  3155. /*newest_key_time*/ kUnknownNewestKeyTime,
  3156. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  3157. /*epoch_number*/ 3);
  3158. Add(0, 3U, "301", "350", 4 * kFileSize, 0, 101, 150,
  3159. /*compensated_file_size*/ 0, /*marked_for_compact*/ false,
  3160. /* temperature*/ Temperature::kUnknown,
  3161. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  3162. /*newest_key_time*/ kUnknownNewestKeyTime,
  3163. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  3164. /*epoch_number*/ 2);
  3165. Add(0, 6U, "501", "750", 8 * kFileSize, 0, 50, 100,
  3166. /*compensated_file_size*/ 0, /*marked_for_compact*/ false,
  3167. /* temperature*/ Temperature::kUnknown,
  3168. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  3169. /*newest_key_time*/ kUnknownNewestKeyTime,
  3170. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  3171. /*epoch_number*/ 1);
  3172. UpdateVersionStorageInfo();
  3173. std::unique_ptr<Compaction> compaction(
  3174. universal_compaction_picker.PickCompaction(
  3175. cf_name_, mutable_cf_options_, mutable_db_options_,
  3176. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3177. vstorage_.get(), &log_buffer_));
  3178. ASSERT_TRUE(compaction);
  3179. // Validate that its a delete triggered compaction
  3180. ASSERT_EQ(CompactionReason::kFilesMarkedForCompaction,
  3181. compaction->compaction_reason());
  3182. ASSERT_EQ(0, compaction->output_level());
  3183. ASSERT_EQ(0, compaction->start_level());
  3184. ASSERT_EQ(3U, compaction->num_input_files(0));
  3185. ASSERT_TRUE(file_map_[5].first->being_compacted);
  3186. ASSERT_TRUE(file_map_[3].first->being_compacted);
  3187. ASSERT_TRUE(file_map_[6].first->being_compacted);
  3188. AddVersionStorage();
  3189. Add(0, 1U, "150", "200", kFileSize, 0, 500, 550, /*compensated_file_size*/ 0,
  3190. /*marked_for_compact*/ false,
  3191. /* temperature*/ Temperature::kUnknown,
  3192. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  3193. /*newest_key_time*/ kUnknownNewestKeyTime,
  3194. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  3195. /*epoch_number*/ 6);
  3196. Add(0, 2U, "201", "250", kFileSize, 0, 401, 450, /*compensated_file_size*/ 0,
  3197. /*marked_for_compact*/ false,
  3198. /* temperature*/ Temperature::kUnknown,
  3199. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  3200. /*newest_key_time*/ kUnknownNewestKeyTime,
  3201. /*ts_of_smallest*/ Slice(), /*ts_of_largest*/ Slice(),
  3202. /*epoch_number*/ 5);
  3203. UpdateVersionStorageInfo();
  3204. std::unique_ptr<Compaction> compaction2(
  3205. universal_compaction_picker.PickCompaction(
  3206. cf_name_, mutable_cf_options_, mutable_db_options_,
  3207. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3208. vstorage_.get(), &log_buffer_));
  3209. ASSERT_TRUE(compaction2);
  3210. ASSERT_EQ(3U, compaction->num_input_files(0));
  3211. ASSERT_TRUE(file_map_[1].first->being_compacted);
  3212. ASSERT_TRUE(file_map_[2].first->being_compacted);
  3213. ASSERT_TRUE(file_map_[4].first->being_compacted);
  3214. }
  3215. TEST_F(CompactionPickerTest, UniversalMarkedManualCompaction) {
  3216. const uint64_t kFileSize = 100000;
  3217. const int kNumLevels = 7;
  3218. // This test makes sure the `files_marked_for_compaction_` is updated after
  3219. // creating manual compaction.
  3220. ioptions_.compaction_style = kCompactionStyleUniversal;
  3221. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3222. NewVersionStorage(kNumLevels, kCompactionStyleUniversal);
  3223. // Add 3 files marked for compaction
  3224. Add(0, 3U, "301", "350", 4 * kFileSize, 0, 101, 150, 0, true);
  3225. Add(0, 4U, "260", "300", 1 * kFileSize, 0, 260, 300, 0, true);
  3226. Add(0, 5U, "240", "290", 2 * kFileSize, 0, 201, 250, 0, true);
  3227. UpdateVersionStorageInfo();
  3228. // All 3 files are marked for compaction
  3229. ASSERT_EQ(3U, vstorage_->FilesMarkedForCompaction().size());
  3230. bool manual_conflict = false;
  3231. InternalKey* manual_end = nullptr;
  3232. std::unique_ptr<Compaction> compaction(
  3233. universal_compaction_picker.PickCompactionForCompactRange(
  3234. cf_name_, mutable_cf_options_, mutable_db_options_, vstorage_.get(),
  3235. ColumnFamilyData::kCompactAllLevels, 6, CompactRangeOptions(),
  3236. nullptr, nullptr, &manual_end, &manual_conflict,
  3237. std::numeric_limits<uint64_t>::max(), ""));
  3238. ASSERT_TRUE(compaction);
  3239. ASSERT_EQ(CompactionReason::kManualCompaction,
  3240. compaction->compaction_reason());
  3241. ASSERT_EQ(kNumLevels - 1, compaction->output_level());
  3242. ASSERT_EQ(0, compaction->start_level());
  3243. ASSERT_EQ(3U, compaction->num_input_files(0));
  3244. ASSERT_TRUE(file_map_[3].first->being_compacted);
  3245. ASSERT_TRUE(file_map_[4].first->being_compacted);
  3246. ASSERT_TRUE(file_map_[5].first->being_compacted);
  3247. // After creating the manual compaction, all files should be cleared from
  3248. // `FilesMarkedForCompaction`. So they won't be picked by others.
  3249. ASSERT_EQ(0U, vstorage_->FilesMarkedForCompaction().size());
  3250. }
  3251. TEST_F(CompactionPickerTest, UniversalSizeAmpTierCompactionNonLastLevel) {
  3252. // This test make sure size amplification compaction could still be triggered
  3253. // if the last sorted run is not the last level.
  3254. const uint64_t kFileSize = 100000;
  3255. const int kNumLevels = 7;
  3256. const int kLastLevel = kNumLevels - 1;
  3257. ioptions_.compaction_style = kCompactionStyleUniversal;
  3258. mutable_cf_options_.preclude_last_level_data_seconds = 1000;
  3259. mutable_cf_options_.compaction_options_universal
  3260. .max_size_amplification_percent = 200;
  3261. // To avoid any L0 file exclusion in size amp compaction intended for reducing
  3262. // write stop
  3263. mutable_cf_options_.compaction_options_universal.max_merge_width = 2;
  3264. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3265. NewVersionStorage(kNumLevels, kCompactionStyleUniversal);
  3266. Add(0, 100U, "100", "300", 1 * kFileSize);
  3267. Add(0, 101U, "200", "400", 1 * kFileSize);
  3268. Add(4, 90U, "100", "600", 4 * kFileSize);
  3269. Add(5, 80U, "200", "300", 2 * kFileSize);
  3270. UpdateVersionStorageInfo();
  3271. std::unique_ptr<Compaction> compaction(
  3272. universal_compaction_picker.PickCompaction(
  3273. cf_name_, mutable_cf_options_, mutable_db_options_,
  3274. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3275. vstorage_.get(), &log_buffer_));
  3276. // Make sure it's a size amp compaction and includes all files
  3277. ASSERT_EQ(compaction->compaction_reason(),
  3278. CompactionReason::kUniversalSizeAmplification);
  3279. ASSERT_EQ(compaction->output_level(), kLastLevel);
  3280. ASSERT_EQ(compaction->input_levels(0)->num_files, 2);
  3281. ASSERT_EQ(compaction->input_levels(4)->num_files, 1);
  3282. ASSERT_EQ(compaction->input_levels(5)->num_files, 1);
  3283. }
  3284. TEST_F(CompactionPickerTest, UniversalSizeRatioTierCompactionLastLevel) {
  3285. // This test makes sure the size amp calculation skips the last level (L6), so
  3286. // size amp compaction is not triggered, instead a size ratio compaction is
  3287. // triggered.
  3288. const uint64_t kFileSize = 100000;
  3289. const int kNumLevels = 7;
  3290. const int kLastLevel = kNumLevels - 1;
  3291. const int kProximalLevel = kLastLevel - 1;
  3292. ioptions_.compaction_style = kCompactionStyleUniversal;
  3293. mutable_cf_options_.preclude_last_level_data_seconds = 1000;
  3294. mutable_cf_options_.compaction_options_universal
  3295. .max_size_amplification_percent = 200;
  3296. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3297. NewVersionStorage(kNumLevels, kCompactionStyleUniversal);
  3298. Add(0, 100U, "100", "300", 1 * kFileSize);
  3299. Add(0, 101U, "200", "400", 1 * kFileSize);
  3300. Add(5, 90U, "100", "600", 4 * kFileSize);
  3301. Add(6, 80U, "200", "300", 2 * kFileSize);
  3302. UpdateVersionStorageInfo();
  3303. std::unique_ptr<Compaction> compaction(
  3304. universal_compaction_picker.PickCompaction(
  3305. cf_name_, mutable_cf_options_, mutable_db_options_,
  3306. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3307. vstorage_.get(), &log_buffer_));
  3308. // Internally, size amp compaction is evaluated before size ratio compaction.
  3309. // Here to make sure it's size ratio compaction instead of size amp
  3310. ASSERT_EQ(compaction->compaction_reason(),
  3311. CompactionReason::kUniversalSizeRatio);
  3312. ASSERT_EQ(compaction->output_level(), kProximalLevel - 1);
  3313. ASSERT_EQ(compaction->input_levels(0)->num_files, 2);
  3314. ASSERT_EQ(compaction->input_levels(5)->num_files, 0);
  3315. ASSERT_EQ(compaction->input_levels(6)->num_files, 0);
  3316. }
  3317. TEST_F(CompactionPickerTest, UniversalSizeAmpTierCompactionNotSuport) {
  3318. // Tiered compaction only support level_num > 2 (otherwise the proximal
  3319. // level is going to be level 0, which may make thing more complicated), so
  3320. // when there's only 2 level, still treating level 1 as the last level for
  3321. // size amp compaction
  3322. const uint64_t kFileSize = 100000;
  3323. const int kNumLevels = 2;
  3324. const int kLastLevel = kNumLevels - 1;
  3325. ioptions_.compaction_style = kCompactionStyleUniversal;
  3326. mutable_cf_options_.preclude_last_level_data_seconds = 1000;
  3327. mutable_cf_options_.compaction_options_universal
  3328. .max_size_amplification_percent = 200;
  3329. // To avoid any L0 file exclusion in size amp compaction intended for reducing
  3330. // write stop
  3331. mutable_cf_options_.compaction_options_universal.max_merge_width = 2;
  3332. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3333. NewVersionStorage(kNumLevels, kCompactionStyleUniversal);
  3334. Add(0, 100U, "100", "300", 1 * kFileSize);
  3335. Add(0, 101U, "200", "400", 1 * kFileSize);
  3336. Add(0, 90U, "100", "600", 4 * kFileSize);
  3337. Add(1, 80U, "200", "300", 2 * kFileSize);
  3338. UpdateVersionStorageInfo();
  3339. std::unique_ptr<Compaction> compaction(
  3340. universal_compaction_picker.PickCompaction(
  3341. cf_name_, mutable_cf_options_, mutable_db_options_,
  3342. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3343. vstorage_.get(), &log_buffer_));
  3344. // size amp compaction is still triggered even preclude_last_level is set
  3345. ASSERT_EQ(compaction->compaction_reason(),
  3346. CompactionReason::kUniversalSizeAmplification);
  3347. ASSERT_EQ(compaction->output_level(), kLastLevel);
  3348. ASSERT_EQ(compaction->input_levels(0)->num_files, 3);
  3349. ASSERT_EQ(compaction->input_levels(1)->num_files, 1);
  3350. }
  3351. TEST_F(CompactionPickerTest, UniversalSizeAmpTierCompactionLastLevel) {
  3352. // This test makes sure the size amp compaction for tiered storage could still
  3353. // be triggered, but only for non-last-level files
  3354. const uint64_t kFileSize = 100000;
  3355. const int kNumLevels = 7;
  3356. const int kLastLevel = kNumLevels - 1;
  3357. const int kProximalLevel = kLastLevel - 1;
  3358. ioptions_.compaction_style = kCompactionStyleUniversal;
  3359. mutable_cf_options_.preclude_last_level_data_seconds = 1000;
  3360. mutable_cf_options_.compaction_options_universal
  3361. .max_size_amplification_percent = 200;
  3362. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3363. NewVersionStorage(kNumLevels, kCompactionStyleUniversal);
  3364. Add(0, 100U, "100", "300", 3 * kFileSize);
  3365. Add(0, 101U, "200", "400", 2 * kFileSize);
  3366. Add(5, 90U, "100", "600", 2 * kFileSize);
  3367. Add(6, 80U, "200", "300", 2 * kFileSize);
  3368. UpdateVersionStorageInfo();
  3369. std::unique_ptr<Compaction> compaction(
  3370. universal_compaction_picker.PickCompaction(
  3371. cf_name_, mutable_cf_options_, mutable_db_options_,
  3372. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3373. vstorage_.get(), &log_buffer_));
  3374. // It's a Size Amp compaction, but doesn't include the last level file and
  3375. // output to the proximal level.
  3376. ASSERT_EQ(compaction->compaction_reason(),
  3377. CompactionReason::kUniversalSizeAmplification);
  3378. ASSERT_EQ(compaction->output_level(), kProximalLevel);
  3379. ASSERT_EQ(compaction->input_levels(0)->num_files, 2);
  3380. ASSERT_EQ(compaction->input_levels(5)->num_files, 1);
  3381. ASSERT_EQ(compaction->input_levels(6)->num_files, 0);
  3382. }
  3383. TEST_F(CompactionPickerU64TsTest, Overlap) {
  3384. int num_levels = ioptions_.num_levels;
  3385. NewVersionStorage(num_levels, kCompactionStyleLevel);
  3386. constexpr int level = 0;
  3387. constexpr uint64_t file_number = 20ULL;
  3388. constexpr char smallest[4] = "500";
  3389. constexpr char largest[4] = "600";
  3390. constexpr uint64_t ts_of_smallest = 12345ULL;
  3391. constexpr uint64_t ts_of_largest = 56789ULL;
  3392. {
  3393. std::string ts1;
  3394. PutFixed64(&ts1, ts_of_smallest);
  3395. std::string ts2;
  3396. PutFixed64(&ts2, ts_of_largest);
  3397. Add(level, file_number, smallest, largest,
  3398. /*file_size=*/1U, /*path_id=*/0,
  3399. /*smallest_seq=*/100, /*largest_seq=*/100, /*compensated_file_size=*/0,
  3400. /*marked_for_compact=*/false, /*temperature=*/Temperature::kUnknown,
  3401. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  3402. /*newest_key_time*/ kUnknownNewestKeyTime, ts1, ts2);
  3403. UpdateVersionStorageInfo();
  3404. }
  3405. std::unordered_set<uint64_t> input{file_number};
  3406. std::vector<CompactionInputFiles> input_files;
  3407. ASSERT_OK(level_compaction_picker.GetCompactionInputsFromFileNumbers(
  3408. &input_files, &input, vstorage_.get(), CompactionOptions()));
  3409. std::unique_ptr<Compaction> comp1(
  3410. level_compaction_picker.PickCompactionForCompactFiles(
  3411. CompactionOptions(), input_files, level, vstorage_.get(),
  3412. mutable_cf_options_, mutable_db_options_, /*output_path_id=*/0));
  3413. {
  3414. // [600, ts=50000] to [600, ts=50000] is the range to check.
  3415. // ucmp->Compare(smallest_user_key, c->GetLargestUserKey()) > 0, but
  3416. // ucmp->CompareWithoutTimestamp(smallest_user_key,
  3417. // c->GetLargestUserKey()) == 0.
  3418. // Should still be considered overlapping.
  3419. std::string user_key_with_ts1(largest);
  3420. PutFixed64(&user_key_with_ts1, ts_of_largest - 1);
  3421. std::string user_key_with_ts2(largest);
  3422. PutFixed64(&user_key_with_ts2, ts_of_largest - 1);
  3423. ASSERT_TRUE(level_compaction_picker.RangeOverlapWithCompaction(
  3424. user_key_with_ts1, user_key_with_ts2, level));
  3425. }
  3426. {
  3427. // [500, ts=60000] to [500, ts=60000] is the range to check.
  3428. // ucmp->Compare(largest_user_key, c->GetSmallestUserKey()) < 0, but
  3429. // ucmp->CompareWithoutTimestamp(largest_user_key,
  3430. // c->GetSmallestUserKey()) == 0.
  3431. // Should still be considered overlapping.
  3432. std::string user_key_with_ts1(smallest);
  3433. PutFixed64(&user_key_with_ts1, ts_of_smallest + 1);
  3434. std::string user_key_with_ts2(smallest);
  3435. PutFixed64(&user_key_with_ts2, ts_of_smallest + 1);
  3436. ASSERT_TRUE(level_compaction_picker.RangeOverlapWithCompaction(
  3437. user_key_with_ts1, user_key_with_ts2, level));
  3438. }
  3439. }
  3440. TEST_F(CompactionPickerU64TsTest, CannotTrivialMoveUniversal) {
  3441. constexpr uint64_t kFileSize = 100000;
  3442. mutable_cf_options_.level0_file_num_compaction_trigger = 2;
  3443. mutable_cf_options_.compaction_options_universal.allow_trivial_move = true;
  3444. NewVersionStorage(1, kCompactionStyleUniversal);
  3445. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3446. UpdateVersionStorageInfo();
  3447. // must return false when there's no files.
  3448. ASSERT_FALSE(universal_compaction_picker.NeedsCompaction(vstorage_.get()));
  3449. std::string ts1;
  3450. PutFixed64(&ts1, 9000);
  3451. std::string ts2;
  3452. PutFixed64(&ts2, 8000);
  3453. std::string ts3;
  3454. PutFixed64(&ts3, 7000);
  3455. std::string ts4;
  3456. PutFixed64(&ts4, 6000);
  3457. NewVersionStorage(3, kCompactionStyleUniversal);
  3458. // A compaction should be triggered and pick file 2
  3459. Add(1, 1U, "150", "150", kFileSize, /*path_id=*/0, /*smallest_seq=*/100,
  3460. /*largest_seq=*/100, /*compensated_file_size=*/kFileSize,
  3461. /*marked_for_compact=*/false, Temperature::kUnknown,
  3462. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  3463. /*newest_key_time*/ kUnknownNewestKeyTime, ts1, ts2);
  3464. Add(2, 2U, "150", "150", kFileSize, /*path_id=*/0, /*smallest_seq=*/100,
  3465. /*largest_seq=*/100, /*compensated_file_size=*/kFileSize,
  3466. /*marked_for_compact=*/false, Temperature::kUnknown,
  3467. /*oldest_ancestor_time*/ kUnknownOldestAncesterTime,
  3468. /*newest_key_time*/ kUnknownNewestKeyTime, ts3, ts4);
  3469. UpdateVersionStorageInfo();
  3470. std::unique_ptr<Compaction> compaction(
  3471. universal_compaction_picker.PickCompaction(
  3472. cf_name_, mutable_cf_options_, mutable_db_options_,
  3473. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3474. vstorage_.get(), &log_buffer_));
  3475. assert(compaction);
  3476. ASSERT_TRUE(!compaction->is_trivial_move());
  3477. }
  3478. class PerKeyPlacementCompactionPickerTest
  3479. : public CompactionPickerTest,
  3480. public testing::WithParamInterface<bool> {
  3481. public:
  3482. PerKeyPlacementCompactionPickerTest() : CompactionPickerTest() {}
  3483. void SetUp() override { enable_per_key_placement_ = GetParam(); }
  3484. protected:
  3485. bool enable_per_key_placement_ = false;
  3486. };
  3487. TEST_P(PerKeyPlacementCompactionPickerTest, OverlapWithNormalCompaction) {
  3488. SyncPoint::GetInstance()->SetCallBack(
  3489. "Compaction::SupportsPerKeyPlacement:Enabled", [&](void* arg) {
  3490. auto supports_per_key_placement = static_cast<bool*>(arg);
  3491. *supports_per_key_placement = enable_per_key_placement_;
  3492. });
  3493. SyncPoint::GetInstance()->EnableProcessing();
  3494. int num_levels = ioptions_.num_levels;
  3495. NewVersionStorage(num_levels, kCompactionStyleLevel);
  3496. Add(0, 21U, "100", "150", 60000000U);
  3497. Add(0, 22U, "300", "350", 60000000U);
  3498. Add(5, 40U, "200", "250", 60000000U);
  3499. Add(6, 50U, "101", "351", 60000000U);
  3500. UpdateVersionStorageInfo();
  3501. CompactionOptions comp_options;
  3502. std::unordered_set<uint64_t> input_set;
  3503. input_set.insert(40);
  3504. std::vector<CompactionInputFiles> input_files;
  3505. ASSERT_OK(level_compaction_picker.GetCompactionInputsFromFileNumbers(
  3506. &input_files, &input_set, vstorage_.get(), comp_options));
  3507. std::unique_ptr<Compaction> comp1(
  3508. level_compaction_picker.PickCompactionForCompactFiles(
  3509. comp_options, input_files, 5, vstorage_.get(), mutable_cf_options_,
  3510. mutable_db_options_, 0));
  3511. input_set.clear();
  3512. input_files.clear();
  3513. input_set.insert(21);
  3514. input_set.insert(22);
  3515. input_set.insert(50);
  3516. ASSERT_OK(level_compaction_picker.GetCompactionInputsFromFileNumbers(
  3517. &input_files, &input_set, vstorage_.get(), comp_options));
  3518. ASSERT_EQ(enable_per_key_placement_,
  3519. level_compaction_picker.FilesRangeOverlapWithCompaction(
  3520. input_files, 6,
  3521. Compaction::EvaluateProximalLevel(
  3522. vstorage_.get(), mutable_cf_options_, ioptions_, 0, 6)));
  3523. }
  3524. TEST_P(PerKeyPlacementCompactionPickerTest, NormalCompactionOverlap) {
  3525. SyncPoint::GetInstance()->SetCallBack(
  3526. "Compaction::SupportsPerKeyPlacement:Enabled", [&](void* arg) {
  3527. auto supports_per_key_placement = static_cast<bool*>(arg);
  3528. *supports_per_key_placement = enable_per_key_placement_;
  3529. });
  3530. SyncPoint::GetInstance()->EnableProcessing();
  3531. int num_levels = ioptions_.num_levels;
  3532. NewVersionStorage(num_levels, kCompactionStyleLevel);
  3533. Add(0, 21U, "100", "150", 60000000U);
  3534. Add(0, 22U, "300", "350", 60000000U);
  3535. Add(4, 40U, "200", "220", 60000000U);
  3536. Add(4, 41U, "230", "250", 60000000U);
  3537. Add(6, 50U, "101", "351", 60000000U);
  3538. UpdateVersionStorageInfo();
  3539. CompactionOptions comp_options;
  3540. std::unordered_set<uint64_t> input_set;
  3541. input_set.insert(21);
  3542. input_set.insert(22);
  3543. input_set.insert(50);
  3544. std::vector<CompactionInputFiles> input_files;
  3545. ASSERT_OK(level_compaction_picker.GetCompactionInputsFromFileNumbers(
  3546. &input_files, &input_set, vstorage_.get(), comp_options));
  3547. std::unique_ptr<Compaction> comp1(
  3548. level_compaction_picker.PickCompactionForCompactFiles(
  3549. comp_options, input_files, 6, vstorage_.get(), mutable_cf_options_,
  3550. mutable_db_options_, 0));
  3551. input_set.clear();
  3552. input_files.clear();
  3553. input_set.insert(40);
  3554. input_set.insert(41);
  3555. ASSERT_OK(level_compaction_picker.GetCompactionInputsFromFileNumbers(
  3556. &input_files, &input_set, vstorage_.get(), comp_options));
  3557. ASSERT_EQ(enable_per_key_placement_,
  3558. level_compaction_picker.FilesRangeOverlapWithCompaction(
  3559. input_files, 5, Compaction::kInvalidLevel));
  3560. }
  3561. TEST_P(PerKeyPlacementCompactionPickerTest,
  3562. OverlapWithNormalCompactionUniveral) {
  3563. SyncPoint::GetInstance()->SetCallBack(
  3564. "Compaction::SupportsPerKeyPlacement:Enabled", [&](void* arg) {
  3565. auto supports_per_key_placement = static_cast<bool*>(arg);
  3566. *supports_per_key_placement = enable_per_key_placement_;
  3567. });
  3568. SyncPoint::GetInstance()->EnableProcessing();
  3569. int num_levels = ioptions_.num_levels;
  3570. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3571. NewVersionStorage(num_levels, kCompactionStyleUniversal);
  3572. Add(0, 21U, "100", "150", 60000000U);
  3573. Add(0, 22U, "300", "350", 60000000U);
  3574. Add(5, 40U, "200", "250", 60000000U);
  3575. Add(6, 50U, "101", "351", 60000000U);
  3576. UpdateVersionStorageInfo();
  3577. CompactionOptions comp_options;
  3578. std::unordered_set<uint64_t> input_set;
  3579. input_set.insert(40);
  3580. std::vector<CompactionInputFiles> input_files;
  3581. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3582. &input_files, &input_set, vstorage_.get(), comp_options));
  3583. std::unique_ptr<Compaction> comp1(
  3584. universal_compaction_picker.PickCompactionForCompactFiles(
  3585. comp_options, input_files, 5, vstorage_.get(), mutable_cf_options_,
  3586. mutable_db_options_, 0));
  3587. input_set.clear();
  3588. input_files.clear();
  3589. input_set.insert(21);
  3590. input_set.insert(22);
  3591. input_set.insert(50);
  3592. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3593. &input_files, &input_set, vstorage_.get(), comp_options));
  3594. ASSERT_EQ(enable_per_key_placement_,
  3595. universal_compaction_picker.FilesRangeOverlapWithCompaction(
  3596. input_files, 6,
  3597. Compaction::EvaluateProximalLevel(
  3598. vstorage_.get(), mutable_cf_options_, ioptions_, 0, 6)));
  3599. }
  3600. TEST_P(PerKeyPlacementCompactionPickerTest, NormalCompactionOverlapUniversal) {
  3601. SyncPoint::GetInstance()->SetCallBack(
  3602. "Compaction::SupportsPerKeyPlacement:Enabled", [&](void* arg) {
  3603. auto supports_per_key_placement = static_cast<bool*>(arg);
  3604. *supports_per_key_placement = enable_per_key_placement_;
  3605. });
  3606. SyncPoint::GetInstance()->EnableProcessing();
  3607. int num_levels = ioptions_.num_levels;
  3608. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3609. NewVersionStorage(num_levels, kCompactionStyleUniversal);
  3610. Add(0, 21U, "100", "150", 60000000U);
  3611. Add(0, 22U, "300", "350", 60000000U);
  3612. Add(4, 40U, "200", "220", 60000000U);
  3613. Add(4, 41U, "230", "250", 60000000U);
  3614. Add(6, 50U, "101", "351", 60000000U);
  3615. UpdateVersionStorageInfo();
  3616. CompactionOptions comp_options;
  3617. std::unordered_set<uint64_t> input_set;
  3618. input_set.insert(21);
  3619. input_set.insert(22);
  3620. input_set.insert(50);
  3621. std::vector<CompactionInputFiles> input_files;
  3622. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3623. &input_files, &input_set, vstorage_.get(), comp_options));
  3624. std::unique_ptr<Compaction> comp1(
  3625. universal_compaction_picker.PickCompactionForCompactFiles(
  3626. comp_options, input_files, 6, vstorage_.get(), mutable_cf_options_,
  3627. mutable_db_options_, 0));
  3628. input_set.clear();
  3629. input_files.clear();
  3630. input_set.insert(40);
  3631. input_set.insert(41);
  3632. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3633. &input_files, &input_set, vstorage_.get(), comp_options));
  3634. ASSERT_EQ(enable_per_key_placement_,
  3635. universal_compaction_picker.FilesRangeOverlapWithCompaction(
  3636. input_files, 5, Compaction::kInvalidLevel));
  3637. }
  3638. TEST_P(PerKeyPlacementCompactionPickerTest, ProximalOverlapUniversal) {
  3639. // This test is make sure the Tiered compaction would lock whole range of
  3640. // both output level and proximal level
  3641. if (enable_per_key_placement_) {
  3642. mutable_cf_options_.preclude_last_level_data_seconds = 10000;
  3643. }
  3644. int num_levels = ioptions_.num_levels;
  3645. ioptions_.compaction_style = kCompactionStyleUniversal;
  3646. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3647. NewVersionStorage(num_levels, kCompactionStyleUniversal);
  3648. // L4: [200, 220] [230, 250] [360, 380]
  3649. // L5:
  3650. // L6: [101, 351]
  3651. Add(4, 40U, "200", "220", 60000000U);
  3652. Add(4, 41U, "230", "250", 60000000U);
  3653. Add(4, 42U, "360", "380", 60000000U);
  3654. Add(6, 60U, "101", "351", 60000000U);
  3655. UpdateVersionStorageInfo();
  3656. // the existing compaction is the 1st L4 file + L6 file
  3657. // then compaction of the 2nd L4 file to L5 (proximal level) is overlapped
  3658. // when the tiered compaction feature is on.
  3659. CompactionOptions comp_options;
  3660. std::unordered_set<uint64_t> input_set;
  3661. input_set.insert(40);
  3662. input_set.insert(60);
  3663. std::vector<CompactionInputFiles> input_files;
  3664. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3665. &input_files, &input_set, vstorage_.get(), comp_options));
  3666. std::unique_ptr<Compaction> comp1(
  3667. universal_compaction_picker.PickCompactionForCompactFiles(
  3668. comp_options, input_files, 6, vstorage_.get(), mutable_cf_options_,
  3669. mutable_db_options_, 0));
  3670. input_set.clear();
  3671. input_files.clear();
  3672. input_set.insert(41);
  3673. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3674. &input_files, &input_set, vstorage_.get(), comp_options));
  3675. ASSERT_EQ(enable_per_key_placement_,
  3676. universal_compaction_picker.FilesRangeOverlapWithCompaction(
  3677. input_files, 5, Compaction::kInvalidLevel));
  3678. // compacting the 3rd L4 file is always safe:
  3679. input_set.clear();
  3680. input_files.clear();
  3681. input_set.insert(42);
  3682. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3683. &input_files, &input_set, vstorage_.get(), comp_options));
  3684. ASSERT_FALSE(universal_compaction_picker.FilesRangeOverlapWithCompaction(
  3685. input_files, 5, Compaction::kInvalidLevel));
  3686. }
  3687. TEST_P(PerKeyPlacementCompactionPickerTest, LastLevelOnlyOverlapUniversal) {
  3688. if (enable_per_key_placement_) {
  3689. mutable_cf_options_.preclude_last_level_data_seconds = 10000;
  3690. }
  3691. int num_levels = ioptions_.num_levels;
  3692. ioptions_.compaction_style = kCompactionStyleUniversal;
  3693. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3694. NewVersionStorage(num_levels, kCompactionStyleUniversal);
  3695. // L4: [200, 220] [230, 250] [360, 380]
  3696. // L5:
  3697. // L6: [101, 351]
  3698. Add(4, 40U, "200", "220", 60000000U);
  3699. Add(4, 41U, "230", "250", 60000000U);
  3700. Add(4, 42U, "360", "380", 60000000U);
  3701. Add(6, 60U, "101", "351", 60000000U);
  3702. UpdateVersionStorageInfo();
  3703. CompactionOptions comp_options;
  3704. std::unordered_set<uint64_t> input_set;
  3705. input_set.insert(60);
  3706. std::vector<CompactionInputFiles> input_files;
  3707. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3708. &input_files, &input_set, vstorage_.get(), comp_options));
  3709. std::unique_ptr<Compaction> comp1(
  3710. universal_compaction_picker.PickCompactionForCompactFiles(
  3711. comp_options, input_files, 6, vstorage_.get(), mutable_cf_options_,
  3712. mutable_db_options_, 0));
  3713. // cannot compact file 41 if the preclude_last_level feature is on, otherwise
  3714. // compact file 41 is okay.
  3715. input_set.clear();
  3716. input_files.clear();
  3717. input_set.insert(41);
  3718. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3719. &input_files, &input_set, vstorage_.get(), comp_options));
  3720. ASSERT_EQ(enable_per_key_placement_,
  3721. universal_compaction_picker.FilesRangeOverlapWithCompaction(
  3722. input_files, 5, Compaction::kInvalidLevel));
  3723. // compacting the 3rd L4 file is always safe:
  3724. input_set.clear();
  3725. input_files.clear();
  3726. input_set.insert(42);
  3727. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3728. &input_files, &input_set, vstorage_.get(), comp_options));
  3729. ASSERT_FALSE(universal_compaction_picker.FilesRangeOverlapWithCompaction(
  3730. input_files, 5, Compaction::kInvalidLevel));
  3731. }
  3732. TEST_P(PerKeyPlacementCompactionPickerTest,
  3733. LastLevelOnlyFailProximalUniversal) {
  3734. // This is to test last_level only compaction still unable to do the
  3735. // proximal level compaction if there's already a file in the proximal
  3736. // level.
  3737. // This should rarely happen in universal compaction, as the non-empty L5
  3738. // should be included in the compaction.
  3739. if (enable_per_key_placement_) {
  3740. mutable_cf_options_.preclude_last_level_data_seconds = 10000;
  3741. }
  3742. int num_levels = ioptions_.num_levels;
  3743. ioptions_.compaction_style = kCompactionStyleUniversal;
  3744. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3745. NewVersionStorage(num_levels, kCompactionStyleUniversal);
  3746. // L4: [200, 220]
  3747. // L5: [230, 250]
  3748. // L6: [101, 351]
  3749. Add(4, 40U, "200", "220", 60000000U);
  3750. Add(5, 50U, "230", "250", 60000000U);
  3751. Add(6, 60U, "101", "351", 60000000U);
  3752. UpdateVersionStorageInfo();
  3753. CompactionOptions comp_options;
  3754. std::unordered_set<uint64_t> input_set;
  3755. input_set.insert(60);
  3756. std::vector<CompactionInputFiles> input_files;
  3757. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3758. &input_files, &input_set, vstorage_.get(), comp_options));
  3759. std::unique_ptr<Compaction> comp1(
  3760. universal_compaction_picker.PickCompactionForCompactFiles(
  3761. comp_options, input_files, 6, vstorage_.get(), mutable_cf_options_,
  3762. mutable_db_options_, 0));
  3763. ASSERT_TRUE(comp1);
  3764. ASSERT_EQ(comp1->GetProximalLevel(), Compaction::kInvalidLevel);
  3765. // As comp1 cannot be output to the proximal level, compacting file 40 to
  3766. // L5 is always safe.
  3767. input_set.clear();
  3768. input_files.clear();
  3769. input_set.insert(40);
  3770. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3771. &input_files, &input_set, vstorage_.get(), comp_options));
  3772. ASSERT_FALSE(universal_compaction_picker.FilesRangeOverlapWithCompaction(
  3773. input_files, 5, Compaction::kInvalidLevel));
  3774. std::unique_ptr<Compaction> comp2(
  3775. universal_compaction_picker.PickCompactionForCompactFiles(
  3776. comp_options, input_files, 5, vstorage_.get(), mutable_cf_options_,
  3777. mutable_db_options_, 0));
  3778. ASSERT_TRUE(comp2);
  3779. ASSERT_EQ(Compaction::kInvalidLevel, comp2->GetProximalLevel());
  3780. }
  3781. TEST_P(PerKeyPlacementCompactionPickerTest,
  3782. LastLevelOnlyConflictWithOngoingUniversal) {
  3783. // This is to test last_level only compaction still unable to do the
  3784. // proximal level compaction if there's already an ongoing compaction to
  3785. // the proximal level
  3786. if (enable_per_key_placement_) {
  3787. mutable_cf_options_.preclude_last_level_data_seconds = 10000;
  3788. }
  3789. int num_levels = ioptions_.num_levels;
  3790. ioptions_.compaction_style = kCompactionStyleUniversal;
  3791. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3792. NewVersionStorage(num_levels, kCompactionStyleUniversal);
  3793. // L4: [200, 220] [230, 250] [360, 380]
  3794. // L5:
  3795. // L6: [101, 351]
  3796. Add(4, 40U, "200", "220", 60000000U);
  3797. Add(4, 41U, "230", "250", 60000000U);
  3798. Add(4, 42U, "360", "380", 60000000U);
  3799. Add(6, 60U, "101", "351", 60000000U);
  3800. UpdateVersionStorageInfo();
  3801. // create an ongoing compaction to L5 (proximal level)
  3802. CompactionOptions comp_options;
  3803. std::unordered_set<uint64_t> input_set;
  3804. input_set.insert(40);
  3805. std::vector<CompactionInputFiles> input_files;
  3806. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3807. &input_files, &input_set, vstorage_.get(), comp_options));
  3808. std::unique_ptr<Compaction> comp1(
  3809. universal_compaction_picker.PickCompactionForCompactFiles(
  3810. comp_options, input_files, 5, vstorage_.get(), mutable_cf_options_,
  3811. mutable_db_options_, 0));
  3812. ASSERT_TRUE(comp1);
  3813. ASSERT_EQ(comp1->GetProximalLevel(), Compaction::kInvalidLevel);
  3814. input_set.clear();
  3815. input_files.clear();
  3816. input_set.insert(60);
  3817. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3818. &input_files, &input_set, vstorage_.get(), comp_options));
  3819. ASSERT_EQ(enable_per_key_placement_,
  3820. universal_compaction_picker.FilesRangeOverlapWithCompaction(
  3821. input_files, 6,
  3822. Compaction::EvaluateProximalLevel(
  3823. vstorage_.get(), mutable_cf_options_, ioptions_, 6, 6)));
  3824. if (!enable_per_key_placement_) {
  3825. std::unique_ptr<Compaction> comp2(
  3826. universal_compaction_picker.PickCompactionForCompactFiles(
  3827. comp_options, input_files, 6, vstorage_.get(), mutable_cf_options_,
  3828. mutable_db_options_, 0));
  3829. ASSERT_TRUE(comp2);
  3830. ASSERT_EQ(Compaction::kInvalidLevel, comp2->GetProximalLevel());
  3831. }
  3832. }
  3833. TEST_P(PerKeyPlacementCompactionPickerTest,
  3834. LastLevelOnlyNoConflictWithOngoingUniversal) {
  3835. // This is similar to `LastLevelOnlyConflictWithOngoingUniversal`, the only
  3836. // change is the ongoing compaction to L5 has no overlap with the last level
  3837. // compaction, so it's safe to move data from the last level to the
  3838. // proximal level.
  3839. if (enable_per_key_placement_) {
  3840. mutable_cf_options_.preclude_last_level_data_seconds = 10000;
  3841. }
  3842. int num_levels = ioptions_.num_levels;
  3843. ioptions_.compaction_style = kCompactionStyleUniversal;
  3844. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  3845. NewVersionStorage(num_levels, kCompactionStyleUniversal);
  3846. // L4: [200, 220] [230, 250] [360, 380]
  3847. // L5:
  3848. // L6: [101, 351]
  3849. Add(4, 40U, "200", "220", 60000000U);
  3850. Add(4, 41U, "230", "250", 60000000U);
  3851. Add(4, 42U, "360", "380", 60000000U);
  3852. Add(6, 60U, "101", "351", 60000000U);
  3853. UpdateVersionStorageInfo();
  3854. // create an ongoing compaction to L5 (proximal level)
  3855. CompactionOptions comp_options;
  3856. std::unordered_set<uint64_t> input_set;
  3857. input_set.insert(42);
  3858. std::vector<CompactionInputFiles> input_files;
  3859. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3860. &input_files, &input_set, vstorage_.get(), comp_options));
  3861. std::unique_ptr<Compaction> comp1(
  3862. universal_compaction_picker.PickCompactionForCompactFiles(
  3863. comp_options, input_files, 5, vstorage_.get(), mutable_cf_options_,
  3864. mutable_db_options_, 0));
  3865. ASSERT_TRUE(comp1);
  3866. ASSERT_EQ(comp1->GetProximalLevel(), Compaction::kInvalidLevel);
  3867. input_set.clear();
  3868. input_files.clear();
  3869. input_set.insert(60);
  3870. ASSERT_OK(universal_compaction_picker.GetCompactionInputsFromFileNumbers(
  3871. &input_files, &input_set, vstorage_.get(), comp_options));
  3872. // always safe to move data up
  3873. ASSERT_FALSE(universal_compaction_picker.FilesRangeOverlapWithCompaction(
  3874. input_files, 6,
  3875. Compaction::EvaluateProximalLevel(vstorage_.get(), mutable_cf_options_,
  3876. ioptions_, 6, 6)));
  3877. // 2 compactions can be run in parallel
  3878. std::unique_ptr<Compaction> comp2(
  3879. universal_compaction_picker.PickCompactionForCompactFiles(
  3880. comp_options, input_files, 6, vstorage_.get(), mutable_cf_options_,
  3881. mutable_db_options_, 0));
  3882. ASSERT_TRUE(comp2);
  3883. if (enable_per_key_placement_) {
  3884. ASSERT_NE(Compaction::kInvalidLevel, comp2->GetProximalLevel());
  3885. } else {
  3886. ASSERT_EQ(Compaction::kInvalidLevel, comp2->GetProximalLevel());
  3887. }
  3888. }
  3889. TEST_F(CompactionPickerTest,
  3890. LevelCompactionPrioritizeFilesMarkedForCompaction1) {
  3891. int num_levels = ioptions_.num_levels;
  3892. ioptions_.level_compaction_dynamic_level_bytes = true;
  3893. ioptions_.compaction_pri = kMinOverlappingRatio;
  3894. mutable_cf_options_.max_bytes_for_level_base = 200;
  3895. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  3896. NewVersionStorage(num_levels, kCompactionStyleLevel);
  3897. // L5
  3898. // F4 [100, 200] size:100, F5 [300, 400] size:100, F6 [500, 600] size:200
  3899. // F5 is marked for compaction
  3900. // L6
  3901. // F1 [100, 200] size:100, F2 [300, 400] size:1000, F3 [500, 600] size:100
  3902. //
  3903. // First compaction should pick F5 since it's marked for compaction.
  3904. // Second compaction should pick F6 since it has min overlap ratio.
  3905. Add(/*level=*/num_levels - 1, /*file_number=*/1U, /*smallest=*/"100",
  3906. /*largest=*/"200",
  3907. /*file_size=*/100, /*path_id=*/0, /*smallest_seq=*/0,
  3908. /*largest_seq=*/0, /*compensated_file_size=*/100,
  3909. /*marked_for_compact=*/false);
  3910. Add(/*level=*/num_levels - 1, /*file_number=*/2U, /*smallest=*/"300",
  3911. /*largest=*/"400",
  3912. /*file_size=*/1000, /*path_id=*/0, /*smallest_seq=*/0,
  3913. /*largest_seq=*/0, /*compensated_file_size=*/1000,
  3914. /*marked_for_compact=*/false);
  3915. Add(/*level=*/num_levels - 1, /*file_number=*/3U, /*smallest=*/"500",
  3916. /*largest=*/"600",
  3917. /*file_size=*/100, /*path_id=*/0, /*smallest_seq=*/0,
  3918. /*largest_seq=*/0, /*compensated_file_size=*/100,
  3919. /*marked_for_compact=*/false);
  3920. Add(/*level=*/num_levels - 2, /*file_number=*/4U, /*smallest=*/"100",
  3921. /*largest=*/"200",
  3922. /*file_size=*/100, /*path_id=*/0, /*smallest_seq=*/100,
  3923. /*largest_seq=*/200, /*compensated_file_size=*/100,
  3924. /*marked_for_compact=*/false);
  3925. // Marked for compaction, but with a larger overlap ratio.
  3926. Add(/*level=*/num_levels - 2, /*file_number=*/5U, /*smallest=*/"300",
  3927. /*largest=*/"400",
  3928. /*file_size=*/100, /*path_id=*/0, /*smallest_seq=*/300,
  3929. /*largest_seq=*/400, /*compensated_file_size=*/100,
  3930. /*marked_for_compact=*/true);
  3931. Add(/*level=*/num_levels - 2, /*file_number=*/6U, /*smallest=*/"500",
  3932. /*largest=*/"600",
  3933. /*file_size=*/200, /*path_id=*/0, /*smallest_seq=*/400,
  3934. /*largest_seq=*/500, /*compensated_file_size=*/200,
  3935. /*marked_for_compact=*/false);
  3936. UpdateVersionStorageInfo();
  3937. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  3938. cf_name_, mutable_cf_options_, mutable_db_options_,
  3939. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3940. vstorage_.get(), &log_buffer_));
  3941. ASSERT_TRUE(compaction);
  3942. ASSERT_EQ(num_levels - 2, compaction->start_level());
  3943. ASSERT_EQ(num_levels - 1, compaction->output_level());
  3944. ASSERT_EQ(1U, compaction->num_input_files(0));
  3945. ASSERT_EQ(5U, compaction->input(0, 0)->fd.GetNumber());
  3946. std::unique_ptr<Compaction> second_compaction(
  3947. level_compaction_picker.PickCompaction(
  3948. cf_name_, mutable_cf_options_, mutable_db_options_,
  3949. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3950. vstorage_.get(), &log_buffer_));
  3951. ASSERT_TRUE(second_compaction);
  3952. ASSERT_EQ(num_levels - 1, compaction->output_level());
  3953. ASSERT_EQ(num_levels - 2, compaction->start_level());
  3954. ASSERT_EQ(1U, compaction->num_input_files(0));
  3955. ASSERT_EQ(6U, second_compaction->input(0, 0)->fd.GetNumber());
  3956. }
  3957. TEST_F(CompactionPickerTest,
  3958. LevelCompactionPrioritizeFilesMarkedForCompaction2) {
  3959. int num_levels = ioptions_.num_levels;
  3960. ioptions_.level_compaction_dynamic_level_bytes = true;
  3961. ioptions_.compaction_pri = kMinOverlappingRatio;
  3962. mutable_cf_options_.max_bytes_for_level_base = 200;
  3963. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  3964. // L4
  3965. // F3 [100, 200] size:2000
  3966. // L5
  3967. // F2 [100, 200] size:2000, marked for compaction
  3968. // L6
  3969. // F1 [100, 200] size: 20000
  3970. //
  3971. // L4 should be prioritized over L5 since L4 has a higher compaction score.
  3972. // Files marked for compaction do not affect level picking order.
  3973. NewVersionStorage(num_levels, kCompactionStyleLevel);
  3974. Add(/*level=*/num_levels - 1, /*file_number=*/1U, /*smallest=*/"100",
  3975. /*largest=*/"200",
  3976. /*file_size=*/20000, /*path_id=*/0, /*smallest_seq=*/0,
  3977. /*largest_seq=*/0, /*compensated_file_size=*/100,
  3978. /*marked_for_compact=*/false);
  3979. // Level score should be 1.
  3980. Add(/*level=*/num_levels - 2, /*file_number=*/2U, /*smallest=*/"100",
  3981. /*largest=*/"200",
  3982. /*file_size=*/2000, /*path_id=*/0, /*smallest_seq=*/100,
  3983. /*largest_seq=*/200, /*compensated_file_size=*/2000,
  3984. /*marked_for_compact=*/true);
  3985. // Level score should be larger than L5.
  3986. Add(/*level=*/num_levels - 3, /*file_number=*/3U, /*smallest=*/"100",
  3987. /*largest=*/"200",
  3988. /*file_size=*/2000, /*path_id=*/0, /*smallest_seq=*/300,
  3989. /*largest_seq=*/400, /*compensated_file_size=*/2000,
  3990. /*marked_for_compact=*/false);
  3991. UpdateVersionStorageInfo();
  3992. std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
  3993. cf_name_, mutable_cf_options_, mutable_db_options_,
  3994. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  3995. vstorage_.get(), &log_buffer_));
  3996. ASSERT_TRUE(compaction);
  3997. ASSERT_EQ(num_levels - 3, compaction->start_level());
  3998. ASSERT_EQ(num_levels - 2, compaction->output_level());
  3999. ASSERT_EQ(1U, compaction->num_input_files(0));
  4000. ASSERT_EQ(3U, compaction->input(0, 0)->fd.GetNumber());
  4001. }
  4002. INSTANTIATE_TEST_CASE_P(PerKeyPlacementCompactionPickerTest,
  4003. PerKeyPlacementCompactionPickerTest, ::testing::Bool());
  4004. TEST_F(CompactionPickerTest, IntraL0WhenL0IsSmall) {
  4005. mutable_cf_options_.level0_file_num_compaction_trigger = 4;
  4006. mutable_cf_options_.max_bytes_for_level_multiplier = 10;
  4007. for (const uint64_t lbase_size_multiplier : {1, 10, 11, 40}) {
  4008. SCOPED_TRACE("lbase_size_multiplier=" +
  4009. std::to_string(lbase_size_multiplier));
  4010. NewVersionStorage(6, kCompactionStyleLevel);
  4011. // When L0 size is <= Lbase size / max_bytes_for_level_multiplier / 2,
  4012. // intra-L0 compaction is picked. Otherwise, L0->L1
  4013. // compaction is picked.
  4014. // compensated_file_size will be used to compute total l0 size.
  4015. Add(/*level=*/0, /*file_number=*/1U, /*smallest=*/"100",
  4016. /*largest=*/"200", /*file_size=*/10, /*path_id=*/0,
  4017. /*smallest_seq=*/10, /*largest_seq=*/11,
  4018. /*compensated_file_size=*/1000);
  4019. Add(/*level=*/0, /*file_number=*/2U, /*smallest=*/"100",
  4020. /*largest=*/"100", /*file_size=*/10, /*path_id=*/0,
  4021. /*smallest_seq=*/20, /*largest_seq=*/21,
  4022. /*compensated_file_size=*/1000);
  4023. Add(/*level=*/0, /*file_number=*/3U, /*smallest=*/"100",
  4024. /*largest=*/"200", /*file_size=*/10, /*path_id=*/0,
  4025. /*smallest_seq=*/30, /*largest_seq=*/31,
  4026. /*compensated_file_size=*/1000);
  4027. Add(/*level=*/0, /*file_number=*/4U, /*smallest=*/"100",
  4028. /*largest=*/"200", /*file_size=*/10, /*path_id=*/0,
  4029. /*smallest_seq=*/40, /*largest_seq=*/41,
  4030. /*compensated_file_size=*/1000);
  4031. const uint64_t l0_size = 4000;
  4032. const uint64_t lbase_size = l0_size * lbase_size_multiplier * 2;
  4033. Add(/*level=*/1, /*file_number=*/5U, /*smallest=*/"100",
  4034. /*largest=*/"200", /*file_size=*/lbase_size, /*path_id=*/0,
  4035. /*smallest_seq=*/0, /*largest_seq=*/0,
  4036. /*compensated_file_size=*/lbase_size);
  4037. UpdateVersionStorageInfo();
  4038. LevelCompactionPicker compaction_picker(ioptions_, &icmp_);
  4039. std::unique_ptr<Compaction> compaction(compaction_picker.PickCompaction(
  4040. cf_name_, mutable_cf_options_, mutable_db_options_,
  4041. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  4042. vstorage_.get(), &log_buffer_));
  4043. ASSERT_TRUE(compaction.get() != nullptr);
  4044. ASSERT_EQ(CompactionReason::kLevelL0FilesNum,
  4045. compaction->compaction_reason());
  4046. ASSERT_EQ(4U, compaction->num_input_files(0));
  4047. if (lbase_size_multiplier >
  4048. mutable_cf_options_.max_bytes_for_level_multiplier) {
  4049. ASSERT_EQ(1U, compaction->num_input_levels());
  4050. ASSERT_EQ(0, compaction->output_level());
  4051. } else {
  4052. ASSERT_EQ(2U, compaction->num_input_levels());
  4053. ASSERT_EQ(1, compaction->output_level());
  4054. }
  4055. }
  4056. }
  4057. TEST_F(CompactionPickerTest, UniversalMaxReadAmpLargeDB) {
  4058. ioptions_.compaction_style = kCompactionStyleUniversal;
  4059. ioptions_.num_levels = 50;
  4060. mutable_cf_options_.RefreshDerivedOptions(ioptions_);
  4061. mutable_cf_options_.compaction_options_universal.size_ratio = 10;
  4062. mutable_cf_options_.write_buffer_size = 256 << 20;
  4063. // Avoid space amp compaction
  4064. mutable_cf_options_.compaction_options_universal
  4065. .max_size_amplification_percent = 200;
  4066. const int kMaxRuns = 8;
  4067. for (int max_read_amp : {kMaxRuns, 0, -1}) {
  4068. SCOPED_TRACE("max_read_amp = " + std::to_string(max_read_amp));
  4069. if (max_read_amp == -1) {
  4070. mutable_cf_options_.level0_file_num_compaction_trigger = kMaxRuns;
  4071. } else {
  4072. mutable_cf_options_.level0_file_num_compaction_trigger = 4;
  4073. }
  4074. mutable_cf_options_.compaction_options_universal.max_read_amp =
  4075. max_read_amp;
  4076. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  4077. uint64_t max_run_size = 20ull << 30;
  4078. // When max_read_amp = 0, we estimate the number of levels needed based on
  4079. // size_ratio and write_buffer_size. See more in
  4080. // UniversalCompactionBuilder::PickCompaction().
  4081. // With a 20GB last level, we estimate that 8 levels are needed:
  4082. // L0 256MB
  4083. // L1 256MB * 1.1 (size_ratio) = 282MB
  4084. // L2 (256MB + 282MB) * 1.1 = 592MB
  4085. // L3 1243MB
  4086. // L4 2610MB
  4087. // L5 5481MB
  4088. // L6 11510MB
  4089. // L7 24171MB > 20GB
  4090. for (int i = 0; i <= kMaxRuns; ++i) {
  4091. SCOPED_TRACE("i = " + std::to_string(i));
  4092. NewVersionStorage(/*num_levels=*/50, kCompactionStyleUniversal);
  4093. Add(/*level=*/49, /*file_number=*/10, /*smallest=*/"100",
  4094. /*largest=*/"200", /*file_size=*/max_run_size, /*path_id=*/0,
  4095. /*smallest_seq=*/0, /*largest_seq=*/0,
  4096. /*compensated_file_size=*/max_run_size);
  4097. // Besides the last sorted run, we add additional `i` sorted runs
  4098. // without triggering space-amp or size-amp compactions.
  4099. uint64_t file_size = 1 << 20;
  4100. for (int j = 0; j < i; ++j) {
  4101. Add(/*level=*/j, /*file_number=*/100 - j, /*smallest=*/"100",
  4102. /*largest=*/"200", /*file_size=*/file_size, /*path_id=*/0,
  4103. /*smallest_seq=*/100 - j, /*largest_seq=*/100 - j,
  4104. /*compensated_file_size=*/file_size);
  4105. // to avoid space-amp and size-amp compaction
  4106. file_size *= 2;
  4107. }
  4108. UpdateVersionStorageInfo();
  4109. // level0_file_num_compaction_trigger is still used as trigger to
  4110. // check potential compactions
  4111. ASSERT_EQ(
  4112. universal_compaction_picker.NeedsCompaction(vstorage_.get()),
  4113. i + 1 >= mutable_cf_options_.level0_file_num_compaction_trigger);
  4114. std::unique_ptr<Compaction> compaction(
  4115. universal_compaction_picker.PickCompaction(
  4116. cf_name_, mutable_cf_options_, mutable_db_options_,
  4117. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  4118. vstorage_.get(), &log_buffer_));
  4119. if (i == kMaxRuns) {
  4120. // There are in total i + 1 > kMaxRuns sorted runs.
  4121. // This triggers compaction ignoring size_ratio.
  4122. ASSERT_NE(nullptr, compaction);
  4123. ASSERT_EQ(CompactionReason::kUniversalSortedRunNum,
  4124. compaction->compaction_reason());
  4125. // First two runs are compacted
  4126. ASSERT_EQ(0, compaction->start_level());
  4127. ASSERT_EQ(1, compaction->output_level());
  4128. ASSERT_EQ(1U, compaction->num_input_files(0));
  4129. ASSERT_EQ(1U, compaction->num_input_files(1));
  4130. } else {
  4131. ASSERT_EQ(nullptr, compaction);
  4132. }
  4133. }
  4134. }
  4135. }
  4136. TEST_F(CompactionPickerTest, UniversalMaxReadAmpSmallDB) {
  4137. ioptions_.compaction_style = kCompactionStyleUniversal;
  4138. ioptions_.num_levels = 50;
  4139. mutable_cf_options_.RefreshDerivedOptions(ioptions_);
  4140. mutable_cf_options_.level0_file_num_compaction_trigger = 1;
  4141. mutable_cf_options_.compaction_options_universal.size_ratio = 10;
  4142. mutable_cf_options_.write_buffer_size = 256 << 20;
  4143. mutable_cf_options_.compaction_options_universal
  4144. .max_size_amplification_percent = 200;
  4145. const int kMaxRuns = 1;
  4146. for (int max_read_amp : {-1, kMaxRuns, 0}) {
  4147. SCOPED_TRACE("max_read_amp = " + std::to_string(max_read_amp));
  4148. mutable_cf_options_.compaction_options_universal.max_read_amp =
  4149. max_read_amp;
  4150. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  4151. NewVersionStorage(/*num_levels=*/50, kCompactionStyleUniversal);
  4152. // max_run_size is much smaller than write_buffer_size,
  4153. // only 1 level is needed.
  4154. uint64_t max_run_size = 8 << 10;
  4155. Add(/*level=*/49, /*file_number=*/10, /*smallest=*/"100",
  4156. /*largest=*/"200", /*file_size=*/max_run_size, /*path_id=*/0,
  4157. /*smallest_seq=*/0, /*largest_seq=*/0,
  4158. /*compensated_file_size=*/max_run_size);
  4159. UpdateVersionStorageInfo();
  4160. ASSERT_TRUE(universal_compaction_picker.NeedsCompaction(vstorage_.get()));
  4161. std::unique_ptr<Compaction> compaction(
  4162. universal_compaction_picker.PickCompaction(
  4163. cf_name_, mutable_cf_options_, mutable_db_options_,
  4164. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  4165. vstorage_.get(), &log_buffer_));
  4166. ASSERT_EQ(nullptr, compaction);
  4167. }
  4168. }
  4169. TEST_F(CompactionPickerTest, StandaloneRangeDeletionOnlyPicksOlderFiles) {
  4170. NewVersionStorage(6, kCompactionStyleUniversal);
  4171. // Create L0 files with overlapping ranges
  4172. // File 1: newest regular file (epoch 5), keys [100, 200]
  4173. Add(0, 1U, "100", "200", 1U, 0, 100, 100, 0, false, Temperature::kUnknown,
  4174. kUnknownOldestAncesterTime, kUnknownNewestKeyTime, Slice(), Slice(), 5);
  4175. // File 2: standalone range deletion (epoch 4), keys [150, 250]
  4176. // This file should be marked as having only range deletions
  4177. Add(0, 2U, "150", "250", 1U, 0, 200, 200, 0, true, Temperature::kUnknown,
  4178. kUnknownOldestAncesterTime, kUnknownNewestKeyTime, Slice(), Slice(), 4);
  4179. // Manually set file 2 as standalone range deletion
  4180. FileMetaData* range_del_file = file_map_[2U].first;
  4181. range_del_file->num_entries = 1;
  4182. range_del_file->num_range_deletions = 1;
  4183. ASSERT_TRUE(range_del_file->FileIsStandAloneRangeTombstone());
  4184. Add(4, 10U, "000", "400", 1U);
  4185. Add(5, 20U, "000", "400", 100);
  4186. UpdateVersionStorageInfo();
  4187. UniversalCompactionPicker universal_compaction_picker(ioptions_, &icmp_);
  4188. ASSERT_TRUE(universal_compaction_picker.NeedsCompaction(vstorage_.get()));
  4189. std::unique_ptr<Compaction> compaction(
  4190. universal_compaction_picker.PickCompaction(
  4191. cf_name_, mutable_cf_options_, mutable_db_options_,
  4192. /*existing_snapshots=*/{}, /* snapshot_checker */ nullptr,
  4193. vstorage_.get(), &log_buffer_));
  4194. ASSERT_NE(nullptr, compaction);
  4195. ASSERT_EQ(2U, compaction->num_input_levels());
  4196. // First input level should be L0 with only the standalone range del file
  4197. // (file 2)
  4198. ASSERT_EQ(0, compaction->level(0));
  4199. ASSERT_EQ(1U, compaction->num_input_files(0));
  4200. ASSERT_EQ(2U, compaction->input(0, 0)->fd.GetNumber());
  4201. ASSERT_TRUE(compaction->input(0, 0)->FileIsStandAloneRangeTombstone());
  4202. // Second input level should be L4 with file 10
  4203. ASSERT_EQ(4, compaction->level(1));
  4204. ASSERT_EQ(1U, compaction->num_input_files(1));
  4205. ASSERT_EQ(10U, compaction->input(1, 0)->fd.GetNumber());
  4206. }
  4207. } // namespace ROCKSDB_NAMESPACE
  4208. int main(int argc, char** argv) {
  4209. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  4210. ::testing::InitGoogleTest(&argc, argv);
  4211. return RUN_ALL_TESTS();
  4212. }