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).
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.
15 #include "db/log_writer.h"
16 #include "db/column_family.h"
23 struct SuperVersionContext
{
24 struct WriteStallNotification
{
25 WriteStallInfo write_stall_info
;
26 const ImmutableCFOptions
* immutable_cf_options
;
29 autovector
<SuperVersion
*> superversions_to_free
;
30 #ifndef ROCKSDB_DISABLE_STALL_NOTIFICATION
31 autovector
<WriteStallNotification
> write_stall_notifications
;
33 unique_ptr
<SuperVersion
> new_superversion
; // if nullptr no new superversion
35 explicit SuperVersionContext(bool create_superversion
= false)
36 : new_superversion(create_superversion
? new SuperVersion() : nullptr) {}
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
)),
43 new_superversion(std::move(other
.new_superversion
)) {
46 void NewSuperVersion() {
47 new_superversion
= unique_ptr
<SuperVersion
>(new SuperVersion());
50 inline bool HaveSomethingToDelete() const {
51 #ifndef ROCKSDB_DISABLE_STALL_NOTIFICATION
52 return !superversions_to_free
.empty() ||
53 !write_stall_notifications
.empty();
55 return !superversions_to_free
.empty();
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
);
74 #endif // !defined(ROCKSDB_LITE) && !defined(ROCKSDB_DISABLE_STALL_NOTIFICATION)
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
);
85 write_stall_notifications
.clear();
86 #endif // !ROCKSDB_LITE
88 for (auto s
: superversions_to_free
) {
91 superversions_to_free
.clear();
94 ~SuperVersionContext() {
95 #ifndef ROCKSDB_DISABLE_STALL_NOTIFICATION
96 assert(write_stall_notifications
.empty());
98 assert(superversions_to_free
.empty());
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();
108 inline bool HaveSomethingToClean() const {
109 bool sv_have_sth
= false;
110 for (const auto& sv_ctx
: superversion_contexts
) {
111 if (sv_ctx
.HaveSomethingToDelete()) {
116 return memtables_to_free
.size() > 0 || logs_to_free
.size() > 0 ||
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
;
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
;
141 // the list of all live sst files that cannot be deleted
142 std::vector
<FileDescriptor
> sst_live
;
144 // a list of sst files that we need to delete
145 std::vector
<ObsoleteFileInfo
> sst_delete_files
;
147 // a list of log files that we need to delete
148 std::vector
<uint64_t> log_delete_files
;
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
;
154 // a list of manifest files that we need to delete
155 std::vector
<std::string
> manifest_delete_files
;
157 // a list of memtables to be free
158 autovector
<MemTable
*> memtables_to_free
;
160 // contexts for installing superversions for multiple column families
161 std::vector
<SuperVersionContext
> superversion_contexts
;
163 autovector
<log::Writer
*> logs_to_free
;
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
;
170 uint64_t prev_log_number
;
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;
177 explicit JobContext(int _job_id
, bool create_superversion
= false) {
179 manifest_file_number
= 0;
180 pending_manifest_file_number
= 0;
183 superversion_contexts
.emplace_back(
184 SuperVersionContext(create_superversion
));
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.
192 // free superversions
193 for (auto& sv_context
: superversion_contexts
) {
196 // free pending memtables
197 for (auto m
: memtables_to_free
) {
200 for (auto l
: logs_to_free
) {
204 memtables_to_free
.clear();
205 logs_to_free
.clear();
209 assert(memtables_to_free
.size() == 0);
210 assert(logs_to_free
.size() == 0);
214 } // namespace rocksdb