repair_test.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include <algorithm>
  6. #include <string>
  7. #include <vector>
  8. #include "db/db_impl/db_impl.h"
  9. #include "db/db_test_util.h"
  10. #include "db/db_with_timestamp_test_util.h"
  11. #include "file/file_util.h"
  12. #include "rocksdb/comparator.h"
  13. #include "rocksdb/db.h"
  14. #include "rocksdb/options.h"
  15. #include "rocksdb/transaction_log.h"
  16. #include "table/unique_id_impl.h"
  17. #include "util/string_util.h"
  18. namespace ROCKSDB_NAMESPACE {
  19. class RepairTest : public DBTestBase {
  20. public:
  21. RepairTest() : DBTestBase("repair_test", /*env_do_fsync=*/true) {}
  22. Status GetFirstSstPath(std::string* first_sst_path) {
  23. assert(first_sst_path != nullptr);
  24. first_sst_path->clear();
  25. uint64_t manifest_size;
  26. std::vector<std::string> files;
  27. Status s = db_->GetLiveFiles(files, &manifest_size);
  28. if (s.ok()) {
  29. auto sst_iter =
  30. std::find_if(files.begin(), files.end(), [](const std::string& file) {
  31. uint64_t number;
  32. FileType type;
  33. bool ok = ParseFileName(file, &number, &type);
  34. return ok && type == kTableFile;
  35. });
  36. *first_sst_path = sst_iter == files.end() ? "" : dbname_ + *sst_iter;
  37. }
  38. return s;
  39. }
  40. void ReopenWithSstIdVerify() {
  41. std::atomic_int verify_passed{0};
  42. SyncPoint::GetInstance()->SetCallBack(
  43. "BlockBasedTable::Open::PassedVerifyUniqueId", [&](void* arg) {
  44. // override job status
  45. auto id = static_cast<UniqueId64x2*>(arg);
  46. assert(*id != kNullUniqueId64x2);
  47. verify_passed++;
  48. });
  49. SyncPoint::GetInstance()->EnableProcessing();
  50. auto options = CurrentOptions();
  51. options.verify_sst_unique_id_in_manifest = true;
  52. Reopen(options);
  53. ASSERT_GT(verify_passed, 0);
  54. SyncPoint::GetInstance()->DisableProcessing();
  55. }
  56. };
  57. TEST_F(RepairTest, SortRepairedDBL0ByEpochNumber) {
  58. Options options = CurrentOptions();
  59. DestroyAndReopen(options);
  60. ASSERT_OK(Put("k1", "oldest"));
  61. ASSERT_OK(Put("k1", "older"));
  62. ASSERT_OK(Flush());
  63. MoveFilesToLevel(1);
  64. ASSERT_OK(Put("k1", "old"));
  65. ASSERT_OK(Flush());
  66. ASSERT_OK(Put("k1", "new"));
  67. std::vector<FileMetaData*> level0_files = GetLevelFileMetadatas(0 /* level*/);
  68. ASSERT_EQ(level0_files.size(), 1);
  69. ASSERT_EQ(level0_files[0]->epoch_number, 2);
  70. std::vector<FileMetaData*> level1_files = GetLevelFileMetadatas(1 /* level*/);
  71. ASSERT_EQ(level1_files.size(), 1);
  72. ASSERT_EQ(level1_files[0]->epoch_number, 1);
  73. std::string manifest_path =
  74. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  75. Close();
  76. ASSERT_OK(env_->FileExists(manifest_path));
  77. ASSERT_OK(env_->DeleteFile(manifest_path));
  78. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  79. ReopenWithSstIdVerify();
  80. EXPECT_EQ(Get("k1"), "new");
  81. level0_files = GetLevelFileMetadatas(0 /* level*/);
  82. ASSERT_EQ(level0_files.size(), 3);
  83. EXPECT_EQ(level0_files[0]->epoch_number, 3);
  84. EXPECT_EQ(level0_files[1]->epoch_number, 2);
  85. EXPECT_EQ(level0_files[2]->epoch_number, 1);
  86. level1_files = GetLevelFileMetadatas(1 /* level*/);
  87. ASSERT_EQ(level1_files.size(), 0);
  88. }
  89. TEST_F(RepairTest, LostManifest) {
  90. // Add a couple SST files, delete the manifest, and verify RepairDB() saves
  91. // the day.
  92. ASSERT_OK(Put("key", "val"));
  93. ASSERT_OK(Flush());
  94. ASSERT_OK(Put("key2", "val2"));
  95. ASSERT_OK(Flush());
  96. // Need to get path before Close() deletes db_, but delete it after Close() to
  97. // ensure Close() didn't change the manifest.
  98. std::string manifest_path =
  99. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  100. Close();
  101. ASSERT_OK(env_->FileExists(manifest_path));
  102. ASSERT_OK(env_->DeleteFile(manifest_path));
  103. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  104. ReopenWithSstIdVerify();
  105. ASSERT_EQ(Get("key"), "val");
  106. ASSERT_EQ(Get("key2"), "val2");
  107. }
  108. TEST_F(RepairTest, LostManifestMoreDbFeatures) {
  109. // Add a couple SST files, delete the manifest, and verify RepairDB() saves
  110. // the day.
  111. ASSERT_OK(Put("key", "val"));
  112. ASSERT_OK(Put("key2", "val2"));
  113. ASSERT_OK(Put("key3", "val3"));
  114. ASSERT_OK(Put("key4", "val4"));
  115. ASSERT_OK(Flush());
  116. // Test an SST file containing only a range tombstone
  117. ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(), "key2",
  118. "key3z"));
  119. ASSERT_OK(Flush());
  120. // Need to get path before Close() deletes db_, but delete it after Close() to
  121. // ensure Close() didn't change the manifest.
  122. std::string manifest_path =
  123. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  124. Close();
  125. ASSERT_OK(env_->FileExists(manifest_path));
  126. ASSERT_OK(env_->DeleteFile(manifest_path));
  127. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  128. // repair from sst should work with unique_id verification
  129. ReopenWithSstIdVerify();
  130. ASSERT_EQ(Get("key"), "val");
  131. ASSERT_EQ(Get("key2"), "NOT_FOUND");
  132. ASSERT_EQ(Get("key3"), "NOT_FOUND");
  133. ASSERT_EQ(Get("key4"), "val4");
  134. }
  135. TEST_F(RepairTest, CorruptManifest) {
  136. // Manifest is in an invalid format. Expect a full recovery.
  137. ASSERT_OK(Put("key", "val"));
  138. ASSERT_OK(Flush());
  139. ASSERT_OK(Put("key2", "val2"));
  140. ASSERT_OK(Flush());
  141. // Need to get path before Close() deletes db_, but overwrite it after Close()
  142. // to ensure Close() didn't change the manifest.
  143. std::string manifest_path =
  144. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  145. Close();
  146. ASSERT_OK(env_->FileExists(manifest_path));
  147. ASSERT_OK(CreateFile(env_->GetFileSystem(), manifest_path, "blah",
  148. false /* use_fsync */));
  149. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  150. ReopenWithSstIdVerify();
  151. ASSERT_EQ(Get("key"), "val");
  152. ASSERT_EQ(Get("key2"), "val2");
  153. }
  154. TEST_F(RepairTest, IncompleteManifest) {
  155. // In this case, the manifest is valid but does not reference all of the SST
  156. // files. Expect a full recovery.
  157. ASSERT_OK(Put("key", "val"));
  158. ASSERT_OK(Flush());
  159. std::string orig_manifest_path =
  160. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  161. CopyFile(orig_manifest_path, orig_manifest_path + ".tmp");
  162. ASSERT_OK(Put("key2", "val2"));
  163. ASSERT_OK(Flush());
  164. // Need to get path before Close() deletes db_, but overwrite it after Close()
  165. // to ensure Close() didn't change the manifest.
  166. std::string new_manifest_path =
  167. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  168. Close();
  169. ASSERT_OK(env_->FileExists(new_manifest_path));
  170. // Replace the manifest with one that is only aware of the first SST file.
  171. CopyFile(orig_manifest_path + ".tmp", new_manifest_path);
  172. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  173. ReopenWithSstIdVerify();
  174. ASSERT_EQ(Get("key"), "val");
  175. ASSERT_EQ(Get("key2"), "val2");
  176. }
  177. TEST_F(RepairTest, PostRepairSstFileNumbering) {
  178. // Verify after a DB is repaired, new files will be assigned higher numbers
  179. // than old files.
  180. ASSERT_OK(Put("key", "val"));
  181. ASSERT_OK(Flush());
  182. ASSERT_OK(Put("key2", "val2"));
  183. ASSERT_OK(Flush());
  184. uint64_t pre_repair_file_num = dbfull()->TEST_Current_Next_FileNo();
  185. Close();
  186. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  187. ReopenWithSstIdVerify();
  188. uint64_t post_repair_file_num = dbfull()->TEST_Current_Next_FileNo();
  189. ASSERT_GE(post_repair_file_num, pre_repair_file_num);
  190. }
  191. TEST_F(RepairTest, LostSst) {
  192. // Delete one of the SST files but preserve the manifest that refers to it,
  193. // then verify the DB is still usable for the intact SST.
  194. ASSERT_OK(Put("key", "val"));
  195. ASSERT_OK(Flush());
  196. ASSERT_OK(Put("key2", "val2"));
  197. ASSERT_OK(Flush());
  198. std::string sst_path;
  199. ASSERT_OK(GetFirstSstPath(&sst_path));
  200. ASSERT_FALSE(sst_path.empty());
  201. ASSERT_OK(env_->DeleteFile(sst_path));
  202. Close();
  203. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  204. ReopenWithSstIdVerify();
  205. // Exactly one of the key-value pairs should be in the DB now.
  206. ASSERT_TRUE((Get("key") == "val") != (Get("key2") == "val2"));
  207. }
  208. TEST_F(RepairTest, CorruptSst) {
  209. // Corrupt one of the SST files but preserve the manifest that refers to it,
  210. // then verify the DB is still usable for the intact SST.
  211. ASSERT_OK(Put("key", "val"));
  212. ASSERT_OK(Flush());
  213. ASSERT_OK(Put("key2", "val2"));
  214. ASSERT_OK(Flush());
  215. std::string sst_path;
  216. ASSERT_OK(GetFirstSstPath(&sst_path));
  217. ASSERT_FALSE(sst_path.empty());
  218. ASSERT_OK(CreateFile(env_->GetFileSystem(), sst_path, "blah",
  219. false /* use_fsync */));
  220. Close();
  221. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  222. ReopenWithSstIdVerify();
  223. // Exactly one of the key-value pairs should be in the DB now.
  224. ASSERT_TRUE((Get("key") == "val") != (Get("key2") == "val2"));
  225. }
  226. TEST_F(RepairTest, UnflushedSst) {
  227. // This test case invokes repair while some data is unflushed, then verifies
  228. // that data is in the db.
  229. ASSERT_OK(Put("key", "val"));
  230. VectorLogPtr wal_files;
  231. ASSERT_OK(dbfull()->GetSortedWalFiles(wal_files));
  232. ASSERT_EQ(wal_files.size(), 1);
  233. {
  234. uint64_t total_ssts_size;
  235. std::unordered_map<std::string, uint64_t> sst_files;
  236. ASSERT_OK(GetAllDataFiles(kTableFile, &sst_files, &total_ssts_size));
  237. ASSERT_EQ(total_ssts_size, 0);
  238. }
  239. // Need to get path before Close() deletes db_, but delete it after Close() to
  240. // ensure Close() didn't change the manifest.
  241. std::string manifest_path =
  242. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  243. Close();
  244. ASSERT_OK(env_->FileExists(manifest_path));
  245. ASSERT_OK(env_->DeleteFile(manifest_path));
  246. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  247. ReopenWithSstIdVerify();
  248. ASSERT_OK(dbfull()->GetSortedWalFiles(wal_files));
  249. ASSERT_EQ(wal_files.size(), 0);
  250. {
  251. uint64_t total_ssts_size;
  252. std::unordered_map<std::string, uint64_t> sst_files;
  253. ASSERT_OK(GetAllDataFiles(kTableFile, &sst_files, &total_ssts_size));
  254. ASSERT_GT(total_ssts_size, 0);
  255. }
  256. ASSERT_EQ(Get("key"), "val");
  257. }
  258. // Test parameters:
  259. // param 0): paranoid file check
  260. // param 1): user-defined timestamp test mode
  261. class RepairTestWithTimestamp
  262. : public DBBasicTestWithTimestampBase,
  263. public testing::WithParamInterface<
  264. std::tuple<bool, test::UserDefinedTimestampTestMode>> {
  265. public:
  266. RepairTestWithTimestamp()
  267. : DBBasicTestWithTimestampBase("repair_test_with_timestamp") {}
  268. Status Put(const Slice& key, const Slice& ts, const Slice& value) {
  269. WriteOptions write_opts;
  270. return db_->Put(write_opts, handles_[0], key, ts, value);
  271. }
  272. void CheckGet(const ReadOptions& read_opts, const Slice& key,
  273. const std::string& expected_value,
  274. const std::string& expected_ts) {
  275. std::string actual_value;
  276. std::string actual_ts;
  277. ASSERT_OK(db_->Get(read_opts, handles_[0], key, &actual_value, &actual_ts));
  278. ASSERT_EQ(expected_value, actual_value);
  279. ASSERT_EQ(expected_ts, actual_ts);
  280. }
  281. void CheckFileBoundaries(const Slice& smallest_user_key,
  282. const Slice& largest_user_key) {
  283. std::vector<std::vector<FileMetaData>> level_to_files;
  284. dbfull()->TEST_GetFilesMetaData(dbfull()->DefaultColumnFamily(),
  285. &level_to_files);
  286. ASSERT_GT(level_to_files.size(), 1);
  287. // L0 only has one SST file.
  288. ASSERT_EQ(level_to_files[0].size(), 1);
  289. auto file_meta = level_to_files[0][0];
  290. ASSERT_EQ(smallest_user_key, file_meta.smallest.user_key());
  291. ASSERT_EQ(largest_user_key, file_meta.largest.user_key());
  292. }
  293. };
  294. TEST_P(RepairTestWithTimestamp, UnflushedSst) {
  295. Destroy(last_options_);
  296. bool paranoid_file_checks = std::get<0>(GetParam());
  297. bool persist_udt = test::ShouldPersistUDT(std::get<1>(GetParam()));
  298. std::string smallest_ukey_without_ts = "bar";
  299. std::string largest_ukey_without_ts = "foo";
  300. Options options = CurrentOptions();
  301. options.env = env_;
  302. options.create_if_missing = true;
  303. std::string min_ts;
  304. std::string write_ts;
  305. PutFixed64(&min_ts, 0);
  306. PutFixed64(&write_ts, 1);
  307. options.comparator = test::BytewiseComparatorWithU64TsWrapper();
  308. options.persist_user_defined_timestamps = persist_udt;
  309. if (!persist_udt) {
  310. options.allow_concurrent_memtable_write = false;
  311. }
  312. options.paranoid_file_checks = paranoid_file_checks;
  313. ColumnFamilyOptions cf_options(options);
  314. std::vector<ColumnFamilyDescriptor> column_families;
  315. column_families.emplace_back(kDefaultColumnFamilyName, cf_options);
  316. ASSERT_OK(DB::Open(options, dbname_, column_families, &handles_, &db_));
  317. ASSERT_OK(Put(smallest_ukey_without_ts, write_ts,
  318. smallest_ukey_without_ts + ":val"));
  319. ASSERT_OK(
  320. Put(largest_ukey_without_ts, write_ts, largest_ukey_without_ts + ":val"));
  321. VectorLogPtr wal_files;
  322. ASSERT_OK(dbfull()->GetSortedWalFiles(wal_files));
  323. ASSERT_EQ(wal_files.size(), 1);
  324. {
  325. uint64_t total_ssts_size;
  326. std::unordered_map<std::string, uint64_t> sst_files;
  327. ASSERT_OK(GetAllDataFiles(kTableFile, &sst_files, &total_ssts_size));
  328. ASSERT_EQ(total_ssts_size, 0);
  329. }
  330. // Need to get path before Close() deletes db_, but delete it after Close() to
  331. // ensure Close() didn't change the manifest.
  332. std::string manifest_path =
  333. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  334. Close();
  335. ASSERT_OK(env_->FileExists(manifest_path));
  336. ASSERT_OK(env_->DeleteFile(manifest_path));
  337. ASSERT_OK(RepairDB(dbname_, options));
  338. ASSERT_OK(DB::Open(options, dbname_, column_families, &handles_, &db_));
  339. ASSERT_OK(dbfull()->GetSortedWalFiles(wal_files));
  340. ASSERT_EQ(wal_files.size(), 0);
  341. {
  342. uint64_t total_ssts_size;
  343. std::unordered_map<std::string, uint64_t> sst_files;
  344. ASSERT_OK(GetAllDataFiles(kTableFile, &sst_files, &total_ssts_size));
  345. ASSERT_GT(total_ssts_size, 0);
  346. }
  347. // Check file boundaries are correct for different
  348. // `persist_user_defined_timestamps` option values.
  349. if (persist_udt) {
  350. CheckFileBoundaries(smallest_ukey_without_ts + write_ts,
  351. largest_ukey_without_ts + write_ts);
  352. } else {
  353. CheckFileBoundaries(smallest_ukey_without_ts + min_ts,
  354. largest_ukey_without_ts + min_ts);
  355. }
  356. ReadOptions read_opts;
  357. Slice read_ts_slice = write_ts;
  358. read_opts.timestamp = &read_ts_slice;
  359. if (persist_udt) {
  360. CheckGet(read_opts, smallest_ukey_without_ts,
  361. smallest_ukey_without_ts + ":val", write_ts);
  362. CheckGet(read_opts, largest_ukey_without_ts,
  363. largest_ukey_without_ts + ":val", write_ts);
  364. } else {
  365. // TODO(yuzhangyu): currently when `persist_user_defined_timestamps` is
  366. // false, ts is unconditionally stripped during flush.
  367. // When `full_history_ts_low` is set and respected during flush.
  368. // We should prohibit reading below `full_history_ts_low` all together.
  369. CheckGet(read_opts, smallest_ukey_without_ts,
  370. smallest_ukey_without_ts + ":val", min_ts);
  371. CheckGet(read_opts, largest_ukey_without_ts,
  372. largest_ukey_without_ts + ":val", min_ts);
  373. }
  374. }
  375. // Param 0: paranoid file check
  376. // Param 1: test mode for the user-defined timestamp feature
  377. INSTANTIATE_TEST_CASE_P(
  378. UnflushedSst, RepairTestWithTimestamp,
  379. ::testing::Combine(
  380. ::testing::Bool(),
  381. ::testing::Values(
  382. test::UserDefinedTimestampTestMode::kStripUserDefinedTimestamp,
  383. test::UserDefinedTimestampTestMode::kNormal)));
  384. TEST_F(RepairTest, SeparateWalDir) {
  385. do {
  386. Options options = CurrentOptions();
  387. DestroyAndReopen(options);
  388. ASSERT_OK(Put("key", "val"));
  389. ASSERT_OK(Put("foo", "bar"));
  390. VectorLogPtr wal_files;
  391. ASSERT_OK(dbfull()->GetSortedWalFiles(wal_files));
  392. ASSERT_EQ(wal_files.size(), 1);
  393. {
  394. uint64_t total_ssts_size;
  395. std::unordered_map<std::string, uint64_t> sst_files;
  396. ASSERT_OK(GetAllDataFiles(kTableFile, &sst_files, &total_ssts_size));
  397. ASSERT_EQ(total_ssts_size, 0);
  398. }
  399. std::string manifest_path =
  400. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  401. Close();
  402. ASSERT_OK(env_->FileExists(manifest_path));
  403. ASSERT_OK(env_->DeleteFile(manifest_path));
  404. ASSERT_OK(RepairDB(dbname_, options));
  405. // make sure that all WALs are converted to SSTables.
  406. options.wal_dir = "";
  407. ReopenWithSstIdVerify();
  408. ASSERT_OK(dbfull()->GetSortedWalFiles(wal_files));
  409. ASSERT_EQ(wal_files.size(), 0);
  410. {
  411. uint64_t total_ssts_size;
  412. std::unordered_map<std::string, uint64_t> sst_files;
  413. ASSERT_OK(GetAllDataFiles(kTableFile, &sst_files, &total_ssts_size));
  414. ASSERT_GT(total_ssts_size, 0);
  415. }
  416. ASSERT_EQ(Get("key"), "val");
  417. ASSERT_EQ(Get("foo"), "bar");
  418. } while (ChangeWalOptions());
  419. }
  420. TEST_F(RepairTest, RepairMultipleColumnFamilies) {
  421. // Verify repair logic associates SST files with their original column
  422. // families.
  423. const int kNumCfs = 3;
  424. const int kEntriesPerCf = 2;
  425. DestroyAndReopen(CurrentOptions());
  426. CreateAndReopenWithCF({"pikachu1", "pikachu2"}, CurrentOptions());
  427. for (int i = 0; i < kNumCfs; ++i) {
  428. for (int j = 0; j < kEntriesPerCf; ++j) {
  429. ASSERT_OK(Put(i, "key" + std::to_string(j), "val" + std::to_string(j)));
  430. if (j == kEntriesPerCf - 1 && i == kNumCfs - 1) {
  431. // Leave one unflushed so we can verify WAL entries are properly
  432. // associated with column families.
  433. continue;
  434. }
  435. ASSERT_OK(Flush(i));
  436. }
  437. }
  438. // Need to get path before Close() deletes db_, but delete it after Close() to
  439. // ensure Close() doesn't re-create the manifest.
  440. std::string manifest_path =
  441. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  442. Close();
  443. ASSERT_OK(env_->FileExists(manifest_path));
  444. ASSERT_OK(env_->DeleteFile(manifest_path));
  445. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  446. ReopenWithColumnFamilies({"default", "pikachu1", "pikachu2"},
  447. CurrentOptions());
  448. for (int i = 0; i < kNumCfs; ++i) {
  449. for (int j = 0; j < kEntriesPerCf; ++j) {
  450. ASSERT_EQ(Get(i, "key" + std::to_string(j)), "val" + std::to_string(j));
  451. }
  452. }
  453. }
  454. TEST_F(RepairTest, RepairColumnFamilyOptions) {
  455. // Verify repair logic uses correct ColumnFamilyOptions when repairing a
  456. // database with different options for column families.
  457. const int kNumCfs = 2;
  458. const int kEntriesPerCf = 2;
  459. Options opts(CurrentOptions()), rev_opts(CurrentOptions());
  460. opts.comparator = BytewiseComparator();
  461. rev_opts.comparator = ReverseBytewiseComparator();
  462. DestroyAndReopen(opts);
  463. CreateColumnFamilies({"reverse"}, rev_opts);
  464. ReopenWithColumnFamilies({"default", "reverse"},
  465. std::vector<Options>{opts, rev_opts});
  466. for (int i = 0; i < kNumCfs; ++i) {
  467. for (int j = 0; j < kEntriesPerCf; ++j) {
  468. ASSERT_OK(Put(i, "key" + std::to_string(j), "val" + std::to_string(j)));
  469. if (i == kNumCfs - 1 && j == kEntriesPerCf - 1) {
  470. // Leave one unflushed so we can verify RepairDB's flush logic
  471. continue;
  472. }
  473. ASSERT_OK(Flush(i));
  474. }
  475. }
  476. Close();
  477. // RepairDB() records the comparator in the manifest, and DB::Open would fail
  478. // if a different comparator were used.
  479. ASSERT_OK(RepairDB(dbname_, opts, {{"default", opts}, {"reverse", rev_opts}},
  480. opts /* unknown_cf_opts */));
  481. ASSERT_OK(TryReopenWithColumnFamilies({"default", "reverse"},
  482. std::vector<Options>{opts, rev_opts}));
  483. for (int i = 0; i < kNumCfs; ++i) {
  484. for (int j = 0; j < kEntriesPerCf; ++j) {
  485. ASSERT_EQ(Get(i, "key" + std::to_string(j)), "val" + std::to_string(j));
  486. }
  487. }
  488. // Examine table properties to verify RepairDB() used the right options when
  489. // converting WAL->SST
  490. TablePropertiesCollection fname_to_props;
  491. ASSERT_OK(db_->GetPropertiesOfAllTables(handles_[1], &fname_to_props));
  492. ASSERT_EQ(fname_to_props.size(), 2U);
  493. for (const auto& fname_and_props : fname_to_props) {
  494. std::string comparator_name(rev_opts.comparator->Name());
  495. ASSERT_EQ(comparator_name, fname_and_props.second->comparator_name);
  496. }
  497. Close();
  498. // Also check comparator when it's provided via "unknown" CF options
  499. ASSERT_OK(RepairDB(dbname_, opts, {{"default", opts}},
  500. rev_opts /* unknown_cf_opts */));
  501. ASSERT_OK(TryReopenWithColumnFamilies({"default", "reverse"},
  502. std::vector<Options>{opts, rev_opts}));
  503. for (int i = 0; i < kNumCfs; ++i) {
  504. for (int j = 0; j < kEntriesPerCf; ++j) {
  505. ASSERT_EQ(Get(i, "key" + std::to_string(j)), "val" + std::to_string(j));
  506. }
  507. }
  508. }
  509. TEST_F(RepairTest, DbNameContainsTrailingSlash) {
  510. {
  511. bool tmp;
  512. if (env_->AreFilesSame("", "", &tmp).IsNotSupported()) {
  513. fprintf(stderr,
  514. "skipping RepairTest.DbNameContainsTrailingSlash due to "
  515. "unsupported Env::AreFilesSame\n");
  516. return;
  517. }
  518. }
  519. ASSERT_OK(Put("key", "val"));
  520. ASSERT_OK(Flush());
  521. Close();
  522. ASSERT_OK(RepairDB(dbname_ + "/", CurrentOptions()));
  523. ReopenWithSstIdVerify();
  524. ASSERT_EQ(Get("key"), "val");
  525. }
  526. } // namespace ROCKSDB_NAMESPACE
  527. int main(int argc, char** argv) {
  528. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  529. ::testing::InitGoogleTest(&argc, argv);
  530. return RUN_ALL_TESTS();
  531. }