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 #pragma once
7 
8 #include <memory>
9 #include <string>
10 #include <unordered_map>
11 #include <vector>
12 
13 #include "rocksdb/file_system.h"
14 #include "rocksdb/status.h"
15 
16 namespace ROCKSDB_NAMESPACE {
17 
18 class Env;
19 class Logger;
20 
21 // SstFileManager is used to track SST files in the DB and control their
22 // deletion rate.
23 // All SstFileManager public functions are thread-safe.
24 // SstFileManager is not extensible.
25 class SstFileManager {
26  public:
~SstFileManager()27   virtual ~SstFileManager() {}
28 
29   // Update the maximum allowed space that should be used by RocksDB, if
30   // the total size of the SST files exceeds max_allowed_space, writes to
31   // RocksDB will fail.
32   //
33   // Setting max_allowed_space to 0 will disable this feature; maximum allowed
34   // space will be infinite (Default value).
35   //
36   // thread-safe.
37   virtual void SetMaxAllowedSpaceUsage(uint64_t max_allowed_space) = 0;
38 
39   // Set the amount of buffer room each compaction should be able to leave.
40   // In other words, at its maximum disk space consumption, the compaction
41   // should still leave compaction_buffer_size available on the disk so that
42   // other background functions may continue, such as logging and flushing.
43   virtual void SetCompactionBufferSize(uint64_t compaction_buffer_size) = 0;
44 
45   // Return true if the total size of SST files exceeded the maximum allowed
46   // space usage.
47   //
48   // thread-safe.
49   virtual bool IsMaxAllowedSpaceReached() = 0;
50 
51   // Returns true if the total size of SST files as well as estimated size
52   // of ongoing compactions exceeds the maximums allowed space usage.
53   virtual bool IsMaxAllowedSpaceReachedIncludingCompactions() = 0;
54 
55   // Return the total size of all tracked files.
56   // thread-safe
57   virtual uint64_t GetTotalSize() = 0;
58 
59   // Return a map containing all tracked files and their corresponding sizes.
60   // thread-safe
61   virtual std::unordered_map<std::string, uint64_t> GetTrackedFiles() = 0;
62 
63   // Return delete rate limit in bytes per second.
64   // thread-safe
65   virtual int64_t GetDeleteRateBytesPerSecond() = 0;
66 
67   // Update the delete rate limit in bytes per second.
68   // zero means disable delete rate limiting and delete files immediately
69   // thread-safe
70   virtual void SetDeleteRateBytesPerSecond(int64_t delete_rate) = 0;
71 
72   // Return trash/DB size ratio where new files will be deleted immediately
73   // thread-safe
74   virtual double GetMaxTrashDBRatio() = 0;
75 
76   // Update trash/DB size ratio where new files will be deleted immediately
77   // thread-safe
78   virtual void SetMaxTrashDBRatio(double ratio) = 0;
79 
80   // Return the total size of trash files
81   // thread-safe
82   virtual uint64_t GetTotalTrashSize() = 0;
83 };
84 
85 // Create a new SstFileManager that can be shared among multiple RocksDB
86 // instances to track SST file and control there deletion rate.
87 // Even though SstFileManager don't track WAL files but it still control
88 // there deletion rate.
89 //
90 // @param env: Pointer to Env object, please see "rocksdb/env.h".
91 // @param fs: Pointer to FileSystem object (rocksdb/file_system.h"
92 // @param info_log: If not nullptr, info_log will be used to log errors.
93 //
94 // == Deletion rate limiting specific arguments ==
95 // @param trash_dir: Deprecated, this argument have no effect
96 // @param rate_bytes_per_sec: How many bytes should be deleted per second, If
97 //    this value is set to 1024 (1 Kb / sec) and we deleted a file of size 4 Kb
98 //    in 1 second, we will wait for another 3 seconds before we delete other
99 //    files, Set to 0 to disable deletion rate limiting.
100 //    This option also affect the delete rate of WAL files in the DB.
101 // @param delete_existing_trash: Deprecated, this argument have no effect, but
102 //    if user provide trash_dir we will schedule deletes for files in the dir
103 // @param status: If not nullptr, status will contain any errors that happened
104 //    during creating the missing trash_dir or deleting existing files in trash.
105 // @param max_trash_db_ratio: If the trash size constitutes for more than this
106 //    fraction of the total DB size we will start deleting new files passed to
107 //    DeleteScheduler immediately
108 // @param bytes_max_delete_chunk: if a file to delete is larger than delete
109 //    chunk, ftruncate the file by this size each time, rather than dropping the
110 //    whole file. 0 means to always delete the whole file. If the file has more
111 //    than one linked names, the file will be deleted as a whole. Either way,
112 //    `rate_bytes_per_sec` will be appreciated. NOTE that with this option,
113 //    files already renamed as a trash may be partial, so users should not
114 //    directly recover them without checking.
115 extern SstFileManager* NewSstFileManager(
116     Env* env, std::shared_ptr<FileSystem> fs,
117     std::shared_ptr<Logger> info_log = nullptr,
118     const std::string& trash_dir = "", int64_t rate_bytes_per_sec = 0,
119     bool delete_existing_trash = true, Status* status = nullptr,
120     double max_trash_db_ratio = 0.25,
121     uint64_t bytes_max_delete_chunk = 64 * 1024 * 1024);
122 
123 // Same as above, but takes a pointer to a legacy Env object, instead of
124 // Env and FileSystem objects
125 extern SstFileManager* NewSstFileManager(
126     Env* env, std::shared_ptr<Logger> info_log = nullptr,
127     std::string trash_dir = "", int64_t rate_bytes_per_sec = 0,
128     bool delete_existing_trash = true, Status* status = nullptr,
129     double max_trash_db_ratio = 0.25,
130     uint64_t bytes_max_delete_chunk = 64 * 1024 * 1024);
131 
132 }  // namespace ROCKSDB_NAMESPACE
133