/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/delete_scheduler.h
Line | Count | Source (jump to first uncovered line) |
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 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <map> |
24 | | #include <queue> |
25 | | #include <string> |
26 | | #include <thread> |
27 | | |
28 | | #include "yb/rocksdb/port/port.h" |
29 | | |
30 | | #include "yb/rocksdb/status.h" |
31 | | |
32 | | namespace rocksdb { |
33 | | |
34 | | class Env; |
35 | | class Logger; |
36 | | class SstFileManagerImpl; |
37 | | |
38 | | // DeleteScheduler allows the DB to enforce a rate limit on file deletion, |
39 | | // Instead of deleteing files immediately, files are moved to trash_dir |
40 | | // and deleted in a background thread that apply sleep penlty between deletes |
41 | | // if they are happening in a rate faster than rate_bytes_per_sec, |
42 | | // |
43 | | // Rate limiting can be turned off by setting rate_bytes_per_sec = 0, In this |
44 | | // case DeleteScheduler will delete files immediately. |
45 | | class DeleteScheduler { |
46 | | public: |
47 | | DeleteScheduler(Env* env, const std::string& trash_dir, |
48 | | int64_t rate_bytes_per_sec, Logger* info_log, |
49 | | SstFileManagerImpl* sst_file_manager); |
50 | | |
51 | | ~DeleteScheduler(); |
52 | | |
53 | | // Return delete rate limit in bytes per second |
54 | 0 | int64_t GetRateBytesPerSecond() { return rate_bytes_per_sec_; } |
55 | | |
56 | | // Move file to trash directory and schedule it's deletion |
57 | | Status DeleteFile(const std::string& fname); |
58 | | |
59 | | // Wait for all files being deleteing in the background to finish or for |
60 | | // destructor to be called. |
61 | | void WaitForEmptyTrash(); |
62 | | |
63 | | // Return a map containing errors that happened in BackgroundEmptyTrash |
64 | | // file_path => error status |
65 | | std::map<std::string, Status> GetBackgroundErrors(); |
66 | | |
67 | | private: |
68 | | Status MoveToTrash(const std::string& file_path, std::string* path_in_trash); |
69 | | |
70 | | Status DeleteTrashFile(const std::string& path_in_trash, |
71 | | uint64_t* deleted_bytes); |
72 | | |
73 | | void BackgroundEmptyTrash(); |
74 | | |
75 | | Env* env_; |
76 | | // Path to the trash directory |
77 | | std::string trash_dir_; |
78 | | // Maximum number of bytes that should be deleted per second |
79 | | int64_t rate_bytes_per_sec_; |
80 | | // Mutex to protect queue_, pending_files_, bg_errors_, closing_ |
81 | | port::Mutex mu_; |
82 | | // Queue of files in trash that need to be deleted |
83 | | std::queue<std::string> queue_; |
84 | | // Number of files in trash that are waiting to be deleted |
85 | | int32_t pending_files_; |
86 | | // Errors that happened in BackgroundEmptyTrash (file_path => error) |
87 | | std::map<std::string, Status> bg_errors_; |
88 | | // Set to true in ~DeleteScheduler() to force BackgroundEmptyTrash to stop |
89 | | bool closing_; |
90 | | // Condition variable signaled in these conditions |
91 | | // - pending_files_ value change from 0 => 1 |
92 | | // - pending_files_ value change from 1 => 0 |
93 | | // - closing_ value is set to true |
94 | | port::CondVar cv_; |
95 | | // Background thread running BackgroundEmptyTrash |
96 | | std::unique_ptr<std::thread> bg_thread_; |
97 | | // Mutex to protect threads from file name conflicts |
98 | | port::Mutex file_move_mu_; |
99 | | Logger* info_log_; |
100 | | SstFileManagerImpl* sst_file_manager_; |
101 | | static const uint64_t kMicrosInSecond = 1000 * 1000LL; |
102 | | }; |
103 | | |
104 | | } // namespace rocksdb |