| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084 |
- // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
- // This source code is licensed under both the GPLv2 (found in the
- // COPYING file in the root directory) and Apache 2.0 License
- // (found in the LICENSE.Apache file in the root directory).
- //
- // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file. See the AUTHORS file for names of contributors.
- #include <algorithm>
- #include <atomic>
- #include <string>
- #include <thread>
- #include <vector>
- #include "db/db_impl/db_impl.h"
- #include "db/db_test_util.h"
- #include "options/options_parser.h"
- #include "port/port.h"
- #include "port/stack_trace.h"
- #include "rocksdb/comparator.h"
- #include "rocksdb/convenience.h"
- #include "rocksdb/db.h"
- #include "rocksdb/env.h"
- #include "rocksdb/iterator.h"
- #include "rocksdb/listener.h"
- #include "rocksdb/utilities/object_registry.h"
- #include "test_util/sync_point.h"
- #include "test_util/testharness.h"
- #include "test_util/testutil.h"
- #include "util/coding.h"
- #include "util/defer.h"
- #include "util/string_util.h"
- #include "utilities/fault_injection_env.h"
- #include "utilities/merge_operators.h"
- namespace ROCKSDB_NAMESPACE {
- namespace {
- std::string EncodeAsUint64(uint64_t number) {
- std::string result;
- PutFixed64(&result, number);
- return result;
- }
- } // namespace
- static const int kValueSize = 1000;
- // counts how many operations were performed
- class EnvCounter : public SpecialEnv {
- public:
- explicit EnvCounter(Env* base)
- : SpecialEnv(base), num_new_writable_file_(0) {}
- int GetNumberOfNewWritableFileCalls() { return num_new_writable_file_; }
- Status NewWritableFile(const std::string& f, std::unique_ptr<WritableFile>* r,
- const EnvOptions& soptions) override {
- ++num_new_writable_file_;
- return EnvWrapper::NewWritableFile(f, r, soptions);
- }
- private:
- std::atomic<int> num_new_writable_file_;
- };
- class ColumnFamilyTestBase : public testing::Test {
- public:
- explicit ColumnFamilyTestBase(uint32_t format) : rnd_(139), format_(format) {
- Env* base_env = Env::Default();
- EXPECT_OK(
- test::CreateEnvFromSystem(ConfigOptions(), &base_env, &env_guard_));
- EXPECT_NE(nullptr, base_env);
- env_ = new EnvCounter(base_env);
- env_->skip_fsync_ = true;
- dbname_ = test::PerThreadDBPath("column_family_test");
- db_options_.create_if_missing = true;
- db_options_.env = env_;
- }
- void SetUp() override {
- EXPECT_OK(DestroyDB(dbname_, Options(db_options_, column_family_options_)));
- }
- ~ColumnFamilyTestBase() override {
- std::vector<ColumnFamilyDescriptor> column_families;
- for (auto h : handles_) {
- ColumnFamilyDescriptor cfdescriptor;
- Status s = h->GetDescriptor(&cfdescriptor);
- EXPECT_OK(s);
- column_families.push_back(cfdescriptor);
- }
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- Destroy(column_families);
- delete env_;
- }
- BlockBasedTableOptions GetBlockBasedTableOptions() {
- BlockBasedTableOptions options;
- options.format_version = format_;
- return options;
- }
- // Return the value to associate with the specified key
- Slice Value(int k, std::string* storage) {
- if (k == 0) {
- // Ugh. Random seed of 0 used to produce no entropy. This code
- // preserves the implementation that was in place when all of the
- // magic values in this file were picked.
- *storage = std::string(kValueSize, ' ');
- } else {
- Random r(k);
- *storage = r.RandomString(kValueSize);
- }
- return Slice(*storage);
- }
- void Build(int base, int n, int flush_every = 0) {
- std::string key_space, value_space;
- WriteBatch batch;
- for (int i = 0; i < n; i++) {
- if (flush_every != 0 && i != 0 && i % flush_every == 0) {
- DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
- dbi->TEST_FlushMemTable();
- }
- int keyi = base + i;
- Slice key(DBTestBase::Key(keyi));
- batch.Clear();
- batch.Put(handles_[0], key, Value(keyi, &value_space));
- batch.Put(handles_[1], key, Value(keyi, &value_space));
- batch.Put(handles_[2], key, Value(keyi, &value_space));
- ASSERT_OK(db_->Write(WriteOptions(), &batch));
- }
- }
- void CheckMissed() {
- uint64_t next_expected = 0;
- uint64_t missed = 0;
- int bad_keys = 0;
- int bad_values = 0;
- int correct = 0;
- std::string value_space;
- for (int cf = 0; cf < 3; cf++) {
- next_expected = 0;
- Iterator* iter = db_->NewIterator(ReadOptions(false, true), handles_[cf]);
- for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
- uint64_t key;
- Slice in(iter->key());
- in.remove_prefix(3);
- if (!ConsumeDecimalNumber(&in, &key) || !in.empty() ||
- key < next_expected) {
- bad_keys++;
- continue;
- }
- missed += (key - next_expected);
- next_expected = key + 1;
- if (iter->value() != Value(static_cast<int>(key), &value_space)) {
- bad_values++;
- } else {
- correct++;
- }
- }
- delete iter;
- }
- ASSERT_EQ(0, bad_keys);
- ASSERT_EQ(0, bad_values);
- ASSERT_EQ(0, missed);
- (void)correct;
- }
- void Close() {
- for (auto h : handles_) {
- if (h) {
- ASSERT_OK(db_->DestroyColumnFamilyHandle(h));
- }
- }
- handles_.clear();
- names_.clear();
- delete db_;
- db_ = nullptr;
- }
- Status TryOpen(std::vector<std::string> cf,
- std::vector<ColumnFamilyOptions> options = {}) {
- std::vector<ColumnFamilyDescriptor> column_families;
- names_.clear();
- for (size_t i = 0; i < cf.size(); ++i) {
- column_families.emplace_back(
- cf[i], options.size() == 0 ? column_family_options_ : options[i]);
- names_.push_back(cf[i]);
- }
- return DB::Open(db_options_, dbname_, column_families, &handles_, &db_);
- }
- Status OpenReadOnly(std::vector<std::string> cf,
- std::vector<ColumnFamilyOptions> options = {}) {
- std::vector<ColumnFamilyDescriptor> column_families;
- names_.clear();
- for (size_t i = 0; i < cf.size(); ++i) {
- column_families.emplace_back(
- cf[i], options.size() == 0 ? column_family_options_ : options[i]);
- names_.push_back(cf[i]);
- }
- return DB::OpenForReadOnly(db_options_, dbname_, column_families, &handles_,
- &db_);
- }
- void AssertOpenReadOnly(std::vector<std::string> cf,
- std::vector<ColumnFamilyOptions> options = {}) {
- ASSERT_OK(OpenReadOnly(cf, options));
- }
- void Open(std::vector<std::string> cf,
- std::vector<ColumnFamilyOptions> options = {}) {
- ASSERT_OK(TryOpen(cf, options));
- }
- void Open() { Open({"default"}); }
- DBImpl* dbfull() { return static_cast_with_check<DBImpl>(db_); }
- int GetProperty(int cf, std::string property) {
- std::string value;
- EXPECT_TRUE(dbfull()->GetProperty(handles_[cf], property, &value));
- #ifndef CYGWIN
- return std::stoi(value);
- #else
- return std::strtol(value.c_str(), 0 /* off */, 10 /* base */);
- #endif
- }
- bool IsDbWriteStopped() {
- uint64_t v;
- EXPECT_TRUE(dbfull()->GetIntProperty("rocksdb.is-write-stopped", &v));
- return (v == 1);
- }
- uint64_t GetDbDelayedWriteRate() {
- uint64_t v;
- EXPECT_TRUE(
- dbfull()->GetIntProperty("rocksdb.actual-delayed-write-rate", &v));
- return v;
- }
- void Destroy(const std::vector<ColumnFamilyDescriptor>& column_families =
- std::vector<ColumnFamilyDescriptor>()) {
- Close();
- ASSERT_OK(DestroyDB(dbname_, Options(db_options_, column_family_options_),
- column_families));
- }
- void CreateColumnFamilies(
- const std::vector<std::string>& cfs,
- const std::vector<ColumnFamilyOptions> options = {}) {
- int cfi = static_cast<int>(handles_.size());
- handles_.resize(cfi + cfs.size());
- names_.resize(cfi + cfs.size());
- for (size_t i = 0; i < cfs.size(); ++i) {
- const auto& current_cf_opt =
- options.size() == 0 ? column_family_options_ : options[i];
- ASSERT_OK(
- db_->CreateColumnFamily(current_cf_opt, cfs[i], &handles_[cfi]));
- names_[cfi] = cfs[i];
- // Verify the CF options of the returned CF handle.
- ColumnFamilyDescriptor desc;
- ASSERT_OK(handles_[cfi]->GetDescriptor(&desc));
- // Need to sanitize the default column family options before comparing
- // them.
- ASSERT_OK(RocksDBOptionsParser::VerifyCFOptions(
- ConfigOptions(), desc.options,
- SanitizeCfOptions(dbfull()->immutable_db_options(),
- /*read_only*/ false, current_cf_opt)));
- cfi++;
- }
- }
- void Reopen(const std::vector<ColumnFamilyOptions> options = {}) {
- std::vector<std::string> names;
- for (const auto& name : names_) {
- if (name != "") {
- names.push_back(name);
- }
- }
- Close();
- assert(options.size() == 0 || names.size() == options.size());
- Open(names, options);
- }
- void CreateColumnFamiliesAndReopen(const std::vector<std::string>& cfs) {
- CreateColumnFamilies(cfs);
- Reopen();
- }
- void DropColumnFamilies(const std::vector<int>& cfs) {
- for (auto cf : cfs) {
- ASSERT_OK(db_->DropColumnFamily(handles_[cf]));
- ASSERT_OK(db_->DestroyColumnFamilyHandle(handles_[cf]));
- handles_[cf] = nullptr;
- names_[cf] = "";
- }
- }
- void PutRandomData(int cf, int num, int key_value_size, bool save = false) {
- if (cf >= static_cast<int>(keys_.size())) {
- keys_.resize(cf + 1);
- }
- for (int i = 0; i < num; ++i) {
- // 10 bytes for key, rest is value
- if (!save) {
- ASSERT_OK(Put(cf, test::RandomKey(&rnd_, 11),
- rnd_.RandomString(key_value_size - 10)));
- } else {
- std::string key = test::RandomKey(&rnd_, 11);
- keys_[cf].insert(key);
- ASSERT_OK(Put(cf, key, rnd_.RandomString(key_value_size - 10)));
- }
- }
- ASSERT_OK(db_->FlushWAL(/*sync=*/false));
- }
- void WaitForFlush(int cf) {
- ASSERT_OK(dbfull()->TEST_WaitForFlushMemTable(handles_[cf]));
- }
- void WaitForCompaction() { ASSERT_OK(dbfull()->TEST_WaitForCompact()); }
- uint64_t MaxTotalInMemoryState() {
- return dbfull()->TEST_MaxTotalInMemoryState();
- }
- void AssertMaxTotalInMemoryState(uint64_t value) {
- ASSERT_EQ(value, MaxTotalInMemoryState());
- }
- Status Put(int cf, const std::string& key, const std::string& value) {
- return db_->Put(WriteOptions(), handles_[cf], Slice(key), Slice(value));
- }
- Status Merge(int cf, const std::string& key, const std::string& value) {
- return db_->Merge(WriteOptions(), handles_[cf], Slice(key), Slice(value));
- }
- Status Flush(int cf) { return db_->Flush(FlushOptions(), handles_[cf]); }
- std::string Get(int cf, const std::string& key) {
- ReadOptions options;
- options.verify_checksums = true;
- std::string result;
- Status s = db_->Get(options, handles_[cf], Slice(key), &result);
- if (s.IsNotFound()) {
- result = "NOT_FOUND";
- } else if (!s.ok()) {
- result = s.ToString();
- }
- return result;
- }
- void CompactAll(int cf) {
- ASSERT_OK(db_->CompactRange(CompactRangeOptions(), handles_[cf], nullptr,
- nullptr));
- }
- void Compact(int cf, const Slice& start, const Slice& limit) {
- ASSERT_OK(
- db_->CompactRange(CompactRangeOptions(), handles_[cf], &start, &limit));
- }
- int NumTableFilesAtLevel(int level, int cf) {
- return GetProperty(cf,
- "rocksdb.num-files-at-level" + std::to_string(level));
- }
- // Return spread of files per level
- std::string FilesPerLevel(int cf) {
- std::string result;
- int last_non_zero_offset = 0;
- for (int level = 0; level < dbfull()->NumberLevels(handles_[cf]); level++) {
- int f = NumTableFilesAtLevel(level, cf);
- char buf[100];
- snprintf(buf, sizeof(buf), "%s%d", (level ? "," : ""), f);
- result += buf;
- if (f > 0) {
- last_non_zero_offset = static_cast<int>(result.size());
- }
- }
- result.resize(last_non_zero_offset);
- return result;
- }
- void AssertFilesPerLevel(const std::string& value, int cf) {
- ASSERT_EQ(value, FilesPerLevel(cf));
- }
- int CountLiveFiles() {
- std::vector<LiveFileMetaData> metadata;
- db_->GetLiveFilesMetaData(&metadata);
- return static_cast<int>(metadata.size());
- }
- void AssertCountLiveFiles(int expected_value) {
- ASSERT_EQ(expected_value, CountLiveFiles());
- }
- // Do n memtable flushes, each of which produces an sstable
- // covering the range [small,large].
- void MakeTables(int cf, int n, const std::string& small,
- const std::string& large) {
- for (int i = 0; i < n; i++) {
- ASSERT_OK(Put(cf, small, "begin"));
- ASSERT_OK(Put(cf, large, "end"));
- ASSERT_OK(db_->Flush(FlushOptions(), handles_[cf]));
- }
- }
- int CountLiveLogFiles() {
- int micros_wait_for_log_deletion = 20000;
- env_->SleepForMicroseconds(micros_wait_for_log_deletion);
- int ret = 0;
- VectorLogPtr wal_files;
- Status s;
- // GetSortedWalFiles is a flakey function -- it gets all the wal_dir
- // children files and then later checks for their existence. if some of the
- // log files doesn't exist anymore, it reports an error. it does all of this
- // without DB mutex held, so if a background process deletes the log file
- // while the function is being executed, it returns an error. We retry the
- // function 10 times to avoid the error failing the test
- for (int retries = 0; retries < 10; ++retries) {
- wal_files.clear();
- s = db_->GetSortedWalFiles(wal_files);
- if (s.ok()) {
- break;
- }
- }
- EXPECT_OK(s);
- for (const auto& wal : wal_files) {
- if (wal->Type() == kAliveLogFile) {
- ++ret;
- }
- }
- return ret;
- return 0;
- }
- void AssertCountLiveLogFiles(int value) {
- ASSERT_EQ(value, CountLiveLogFiles());
- }
- void AssertNumberOfImmutableMemtables(std::vector<int> num_per_cf) {
- assert(num_per_cf.size() == handles_.size());
- for (size_t i = 0; i < num_per_cf.size(); ++i) {
- ASSERT_EQ(num_per_cf[i], GetProperty(static_cast<int>(i),
- "rocksdb.num-immutable-mem-table"));
- }
- }
- void CopyFile(const std::string& source, const std::string& destination,
- uint64_t size = 0) {
- const EnvOptions soptions;
- std::unique_ptr<SequentialFile> srcfile;
- ASSERT_OK(env_->NewSequentialFile(source, &srcfile, soptions));
- std::unique_ptr<WritableFile> destfile;
- ASSERT_OK(env_->NewWritableFile(destination, &destfile, soptions));
- if (size == 0) {
- // default argument means copy everything
- ASSERT_OK(env_->GetFileSize(source, &size));
- }
- char buffer[4096];
- Slice slice;
- while (size > 0) {
- uint64_t one = std::min(uint64_t(sizeof(buffer)), size);
- ASSERT_OK(srcfile->Read(one, &slice, buffer));
- ASSERT_OK(destfile->Append(slice));
- size -= slice.size();
- }
- ASSERT_OK(destfile->Close());
- }
- int GetSstFileCount(std::string path) {
- std::vector<std::string> files;
- DBTestBase::GetSstFiles(env_, path, &files);
- return static_cast<int>(files.size());
- }
- void RecalculateWriteStallConditions(
- ColumnFamilyData* cfd, const MutableCFOptions& mutable_cf_options) {
- // add lock to avoid race condition between
- // `RecalculateWriteStallConditions` which writes to CFStats and
- // background `DBImpl::DumpStats()` threads which read CFStats
- dbfull()->TEST_LockMutex();
- cfd->RecalculateWriteStallConditions(mutable_cf_options);
- dbfull()->TEST_UnlockMutex();
- }
- std::vector<ColumnFamilyHandle*> handles_;
- std::vector<std::string> names_;
- std::vector<std::set<std::string>> keys_;
- ColumnFamilyOptions column_family_options_;
- DBOptions db_options_;
- std::string dbname_;
- DB* db_ = nullptr;
- EnvCounter* env_;
- std::shared_ptr<Env> env_guard_;
- Random rnd_;
- uint32_t format_;
- };
- class ColumnFamilyTest
- : public ColumnFamilyTestBase,
- virtual public ::testing::WithParamInterface<uint32_t> {
- public:
- ColumnFamilyTest() : ColumnFamilyTestBase(GetParam()) {}
- };
- INSTANTIATE_TEST_CASE_P(FormatDef, ColumnFamilyTest,
- testing::Values(test::kDefaultFormatVersion));
- INSTANTIATE_TEST_CASE_P(FormatLatest, ColumnFamilyTest,
- testing::Values(kLatestFormatVersion));
- TEST_P(ColumnFamilyTest, DontReuseColumnFamilyID) {
- for (int iter = 0; iter < 3; ++iter) {
- Open();
- CreateColumnFamilies({"one", "two", "three"});
- for (size_t i = 0; i < handles_.size(); ++i) {
- auto cfh = static_cast_with_check<ColumnFamilyHandleImpl>(handles_[i]);
- ASSERT_EQ(i, cfh->GetID());
- }
- if (iter == 1) {
- Reopen();
- }
- DropColumnFamilies({3});
- Reopen();
- if (iter == 2) {
- // this tests if max_column_family is correctly persisted with
- // WriteSnapshot()
- Reopen();
- }
- CreateColumnFamilies({"three2"});
- // ID 3 that was used for dropped column family "three" should not be
- // reused
- auto cfh3 = static_cast_with_check<ColumnFamilyHandleImpl>(handles_[3]);
- ASSERT_EQ(4U, cfh3->GetID());
- Close();
- Destroy();
- }
- }
- TEST_P(ColumnFamilyTest, CreateCFRaceWithGetAggProperty) {
- Open();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
- {{"DBImpl::WriteOptionsFile:1",
- "ColumnFamilyTest.CreateCFRaceWithGetAggProperty:1"},
- {"ColumnFamilyTest.CreateCFRaceWithGetAggProperty:2",
- "DBImpl::WriteOptionsFile:2"}});
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- ROCKSDB_NAMESPACE::port::Thread thread(
- [&] { CreateColumnFamilies({"one"}); });
- TEST_SYNC_POINT("ColumnFamilyTest.CreateCFRaceWithGetAggProperty:1");
- uint64_t pv;
- db_->GetAggregatedIntProperty(DB::Properties::kEstimateTableReadersMem, &pv);
- TEST_SYNC_POINT("ColumnFamilyTest.CreateCFRaceWithGetAggProperty:2");
- thread.join();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- }
- class FlushEmptyCFTestWithParam
- : public ColumnFamilyTestBase,
- virtual public testing::WithParamInterface<std::tuple<uint32_t, bool>> {
- public:
- FlushEmptyCFTestWithParam()
- : ColumnFamilyTestBase(std::get<0>(GetParam())),
- allow_2pc_(std::get<1>(GetParam())) {}
- // Required if inheriting from testing::WithParamInterface<>
- static void SetUpTestCase() {}
- static void TearDownTestCase() {}
- bool allow_2pc_;
- };
- TEST_P(FlushEmptyCFTestWithParam, FlushEmptyCFTest) {
- std::unique_ptr<FaultInjectionTestEnv> fault_env(
- new FaultInjectionTestEnv(env_));
- db_options_.env = fault_env.get();
- db_options_.allow_2pc = allow_2pc_;
- Open();
- CreateColumnFamilies({"one", "two"});
- // Generate log file A.
- ASSERT_OK(Put(1, "foo", "v1")); // seqID 1
- Reopen();
- // Log file A is not dropped after reopening because default column family's
- // min log number is 0.
- // It flushes to SST file X
- ASSERT_OK(Put(1, "foo", "v1")); // seqID 2
- ASSERT_OK(Put(1, "bar", "v2")); // seqID 3
- // Current log file is file B now. While flushing, a new log file C is created
- // and is set to current. Boths' min log number is set to file C in memory, so
- // after flushing file B is deleted. At the same time, the min log number of
- // default CF is not written to manifest. Log file A still remains.
- // Flushed to SST file Y.
- ASSERT_OK(Flush(1));
- ASSERT_OK(Flush(0));
- ASSERT_OK(Put(1, "bar", "v3")); // seqID 4
- ASSERT_OK(Put(1, "foo", "v4")); // seqID 5
- ASSERT_OK(db_->FlushWAL(/*sync=*/false));
- // Preserve file system state up to here to simulate a crash condition.
- fault_env->SetFilesystemActive(false);
- std::vector<std::string> names;
- for (const auto& name : names_) {
- if (name != "") {
- names.push_back(name);
- }
- }
- Close();
- fault_env->ResetState();
- // Before opening, there are four files:
- // Log file A contains seqID 1
- // Log file C contains seqID 4, 5
- // SST file X contains seqID 1
- // SST file Y contains seqID 2, 3
- // Min log number:
- // default CF: 0
- // CF one, two: C
- // When opening the DB, all the seqID should be preserved.
- Open(names, {});
- ASSERT_EQ("v4", Get(1, "foo"));
- ASSERT_EQ("v3", Get(1, "bar"));
- Close();
- db_options_.env = env_;
- }
- TEST_P(FlushEmptyCFTestWithParam, FlushEmptyCFTest2) {
- std::unique_ptr<FaultInjectionTestEnv> fault_env(
- new FaultInjectionTestEnv(env_));
- db_options_.env = fault_env.get();
- db_options_.allow_2pc = allow_2pc_;
- Open();
- CreateColumnFamilies({"one", "two"});
- // Generate log file A.
- ASSERT_OK(Put(1, "foo", "v1")); // seqID 1
- Reopen();
- // Log file A is not dropped after reopening because default column family's
- // min log number is 0.
- // It flushes to SST file X
- ASSERT_OK(Put(1, "foo", "v1")); // seqID 2
- ASSERT_OK(Put(1, "bar", "v2")); // seqID 3
- // Current log file is file B now. While flushing, a new log file C is created
- // and is set to current. Both CFs' min log number is set to file C so after
- // flushing file B is deleted. Log file A still remains.
- // Flushed to SST file Y.
- ASSERT_OK(Flush(1));
- ASSERT_OK(Put(0, "bar", "v2")); // seqID 4
- ASSERT_OK(Put(2, "bar", "v2")); // seqID 5
- ASSERT_OK(Put(1, "bar", "v3")); // seqID 6
- // Flushing all column families. This forces all CFs' min log to current. This
- // is written to the manifest file. Log file C is cleared.
- ASSERT_OK(Flush(0));
- ASSERT_OK(Flush(1));
- ASSERT_OK(Flush(2));
- // Write to log file D
- ASSERT_OK(Put(1, "bar", "v4")); // seqID 7
- ASSERT_OK(Put(1, "bar", "v5")); // seqID 8
- ASSERT_OK(db_->FlushWAL(/*sync=*/false));
- // Preserve file system state up to here to simulate a crash condition.
- fault_env->SetFilesystemActive(false);
- std::vector<std::string> names;
- for (const auto& name : names_) {
- if (name != "") {
- names.push_back(name);
- }
- }
- Close();
- fault_env->ResetState();
- // Before opening, there are two logfiles:
- // Log file A contains seqID 1
- // Log file D contains seqID 7, 8
- // Min log number:
- // default CF: D
- // CF one, two: D
- // When opening the DB, log file D should be replayed using the seqID
- // specified in the file.
- Open(names, {});
- ASSERT_EQ("v1", Get(1, "foo"));
- ASSERT_EQ("v5", Get(1, "bar"));
- Close();
- db_options_.env = env_;
- }
- INSTANTIATE_TEST_CASE_P(
- FormatDef, FlushEmptyCFTestWithParam,
- testing::Values(std::make_tuple(test::kDefaultFormatVersion, true),
- std::make_tuple(test::kDefaultFormatVersion, false)));
- INSTANTIATE_TEST_CASE_P(
- FormatLatest, FlushEmptyCFTestWithParam,
- testing::Values(std::make_tuple(kLatestFormatVersion, true),
- std::make_tuple(kLatestFormatVersion, false)));
- TEST_P(ColumnFamilyTest, AddDrop) {
- Open();
- CreateColumnFamilies({"one", "two", "three"});
- ASSERT_EQ("NOT_FOUND", Get(1, "fodor"));
- ASSERT_EQ("NOT_FOUND", Get(2, "fodor"));
- DropColumnFamilies({2});
- ASSERT_EQ("NOT_FOUND", Get(1, "fodor"));
- CreateColumnFamilies({"four"});
- ASSERT_EQ("NOT_FOUND", Get(3, "fodor"));
- ASSERT_OK(Put(1, "fodor", "mirko"));
- ASSERT_EQ("mirko", Get(1, "fodor"));
- ASSERT_EQ("NOT_FOUND", Get(3, "fodor"));
- Close();
- ASSERT_TRUE(TryOpen({"default"}).IsInvalidArgument());
- Open({"default", "one", "three", "four"});
- DropColumnFamilies({1});
- Reopen();
- Close();
- std::vector<std::string> families;
- ASSERT_OK(DB::ListColumnFamilies(db_options_, dbname_, &families));
- std::sort(families.begin(), families.end());
- ASSERT_TRUE(families ==
- std::vector<std::string>({"default", "four", "three"}));
- }
- TEST_P(ColumnFamilyTest, BulkAddDrop) {
- constexpr int kNumCF = 1000;
- ColumnFamilyOptions cf_options;
- WriteOptions write_options;
- Open();
- std::vector<std::string> cf_names;
- std::vector<ColumnFamilyHandle*> cf_handles;
- for (int i = 1; i <= kNumCF; i++) {
- cf_names.push_back("cf1-" + std::to_string(i));
- }
- ASSERT_OK(db_->CreateColumnFamilies(cf_options, cf_names, &cf_handles));
- for (int i = 1; i <= kNumCF; i++) {
- ASSERT_OK(db_->Put(write_options, cf_handles[i - 1], "foo", "bar"));
- }
- ASSERT_OK(db_->DropColumnFamilies(cf_handles));
- std::vector<ColumnFamilyDescriptor> cf_descriptors;
- for (auto* handle : cf_handles) {
- delete handle;
- }
- cf_handles.clear();
- for (int i = 1; i <= kNumCF; i++) {
- cf_descriptors.emplace_back("cf2-" + std::to_string(i),
- ColumnFamilyOptions());
- }
- ASSERT_OK(db_->CreateColumnFamilies(cf_descriptors, &cf_handles));
- for (int i = 1; i <= kNumCF; i++) {
- ASSERT_OK(db_->Put(write_options, cf_handles[i - 1], "foo", "bar"));
- }
- ASSERT_OK(db_->DropColumnFamilies(cf_handles));
- for (auto* handle : cf_handles) {
- delete handle;
- }
- Close();
- std::vector<std::string> families;
- ASSERT_OK(DB::ListColumnFamilies(db_options_, dbname_, &families));
- std::sort(families.begin(), families.end());
- ASSERT_TRUE(families == std::vector<std::string>({"default"}));
- }
- TEST_P(ColumnFamilyTest, DropTest) {
- // first iteration - don't reopen DB before dropping
- // second iteration - reopen DB before dropping
- for (int iter = 0; iter < 2; ++iter) {
- Open({"default"});
- CreateColumnFamiliesAndReopen({"pikachu"});
- for (int i = 0; i < 100; ++i) {
- ASSERT_OK(Put(1, std::to_string(i), "bar" + std::to_string(i)));
- }
- ASSERT_OK(Flush(1));
- if (iter == 1) {
- Reopen();
- }
- ASSERT_EQ("bar1", Get(1, "1"));
- AssertCountLiveFiles(1);
- DropColumnFamilies({1});
- // make sure that all files are deleted when we drop the column family
- AssertCountLiveFiles(0);
- Destroy();
- }
- }
- TEST_P(ColumnFamilyTest, WriteBatchFailure) {
- Open();
- CreateColumnFamiliesAndReopen({"one", "two"});
- WriteBatch batch;
- ASSERT_OK(batch.Put(handles_[0], Slice("existing"), Slice("column-family")));
- ASSERT_OK(
- batch.Put(handles_[1], Slice("non-existing"), Slice("column-family")));
- ASSERT_OK(db_->Write(WriteOptions(), &batch));
- DropColumnFamilies({1});
- WriteOptions woptions_ignore_missing_cf;
- woptions_ignore_missing_cf.ignore_missing_column_families = true;
- ASSERT_OK(
- batch.Put(handles_[0], Slice("still here"), Slice("column-family")));
- ASSERT_OK(db_->Write(woptions_ignore_missing_cf, &batch));
- ASSERT_EQ("column-family", Get(0, "still here"));
- Status s = db_->Write(WriteOptions(), &batch);
- ASSERT_TRUE(s.IsInvalidArgument());
- Close();
- }
- TEST_P(ColumnFamilyTest, ReadWrite) {
- Open();
- CreateColumnFamiliesAndReopen({"one", "two"});
- ASSERT_OK(Put(0, "foo", "v1"));
- ASSERT_OK(Put(0, "bar", "v2"));
- ASSERT_OK(Put(1, "mirko", "v3"));
- ASSERT_OK(Put(0, "foo", "v2"));
- ASSERT_OK(Put(2, "fodor", "v5"));
- for (int iter = 0; iter <= 3; ++iter) {
- ASSERT_EQ("v2", Get(0, "foo"));
- ASSERT_EQ("v2", Get(0, "bar"));
- ASSERT_EQ("v3", Get(1, "mirko"));
- ASSERT_EQ("v5", Get(2, "fodor"));
- ASSERT_EQ("NOT_FOUND", Get(0, "fodor"));
- ASSERT_EQ("NOT_FOUND", Get(1, "fodor"));
- ASSERT_EQ("NOT_FOUND", Get(2, "foo"));
- if (iter <= 1) {
- Reopen();
- }
- }
- Close();
- }
- TEST_P(ColumnFamilyTest, IgnoreRecoveredLog) {
- std::string backup_logs = dbname_ + "/backup_logs";
- // delete old files in backup_logs directory
- ASSERT_OK(env_->CreateDirIfMissing(dbname_));
- ASSERT_OK(env_->CreateDirIfMissing(backup_logs));
- std::vector<std::string> old_files;
- ASSERT_OK(env_->GetChildren(backup_logs, &old_files));
- for (auto& file : old_files) {
- ASSERT_OK(env_->DeleteFile(backup_logs + "/" + file));
- }
- column_family_options_.merge_operator =
- MergeOperators::CreateUInt64AddOperator();
- db_options_.wal_dir = dbname_ + "/logs";
- Destroy();
- Open();
- CreateColumnFamilies({"cf1", "cf2"});
- // fill up the DB
- std::string one, two, three;
- PutFixed64(&one, 1);
- PutFixed64(&two, 2);
- PutFixed64(&three, 3);
- ASSERT_OK(Merge(0, "foo", one));
- ASSERT_OK(Merge(1, "mirko", one));
- ASSERT_OK(Merge(0, "foo", one));
- ASSERT_OK(Merge(2, "bla", one));
- ASSERT_OK(Merge(2, "fodor", one));
- ASSERT_OK(Merge(0, "bar", one));
- ASSERT_OK(Merge(2, "bla", one));
- ASSERT_OK(Merge(1, "mirko", two));
- ASSERT_OK(Merge(1, "franjo", one));
- // copy the logs to backup
- std::vector<std::string> logs;
- ASSERT_OK(env_->GetChildren(db_options_.wal_dir, &logs));
- for (auto& log : logs) {
- CopyFile(db_options_.wal_dir + "/" + log, backup_logs + "/" + log);
- }
- // recover the DB
- Close();
- // 1. check consistency
- // 2. copy the logs from backup back to WAL dir. if the recovery happens
- // again on the same log files, this should lead to incorrect results
- // due to applying merge operator twice
- // 3. check consistency
- for (int iter = 0; iter < 2; ++iter) {
- // assert consistency
- Open({"default", "cf1", "cf2"});
- ASSERT_EQ(two, Get(0, "foo"));
- ASSERT_EQ(one, Get(0, "bar"));
- ASSERT_EQ(three, Get(1, "mirko"));
- ASSERT_EQ(one, Get(1, "franjo"));
- ASSERT_EQ(one, Get(2, "fodor"));
- ASSERT_EQ(two, Get(2, "bla"));
- Close();
- if (iter == 0) {
- // copy the logs from backup back to wal dir
- for (auto& log : logs) {
- CopyFile(backup_logs + "/" + log, db_options_.wal_dir + "/" + log);
- }
- }
- }
- }
- TEST_P(ColumnFamilyTest, FlushTest) {
- Open();
- CreateColumnFamiliesAndReopen({"one", "two"});
- ASSERT_OK(Put(0, "foo", "v1"));
- ASSERT_OK(Put(0, "bar", "v2"));
- ASSERT_OK(Put(1, "mirko", "v3"));
- ASSERT_OK(Put(0, "foo", "v2"));
- ASSERT_OK(Put(2, "fodor", "v5"));
- for (int j = 0; j < 2; j++) {
- ReadOptions ro;
- std::vector<Iterator*> iterators;
- // Hold super version.
- if (j == 0) {
- ASSERT_OK(db_->NewIterators(ro, handles_, &iterators));
- }
- for (int i = 0; i < 3; ++i) {
- uint64_t max_total_in_memory_state = MaxTotalInMemoryState();
- ASSERT_OK(Flush(i));
- AssertMaxTotalInMemoryState(max_total_in_memory_state);
- }
- ASSERT_OK(Put(1, "foofoo", "bar"));
- ASSERT_OK(Put(0, "foofoo", "bar"));
- for (auto* it : iterators) {
- ASSERT_OK(it->status());
- delete it;
- }
- }
- Reopen();
- for (int iter = 0; iter <= 2; ++iter) {
- ASSERT_EQ("v2", Get(0, "foo"));
- ASSERT_EQ("v2", Get(0, "bar"));
- ASSERT_EQ("v3", Get(1, "mirko"));
- ASSERT_EQ("v5", Get(2, "fodor"));
- ASSERT_EQ("NOT_FOUND", Get(0, "fodor"));
- ASSERT_EQ("NOT_FOUND", Get(1, "fodor"));
- ASSERT_EQ("NOT_FOUND", Get(2, "foo"));
- if (iter <= 1) {
- Reopen();
- }
- }
- Close();
- }
- // Makes sure that obsolete log files get deleted
- TEST_P(ColumnFamilyTest, LogDeletionTest) {
- db_options_.max_total_wal_size = std::numeric_limits<uint64_t>::max();
- column_family_options_.arena_block_size = 4 * 1024;
- column_family_options_.write_buffer_size = 128000; // 128KB
- Open();
- CreateColumnFamilies({"one", "two", "three", "four"});
- // Each bracket is one log file. if number is in (), it means
- // we don't need it anymore (it's been flushed)
- // []
- AssertCountLiveLogFiles(0);
- PutRandomData(0, 1, 128);
- // [0]
- PutRandomData(1, 1, 128);
- // [0, 1]
- PutRandomData(1, 1000, 128);
- WaitForFlush(1);
- // [0, (1)] [1]
- AssertCountLiveLogFiles(2);
- PutRandomData(0, 1, 128);
- // [0, (1)] [0, 1]
- AssertCountLiveLogFiles(2);
- PutRandomData(2, 1, 128);
- // [0, (1)] [0, 1, 2]
- PutRandomData(2, 1000, 128);
- WaitForFlush(2);
- // [0, (1)] [0, 1, (2)] [2]
- AssertCountLiveLogFiles(3);
- PutRandomData(2, 1000, 128);
- WaitForFlush(2);
- // [0, (1)] [0, 1, (2)] [(2)] [2]
- AssertCountLiveLogFiles(4);
- PutRandomData(3, 1, 128);
- // [0, (1)] [0, 1, (2)] [(2)] [2, 3]
- PutRandomData(1, 1, 128);
- // [0, (1)] [0, 1, (2)] [(2)] [1, 2, 3]
- AssertCountLiveLogFiles(4);
- PutRandomData(1, 1000, 128);
- WaitForFlush(1);
- // [0, (1)] [0, (1), (2)] [(2)] [(1), 2, 3] [1]
- AssertCountLiveLogFiles(5);
- PutRandomData(0, 1000, 128);
- WaitForFlush(0);
- // [(0), (1)] [(0), (1), (2)] [(2)] [(1), 2, 3] [1, (0)] [0]
- // delete obsolete logs -->
- // [(1), 2, 3] [1, (0)] [0]
- AssertCountLiveLogFiles(3);
- PutRandomData(0, 1000, 128);
- WaitForFlush(0);
- // [(1), 2, 3] [1, (0)], [(0)] [0]
- AssertCountLiveLogFiles(4);
- PutRandomData(1, 1000, 128);
- WaitForFlush(1);
- // [(1), 2, 3] [(1), (0)] [(0)] [0, (1)] [1]
- AssertCountLiveLogFiles(5);
- PutRandomData(2, 1000, 128);
- WaitForFlush(2);
- // [(1), (2), 3] [(1), (0)] [(0)] [0, (1)] [1, (2)], [2]
- AssertCountLiveLogFiles(6);
- PutRandomData(3, 1000, 128);
- WaitForFlush(3);
- // [(1), (2), (3)] [(1), (0)] [(0)] [0, (1)] [1, (2)], [2, (3)] [3]
- // delete obsolete logs -->
- // [0, (1)] [1, (2)], [2, (3)] [3]
- AssertCountLiveLogFiles(4);
- Close();
- }
- TEST_P(ColumnFamilyTest, CrashAfterFlush) {
- std::unique_ptr<FaultInjectionTestEnv> fault_env(
- new FaultInjectionTestEnv(env_));
- db_options_.env = fault_env.get();
- Open();
- CreateColumnFamilies({"one"});
- WriteBatch batch;
- ASSERT_OK(batch.Put(handles_[0], Slice("foo"), Slice("bar")));
- ASSERT_OK(batch.Put(handles_[1], Slice("foo"), Slice("bar")));
- ASSERT_OK(db_->Write(WriteOptions(), &batch));
- ASSERT_OK(Flush(0));
- fault_env->SetFilesystemActive(false);
- std::vector<std::string> names;
- for (const auto& name : names_) {
- if (name != "") {
- names.push_back(name);
- }
- }
- Close();
- ASSERT_OK(fault_env->DropUnsyncedFileData());
- fault_env->ResetState();
- Open(names, {});
- // Write batch should be atomic.
- ASSERT_EQ(Get(0, "foo"), Get(1, "foo"));
- Close();
- db_options_.env = env_;
- }
- TEST_P(ColumnFamilyTest, OpenNonexistentColumnFamily) {
- ASSERT_OK(TryOpen({"default"}));
- Close();
- ASSERT_TRUE(TryOpen({"default", "dne"}).IsInvalidArgument());
- }
- // Makes sure that obsolete log files get deleted
- TEST_P(ColumnFamilyTest, DifferentWriteBufferSizes) {
- // disable flushing stale column families
- db_options_.max_total_wal_size = std::numeric_limits<uint64_t>::max();
- Open();
- CreateColumnFamilies({"one", "two", "three"});
- ColumnFamilyOptions default_cf, one, two, three;
- // setup options. all column families have max_write_buffer_number setup to 10
- // "default" -> 100KB memtable, start flushing immediately
- // "one" -> 200KB memtable, start flushing with two immutable memtables
- // "two" -> 1MB memtable, start flushing with three immutable memtables
- // "three" -> 90KB memtable, start flushing with four immutable memtables
- default_cf.write_buffer_size = 100000;
- default_cf.arena_block_size = 4 * 4096;
- default_cf.max_write_buffer_number = 10;
- default_cf.min_write_buffer_number_to_merge = 1;
- default_cf.max_write_buffer_size_to_maintain = 0;
- one.write_buffer_size = 200000;
- one.arena_block_size = 4 * 4096;
- one.max_write_buffer_number = 10;
- one.min_write_buffer_number_to_merge = 2;
- one.max_write_buffer_size_to_maintain =
- static_cast<int>(one.write_buffer_size);
- two.write_buffer_size = 1000000;
- two.arena_block_size = 4 * 4096;
- two.max_write_buffer_number = 10;
- two.min_write_buffer_number_to_merge = 3;
- two.max_write_buffer_size_to_maintain =
- static_cast<int>(two.write_buffer_size);
- three.write_buffer_size = 4096 * 22;
- three.arena_block_size = 4096;
- three.max_write_buffer_number = 10;
- three.min_write_buffer_number_to_merge = 4;
- three.max_write_buffer_size_to_maintain =
- static_cast<int>(three.write_buffer_size);
- Reopen({default_cf, one, two, three});
- int micros_wait_for_flush = 10000;
- PutRandomData(0, 100, 1000);
- WaitForFlush(0);
- AssertNumberOfImmutableMemtables({0, 0, 0, 0});
- AssertCountLiveLogFiles(1);
- PutRandomData(1, 200, 1000);
- env_->SleepForMicroseconds(micros_wait_for_flush);
- AssertNumberOfImmutableMemtables({0, 1, 0, 0});
- AssertCountLiveLogFiles(2);
- PutRandomData(2, 1000, 1000);
- env_->SleepForMicroseconds(micros_wait_for_flush);
- AssertNumberOfImmutableMemtables({0, 1, 1, 0});
- AssertCountLiveLogFiles(3);
- PutRandomData(2, 1000, 1000);
- env_->SleepForMicroseconds(micros_wait_for_flush);
- AssertNumberOfImmutableMemtables({0, 1, 2, 0});
- AssertCountLiveLogFiles(4);
- PutRandomData(3, 93, 990);
- env_->SleepForMicroseconds(micros_wait_for_flush);
- AssertNumberOfImmutableMemtables({0, 1, 2, 1});
- AssertCountLiveLogFiles(5);
- PutRandomData(3, 88, 990);
- env_->SleepForMicroseconds(micros_wait_for_flush);
- AssertNumberOfImmutableMemtables({0, 1, 2, 2});
- AssertCountLiveLogFiles(6);
- PutRandomData(3, 88, 990);
- env_->SleepForMicroseconds(micros_wait_for_flush);
- AssertNumberOfImmutableMemtables({0, 1, 2, 3});
- AssertCountLiveLogFiles(7);
- PutRandomData(0, 100, 1000);
- WaitForFlush(0);
- AssertNumberOfImmutableMemtables({0, 1, 2, 3});
- AssertCountLiveLogFiles(8);
- PutRandomData(2, 100, 10000);
- WaitForFlush(2);
- AssertNumberOfImmutableMemtables({0, 1, 0, 3});
- AssertCountLiveLogFiles(9);
- PutRandomData(3, 88, 990);
- WaitForFlush(3);
- AssertNumberOfImmutableMemtables({0, 1, 0, 0});
- AssertCountLiveLogFiles(10);
- PutRandomData(3, 88, 990);
- env_->SleepForMicroseconds(micros_wait_for_flush);
- AssertNumberOfImmutableMemtables({0, 1, 0, 1});
- AssertCountLiveLogFiles(11);
- PutRandomData(1, 200, 1000);
- WaitForFlush(1);
- AssertNumberOfImmutableMemtables({0, 0, 0, 1});
- AssertCountLiveLogFiles(5);
- PutRandomData(3, 88 * 3, 990);
- WaitForFlush(3);
- PutRandomData(3, 88 * 4, 990);
- WaitForFlush(3);
- AssertNumberOfImmutableMemtables({0, 0, 0, 0});
- AssertCountLiveLogFiles(12);
- PutRandomData(0, 100, 1000);
- WaitForFlush(0);
- AssertNumberOfImmutableMemtables({0, 0, 0, 0});
- AssertCountLiveLogFiles(12);
- PutRandomData(2, 3 * 1000, 1000);
- WaitForFlush(2);
- AssertNumberOfImmutableMemtables({0, 0, 0, 0});
- AssertCountLiveLogFiles(12);
- PutRandomData(1, 2 * 200, 1000);
- WaitForFlush(1);
- AssertNumberOfImmutableMemtables({0, 0, 0, 0});
- AssertCountLiveLogFiles(7);
- Close();
- }
- // The test is commented out because we want to test that snapshot is
- // not created for memtables not supported it, but There isn't a memtable
- // that doesn't support snapshot right now. If we have one later, we can
- // re-enable the test.
- //
- // TEST_P(ColumnFamilyTest, MemtableNotSupportSnapshot) {
- // db_options_.allow_concurrent_memtable_write = false;
- // Open();
- // auto* s1 = dbfull()->GetSnapshot();
- // ASSERT_TRUE(s1 != nullptr);
- // dbfull()->ReleaseSnapshot(s1);
- // // Add a column family that doesn't support snapshot
- // ColumnFamilyOptions first;
- // first.memtable_factory.reset(new DummyMemtableNotSupportingSnapshot());
- // CreateColumnFamilies({"first"}, {first});
- // auto* s2 = dbfull()->GetSnapshot();
- // ASSERT_TRUE(s2 == nullptr);
- // // Add a column family that supports snapshot. Snapshot stays not
- // supported. ColumnFamilyOptions second; CreateColumnFamilies({"second"},
- // {second}); auto* s3 = dbfull()->GetSnapshot(); ASSERT_TRUE(s3 == nullptr);
- // Close();
- // }
- class TestComparator : public Comparator {
- int Compare(const ROCKSDB_NAMESPACE::Slice& /*a*/,
- const ROCKSDB_NAMESPACE::Slice& /*b*/) const override {
- return 0;
- }
- const char* Name() const override { return "Test"; }
- void FindShortestSeparator(
- std::string* /*start*/,
- const ROCKSDB_NAMESPACE::Slice& /*limit*/) const override {}
- void FindShortSuccessor(std::string* /*key*/) const override {}
- };
- static TestComparator third_comparator;
- static TestComparator fourth_comparator;
- // Test that we can retrieve the comparator from a created CF
- TEST_P(ColumnFamilyTest, GetComparator) {
- Open();
- // Add a column family with no comparator specified
- CreateColumnFamilies({"first"});
- const Comparator* comp = handles_[0]->GetComparator();
- ASSERT_EQ(comp, BytewiseComparator());
- // Add three column families - one with no comparator and two
- // with comparators specified
- ColumnFamilyOptions second, third, fourth;
- second.comparator = &third_comparator;
- third.comparator = &fourth_comparator;
- CreateColumnFamilies({"second", "third", "fourth"}, {second, third, fourth});
- ASSERT_EQ(handles_[1]->GetComparator(), BytewiseComparator());
- ASSERT_EQ(handles_[2]->GetComparator(), &third_comparator);
- ASSERT_EQ(handles_[3]->GetComparator(), &fourth_comparator);
- Close();
- }
- TEST_P(ColumnFamilyTest, DifferentMergeOperators) {
- Open();
- CreateColumnFamilies({"first", "second"});
- ColumnFamilyOptions default_cf, first, second;
- first.merge_operator = MergeOperators::CreateUInt64AddOperator();
- second.merge_operator = MergeOperators::CreateStringAppendOperator();
- Reopen({default_cf, first, second});
- std::string one, two, three;
- PutFixed64(&one, 1);
- PutFixed64(&two, 2);
- PutFixed64(&three, 3);
- ASSERT_OK(Put(0, "foo", two));
- ASSERT_OK(Put(0, "foo", one));
- ASSERT_TRUE(Merge(0, "foo", two).IsNotSupported());
- ASSERT_EQ(Get(0, "foo"), one);
- ASSERT_OK(Put(1, "foo", two));
- ASSERT_OK(Put(1, "foo", one));
- ASSERT_OK(Merge(1, "foo", two));
- ASSERT_EQ(Get(1, "foo"), three);
- ASSERT_OK(Put(2, "foo", two));
- ASSERT_OK(Put(2, "foo", one));
- ASSERT_OK(Merge(2, "foo", two));
- ASSERT_EQ(Get(2, "foo"), one + "," + two);
- Close();
- }
- TEST_P(ColumnFamilyTest, DifferentCompactionStyles) {
- Open();
- CreateColumnFamilies({"one", "two"});
- ColumnFamilyOptions default_cf, one, two;
- db_options_.max_open_files = 20; // only 10 files in file cache
- default_cf.level_compaction_dynamic_level_bytes = false;
- default_cf.compaction_style = kCompactionStyleLevel;
- default_cf.num_levels = 3;
- default_cf.write_buffer_size = 64 << 10; // 64KB
- default_cf.target_file_size_base = 30 << 10;
- default_cf.max_compaction_bytes = static_cast<uint64_t>(1) << 60;
- BlockBasedTableOptions table_options = GetBlockBasedTableOptions();
- table_options.no_block_cache = true;
- default_cf.table_factory.reset(NewBlockBasedTableFactory(table_options));
- one.compaction_style = kCompactionStyleUniversal;
- one.num_levels = 1;
- // trigger compaction if there are >= 4 files
- one.level0_file_num_compaction_trigger = 4;
- one.write_buffer_size = 120000;
- two.level_compaction_dynamic_level_bytes = false;
- two.compaction_style = kCompactionStyleLevel;
- two.num_levels = 4;
- two.level0_file_num_compaction_trigger = 3;
- two.write_buffer_size = 100000;
- Reopen({default_cf, one, two});
- // SETUP column family "one" -- universal style
- for (int i = 0; i < one.level0_file_num_compaction_trigger - 1; ++i) {
- PutRandomData(1, 10, 12000);
- PutRandomData(1, 1, 10);
- WaitForFlush(1);
- AssertFilesPerLevel(std::to_string(i + 1), 1);
- }
- // SETUP column family "two" -- level style with 4 levels
- for (int i = 0; i < two.level0_file_num_compaction_trigger - 1; ++i) {
- PutRandomData(2, 10, 12000);
- PutRandomData(2, 1, 10);
- WaitForFlush(2);
- AssertFilesPerLevel(std::to_string(i + 1), 2);
- }
- // TRIGGER compaction "one"
- PutRandomData(1, 10, 12000);
- PutRandomData(1, 1, 10);
- // TRIGGER compaction "two"
- PutRandomData(2, 10, 12000);
- PutRandomData(2, 1, 10);
- // WAIT for compactions
- WaitForCompaction();
- // VERIFY compaction "one"
- AssertFilesPerLevel("1", 1);
- // VERIFY compaction "two"
- AssertFilesPerLevel("0,1", 2);
- CompactAll(2);
- AssertFilesPerLevel("0,1", 2);
- Close();
- }
- // Sync points not supported in RocksDB Lite
- TEST_P(ColumnFamilyTest, MultipleManualCompactions) {
- Open();
- CreateColumnFamilies({"one", "two"});
- ColumnFamilyOptions default_cf, one, two;
- db_options_.max_open_files = 20; // only 10 files in file cache
- db_options_.max_background_compactions = 3;
- default_cf.level_compaction_dynamic_level_bytes = false;
- default_cf.compaction_style = kCompactionStyleLevel;
- default_cf.num_levels = 3;
- default_cf.write_buffer_size = 64 << 10; // 64KB
- default_cf.target_file_size_base = 30 << 10;
- default_cf.max_compaction_bytes = default_cf.target_file_size_base * 1100;
- BlockBasedTableOptions table_options = GetBlockBasedTableOptions();
- table_options.no_block_cache = true;
- default_cf.table_factory.reset(NewBlockBasedTableFactory(table_options));
- one.compaction_style = kCompactionStyleUniversal;
- one.num_levels = 1;
- // trigger compaction if there are >= 4 files
- one.level0_file_num_compaction_trigger = 4;
- one.write_buffer_size = 120000;
- two.level_compaction_dynamic_level_bytes = false;
- two.compaction_style = kCompactionStyleLevel;
- two.num_levels = 4;
- two.level0_file_num_compaction_trigger = 3;
- two.write_buffer_size = 100000;
- Reopen({default_cf, one, two});
- // SETUP column family "one" -- universal style
- for (int i = 0; i < one.level0_file_num_compaction_trigger - 2; ++i) {
- PutRandomData(1, 10, 12000, true);
- PutRandomData(1, 1, 10, true);
- WaitForFlush(1);
- AssertFilesPerLevel(std::to_string(i + 1), 1);
- }
- std::atomic_bool cf_1_1{true};
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
- {{"ColumnFamilyTest::MultiManual:4", "ColumnFamilyTest::MultiManual:1"},
- {"ColumnFamilyTest::MultiManual:2", "ColumnFamilyTest::MultiManual:5"},
- {"ColumnFamilyTest::MultiManual:2", "ColumnFamilyTest::MultiManual:3"}});
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::BackgroundCompaction:NonTrivial:AfterRun", [&](void* /*arg*/) {
- if (cf_1_1.exchange(false)) {
- TEST_SYNC_POINT("ColumnFamilyTest::MultiManual:4");
- TEST_SYNC_POINT("ColumnFamilyTest::MultiManual:3");
- }
- });
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- std::vector<port::Thread> threads;
- threads.emplace_back([&] {
- CompactRangeOptions compact_options;
- compact_options.exclusive_manual_compaction = false;
- ASSERT_OK(
- db_->CompactRange(compact_options, handles_[1], nullptr, nullptr));
- });
- // SETUP column family "two" -- level style with 4 levels
- for (int i = 0; i < two.level0_file_num_compaction_trigger - 2; ++i) {
- PutRandomData(2, 10, 12000);
- PutRandomData(2, 1, 10);
- WaitForFlush(2);
- AssertFilesPerLevel(std::to_string(i + 1), 2);
- }
- threads.emplace_back([&] {
- TEST_SYNC_POINT("ColumnFamilyTest::MultiManual:1");
- CompactRangeOptions compact_options;
- compact_options.exclusive_manual_compaction = false;
- ASSERT_OK(
- db_->CompactRange(compact_options, handles_[2], nullptr, nullptr));
- TEST_SYNC_POINT("ColumnFamilyTest::MultiManual:2");
- });
- TEST_SYNC_POINT("ColumnFamilyTest::MultiManual:5");
- for (auto& t : threads) {
- t.join();
- }
- // VERIFY compaction "one"
- AssertFilesPerLevel("1", 1);
- // VERIFY compaction "two"
- AssertFilesPerLevel("0,1", 2);
- CompactAll(2);
- AssertFilesPerLevel("0,1", 2);
- // Compare against saved keys
- std::set<std::string>::iterator key_iter = keys_[1].begin();
- while (key_iter != keys_[1].end()) {
- ASSERT_NE("NOT_FOUND", Get(1, *key_iter));
- key_iter++;
- }
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
- Close();
- }
- TEST_P(ColumnFamilyTest, AutomaticAndManualCompactions) {
- Open();
- CreateColumnFamilies({"one", "two"});
- ColumnFamilyOptions default_cf, one, two;
- db_options_.max_open_files = 20; // only 10 files in file cache
- db_options_.max_background_compactions = 3;
- default_cf.level_compaction_dynamic_level_bytes = false;
- default_cf.compaction_style = kCompactionStyleLevel;
- default_cf.num_levels = 3;
- default_cf.write_buffer_size = 64 << 10; // 64KB
- default_cf.target_file_size_base = 30 << 10;
- default_cf.max_compaction_bytes = default_cf.target_file_size_base * 1100;
- BlockBasedTableOptions table_options = GetBlockBasedTableOptions();
- table_options.no_block_cache = true;
- default_cf.table_factory.reset(NewBlockBasedTableFactory(table_options));
- one.compaction_style = kCompactionStyleUniversal;
- one.num_levels = 1;
- // trigger compaction if there are >= 4 files
- one.level0_file_num_compaction_trigger = 4;
- one.write_buffer_size = 120000;
- two.level_compaction_dynamic_level_bytes = false;
- two.compaction_style = kCompactionStyleLevel;
- two.num_levels = 4;
- two.level0_file_num_compaction_trigger = 3;
- two.write_buffer_size = 100000;
- Reopen({default_cf, one, two});
- // make sure all background compaction jobs can be scheduled
- auto stop_token =
- dbfull()->TEST_write_controler().GetCompactionPressureToken();
- std::atomic_bool cf_1_1{true};
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
- {{"ColumnFamilyTest::AutoManual:4", "ColumnFamilyTest::AutoManual:1"},
- {"ColumnFamilyTest::AutoManual:2", "ColumnFamilyTest::AutoManual:5"},
- {"ColumnFamilyTest::AutoManual:2", "ColumnFamilyTest::AutoManual:3"}});
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::BackgroundCompaction:NonTrivial:AfterRun", [&](void* /*arg*/) {
- if (cf_1_1.exchange(false)) {
- TEST_SYNC_POINT("ColumnFamilyTest::AutoManual:4");
- TEST_SYNC_POINT("ColumnFamilyTest::AutoManual:3");
- }
- });
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- // SETUP column family "one" -- universal style
- for (int i = 0; i < one.level0_file_num_compaction_trigger; ++i) {
- PutRandomData(1, 10, 12000, true);
- PutRandomData(1, 1, 10, true);
- WaitForFlush(1);
- AssertFilesPerLevel(std::to_string(i + 1), 1);
- }
- TEST_SYNC_POINT("ColumnFamilyTest::AutoManual:1");
- // SETUP column family "two" -- level style with 4 levels
- for (int i = 0; i < two.level0_file_num_compaction_trigger - 2; ++i) {
- PutRandomData(2, 10, 12000);
- PutRandomData(2, 1, 10);
- WaitForFlush(2);
- AssertFilesPerLevel(std::to_string(i + 1), 2);
- }
- ROCKSDB_NAMESPACE::port::Thread threads([&] {
- CompactRangeOptions compact_options;
- compact_options.exclusive_manual_compaction = false;
- ASSERT_OK(
- db_->CompactRange(compact_options, handles_[2], nullptr, nullptr));
- TEST_SYNC_POINT("ColumnFamilyTest::AutoManual:2");
- });
- TEST_SYNC_POINT("ColumnFamilyTest::AutoManual:5");
- threads.join();
- // WAIT for compactions
- WaitForCompaction();
- // VERIFY compaction "one"
- AssertFilesPerLevel("1", 1);
- // VERIFY compaction "two"
- AssertFilesPerLevel("0,1", 2);
- CompactAll(2);
- AssertFilesPerLevel("0,1", 2);
- // Compare against saved keys
- std::set<std::string>::iterator key_iter = keys_[1].begin();
- while (key_iter != keys_[1].end()) {
- ASSERT_NE("NOT_FOUND", Get(1, *key_iter));
- key_iter++;
- }
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- TEST_P(ColumnFamilyTest, ManualAndAutomaticCompactions) {
- Open();
- CreateColumnFamilies({"one", "two"});
- ColumnFamilyOptions default_cf, one, two;
- db_options_.max_open_files = 20; // only 10 files in file cache
- db_options_.max_background_compactions = 3;
- default_cf.level_compaction_dynamic_level_bytes = false;
- default_cf.compaction_style = kCompactionStyleLevel;
- default_cf.num_levels = 3;
- default_cf.write_buffer_size = 64 << 10; // 64KB
- default_cf.target_file_size_base = 30 << 10;
- default_cf.max_compaction_bytes = default_cf.target_file_size_base * 1100;
- BlockBasedTableOptions table_options = GetBlockBasedTableOptions();
- table_options.no_block_cache = true;
- default_cf.table_factory.reset(NewBlockBasedTableFactory(table_options));
- one.compaction_style = kCompactionStyleUniversal;
- one.num_levels = 1;
- // trigger compaction if there are >= 4 files
- one.level0_file_num_compaction_trigger = 4;
- one.write_buffer_size = 120000;
- two.level_compaction_dynamic_level_bytes = false;
- two.compaction_style = kCompactionStyleLevel;
- two.num_levels = 4;
- two.level0_file_num_compaction_trigger = 3;
- two.write_buffer_size = 100000;
- Reopen({default_cf, one, two});
- // make sure all background compaction jobs can be scheduled
- auto stop_token =
- dbfull()->TEST_write_controler().GetCompactionPressureToken();
- // SETUP column family "one" -- universal style
- for (int i = 0; i < one.level0_file_num_compaction_trigger - 2; ++i) {
- PutRandomData(1, 10, 12000, true);
- PutRandomData(1, 1, 10, true);
- WaitForFlush(1);
- AssertFilesPerLevel(std::to_string(i + 1), 1);
- }
- std::atomic_bool cf_1_1{true};
- std::atomic_bool cf_1_2{true};
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
- {{"ColumnFamilyTest::ManualAuto:4", "ColumnFamilyTest::ManualAuto:1"},
- {"ColumnFamilyTest::ManualAuto:5", "ColumnFamilyTest::ManualAuto:2"},
- {"ColumnFamilyTest::ManualAuto:2", "ColumnFamilyTest::ManualAuto:3"}});
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::BackgroundCompaction:NonTrivial:AfterRun", [&](void* /*arg*/) {
- if (cf_1_1.exchange(false)) {
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:4");
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:3");
- } else if (cf_1_2.exchange(false)) {
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:2");
- }
- });
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- ROCKSDB_NAMESPACE::port::Thread threads([&] {
- CompactRangeOptions compact_options;
- compact_options.exclusive_manual_compaction = false;
- ASSERT_OK(
- db_->CompactRange(compact_options, handles_[1], nullptr, nullptr));
- });
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:1");
- // SETUP column family "two" -- level style with 4 levels
- for (int i = 0; i < two.level0_file_num_compaction_trigger; ++i) {
- PutRandomData(2, 10, 12000);
- PutRandomData(2, 1, 10);
- WaitForFlush(2);
- AssertFilesPerLevel(std::to_string(i + 1), 2);
- }
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:5");
- threads.join();
- // WAIT for compactions
- WaitForCompaction();
- // VERIFY compaction "one"
- AssertFilesPerLevel("1", 1);
- // VERIFY compaction "two"
- AssertFilesPerLevel("0,1", 2);
- CompactAll(2);
- AssertFilesPerLevel("0,1", 2);
- // Compare against saved keys
- std::set<std::string>::iterator key_iter = keys_[1].begin();
- while (key_iter != keys_[1].end()) {
- ASSERT_NE("NOT_FOUND", Get(1, *key_iter));
- key_iter++;
- }
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- TEST_P(ColumnFamilyTest, SameCFManualManualCompactions) {
- Open();
- CreateColumnFamilies({"one"});
- ColumnFamilyOptions default_cf, one;
- db_options_.max_open_files = 20; // only 10 files in file cache
- db_options_.max_background_compactions = 3;
- default_cf.compaction_style = kCompactionStyleLevel;
- default_cf.num_levels = 3;
- default_cf.write_buffer_size = 64 << 10; // 64KB
- default_cf.target_file_size_base = 30 << 10;
- default_cf.max_compaction_bytes = default_cf.target_file_size_base * 1100;
- BlockBasedTableOptions table_options = GetBlockBasedTableOptions();
- ;
- table_options.no_block_cache = true;
- default_cf.table_factory.reset(NewBlockBasedTableFactory(table_options));
- one.compaction_style = kCompactionStyleUniversal;
- one.num_levels = 1;
- // trigger compaction if there are >= 4 files
- one.level0_file_num_compaction_trigger = 4;
- one.write_buffer_size = 120000;
- Reopen({default_cf, one});
- // make sure all background compaction jobs can be scheduled
- auto stop_token =
- dbfull()->TEST_write_controler().GetCompactionPressureToken();
- // SETUP column family "one" -- universal style
- for (int i = 0; i < one.level0_file_num_compaction_trigger - 2; ++i) {
- PutRandomData(1, 10, 12000, true);
- PutRandomData(1, 1, 10, true);
- WaitForFlush(1);
- AssertFilesPerLevel(std::to_string(i + 1), 1);
- }
- std::atomic_bool cf_1_1{true};
- std::atomic_bool cf_1_2{true};
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
- {{"ColumnFamilyTest::ManualManual:4", "ColumnFamilyTest::ManualManual:2"},
- {"ColumnFamilyTest::ManualManual:4", "ColumnFamilyTest::ManualManual:5"},
- {"ColumnFamilyTest::ManualManual:1", "ColumnFamilyTest::ManualManual:2"},
- {"ColumnFamilyTest::ManualManual:1",
- "ColumnFamilyTest::ManualManual:3"}});
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::BackgroundCompaction:NonTrivial:AfterRun", [&](void* /*arg*/) {
- if (cf_1_1.exchange(false)) {
- TEST_SYNC_POINT("ColumnFamilyTest::ManualManual:4");
- TEST_SYNC_POINT("ColumnFamilyTest::ManualManual:3");
- } else if (cf_1_2.exchange(false)) {
- TEST_SYNC_POINT("ColumnFamilyTest::ManualManual:2");
- }
- });
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- ROCKSDB_NAMESPACE::port::Thread threads([&] {
- CompactRangeOptions compact_options;
- compact_options.exclusive_manual_compaction = true;
- ASSERT_OK(
- db_->CompactRange(compact_options, handles_[1], nullptr, nullptr));
- });
- TEST_SYNC_POINT("ColumnFamilyTest::ManualManual:5");
- WaitForFlush(1);
- // Add more L0 files and force another manual compaction
- for (int i = 0; i < one.level0_file_num_compaction_trigger - 2; ++i) {
- PutRandomData(1, 10, 12000, true);
- PutRandomData(1, 1, 10, true);
- WaitForFlush(1);
- AssertFilesPerLevel(
- std::to_string(one.level0_file_num_compaction_trigger + i), 1);
- }
- ROCKSDB_NAMESPACE::port::Thread threads1([&] {
- CompactRangeOptions compact_options;
- compact_options.exclusive_manual_compaction = false;
- ASSERT_OK(
- db_->CompactRange(compact_options, handles_[1], nullptr, nullptr));
- });
- TEST_SYNC_POINT("ColumnFamilyTest::ManualManual:1");
- threads.join();
- threads1.join();
- WaitForCompaction();
- // VERIFY compaction "one"
- ASSERT_LE(NumTableFilesAtLevel(0, 1), 2);
- // Compare against saved keys
- std::set<std::string>::iterator key_iter = keys_[1].begin();
- while (key_iter != keys_[1].end()) {
- ASSERT_NE("NOT_FOUND", Get(1, *key_iter));
- key_iter++;
- }
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- TEST_P(ColumnFamilyTest, SameCFManualAutomaticCompactions) {
- Open();
- CreateColumnFamilies({"one"});
- ColumnFamilyOptions default_cf, one;
- db_options_.max_open_files = 20; // only 10 files in file cache
- db_options_.max_background_compactions = 3;
- default_cf.compaction_style = kCompactionStyleLevel;
- default_cf.num_levels = 3;
- default_cf.write_buffer_size = 64 << 10; // 64KB
- default_cf.target_file_size_base = 30 << 10;
- default_cf.max_compaction_bytes = default_cf.target_file_size_base * 1100;
- BlockBasedTableOptions table_options = GetBlockBasedTableOptions();
- ;
- table_options.no_block_cache = true;
- default_cf.table_factory.reset(NewBlockBasedTableFactory(table_options));
- one.compaction_style = kCompactionStyleUniversal;
- one.num_levels = 1;
- // trigger compaction if there are >= 4 files
- one.level0_file_num_compaction_trigger = 4;
- one.write_buffer_size = 120000;
- Reopen({default_cf, one});
- // make sure all background compaction jobs can be scheduled
- auto stop_token =
- dbfull()->TEST_write_controler().GetCompactionPressureToken();
- // SETUP column family "one" -- universal style
- for (int i = 0; i < one.level0_file_num_compaction_trigger - 2; ++i) {
- PutRandomData(1, 10, 12000, true);
- PutRandomData(1, 1, 10, true);
- WaitForFlush(1);
- AssertFilesPerLevel(std::to_string(i + 1), 1);
- }
- std::atomic_bool cf_1_1{true};
- std::atomic_bool cf_1_2{true};
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
- {{"ColumnFamilyTest::ManualAuto:4", "ColumnFamilyTest::ManualAuto:2"},
- {"ColumnFamilyTest::ManualAuto:4", "ColumnFamilyTest::ManualAuto:5"},
- {"ColumnFamilyTest::ManualAuto:1", "ColumnFamilyTest::ManualAuto:2"},
- {"ColumnFamilyTest::ManualAuto:1", "ColumnFamilyTest::ManualAuto:3"}});
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::BackgroundCompaction:NonTrivial:AfterRun", [&](void* /*arg*/) {
- if (cf_1_1.exchange(false)) {
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:4");
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:3");
- } else if (cf_1_2.exchange(false)) {
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:2");
- }
- });
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- ROCKSDB_NAMESPACE::port::Thread threads([&] {
- CompactRangeOptions compact_options;
- compact_options.exclusive_manual_compaction = false;
- ASSERT_OK(
- db_->CompactRange(compact_options, handles_[1], nullptr, nullptr));
- });
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:5");
- WaitForFlush(1);
- // Add more L0 files and force automatic compaction
- for (int i = 0; i < one.level0_file_num_compaction_trigger; ++i) {
- PutRandomData(1, 10, 12000, true);
- PutRandomData(1, 1, 10, true);
- WaitForFlush(1);
- AssertFilesPerLevel(
- std::to_string(one.level0_file_num_compaction_trigger + i), 1);
- }
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:1");
- threads.join();
- WaitForCompaction();
- // VERIFY compaction "one"
- ASSERT_LE(NumTableFilesAtLevel(0, 1), 2);
- // Compare against saved keys
- std::set<std::string>::iterator key_iter = keys_[1].begin();
- while (key_iter != keys_[1].end()) {
- ASSERT_NE("NOT_FOUND", Get(1, *key_iter));
- key_iter++;
- }
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- TEST_P(ColumnFamilyTest, SameCFManualAutomaticCompactionsLevel) {
- Open();
- CreateColumnFamilies({"one"});
- ColumnFamilyOptions default_cf, one;
- db_options_.max_open_files = 20; // only 10 files in file cache
- db_options_.max_background_compactions = 3;
- default_cf.compaction_style = kCompactionStyleLevel;
- default_cf.num_levels = 3;
- default_cf.write_buffer_size = 64 << 10; // 64KB
- default_cf.target_file_size_base = 30 << 10;
- default_cf.max_compaction_bytes = default_cf.target_file_size_base * 1100;
- BlockBasedTableOptions table_options = GetBlockBasedTableOptions();
- ;
- table_options.no_block_cache = true;
- default_cf.table_factory.reset(NewBlockBasedTableFactory(table_options));
- one.compaction_style = kCompactionStyleLevel;
- one.num_levels = 1;
- // trigger compaction if there are >= 4 files
- one.level0_file_num_compaction_trigger = 3;
- one.write_buffer_size = 120000;
- Reopen({default_cf, one});
- // make sure all background compaction jobs can be scheduled
- auto stop_token =
- dbfull()->TEST_write_controler().GetCompactionPressureToken();
- // SETUP column family "one" -- level style
- for (int i = 0; i < one.level0_file_num_compaction_trigger - 2; ++i) {
- PutRandomData(1, 10, 12000, true);
- PutRandomData(1, 1, 10, true);
- WaitForFlush(1);
- AssertFilesPerLevel(std::to_string(i + 1), 1);
- }
- std::atomic_bool cf_1_1{true};
- std::atomic_bool cf_1_2{true};
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
- {{"ColumnFamilyTest::ManualAuto:4", "ColumnFamilyTest::ManualAuto:2"},
- {"ColumnFamilyTest::ManualAuto:4", "ColumnFamilyTest::ManualAuto:5"},
- {"ColumnFamilyTest::ManualAuto:3", "ColumnFamilyTest::ManualAuto:2"},
- {"LevelCompactionPicker::PickCompactionBySize:0",
- "ColumnFamilyTest::ManualAuto:3"},
- {"ColumnFamilyTest::ManualAuto:1", "ColumnFamilyTest::ManualAuto:3"}});
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::BackgroundCompaction:NonTrivial:AfterRun", [&](void* /*arg*/) {
- if (cf_1_1.exchange(false)) {
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:4");
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:3");
- } else if (cf_1_2.exchange(false)) {
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:2");
- }
- });
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- ROCKSDB_NAMESPACE::port::Thread threads([&] {
- CompactRangeOptions compact_options;
- compact_options.exclusive_manual_compaction = false;
- ASSERT_OK(
- db_->CompactRange(compact_options, handles_[1], nullptr, nullptr));
- });
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:5");
- // Add more L0 files and force automatic compaction
- for (int i = 0; i < one.level0_file_num_compaction_trigger; ++i) {
- PutRandomData(1, 10, 12000, true);
- PutRandomData(1, 1, 10, true);
- WaitForFlush(1);
- AssertFilesPerLevel(
- std::to_string(one.level0_file_num_compaction_trigger + i), 1);
- }
- TEST_SYNC_POINT("ColumnFamilyTest::ManualAuto:1");
- threads.join();
- WaitForCompaction();
- // VERIFY compaction "one"
- AssertFilesPerLevel("0,1", 1);
- // Compare against saved keys
- std::set<std::string>::iterator key_iter = keys_[1].begin();
- while (key_iter != keys_[1].end()) {
- ASSERT_NE("NOT_FOUND", Get(1, *key_iter));
- key_iter++;
- }
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- // In this test, we generate enough files to trigger automatic compactions.
- // The automatic compaction waits in NonTrivial:AfterRun
- // We generate more files and then trigger an automatic compaction
- // This will wait because the automatic compaction has files it needs.
- // Once the conflict is hit, the automatic compaction starts and ends
- // Then the manual will run and end.
- TEST_P(ColumnFamilyTest, SameCFAutomaticManualCompactions) {
- Open();
- CreateColumnFamilies({"one"});
- ColumnFamilyOptions default_cf, one;
- db_options_.max_open_files = 20; // only 10 files in file cache
- db_options_.max_background_compactions = 3;
- default_cf.compaction_style = kCompactionStyleLevel;
- default_cf.num_levels = 3;
- default_cf.write_buffer_size = 64 << 10; // 64KB
- default_cf.target_file_size_base = 30 << 10;
- default_cf.max_compaction_bytes = default_cf.target_file_size_base * 1100;
- BlockBasedTableOptions table_options = GetBlockBasedTableOptions();
- ;
- table_options.no_block_cache = true;
- default_cf.table_factory.reset(NewBlockBasedTableFactory(table_options));
- one.compaction_style = kCompactionStyleUniversal;
- one.num_levels = 1;
- // trigger compaction if there are >= 4 files
- one.level0_file_num_compaction_trigger = 4;
- one.write_buffer_size = 120000;
- Reopen({default_cf, one});
- // make sure all background compaction jobs can be scheduled
- auto stop_token =
- dbfull()->TEST_write_controler().GetCompactionPressureToken();
- std::atomic_bool cf_1_1{true};
- std::atomic_bool cf_1_2{true};
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
- {{"ColumnFamilyTest::AutoManual:4", "ColumnFamilyTest::AutoManual:2"},
- {"ColumnFamilyTest::AutoManual:4", "ColumnFamilyTest::AutoManual:5"},
- {"CompactionPicker::CompactRange:Conflict",
- "ColumnFamilyTest::AutoManual:3"}});
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::BackgroundCompaction:NonTrivial:AfterRun", [&](void* /*arg*/) {
- if (cf_1_1.exchange(false)) {
- TEST_SYNC_POINT("ColumnFamilyTest::AutoManual:4");
- TEST_SYNC_POINT("ColumnFamilyTest::AutoManual:3");
- } else if (cf_1_2.exchange(false)) {
- TEST_SYNC_POINT("ColumnFamilyTest::AutoManual:2");
- }
- });
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- // SETUP column family "one" -- universal style
- for (int i = 0; i < one.level0_file_num_compaction_trigger; ++i) {
- PutRandomData(1, 10, 12000, true);
- PutRandomData(1, 1, 10, true);
- WaitForFlush(1);
- AssertFilesPerLevel(std::to_string(i + 1), 1);
- }
- TEST_SYNC_POINT("ColumnFamilyTest::AutoManual:5");
- // Add another L0 file and force automatic compaction
- for (int i = 0; i < one.level0_file_num_compaction_trigger - 2; ++i) {
- PutRandomData(1, 10, 12000, true);
- PutRandomData(1, 1, 10, true);
- WaitForFlush(1);
- }
- CompactRangeOptions compact_options;
- compact_options.exclusive_manual_compaction = false;
- ASSERT_OK(db_->CompactRange(compact_options, handles_[1], nullptr, nullptr));
- TEST_SYNC_POINT("ColumnFamilyTest::AutoManual:1");
- WaitForCompaction();
- // VERIFY compaction "one"
- AssertFilesPerLevel("1", 1);
- // Compare against saved keys
- std::set<std::string>::iterator key_iter = keys_[1].begin();
- while (key_iter != keys_[1].end()) {
- ASSERT_NE("NOT_FOUND", Get(1, *key_iter));
- key_iter++;
- }
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- namespace {
- std::string IterStatus(Iterator* iter) {
- std::string result;
- if (iter->Valid()) {
- result = iter->key().ToString() + "->" + iter->value().ToString();
- } else {
- EXPECT_OK(iter->status());
- result = "(invalid)";
- }
- return result;
- }
- } // anonymous namespace
- TEST_P(ColumnFamilyTest, NewIteratorsTest) {
- // iter == 0 -- no tailing
- // iter == 2 -- tailing
- for (int iter = 0; iter < 2; ++iter) {
- Open();
- CreateColumnFamiliesAndReopen({"one", "two"});
- ASSERT_OK(Put(0, "a", "b"));
- ASSERT_OK(Put(1, "b", "a"));
- ASSERT_OK(Put(2, "c", "m"));
- ASSERT_OK(Put(2, "v", "t"));
- std::vector<Iterator*> iterators;
- ReadOptions options;
- options.tailing = (iter == 1);
- ASSERT_OK(db_->NewIterators(options, handles_, &iterators));
- for (auto it : iterators) {
- it->SeekToFirst();
- }
- ASSERT_EQ(IterStatus(iterators[0]), "a->b");
- ASSERT_EQ(IterStatus(iterators[1]), "b->a");
- ASSERT_EQ(IterStatus(iterators[2]), "c->m");
- ASSERT_OK(Put(1, "x", "x"));
- for (auto it : iterators) {
- it->Next();
- }
- ASSERT_EQ(IterStatus(iterators[0]), "(invalid)");
- if (iter == 0) {
- // no tailing
- ASSERT_EQ(IterStatus(iterators[1]), "(invalid)");
- } else {
- // tailing
- ASSERT_EQ(IterStatus(iterators[1]), "x->x");
- }
- ASSERT_EQ(IterStatus(iterators[2]), "v->t");
- for (auto it : iterators) {
- delete it;
- }
- Destroy();
- }
- }
- TEST_P(ColumnFamilyTest, ReadOnlyDBTest) {
- Open();
- CreateColumnFamiliesAndReopen({"one", "two", "three", "four"});
- ASSERT_OK(Put(0, "a", "b"));
- ASSERT_OK(Put(1, "foo", "bla"));
- ASSERT_OK(Put(2, "foo", "blabla"));
- ASSERT_OK(Put(3, "foo", "blablabla"));
- ASSERT_OK(Put(4, "foo", "blablablabla"));
- DropColumnFamilies({2});
- Close();
- // open only a subset of column families
- AssertOpenReadOnly({"default", "one", "four"});
- ASSERT_EQ("NOT_FOUND", Get(0, "foo"));
- ASSERT_EQ("bla", Get(1, "foo"));
- ASSERT_EQ("blablablabla", Get(2, "foo"));
- // test newiterators
- {
- std::vector<Iterator*> iterators;
- ASSERT_OK(db_->NewIterators(ReadOptions(), handles_, &iterators));
- for (auto it : iterators) {
- it->SeekToFirst();
- }
- ASSERT_EQ(IterStatus(iterators[0]), "a->b");
- ASSERT_EQ(IterStatus(iterators[1]), "foo->bla");
- ASSERT_EQ(IterStatus(iterators[2]), "foo->blablablabla");
- for (auto it : iterators) {
- it->Next();
- }
- ASSERT_EQ(IterStatus(iterators[0]), "(invalid)");
- ASSERT_EQ(IterStatus(iterators[1]), "(invalid)");
- ASSERT_EQ(IterStatus(iterators[2]), "(invalid)");
- for (auto it : iterators) {
- delete it;
- }
- }
- Close();
- // can't open dropped column family
- Status s = OpenReadOnly({"default", "one", "two"});
- ASSERT_TRUE(!s.ok());
- // Can't open without specifying default column family
- s = OpenReadOnly({"one", "four"});
- ASSERT_TRUE(!s.ok());
- }
- TEST_P(ColumnFamilyTest, DontRollEmptyLogs) {
- Open();
- CreateColumnFamiliesAndReopen({"one", "two", "three", "four"});
- for (size_t i = 0; i < handles_.size(); ++i) {
- PutRandomData(static_cast<int>(i), 10, 100);
- }
- int num_writable_file_start = env_->GetNumberOfNewWritableFileCalls();
- // this will trigger the flushes
- for (int i = 0; i <= 4; ++i) {
- ASSERT_OK(Flush(i));
- }
- for (int i = 0; i < 4; ++i) {
- WaitForFlush(i);
- }
- int total_new_writable_files =
- env_->GetNumberOfNewWritableFileCalls() - num_writable_file_start;
- ASSERT_EQ(static_cast<size_t>(total_new_writable_files), handles_.size() + 1);
- Close();
- }
- TEST_P(ColumnFamilyTest, FlushStaleColumnFamilies) {
- Open();
- CreateColumnFamilies({"one", "two"});
- ColumnFamilyOptions default_cf, one, two;
- default_cf.write_buffer_size = 100000; // small write buffer size
- default_cf.arena_block_size = 4096;
- default_cf.disable_auto_compactions = true;
- one.disable_auto_compactions = true;
- two.disable_auto_compactions = true;
- db_options_.max_total_wal_size = 210000;
- Reopen({default_cf, one, two});
- PutRandomData(2, 1, 10); // 10 bytes
- for (int i = 0; i < 2; ++i) {
- PutRandomData(0, 100, 1000); // flush
- WaitForFlush(0);
- AssertCountLiveFiles(i + 1);
- }
- // third flush. now, CF [two] should be detected as stale and flushed
- // column family 1 should not be flushed since it's empty
- PutRandomData(0, 100, 1000); // flush
- WaitForFlush(0);
- WaitForFlush(2);
- // at least 3 files for default column families, 1 file for column family
- // [two], zero files for column family [one], because it's empty
- std::vector<LiveFileMetaData> metadata;
- db_->GetLiveFilesMetaData(&metadata);
- ASSERT_GE(metadata.size(), 4);
- bool has_cf1_sst = false;
- bool has_cf2_sst = false;
- for (const auto& file : metadata) {
- if (file.column_family_name == "one") {
- has_cf1_sst = true;
- } else if (file.column_family_name == "two") {
- has_cf2_sst = true;
- }
- }
- ASSERT_FALSE(has_cf1_sst);
- ASSERT_TRUE(has_cf2_sst);
- ASSERT_OK(Flush(0));
- ASSERT_EQ(0, dbfull()->TEST_wals_total_size());
- Close();
- }
- namespace {
- struct CountOptionsFilesFs : public FileSystemWrapper {
- explicit CountOptionsFilesFs(const std::shared_ptr<FileSystem>& t)
- : FileSystemWrapper(t) {}
- const char* Name() const override { return "CountOptionsFilesFs"; }
- IOStatus NewWritableFile(const std::string& f, const FileOptions& file_opts,
- std::unique_ptr<FSWritableFile>* r,
- IODebugContext* dbg) override {
- if (f.find("OPTIONS-") != std::string::npos) {
- options_files_created.fetch_add(1, std::memory_order_relaxed);
- }
- return FileSystemWrapper::NewWritableFile(f, file_opts, r, dbg);
- }
- std::atomic<int> options_files_created{};
- };
- } // namespace
- TEST_P(ColumnFamilyTest, CreateMissingColumnFamilies) {
- // Can't accidentally add CFs to an existing DB
- Open();
- Close();
- ASSERT_FALSE(db_options_.create_missing_column_families);
- ASSERT_NOK(TryOpen({"one", "two"}));
- // Nor accidentally create in a new DB
- Destroy();
- db_options_.create_if_missing = true;
- ASSERT_NOK(TryOpen({"one", "two"}));
- // Only with the option (new DB case)
- db_options_.create_missing_column_families = true;
- // Also setup to count number of options files created (see check below)
- auto my_fs =
- std::make_shared<CountOptionsFilesFs>(db_options_.env->GetFileSystem());
- auto my_env = std::make_unique<CompositeEnvWrapper>(db_options_.env, my_fs);
- SaveAndRestore<Env*> save_restore_env(&db_options_.env, my_env.get());
- ASSERT_OK(TryOpen({"default", "one", "two"}));
- Close();
- // An older version would write an updated options file for each column
- // family created under create_missing_column_families, which would be
- // quadratic I/O in the number of column families.
- ASSERT_EQ(my_fs->options_files_created.load(), 1);
- // Add to existing DB case
- ASSERT_OK(TryOpen({"default", "one", "two", "three", "four"}));
- Close();
- ASSERT_EQ(my_fs->options_files_created.load(), 2);
- }
- TEST_P(ColumnFamilyTest, SanitizeCfOptions) {
- DBOptions db_options;
- for (int s = kCompactionStyleLevel; s <= kCompactionStyleUniversal; ++s) {
- for (int l = 0; l <= 2; l++) {
- for (int i = 1; i <= 3; i++) {
- for (int j = 1; j <= 3; j++) {
- for (int k = 1; k <= 3; k++) {
- ColumnFamilyOptions original;
- original.compaction_style = static_cast<CompactionStyle>(s);
- original.num_levels = l;
- original.level0_stop_writes_trigger = i;
- original.level0_slowdown_writes_trigger = j;
- original.level0_file_num_compaction_trigger = k;
- original.write_buffer_size =
- l * 4 * 1024 * 1024 + i * 1024 * 1024 + j * 1024 + k;
- ColumnFamilyOptions result = SanitizeCfOptions(
- ImmutableDBOptions(db_options), /*read_only*/ false, original);
- ASSERT_TRUE(result.level0_stop_writes_trigger >=
- result.level0_slowdown_writes_trigger);
- ASSERT_TRUE(result.level0_slowdown_writes_trigger >=
- result.level0_file_num_compaction_trigger);
- ASSERT_TRUE(result.level0_file_num_compaction_trigger ==
- original.level0_file_num_compaction_trigger);
- if (s == kCompactionStyleLevel) {
- ASSERT_GE(result.num_levels, 2);
- } else {
- ASSERT_GE(result.num_levels, 1);
- if (original.num_levels >= 1) {
- ASSERT_EQ(result.num_levels, original.num_levels);
- }
- }
- // Make sure Sanitize options sets arena_block_size to 1/8 of
- // the write_buffer_size, rounded up to a multiple of 4k.
- size_t expected_arena_block_size =
- l * 4 * 1024 * 1024 / 8 + i * 1024 * 1024 / 8;
- if (j + k != 0) {
- // not a multiple of 4k, round up 4k
- expected_arena_block_size += 4 * 1024;
- }
- expected_arena_block_size =
- std::min(size_t{1024 * 1024}, expected_arena_block_size);
- ASSERT_EQ(expected_arena_block_size, result.arena_block_size);
- }
- }
- }
- }
- }
- }
- TEST_P(ColumnFamilyTest, ReadDroppedColumnFamily) {
- // iter 0 -- drop CF, don't reopen
- // iter 1 -- delete CF, reopen
- for (int iter = 0; iter < 2; ++iter) {
- db_options_.create_missing_column_families = true;
- db_options_.max_open_files = 20;
- // delete obsolete files always
- db_options_.delete_obsolete_files_period_micros = 0;
- Open({"default", "one", "two"});
- ColumnFamilyOptions options;
- options.level0_file_num_compaction_trigger = 100;
- options.level0_slowdown_writes_trigger = 200;
- options.level0_stop_writes_trigger = 200;
- options.write_buffer_size = 100000; // small write buffer size
- Reopen({options, options, options});
- // 1MB should create ~10 files for each CF
- int kKeysNum = 10000;
- PutRandomData(0, kKeysNum, 100);
- PutRandomData(1, kKeysNum, 100);
- PutRandomData(2, kKeysNum, 100);
- {
- std::unique_ptr<Iterator> iterator(
- db_->NewIterator(ReadOptions(), handles_[2]));
- iterator->SeekToFirst();
- if (iter == 0) {
- // Drop CF two
- ASSERT_OK(db_->DropColumnFamily(handles_[2]));
- } else {
- // delete CF two
- ASSERT_OK(db_->DestroyColumnFamilyHandle(handles_[2]));
- handles_[2] = nullptr;
- }
- // Make sure iterator created can still be used.
- int count = 0;
- for (; iterator->Valid(); iterator->Next()) {
- ASSERT_OK(iterator->status());
- ++count;
- }
- ASSERT_OK(iterator->status());
- ASSERT_EQ(count, kKeysNum);
- }
- // Add bunch more data to other CFs
- PutRandomData(0, kKeysNum, 100);
- PutRandomData(1, kKeysNum, 100);
- if (iter == 1) {
- Reopen();
- }
- // Since we didn't delete CF handle, RocksDB's contract guarantees that
- // we're still able to read dropped CF
- for (int i = 0; i < 3; ++i) {
- std::unique_ptr<Iterator> iterator(
- db_->NewIterator(ReadOptions(), handles_[i]));
- int count = 0;
- for (iterator->SeekToFirst(); iterator->Valid(); iterator->Next()) {
- ASSERT_OK(iterator->status());
- ++count;
- }
- ASSERT_OK(iterator->status());
- ASSERT_EQ(count, kKeysNum * ((i == 2) ? 1 : 2));
- }
- Close();
- Destroy();
- }
- }
- TEST_P(ColumnFamilyTest, LiveIteratorWithDroppedColumnFamily) {
- db_options_.create_missing_column_families = true;
- db_options_.max_open_files = 20;
- // delete obsolete files always
- db_options_.delete_obsolete_files_period_micros = 0;
- Open({"default", "one", "two"});
- ColumnFamilyOptions options;
- options.level0_file_num_compaction_trigger = 100;
- options.level0_slowdown_writes_trigger = 200;
- options.level0_stop_writes_trigger = 200;
- options.write_buffer_size = 100000; // small write buffer size
- Reopen({options, options, options});
- // 1MB should create ~10 files for each CF
- int kKeysNum = 10000;
- PutRandomData(1, kKeysNum, 100);
- {
- std::unique_ptr<Iterator> iterator(
- db_->NewIterator(ReadOptions(), handles_[1]));
- iterator->SeekToFirst();
- DropColumnFamilies({1});
- // Make sure iterator created can still be used.
- int count = 0;
- for (; iterator->Valid(); iterator->Next()) {
- ASSERT_OK(iterator->status());
- ++count;
- }
- ASSERT_OK(iterator->status());
- ASSERT_EQ(count, kKeysNum);
- }
- Reopen();
- Close();
- Destroy();
- }
- TEST_P(ColumnFamilyTest, FlushAndDropRaceCondition) {
- db_options_.create_missing_column_families = true;
- Open({"default", "one"});
- ColumnFamilyOptions options;
- options.level0_file_num_compaction_trigger = 100;
- options.level0_slowdown_writes_trigger = 200;
- options.level0_stop_writes_trigger = 200;
- options.max_write_buffer_number = 20;
- options.write_buffer_size = 100000; // small write buffer size
- Reopen({options, options});
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
- {{"VersionSet::LogAndApply::ColumnFamilyDrop:0",
- "FlushJob::WriteLevel0Table"},
- {"VersionSet::LogAndApply::ColumnFamilyDrop:1",
- "FlushJob::InstallResults"},
- {"FlushJob::InstallResults",
- "VersionSet::LogAndApply::ColumnFamilyDrop:2"}});
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- test::SleepingBackgroundTask sleeping_task;
- env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &sleeping_task,
- Env::Priority::HIGH);
- // Make sure the task is sleeping. Otherwise, it might start to execute
- // after sleeping_task.WaitUntilDone() and cause TSAN warning.
- sleeping_task.WaitUntilSleeping();
- // 1MB should create ~10 files for each CF
- int kKeysNum = 10000;
- PutRandomData(1, kKeysNum, 100);
- std::vector<port::Thread> threads;
- threads.emplace_back([&] { ASSERT_OK(db_->DropColumnFamily(handles_[1])); });
- sleeping_task.WakeUp();
- sleeping_task.WaitUntilDone();
- sleeping_task.Reset();
- // now we sleep again. this is just so we're certain that flush job finished
- env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &sleeping_task,
- Env::Priority::HIGH);
- // Make sure the task is sleeping. Otherwise, it might start to execute
- // after sleeping_task.WaitUntilDone() and cause TSAN warning.
- sleeping_task.WaitUntilSleeping();
- sleeping_task.WakeUp();
- sleeping_task.WaitUntilDone();
- {
- // Since we didn't delete CF handle, RocksDB's contract guarantees that
- // we're still able to read dropped CF
- std::unique_ptr<Iterator> iterator(
- db_->NewIterator(ReadOptions(), handles_[1]));
- int count = 0;
- for (iterator->SeekToFirst(); iterator->Valid(); iterator->Next()) {
- ASSERT_OK(iterator->status());
- ++count;
- }
- ASSERT_OK(iterator->status());
- ASSERT_EQ(count, kKeysNum);
- }
- for (auto& t : threads) {
- t.join();
- }
- Close();
- Destroy();
- }
- namespace {
- std::atomic<int> test_stage(0);
- std::atomic<bool> ordered_by_writethread(false);
- const int kMainThreadStartPersistingOptionsFile = 1;
- const int kChildThreadFinishDroppingColumnFamily = 2;
- void DropSingleColumnFamily(ColumnFamilyTest* cf_test, int cf_id,
- std::vector<Comparator*>* comparators) {
- while (test_stage < kMainThreadStartPersistingOptionsFile &&
- !ordered_by_writethread) {
- Env::Default()->SleepForMicroseconds(100);
- }
- cf_test->DropColumnFamilies({cf_id});
- if ((*comparators)[cf_id]) {
- delete (*comparators)[cf_id];
- (*comparators)[cf_id] = nullptr;
- }
- test_stage = kChildThreadFinishDroppingColumnFamily;
- }
- } // anonymous namespace
- // This test attempts to set up a race condition in a way that is no longer
- // possible, causing the test to hang. If DBImpl::options_mutex_ is removed
- // in the future, this test might become relevant again.
- TEST_P(ColumnFamilyTest, DISABLED_CreateAndDropRace) {
- const int kCfCount = 5;
- std::vector<ColumnFamilyOptions> cf_opts;
- std::vector<Comparator*> comparators;
- for (int i = 0; i < kCfCount; ++i) {
- cf_opts.emplace_back();
- comparators.push_back(new test::SimpleSuffixReverseComparator());
- cf_opts.back().comparator = comparators.back();
- }
- db_options_.create_if_missing = true;
- db_options_.create_missing_column_families = true;
- auto main_thread_id = std::this_thread::get_id();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
- "PersistRocksDBOptions:start", [&](void* /*arg*/) {
- auto current_thread_id = std::this_thread::get_id();
- // If it's the main thread hitting this sync-point, then it
- // will be blocked until some other thread update the test_stage.
- if (main_thread_id == current_thread_id) {
- test_stage = kMainThreadStartPersistingOptionsFile;
- while (test_stage < kChildThreadFinishDroppingColumnFamily &&
- !ordered_by_writethread) {
- Env::Default()->SleepForMicroseconds(100);
- }
- }
- });
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
- "WriteThread::EnterUnbatched:Wait", [&](void* /*arg*/) {
- // This means a thread doing DropColumnFamily() is waiting for
- // other thread to finish persisting options.
- // In such case, we update the test_stage to unblock the main thread.
- ordered_by_writethread = true;
- });
- // Create a database with four column families
- Open({"default", "one", "two", "three"},
- {cf_opts[0], cf_opts[1], cf_opts[2], cf_opts[3]});
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- // Start a thread that will drop the first column family
- // and its comparator
- ROCKSDB_NAMESPACE::port::Thread drop_cf_thread(DropSingleColumnFamily, this,
- 1, &comparators);
- DropColumnFamilies({2});
- drop_cf_thread.join();
- Close();
- Destroy();
- for (auto* comparator : comparators) {
- if (comparator) {
- delete comparator;
- }
- }
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- TEST_P(ColumnFamilyTest, CreateAndDropPeriodicRace) {
- // This is a mini-stress test looking for inconsistency between the set of
- // CFs in the DB, particularly whether any use preserve_internal_time_seconds,
- // and whether that is accurately reflected in the periodic task setup.
- constexpr size_t kNumThreads = 12;
- std::vector<std::thread> threads;
- bool last_cf_on = Random::GetTLSInstance()->OneIn(2);
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::RegisterRecordSeqnoTimeWorker:BeforePeriodicTaskType",
- [&](void* /*arg*/) { std::this_thread::yield(); });
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- ASSERT_EQ(column_family_options_.preserve_internal_time_seconds, 0U);
- ColumnFamilyOptions other_opts = column_family_options_;
- ColumnFamilyOptions last_opts = column_family_options_;
- (last_cf_on ? last_opts : other_opts).preserve_internal_time_seconds =
- 1000000;
- Open();
- for (size_t i = 0; i < kNumThreads; i++) {
- threads.emplace_back([this, &other_opts, i]() {
- ColumnFamilyHandle* cfh;
- ASSERT_OK(db_->CreateColumnFamily(other_opts, std::to_string(i), &cfh));
- ASSERT_OK(db_->DropColumnFamily(cfh));
- ASSERT_OK(db_->DestroyColumnFamilyHandle(cfh));
- });
- }
- ColumnFamilyHandle* last_cfh;
- ASSERT_OK(db_->CreateColumnFamily(last_opts, "last", &last_cfh));
- for (auto& t : threads) {
- t.join();
- }
- bool task_enabled = dbfull()->TEST_GetPeriodicTaskScheduler().TEST_HasTask(
- PeriodicTaskType::kRecordSeqnoTime);
- ASSERT_EQ(last_cf_on, task_enabled);
- ASSERT_OK(db_->DropColumnFamily(last_cfh));
- ASSERT_OK(db_->DestroyColumnFamilyHandle(last_cfh));
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- TEST_P(ColumnFamilyTest, WriteStallSingleColumnFamily) {
- const uint64_t kBaseRate = 800000u;
- db_options_.delayed_write_rate = kBaseRate;
- db_options_.max_background_compactions = 6;
- Open({"default"});
- ColumnFamilyData* cfd =
- static_cast<ColumnFamilyHandleImpl*>(db_->DefaultColumnFamily())->cfd();
- VersionStorageInfo* vstorage = cfd->current()->storage_info();
- MutableCFOptions mutable_cf_options(column_family_options_);
- mutable_cf_options.level0_slowdown_writes_trigger = 20;
- mutable_cf_options.level0_stop_writes_trigger = 10000;
- mutable_cf_options.soft_pending_compaction_bytes_limit = 200;
- mutable_cf_options.hard_pending_compaction_bytes_limit = 2000;
- mutable_cf_options.disable_auto_compactions = false;
- auto dbmu = dbfull()->TEST_Mutex();
- vstorage->TEST_set_estimated_compaction_needed_bytes(50, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(!dbfull()->TEST_write_controler().NeedsDelay());
- vstorage->TEST_set_estimated_compaction_needed_bytes(201, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate, GetDbDelayedWriteRate());
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->TEST_set_estimated_compaction_needed_bytes(400, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25, GetDbDelayedWriteRate());
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->TEST_set_estimated_compaction_needed_bytes(500, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25 / 1.25, GetDbDelayedWriteRate());
- vstorage->TEST_set_estimated_compaction_needed_bytes(450, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25, GetDbDelayedWriteRate());
- vstorage->TEST_set_estimated_compaction_needed_bytes(205, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate, GetDbDelayedWriteRate());
- vstorage->TEST_set_estimated_compaction_needed_bytes(202, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate, GetDbDelayedWriteRate());
- vstorage->TEST_set_estimated_compaction_needed_bytes(201, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate, GetDbDelayedWriteRate());
- vstorage->TEST_set_estimated_compaction_needed_bytes(198, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(!dbfull()->TEST_write_controler().NeedsDelay());
- vstorage->TEST_set_estimated_compaction_needed_bytes(399, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate, GetDbDelayedWriteRate());
- vstorage->TEST_set_estimated_compaction_needed_bytes(599, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25, GetDbDelayedWriteRate());
- vstorage->TEST_set_estimated_compaction_needed_bytes(2001, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(IsDbWriteStopped());
- ASSERT_TRUE(!dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->TEST_set_estimated_compaction_needed_bytes(3001, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(IsDbWriteStopped());
- ASSERT_TRUE(!dbfull()->TEST_write_controler().NeedsDelay());
- vstorage->TEST_set_estimated_compaction_needed_bytes(390, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25, GetDbDelayedWriteRate());
- vstorage->TEST_set_estimated_compaction_needed_bytes(100, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(!dbfull()->TEST_write_controler().NeedsDelay());
- vstorage->set_l0_delay_trigger_count(100);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate, GetDbDelayedWriteRate());
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->set_l0_delay_trigger_count(101);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25, GetDbDelayedWriteRate());
- vstorage->set_l0_delay_trigger_count(0);
- vstorage->TEST_set_estimated_compaction_needed_bytes(300, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25 / 1.25, GetDbDelayedWriteRate());
- vstorage->set_l0_delay_trigger_count(101);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25 / 1.25 / 1.25, GetDbDelayedWriteRate());
- vstorage->TEST_set_estimated_compaction_needed_bytes(200, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25 / 1.25, GetDbDelayedWriteRate());
- vstorage->set_l0_delay_trigger_count(0);
- vstorage->TEST_set_estimated_compaction_needed_bytes(0, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(!dbfull()->TEST_write_controler().NeedsDelay());
- mutable_cf_options.disable_auto_compactions = true;
- dbfull()->TEST_write_controler().set_delayed_write_rate(kBaseRate);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(!dbfull()->TEST_write_controler().NeedsDelay());
- vstorage->set_l0_delay_trigger_count(50);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(!dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(0, GetDbDelayedWriteRate());
- ASSERT_EQ(kBaseRate, dbfull()->TEST_write_controler().delayed_write_rate());
- vstorage->set_l0_delay_trigger_count(60);
- vstorage->TEST_set_estimated_compaction_needed_bytes(300, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(!dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(0, GetDbDelayedWriteRate());
- ASSERT_EQ(kBaseRate, dbfull()->TEST_write_controler().delayed_write_rate());
- mutable_cf_options.disable_auto_compactions = false;
- vstorage->set_l0_delay_trigger_count(70);
- vstorage->TEST_set_estimated_compaction_needed_bytes(500, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate, GetDbDelayedWriteRate());
- vstorage->set_l0_delay_trigger_count(71);
- vstorage->TEST_set_estimated_compaction_needed_bytes(501, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25, GetDbDelayedWriteRate());
- }
- TEST_P(ColumnFamilyTest, CompactionSpeedupSingleColumnFamily) {
- db_options_.max_background_compactions = 6;
- Open({"default"});
- ColumnFamilyData* cfd =
- static_cast<ColumnFamilyHandleImpl*>(db_->DefaultColumnFamily())->cfd();
- VersionStorageInfo* vstorage = cfd->current()->storage_info();
- MutableCFOptions mutable_cf_options(column_family_options_);
- // Speed up threshold = min(4 * 2, 4 + (36 - 4)/4) = 8
- mutable_cf_options.level0_file_num_compaction_trigger = 4;
- mutable_cf_options.level0_slowdown_writes_trigger = 36;
- mutable_cf_options.level0_stop_writes_trigger = 50;
- // Speedup threshold = 200 / 4 = 50
- mutable_cf_options.soft_pending_compaction_bytes_limit = 200;
- mutable_cf_options.hard_pending_compaction_bytes_limit = 2000;
- auto dbmu = dbfull()->TEST_Mutex();
- vstorage->TEST_set_estimated_compaction_needed_bytes(40, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->TEST_set_estimated_compaction_needed_bytes(50, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->TEST_set_estimated_compaction_needed_bytes(300, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->TEST_set_estimated_compaction_needed_bytes(45, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->set_l0_delay_trigger_count(7);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->set_l0_delay_trigger_count(9);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->set_l0_delay_trigger_count(6);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- // Speed up threshold = min(4 * 2, 4 + (12 - 4)/4) = 6
- mutable_cf_options.level0_file_num_compaction_trigger = 4;
- mutable_cf_options.level0_slowdown_writes_trigger = 16;
- mutable_cf_options.level0_stop_writes_trigger = 30;
- vstorage->set_l0_delay_trigger_count(5);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->set_l0_delay_trigger_count(7);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->set_l0_delay_trigger_count(3);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- }
- TEST_P(ColumnFamilyTest, WriteStallTwoColumnFamilies) {
- const uint64_t kBaseRate = 810000u;
- db_options_.delayed_write_rate = kBaseRate;
- Open();
- CreateColumnFamilies({"one"});
- ColumnFamilyData* cfd =
- static_cast<ColumnFamilyHandleImpl*>(db_->DefaultColumnFamily())->cfd();
- VersionStorageInfo* vstorage = cfd->current()->storage_info();
- ColumnFamilyData* cfd1 =
- static_cast<ColumnFamilyHandleImpl*>(handles_[1])->cfd();
- VersionStorageInfo* vstorage1 = cfd1->current()->storage_info();
- MutableCFOptions mutable_cf_options(column_family_options_);
- mutable_cf_options.level0_slowdown_writes_trigger = 20;
- mutable_cf_options.level0_stop_writes_trigger = 10000;
- mutable_cf_options.soft_pending_compaction_bytes_limit = 200;
- mutable_cf_options.hard_pending_compaction_bytes_limit = 2000;
- MutableCFOptions mutable_cf_options1 = mutable_cf_options;
- mutable_cf_options1.soft_pending_compaction_bytes_limit = 500;
- auto dbmu = dbfull()->TEST_Mutex();
- vstorage->TEST_set_estimated_compaction_needed_bytes(50, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(!dbfull()->TEST_write_controler().NeedsDelay());
- vstorage1->TEST_set_estimated_compaction_needed_bytes(201, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(!dbfull()->TEST_write_controler().NeedsDelay());
- vstorage1->TEST_set_estimated_compaction_needed_bytes(600, dbmu);
- RecalculateWriteStallConditions(cfd1, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate, GetDbDelayedWriteRate());
- vstorage->TEST_set_estimated_compaction_needed_bytes(70, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate, GetDbDelayedWriteRate());
- vstorage1->TEST_set_estimated_compaction_needed_bytes(800, dbmu);
- RecalculateWriteStallConditions(cfd1, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25, GetDbDelayedWriteRate());
- vstorage->TEST_set_estimated_compaction_needed_bytes(300, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25 / 1.25, GetDbDelayedWriteRate());
- vstorage1->TEST_set_estimated_compaction_needed_bytes(700, dbmu);
- RecalculateWriteStallConditions(cfd1, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25, GetDbDelayedWriteRate());
- vstorage->TEST_set_estimated_compaction_needed_bytes(500, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25 / 1.25, GetDbDelayedWriteRate());
- vstorage1->TEST_set_estimated_compaction_needed_bytes(600, dbmu);
- RecalculateWriteStallConditions(cfd1, mutable_cf_options);
- ASSERT_TRUE(!IsDbWriteStopped());
- ASSERT_TRUE(dbfull()->TEST_write_controler().NeedsDelay());
- ASSERT_EQ(kBaseRate / 1.25, GetDbDelayedWriteRate());
- }
- TEST_P(ColumnFamilyTest, CompactionSpeedupTwoColumnFamilies) {
- db_options_.max_background_compactions = 6;
- column_family_options_.soft_pending_compaction_bytes_limit = 200;
- column_family_options_.hard_pending_compaction_bytes_limit = 2000;
- Open();
- CreateColumnFamilies({"one"});
- ColumnFamilyData* cfd =
- static_cast<ColumnFamilyHandleImpl*>(db_->DefaultColumnFamily())->cfd();
- VersionStorageInfo* vstorage = cfd->current()->storage_info();
- ColumnFamilyData* cfd1 =
- static_cast<ColumnFamilyHandleImpl*>(handles_[1])->cfd();
- VersionStorageInfo* vstorage1 = cfd1->current()->storage_info();
- MutableCFOptions mutable_cf_options(column_family_options_);
- // Speed up threshold = min(4 * 2, 4 + (36 - 4)/4) = 8
- mutable_cf_options.level0_file_num_compaction_trigger = 4;
- mutable_cf_options.level0_slowdown_writes_trigger = 36;
- mutable_cf_options.level0_stop_writes_trigger = 30;
- // Speedup threshold = 200 / 4 = 50
- mutable_cf_options.soft_pending_compaction_bytes_limit = 200;
- mutable_cf_options.hard_pending_compaction_bytes_limit = 2000;
- MutableCFOptions mutable_cf_options1 = mutable_cf_options;
- mutable_cf_options1.level0_slowdown_writes_trigger = 16;
- auto dbmu = dbfull()->TEST_Mutex();
- vstorage->TEST_set_estimated_compaction_needed_bytes(40, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->TEST_set_estimated_compaction_needed_bytes(60, dbmu);
- RecalculateWriteStallConditions(cfd1, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage1->TEST_set_estimated_compaction_needed_bytes(30, dbmu);
- RecalculateWriteStallConditions(cfd1, mutable_cf_options);
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage1->TEST_set_estimated_compaction_needed_bytes(70, dbmu);
- RecalculateWriteStallConditions(cfd1, mutable_cf_options);
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->TEST_set_estimated_compaction_needed_bytes(20, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage1->TEST_set_estimated_compaction_needed_bytes(3, dbmu);
- RecalculateWriteStallConditions(cfd1, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->set_l0_delay_trigger_count(9);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage1->set_l0_delay_trigger_count(2);
- RecalculateWriteStallConditions(cfd1, mutable_cf_options);
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->set_l0_delay_trigger_count(0);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- }
- TEST_P(ColumnFamilyTest, CompactionSpeedupForCompactionDebt) {
- db_options_.max_background_compactions = 6;
- Open();
- ColumnFamilyData* cfd =
- static_cast<ColumnFamilyHandleImpl*>(db_->DefaultColumnFamily())->cfd();
- MutableCFOptions mutable_cf_options(column_family_options_);
- mutable_cf_options.soft_pending_compaction_bytes_limit =
- std::numeric_limits<uint64_t>::max();
- auto dbmu = dbfull()->TEST_Mutex();
- {
- // No bottommost data, so debt ratio cannot trigger speedup.
- VersionStorageInfo* vstorage = cfd->current()->storage_info();
- vstorage->TEST_set_estimated_compaction_needed_bytes(1048576 /* 1MB */,
- dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- }
- // Add a tiny amount of bottommost data.
- ASSERT_OK(db_->Put(WriteOptions(), "foo", "bar"));
- ASSERT_OK(db_->Flush(FlushOptions()));
- {
- VersionStorageInfo* vstorage = cfd->current()->storage_info();
- // Eight bytes is way smaller than bottommost data so definitely does not
- // trigger speedup.
- vstorage->TEST_set_estimated_compaction_needed_bytes(8, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- // 1MB is much larger than bottommost level size. However, since it's too
- // small in terms of absolute size, it does not trigger parallel compaction
- // in this case (see GetPendingCompactionBytesForCompactionSpeedup()).
- vstorage->TEST_set_estimated_compaction_needed_bytes(1048576 /* 1MB */,
- dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- vstorage->TEST_set_estimated_compaction_needed_bytes(
- 2 * mutable_cf_options.max_bytes_for_level_base, dbmu);
- RecalculateWriteStallConditions(cfd, mutable_cf_options);
- ASSERT_EQ(6, dbfull()->TEST_BGCompactionsAllowed());
- }
- }
- TEST_P(ColumnFamilyTest, CompactionSpeedupForMarkedFiles) {
- const int kParallelismLimit = 3;
- class AlwaysCompactTpc : public TablePropertiesCollector {
- public:
- Status Finish(UserCollectedProperties* /* properties */) override {
- return Status::OK();
- }
- UserCollectedProperties GetReadableProperties() const override {
- return UserCollectedProperties{};
- }
- const char* Name() const override { return "AlwaysCompactTpc"; }
- bool NeedCompact() const override { return true; }
- };
- class AlwaysCompactTpcf : public TablePropertiesCollectorFactory {
- public:
- TablePropertiesCollector* CreateTablePropertiesCollector(
- TablePropertiesCollectorFactory::Context /* context */) override {
- return new AlwaysCompactTpc();
- }
- const char* Name() const override { return "AlwaysCompactTpcf"; }
- };
- column_family_options_.num_levels = 2;
- column_family_options_.table_properties_collector_factories.emplace_back(
- std::make_shared<AlwaysCompactTpcf>());
- db_options_.max_background_compactions = kParallelismLimit;
- Open();
- // Make a nonempty last level. Only marked files in upper levels count.
- ASSERT_OK(db_->Put(WriteOptions(), "foo", "bar"));
- ASSERT_OK(db_->Flush(FlushOptions()));
- WaitForCompaction();
- AssertFilesPerLevel("0,1", 0 /* cf */);
- // We should calculate the limit by obtaining the number of env background
- // threads, because the current test case will share the same env
- // with another case that may have already increased the number of
- // background threads which is larger than kParallelismLimit
- const auto limit = env_->GetBackgroundThreads(Env::Priority::LOW);
- // Block the compaction thread pool so marked files accumulate in L0.
- std::vector<std::shared_ptr<test::SleepingBackgroundTask>> sleeping_tasks;
- for (int i = 0; i < limit; i++) {
- sleeping_tasks.emplace_back(
- std::make_shared<test::SleepingBackgroundTask>());
- env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask,
- sleeping_tasks[i].get(), Env::Priority::LOW);
- sleeping_tasks[i]->WaitUntilSleeping();
- }
- // Zero marked upper-level files. No speedup.
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- AssertFilesPerLevel("0,1", 0 /* cf */);
- // One marked upper-level file. No speedup.
- ASSERT_OK(db_->Put(WriteOptions(), "foo", "bar"));
- ASSERT_OK(db_->Flush(FlushOptions()));
- ASSERT_EQ(1, dbfull()->TEST_BGCompactionsAllowed());
- AssertFilesPerLevel("1,1", 0 /* cf */);
- // Two marked upper-level files. Speedup.
- ASSERT_OK(db_->Put(WriteOptions(), "foo", "bar"));
- ASSERT_OK(db_->Flush(FlushOptions()));
- ASSERT_EQ(kParallelismLimit, dbfull()->TEST_BGCompactionsAllowed());
- AssertFilesPerLevel("2,1", 0 /* cf */);
- for (int i = 0; i < limit; i++) {
- sleeping_tasks[i]->WakeUp();
- sleeping_tasks[i]->WaitUntilDone();
- }
- }
- TEST_P(ColumnFamilyTest, CreateAndDestroyOptions) {
- std::unique_ptr<ColumnFamilyOptions> cfo(new ColumnFamilyOptions());
- ColumnFamilyHandle* cfh;
- Open();
- ASSERT_OK(db_->CreateColumnFamily(*(cfo.get()), "yoyo", &cfh));
- cfo.reset();
- ASSERT_OK(db_->Put(WriteOptions(), cfh, "foo", "bar"));
- ASSERT_OK(db_->Flush(FlushOptions(), cfh));
- ASSERT_OK(db_->DropColumnFamily(cfh));
- ASSERT_OK(db_->DestroyColumnFamilyHandle(cfh));
- }
- TEST_P(ColumnFamilyTest, CreateDropAndDestroy) {
- ColumnFamilyHandle* cfh;
- Open();
- ASSERT_OK(db_->CreateColumnFamily(ColumnFamilyOptions(), "yoyo", &cfh));
- ASSERT_OK(db_->Put(WriteOptions(), cfh, "foo", "bar"));
- ASSERT_OK(db_->Flush(FlushOptions(), cfh));
- ASSERT_OK(db_->DropColumnFamily(cfh));
- ASSERT_OK(db_->DestroyColumnFamilyHandle(cfh));
- }
- TEST_P(ColumnFamilyTest, CreateDropAndDestroyWithoutFileDeletion) {
- ColumnFamilyHandle* cfh;
- Open();
- ASSERT_OK(db_->CreateColumnFamily(ColumnFamilyOptions(), "yoyo", &cfh));
- ASSERT_OK(db_->Put(WriteOptions(), cfh, "foo", "bar"));
- ASSERT_OK(db_->Flush(FlushOptions(), cfh));
- ASSERT_OK(db_->DisableFileDeletions());
- ASSERT_OK(db_->DropColumnFamily(cfh));
- ASSERT_OK(db_->DestroyColumnFamilyHandle(cfh));
- }
- TEST_P(ColumnFamilyTest, FlushCloseWALFiles) {
- SpecialEnv env(Env::Default());
- db_options_.env = &env;
- db_options_.max_background_flushes = 1;
- column_family_options_.memtable_factory.reset(
- test::NewSpecialSkipListFactory(2));
- Open();
- CreateColumnFamilies({"one"});
- ASSERT_OK(Put(1, "fodor", "mirko"));
- ASSERT_OK(Put(0, "fodor", "mirko"));
- ASSERT_OK(Put(1, "fodor", "mirko"));
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
- {"DBImpl::BGWorkFlush:done", "FlushCloseWALFiles:0"},
- });
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- // Block flush jobs from running
- test::SleepingBackgroundTask sleeping_task;
- env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &sleeping_task,
- Env::Priority::HIGH);
- // Make sure the task is sleeping. Otherwise, it might start to execute
- // after sleeping_task.WaitUntilDone() and cause TSAN warning.
- sleeping_task.WaitUntilSleeping();
- WriteOptions wo;
- wo.sync = true;
- ASSERT_OK(db_->Put(wo, handles_[1], "fodor", "mirko"));
- ASSERT_EQ(2, env.num_open_wal_file_.load());
- sleeping_task.WakeUp();
- sleeping_task.WaitUntilDone();
- TEST_SYNC_POINT("FlushCloseWALFiles:0");
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- ASSERT_EQ(1, env.num_open_wal_file_.load());
- Reopen();
- ASSERT_EQ("mirko", Get(0, "fodor"));
- ASSERT_EQ("mirko", Get(1, "fodor"));
- db_options_.env = env_;
- Close();
- }
- TEST_P(ColumnFamilyTest, IteratorCloseWALFile1) {
- SpecialEnv env(Env::Default());
- db_options_.env = &env;
- db_options_.max_background_flushes = 1;
- // When this option is removed, the test will need re-engineering
- db_options_.background_close_inactive_wals = true;
- column_family_options_.memtable_factory.reset(
- test::NewSpecialSkipListFactory(2));
- Open();
- CreateColumnFamilies({"one"});
- ASSERT_OK(Put(1, "fodor", "mirko"));
- // Create an iterator holding the current super version.
- Iterator* it = db_->NewIterator(ReadOptions(), handles_[1]);
- ASSERT_OK(it->status());
- // A flush will make `it` hold the last reference of its super version.
- ASSERT_OK(Flush(1));
- ASSERT_OK(Put(1, "fodor", "mirko"));
- ASSERT_OK(Put(0, "fodor", "mirko"));
- ASSERT_OK(Put(1, "fodor", "mirko"));
- // Flush jobs will close previous WAL files after finishing. By
- // block flush jobs from running, we trigger a condition where
- // the iterator destructor should close the WAL files.
- test::SleepingBackgroundTask sleeping_task;
- env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &sleeping_task,
- Env::Priority::HIGH);
- // Make sure the task is sleeping. Otherwise, it might start to execute
- // after sleeping_task.WaitUntilDone() and cause TSAN warning.
- sleeping_task.WaitUntilSleeping();
- WriteOptions wo;
- wo.sync = true;
- ASSERT_OK(db_->Put(wo, handles_[1], "fodor", "mirko"));
- ASSERT_EQ(2, env.num_open_wal_file_.load());
- // Deleting the iterator will clear its super version, triggering
- // closing all files
- delete it;
- ASSERT_EQ(1, env.num_open_wal_file_.load());
- sleeping_task.WakeUp();
- sleeping_task.WaitUntilDone();
- WaitForFlush(1);
- Reopen();
- ASSERT_EQ("mirko", Get(0, "fodor"));
- ASSERT_EQ("mirko", Get(1, "fodor"));
- db_options_.env = env_;
- Close();
- }
- TEST_P(ColumnFamilyTest, IteratorCloseWALFile2) {
- SpecialEnv env(Env::Default());
- // Allow both of flush and purge job to schedule.
- env.SetBackgroundThreads(2, Env::HIGH);
- db_options_.env = &env;
- db_options_.max_background_flushes = 1;
- // When this option is removed, the test will need re-engineering
- db_options_.background_close_inactive_wals = true;
- column_family_options_.memtable_factory.reset(
- test::NewSpecialSkipListFactory(2));
- Open();
- CreateColumnFamilies({"one"});
- ASSERT_OK(Put(1, "fodor", "mirko"));
- // Create an iterator holding the current super version.
- ReadOptions ro;
- ro.background_purge_on_iterator_cleanup = true;
- Iterator* it = db_->NewIterator(ro, handles_[1]);
- ASSERT_OK(it->status());
- // A flush will make `it` hold the last reference of its super version.
- ASSERT_OK(Flush(1));
- ASSERT_OK(Put(1, "fodor", "mirko"));
- ASSERT_OK(Put(0, "fodor", "mirko"));
- ASSERT_OK(Put(1, "fodor", "mirko"));
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
- {"ColumnFamilyTest::IteratorCloseWALFile2:0",
- "DBImpl::BGWorkPurge:start"},
- {"ColumnFamilyTest::IteratorCloseWALFile2:2",
- "DBImpl::BackgroundCallFlush:start"},
- {"DBImpl::BGWorkPurge:end", "ColumnFamilyTest::IteratorCloseWALFile2:1"},
- });
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- WriteOptions wo;
- wo.sync = true;
- ASSERT_OK(db_->Put(wo, handles_[1], "fodor", "mirko"));
- ASSERT_EQ(2, env.num_open_wal_file_.load());
- // Deleting the iterator will clear its super version, triggering
- // closing all files
- delete it;
- ASSERT_EQ(2, env.num_open_wal_file_.load());
- TEST_SYNC_POINT("ColumnFamilyTest::IteratorCloseWALFile2:0");
- TEST_SYNC_POINT("ColumnFamilyTest::IteratorCloseWALFile2:1");
- ASSERT_EQ(1, env.num_open_wal_file_.load());
- TEST_SYNC_POINT("ColumnFamilyTest::IteratorCloseWALFile2:2");
- WaitForFlush(1);
- ASSERT_EQ(1, env.num_open_wal_file_.load());
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- Reopen();
- ASSERT_EQ("mirko", Get(0, "fodor"));
- ASSERT_EQ("mirko", Get(1, "fodor"));
- db_options_.env = env_;
- Close();
- }
- TEST_P(ColumnFamilyTest, ForwardIteratorCloseWALFile) {
- SpecialEnv env(Env::Default());
- // Allow both of flush and purge job to schedule.
- env.SetBackgroundThreads(2, Env::HIGH);
- db_options_.env = &env;
- db_options_.max_background_flushes = 1;
- // When this option is removed, the test will need re-engineering
- db_options_.background_close_inactive_wals = true;
- column_family_options_.memtable_factory.reset(
- test::NewSpecialSkipListFactory(3));
- column_family_options_.level0_file_num_compaction_trigger = 2;
- Open();
- CreateColumnFamilies({"one"});
- ASSERT_OK(Put(1, "fodor", "mirko"));
- ASSERT_OK(Put(1, "fodar2", "mirko"));
- ASSERT_OK(Flush(1));
- // Create an iterator holding the current super version, as well as
- // the SST file just flushed.
- ReadOptions ro;
- ro.tailing = true;
- ro.background_purge_on_iterator_cleanup = true;
- Iterator* it = db_->NewIterator(ro, handles_[1]);
- // A flush will make `it` hold the last reference of its super version.
- ASSERT_OK(Put(1, "fodor", "mirko"));
- ASSERT_OK(Put(1, "fodar2", "mirko"));
- ASSERT_OK(Flush(1));
- WaitForCompaction();
- ASSERT_OK(Put(1, "fodor", "mirko"));
- ASSERT_OK(Put(1, "fodor", "mirko"));
- ASSERT_OK(Put(0, "fodor", "mirko"));
- ASSERT_OK(Put(1, "fodor", "mirko"));
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
- {"ColumnFamilyTest::IteratorCloseWALFile2:0",
- "DBImpl::BGWorkPurge:start"},
- {"ColumnFamilyTest::IteratorCloseWALFile2:2",
- "DBImpl::BackgroundCallFlush:start"},
- {"DBImpl::BGWorkPurge:end", "ColumnFamilyTest::IteratorCloseWALFile2:1"},
- });
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- WriteOptions wo;
- wo.sync = true;
- ASSERT_OK(db_->Put(wo, handles_[1], "fodor", "mirko"));
- env.delete_count_.store(0);
- ASSERT_EQ(2, env.num_open_wal_file_.load());
- // Deleting the iterator will clear its super version, triggering
- // closing all files
- it->Seek("");
- ASSERT_OK(it->status());
- ASSERT_EQ(2, env.num_open_wal_file_.load());
- ASSERT_EQ(0, env.delete_count_.load());
- TEST_SYNC_POINT("ColumnFamilyTest::IteratorCloseWALFile2:0");
- TEST_SYNC_POINT("ColumnFamilyTest::IteratorCloseWALFile2:1");
- ASSERT_EQ(1, env.num_open_wal_file_.load());
- ASSERT_EQ(1, env.delete_count_.load());
- TEST_SYNC_POINT("ColumnFamilyTest::IteratorCloseWALFile2:2");
- WaitForFlush(1);
- ASSERT_EQ(1, env.num_open_wal_file_.load());
- ASSERT_EQ(1, env.delete_count_.load());
- delete it;
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- Reopen();
- ASSERT_EQ("mirko", Get(0, "fodor"));
- ASSERT_EQ("mirko", Get(1, "fodor"));
- db_options_.env = env_;
- Close();
- }
- // Disable on windows because SyncWAL requires env->IsSyncThreadSafe()
- // to return true which is not so in unbuffered mode.
- #ifndef OS_WIN
- TEST_P(ColumnFamilyTest, LogSyncConflictFlush) {
- Open();
- CreateColumnFamiliesAndReopen({"one", "two"});
- ASSERT_OK(Put(0, "", ""));
- ASSERT_OK(Put(1, "foo", "bar"));
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
- {{"DBImpl::SyncWAL:BeforeMarkLogsSynced:1",
- "ColumnFamilyTest::LogSyncConflictFlush:1"},
- {"ColumnFamilyTest::LogSyncConflictFlush:2",
- "DBImpl::SyncWAL:BeforeMarkLogsSynced:2"}});
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
- ROCKSDB_NAMESPACE::port::Thread thread([&] { ASSERT_OK(db_->SyncWAL()); });
- TEST_SYNC_POINT("ColumnFamilyTest::LogSyncConflictFlush:1");
- ASSERT_OK(Flush(1));
- ASSERT_OK(Put(1, "foo", "bar"));
- ASSERT_OK(Flush(1));
- TEST_SYNC_POINT("ColumnFamilyTest::LogSyncConflictFlush:2");
- thread.join();
- ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
- Close();
- }
- #endif
- // this test is placed here, because the infrastructure for Column Family
- // test is being used to ensure a roll of wal files.
- // Basic idea is to test that WAL truncation is being detected and not
- // ignored
- TEST_P(ColumnFamilyTest, DISABLED_LogTruncationTest) {
- Open();
- CreateColumnFamiliesAndReopen({"one", "two"});
- Build(0, 100);
- // Flush the 0th column family to force a roll of the wal log
- ASSERT_OK(Flush(0));
- // Add some more entries
- Build(100, 100);
- std::vector<std::string> filenames;
- ASSERT_OK(env_->GetChildren(dbname_, &filenames));
- // collect wal files
- std::vector<std::string> logfs;
- for (size_t i = 0; i < filenames.size(); i++) {
- uint64_t number;
- FileType type;
- if (!(ParseFileName(filenames[i], &number, &type))) {
- continue;
- }
- if (type != kWalFile) {
- continue;
- }
- logfs.push_back(filenames[i]);
- }
- std::sort(logfs.begin(), logfs.end());
- ASSERT_GE(logfs.size(), 2);
- // Take the last but one file, and truncate it
- std::string fpath = dbname_ + "/" + logfs[logfs.size() - 2];
- std::vector<std::string> names_save = names_;
- uint64_t fsize;
- ASSERT_OK(env_->GetFileSize(fpath, &fsize));
- ASSERT_GT(fsize, 0);
- Close();
- std::string backup_logs = dbname_ + "/backup_logs";
- std::string t_fpath = backup_logs + "/" + logfs[logfs.size() - 2];
- ASSERT_OK(env_->CreateDirIfMissing(backup_logs));
- // Not sure how easy it is to make this data driven.
- // need to read back the WAL file and truncate last 10
- // entries
- CopyFile(fpath, t_fpath, fsize - 9180);
- ASSERT_OK(env_->DeleteFile(fpath));
- ASSERT_OK(env_->RenameFile(t_fpath, fpath));
- db_options_.wal_recovery_mode = WALRecoveryMode::kPointInTimeRecovery;
- OpenReadOnly(names_save);
- CheckMissed();
- Close();
- Open(names_save);
- CheckMissed();
- Close();
- // cleanup
- ASSERT_OK(env_->DeleteDir(backup_logs));
- }
- TEST_P(ColumnFamilyTest, DefaultCfPathsTest) {
- Open();
- // Leave cf_paths for one column families to be empty.
- // Files should be generated according to db_paths for that
- // column family.
- ColumnFamilyOptions cf_opt1, cf_opt2;
- cf_opt1.cf_paths.emplace_back(dbname_ + "_one_1",
- std::numeric_limits<uint64_t>::max());
- CreateColumnFamilies({"one", "two"}, {cf_opt1, cf_opt2});
- Reopen({ColumnFamilyOptions(), cf_opt1, cf_opt2});
- // Fill Column family 1.
- PutRandomData(1, 100, 100);
- ASSERT_OK(Flush(1));
- ASSERT_EQ(1, GetSstFileCount(cf_opt1.cf_paths[0].path));
- ASSERT_EQ(0, GetSstFileCount(dbname_));
- // Fill column family 2
- PutRandomData(2, 100, 100);
- ASSERT_OK(Flush(2));
- // SST from Column family 2 should be generated in
- // db_paths which is dbname_ in this case.
- ASSERT_EQ(1, GetSstFileCount(dbname_));
- }
- TEST_P(ColumnFamilyTest, MultipleCFPathsTest) {
- Open();
- // Configure Column family specific paths.
- ColumnFamilyOptions cf_opt1, cf_opt2;
- cf_opt1.cf_paths.emplace_back(dbname_ + "_one_1",
- std::numeric_limits<uint64_t>::max());
- cf_opt2.cf_paths.emplace_back(dbname_ + "_two_1",
- std::numeric_limits<uint64_t>::max());
- CreateColumnFamilies({"one", "two"}, {cf_opt1, cf_opt2});
- Reopen({ColumnFamilyOptions(), cf_opt1, cf_opt2});
- PutRandomData(1, 100, 100, true /* save */);
- ASSERT_OK(Flush(1));
- // Check that files are generated in appropriate paths.
- ASSERT_EQ(1, GetSstFileCount(cf_opt1.cf_paths[0].path));
- ASSERT_EQ(0, GetSstFileCount(dbname_));
- PutRandomData(2, 100, 100, true /* save */);
- ASSERT_OK(Flush(2));
- ASSERT_EQ(1, GetSstFileCount(cf_opt2.cf_paths[0].path));
- ASSERT_EQ(0, GetSstFileCount(dbname_));
- // Re-open and verify the keys.
- Reopen({ColumnFamilyOptions(), cf_opt1, cf_opt2});
- DBImpl* dbi = static_cast_with_check<DBImpl>(db_);
- for (int cf = 1; cf != 3; ++cf) {
- ReadOptions read_options;
- read_options.readahead_size = 0;
- auto it = dbi->NewIterator(read_options, handles_[cf]);
- for (it->SeekToFirst(); it->Valid(); it->Next()) {
- ASSERT_OK(it->status());
- Slice key(it->key());
- ASSERT_NE(keys_[cf].end(), keys_[cf].find(key.ToString()));
- }
- ASSERT_OK(it->status());
- delete it;
- for (const auto& key : keys_[cf]) {
- ASSERT_NE("NOT_FOUND", Get(cf, key));
- }
- }
- }
- TEST(ColumnFamilyTest, ValidateBlobGCCutoff) {
- DBOptions db_options;
- ColumnFamilyOptions cf_options;
- cf_options.enable_blob_garbage_collection = true;
- cf_options.blob_garbage_collection_age_cutoff = -0.5;
- ASSERT_TRUE(ColumnFamilyData::ValidateOptions(db_options, cf_options)
- .IsInvalidArgument());
- cf_options.blob_garbage_collection_age_cutoff = 0.0;
- ASSERT_OK(ColumnFamilyData::ValidateOptions(db_options, cf_options));
- cf_options.blob_garbage_collection_age_cutoff = 0.5;
- ASSERT_OK(ColumnFamilyData::ValidateOptions(db_options, cf_options));
- cf_options.blob_garbage_collection_age_cutoff = 1.0;
- ASSERT_OK(ColumnFamilyData::ValidateOptions(db_options, cf_options));
- cf_options.blob_garbage_collection_age_cutoff = 1.5;
- ASSERT_TRUE(ColumnFamilyData::ValidateOptions(db_options, cf_options)
- .IsInvalidArgument());
- }
- TEST(ColumnFamilyTest, ValidateBlobGCForceThreshold) {
- DBOptions db_options;
- ColumnFamilyOptions cf_options;
- cf_options.enable_blob_garbage_collection = true;
- cf_options.blob_garbage_collection_force_threshold = -0.5;
- ASSERT_TRUE(ColumnFamilyData::ValidateOptions(db_options, cf_options)
- .IsInvalidArgument());
- cf_options.blob_garbage_collection_force_threshold = 0.0;
- ASSERT_OK(ColumnFamilyData::ValidateOptions(db_options, cf_options));
- cf_options.blob_garbage_collection_force_threshold = 0.5;
- ASSERT_OK(ColumnFamilyData::ValidateOptions(db_options, cf_options));
- cf_options.blob_garbage_collection_force_threshold = 1.0;
- ASSERT_OK(ColumnFamilyData::ValidateOptions(db_options, cf_options));
- cf_options.blob_garbage_collection_force_threshold = 1.5;
- ASSERT_TRUE(ColumnFamilyData::ValidateOptions(db_options, cf_options)
- .IsInvalidArgument());
- }
- TEST(ColumnFamilyTest, ValidateMemtableKVChecksumOption) {
- DBOptions db_options;
- ColumnFamilyOptions cf_options;
- ASSERT_OK(ColumnFamilyData::ValidateOptions(db_options, cf_options));
- cf_options.memtable_protection_bytes_per_key = 5;
- ASSERT_TRUE(ColumnFamilyData::ValidateOptions(db_options, cf_options)
- .IsNotSupported());
- cf_options.memtable_protection_bytes_per_key = 1;
- ASSERT_OK(ColumnFamilyData::ValidateOptions(db_options, cf_options));
- cf_options.memtable_protection_bytes_per_key = 16;
- ASSERT_TRUE(ColumnFamilyData::ValidateOptions(db_options, cf_options)
- .IsNotSupported());
- cf_options.memtable_protection_bytes_per_key = 0;
- ASSERT_OK(ColumnFamilyData::ValidateOptions(db_options, cf_options));
- }
- // Tests the flushing behavior of a column family to retain user-defined
- // timestamp when `persist_user_defined_timestamp` is false. The behavior of
- // auto flush is it makes some effort to retain user-defined timestamps while
- // the behavior of manual flush is that it skips retaining UDTs.
- class ColumnFamilyRetainUDTTest : public ColumnFamilyTestBase {
- public:
- ColumnFamilyRetainUDTTest() : ColumnFamilyTestBase(kLatestFormatVersion) {}
- void SetUp() override {
- db_options_.allow_concurrent_memtable_write = false;
- column_family_options_.comparator =
- test::BytewiseComparatorWithU64TsWrapper();
- column_family_options_.persist_user_defined_timestamps = false;
- ColumnFamilyTestBase::SetUp();
- }
- Status Put(int cf, const std::string& key, const std::string& ts,
- const std::string& value) {
- return db_->Put(WriteOptions(), handles_[cf], Slice(key), Slice(ts),
- Slice(value));
- }
- std::string Get(int cf, const std::string& key, const std::string& read_ts) {
- ReadOptions ropts;
- Slice timestamp = read_ts;
- ropts.timestamp = ×tamp;
- std::string value;
- Status s = db_->Get(ropts, handles_[cf], Slice(key), &value);
- if (s.IsNotFound()) {
- return "NOT_FOUND";
- } else if (s.ok()) {
- return value;
- }
- return "";
- }
- void CheckEffectiveCutoffTime(uint64_t expected_cutoff) {
- std::string effective_full_history_ts_low;
- EXPECT_OK(
- db_->GetFullHistoryTsLow(handles_[0], &effective_full_history_ts_low));
- EXPECT_EQ(EncodeAsUint64(expected_cutoff), effective_full_history_ts_low);
- }
- };
- class TestTsComparator : public Comparator {
- public:
- TestTsComparator() : Comparator(8 /*ts_sz*/) {}
- int Compare(const ROCKSDB_NAMESPACE::Slice& /*a*/,
- const ROCKSDB_NAMESPACE::Slice& /*b*/) const override {
- return 0;
- }
- const char* Name() const override { return "TestTs"; }
- void FindShortestSeparator(
- std::string* /*start*/,
- const ROCKSDB_NAMESPACE::Slice& /*limit*/) const override {}
- void FindShortSuccessor(std::string* /*key*/) const override {}
- };
- TEST_F(ColumnFamilyRetainUDTTest, SanityCheck) {
- Open();
- ColumnFamilyOptions cf_options;
- cf_options.persist_user_defined_timestamps = false;
- TestTsComparator test_comparator;
- cf_options.comparator = &test_comparator;
- ColumnFamilyHandle* handle;
- // Not persisting user-defined timestamps feature only supports user-defined
- // timestamps formatted as uint64_t.
- ASSERT_TRUE(
- db_->CreateColumnFamily(cf_options, "pikachu", &handle).IsNotSupported());
- Destroy();
- // Not persisting user-defined timestamps feature doesn't work in combination
- // with atomic flush.
- db_options_.atomic_flush = true;
- ASSERT_TRUE(TryOpen({"default"}).IsNotSupported());
- // Not persisting user-defined timestamps feature doesn't work in combination
- // with concurrent memtable write.
- db_options_.atomic_flush = false;
- db_options_.allow_concurrent_memtable_write = true;
- ASSERT_TRUE(TryOpen({"default"}).IsNotSupported());
- Close();
- }
- class AutoFlushRetainUDTTest : public ColumnFamilyRetainUDTTest {};
- TEST_F(AutoFlushRetainUDTTest, FullHistoryTsLowNotSet) {
- SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::BackgroundFlush:CheckFlushRequest:cb", [&](void* arg) {
- ASSERT_NE(nullptr, arg);
- auto reschedule_count = *static_cast<int*>(arg);
- ASSERT_EQ(1, reschedule_count);
- });
- SyncPoint::GetInstance()->EnableProcessing();
- Open();
- ASSERT_OK(Put(0, "foo", EncodeAsUint64(1), "v1"));
- // No `full_history_ts_low` explicitly set by user, auto flush is continued
- // without checking if its UDTs expired.
- ASSERT_OK(dbfull()->TEST_SwitchWAL());
- ASSERT_OK(dbfull()->TEST_WaitForFlushMemTable());
- // After flush, `full_history_ts_low` should be automatically advanced to
- // the effective cutoff timestamp: write_ts + 1
- CheckEffectiveCutoffTime(2);
- Close();
- SyncPoint::GetInstance()->DisableProcessing();
- SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- TEST_F(AutoFlushRetainUDTTest, AllKeysExpired) {
- SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::BackgroundFlush:CheckFlushRequest:cb", [&](void* arg) {
- ASSERT_NE(nullptr, arg);
- auto reschedule_count = *static_cast<int*>(arg);
- ASSERT_EQ(1, reschedule_count);
- });
- SyncPoint::GetInstance()->EnableProcessing();
- Open();
- ASSERT_OK(Put(0, "foo", EncodeAsUint64(1), "v1"));
- ASSERT_OK(db_->IncreaseFullHistoryTsLow(handles_[0], EncodeAsUint64(3)));
- // All keys expired w.r.t the configured `full_history_ts_low`, auto flush
- // continue without the need for a re-schedule.
- ASSERT_OK(dbfull()->TEST_SwitchWAL());
- ASSERT_OK(dbfull()->TEST_WaitForFlushMemTable());
- // `full_history_ts_low` stays unchanged after flush.
- CheckEffectiveCutoffTime(3);
- Close();
- SyncPoint::GetInstance()->DisableProcessing();
- SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- TEST_F(AutoFlushRetainUDTTest, NotAllKeysExpiredFlushToAvoidWriteStall) {
- SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::BackgroundFlush:CheckFlushRequest:cb", [&](void* arg) {
- ASSERT_NE(nullptr, arg);
- auto reschedule_count = *static_cast<int*>(arg);
- ASSERT_EQ(1, reschedule_count);
- });
- SyncPoint::GetInstance()->EnableProcessing();
- Open();
- ASSERT_OK(Put(0, "foo", EncodeAsUint64(1), "v1"));
- ASSERT_OK(db_->IncreaseFullHistoryTsLow(handles_[0], EncodeAsUint64(1)));
- ASSERT_OK(db_->SetOptions(handles_[0], {{"max_write_buffer_number", "1"}}));
- // Not all keys expired, but auto flush is continued without a re-schedule
- // because of risk of write stall.
- ASSERT_OK(dbfull()->TEST_SwitchWAL());
- ASSERT_OK(dbfull()->TEST_WaitForFlushMemTable());
- // After flush, `full_history_ts_low` should be automatically advanced to
- // the effective cutoff timestamp: write_ts + 1
- CheckEffectiveCutoffTime(2);
- Close();
- SyncPoint::GetInstance()->DisableProcessing();
- SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- TEST_F(AutoFlushRetainUDTTest, NotAllKeysExpiredFlushRescheduled) {
- std::atomic<int> local_counter{1};
- SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::AfterRetainUDTReschedule:cb", [&](void* /*arg*/) {
- // Increasing full_history_ts_low so all keys expired after the initial
- // FlushRequest is rescheduled
- ASSERT_OK(
- db_->IncreaseFullHistoryTsLow(handles_[0], EncodeAsUint64(3)));
- });
- SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::BackgroundFlush:CheckFlushRequest:cb", [&](void* arg) {
- ASSERT_NE(nullptr, arg);
- auto reschedule_count = *static_cast<int*>(arg);
- ASSERT_EQ(2, reschedule_count);
- local_counter.fetch_add(1);
- });
- SyncPoint::GetInstance()->EnableProcessing();
- Open();
- ASSERT_OK(Put(0, "foo", EncodeAsUint64(1), "v1"));
- ASSERT_OK(db_->IncreaseFullHistoryTsLow(handles_[0], EncodeAsUint64(1)));
- // Not all keys expired, and there is no risk of write stall. Flush is
- // rescheduled. The actual flush happens after `full_history_ts_low` is
- // increased to mark all keys expired.
- ASSERT_OK(dbfull()->TEST_SwitchWAL());
- ASSERT_OK(dbfull()->TEST_WaitForFlushMemTable());
- // Make sure callback is not skipped.
- ASSERT_EQ(2, local_counter);
- CheckEffectiveCutoffTime(3);
- Close();
- SyncPoint::GetInstance()->DisableProcessing();
- SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- class ManualFlushSkipRetainUDTTest : public ColumnFamilyRetainUDTTest {
- public:
- // Write an entry with timestamp that is not expired w.r.t cutoff timestamp,
- // and make sure automatic flush would be rescheduled to retain UDT.
- void CheckAutomaticFlushRetainUDT(uint64_t write_ts) {
- std::atomic<int> local_counter{1};
- SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::AfterRetainUDTReschedule:cb", [&](void* /*arg*/) {
- // Increasing full_history_ts_low so all keys expired after the
- // initial FlushRequest is rescheduled
- ASSERT_OK(db_->IncreaseFullHistoryTsLow(
- handles_[0], EncodeAsUint64(write_ts + 1)));
- });
- SyncPoint::GetInstance()->SetCallBack(
- "DBImpl::BackgroundFlush:CheckFlushRequest:cb", [&](void* arg) {
- ASSERT_NE(nullptr, arg);
- auto reschedule_count = *static_cast<int*>(arg);
- ASSERT_EQ(2, reschedule_count);
- local_counter.fetch_add(1);
- });
- SyncPoint::GetInstance()->EnableProcessing();
- EXPECT_OK(Put(0, "foo", EncodeAsUint64(write_ts),
- "foo" + std::to_string(write_ts)));
- EXPECT_OK(dbfull()->TEST_SwitchWAL());
- EXPECT_OK(dbfull()->TEST_WaitForFlushMemTable());
- // Make sure callback is not skipped.
- EXPECT_EQ(2, local_counter);
- SyncPoint::GetInstance()->DisableProcessing();
- SyncPoint::GetInstance()->ClearAllCallBacks();
- }
- };
- TEST_F(ManualFlushSkipRetainUDTTest, ManualFlush) {
- Open();
- ASSERT_OK(db_->IncreaseFullHistoryTsLow(handles_[0], EncodeAsUint64(0)));
- // Manual flush proceeds without trying to retain UDT.
- ASSERT_OK(Put(0, "foo", EncodeAsUint64(1), "v1"));
- ASSERT_OK(Flush(0));
- CheckEffectiveCutoffTime(2);
- CheckAutomaticFlushRetainUDT(3);
- Close();
- }
- TEST_F(ManualFlushSkipRetainUDTTest, FlushRemovesStaleEntries) {
- column_family_options_.max_write_buffer_number = 4;
- Open();
- ASSERT_OK(db_->IncreaseFullHistoryTsLow(handles_[0], EncodeAsUint64(0)));
- ColumnFamilyHandle* cfh = db_->DefaultColumnFamily();
- ColumnFamilyData* cfd =
- static_cast_with_check<ColumnFamilyHandleImpl>(cfh)->cfd();
- for (int version = 0; version < 100; version++) {
- if (version == 50) {
- ASSERT_OK(static_cast_with_check<DBImpl>(db_)->TEST_SwitchMemtable(cfd));
- }
- ASSERT_OK(
- Put(0, "foo", EncodeAsUint64(version), "v" + std::to_string(version)));
- }
- ASSERT_OK(Flush(0));
- TablePropertiesCollection tables_properties;
- ASSERT_OK(db_->GetPropertiesOfAllTables(&tables_properties));
- ASSERT_EQ(1, tables_properties.size());
- std::shared_ptr<const TableProperties> table_properties =
- tables_properties.begin()->second;
- ASSERT_EQ(1, table_properties->num_entries);
- ASSERT_EQ(0, table_properties->num_deletions);
- ASSERT_EQ(0, table_properties->num_range_deletions);
- CheckEffectiveCutoffTime(100);
- CheckAutomaticFlushRetainUDT(101);
- Close();
- }
- TEST_F(ManualFlushSkipRetainUDTTest, RangeDeletionFlushRemovesStaleEntries) {
- column_family_options_.max_write_buffer_number = 4;
- Open();
- // TODO(yuzhangyu): a non 0 full history ts low is needed for this garbage
- // collection to kick in. This doesn't work well for the very first flush of
- // the column family. Not a big issue, but would be nice to improve this.
- ASSERT_OK(db_->IncreaseFullHistoryTsLow(handles_[0], EncodeAsUint64(9)));
- for (int i = 10; i < 100; i++) {
- ASSERT_OK(Put(0, "foo" + std::to_string(i), EncodeAsUint64(i),
- "val" + std::to_string(i)));
- if (i % 2 == 1) {
- ASSERT_OK(db_->DeleteRange(WriteOptions(), "foo" + std::to_string(i - 1),
- "foo" + std::to_string(i), EncodeAsUint64(i)));
- }
- }
- ASSERT_OK(Flush(0));
- CheckEffectiveCutoffTime(100);
- std::string read_ts = EncodeAsUint64(100);
- std::string min_ts = EncodeAsUint64(0);
- ReadOptions ropts;
- Slice read_ts_slice = read_ts;
- std::string value;
- ropts.timestamp = &read_ts_slice;
- {
- Iterator* iter = db_->NewIterator(ropts);
- iter->SeekToFirst();
- int i = 11;
- while (iter->Valid()) {
- ASSERT_TRUE(iter->Valid());
- ASSERT_EQ("foo" + std::to_string(i), iter->key());
- ASSERT_EQ("val" + std::to_string(i), iter->value());
- ASSERT_EQ(min_ts, iter->timestamp());
- iter->Next();
- i += 2;
- }
- ASSERT_OK(iter->status());
- delete iter;
- }
- TablePropertiesCollection tables_properties;
- ASSERT_OK(db_->GetPropertiesOfAllTables(&tables_properties));
- ASSERT_EQ(1, tables_properties.size());
- std::shared_ptr<const TableProperties> table_properties =
- tables_properties.begin()->second;
- // 45 point data + 45 range deletions. 45 obsolete point data are garbage
- // collected.
- ASSERT_EQ(90, table_properties->num_entries);
- ASSERT_EQ(45, table_properties->num_deletions);
- ASSERT_EQ(45, table_properties->num_range_deletions);
- Close();
- }
- TEST_F(ManualFlushSkipRetainUDTTest, ManualCompaction) {
- Open();
- ASSERT_OK(db_->IncreaseFullHistoryTsLow(handles_[0], EncodeAsUint64(0)));
- // Manual compaction proceeds without trying to retain UDT.
- ASSERT_OK(Put(0, "foo", EncodeAsUint64(1), "v2"));
- ASSERT_OK(
- db_->CompactRange(CompactRangeOptions(), handles_[0], nullptr, nullptr));
- CheckEffectiveCutoffTime(2);
- CheckAutomaticFlushRetainUDT(3);
- Close();
- }
- TEST_F(ManualFlushSkipRetainUDTTest, BulkLoading) {
- Open();
- ASSERT_OK(db_->IncreaseFullHistoryTsLow(handles_[0], EncodeAsUint64(0)));
- ASSERT_OK(Put(0, "foo", EncodeAsUint64(1), "v1"));
- // Test flush behavior in bulk loading scenarios.
- Options options(db_options_, column_family_options_);
- std::string sst_files_dir = dbname_ + "/sst_files/";
- ASSERT_OK(DestroyDir(env_, sst_files_dir));
- ASSERT_OK(env_->CreateDir(sst_files_dir));
- SstFileWriter sst_file_writer(EnvOptions(), options);
- std::string file1 = sst_files_dir + "file1.sst";
- ASSERT_OK(sst_file_writer.Open(file1));
- ASSERT_OK(sst_file_writer.Put("foo", EncodeAsUint64(0), "v2"));
- ExternalSstFileInfo file1_info;
- ASSERT_OK(sst_file_writer.Finish(&file1_info));
- // Bulk loading in UDT mode doesn't support external file key range overlap
- // with DB key range.
- ASSERT_TRUE(db_->IngestExternalFile({file1}, IngestExternalFileOptions())
- .IsInvalidArgument());
- std::string file2 = sst_files_dir + "file2.sst";
- ASSERT_OK(sst_file_writer.Open(file2));
- ASSERT_OK(sst_file_writer.Put("bar", EncodeAsUint64(0), "val"));
- ExternalSstFileInfo file2_info;
- ASSERT_OK(sst_file_writer.Finish(&file2_info));
- // A successful bulk loading, and it doesn't trigger any flush. As a result
- // the effective cutoff timestamp is also unchanged.
- ASSERT_OK(db_->IngestExternalFile({file2}, IngestExternalFileOptions()));
- ASSERT_EQ(Get(0, "foo", EncodeAsUint64(1)), "v1");
- ASSERT_EQ(Get(0, "bar", EncodeAsUint64(0)), "val");
- CheckEffectiveCutoffTime(0);
- CheckAutomaticFlushRetainUDT(1);
- Close();
- }
- TEST_F(ManualFlushSkipRetainUDTTest, AutomaticFlushQueued) {
- Open();
- ASSERT_OK(db_->IncreaseFullHistoryTsLow(handles_[0], EncodeAsUint64(0)));
- ASSERT_OK(Put(0, "foo", EncodeAsUint64(1), "v1"));
- ASSERT_OK(dbfull()->TEST_SwitchWAL());
- CheckEffectiveCutoffTime(0);
- // Default `max_write_buffer_number=2` used, writing another memtable can get
- // automatic flush to proceed because of memory pressure. Not doing that so
- // we can test automatic flush gets to proceed because of an ongoing manual
- // flush attempt.
- ASSERT_OK(Flush(0));
- CheckEffectiveCutoffTime(2);
- CheckAutomaticFlushRetainUDT(3);
- Close();
- }
- TEST_F(ManualFlushSkipRetainUDTTest, ConcurrentManualFlushes) {
- Open();
- ASSERT_OK(db_->IncreaseFullHistoryTsLow(handles_[0], EncodeAsUint64(0)));
- std::vector<ROCKSDB_NAMESPACE::port::Thread> manual_flush_tds;
- std::atomic<int> next_ts{0};
- std::mutex mtx;
- std::condition_variable cv;
- auto manual_flush = [&](int write_ts) {
- {
- std::unique_lock<std::mutex> lock(mtx);
- cv.wait(lock,
- [&write_ts, &next_ts] { return write_ts == next_ts.load(); });
- ASSERT_OK(Put(0, "foo" + std::to_string(write_ts),
- EncodeAsUint64(write_ts),
- "val_" + std::to_string(write_ts)));
- next_ts.fetch_add(1);
- cv.notify_all();
- }
- if (write_ts % 2 == 0) {
- ASSERT_OK(Flush(0));
- } else {
- ASSERT_OK(db_->CompactRange(CompactRangeOptions(), handles_[0], nullptr,
- nullptr));
- }
- };
- for (int write_ts = 0; write_ts < 10; write_ts++) {
- manual_flush_tds.emplace_back(manual_flush, write_ts);
- }
- for (auto& td : manual_flush_tds) {
- td.join();
- }
- CheckEffectiveCutoffTime(10);
- CheckAutomaticFlushRetainUDT(11);
- Close();
- }
- } // namespace ROCKSDB_NAMESPACE
- int main(int argc, char** argv) {
- ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
- ::testing::InitGoogleTest(&argc, argv);
- RegisterCustomObjects(argc, argv);
- return RUN_ALL_TESTS();
- }
|