2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// 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).
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2020-07-09 21:33:42 +00:00
|
|
|
#include "file/delete_scheduler.h"
|
|
|
|
|
2015-08-05 03:45:27 +00:00
|
|
|
#include <atomic>
|
2019-09-20 19:00:55 +00:00
|
|
|
#include <cinttypes>
|
2015-08-05 03:45:27 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-07-09 21:33:42 +00:00
|
|
|
#include "file/file_util.h"
|
2019-05-30 03:44:08 +00:00
|
|
|
#include "file/sst_file_manager_impl.h"
|
2015-08-05 03:45:27 +00:00
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/options.h"
|
2019-05-30 18:21:38 +00:00
|
|
|
#include "test_util/sync_point.h"
|
|
|
|
#include "test_util/testharness.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "util/string_util.h"
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2016-12-22 01:35:00 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
class DeleteSchedulerTest : public testing::Test {
|
|
|
|
public:
|
|
|
|
DeleteSchedulerTest() : env_(Env::Default()) {
|
2018-03-06 20:31:25 +00:00
|
|
|
const int kNumDataDirs = 3;
|
|
|
|
dummy_files_dirs_.reserve(kNumDataDirs);
|
|
|
|
for (size_t i = 0; i < kNumDataDirs; ++i) {
|
2018-07-14 00:18:39 +00:00
|
|
|
dummy_files_dirs_.emplace_back(
|
|
|
|
test::PerThreadDBPath(env_, "delete_scheduler_dummy_data_dir") +
|
2022-05-06 20:03:58 +00:00
|
|
|
std::to_string(i));
|
2018-03-06 20:31:25 +00:00
|
|
|
DestroyAndCreateDir(dummy_files_dirs_.back());
|
|
|
|
}
|
2020-06-05 16:41:03 +00:00
|
|
|
stats_ = ROCKSDB_NAMESPACE::CreateDBStatistics();
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
~DeleteSchedulerTest() override {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({});
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
2018-03-06 20:31:25 +00:00
|
|
|
for (const auto& dummy_files_dir : dummy_files_dirs_) {
|
2023-08-09 22:46:44 +00:00
|
|
|
EXPECT_OK(DestroyDir(env_, dummy_files_dir));
|
2018-03-06 20:31:25 +00:00
|
|
|
}
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DestroyAndCreateDir(const std::string& dir) {
|
2020-07-09 21:33:42 +00:00
|
|
|
ASSERT_OK(DestroyDir(env_, dir));
|
2015-08-05 03:45:27 +00:00
|
|
|
EXPECT_OK(env_->CreateDir(dir));
|
|
|
|
}
|
|
|
|
|
2018-03-06 20:31:25 +00:00
|
|
|
int CountNormalFiles(size_t dummy_files_dirs_idx = 0) {
|
2015-08-05 03:45:27 +00:00
|
|
|
std::vector<std::string> files_in_dir;
|
2018-03-06 20:31:25 +00:00
|
|
|
EXPECT_OK(env_->GetChildren(dummy_files_dirs_[dummy_files_dirs_idx],
|
|
|
|
&files_in_dir));
|
2017-10-27 20:25:54 +00:00
|
|
|
|
|
|
|
int normal_cnt = 0;
|
|
|
|
for (auto& f : files_in_dir) {
|
2021-01-09 17:42:21 +00:00
|
|
|
if (!DeleteScheduler::IsTrashFile(f)) {
|
2017-10-27 20:25:54 +00:00
|
|
|
normal_cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return normal_cnt;
|
|
|
|
}
|
|
|
|
|
2018-03-06 20:31:25 +00:00
|
|
|
int CountTrashFiles(size_t dummy_files_dirs_idx = 0) {
|
2017-10-27 20:25:54 +00:00
|
|
|
std::vector<std::string> files_in_dir;
|
2018-03-06 20:31:25 +00:00
|
|
|
EXPECT_OK(env_->GetChildren(dummy_files_dirs_[dummy_files_dirs_idx],
|
|
|
|
&files_in_dir));
|
2017-10-27 20:25:54 +00:00
|
|
|
|
|
|
|
int trash_cnt = 0;
|
|
|
|
for (auto& f : files_in_dir) {
|
|
|
|
if (DeleteScheduler::IsTrashFile(f)) {
|
|
|
|
trash_cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return trash_cnt;
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 20:31:25 +00:00
|
|
|
std::string NewDummyFile(const std::string& file_name, uint64_t size = 1024,
|
|
|
|
size_t dummy_files_dirs_idx = 0) {
|
|
|
|
std::string file_path =
|
|
|
|
dummy_files_dirs_[dummy_files_dirs_idx] + "/" + file_name;
|
2015-08-05 03:45:27 +00:00
|
|
|
std::unique_ptr<WritableFile> f;
|
2023-08-09 22:46:44 +00:00
|
|
|
EXPECT_OK(env_->NewWritableFile(file_path, &f, EnvOptions()));
|
2015-08-05 03:45:27 +00:00
|
|
|
std::string data(size, 'A');
|
|
|
|
EXPECT_OK(f->Append(data));
|
|
|
|
EXPECT_OK(f->Close());
|
2023-08-09 22:46:44 +00:00
|
|
|
EXPECT_OK(sst_file_mgr_->OnAddFile(file_path));
|
2015-08-05 03:45:27 +00:00
|
|
|
return file_path;
|
|
|
|
}
|
|
|
|
|
2016-01-29 02:35:01 +00:00
|
|
|
void NewDeleteScheduler() {
|
2020-02-28 22:10:51 +00:00
|
|
|
// Tests in this file are for DeleteScheduler component and don't create any
|
2017-11-17 19:56:41 +00:00
|
|
|
// DBs, so we need to set max_trash_db_ratio to 100% (instead of default
|
|
|
|
// 25%)
|
2021-01-26 06:07:26 +00:00
|
|
|
sst_file_mgr_.reset(
|
|
|
|
new SstFileManagerImpl(env_->GetSystemClock(), env_->GetFileSystem(),
|
|
|
|
nullptr, rate_bytes_per_sec_,
|
|
|
|
/* max_trash_db_ratio= */ 1.1, 128 * 1024));
|
2017-06-12 23:51:37 +00:00
|
|
|
delete_scheduler_ = sst_file_mgr_->delete_scheduler();
|
2020-06-05 16:41:03 +00:00
|
|
|
sst_file_mgr_->SetStatisticsPtr(stats_);
|
2016-01-29 02:35:01 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 03:45:27 +00:00
|
|
|
Env* env_;
|
2018-03-06 20:31:25 +00:00
|
|
|
std::vector<std::string> dummy_files_dirs_;
|
2015-08-05 03:45:27 +00:00
|
|
|
int64_t rate_bytes_per_sec_;
|
2017-06-12 23:51:37 +00:00
|
|
|
DeleteScheduler* delete_scheduler_;
|
|
|
|
std::unique_ptr<SstFileManagerImpl> sst_file_mgr_;
|
2020-06-05 16:41:03 +00:00
|
|
|
std::shared_ptr<Statistics> stats_;
|
2015-08-05 03:45:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Test the basic functionality of DeleteScheduler (Rate Limiting).
|
|
|
|
// 1- Create 100 dummy files
|
|
|
|
// 2- Delete the 100 dummy files using DeleteScheduler
|
2016-01-29 02:35:01 +00:00
|
|
|
// --- Hold DeleteScheduler::BackgroundEmptyTrash ---
|
2015-08-05 03:45:27 +00:00
|
|
|
// 3- Wait for DeleteScheduler to delete all files in trash
|
2015-08-06 02:16:52 +00:00
|
|
|
// 4- Verify that BackgroundEmptyTrash used to correct penlties for the files
|
2015-08-05 03:45:27 +00:00
|
|
|
// 5- Make sure that all created files were completely deleted
|
|
|
|
TEST_F(DeleteSchedulerTest, BasicRateLimiting) {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
|
2015-08-06 02:16:52 +00:00
|
|
|
{"DeleteSchedulerTest::BasicRateLimiting:1",
|
2016-01-29 02:35:01 +00:00
|
|
|
"DeleteScheduler::BackgroundEmptyTrash"},
|
2015-08-06 02:16:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
std::vector<uint64_t> penalties;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2016-01-29 02:35:01 +00:00
|
|
|
"DeleteScheduler::BackgroundEmptyTrash:Wait",
|
2017-04-22 03:41:37 +00:00
|
|
|
[&](void* arg) { penalties.push_back(*(static_cast<uint64_t*>(arg))); });
|
2018-04-26 20:51:39 +00:00
|
|
|
int dir_synced = 0;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2018-04-26 20:51:39 +00:00
|
|
|
"DeleteScheduler::DeleteTrashFile::AfterSyncDir", [&](void* arg) {
|
|
|
|
dir_synced++;
|
Prefer static_cast in place of most reinterpret_cast (#12308)
Summary:
The following are risks associated with pointer-to-pointer reinterpret_cast:
* Can produce the "wrong result" (crash or memory corruption). IIRC, in theory this can happen for any up-cast or down-cast for a non-standard-layout type, though in practice would only happen for multiple inheritance cases (where the base class pointer might be "inside" the derived object). We don't use multiple inheritance a lot, but we do.
* Can mask useful compiler errors upon code change, including converting between unrelated pointer types that you are expecting to be related, and converting between pointer and scalar types unintentionally.
I can only think of some obscure cases where static_cast could be troublesome when it compiles as a replacement:
* Going through `void*` could plausibly cause unnecessary or broken pointer arithmetic. Suppose we have
`struct Derived: public Base1, public Base2`. If we have `Derived*` -> `void*` -> `Base2*` -> `Derived*` through reinterpret casts, this could plausibly work (though technical UB) assuming the `Base2*` is not dereferenced. Changing to static cast could introduce breaking pointer arithmetic.
* Unnecessary (but safe) pointer arithmetic could arise in a case like `Derived*` -> `Base2*` -> `Derived*` where before the Base2 pointer might not have been dereferenced. This could potentially affect performance.
With some light scripting, I tried replacing pointer-to-pointer reinterpret_casts with static_cast and kept the cases that still compile. Most occurrences of reinterpret_cast have successfully been changed (except for java/ and third-party/). 294 changed, 257 remain.
A couple of related interventions included here:
* Previously Cache::Handle was not actually derived from in the implementations and just used as a `void*` stand-in with reinterpret_cast. Now there is a relationship to allow static_cast. In theory, this could introduce pointer arithmetic (as described above) but is unlikely without multiple inheritance AND non-empty Cache::Handle.
* Remove some unnecessary casts to void* as this is allowed to be implicit (for better or worse).
Most of the remaining reinterpret_casts are for converting to/from raw bytes of objects. We could consider better idioms for these patterns in follow-up work.
I wish there were a way to implement a template variant of static_cast that would only compile if no pointer arithmetic is generated, but best I can tell, this is not possible. AFAIK the best you could do is a dynamic check that the void* conversion after the static cast is unchanged.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/12308
Test Plan: existing tests, CI
Reviewed By: ltamasi
Differential Revision: D53204947
Pulled By: pdillinger
fbshipit-source-id: 9de23e618263b0d5b9820f4e15966876888a16e2
2024-02-07 18:44:11 +00:00
|
|
|
std::string* dir = static_cast<std::string*>(arg);
|
2018-04-26 20:51:39 +00:00
|
|
|
EXPECT_EQ(dummy_files_dirs_[0], *dir);
|
|
|
|
});
|
2015-08-06 02:16:52 +00:00
|
|
|
|
2022-10-25 01:34:52 +00:00
|
|
|
int num_files = 100; // 100 files
|
2015-08-05 03:45:27 +00:00
|
|
|
uint64_t file_size = 1024; // every file is 1 kb
|
|
|
|
std::vector<uint64_t> delete_kbs_per_sec = {512, 200, 100, 50, 25};
|
|
|
|
|
|
|
|
for (size_t t = 0; t < delete_kbs_per_sec.size(); t++) {
|
2015-08-06 02:16:52 +00:00
|
|
|
penalties.clear();
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearTrace();
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2015-09-28 22:58:50 +00:00
|
|
|
|
2018-03-06 20:31:25 +00:00
|
|
|
DestroyAndCreateDir(dummy_files_dirs_[0]);
|
2015-08-05 03:45:27 +00:00
|
|
|
rate_bytes_per_sec_ = delete_kbs_per_sec[t] * 1024;
|
2016-01-29 02:35:01 +00:00
|
|
|
NewDeleteScheduler();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2018-04-26 20:51:39 +00:00
|
|
|
dir_synced = 0;
|
2015-08-05 03:45:27 +00:00
|
|
|
// Create 100 dummy files, every file is 1 Kb
|
|
|
|
std::vector<std::string> generated_files;
|
|
|
|
for (int i = 0; i < num_files; i++) {
|
2022-05-06 20:03:58 +00:00
|
|
|
std::string file_name = "file" + std::to_string(i) + ".data";
|
2015-08-05 03:45:27 +00:00
|
|
|
generated_files.push_back(NewDummyFile(file_name, file_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete dummy files and measure time spent to empty trash
|
|
|
|
for (int i = 0; i < num_files; i++) {
|
2018-04-26 20:51:39 +00:00
|
|
|
ASSERT_OK(delete_scheduler_->DeleteFile(generated_files[i],
|
|
|
|
dummy_files_dirs_[0]));
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_EQ(CountNormalFiles(), 0);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2015-08-06 02:16:52 +00:00
|
|
|
uint64_t delete_start_time = env_->NowMicros();
|
2015-08-07 22:37:56 +00:00
|
|
|
TEST_SYNC_POINT("DeleteSchedulerTest::BasicRateLimiting:1");
|
2015-08-19 22:02:17 +00:00
|
|
|
delete_scheduler_->WaitForEmptyTrash();
|
2015-08-05 03:45:27 +00:00
|
|
|
uint64_t time_spent_deleting = env_->NowMicros() - delete_start_time;
|
2015-08-06 02:16:52 +00:00
|
|
|
|
2015-09-28 22:58:50 +00:00
|
|
|
auto bg_errors = delete_scheduler_->GetBackgroundErrors();
|
|
|
|
ASSERT_EQ(bg_errors.size(), 0);
|
|
|
|
|
2015-08-06 02:16:52 +00:00
|
|
|
uint64_t total_files_size = 0;
|
|
|
|
uint64_t expected_penlty = 0;
|
|
|
|
ASSERT_EQ(penalties.size(), num_files);
|
|
|
|
for (int i = 0; i < num_files; i++) {
|
|
|
|
total_files_size += file_size;
|
|
|
|
expected_penlty = ((total_files_size * 1000000) / rate_bytes_per_sec_);
|
|
|
|
ASSERT_EQ(expected_penlty, penalties[i]);
|
|
|
|
}
|
|
|
|
ASSERT_GT(time_spent_deleting, expected_penlty * 0.9);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2018-04-26 20:51:39 +00:00
|
|
|
ASSERT_EQ(num_files, dir_synced);
|
|
|
|
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_EQ(CountTrashFiles(), 0);
|
2020-06-05 16:41:03 +00:00
|
|
|
ASSERT_EQ(num_files, stats_->getAndResetTickerCount(FILES_MARKED_TRASH));
|
2023-06-16 17:05:25 +00:00
|
|
|
ASSERT_EQ(num_files,
|
|
|
|
stats_->getAndResetTickerCount(FILES_DELETED_FROM_TRASH_QUEUE));
|
2020-06-05 16:41:03 +00:00
|
|
|
ASSERT_EQ(0, stats_->getAndResetTickerCount(FILES_DELETED_IMMEDIATELY));
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-06 20:31:25 +00:00
|
|
|
TEST_F(DeleteSchedulerTest, MultiDirectoryDeletionsScheduled) {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
|
2018-03-06 20:31:25 +00:00
|
|
|
{"DeleteSchedulerTest::MultiDbPathDeletionsScheduled:1",
|
|
|
|
"DeleteScheduler::BackgroundEmptyTrash"},
|
|
|
|
});
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2018-03-06 20:31:25 +00:00
|
|
|
rate_bytes_per_sec_ = 1 << 20; // 1MB
|
|
|
|
NewDeleteScheduler();
|
|
|
|
|
|
|
|
// Generate dummy files in multiple directories
|
|
|
|
const size_t kNumFiles = dummy_files_dirs_.size();
|
|
|
|
const size_t kFileSize = 1 << 10; // 1KB
|
|
|
|
std::vector<std::string> generated_files;
|
|
|
|
for (size_t i = 0; i < kNumFiles; i++) {
|
|
|
|
generated_files.push_back(NewDummyFile("file", kFileSize, i));
|
|
|
|
ASSERT_EQ(1, CountNormalFiles(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark dummy files as trash
|
|
|
|
for (size_t i = 0; i < kNumFiles; i++) {
|
2018-04-26 20:51:39 +00:00
|
|
|
ASSERT_OK(delete_scheduler_->DeleteFile(generated_files[i], ""));
|
2018-03-06 20:31:25 +00:00
|
|
|
ASSERT_EQ(0, CountNormalFiles(i));
|
|
|
|
ASSERT_EQ(1, CountTrashFiles(i));
|
|
|
|
}
|
|
|
|
TEST_SYNC_POINT("DeleteSchedulerTest::MultiDbPathDeletionsScheduled:1");
|
|
|
|
delete_scheduler_->WaitForEmptyTrash();
|
|
|
|
|
|
|
|
// Verify dummy files eventually got deleted
|
|
|
|
for (size_t i = 0; i < kNumFiles; i++) {
|
|
|
|
ASSERT_EQ(0, CountNormalFiles(i));
|
|
|
|
ASSERT_EQ(0, CountTrashFiles(i));
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:41:03 +00:00
|
|
|
ASSERT_EQ(kNumFiles, stats_->getAndResetTickerCount(FILES_MARKED_TRASH));
|
2023-06-16 17:05:25 +00:00
|
|
|
ASSERT_EQ(kNumFiles,
|
|
|
|
stats_->getAndResetTickerCount(FILES_DELETED_FROM_TRASH_QUEUE));
|
2020-06-05 16:41:03 +00:00
|
|
|
ASSERT_EQ(0, stats_->getAndResetTickerCount(FILES_DELETED_IMMEDIATELY));
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2018-03-06 20:31:25 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 03:45:27 +00:00
|
|
|
// Same as the BasicRateLimiting test but delete files in multiple threads.
|
|
|
|
// 1- Create 100 dummy files
|
|
|
|
// 2- Delete the 100 dummy files using DeleteScheduler using 10 threads
|
2016-01-29 02:35:01 +00:00
|
|
|
// --- Hold DeleteScheduler::BackgroundEmptyTrash ---
|
2015-08-05 03:45:27 +00:00
|
|
|
// 3- Wait for DeleteScheduler to delete all files in queue
|
2015-08-06 02:16:52 +00:00
|
|
|
// 4- Verify that BackgroundEmptyTrash used to correct penlties for the files
|
2015-08-05 03:45:27 +00:00
|
|
|
// 5- Make sure that all created files were completely deleted
|
|
|
|
TEST_F(DeleteSchedulerTest, RateLimitingMultiThreaded) {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
|
2015-08-06 02:16:52 +00:00
|
|
|
{"DeleteSchedulerTest::RateLimitingMultiThreaded:1",
|
2016-01-29 02:35:01 +00:00
|
|
|
"DeleteScheduler::BackgroundEmptyTrash"},
|
2015-08-06 02:16:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
std::vector<uint64_t> penalties;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2016-01-29 02:35:01 +00:00
|
|
|
"DeleteScheduler::BackgroundEmptyTrash:Wait",
|
2017-04-22 03:41:37 +00:00
|
|
|
[&](void* arg) { penalties.push_back(*(static_cast<uint64_t*>(arg))); });
|
2015-08-06 02:16:52 +00:00
|
|
|
|
2015-08-05 03:45:27 +00:00
|
|
|
int thread_cnt = 10;
|
2022-10-25 01:34:52 +00:00
|
|
|
int num_files = 10; // 10 files per thread
|
2015-08-05 03:45:27 +00:00
|
|
|
uint64_t file_size = 1024; // every file is 1 kb
|
|
|
|
|
2015-08-05 18:45:31 +00:00
|
|
|
std::vector<uint64_t> delete_kbs_per_sec = {512, 200, 100, 50, 25};
|
2015-08-05 03:45:27 +00:00
|
|
|
for (size_t t = 0; t < delete_kbs_per_sec.size(); t++) {
|
2015-08-06 02:16:52 +00:00
|
|
|
penalties.clear();
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearTrace();
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2015-09-28 22:58:50 +00:00
|
|
|
|
2018-03-06 20:31:25 +00:00
|
|
|
DestroyAndCreateDir(dummy_files_dirs_[0]);
|
2015-08-05 03:45:27 +00:00
|
|
|
rate_bytes_per_sec_ = delete_kbs_per_sec[t] * 1024;
|
2016-01-29 02:35:01 +00:00
|
|
|
NewDeleteScheduler();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
// Create 100 dummy files, every file is 1 Kb
|
|
|
|
std::vector<std::string> generated_files;
|
|
|
|
for (int i = 0; i < num_files * thread_cnt; i++) {
|
2022-05-06 20:03:58 +00:00
|
|
|
std::string file_name = "file" + std::to_string(i) + ".data";
|
2015-08-05 03:45:27 +00:00
|
|
|
generated_files.push_back(NewDummyFile(file_name, file_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete dummy files using 10 threads and measure time spent to empty trash
|
|
|
|
std::atomic<int> thread_num(0);
|
2017-02-06 22:43:55 +00:00
|
|
|
std::vector<port::Thread> threads;
|
2015-08-05 18:45:31 +00:00
|
|
|
std::function<void()> delete_thread = [&]() {
|
|
|
|
int idx = thread_num.fetch_add(1);
|
|
|
|
int range_start = idx * num_files;
|
|
|
|
int range_end = range_start + num_files;
|
|
|
|
for (int j = range_start; j < range_end; j++) {
|
2018-04-26 20:51:39 +00:00
|
|
|
ASSERT_OK(delete_scheduler_->DeleteFile(generated_files[j], ""));
|
2015-08-05 18:45:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-05 03:45:27 +00:00
|
|
|
for (int i = 0; i < thread_cnt; i++) {
|
2015-08-05 18:45:31 +00:00
|
|
|
threads.emplace_back(delete_thread);
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < threads.size(); i++) {
|
|
|
|
threads[i].join();
|
|
|
|
}
|
|
|
|
|
2015-08-06 02:16:52 +00:00
|
|
|
uint64_t delete_start_time = env_->NowMicros();
|
2015-08-07 22:37:56 +00:00
|
|
|
TEST_SYNC_POINT("DeleteSchedulerTest::RateLimitingMultiThreaded:1");
|
2015-08-19 22:02:17 +00:00
|
|
|
delete_scheduler_->WaitForEmptyTrash();
|
2015-08-05 03:45:27 +00:00
|
|
|
uint64_t time_spent_deleting = env_->NowMicros() - delete_start_time;
|
2015-08-06 02:16:52 +00:00
|
|
|
|
2015-09-28 22:58:50 +00:00
|
|
|
auto bg_errors = delete_scheduler_->GetBackgroundErrors();
|
|
|
|
ASSERT_EQ(bg_errors.size(), 0);
|
|
|
|
|
2015-08-06 02:16:52 +00:00
|
|
|
uint64_t total_files_size = 0;
|
|
|
|
uint64_t expected_penlty = 0;
|
|
|
|
ASSERT_EQ(penalties.size(), num_files * thread_cnt);
|
|
|
|
for (int i = 0; i < num_files * thread_cnt; i++) {
|
|
|
|
total_files_size += file_size;
|
|
|
|
expected_penlty = ((total_files_size * 1000000) / rate_bytes_per_sec_);
|
|
|
|
ASSERT_EQ(expected_penlty, penalties[i]);
|
|
|
|
}
|
|
|
|
ASSERT_GT(time_spent_deleting, expected_penlty * 0.9);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_EQ(CountNormalFiles(), 0);
|
|
|
|
ASSERT_EQ(CountTrashFiles(), 0);
|
2023-06-16 17:05:25 +00:00
|
|
|
int total_num_files = num_files * thread_cnt;
|
|
|
|
ASSERT_EQ(total_num_files,
|
2020-06-05 16:41:03 +00:00
|
|
|
stats_->getAndResetTickerCount(FILES_MARKED_TRASH));
|
2023-06-16 17:05:25 +00:00
|
|
|
ASSERT_EQ(total_num_files,
|
|
|
|
stats_->getAndResetTickerCount(FILES_DELETED_FROM_TRASH_QUEUE));
|
2020-06-05 16:41:03 +00:00
|
|
|
ASSERT_EQ(0, stats_->getAndResetTickerCount(FILES_DELETED_IMMEDIATELY));
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable rate limiting by setting rate_bytes_per_sec_ to 0 and make sure
|
2020-02-28 22:10:51 +00:00
|
|
|
// that when DeleteScheduler delete a file it delete it immediately and don't
|
2015-08-05 03:45:27 +00:00
|
|
|
// move it to trash
|
|
|
|
TEST_F(DeleteSchedulerTest, DisableRateLimiting) {
|
|
|
|
int bg_delete_file = 0;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2016-01-29 02:35:01 +00:00
|
|
|
"DeleteScheduler::DeleteTrashFile:DeleteFile",
|
2018-04-13 00:55:14 +00:00
|
|
|
[&](void* /*arg*/) { bg_delete_file++; });
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2016-01-29 02:35:01 +00:00
|
|
|
rate_bytes_per_sec_ = 0;
|
|
|
|
NewDeleteScheduler();
|
2020-06-05 16:41:03 +00:00
|
|
|
constexpr int num_files = 10;
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2020-06-05 16:41:03 +00:00
|
|
|
for (int i = 0; i < num_files; i++) {
|
2015-08-05 03:45:27 +00:00
|
|
|
// Every file we delete will be deleted immediately
|
|
|
|
std::string dummy_file = NewDummyFile("dummy.data");
|
2018-04-26 20:51:39 +00:00
|
|
|
ASSERT_OK(delete_scheduler_->DeleteFile(dummy_file, ""));
|
2015-08-05 03:45:27 +00:00
|
|
|
ASSERT_TRUE(env_->FileExists(dummy_file).IsNotFound());
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_EQ(CountNormalFiles(), 0);
|
|
|
|
ASSERT_EQ(CountTrashFiles(), 0);
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(bg_delete_file, 0);
|
2020-06-05 16:41:03 +00:00
|
|
|
ASSERT_EQ(0, stats_->getAndResetTickerCount(FILES_MARKED_TRASH));
|
2023-06-16 17:05:25 +00:00
|
|
|
ASSERT_EQ(0, stats_->getAndResetTickerCount(FILES_DELETED_FROM_TRASH_QUEUE));
|
2020-06-05 16:41:03 +00:00
|
|
|
ASSERT_EQ(num_files,
|
|
|
|
stats_->getAndResetTickerCount(FILES_DELETED_IMMEDIATELY));
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Testing that moving files to trash with the same name is not a problem
|
|
|
|
// 1- Create 10 files with the same name "conflict.data"
|
|
|
|
// 2- Delete the 10 files using DeleteScheduler
|
|
|
|
// 3- Make sure that trash directory contain 10 files ("conflict.data" x 10)
|
2016-01-29 02:35:01 +00:00
|
|
|
// --- Hold DeleteScheduler::BackgroundEmptyTrash ---
|
2015-08-05 03:45:27 +00:00
|
|
|
// 4- Make sure that files are deleted from trash
|
|
|
|
TEST_F(DeleteSchedulerTest, ConflictNames) {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
|
2015-08-05 03:45:27 +00:00
|
|
|
{"DeleteSchedulerTest::ConflictNames:1",
|
2016-01-29 02:35:01 +00:00
|
|
|
"DeleteScheduler::BackgroundEmptyTrash"},
|
2015-08-05 03:45:27 +00:00
|
|
|
});
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
rate_bytes_per_sec_ = 1024 * 1024; // 1 Mb/sec
|
2016-01-29 02:35:01 +00:00
|
|
|
NewDeleteScheduler();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
// Create "conflict.data" and move it to trash 10 times
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
std::string dummy_file = NewDummyFile("conflict.data");
|
2018-04-26 20:51:39 +00:00
|
|
|
ASSERT_OK(delete_scheduler_->DeleteFile(dummy_file, ""));
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_EQ(CountNormalFiles(), 0);
|
2015-08-05 03:45:27 +00:00
|
|
|
// 10 files ("conflict.data" x 10) in trash
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_EQ(CountTrashFiles(), 10);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
// Hold BackgroundEmptyTrash
|
|
|
|
TEST_SYNC_POINT("DeleteSchedulerTest::ConflictNames:1");
|
2015-08-19 22:02:17 +00:00
|
|
|
delete_scheduler_->WaitForEmptyTrash();
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_EQ(CountTrashFiles(), 0);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
auto bg_errors = delete_scheduler_->GetBackgroundErrors();
|
|
|
|
ASSERT_EQ(bg_errors.size(), 0);
|
2020-06-05 16:41:03 +00:00
|
|
|
ASSERT_EQ(10, stats_->getAndResetTickerCount(FILES_MARKED_TRASH));
|
2023-06-16 17:05:25 +00:00
|
|
|
ASSERT_EQ(10, stats_->getAndResetTickerCount(FILES_DELETED_FROM_TRASH_QUEUE));
|
2020-06-05 16:41:03 +00:00
|
|
|
ASSERT_EQ(0, stats_->getAndResetTickerCount(FILES_DELETED_IMMEDIATELY));
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1- Create 10 dummy files
|
|
|
|
// 2- Delete the 10 files using DeleteScheduler (move them to trsah)
|
|
|
|
// 3- Delete the 10 files directly (using env_->DeleteFile)
|
2016-01-29 02:35:01 +00:00
|
|
|
// --- Hold DeleteScheduler::BackgroundEmptyTrash ---
|
2015-08-05 03:45:27 +00:00
|
|
|
// 4- Make sure that DeleteScheduler failed to delete the 10 files and
|
|
|
|
// reported 10 background errors
|
|
|
|
TEST_F(DeleteSchedulerTest, BackgroundError) {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
|
2015-08-05 03:45:27 +00:00
|
|
|
{"DeleteSchedulerTest::BackgroundError:1",
|
2016-01-29 02:35:01 +00:00
|
|
|
"DeleteScheduler::BackgroundEmptyTrash"},
|
2015-08-05 03:45:27 +00:00
|
|
|
});
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
rate_bytes_per_sec_ = 1024 * 1024; // 1 Mb/sec
|
2016-01-29 02:35:01 +00:00
|
|
|
NewDeleteScheduler();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
// Generate 10 dummy files and move them to trash
|
|
|
|
for (int i = 0; i < 10; i++) {
|
2022-05-06 20:03:58 +00:00
|
|
|
std::string file_name = "data_" + std::to_string(i) + ".data";
|
2018-04-26 20:51:39 +00:00
|
|
|
ASSERT_OK(delete_scheduler_->DeleteFile(NewDummyFile(file_name), ""));
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_EQ(CountNormalFiles(), 0);
|
|
|
|
ASSERT_EQ(CountTrashFiles(), 10);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
// Delete 10 files from trash, this will cause background errors in
|
|
|
|
// BackgroundEmptyTrash since we already deleted the files it was
|
|
|
|
// goind to delete
|
|
|
|
for (int i = 0; i < 10; i++) {
|
2022-05-06 20:03:58 +00:00
|
|
|
std::string file_name = "data_" + std::to_string(i) + ".data.trash";
|
2018-03-06 20:31:25 +00:00
|
|
|
ASSERT_OK(env_->DeleteFile(dummy_files_dirs_[0] + "/" + file_name));
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hold BackgroundEmptyTrash
|
|
|
|
TEST_SYNC_POINT("DeleteSchedulerTest::BackgroundError:1");
|
2015-08-19 22:02:17 +00:00
|
|
|
delete_scheduler_->WaitForEmptyTrash();
|
2015-08-05 03:45:27 +00:00
|
|
|
auto bg_errors = delete_scheduler_->GetBackgroundErrors();
|
|
|
|
ASSERT_EQ(bg_errors.size(), 10);
|
2021-01-07 23:21:51 +00:00
|
|
|
for (const auto& it : bg_errors) {
|
|
|
|
ASSERT_TRUE(it.second.IsPathNotFound());
|
|
|
|
}
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:30:28 +00:00
|
|
|
// 1- Create kTestFileNum dummy files
|
|
|
|
// 2- Delete kTestFileNum dummy files using DeleteScheduler
|
2015-08-05 03:45:27 +00:00
|
|
|
// 3- Wait for DeleteScheduler to delete all files in queue
|
|
|
|
// 4- Make sure all files in trash directory were deleted
|
|
|
|
// 5- Repeat previous steps 5 times
|
|
|
|
TEST_F(DeleteSchedulerTest, StartBGEmptyTrashMultipleTimes) {
|
2022-03-17 20:30:28 +00:00
|
|
|
constexpr int kTestFileNum = 10;
|
|
|
|
std::atomic_int bg_delete_file = 0;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2016-01-29 02:35:01 +00:00
|
|
|
"DeleteScheduler::DeleteTrashFile:DeleteFile",
|
2018-04-13 00:55:14 +00:00
|
|
|
[&](void* /*arg*/) { bg_delete_file++; });
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
rate_bytes_per_sec_ = 1024 * 1024; // 1 MB / sec
|
2016-01-29 02:35:01 +00:00
|
|
|
NewDeleteScheduler();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2022-03-17 20:30:28 +00:00
|
|
|
// If trash file is generated faster than deleting, delete_scheduler will
|
|
|
|
// delete it directly instead of waiting for background trash empty thread to
|
|
|
|
// clean it. Set the ratio higher to avoid that.
|
|
|
|
sst_file_mgr_->SetMaxTrashDBRatio(kTestFileNum + 1);
|
|
|
|
|
2015-08-05 03:45:27 +00:00
|
|
|
// Move files to trash, wait for empty trash, start again
|
|
|
|
for (int run = 1; run <= 5; run++) {
|
2022-03-17 20:30:28 +00:00
|
|
|
// Generate kTestFileNum dummy files and move them to trash
|
|
|
|
for (int i = 0; i < kTestFileNum; i++) {
|
2022-05-06 20:03:58 +00:00
|
|
|
std::string file_name = "data_" + std::to_string(i) + ".data";
|
2018-04-26 20:51:39 +00:00
|
|
|
ASSERT_OK(delete_scheduler_->DeleteFile(NewDummyFile(file_name), ""));
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_EQ(CountNormalFiles(), 0);
|
2015-08-19 22:02:17 +00:00
|
|
|
delete_scheduler_->WaitForEmptyTrash();
|
2022-03-17 20:30:28 +00:00
|
|
|
ASSERT_EQ(bg_delete_file, kTestFileNum * run);
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_EQ(CountTrashFiles(), 0);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
auto bg_errors = delete_scheduler_->GetBackgroundErrors();
|
|
|
|
ASSERT_EQ(bg_errors.size(), 0);
|
2022-03-17 20:30:28 +00:00
|
|
|
ASSERT_EQ(kTestFileNum, stats_->getAndResetTickerCount(FILES_MARKED_TRASH));
|
2023-06-16 17:05:25 +00:00
|
|
|
ASSERT_EQ(kTestFileNum,
|
|
|
|
stats_->getAndResetTickerCount(FILES_DELETED_FROM_TRASH_QUEUE));
|
2020-06-05 16:41:03 +00:00
|
|
|
ASSERT_EQ(0, stats_->getAndResetTickerCount(FILES_DELETED_IMMEDIATELY));
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 20:30:28 +00:00
|
|
|
ASSERT_EQ(bg_delete_file, 5 * kTestFileNum);
|
2020-06-05 16:41:03 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 22:42:44 +00:00
|
|
|
TEST_F(DeleteSchedulerTest, DeletePartialFile) {
|
|
|
|
int bg_delete_file = 0;
|
|
|
|
int bg_fsync = 0;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2018-03-22 22:42:44 +00:00
|
|
|
"DeleteScheduler::DeleteTrashFile:DeleteFile",
|
|
|
|
[&](void*) { bg_delete_file++; });
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2018-03-22 22:42:44 +00:00
|
|
|
"DeleteScheduler::DeleteTrashFile:Fsync", [&](void*) { bg_fsync++; });
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2018-03-22 22:42:44 +00:00
|
|
|
|
|
|
|
rate_bytes_per_sec_ = 1024 * 1024; // 1 MB / sec
|
|
|
|
NewDeleteScheduler();
|
|
|
|
|
|
|
|
// Should delete in 4 batch
|
2018-04-26 20:51:39 +00:00
|
|
|
ASSERT_OK(
|
|
|
|
delete_scheduler_->DeleteFile(NewDummyFile("data_1", 500 * 1024), ""));
|
|
|
|
ASSERT_OK(
|
|
|
|
delete_scheduler_->DeleteFile(NewDummyFile("data_2", 100 * 1024), ""));
|
2018-03-22 22:42:44 +00:00
|
|
|
// Should delete in 2 batch
|
2018-04-26 20:51:39 +00:00
|
|
|
ASSERT_OK(
|
|
|
|
delete_scheduler_->DeleteFile(NewDummyFile("data_2", 200 * 1024), ""));
|
2018-03-22 22:42:44 +00:00
|
|
|
|
|
|
|
delete_scheduler_->WaitForEmptyTrash();
|
|
|
|
|
|
|
|
auto bg_errors = delete_scheduler_->GetBackgroundErrors();
|
|
|
|
ASSERT_EQ(bg_errors.size(), 0);
|
|
|
|
ASSERT_EQ(7, bg_delete_file);
|
|
|
|
ASSERT_EQ(4, bg_fsync);
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2018-03-22 22:42:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 22:17:38 +00:00
|
|
|
#ifdef OS_LINUX
|
|
|
|
TEST_F(DeleteSchedulerTest, NoPartialDeleteWithLink) {
|
|
|
|
int bg_delete_file = 0;
|
|
|
|
int bg_fsync = 0;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2018-07-09 22:17:38 +00:00
|
|
|
"DeleteScheduler::DeleteTrashFile:DeleteFile",
|
|
|
|
[&](void*) { bg_delete_file++; });
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2018-07-09 22:17:38 +00:00
|
|
|
"DeleteScheduler::DeleteTrashFile:Fsync", [&](void*) { bg_fsync++; });
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2018-07-09 22:17:38 +00:00
|
|
|
|
|
|
|
rate_bytes_per_sec_ = 1024 * 1024; // 1 MB / sec
|
|
|
|
NewDeleteScheduler();
|
|
|
|
|
|
|
|
std::string file1 = NewDummyFile("data_1", 500 * 1024);
|
|
|
|
std::string file2 = NewDummyFile("data_2", 100 * 1024);
|
|
|
|
|
|
|
|
ASSERT_OK(env_->LinkFile(file1, dummy_files_dirs_[0] + "/data_1b"));
|
|
|
|
ASSERT_OK(env_->LinkFile(file2, dummy_files_dirs_[0] + "/data_2b"));
|
|
|
|
|
|
|
|
// Should delete in 4 batch if there is no hardlink
|
|
|
|
ASSERT_OK(delete_scheduler_->DeleteFile(file1, ""));
|
|
|
|
ASSERT_OK(delete_scheduler_->DeleteFile(file2, ""));
|
|
|
|
|
|
|
|
delete_scheduler_->WaitForEmptyTrash();
|
|
|
|
|
|
|
|
auto bg_errors = delete_scheduler_->GetBackgroundErrors();
|
|
|
|
ASSERT_EQ(bg_errors.size(), 0);
|
|
|
|
ASSERT_EQ(2, bg_delete_file);
|
|
|
|
ASSERT_EQ(0, bg_fsync);
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2018-07-09 22:17:38 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-08-05 03:45:27 +00:00
|
|
|
// 1- Create a DeleteScheduler with very slow rate limit (1 Byte / sec)
|
|
|
|
// 2- Delete 100 files using DeleteScheduler
|
|
|
|
// 3- Delete the DeleteScheduler (call the destructor while queue is not empty)
|
|
|
|
// 4- Make sure that not all files were deleted from trash and that
|
|
|
|
// DeleteScheduler background thread did not delete all files
|
|
|
|
TEST_F(DeleteSchedulerTest, DestructorWithNonEmptyQueue) {
|
|
|
|
int bg_delete_file = 0;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2016-01-29 02:35:01 +00:00
|
|
|
"DeleteScheduler::DeleteTrashFile:DeleteFile",
|
2018-04-13 00:55:14 +00:00
|
|
|
[&](void* /*arg*/) { bg_delete_file++; });
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
rate_bytes_per_sec_ = 1; // 1 Byte / sec
|
2016-01-29 02:35:01 +00:00
|
|
|
NewDeleteScheduler();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
2022-05-06 20:03:58 +00:00
|
|
|
std::string file_name = "data_" + std::to_string(i) + ".data";
|
2018-04-26 20:51:39 +00:00
|
|
|
ASSERT_OK(delete_scheduler_->DeleteFile(NewDummyFile(file_name), ""));
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deleting 100 files will need >28 hours to delete
|
|
|
|
// we will delete the DeleteScheduler while delete queue is not empty
|
2017-06-12 23:51:37 +00:00
|
|
|
sst_file_mgr_.reset();
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
ASSERT_LT(bg_delete_file, 100);
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_GT(CountTrashFiles(), 0);
|
2015-08-05 03:45:27 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2015-08-05 03:45:27 +00:00
|
|
|
}
|
2017-03-16 19:06:04 +00:00
|
|
|
|
2017-03-30 23:45:39 +00:00
|
|
|
TEST_F(DeleteSchedulerTest, DISABLED_DynamicRateLimiting1) {
|
2017-03-16 19:06:04 +00:00
|
|
|
std::vector<uint64_t> penalties;
|
|
|
|
int bg_delete_file = 0;
|
|
|
|
int fg_delete_file = 0;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2017-03-16 19:06:04 +00:00
|
|
|
"DeleteScheduler::DeleteTrashFile:DeleteFile",
|
2018-04-13 00:55:14 +00:00
|
|
|
[&](void* /*arg*/) { bg_delete_file++; });
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
|
|
|
"DeleteScheduler::DeleteFile", [&](void* /*arg*/) { fg_delete_file++; });
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2017-03-16 19:06:04 +00:00
|
|
|
"DeleteScheduler::BackgroundEmptyTrash:Wait",
|
|
|
|
[&](void* arg) { penalties.push_back(*(static_cast<int*>(arg))); });
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({
|
2017-03-16 19:06:04 +00:00
|
|
|
{"DeleteSchedulerTest::DynamicRateLimiting1:1",
|
|
|
|
"DeleteScheduler::BackgroundEmptyTrash"},
|
|
|
|
});
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2017-03-16 19:06:04 +00:00
|
|
|
|
|
|
|
rate_bytes_per_sec_ = 0; // Disable rate limiting initially
|
|
|
|
NewDeleteScheduler();
|
|
|
|
|
2022-10-25 01:34:52 +00:00
|
|
|
int num_files = 10; // 10 files
|
2017-03-16 19:06:04 +00:00
|
|
|
uint64_t file_size = 1024; // every file is 1 kb
|
|
|
|
|
|
|
|
std::vector<int64_t> delete_kbs_per_sec = {512, 200, 0, 100, 50, -2, 25};
|
|
|
|
for (size_t t = 0; t < delete_kbs_per_sec.size(); t++) {
|
|
|
|
penalties.clear();
|
|
|
|
bg_delete_file = 0;
|
|
|
|
fg_delete_file = 0;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearTrace();
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2017-03-16 19:06:04 +00:00
|
|
|
|
2018-03-06 20:31:25 +00:00
|
|
|
DestroyAndCreateDir(dummy_files_dirs_[0]);
|
2017-03-16 19:06:04 +00:00
|
|
|
rate_bytes_per_sec_ = delete_kbs_per_sec[t] * 1024;
|
|
|
|
delete_scheduler_->SetRateBytesPerSecond(rate_bytes_per_sec_);
|
|
|
|
|
|
|
|
// Create 100 dummy files, every file is 1 Kb
|
|
|
|
std::vector<std::string> generated_files;
|
|
|
|
for (int i = 0; i < num_files; i++) {
|
2022-05-06 20:03:58 +00:00
|
|
|
std::string file_name = "file" + std::to_string(i) + ".data";
|
2017-03-16 19:06:04 +00:00
|
|
|
generated_files.push_back(NewDummyFile(file_name, file_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete dummy files and measure time spent to empty trash
|
|
|
|
for (int i = 0; i < num_files; i++) {
|
2018-04-26 20:51:39 +00:00
|
|
|
ASSERT_OK(delete_scheduler_->DeleteFile(generated_files[i], ""));
|
2017-03-16 19:06:04 +00:00
|
|
|
}
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_EQ(CountNormalFiles(), 0);
|
2017-03-16 19:06:04 +00:00
|
|
|
|
|
|
|
if (rate_bytes_per_sec_ > 0) {
|
|
|
|
uint64_t delete_start_time = env_->NowMicros();
|
|
|
|
TEST_SYNC_POINT("DeleteSchedulerTest::DynamicRateLimiting1:1");
|
|
|
|
delete_scheduler_->WaitForEmptyTrash();
|
|
|
|
uint64_t time_spent_deleting = env_->NowMicros() - delete_start_time;
|
|
|
|
|
|
|
|
auto bg_errors = delete_scheduler_->GetBackgroundErrors();
|
|
|
|
ASSERT_EQ(bg_errors.size(), 0);
|
|
|
|
|
|
|
|
uint64_t total_files_size = 0;
|
|
|
|
uint64_t expected_penlty = 0;
|
|
|
|
ASSERT_EQ(penalties.size(), num_files);
|
|
|
|
for (int i = 0; i < num_files; i++) {
|
|
|
|
total_files_size += file_size;
|
|
|
|
expected_penlty = ((total_files_size * 1000000) / rate_bytes_per_sec_);
|
|
|
|
ASSERT_EQ(expected_penlty, penalties[i]);
|
|
|
|
}
|
|
|
|
ASSERT_GT(time_spent_deleting, expected_penlty * 0.9);
|
|
|
|
ASSERT_EQ(bg_delete_file, num_files);
|
|
|
|
ASSERT_EQ(fg_delete_file, 0);
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(penalties.size(), 0);
|
|
|
|
ASSERT_EQ(bg_delete_file, 0);
|
|
|
|
ASSERT_EQ(fg_delete_file, num_files);
|
|
|
|
}
|
|
|
|
|
2017-10-27 20:25:54 +00:00
|
|
|
ASSERT_EQ(CountTrashFiles(), 0);
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2017-03-16 19:06:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 23:51:37 +00:00
|
|
|
TEST_F(DeleteSchedulerTest, ImmediateDeleteOn25PercDBSize) {
|
|
|
|
int bg_delete_file = 0;
|
|
|
|
int fg_delete_file = 0;
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2017-06-12 23:51:37 +00:00
|
|
|
"DeleteScheduler::DeleteTrashFile:DeleteFile",
|
2018-04-13 00:55:14 +00:00
|
|
|
[&](void* /*arg*/) { bg_delete_file++; });
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2018-04-13 00:55:14 +00:00
|
|
|
"DeleteScheduler::DeleteFile", [&](void* /*arg*/) { fg_delete_file++; });
|
2017-06-12 23:51:37 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2017-06-12 23:51:37 +00:00
|
|
|
|
2022-10-25 01:34:52 +00:00
|
|
|
int num_files = 100; // 100 files
|
|
|
|
uint64_t file_size = 1024 * 10; // 100 KB as a file size
|
|
|
|
rate_bytes_per_sec_ = 1; // 1 byte per sec (very slow trash delete)
|
2017-06-12 23:51:37 +00:00
|
|
|
|
|
|
|
NewDeleteScheduler();
|
2017-11-17 19:56:41 +00:00
|
|
|
delete_scheduler_->SetMaxTrashDBRatio(0.25);
|
2017-06-12 23:51:37 +00:00
|
|
|
|
|
|
|
std::vector<std::string> generated_files;
|
|
|
|
for (int i = 0; i < num_files; i++) {
|
2022-05-06 20:03:58 +00:00
|
|
|
std::string file_name = "file" + std::to_string(i) + ".data";
|
2017-06-12 23:51:37 +00:00
|
|
|
generated_files.push_back(NewDummyFile(file_name, file_size));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::string& file_name : generated_files) {
|
2021-01-07 23:21:51 +00:00
|
|
|
ASSERT_OK(delete_scheduler_->DeleteFile(file_name, ""));
|
2017-06-12 23:51:37 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 18:22:31 +00:00
|
|
|
// When we end up with 26 files in trash we will start
|
2017-06-12 23:51:37 +00:00
|
|
|
// deleting new files immediately
|
|
|
|
ASSERT_EQ(fg_delete_file, 74);
|
2020-06-05 16:41:03 +00:00
|
|
|
ASSERT_EQ(26, stats_->getAndResetTickerCount(FILES_MARKED_TRASH));
|
|
|
|
ASSERT_EQ(74, stats_->getAndResetTickerCount(FILES_DELETED_IMMEDIATELY));
|
2017-06-12 23:51:37 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2017-06-12 23:51:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 20:25:54 +00:00
|
|
|
TEST_F(DeleteSchedulerTest, IsTrashCheck) {
|
|
|
|
// Trash files
|
|
|
|
ASSERT_TRUE(DeleteScheduler::IsTrashFile("x.trash"));
|
|
|
|
ASSERT_TRUE(DeleteScheduler::IsTrashFile(".trash"));
|
|
|
|
ASSERT_TRUE(DeleteScheduler::IsTrashFile("abc.sst.trash"));
|
|
|
|
ASSERT_TRUE(DeleteScheduler::IsTrashFile("/a/b/c/abc..sst.trash"));
|
|
|
|
ASSERT_TRUE(DeleteScheduler::IsTrashFile("log.trash"));
|
|
|
|
ASSERT_TRUE(DeleteScheduler::IsTrashFile("^^^^^.log.trash"));
|
|
|
|
ASSERT_TRUE(DeleteScheduler::IsTrashFile("abc.t.trash"));
|
|
|
|
|
|
|
|
// Not trash files
|
|
|
|
ASSERT_FALSE(DeleteScheduler::IsTrashFile("abc.sst"));
|
|
|
|
ASSERT_FALSE(DeleteScheduler::IsTrashFile("abc.txt"));
|
|
|
|
ASSERT_FALSE(DeleteScheduler::IsTrashFile("/a/b/c/abc.sst"));
|
|
|
|
ASSERT_FALSE(DeleteScheduler::IsTrashFile("/a/b/c/abc.sstrash"));
|
|
|
|
ASSERT_FALSE(DeleteScheduler::IsTrashFile("^^^^^.trashh"));
|
|
|
|
ASSERT_FALSE(DeleteScheduler::IsTrashFile("abc.ttrash"));
|
|
|
|
ASSERT_FALSE(DeleteScheduler::IsTrashFile(".ttrash"));
|
|
|
|
ASSERT_FALSE(DeleteScheduler::IsTrashFile("abc.trashx"));
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2015-08-05 03:45:27 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2022-10-18 07:35:35 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2015-08-05 03:45:27 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|
2016-12-22 01:35:00 +00:00
|
|
|
|