repair_test.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. #ifndef ROCKSDB_LITE
  6. #include <algorithm>
  7. #include <string>
  8. #include <vector>
  9. #include "db/db_impl/db_impl.h"
  10. #include "db/db_test_util.h"
  11. #include "file/file_util.h"
  12. #include "rocksdb/comparator.h"
  13. #include "rocksdb/db.h"
  14. #include "rocksdb/transaction_log.h"
  15. #include "util/string_util.h"
  16. namespace ROCKSDB_NAMESPACE {
  17. #ifndef ROCKSDB_LITE
  18. class RepairTest : public DBTestBase {
  19. public:
  20. RepairTest() : DBTestBase("/repair_test") {}
  21. std::string GetFirstSstPath() {
  22. uint64_t manifest_size;
  23. std::vector<std::string> files;
  24. db_->GetLiveFiles(files, &manifest_size);
  25. auto sst_iter =
  26. std::find_if(files.begin(), files.end(), [](const std::string& file) {
  27. uint64_t number;
  28. FileType type;
  29. bool ok = ParseFileName(file, &number, &type);
  30. return ok && type == kTableFile;
  31. });
  32. return sst_iter == files.end() ? "" : dbname_ + *sst_iter;
  33. }
  34. };
  35. TEST_F(RepairTest, LostManifest) {
  36. // Add a couple SST files, delete the manifest, and verify RepairDB() saves
  37. // the day.
  38. Put("key", "val");
  39. Flush();
  40. Put("key2", "val2");
  41. Flush();
  42. // Need to get path before Close() deletes db_, but delete it after Close() to
  43. // ensure Close() didn't change the manifest.
  44. std::string manifest_path =
  45. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  46. Close();
  47. ASSERT_OK(env_->FileExists(manifest_path));
  48. ASSERT_OK(env_->DeleteFile(manifest_path));
  49. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  50. Reopen(CurrentOptions());
  51. ASSERT_EQ(Get("key"), "val");
  52. ASSERT_EQ(Get("key2"), "val2");
  53. }
  54. TEST_F(RepairTest, CorruptManifest) {
  55. // Manifest is in an invalid format. Expect a full recovery.
  56. Put("key", "val");
  57. Flush();
  58. Put("key2", "val2");
  59. Flush();
  60. // Need to get path before Close() deletes db_, but overwrite it after Close()
  61. // to ensure Close() didn't change the manifest.
  62. std::string manifest_path =
  63. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  64. Close();
  65. ASSERT_OK(env_->FileExists(manifest_path));
  66. LegacyFileSystemWrapper fs(env_);
  67. CreateFile(&fs, manifest_path, "blah", false /* use_fsync */);
  68. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  69. Reopen(CurrentOptions());
  70. ASSERT_EQ(Get("key"), "val");
  71. ASSERT_EQ(Get("key2"), "val2");
  72. }
  73. TEST_F(RepairTest, IncompleteManifest) {
  74. // In this case, the manifest is valid but does not reference all of the SST
  75. // files. Expect a full recovery.
  76. Put("key", "val");
  77. Flush();
  78. std::string orig_manifest_path =
  79. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  80. CopyFile(orig_manifest_path, orig_manifest_path + ".tmp");
  81. Put("key2", "val2");
  82. Flush();
  83. // Need to get path before Close() deletes db_, but overwrite it after Close()
  84. // to ensure Close() didn't change the manifest.
  85. std::string new_manifest_path =
  86. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  87. Close();
  88. ASSERT_OK(env_->FileExists(new_manifest_path));
  89. // Replace the manifest with one that is only aware of the first SST file.
  90. CopyFile(orig_manifest_path + ".tmp", new_manifest_path);
  91. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  92. Reopen(CurrentOptions());
  93. ASSERT_EQ(Get("key"), "val");
  94. ASSERT_EQ(Get("key2"), "val2");
  95. }
  96. TEST_F(RepairTest, PostRepairSstFileNumbering) {
  97. // Verify after a DB is repaired, new files will be assigned higher numbers
  98. // than old files.
  99. Put("key", "val");
  100. Flush();
  101. Put("key2", "val2");
  102. Flush();
  103. uint64_t pre_repair_file_num = dbfull()->TEST_Current_Next_FileNo();
  104. Close();
  105. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  106. Reopen(CurrentOptions());
  107. uint64_t post_repair_file_num = dbfull()->TEST_Current_Next_FileNo();
  108. ASSERT_GE(post_repair_file_num, pre_repair_file_num);
  109. }
  110. TEST_F(RepairTest, LostSst) {
  111. // Delete one of the SST files but preserve the manifest that refers to it,
  112. // then verify the DB is still usable for the intact SST.
  113. Put("key", "val");
  114. Flush();
  115. Put("key2", "val2");
  116. Flush();
  117. auto sst_path = GetFirstSstPath();
  118. ASSERT_FALSE(sst_path.empty());
  119. ASSERT_OK(env_->DeleteFile(sst_path));
  120. Close();
  121. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  122. Reopen(CurrentOptions());
  123. // Exactly one of the key-value pairs should be in the DB now.
  124. ASSERT_TRUE((Get("key") == "val") != (Get("key2") == "val2"));
  125. }
  126. TEST_F(RepairTest, CorruptSst) {
  127. // Corrupt one of the SST files but preserve the manifest that refers to it,
  128. // then verify the DB is still usable for the intact SST.
  129. Put("key", "val");
  130. Flush();
  131. Put("key2", "val2");
  132. Flush();
  133. auto sst_path = GetFirstSstPath();
  134. ASSERT_FALSE(sst_path.empty());
  135. LegacyFileSystemWrapper fs(env_);
  136. CreateFile(&fs, sst_path, "blah", false /* use_fsync */);
  137. Close();
  138. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  139. Reopen(CurrentOptions());
  140. // Exactly one of the key-value pairs should be in the DB now.
  141. ASSERT_TRUE((Get("key") == "val") != (Get("key2") == "val2"));
  142. }
  143. TEST_F(RepairTest, UnflushedSst) {
  144. // This test case invokes repair while some data is unflushed, then verifies
  145. // that data is in the db.
  146. Put("key", "val");
  147. VectorLogPtr wal_files;
  148. ASSERT_OK(dbfull()->GetSortedWalFiles(wal_files));
  149. ASSERT_EQ(wal_files.size(), 1);
  150. uint64_t total_ssts_size;
  151. GetAllSSTFiles(&total_ssts_size);
  152. ASSERT_EQ(total_ssts_size, 0);
  153. // Need to get path before Close() deletes db_, but delete it after Close() to
  154. // ensure Close() didn't change the manifest.
  155. std::string manifest_path =
  156. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  157. Close();
  158. ASSERT_OK(env_->FileExists(manifest_path));
  159. ASSERT_OK(env_->DeleteFile(manifest_path));
  160. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  161. Reopen(CurrentOptions());
  162. ASSERT_OK(dbfull()->GetSortedWalFiles(wal_files));
  163. ASSERT_EQ(wal_files.size(), 0);
  164. GetAllSSTFiles(&total_ssts_size);
  165. ASSERT_GT(total_ssts_size, 0);
  166. ASSERT_EQ(Get("key"), "val");
  167. }
  168. TEST_F(RepairTest, SeparateWalDir) {
  169. do {
  170. Options options = CurrentOptions();
  171. DestroyAndReopen(options);
  172. Put("key", "val");
  173. Put("foo", "bar");
  174. VectorLogPtr wal_files;
  175. ASSERT_OK(dbfull()->GetSortedWalFiles(wal_files));
  176. ASSERT_EQ(wal_files.size(), 1);
  177. uint64_t total_ssts_size;
  178. GetAllSSTFiles(&total_ssts_size);
  179. ASSERT_EQ(total_ssts_size, 0);
  180. std::string manifest_path =
  181. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  182. Close();
  183. ASSERT_OK(env_->FileExists(manifest_path));
  184. ASSERT_OK(env_->DeleteFile(manifest_path));
  185. ASSERT_OK(RepairDB(dbname_, options));
  186. // make sure that all WALs are converted to SSTables.
  187. options.wal_dir = "";
  188. Reopen(options);
  189. ASSERT_OK(dbfull()->GetSortedWalFiles(wal_files));
  190. ASSERT_EQ(wal_files.size(), 0);
  191. GetAllSSTFiles(&total_ssts_size);
  192. ASSERT_GT(total_ssts_size, 0);
  193. ASSERT_EQ(Get("key"), "val");
  194. ASSERT_EQ(Get("foo"), "bar");
  195. } while(ChangeWalOptions());
  196. }
  197. TEST_F(RepairTest, RepairMultipleColumnFamilies) {
  198. // Verify repair logic associates SST files with their original column
  199. // families.
  200. const int kNumCfs = 3;
  201. const int kEntriesPerCf = 2;
  202. DestroyAndReopen(CurrentOptions());
  203. CreateAndReopenWithCF({"pikachu1", "pikachu2"}, CurrentOptions());
  204. for (int i = 0; i < kNumCfs; ++i) {
  205. for (int j = 0; j < kEntriesPerCf; ++j) {
  206. Put(i, "key" + ToString(j), "val" + ToString(j));
  207. if (j == kEntriesPerCf - 1 && i == kNumCfs - 1) {
  208. // Leave one unflushed so we can verify WAL entries are properly
  209. // associated with column families.
  210. continue;
  211. }
  212. Flush(i);
  213. }
  214. }
  215. // Need to get path before Close() deletes db_, but delete it after Close() to
  216. // ensure Close() doesn't re-create the manifest.
  217. std::string manifest_path =
  218. DescriptorFileName(dbname_, dbfull()->TEST_Current_Manifest_FileNo());
  219. Close();
  220. ASSERT_OK(env_->FileExists(manifest_path));
  221. ASSERT_OK(env_->DeleteFile(manifest_path));
  222. ASSERT_OK(RepairDB(dbname_, CurrentOptions()));
  223. ReopenWithColumnFamilies({"default", "pikachu1", "pikachu2"},
  224. CurrentOptions());
  225. for (int i = 0; i < kNumCfs; ++i) {
  226. for (int j = 0; j < kEntriesPerCf; ++j) {
  227. ASSERT_EQ(Get(i, "key" + ToString(j)), "val" + ToString(j));
  228. }
  229. }
  230. }
  231. TEST_F(RepairTest, RepairColumnFamilyOptions) {
  232. // Verify repair logic uses correct ColumnFamilyOptions when repairing a
  233. // database with different options for column families.
  234. const int kNumCfs = 2;
  235. const int kEntriesPerCf = 2;
  236. Options opts(CurrentOptions()), rev_opts(CurrentOptions());
  237. opts.comparator = BytewiseComparator();
  238. rev_opts.comparator = ReverseBytewiseComparator();
  239. DestroyAndReopen(opts);
  240. CreateColumnFamilies({"reverse"}, rev_opts);
  241. ReopenWithColumnFamilies({"default", "reverse"},
  242. std::vector<Options>{opts, rev_opts});
  243. for (int i = 0; i < kNumCfs; ++i) {
  244. for (int j = 0; j < kEntriesPerCf; ++j) {
  245. Put(i, "key" + ToString(j), "val" + ToString(j));
  246. if (i == kNumCfs - 1 && j == kEntriesPerCf - 1) {
  247. // Leave one unflushed so we can verify RepairDB's flush logic
  248. continue;
  249. }
  250. Flush(i);
  251. }
  252. }
  253. Close();
  254. // RepairDB() records the comparator in the manifest, and DB::Open would fail
  255. // if a different comparator were used.
  256. ASSERT_OK(RepairDB(dbname_, opts, {{"default", opts}, {"reverse", rev_opts}},
  257. opts /* unknown_cf_opts */));
  258. ASSERT_OK(TryReopenWithColumnFamilies({"default", "reverse"},
  259. std::vector<Options>{opts, rev_opts}));
  260. for (int i = 0; i < kNumCfs; ++i) {
  261. for (int j = 0; j < kEntriesPerCf; ++j) {
  262. ASSERT_EQ(Get(i, "key" + ToString(j)), "val" + ToString(j));
  263. }
  264. }
  265. // Examine table properties to verify RepairDB() used the right options when
  266. // converting WAL->SST
  267. TablePropertiesCollection fname_to_props;
  268. db_->GetPropertiesOfAllTables(handles_[1], &fname_to_props);
  269. ASSERT_EQ(fname_to_props.size(), 2U);
  270. for (const auto& fname_and_props : fname_to_props) {
  271. std::string comparator_name (
  272. InternalKeyComparator(rev_opts.comparator).Name());
  273. comparator_name = comparator_name.substr(comparator_name.find(':') + 1);
  274. ASSERT_EQ(comparator_name,
  275. fname_and_props.second->comparator_name);
  276. }
  277. Close();
  278. // Also check comparator when it's provided via "unknown" CF options
  279. ASSERT_OK(RepairDB(dbname_, opts, {{"default", opts}},
  280. rev_opts /* unknown_cf_opts */));
  281. ASSERT_OK(TryReopenWithColumnFamilies({"default", "reverse"},
  282. std::vector<Options>{opts, rev_opts}));
  283. for (int i = 0; i < kNumCfs; ++i) {
  284. for (int j = 0; j < kEntriesPerCf; ++j) {
  285. ASSERT_EQ(Get(i, "key" + ToString(j)), "val" + ToString(j));
  286. }
  287. }
  288. }
  289. TEST_F(RepairTest, DbNameContainsTrailingSlash) {
  290. {
  291. bool tmp;
  292. if (env_->AreFilesSame("", "", &tmp).IsNotSupported()) {
  293. fprintf(stderr,
  294. "skipping RepairTest.DbNameContainsTrailingSlash due to "
  295. "unsupported Env::AreFilesSame\n");
  296. return;
  297. }
  298. }
  299. Put("key", "val");
  300. Flush();
  301. Close();
  302. ASSERT_OK(RepairDB(dbname_ + "/", CurrentOptions()));
  303. Reopen(CurrentOptions());
  304. ASSERT_EQ(Get("key"), "val");
  305. }
  306. #endif // ROCKSDB_LITE
  307. } // namespace ROCKSDB_NAMESPACE
  308. int main(int argc, char** argv) {
  309. ::testing::InitGoogleTest(&argc, argv);
  310. return RUN_ALL_TESTS();
  311. }
  312. #else
  313. #include <stdio.h>
  314. int main(int /*argc*/, char** /*argv*/) {
  315. fprintf(stderr, "SKIPPED as RepairDB is not supported in ROCKSDB_LITE\n");
  316. return 0;
  317. }
  318. #endif // ROCKSDB_LITE