]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/delete_scheduler.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / util / delete_scheduler.h
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.
5
6 #pragma once
7
8 #ifndef ROCKSDB_LITE
9
10 #include <map>
11 #include <queue>
12 #include <string>
13 #include <thread>
14
15 #include "monitoring/instrumented_mutex.h"
16 #include "port/port.h"
17
18 #include "rocksdb/status.h"
19
20 namespace rocksdb {
21
22 class Env;
23 class Logger;
24 class SstFileManagerImpl;
25
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,
30 //
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 {
34 public:
35 DeleteScheduler(Env* env, const std::string& trash_dir,
36 int64_t rate_bytes_per_sec, Logger* info_log,
37 SstFileManagerImpl* sst_file_manager);
38
39 ~DeleteScheduler();
40
41 // Return delete rate limit in bytes per second
42 int64_t GetRateBytesPerSecond() { return rate_bytes_per_sec_.load(); }
43
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);
47 }
48
49 // Move file to trash directory and schedule it's deletion
50 Status DeleteFile(const std::string& fname);
51
52 // Wait for all files being deleteing in the background to finish or for
53 // destructor to be called.
54 void WaitForEmptyTrash();
55
56 // Return a map containing errors that happened in BackgroundEmptyTrash
57 // file_path => error status
58 std::map<std::string, Status> GetBackgroundErrors();
59
60 private:
61 Status MoveToTrash(const std::string& file_path, std::string* path_in_trash);
62
63 Status DeleteTrashFile(const std::string& path_in_trash,
64 uint64_t* deleted_bytes);
65
66 void BackgroundEmptyTrash();
67
68 Env* env_;
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
82 bool closing_;
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_;
92 Logger* info_log_;
93 SstFileManagerImpl* sst_file_manager_;
94 static const uint64_t kMicrosInSecond = 1000 * 1000LL;
95 };
96
97 } // namespace rocksdb
98
99 #endif // ROCKSDB_LITE