diff --git a/file/delete_scheduler.cc b/file/delete_scheduler.cc index 8ebdd1560..4595d8939 100644 --- a/file/delete_scheduler.cc +++ b/file/delete_scheduler.cc @@ -32,13 +32,13 @@ DeleteScheduler::DeleteScheduler(Env* env, FileSystem* fs, bytes_max_delete_chunk_(bytes_max_delete_chunk), closing_(false), cv_(&mu_), + bg_thread_(nullptr), info_log_(info_log), sst_file_manager_(sst_file_manager), max_trash_db_ratio_(max_trash_db_ratio) { assert(sst_file_manager != nullptr); assert(max_trash_db_ratio >= 0); - bg_thread_.reset( - new port::Thread(&DeleteScheduler::BackgroundEmptyTrash, this)); + MaybeCreateBackgroundThread(); } DeleteScheduler::~DeleteScheduler() { @@ -352,6 +352,13 @@ void DeleteScheduler::WaitForEmptyTrash() { } } +void DeleteScheduler::MaybeCreateBackgroundThread() { + if(bg_thread_ == nullptr && rate_bytes_per_sec_.load() > 0) { + bg_thread_.reset( + new port::Thread(&DeleteScheduler::BackgroundEmptyTrash, this)); + } +} + } // namespace ROCKSDB_NAMESPACE #endif // ROCKSDB_LITE diff --git a/file/delete_scheduler.h b/file/delete_scheduler.h index 60948480a..e4bf00d32 100644 --- a/file/delete_scheduler.h +++ b/file/delete_scheduler.h @@ -45,6 +45,7 @@ class DeleteScheduler { // Set delete rate limit in bytes per second void SetRateBytesPerSecond(int64_t bytes_per_sec) { rate_bytes_per_sec_.store(bytes_per_sec); + MaybeCreateBackgroundThread(); } // Mark file as trash directory and schedule it's deletion. If force_bg is @@ -91,6 +92,8 @@ class DeleteScheduler { void BackgroundEmptyTrash(); + void MaybeCreateBackgroundThread(); + Env* env_; FileSystem* fs_;