deletefile_test.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. #include <cstdlib>
  10. #include <map>
  11. #include <string>
  12. #include <vector>
  13. #include "db/db_impl/db_impl.h"
  14. #include "db/db_test_util.h"
  15. #include "db/version_set.h"
  16. #include "db/write_batch_internal.h"
  17. #include "file/filename.h"
  18. #include "port/stack_trace.h"
  19. #include "rocksdb/db.h"
  20. #include "rocksdb/env.h"
  21. #include "rocksdb/transaction_log.h"
  22. #include "test_util/sync_point.h"
  23. #include "test_util/testharness.h"
  24. #include "test_util/testutil.h"
  25. #include "util/string_util.h"
  26. namespace ROCKSDB_NAMESPACE {
  27. class DeleteFileTest : public DBTestBase {
  28. public:
  29. const int numlevels_;
  30. const std::string wal_dir_;
  31. DeleteFileTest()
  32. : DBTestBase("deletefile_test", /*env_do_fsync=*/true),
  33. numlevels_(7),
  34. wal_dir_(dbname_ + "/wal_files") {}
  35. void SetOptions(Options* options) {
  36. ASSERT_NE(options, nullptr);
  37. options->delete_obsolete_files_period_micros = 0; // always do full purge
  38. options->enable_thread_tracking = true;
  39. options->write_buffer_size = 1024 * 1024 * 1000;
  40. options->target_file_size_base = 1024 * 1024 * 1000;
  41. options->max_bytes_for_level_base = 1024 * 1024 * 1000;
  42. options->WAL_ttl_seconds = 300; // Used to test log files
  43. options->WAL_size_limit_MB = 1024; // Used to test log files
  44. options->wal_dir = wal_dir_;
  45. }
  46. void AddKeys(int numkeys, int startkey = 0) {
  47. WriteOptions options;
  48. options.sync = false;
  49. ReadOptions roptions;
  50. for (int i = startkey; i < (numkeys + startkey); i++) {
  51. std::string temp = std::to_string(i);
  52. Slice key(temp);
  53. Slice value(temp);
  54. ASSERT_OK(db_->Put(options, key, value));
  55. }
  56. }
  57. int numKeysInLevels(std::vector<LiveFileMetaData>& metadata,
  58. std::vector<int>* keysperlevel = nullptr) {
  59. if (keysperlevel != nullptr) {
  60. keysperlevel->resize(numlevels_);
  61. }
  62. int numKeys = 0;
  63. for (size_t i = 0; i < metadata.size(); i++) {
  64. int startkey = atoi(metadata[i].smallestkey.c_str());
  65. int endkey = atoi(metadata[i].largestkey.c_str());
  66. int numkeysinfile = (endkey - startkey + 1);
  67. numKeys += numkeysinfile;
  68. if (keysperlevel != nullptr) {
  69. (*keysperlevel)[(int)metadata[i].level] += numkeysinfile;
  70. }
  71. fprintf(stderr, "level %d name %s smallest %s largest %s\n",
  72. metadata[i].level, metadata[i].name.c_str(),
  73. metadata[i].smallestkey.c_str(), metadata[i].largestkey.c_str());
  74. }
  75. return numKeys;
  76. }
  77. void CreateTwoLevels() {
  78. AddKeys(50000, 10000);
  79. ASSERT_OK(dbfull()->TEST_FlushMemTable());
  80. ASSERT_OK(dbfull()->TEST_WaitForFlushMemTable());
  81. for (int i = 0; i < 2; ++i) {
  82. ASSERT_OK(dbfull()->TEST_CompactRange(i, nullptr, nullptr));
  83. }
  84. AddKeys(50000, 10000);
  85. ASSERT_OK(dbfull()->TEST_FlushMemTable());
  86. ASSERT_OK(dbfull()->TEST_WaitForFlushMemTable());
  87. ASSERT_OK(dbfull()->TEST_CompactRange(0, nullptr, nullptr));
  88. }
  89. void CheckFileTypeCounts(const std::string& dir, int required_log,
  90. int required_sst, int required_manifest) {
  91. std::vector<std::string> filenames;
  92. ASSERT_OK(env_->GetChildren(dir, &filenames));
  93. int log_cnt = 0, sst_cnt = 0, manifest_cnt = 0;
  94. for (const auto& file : filenames) {
  95. uint64_t number;
  96. FileType type;
  97. if (ParseFileName(file, &number, &type)) {
  98. log_cnt += (type == kWalFile);
  99. sst_cnt += (type == kTableFile);
  100. manifest_cnt += (type == kDescriptorFile);
  101. }
  102. }
  103. if (required_log >= 0) {
  104. ASSERT_EQ(required_log, log_cnt);
  105. }
  106. if (required_sst >= 0) {
  107. ASSERT_EQ(required_sst, sst_cnt);
  108. }
  109. if (required_manifest >= 0) {
  110. ASSERT_EQ(required_manifest, manifest_cnt);
  111. }
  112. }
  113. static void DoSleep(void* arg) {
  114. auto test = static_cast<DeleteFileTest*>(arg);
  115. test->env_->SleepForMicroseconds(2 * 1000 * 1000);
  116. }
  117. // An empty job to guard all jobs are processed
  118. static void GuardFinish(void* /*arg*/) {
  119. TEST_SYNC_POINT("DeleteFileTest::GuardFinish");
  120. }
  121. };
  122. TEST_F(DeleteFileTest, PurgeObsoleteFilesTest) {
  123. Options options = CurrentOptions();
  124. SetOptions(&options);
  125. Destroy(options);
  126. options.create_if_missing = true;
  127. Reopen(options);
  128. CreateTwoLevels();
  129. // there should be only one (empty) log file because CreateTwoLevels()
  130. // flushes the memtables to disk
  131. CheckFileTypeCounts(wal_dir_, 1, 0, 0);
  132. // 2 ssts, 1 manifest
  133. CheckFileTypeCounts(dbname_, 0, 2, 1);
  134. std::string first("0"), last("999999");
  135. CompactRangeOptions compact_options;
  136. compact_options.change_level = true;
  137. compact_options.target_level = 2;
  138. Slice first_slice(first), last_slice(last);
  139. ASSERT_OK(db_->CompactRange(compact_options, &first_slice, &last_slice));
  140. // 1 sst after compaction
  141. CheckFileTypeCounts(dbname_, 0, 1, 1);
  142. // this time, we keep an iterator alive
  143. Reopen(options);
  144. Iterator* itr = nullptr;
  145. CreateTwoLevels();
  146. itr = db_->NewIterator(ReadOptions());
  147. ASSERT_OK(itr->status());
  148. ASSERT_OK(db_->CompactRange(compact_options, &first_slice, &last_slice));
  149. ASSERT_OK(itr->status());
  150. // 3 sst after compaction with live iterator
  151. CheckFileTypeCounts(dbname_, 0, 3, 1);
  152. delete itr;
  153. // 1 sst after iterator deletion
  154. CheckFileTypeCounts(dbname_, 0, 1, 1);
  155. }
  156. TEST_F(DeleteFileTest, WaitForCompactWithWaitForPurgeOptionTest) {
  157. Options options = CurrentOptions();
  158. SetOptions(&options);
  159. Destroy(options);
  160. options.create_if_missing = true;
  161. Reopen(options);
  162. std::string first("0"), last("999999");
  163. CompactRangeOptions compact_options;
  164. compact_options.change_level = true;
  165. compact_options.target_level = 2;
  166. Slice first_slice(first), last_slice(last);
  167. CreateTwoLevels();
  168. Iterator* itr = nullptr;
  169. ReadOptions read_options;
  170. read_options.background_purge_on_iterator_cleanup = true;
  171. itr = db_->NewIterator(read_options);
  172. ASSERT_OK(itr->status());
  173. ASSERT_OK(db_->CompactRange(compact_options, &first_slice, &last_slice));
  174. SyncPoint::GetInstance()->LoadDependency(
  175. {{"DBImpl::BGWorkPurge:start", "DeleteFileTest::WaitForPurgeTest"},
  176. {"DBImpl::WaitForCompact:InsideLoop",
  177. "DBImpl::BackgroundCallPurge:beforeMutexLock"}});
  178. SyncPoint::GetInstance()->EnableProcessing();
  179. delete itr;
  180. TEST_SYNC_POINT("DeleteFileTest::WaitForPurgeTest");
  181. // At this point, purge got started, but can't finish due to sync points
  182. // not purged yet
  183. CheckFileTypeCounts(dbname_, 0, 3, 1);
  184. // The sync point in WaitForCompact should unblock the purge
  185. WaitForCompactOptions wait_for_compact_options;
  186. wait_for_compact_options.wait_for_purge = true;
  187. Status s = dbfull()->WaitForCompact(wait_for_compact_options);
  188. ASSERT_OK(s);
  189. // Now files should be purged
  190. CheckFileTypeCounts(dbname_, 0, 1, 1);
  191. }
  192. TEST_F(DeleteFileTest, BackgroundPurgeIteratorTest) {
  193. Options options = CurrentOptions();
  194. SetOptions(&options);
  195. Destroy(options);
  196. options.create_if_missing = true;
  197. Reopen(options);
  198. std::string first("0"), last("999999");
  199. CompactRangeOptions compact_options;
  200. compact_options.change_level = true;
  201. compact_options.target_level = 2;
  202. Slice first_slice(first), last_slice(last);
  203. // We keep an iterator alive
  204. Iterator* itr = nullptr;
  205. CreateTwoLevels();
  206. ReadOptions read_options;
  207. read_options.background_purge_on_iterator_cleanup = true;
  208. itr = db_->NewIterator(read_options);
  209. ASSERT_OK(itr->status());
  210. ASSERT_OK(db_->CompactRange(compact_options, &first_slice, &last_slice));
  211. // 3 sst after compaction with live iterator
  212. CheckFileTypeCounts(dbname_, 0, 3, 1);
  213. test::SleepingBackgroundTask sleeping_task_before;
  214. env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask,
  215. &sleeping_task_before, Env::Priority::HIGH);
  216. delete itr;
  217. test::SleepingBackgroundTask sleeping_task_after;
  218. env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask,
  219. &sleeping_task_after, Env::Priority::HIGH);
  220. // Make sure no purges are executed foreground
  221. CheckFileTypeCounts(dbname_, 0, 3, 1);
  222. sleeping_task_before.WakeUp();
  223. sleeping_task_before.WaitUntilDone();
  224. // Make sure all background purges are executed
  225. sleeping_task_after.WakeUp();
  226. sleeping_task_after.WaitUntilDone();
  227. // 1 sst after iterator deletion
  228. CheckFileTypeCounts(dbname_, 0, 1, 1);
  229. }
  230. TEST_F(DeleteFileTest, PurgeDuringOpen) {
  231. Options options = CurrentOptions();
  232. CheckFileTypeCounts(dbname_, -1, 0, -1);
  233. Close();
  234. std::unique_ptr<WritableFile> file;
  235. ASSERT_OK(options.env->NewWritableFile(dbname_ + "/000002.sst", &file,
  236. EnvOptions()));
  237. ASSERT_OK(file->Close());
  238. CheckFileTypeCounts(dbname_, -1, 1, -1);
  239. options.avoid_unnecessary_blocking_io = false;
  240. options.create_if_missing = false;
  241. Reopen(options);
  242. CheckFileTypeCounts(dbname_, -1, 0, -1);
  243. Close();
  244. // test background purge
  245. options.avoid_unnecessary_blocking_io = true;
  246. options.create_if_missing = false;
  247. ASSERT_OK(options.env->NewWritableFile(dbname_ + "/000002.sst", &file,
  248. EnvOptions()));
  249. ASSERT_OK(file->Close());
  250. CheckFileTypeCounts(dbname_, -1, 1, -1);
  251. SyncPoint::GetInstance()->DisableProcessing();
  252. SyncPoint::GetInstance()->ClearAllCallBacks();
  253. SyncPoint::GetInstance()->LoadDependency(
  254. {{"DeleteFileTest::PurgeDuringOpen:1", "DBImpl::BGWorkPurge:start"}});
  255. SyncPoint::GetInstance()->EnableProcessing();
  256. Reopen(options);
  257. // the obsolete file is not deleted until the background purge job is ran
  258. CheckFileTypeCounts(dbname_, -1, 1, -1);
  259. TEST_SYNC_POINT("DeleteFileTest::PurgeDuringOpen:1");
  260. ASSERT_OK(dbfull()->TEST_WaitForPurge());
  261. CheckFileTypeCounts(dbname_, -1, 0, -1);
  262. }
  263. TEST_F(DeleteFileTest, BackgroundPurgeCFDropTest) {
  264. Options options = CurrentOptions();
  265. SetOptions(&options);
  266. Destroy(options);
  267. options.create_if_missing = true;
  268. Reopen(options);
  269. auto do_test = [&](bool bg_purge) {
  270. ColumnFamilyOptions co;
  271. co.max_write_buffer_size_to_maintain =
  272. static_cast<int64_t>(co.write_buffer_size);
  273. WriteOptions wo;
  274. FlushOptions fo;
  275. ColumnFamilyHandle* cfh = nullptr;
  276. ASSERT_OK(db_->CreateColumnFamily(co, "dropme", &cfh));
  277. ASSERT_OK(db_->Put(wo, cfh, "pika", "chu"));
  278. ASSERT_OK(db_->Flush(fo, cfh));
  279. // Expect 1 sst file.
  280. CheckFileTypeCounts(dbname_, 0, 1, 1);
  281. ASSERT_OK(db_->DropColumnFamily(cfh));
  282. // Still 1 file, it won't be deleted while ColumnFamilyHandle is alive.
  283. CheckFileTypeCounts(dbname_, 0, 1, 1);
  284. delete cfh;
  285. test::SleepingBackgroundTask sleeping_task_after;
  286. env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask,
  287. &sleeping_task_after, Env::Priority::HIGH);
  288. // If background purge is enabled, the file should still be there.
  289. CheckFileTypeCounts(dbname_, 0, bg_purge ? 1 : 0, 1);
  290. TEST_SYNC_POINT("DeleteFileTest::BackgroundPurgeCFDropTest:1");
  291. // Execute background purges.
  292. sleeping_task_after.WakeUp();
  293. sleeping_task_after.WaitUntilDone();
  294. // The file should have been deleted.
  295. CheckFileTypeCounts(dbname_, 0, 0, 1);
  296. };
  297. {
  298. SCOPED_TRACE("avoid_unnecessary_blocking_io = false");
  299. do_test(false);
  300. }
  301. options.avoid_unnecessary_blocking_io = true;
  302. options.create_if_missing = false;
  303. Reopen(options);
  304. ASSERT_OK(dbfull()->TEST_WaitForPurge());
  305. SyncPoint::GetInstance()->DisableProcessing();
  306. SyncPoint::GetInstance()->ClearAllCallBacks();
  307. SyncPoint::GetInstance()->LoadDependency(
  308. {{"DeleteFileTest::BackgroundPurgeCFDropTest:1",
  309. "DBImpl::BGWorkPurge:start"}});
  310. SyncPoint::GetInstance()->EnableProcessing();
  311. {
  312. SCOPED_TRACE("avoid_unnecessary_blocking_io = true");
  313. do_test(true);
  314. }
  315. }
  316. // This test is to reproduce a bug that read invalid ReadOption in iterator
  317. // cleanup function
  318. TEST_F(DeleteFileTest, BackgroundPurgeCopyOptions) {
  319. Options options = CurrentOptions();
  320. SetOptions(&options);
  321. Destroy(options);
  322. options.create_if_missing = true;
  323. Reopen(options);
  324. std::string first("0"), last("999999");
  325. CompactRangeOptions compact_options;
  326. compact_options.change_level = true;
  327. compact_options.target_level = 2;
  328. Slice first_slice(first), last_slice(last);
  329. // We keep an iterator alive
  330. Iterator* itr = nullptr;
  331. CreateTwoLevels();
  332. {
  333. ReadOptions read_options;
  334. read_options.background_purge_on_iterator_cleanup = true;
  335. itr = db_->NewIterator(read_options);
  336. ASSERT_OK(itr->status());
  337. // ReadOptions is deleted, but iterator cleanup function should not be
  338. // affected
  339. }
  340. ASSERT_OK(db_->CompactRange(compact_options, &first_slice, &last_slice));
  341. // 3 sst after compaction with live iterator
  342. CheckFileTypeCounts(dbname_, 0, 3, 1);
  343. delete itr;
  344. test::SleepingBackgroundTask sleeping_task_after;
  345. env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask,
  346. &sleeping_task_after, Env::Priority::HIGH);
  347. // Make sure all background purges are executed
  348. sleeping_task_after.WakeUp();
  349. sleeping_task_after.WaitUntilDone();
  350. // 1 sst after iterator deletion
  351. CheckFileTypeCounts(dbname_, 0, 1, 1);
  352. }
  353. TEST_F(DeleteFileTest, BackgroundPurgeTestMultipleJobs) {
  354. Options options = CurrentOptions();
  355. SetOptions(&options);
  356. Destroy(options);
  357. options.create_if_missing = true;
  358. Reopen(options);
  359. std::string first("0"), last("999999");
  360. CompactRangeOptions compact_options;
  361. compact_options.change_level = true;
  362. compact_options.target_level = 2;
  363. Slice first_slice(first), last_slice(last);
  364. // We keep an iterator alive
  365. CreateTwoLevels();
  366. ReadOptions read_options;
  367. read_options.background_purge_on_iterator_cleanup = true;
  368. Iterator* itr1 = db_->NewIterator(read_options);
  369. ASSERT_OK(itr1->status());
  370. CreateTwoLevels();
  371. Iterator* itr2 = db_->NewIterator(read_options);
  372. ASSERT_OK(itr2->status());
  373. ASSERT_OK(db_->CompactRange(compact_options, &first_slice, &last_slice));
  374. // 5 sst files after 2 compactions with 2 live iterators
  375. CheckFileTypeCounts(dbname_, 0, 5, 1);
  376. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
  377. // ~DBImpl should wait until all BGWorkPurge are finished
  378. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
  379. {{"DBImpl::~DBImpl:WaitJob", "DBImpl::BGWorkPurge"},
  380. {"DeleteFileTest::GuardFinish",
  381. "DeleteFileTest::BackgroundPurgeTestMultipleJobs:DBClose"}});
  382. ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
  383. delete itr1;
  384. env_->Schedule(&DeleteFileTest::DoSleep, this, Env::Priority::HIGH);
  385. delete itr2;
  386. env_->Schedule(&DeleteFileTest::GuardFinish, nullptr, Env::Priority::HIGH);
  387. Close();
  388. TEST_SYNC_POINT("DeleteFileTest::BackgroundPurgeTestMultipleJobs:DBClose");
  389. // 1 sst after iterator deletion
  390. CheckFileTypeCounts(dbname_, 0, 1, 1);
  391. }
  392. } // namespace ROCKSDB_NAMESPACE
  393. int main(int argc, char** argv) {
  394. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  395. ::testing::InitGoogleTest(&argc, argv);
  396. RegisterCustomObjects(argc, argv);
  397. return RUN_ALL_TESTS();
  398. }