]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/db/job_context.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rocksdb / db / job_context.h
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 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
9
10 #pragma once
11
12 #include <string>
13 #include <vector>
14
15 #include "db/log_writer.h"
16 #include "db/column_family.h"
17
18 namespace rocksdb {
19
20 class MemTable;
21 struct SuperVersion;
22
23 struct SuperVersionContext {
24 struct WriteStallNotification {
25 WriteStallInfo write_stall_info;
26 const ImmutableCFOptions* immutable_cf_options;
27 };
28
29 autovector<SuperVersion*> superversions_to_free;
30 #ifndef ROCKSDB_DISABLE_STALL_NOTIFICATION
31 autovector<WriteStallNotification> write_stall_notifications;
32 #endif
33 unique_ptr<SuperVersion> new_superversion; // if nullptr no new superversion
34
35 explicit SuperVersionContext(bool create_superversion = false)
36 : new_superversion(create_superversion ? new SuperVersion() : nullptr) {}
37
38 explicit SuperVersionContext(SuperVersionContext&& other)
39 : superversions_to_free(std::move(other.superversions_to_free)),
40 #ifndef ROCKSDB_DISABLE_STALL_NOTIFICATION
41 write_stall_notifications(std::move(other.write_stall_notifications)),
42 #endif
43 new_superversion(std::move(other.new_superversion)) {
44 }
45
46 void NewSuperVersion() {
47 new_superversion = unique_ptr<SuperVersion>(new SuperVersion());
48 }
49
50 inline bool HaveSomethingToDelete() const {
51 #ifndef ROCKSDB_DISABLE_STALL_NOTIFICATION
52 return !superversions_to_free.empty() ||
53 !write_stall_notifications.empty();
54 #else
55 return !superversions_to_free.empty();
56 #endif
57 }
58
59 void PushWriteStallNotification(
60 WriteStallCondition old_cond, WriteStallCondition new_cond,
61 const std::string& name, const ImmutableCFOptions* ioptions) {
62 #if !defined(ROCKSDB_LITE) && !defined(ROCKSDB_DISABLE_STALL_NOTIFICATION)
63 WriteStallNotification notif;
64 notif.write_stall_info.cf_name = name;
65 notif.write_stall_info.condition.prev = old_cond;
66 notif.write_stall_info.condition.cur = new_cond;
67 notif.immutable_cf_options = ioptions;
68 write_stall_notifications.push_back(notif);
69 #else
70 (void)old_cond;
71 (void)new_cond;
72 (void)name;
73 (void)ioptions;
74 #endif // !defined(ROCKSDB_LITE) && !defined(ROCKSDB_DISABLE_STALL_NOTIFICATION)
75 }
76
77 void Clean() {
78 #if !defined(ROCKSDB_LITE) && !defined(ROCKSDB_DISABLE_STALL_NOTIFICATION)
79 // notify listeners on changed write stall conditions
80 for (auto& notif : write_stall_notifications) {
81 for (auto& listener : notif.immutable_cf_options->listeners) {
82 listener->OnStallConditionsChanged(notif.write_stall_info);
83 }
84 }
85 write_stall_notifications.clear();
86 #endif // !ROCKSDB_LITE
87 // free superversions
88 for (auto s : superversions_to_free) {
89 delete s;
90 }
91 superversions_to_free.clear();
92 }
93
94 ~SuperVersionContext() {
95 #ifndef ROCKSDB_DISABLE_STALL_NOTIFICATION
96 assert(write_stall_notifications.empty());
97 #endif
98 assert(superversions_to_free.empty());
99 }
100 };
101
102 struct JobContext {
103 inline bool HaveSomethingToDelete() const {
104 return full_scan_candidate_files.size() || sst_delete_files.size() ||
105 log_delete_files.size() || manifest_delete_files.size();
106 }
107
108 inline bool HaveSomethingToClean() const {
109 bool sv_have_sth = false;
110 for (const auto& sv_ctx : superversion_contexts) {
111 if (sv_ctx.HaveSomethingToDelete()) {
112 sv_have_sth = true;
113 break;
114 }
115 }
116 return memtables_to_free.size() > 0 || logs_to_free.size() > 0 ||
117 sv_have_sth;
118 }
119
120 // Structure to store information for candidate files to delete.
121 struct CandidateFileInfo {
122 std::string file_name;
123 std::string file_path;
124 CandidateFileInfo(std::string name, std::string path)
125 : file_name(std::move(name)), file_path(std::move(path)) {}
126 bool operator==(const CandidateFileInfo& other) const {
127 return file_name == other.file_name &&
128 file_path == other.file_path;
129 }
130 };
131
132 // Unique job id
133 int job_id;
134
135 // a list of all files that we'll consider deleting
136 // (every once in a while this is filled up with all files
137 // in the DB directory)
138 // (filled only if we're doing full scan)
139 std::vector<CandidateFileInfo> full_scan_candidate_files;
140
141 // the list of all live sst files that cannot be deleted
142 std::vector<FileDescriptor> sst_live;
143
144 // a list of sst files that we need to delete
145 std::vector<ObsoleteFileInfo> sst_delete_files;
146
147 // a list of log files that we need to delete
148 std::vector<uint64_t> log_delete_files;
149
150 // a list of log files that we need to preserve during full purge since they
151 // will be reused later
152 std::vector<uint64_t> log_recycle_files;
153
154 // a list of manifest files that we need to delete
155 std::vector<std::string> manifest_delete_files;
156
157 // a list of memtables to be free
158 autovector<MemTable*> memtables_to_free;
159
160 // contexts for installing superversions for multiple column families
161 std::vector<SuperVersionContext> superversion_contexts;
162
163 autovector<log::Writer*> logs_to_free;
164
165 // the current manifest_file_number, log_number and prev_log_number
166 // that corresponds to the set of files in 'live'.
167 uint64_t manifest_file_number;
168 uint64_t pending_manifest_file_number;
169 uint64_t log_number;
170 uint64_t prev_log_number;
171
172 uint64_t min_pending_output = 0;
173 uint64_t prev_total_log_size = 0;
174 size_t num_alive_log_files = 0;
175 uint64_t size_log_to_delete = 0;
176
177 explicit JobContext(int _job_id, bool create_superversion = false) {
178 job_id = _job_id;
179 manifest_file_number = 0;
180 pending_manifest_file_number = 0;
181 log_number = 0;
182 prev_log_number = 0;
183 superversion_contexts.emplace_back(
184 SuperVersionContext(create_superversion));
185 }
186
187 // For non-empty JobContext Clean() has to be called at least once before
188 // before destruction (see asserts in ~JobContext()). Should be called with
189 // unlocked DB mutex. Destructor doesn't call Clean() to avoid accidentally
190 // doing potentially slow Clean() with locked DB mutex.
191 void Clean() {
192 // free superversions
193 for (auto& sv_context : superversion_contexts) {
194 sv_context.Clean();
195 }
196 // free pending memtables
197 for (auto m : memtables_to_free) {
198 delete m;
199 }
200 for (auto l : logs_to_free) {
201 delete l;
202 }
203
204 memtables_to_free.clear();
205 logs_to_free.clear();
206 }
207
208 ~JobContext() {
209 assert(memtables_to_free.size() == 0);
210 assert(logs_to_free.size() == 0);
211 }
212 };
213
214 } // namespace rocksdb