1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
15 #include "monitoring/instrumented_mutex.h"
16 #include "port/port.h"
18 #include "rocksdb/status.h"
24 class SstFileManagerImpl
;
26 // DeleteScheduler allows the DB to enforce a rate limit on file deletion,
27 // Instead of deleteing files immediately, files are moved to trash_dir
28 // and deleted in a background thread that apply sleep penlty between deletes
29 // if they are happening in a rate faster than rate_bytes_per_sec,
31 // Rate limiting can be turned off by setting rate_bytes_per_sec = 0, In this
32 // case DeleteScheduler will delete files immediately.
33 class DeleteScheduler
{
35 DeleteScheduler(Env
* env
, const std::string
& trash_dir
,
36 int64_t rate_bytes_per_sec
, Logger
* info_log
,
37 SstFileManagerImpl
* sst_file_manager
);
41 // Return delete rate limit in bytes per second
42 int64_t GetRateBytesPerSecond() { return rate_bytes_per_sec_
.load(); }
44 // Set delete rate limit in bytes per second
45 void SetRateBytesPerSecond(int64_t bytes_per_sec
) {
46 return rate_bytes_per_sec_
.store(bytes_per_sec
);
49 // Move file to trash directory and schedule it's deletion
50 Status
DeleteFile(const std::string
& fname
);
52 // Wait for all files being deleteing in the background to finish or for
53 // destructor to be called.
54 void WaitForEmptyTrash();
56 // Return a map containing errors that happened in BackgroundEmptyTrash
57 // file_path => error status
58 std::map
<std::string
, Status
> GetBackgroundErrors();
61 Status
MoveToTrash(const std::string
& file_path
, std::string
* path_in_trash
);
63 Status
DeleteTrashFile(const std::string
& path_in_trash
,
64 uint64_t* deleted_bytes
);
66 void BackgroundEmptyTrash();
69 // Path to the trash directory
70 std::string trash_dir_
;
71 // Maximum number of bytes that should be deleted per second
72 std::atomic
<int64_t> rate_bytes_per_sec_
;
73 // Mutex to protect queue_, pending_files_, bg_errors_, closing_
74 InstrumentedMutex mu_
;
75 // Queue of files in trash that need to be deleted
76 std::queue
<std::string
> queue_
;
77 // Number of files in trash that are waiting to be deleted
78 int32_t pending_files_
;
79 // Errors that happened in BackgroundEmptyTrash (file_path => error)
80 std::map
<std::string
, Status
> bg_errors_
;
81 // Set to true in ~DeleteScheduler() to force BackgroundEmptyTrash to stop
83 // Condition variable signaled in these conditions
84 // - pending_files_ value change from 0 => 1
85 // - pending_files_ value change from 1 => 0
86 // - closing_ value is set to true
87 InstrumentedCondVar cv_
;
88 // Background thread running BackgroundEmptyTrash
89 std::unique_ptr
<port::Thread
> bg_thread_
;
90 // Mutex to protect threads from file name conflicts
91 InstrumentedMutex file_move_mu_
;
93 SstFileManagerImpl
* sst_file_manager_
;
94 static const uint64_t kMicrosInSecond
= 1000 * 1000LL;
97 } // namespace rocksdb
99 #endif // ROCKSDB_LITE