checkpoint_test.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. // Syncpoint prevents us building and running tests in release
  10. #ifndef ROCKSDB_LITE
  11. #ifndef OS_WIN
  12. #include <unistd.h>
  13. #endif
  14. #include <iostream>
  15. #include <thread>
  16. #include <utility>
  17. #include "db/db_impl/db_impl.h"
  18. #include "port/port.h"
  19. #include "port/stack_trace.h"
  20. #include "rocksdb/db.h"
  21. #include "rocksdb/env.h"
  22. #include "rocksdb/utilities/checkpoint.h"
  23. #include "rocksdb/utilities/transaction_db.h"
  24. #include "test_util/fault_injection_test_env.h"
  25. #include "test_util/sync_point.h"
  26. #include "test_util/testharness.h"
  27. #include "test_util/testutil.h"
  28. namespace ROCKSDB_NAMESPACE {
  29. class CheckpointTest : public testing::Test {
  30. protected:
  31. // Sequence of option configurations to try
  32. enum OptionConfig {
  33. kDefault = 0,
  34. };
  35. int option_config_;
  36. public:
  37. std::string dbname_;
  38. std::string alternative_wal_dir_;
  39. Env* env_;
  40. DB* db_;
  41. Options last_options_;
  42. std::vector<ColumnFamilyHandle*> handles_;
  43. std::string snapshot_name_;
  44. std::string export_path_;
  45. ColumnFamilyHandle* cfh_reverse_comp_;
  46. ExportImportFilesMetaData* metadata_;
  47. CheckpointTest() : env_(Env::Default()) {
  48. env_->SetBackgroundThreads(1, Env::LOW);
  49. env_->SetBackgroundThreads(1, Env::HIGH);
  50. dbname_ = test::PerThreadDBPath(env_, "checkpoint_test");
  51. alternative_wal_dir_ = dbname_ + "/wal";
  52. auto options = CurrentOptions();
  53. auto delete_options = options;
  54. delete_options.wal_dir = alternative_wal_dir_;
  55. EXPECT_OK(DestroyDB(dbname_, delete_options));
  56. // Destroy it for not alternative WAL dir is used.
  57. EXPECT_OK(DestroyDB(dbname_, options));
  58. db_ = nullptr;
  59. snapshot_name_ = test::PerThreadDBPath(env_, "snapshot");
  60. std::string snapshot_tmp_name = snapshot_name_ + ".tmp";
  61. EXPECT_OK(DestroyDB(snapshot_name_, options));
  62. env_->DeleteDir(snapshot_name_);
  63. EXPECT_OK(DestroyDB(snapshot_tmp_name, options));
  64. env_->DeleteDir(snapshot_tmp_name);
  65. Reopen(options);
  66. export_path_ = test::TmpDir(env_) + "/export";
  67. test::DestroyDir(env_, export_path_);
  68. cfh_reverse_comp_ = nullptr;
  69. metadata_ = nullptr;
  70. }
  71. ~CheckpointTest() override {
  72. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  73. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({});
  74. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
  75. if (cfh_reverse_comp_) {
  76. EXPECT_OK(db_->DestroyColumnFamilyHandle(cfh_reverse_comp_));
  77. cfh_reverse_comp_ = nullptr;
  78. }
  79. if (metadata_) {
  80. delete metadata_;
  81. metadata_ = nullptr;
  82. }
  83. Close();
  84. Options options;
  85. options.db_paths.emplace_back(dbname_, 0);
  86. options.db_paths.emplace_back(dbname_ + "_2", 0);
  87. options.db_paths.emplace_back(dbname_ + "_3", 0);
  88. options.db_paths.emplace_back(dbname_ + "_4", 0);
  89. EXPECT_OK(DestroyDB(dbname_, options));
  90. EXPECT_OK(DestroyDB(snapshot_name_, options));
  91. test::DestroyDir(env_, export_path_);
  92. }
  93. // Return the current option configuration.
  94. Options CurrentOptions() {
  95. Options options;
  96. options.env = env_;
  97. options.create_if_missing = true;
  98. return options;
  99. }
  100. void CreateColumnFamilies(const std::vector<std::string>& cfs,
  101. const Options& options) {
  102. ColumnFamilyOptions cf_opts(options);
  103. size_t cfi = handles_.size();
  104. handles_.resize(cfi + cfs.size());
  105. for (auto cf : cfs) {
  106. ASSERT_OK(db_->CreateColumnFamily(cf_opts, cf, &handles_[cfi++]));
  107. }
  108. }
  109. void CreateAndReopenWithCF(const std::vector<std::string>& cfs,
  110. const Options& options) {
  111. CreateColumnFamilies(cfs, options);
  112. std::vector<std::string> cfs_plus_default = cfs;
  113. cfs_plus_default.insert(cfs_plus_default.begin(), kDefaultColumnFamilyName);
  114. ReopenWithColumnFamilies(cfs_plus_default, options);
  115. }
  116. void ReopenWithColumnFamilies(const std::vector<std::string>& cfs,
  117. const std::vector<Options>& options) {
  118. ASSERT_OK(TryReopenWithColumnFamilies(cfs, options));
  119. }
  120. void ReopenWithColumnFamilies(const std::vector<std::string>& cfs,
  121. const Options& options) {
  122. ASSERT_OK(TryReopenWithColumnFamilies(cfs, options));
  123. }
  124. Status TryReopenWithColumnFamilies(
  125. const std::vector<std::string>& cfs,
  126. const std::vector<Options>& options) {
  127. Close();
  128. EXPECT_EQ(cfs.size(), options.size());
  129. std::vector<ColumnFamilyDescriptor> column_families;
  130. for (size_t i = 0; i < cfs.size(); ++i) {
  131. column_families.push_back(ColumnFamilyDescriptor(cfs[i], options[i]));
  132. }
  133. DBOptions db_opts = DBOptions(options[0]);
  134. return DB::Open(db_opts, dbname_, column_families, &handles_, &db_);
  135. }
  136. Status TryReopenWithColumnFamilies(const std::vector<std::string>& cfs,
  137. const Options& options) {
  138. Close();
  139. std::vector<Options> v_opts(cfs.size(), options);
  140. return TryReopenWithColumnFamilies(cfs, v_opts);
  141. }
  142. void Reopen(const Options& options) {
  143. ASSERT_OK(TryReopen(options));
  144. }
  145. void CompactAll() {
  146. for (auto h : handles_) {
  147. ASSERT_OK(db_->CompactRange(CompactRangeOptions(), h, nullptr, nullptr));
  148. }
  149. }
  150. void Close() {
  151. for (auto h : handles_) {
  152. delete h;
  153. }
  154. handles_.clear();
  155. delete db_;
  156. db_ = nullptr;
  157. }
  158. void DestroyAndReopen(const Options& options) {
  159. // Destroy using last options
  160. Destroy(last_options_);
  161. ASSERT_OK(TryReopen(options));
  162. }
  163. void Destroy(const Options& options) {
  164. Close();
  165. ASSERT_OK(DestroyDB(dbname_, options));
  166. }
  167. Status ReadOnlyReopen(const Options& options) {
  168. return DB::OpenForReadOnly(options, dbname_, &db_);
  169. }
  170. Status ReadOnlyReopenWithColumnFamilies(const std::vector<std::string>& cfs,
  171. const Options& options) {
  172. std::vector<ColumnFamilyDescriptor> column_families;
  173. for (const auto& cf : cfs) {
  174. column_families.emplace_back(cf, options);
  175. }
  176. return DB::OpenForReadOnly(options, dbname_, column_families, &handles_,
  177. &db_);
  178. }
  179. Status TryReopen(const Options& options) {
  180. Close();
  181. last_options_ = options;
  182. return DB::Open(options, dbname_, &db_);
  183. }
  184. Status Flush(int cf = 0) {
  185. if (cf == 0) {
  186. return db_->Flush(FlushOptions());
  187. } else {
  188. return db_->Flush(FlushOptions(), handles_[cf]);
  189. }
  190. }
  191. Status Put(const Slice& k, const Slice& v, WriteOptions wo = WriteOptions()) {
  192. return db_->Put(wo, k, v);
  193. }
  194. Status Put(int cf, const Slice& k, const Slice& v,
  195. WriteOptions wo = WriteOptions()) {
  196. return db_->Put(wo, handles_[cf], k, v);
  197. }
  198. Status Delete(const std::string& k) {
  199. return db_->Delete(WriteOptions(), k);
  200. }
  201. Status Delete(int cf, const std::string& k) {
  202. return db_->Delete(WriteOptions(), handles_[cf], k);
  203. }
  204. std::string Get(const std::string& k, const Snapshot* snapshot = nullptr) {
  205. ReadOptions options;
  206. options.verify_checksums = true;
  207. options.snapshot = snapshot;
  208. std::string result;
  209. Status s = db_->Get(options, k, &result);
  210. if (s.IsNotFound()) {
  211. result = "NOT_FOUND";
  212. } else if (!s.ok()) {
  213. result = s.ToString();
  214. }
  215. return result;
  216. }
  217. std::string Get(int cf, const std::string& k,
  218. const Snapshot* snapshot = nullptr) {
  219. ReadOptions options;
  220. options.verify_checksums = true;
  221. options.snapshot = snapshot;
  222. std::string result;
  223. Status s = db_->Get(options, handles_[cf], k, &result);
  224. if (s.IsNotFound()) {
  225. result = "NOT_FOUND";
  226. } else if (!s.ok()) {
  227. result = s.ToString();
  228. }
  229. return result;
  230. }
  231. };
  232. TEST_F(CheckpointTest, GetSnapshotLink) {
  233. for (uint64_t log_size_for_flush : {0, 1000000}) {
  234. Options options;
  235. DB* snapshotDB;
  236. ReadOptions roptions;
  237. std::string result;
  238. Checkpoint* checkpoint;
  239. options = CurrentOptions();
  240. delete db_;
  241. db_ = nullptr;
  242. ASSERT_OK(DestroyDB(dbname_, options));
  243. // Create a database
  244. Status s;
  245. options.create_if_missing = true;
  246. ASSERT_OK(DB::Open(options, dbname_, &db_));
  247. std::string key = std::string("foo");
  248. ASSERT_OK(Put(key, "v1"));
  249. // Take a snapshot
  250. ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
  251. ASSERT_OK(checkpoint->CreateCheckpoint(snapshot_name_, log_size_for_flush));
  252. ASSERT_OK(Put(key, "v2"));
  253. ASSERT_EQ("v2", Get(key));
  254. ASSERT_OK(Flush());
  255. ASSERT_EQ("v2", Get(key));
  256. // Open snapshot and verify contents while DB is running
  257. options.create_if_missing = false;
  258. ASSERT_OK(DB::Open(options, snapshot_name_, &snapshotDB));
  259. ASSERT_OK(snapshotDB->Get(roptions, key, &result));
  260. ASSERT_EQ("v1", result);
  261. delete snapshotDB;
  262. snapshotDB = nullptr;
  263. delete db_;
  264. db_ = nullptr;
  265. // Destroy original DB
  266. ASSERT_OK(DestroyDB(dbname_, options));
  267. // Open snapshot and verify contents
  268. options.create_if_missing = false;
  269. dbname_ = snapshot_name_;
  270. ASSERT_OK(DB::Open(options, dbname_, &db_));
  271. ASSERT_EQ("v1", Get(key));
  272. delete db_;
  273. db_ = nullptr;
  274. ASSERT_OK(DestroyDB(dbname_, options));
  275. delete checkpoint;
  276. // Restore DB name
  277. dbname_ = test::PerThreadDBPath(env_, "db_test");
  278. }
  279. }
  280. TEST_F(CheckpointTest, ExportColumnFamilyWithLinks) {
  281. // Create a database
  282. Status s;
  283. auto options = CurrentOptions();
  284. options.create_if_missing = true;
  285. CreateAndReopenWithCF({}, options);
  286. // Helper to verify the number of files in metadata and export dir
  287. auto verify_files_exported = [&](const ExportImportFilesMetaData& metadata,
  288. int num_files_expected) {
  289. ASSERT_EQ(metadata.files.size(), num_files_expected);
  290. std::vector<std::string> subchildren;
  291. env_->GetChildren(export_path_, &subchildren);
  292. int num_children = 0;
  293. for (const auto& child : subchildren) {
  294. if (child != "." && child != "..") {
  295. ++num_children;
  296. }
  297. }
  298. ASSERT_EQ(num_children, num_files_expected);
  299. };
  300. // Test DefaultColumnFamily
  301. {
  302. const auto key = std::string("foo");
  303. ASSERT_OK(Put(key, "v1"));
  304. Checkpoint* checkpoint;
  305. ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
  306. // Export the Tables and verify
  307. ASSERT_OK(checkpoint->ExportColumnFamily(db_->DefaultColumnFamily(),
  308. export_path_, &metadata_));
  309. verify_files_exported(*metadata_, 1);
  310. ASSERT_EQ(metadata_->db_comparator_name, options.comparator->Name());
  311. test::DestroyDir(env_, export_path_);
  312. delete metadata_;
  313. metadata_ = nullptr;
  314. // Check again after compaction
  315. CompactAll();
  316. ASSERT_OK(Put(key, "v2"));
  317. ASSERT_OK(checkpoint->ExportColumnFamily(db_->DefaultColumnFamily(),
  318. export_path_, &metadata_));
  319. verify_files_exported(*metadata_, 2);
  320. ASSERT_EQ(metadata_->db_comparator_name, options.comparator->Name());
  321. test::DestroyDir(env_, export_path_);
  322. delete metadata_;
  323. metadata_ = nullptr;
  324. delete checkpoint;
  325. }
  326. // Test non default column family with non default comparator
  327. {
  328. auto cf_options = CurrentOptions();
  329. cf_options.comparator = ReverseBytewiseComparator();
  330. ASSERT_OK(db_->CreateColumnFamily(cf_options, "yoyo", &cfh_reverse_comp_));
  331. const auto key = std::string("foo");
  332. ASSERT_OK(db_->Put(WriteOptions(), cfh_reverse_comp_, key, "v1"));
  333. Checkpoint* checkpoint;
  334. ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
  335. // Export the Tables and verify
  336. ASSERT_OK(checkpoint->ExportColumnFamily(cfh_reverse_comp_, export_path_,
  337. &metadata_));
  338. verify_files_exported(*metadata_, 1);
  339. ASSERT_EQ(metadata_->db_comparator_name,
  340. ReverseBytewiseComparator()->Name());
  341. delete checkpoint;
  342. }
  343. }
  344. TEST_F(CheckpointTest, ExportColumnFamilyNegativeTest) {
  345. // Create a database
  346. Status s;
  347. auto options = CurrentOptions();
  348. options.create_if_missing = true;
  349. CreateAndReopenWithCF({}, options);
  350. const auto key = std::string("foo");
  351. ASSERT_OK(Put(key, "v1"));
  352. Checkpoint* checkpoint;
  353. ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
  354. // Export onto existing directory
  355. env_->CreateDirIfMissing(export_path_);
  356. ASSERT_EQ(checkpoint->ExportColumnFamily(db_->DefaultColumnFamily(),
  357. export_path_, &metadata_),
  358. Status::InvalidArgument("Specified export_dir exists"));
  359. test::DestroyDir(env_, export_path_);
  360. // Export with invalid directory specification
  361. export_path_ = "";
  362. ASSERT_EQ(checkpoint->ExportColumnFamily(db_->DefaultColumnFamily(),
  363. export_path_, &metadata_),
  364. Status::InvalidArgument("Specified export_dir invalid"));
  365. delete checkpoint;
  366. }
  367. TEST_F(CheckpointTest, CheckpointCF) {
  368. Options options = CurrentOptions();
  369. CreateAndReopenWithCF({"one", "two", "three", "four", "five"}, options);
  370. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
  371. {{"CheckpointTest::CheckpointCF:2", "DBImpl::GetLiveFiles:2"},
  372. {"DBImpl::GetLiveFiles:1", "CheckpointTest::CheckpointCF:1"}});
  373. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  374. ASSERT_OK(Put(0, "Default", "Default"));
  375. ASSERT_OK(Put(1, "one", "one"));
  376. ASSERT_OK(Put(2, "two", "two"));
  377. ASSERT_OK(Put(3, "three", "three"));
  378. ASSERT_OK(Put(4, "four", "four"));
  379. ASSERT_OK(Put(5, "five", "five"));
  380. DB* snapshotDB;
  381. ReadOptions roptions;
  382. std::string result;
  383. std::vector<ColumnFamilyHandle*> cphandles;
  384. Status s;
  385. // Take a snapshot
  386. ROCKSDB_NAMESPACE::port::Thread t([&]() {
  387. Checkpoint* checkpoint;
  388. ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
  389. ASSERT_OK(checkpoint->CreateCheckpoint(snapshot_name_));
  390. delete checkpoint;
  391. });
  392. TEST_SYNC_POINT("CheckpointTest::CheckpointCF:1");
  393. ASSERT_OK(Put(0, "Default", "Default1"));
  394. ASSERT_OK(Put(1, "one", "eleven"));
  395. ASSERT_OK(Put(2, "two", "twelve"));
  396. ASSERT_OK(Put(3, "three", "thirteen"));
  397. ASSERT_OK(Put(4, "four", "fourteen"));
  398. ASSERT_OK(Put(5, "five", "fifteen"));
  399. TEST_SYNC_POINT("CheckpointTest::CheckpointCF:2");
  400. t.join();
  401. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  402. ASSERT_OK(Put(1, "one", "twentyone"));
  403. ASSERT_OK(Put(2, "two", "twentytwo"));
  404. ASSERT_OK(Put(3, "three", "twentythree"));
  405. ASSERT_OK(Put(4, "four", "twentyfour"));
  406. ASSERT_OK(Put(5, "five", "twentyfive"));
  407. ASSERT_OK(Flush());
  408. // Open snapshot and verify contents while DB is running
  409. options.create_if_missing = false;
  410. std::vector<std::string> cfs;
  411. cfs= {kDefaultColumnFamilyName, "one", "two", "three", "four", "five"};
  412. std::vector<ColumnFamilyDescriptor> column_families;
  413. for (size_t i = 0; i < cfs.size(); ++i) {
  414. column_families.push_back(ColumnFamilyDescriptor(cfs[i], options));
  415. }
  416. ASSERT_OK(DB::Open(options, snapshot_name_,
  417. column_families, &cphandles, &snapshotDB));
  418. ASSERT_OK(snapshotDB->Get(roptions, cphandles[0], "Default", &result));
  419. ASSERT_EQ("Default1", result);
  420. ASSERT_OK(snapshotDB->Get(roptions, cphandles[1], "one", &result));
  421. ASSERT_EQ("eleven", result);
  422. ASSERT_OK(snapshotDB->Get(roptions, cphandles[2], "two", &result));
  423. for (auto h : cphandles) {
  424. delete h;
  425. }
  426. cphandles.clear();
  427. delete snapshotDB;
  428. snapshotDB = nullptr;
  429. }
  430. TEST_F(CheckpointTest, CheckpointCFNoFlush) {
  431. Options options = CurrentOptions();
  432. CreateAndReopenWithCF({"one", "two", "three", "four", "five"}, options);
  433. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  434. ASSERT_OK(Put(0, "Default", "Default"));
  435. ASSERT_OK(Put(1, "one", "one"));
  436. Flush();
  437. ASSERT_OK(Put(2, "two", "two"));
  438. DB* snapshotDB;
  439. ReadOptions roptions;
  440. std::string result;
  441. std::vector<ColumnFamilyHandle*> cphandles;
  442. Status s;
  443. // Take a snapshot
  444. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
  445. "DBImpl::BackgroundCallFlush:start", [&](void* /*arg*/) {
  446. // Flush should never trigger.
  447. FAIL();
  448. });
  449. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  450. Checkpoint* checkpoint;
  451. ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
  452. ASSERT_OK(checkpoint->CreateCheckpoint(snapshot_name_, 1000000));
  453. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  454. delete checkpoint;
  455. ASSERT_OK(Put(1, "one", "two"));
  456. ASSERT_OK(Flush(1));
  457. ASSERT_OK(Put(2, "two", "twentytwo"));
  458. Close();
  459. EXPECT_OK(DestroyDB(dbname_, options));
  460. // Open snapshot and verify contents while DB is running
  461. options.create_if_missing = false;
  462. std::vector<std::string> cfs;
  463. cfs = {kDefaultColumnFamilyName, "one", "two", "three", "four", "five"};
  464. std::vector<ColumnFamilyDescriptor> column_families;
  465. for (size_t i = 0; i < cfs.size(); ++i) {
  466. column_families.push_back(ColumnFamilyDescriptor(cfs[i], options));
  467. }
  468. ASSERT_OK(DB::Open(options, snapshot_name_, column_families, &cphandles,
  469. &snapshotDB));
  470. ASSERT_OK(snapshotDB->Get(roptions, cphandles[0], "Default", &result));
  471. ASSERT_EQ("Default", result);
  472. ASSERT_OK(snapshotDB->Get(roptions, cphandles[1], "one", &result));
  473. ASSERT_EQ("one", result);
  474. ASSERT_OK(snapshotDB->Get(roptions, cphandles[2], "two", &result));
  475. ASSERT_EQ("two", result);
  476. for (auto h : cphandles) {
  477. delete h;
  478. }
  479. cphandles.clear();
  480. delete snapshotDB;
  481. snapshotDB = nullptr;
  482. }
  483. TEST_F(CheckpointTest, CurrentFileModifiedWhileCheckpointing) {
  484. Options options = CurrentOptions();
  485. options.max_manifest_file_size = 0; // always rollover manifest for file add
  486. Reopen(options);
  487. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
  488. {// Get past the flush in the checkpoint thread before adding any keys to
  489. // the db so the checkpoint thread won't hit the WriteManifest
  490. // syncpoints.
  491. {"DBImpl::GetLiveFiles:1",
  492. "CheckpointTest::CurrentFileModifiedWhileCheckpointing:PrePut"},
  493. // Roll the manifest during checkpointing right after live files are
  494. // snapshotted.
  495. {"CheckpointImpl::CreateCheckpoint:SavedLiveFiles1",
  496. "VersionSet::LogAndApply:WriteManifest"},
  497. {"VersionSet::LogAndApply:WriteManifestDone",
  498. "CheckpointImpl::CreateCheckpoint:SavedLiveFiles2"}});
  499. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  500. ROCKSDB_NAMESPACE::port::Thread t([&]() {
  501. Checkpoint* checkpoint;
  502. ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
  503. ASSERT_OK(checkpoint->CreateCheckpoint(snapshot_name_));
  504. delete checkpoint;
  505. });
  506. TEST_SYNC_POINT(
  507. "CheckpointTest::CurrentFileModifiedWhileCheckpointing:PrePut");
  508. ASSERT_OK(Put("Default", "Default1"));
  509. ASSERT_OK(Flush());
  510. t.join();
  511. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  512. DB* snapshotDB;
  513. // Successful Open() implies that CURRENT pointed to the manifest in the
  514. // checkpoint.
  515. ASSERT_OK(DB::Open(options, snapshot_name_, &snapshotDB));
  516. delete snapshotDB;
  517. snapshotDB = nullptr;
  518. }
  519. TEST_F(CheckpointTest, CurrentFileModifiedWhileCheckpointing2PC) {
  520. Close();
  521. const std::string dbname = test::PerThreadDBPath("transaction_testdb");
  522. ASSERT_OK(DestroyDB(dbname, CurrentOptions()));
  523. env_->DeleteDir(dbname);
  524. Options options = CurrentOptions();
  525. options.allow_2pc = true;
  526. // allow_2pc is implicitly set with tx prepare
  527. // options.allow_2pc = true;
  528. TransactionDBOptions txn_db_options;
  529. TransactionDB* txdb;
  530. Status s = TransactionDB::Open(options, txn_db_options, dbname, &txdb);
  531. assert(s.ok());
  532. ColumnFamilyHandle* cfa;
  533. ColumnFamilyHandle* cfb;
  534. ColumnFamilyOptions cf_options;
  535. ASSERT_OK(txdb->CreateColumnFamily(cf_options, "CFA", &cfa));
  536. WriteOptions write_options;
  537. // Insert something into CFB so lots of log files will be kept
  538. // before creating the checkpoint.
  539. ASSERT_OK(txdb->CreateColumnFamily(cf_options, "CFB", &cfb));
  540. ASSERT_OK(txdb->Put(write_options, cfb, "", ""));
  541. ReadOptions read_options;
  542. std::string value;
  543. TransactionOptions txn_options;
  544. Transaction* txn = txdb->BeginTransaction(write_options, txn_options);
  545. s = txn->SetName("xid");
  546. ASSERT_OK(s);
  547. ASSERT_EQ(txdb->GetTransactionByName("xid"), txn);
  548. s = txn->Put(Slice("foo"), Slice("bar"));
  549. s = txn->Put(cfa, Slice("foocfa"), Slice("barcfa"));
  550. ASSERT_OK(s);
  551. // Writing prepare into middle of first WAL, then flush WALs many times
  552. for (int i = 1; i <= 100000; i++) {
  553. Transaction* tx = txdb->BeginTransaction(write_options, txn_options);
  554. ASSERT_OK(tx->SetName("x"));
  555. ASSERT_OK(tx->Put(Slice(std::to_string(i)), Slice("val")));
  556. ASSERT_OK(tx->Put(cfa, Slice("aaa"), Slice("111")));
  557. ASSERT_OK(tx->Prepare());
  558. ASSERT_OK(tx->Commit());
  559. if (i % 10000 == 0) {
  560. txdb->Flush(FlushOptions());
  561. }
  562. if (i == 88888) {
  563. ASSERT_OK(txn->Prepare());
  564. }
  565. delete tx;
  566. }
  567. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
  568. {{"CheckpointImpl::CreateCheckpoint:SavedLiveFiles1",
  569. "CheckpointTest::CurrentFileModifiedWhileCheckpointing2PC:PreCommit"},
  570. {"CheckpointTest::CurrentFileModifiedWhileCheckpointing2PC:PostCommit",
  571. "CheckpointImpl::CreateCheckpoint:SavedLiveFiles2"}});
  572. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  573. ROCKSDB_NAMESPACE::port::Thread t([&]() {
  574. Checkpoint* checkpoint;
  575. ASSERT_OK(Checkpoint::Create(txdb, &checkpoint));
  576. ASSERT_OK(checkpoint->CreateCheckpoint(snapshot_name_));
  577. delete checkpoint;
  578. });
  579. TEST_SYNC_POINT(
  580. "CheckpointTest::CurrentFileModifiedWhileCheckpointing2PC:PreCommit");
  581. ASSERT_OK(txn->Commit());
  582. delete txn;
  583. TEST_SYNC_POINT(
  584. "CheckpointTest::CurrentFileModifiedWhileCheckpointing2PC:PostCommit");
  585. t.join();
  586. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  587. // No more than two logs files should exist.
  588. std::vector<std::string> files;
  589. env_->GetChildren(snapshot_name_, &files);
  590. int num_log_files = 0;
  591. for (auto& file : files) {
  592. uint64_t num;
  593. FileType type;
  594. WalFileType log_type;
  595. if (ParseFileName(file, &num, &type, &log_type) && type == kLogFile) {
  596. num_log_files++;
  597. }
  598. }
  599. // One flush after preapare + one outstanding file before checkpoint + one log
  600. // file generated after checkpoint.
  601. ASSERT_LE(num_log_files, 3);
  602. TransactionDB* snapshotDB;
  603. std::vector<ColumnFamilyDescriptor> column_families;
  604. column_families.push_back(
  605. ColumnFamilyDescriptor(kDefaultColumnFamilyName, ColumnFamilyOptions()));
  606. column_families.push_back(
  607. ColumnFamilyDescriptor("CFA", ColumnFamilyOptions()));
  608. column_families.push_back(
  609. ColumnFamilyDescriptor("CFB", ColumnFamilyOptions()));
  610. std::vector<ROCKSDB_NAMESPACE::ColumnFamilyHandle*> cf_handles;
  611. ASSERT_OK(TransactionDB::Open(options, txn_db_options, snapshot_name_,
  612. column_families, &cf_handles, &snapshotDB));
  613. ASSERT_OK(snapshotDB->Get(read_options, "foo", &value));
  614. ASSERT_EQ(value, "bar");
  615. ASSERT_OK(snapshotDB->Get(read_options, cf_handles[1], "foocfa", &value));
  616. ASSERT_EQ(value, "barcfa");
  617. delete cfa;
  618. delete cfb;
  619. delete cf_handles[0];
  620. delete cf_handles[1];
  621. delete cf_handles[2];
  622. delete snapshotDB;
  623. snapshotDB = nullptr;
  624. delete txdb;
  625. }
  626. TEST_F(CheckpointTest, CheckpointInvalidDirectoryName) {
  627. for (std::string checkpoint_dir : {"", "/", "////"}) {
  628. Checkpoint* checkpoint;
  629. ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
  630. ASSERT_TRUE(checkpoint->CreateCheckpoint("").IsInvalidArgument());
  631. delete checkpoint;
  632. }
  633. }
  634. TEST_F(CheckpointTest, CheckpointWithParallelWrites) {
  635. // When run with TSAN, this exposes the data race fixed in
  636. // https://github.com/facebook/rocksdb/pull/3603
  637. ASSERT_OK(Put("key1", "val1"));
  638. port::Thread thread([this]() { ASSERT_OK(Put("key2", "val2")); });
  639. Checkpoint* checkpoint;
  640. ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
  641. ASSERT_OK(checkpoint->CreateCheckpoint(snapshot_name_));
  642. delete checkpoint;
  643. thread.join();
  644. }
  645. TEST_F(CheckpointTest, CheckpointWithUnsyncedDataDropped) {
  646. Options options = CurrentOptions();
  647. std::unique_ptr<FaultInjectionTestEnv> env(new FaultInjectionTestEnv(env_));
  648. options.env = env.get();
  649. Reopen(options);
  650. ASSERT_OK(Put("key1", "val1"));
  651. Checkpoint* checkpoint;
  652. ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
  653. ASSERT_OK(checkpoint->CreateCheckpoint(snapshot_name_));
  654. delete checkpoint;
  655. env->DropUnsyncedFileData();
  656. // make sure it's openable even though whatever data that wasn't synced got
  657. // dropped.
  658. options.env = env_;
  659. DB* snapshot_db;
  660. ASSERT_OK(DB::Open(options, snapshot_name_, &snapshot_db));
  661. ReadOptions read_opts;
  662. std::string get_result;
  663. ASSERT_OK(snapshot_db->Get(read_opts, "key1", &get_result));
  664. ASSERT_EQ("val1", get_result);
  665. delete snapshot_db;
  666. delete db_;
  667. db_ = nullptr;
  668. }
  669. TEST_F(CheckpointTest, CheckpointReadOnlyDB) {
  670. ASSERT_OK(Put("foo", "foo_value"));
  671. ASSERT_OK(Flush());
  672. Close();
  673. Options options = CurrentOptions();
  674. ASSERT_OK(ReadOnlyReopen(options));
  675. Checkpoint* checkpoint = nullptr;
  676. ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
  677. ASSERT_OK(checkpoint->CreateCheckpoint(snapshot_name_));
  678. delete checkpoint;
  679. checkpoint = nullptr;
  680. Close();
  681. DB* snapshot_db = nullptr;
  682. ASSERT_OK(DB::Open(options, snapshot_name_, &snapshot_db));
  683. ReadOptions read_opts;
  684. std::string get_result;
  685. ASSERT_OK(snapshot_db->Get(read_opts, "foo", &get_result));
  686. ASSERT_EQ("foo_value", get_result);
  687. delete snapshot_db;
  688. }
  689. TEST_F(CheckpointTest, CheckpointReadOnlyDBWithMultipleColumnFamilies) {
  690. Options options = CurrentOptions();
  691. CreateAndReopenWithCF({"pikachu", "eevee"}, options);
  692. for (int i = 0; i != 3; ++i) {
  693. ASSERT_OK(Put(i, "foo", "foo_value"));
  694. ASSERT_OK(Flush(i));
  695. }
  696. Close();
  697. Status s = ReadOnlyReopenWithColumnFamilies(
  698. {kDefaultColumnFamilyName, "pikachu", "eevee"}, options);
  699. ASSERT_OK(s);
  700. Checkpoint* checkpoint = nullptr;
  701. ASSERT_OK(Checkpoint::Create(db_, &checkpoint));
  702. ASSERT_OK(checkpoint->CreateCheckpoint(snapshot_name_));
  703. delete checkpoint;
  704. checkpoint = nullptr;
  705. Close();
  706. std::vector<ColumnFamilyDescriptor> column_families{
  707. {kDefaultColumnFamilyName, options},
  708. {"pikachu", options},
  709. {"eevee", options}};
  710. DB* snapshot_db = nullptr;
  711. std::vector<ColumnFamilyHandle*> snapshot_handles;
  712. s = DB::Open(options, snapshot_name_, column_families, &snapshot_handles,
  713. &snapshot_db);
  714. ASSERT_OK(s);
  715. ReadOptions read_opts;
  716. for (int i = 0; i != 3; ++i) {
  717. std::string get_result;
  718. s = snapshot_db->Get(read_opts, snapshot_handles[i], "foo", &get_result);
  719. ASSERT_OK(s);
  720. ASSERT_EQ("foo_value", get_result);
  721. }
  722. for (auto snapshot_h : snapshot_handles) {
  723. delete snapshot_h;
  724. }
  725. snapshot_handles.clear();
  726. delete snapshot_db;
  727. }
  728. } // namespace ROCKSDB_NAMESPACE
  729. int main(int argc, char** argv) {
  730. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  731. ::testing::InitGoogleTest(&argc, argv);
  732. return RUN_ALL_TESTS();
  733. }
  734. #else
  735. #include <stdio.h>
  736. int main(int /*argc*/, char** /*argv*/) {
  737. fprintf(stderr, "SKIPPED as Checkpoint is not supported in ROCKSDB_LITE\n");
  738. return 0;
  739. }
  740. #endif // !ROCKSDB_LITE