]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
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 | ||
f67539c2 | 10 | #include "db/compaction/compaction_picker.h" |
7c673cae | 11 | |
f67539c2 | 12 | #include <cinttypes> |
7c673cae FG |
13 | #include <limits> |
14 | #include <queue> | |
15 | #include <string> | |
16 | #include <utility> | |
494da23a | 17 | #include <vector> |
7c673cae | 18 | #include "db/column_family.h" |
f67539c2 TL |
19 | #include "file/filename.h" |
20 | #include "logging/log_buffer.h" | |
7c673cae | 21 | #include "monitoring/statistics.h" |
f67539c2 | 22 | #include "test_util/sync_point.h" |
7c673cae FG |
23 | #include "util/random.h" |
24 | #include "util/string_util.h" | |
7c673cae | 25 | |
f67539c2 | 26 | namespace ROCKSDB_NAMESPACE { |
7c673cae FG |
27 | |
28 | namespace { | |
29 | uint64_t TotalCompensatedFileSize(const std::vector<FileMetaData*>& files) { | |
30 | uint64_t sum = 0; | |
31 | for (size_t i = 0; i < files.size() && files[i]; i++) { | |
32 | sum += files[i]->compensated_file_size; | |
33 | } | |
34 | return sum; | |
35 | } | |
494da23a | 36 | } // anonymous namespace |
11fdf7f2 TL |
37 | |
38 | bool FindIntraL0Compaction(const std::vector<FileMetaData*>& level_files, | |
39 | size_t min_files_to_compact, | |
40 | uint64_t max_compact_bytes_per_del_file, | |
f67539c2 TL |
41 | uint64_t max_compaction_bytes, |
42 | CompactionInputFiles* comp_inputs, | |
43 | SequenceNumber earliest_mem_seqno) { | |
44 | // Do not pick ingested file when there is at least one memtable not flushed | |
45 | // which of seqno is overlap with the sst. | |
46 | TEST_SYNC_POINT("FindIntraL0Compaction"); | |
47 | size_t start = 0; | |
48 | for (; start < level_files.size(); start++) { | |
49 | if (level_files[start]->being_compacted) { | |
50 | return false; | |
51 | } | |
52 | // If there is no data in memtable, the earliest sequence number would the | |
53 | // largest sequence number in last memtable. | |
54 | // Because all files are sorted in descending order by largest_seqno, so we | |
55 | // only need to check the first one. | |
56 | if (level_files[start]->fd.largest_seqno <= earliest_mem_seqno) { | |
57 | break; | |
58 | } | |
59 | } | |
60 | if (start >= level_files.size()) { | |
61 | return false; | |
62 | } | |
63 | size_t compact_bytes = static_cast<size_t>(level_files[start]->fd.file_size); | |
64 | uint64_t compensated_compact_bytes = | |
65 | level_files[start]->compensated_file_size; | |
11fdf7f2 | 66 | size_t compact_bytes_per_del_file = port::kMaxSizet; |
f67539c2 TL |
67 | // Compaction range will be [start, limit). |
68 | size_t limit; | |
69 | // Pull in files until the amount of compaction work per deleted file begins | |
70 | // increasing or maximum total compaction size is reached. | |
11fdf7f2 | 71 | size_t new_compact_bytes_per_del_file = 0; |
f67539c2 TL |
72 | for (limit = start + 1; limit < level_files.size(); ++limit) { |
73 | compact_bytes += static_cast<size_t>(level_files[limit]->fd.file_size); | |
74 | compensated_compact_bytes += level_files[limit]->compensated_file_size; | |
75 | new_compact_bytes_per_del_file = compact_bytes / (limit - start); | |
76 | if (level_files[limit]->being_compacted || | |
77 | new_compact_bytes_per_del_file > compact_bytes_per_del_file || | |
78 | compensated_compact_bytes > max_compaction_bytes) { | |
11fdf7f2 TL |
79 | break; |
80 | } | |
81 | compact_bytes_per_del_file = new_compact_bytes_per_del_file; | |
82 | } | |
83 | ||
f67539c2 | 84 | if ((limit - start) >= min_files_to_compact && |
11fdf7f2 TL |
85 | compact_bytes_per_del_file < max_compact_bytes_per_del_file) { |
86 | assert(comp_inputs != nullptr); | |
87 | comp_inputs->level = 0; | |
f67539c2 | 88 | for (size_t i = start; i < limit; ++i) { |
11fdf7f2 TL |
89 | comp_inputs->files.push_back(level_files[i]); |
90 | } | |
91 | return true; | |
92 | } | |
93 | return false; | |
94 | } | |
7c673cae FG |
95 | |
96 | // Determine compression type, based on user options, level of the output | |
97 | // file and whether compression is disabled. | |
98 | // If enable_compression is false, then compression is always disabled no | |
99 | // matter what the values of the other two parameters are. | |
100 | // Otherwise, the compression type is determined based on options and level. | |
101 | CompressionType GetCompressionType(const ImmutableCFOptions& ioptions, | |
102 | const VersionStorageInfo* vstorage, | |
103 | const MutableCFOptions& mutable_cf_options, | |
104 | int level, int base_level, | |
105 | const bool enable_compression) { | |
106 | if (!enable_compression) { | |
107 | // disable compression | |
108 | return kNoCompression; | |
109 | } | |
110 | ||
111 | // If bottommost_compression is set and we are compacting to the | |
112 | // bottommost level then we should use it. | |
20effc67 | 113 | if (mutable_cf_options.bottommost_compression != kDisableCompressionOption && |
11fdf7f2 | 114 | level >= (vstorage->num_non_empty_levels() - 1)) { |
20effc67 | 115 | return mutable_cf_options.bottommost_compression; |
7c673cae FG |
116 | } |
117 | // If the user has specified a different compression level for each level, | |
118 | // then pick the compression for that level. | |
119 | if (!ioptions.compression_per_level.empty()) { | |
120 | assert(level == 0 || level >= base_level); | |
121 | int idx = (level == 0) ? 0 : level - base_level + 1; | |
122 | ||
123 | const int n = static_cast<int>(ioptions.compression_per_level.size()) - 1; | |
124 | // It is possible for level_ to be -1; in that case, we use level | |
125 | // 0's compression. This occurs mostly in backwards compatibility | |
126 | // situations when the builder doesn't know what level the file | |
127 | // belongs to. Likewise, if level is beyond the end of the | |
128 | // specified compression levels, use the last value. | |
129 | return ioptions.compression_per_level[std::max(0, std::min(idx, n))]; | |
130 | } else { | |
131 | return mutable_cf_options.compression; | |
132 | } | |
133 | } | |
134 | ||
20effc67 | 135 | CompressionOptions GetCompressionOptions(const MutableCFOptions& cf_options, |
11fdf7f2 TL |
136 | const VersionStorageInfo* vstorage, |
137 | int level, | |
138 | const bool enable_compression) { | |
139 | if (!enable_compression) { | |
20effc67 | 140 | return cf_options.compression_opts; |
11fdf7f2 | 141 | } |
20effc67 TL |
142 | // If bottommost_compression_opts is enabled and we are compacting to the |
143 | // bottommost level then we should use the specified compression options. | |
144 | if (level >= (vstorage->num_non_empty_levels() - 1) && | |
145 | cf_options.bottommost_compression_opts.enabled) { | |
146 | return cf_options.bottommost_compression_opts; | |
11fdf7f2 | 147 | } |
20effc67 | 148 | return cf_options.compression_opts; |
11fdf7f2 TL |
149 | } |
150 | ||
7c673cae FG |
151 | CompactionPicker::CompactionPicker(const ImmutableCFOptions& ioptions, |
152 | const InternalKeyComparator* icmp) | |
153 | : ioptions_(ioptions), icmp_(icmp) {} | |
154 | ||
155 | CompactionPicker::~CompactionPicker() {} | |
156 | ||
157 | // Delete this compaction from the list of running compactions. | |
158 | void CompactionPicker::ReleaseCompactionFiles(Compaction* c, Status status) { | |
159 | UnregisterCompaction(c); | |
160 | if (!status.ok()) { | |
161 | c->ResetNextCompactionIndex(); | |
162 | } | |
163 | } | |
164 | ||
165 | void CompactionPicker::GetRange(const CompactionInputFiles& inputs, | |
166 | InternalKey* smallest, | |
167 | InternalKey* largest) const { | |
168 | const int level = inputs.level; | |
169 | assert(!inputs.empty()); | |
170 | smallest->Clear(); | |
171 | largest->Clear(); | |
172 | ||
173 | if (level == 0) { | |
174 | for (size_t i = 0; i < inputs.size(); i++) { | |
175 | FileMetaData* f = inputs[i]; | |
176 | if (i == 0) { | |
177 | *smallest = f->smallest; | |
178 | *largest = f->largest; | |
179 | } else { | |
180 | if (icmp_->Compare(f->smallest, *smallest) < 0) { | |
181 | *smallest = f->smallest; | |
182 | } | |
183 | if (icmp_->Compare(f->largest, *largest) > 0) { | |
184 | *largest = f->largest; | |
185 | } | |
186 | } | |
187 | } | |
188 | } else { | |
189 | *smallest = inputs[0]->smallest; | |
190 | *largest = inputs[inputs.size() - 1]->largest; | |
191 | } | |
192 | } | |
193 | ||
194 | void CompactionPicker::GetRange(const CompactionInputFiles& inputs1, | |
195 | const CompactionInputFiles& inputs2, | |
196 | InternalKey* smallest, | |
197 | InternalKey* largest) const { | |
198 | assert(!inputs1.empty() || !inputs2.empty()); | |
199 | if (inputs1.empty()) { | |
200 | GetRange(inputs2, smallest, largest); | |
201 | } else if (inputs2.empty()) { | |
202 | GetRange(inputs1, smallest, largest); | |
203 | } else { | |
204 | InternalKey smallest1, smallest2, largest1, largest2; | |
205 | GetRange(inputs1, &smallest1, &largest1); | |
206 | GetRange(inputs2, &smallest2, &largest2); | |
207 | *smallest = | |
208 | icmp_->Compare(smallest1, smallest2) < 0 ? smallest1 : smallest2; | |
209 | *largest = icmp_->Compare(largest1, largest2) < 0 ? largest2 : largest1; | |
210 | } | |
211 | } | |
212 | ||
213 | void CompactionPicker::GetRange(const std::vector<CompactionInputFiles>& inputs, | |
214 | InternalKey* smallest, | |
215 | InternalKey* largest) const { | |
216 | InternalKey current_smallest; | |
217 | InternalKey current_largest; | |
218 | bool initialized = false; | |
219 | for (const auto& in : inputs) { | |
220 | if (in.empty()) { | |
221 | continue; | |
222 | } | |
223 | GetRange(in, ¤t_smallest, ¤t_largest); | |
224 | if (!initialized) { | |
225 | *smallest = current_smallest; | |
226 | *largest = current_largest; | |
227 | initialized = true; | |
228 | } else { | |
229 | if (icmp_->Compare(current_smallest, *smallest) < 0) { | |
230 | *smallest = current_smallest; | |
231 | } | |
232 | if (icmp_->Compare(current_largest, *largest) > 0) { | |
233 | *largest = current_largest; | |
234 | } | |
235 | } | |
236 | } | |
237 | assert(initialized); | |
238 | } | |
239 | ||
11fdf7f2 | 240 | bool CompactionPicker::ExpandInputsToCleanCut(const std::string& /*cf_name*/, |
7c673cae | 241 | VersionStorageInfo* vstorage, |
11fdf7f2 TL |
242 | CompactionInputFiles* inputs, |
243 | InternalKey** next_smallest) { | |
7c673cae FG |
244 | // This isn't good compaction |
245 | assert(!inputs->empty()); | |
246 | ||
247 | const int level = inputs->level; | |
248 | // GetOverlappingInputs will always do the right thing for level-0. | |
249 | // So we don't need to do any expansion if level == 0. | |
250 | if (level == 0) { | |
251 | return true; | |
252 | } | |
253 | ||
254 | InternalKey smallest, largest; | |
255 | ||
256 | // Keep expanding inputs until we are sure that there is a "clean cut" | |
257 | // boundary between the files in input and the surrounding files. | |
258 | // This will ensure that no parts of a key are lost during compaction. | |
259 | int hint_index = -1; | |
260 | size_t old_size; | |
261 | do { | |
262 | old_size = inputs->size(); | |
263 | GetRange(*inputs, &smallest, &largest); | |
264 | inputs->clear(); | |
265 | vstorage->GetOverlappingInputs(level, &smallest, &largest, &inputs->files, | |
11fdf7f2 TL |
266 | hint_index, &hint_index, true, |
267 | next_smallest); | |
7c673cae FG |
268 | } while (inputs->size() > old_size); |
269 | ||
270 | // we started off with inputs non-empty and the previous loop only grew | |
271 | // inputs. thus, inputs should be non-empty here | |
272 | assert(!inputs->empty()); | |
273 | ||
274 | // If, after the expansion, there are files that are already under | |
275 | // compaction, then we must drop/cancel this compaction. | |
276 | if (AreFilesInCompaction(inputs->files)) { | |
7c673cae FG |
277 | return false; |
278 | } | |
279 | return true; | |
280 | } | |
281 | ||
282 | bool CompactionPicker::RangeOverlapWithCompaction( | |
283 | const Slice& smallest_user_key, const Slice& largest_user_key, | |
284 | int level) const { | |
285 | const Comparator* ucmp = icmp_->user_comparator(); | |
286 | for (Compaction* c : compactions_in_progress_) { | |
287 | if (c->output_level() == level && | |
288 | ucmp->Compare(smallest_user_key, c->GetLargestUserKey()) <= 0 && | |
289 | ucmp->Compare(largest_user_key, c->GetSmallestUserKey()) >= 0) { | |
290 | // Overlap | |
291 | return true; | |
292 | } | |
293 | } | |
294 | // Did not overlap with any running compaction in level `level` | |
295 | return false; | |
296 | } | |
297 | ||
298 | bool CompactionPicker::FilesRangeOverlapWithCompaction( | |
299 | const std::vector<CompactionInputFiles>& inputs, int level) const { | |
300 | bool is_empty = true; | |
301 | for (auto& in : inputs) { | |
302 | if (!in.empty()) { | |
303 | is_empty = false; | |
304 | break; | |
305 | } | |
306 | } | |
307 | if (is_empty) { | |
308 | // No files in inputs | |
309 | return false; | |
310 | } | |
311 | ||
312 | InternalKey smallest, largest; | |
313 | GetRange(inputs, &smallest, &largest); | |
314 | return RangeOverlapWithCompaction(smallest.user_key(), largest.user_key(), | |
315 | level); | |
316 | } | |
317 | ||
318 | // Returns true if any one of specified files are being compacted | |
319 | bool CompactionPicker::AreFilesInCompaction( | |
320 | const std::vector<FileMetaData*>& files) { | |
321 | for (size_t i = 0; i < files.size(); i++) { | |
322 | if (files[i]->being_compacted) { | |
323 | return true; | |
324 | } | |
325 | } | |
326 | return false; | |
327 | } | |
328 | ||
329 | Compaction* CompactionPicker::CompactFiles( | |
330 | const CompactionOptions& compact_options, | |
331 | const std::vector<CompactionInputFiles>& input_files, int output_level, | |
332 | VersionStorageInfo* vstorage, const MutableCFOptions& mutable_cf_options, | |
20effc67 | 333 | const MutableDBOptions& mutable_db_options, uint32_t output_path_id) { |
7c673cae | 334 | assert(input_files.size()); |
11fdf7f2 TL |
335 | // This compaction output should not overlap with a running compaction as |
336 | // `SanitizeCompactionInputFiles` should've checked earlier and db mutex | |
337 | // shouldn't have been released since. | |
338 | assert(!FilesRangeOverlapWithCompaction(input_files, output_level)); | |
339 | ||
340 | CompressionType compression_type; | |
341 | if (compact_options.compression == kDisableCompressionOption) { | |
342 | int base_level; | |
343 | if (ioptions_.compaction_style == kCompactionStyleLevel) { | |
344 | base_level = vstorage->base_level(); | |
345 | } else { | |
346 | base_level = 1; | |
347 | } | |
348 | compression_type = | |
349 | GetCompressionType(ioptions_, vstorage, mutable_cf_options, | |
350 | output_level, base_level); | |
351 | } else { | |
352 | // TODO(ajkr): `CompactionOptions` offers configurable `CompressionType` | |
353 | // without configurable `CompressionOptions`, which is inconsistent. | |
354 | compression_type = compact_options.compression; | |
7c673cae | 355 | } |
11fdf7f2 | 356 | auto c = new Compaction( |
20effc67 TL |
357 | vstorage, ioptions_, mutable_cf_options, mutable_db_options, input_files, |
358 | output_level, compact_options.output_file_size_limit, | |
11fdf7f2 | 359 | mutable_cf_options.max_compaction_bytes, output_path_id, compression_type, |
20effc67 | 360 | GetCompressionOptions(mutable_cf_options, vstorage, output_level), |
11fdf7f2 TL |
361 | compact_options.max_subcompactions, |
362 | /* grandparents */ {}, true); | |
7c673cae FG |
363 | RegisterCompaction(c); |
364 | return c; | |
365 | } | |
366 | ||
367 | Status CompactionPicker::GetCompactionInputsFromFileNumbers( | |
368 | std::vector<CompactionInputFiles>* input_files, | |
369 | std::unordered_set<uint64_t>* input_set, const VersionStorageInfo* vstorage, | |
11fdf7f2 | 370 | const CompactionOptions& /*compact_options*/) const { |
7c673cae FG |
371 | if (input_set->size() == 0U) { |
372 | return Status::InvalidArgument( | |
373 | "Compaction must include at least one file."); | |
374 | } | |
375 | assert(input_files); | |
376 | ||
377 | std::vector<CompactionInputFiles> matched_input_files; | |
378 | matched_input_files.resize(vstorage->num_levels()); | |
379 | int first_non_empty_level = -1; | |
380 | int last_non_empty_level = -1; | |
381 | // TODO(yhchiang): use a lazy-initialized mapping from | |
382 | // file_number to FileMetaData in Version. | |
383 | for (int level = 0; level < vstorage->num_levels(); ++level) { | |
384 | for (auto file : vstorage->LevelFiles(level)) { | |
385 | auto iter = input_set->find(file->fd.GetNumber()); | |
386 | if (iter != input_set->end()) { | |
387 | matched_input_files[level].files.push_back(file); | |
388 | input_set->erase(iter); | |
389 | last_non_empty_level = level; | |
390 | if (first_non_empty_level == -1) { | |
391 | first_non_empty_level = level; | |
392 | } | |
393 | } | |
394 | } | |
395 | } | |
396 | ||
397 | if (!input_set->empty()) { | |
398 | std::string message( | |
399 | "Cannot find matched SST files for the following file numbers:"); | |
400 | for (auto fn : *input_set) { | |
401 | message += " "; | |
402 | message += ToString(fn); | |
403 | } | |
404 | return Status::InvalidArgument(message); | |
405 | } | |
406 | ||
407 | for (int level = first_non_empty_level; level <= last_non_empty_level; | |
408 | ++level) { | |
409 | matched_input_files[level].level = level; | |
410 | input_files->emplace_back(std::move(matched_input_files[level])); | |
411 | } | |
412 | ||
413 | return Status::OK(); | |
414 | } | |
415 | ||
416 | // Returns true if any one of the parent files are being compacted | |
417 | bool CompactionPicker::IsRangeInCompaction(VersionStorageInfo* vstorage, | |
418 | const InternalKey* smallest, | |
419 | const InternalKey* largest, | |
420 | int level, int* level_index) { | |
421 | std::vector<FileMetaData*> inputs; | |
422 | assert(level < NumberLevels()); | |
423 | ||
424 | vstorage->GetOverlappingInputs(level, smallest, largest, &inputs, | |
11fdf7f2 | 425 | level_index ? *level_index : 0, level_index); |
7c673cae FG |
426 | return AreFilesInCompaction(inputs); |
427 | } | |
428 | ||
429 | // Populates the set of inputs of all other levels that overlap with the | |
430 | // start level. | |
431 | // Now we assume all levels except start level and output level are empty. | |
432 | // Will also attempt to expand "start level" if that doesn't expand | |
433 | // "output level" or cause "level" to include a file for compaction that has an | |
434 | // overlapping user-key with another file. | |
435 | // REQUIRES: input_level and output_level are different | |
436 | // REQUIRES: inputs->empty() == false | |
437 | // Returns false if files on parent level are currently in compaction, which | |
438 | // means that we can't compact them | |
439 | bool CompactionPicker::SetupOtherInputs( | |
440 | const std::string& cf_name, const MutableCFOptions& mutable_cf_options, | |
441 | VersionStorageInfo* vstorage, CompactionInputFiles* inputs, | |
442 | CompactionInputFiles* output_level_inputs, int* parent_index, | |
443 | int base_index) { | |
444 | assert(!inputs->empty()); | |
445 | assert(output_level_inputs->empty()); | |
446 | const int input_level = inputs->level; | |
447 | const int output_level = output_level_inputs->level; | |
11fdf7f2 TL |
448 | if (input_level == output_level) { |
449 | // no possibility of conflict | |
450 | return true; | |
451 | } | |
7c673cae FG |
452 | |
453 | // For now, we only support merging two levels, start level and output level. | |
454 | // We need to assert other levels are empty. | |
455 | for (int l = input_level + 1; l < output_level; l++) { | |
456 | assert(vstorage->NumLevelFiles(l) == 0); | |
457 | } | |
458 | ||
459 | InternalKey smallest, largest; | |
460 | ||
461 | // Get the range one last time. | |
462 | GetRange(*inputs, &smallest, &largest); | |
463 | ||
464 | // Populate the set of next-level files (inputs_GetOutputLevelInputs()) to | |
465 | // include in compaction | |
466 | vstorage->GetOverlappingInputs(output_level, &smallest, &largest, | |
467 | &output_level_inputs->files, *parent_index, | |
468 | parent_index); | |
469 | if (AreFilesInCompaction(output_level_inputs->files)) { | |
470 | return false; | |
471 | } | |
472 | if (!output_level_inputs->empty()) { | |
473 | if (!ExpandInputsToCleanCut(cf_name, vstorage, output_level_inputs)) { | |
474 | return false; | |
475 | } | |
476 | } | |
477 | ||
478 | // See if we can further grow the number of inputs in "level" without | |
479 | // changing the number of "level+1" files we pick up. We also choose NOT | |
480 | // to expand if this would cause "level" to include some entries for some | |
481 | // user key, while excluding other entries for the same user key. This | |
482 | // can happen when one user key spans multiple files. | |
483 | if (!output_level_inputs->empty()) { | |
484 | const uint64_t limit = mutable_cf_options.max_compaction_bytes; | |
485 | const uint64_t output_level_inputs_size = | |
486 | TotalCompensatedFileSize(output_level_inputs->files); | |
487 | const uint64_t inputs_size = TotalCompensatedFileSize(inputs->files); | |
488 | bool expand_inputs = false; | |
489 | ||
490 | CompactionInputFiles expanded_inputs; | |
491 | expanded_inputs.level = input_level; | |
492 | // Get closed interval of output level | |
493 | InternalKey all_start, all_limit; | |
494 | GetRange(*inputs, *output_level_inputs, &all_start, &all_limit); | |
495 | bool try_overlapping_inputs = true; | |
496 | vstorage->GetOverlappingInputs(input_level, &all_start, &all_limit, | |
497 | &expanded_inputs.files, base_index, nullptr); | |
498 | uint64_t expanded_inputs_size = | |
499 | TotalCompensatedFileSize(expanded_inputs.files); | |
500 | if (!ExpandInputsToCleanCut(cf_name, vstorage, &expanded_inputs)) { | |
501 | try_overlapping_inputs = false; | |
502 | } | |
503 | if (try_overlapping_inputs && expanded_inputs.size() > inputs->size() && | |
504 | output_level_inputs_size + expanded_inputs_size < limit && | |
505 | !AreFilesInCompaction(expanded_inputs.files)) { | |
506 | InternalKey new_start, new_limit; | |
507 | GetRange(expanded_inputs, &new_start, &new_limit); | |
508 | CompactionInputFiles expanded_output_level_inputs; | |
509 | expanded_output_level_inputs.level = output_level; | |
510 | vstorage->GetOverlappingInputs(output_level, &new_start, &new_limit, | |
511 | &expanded_output_level_inputs.files, | |
512 | *parent_index, parent_index); | |
513 | assert(!expanded_output_level_inputs.empty()); | |
514 | if (!AreFilesInCompaction(expanded_output_level_inputs.files) && | |
515 | ExpandInputsToCleanCut(cf_name, vstorage, | |
516 | &expanded_output_level_inputs) && | |
517 | expanded_output_level_inputs.size() == output_level_inputs->size()) { | |
518 | expand_inputs = true; | |
519 | } | |
520 | } | |
521 | if (!expand_inputs) { | |
522 | vstorage->GetCleanInputsWithinInterval(input_level, &all_start, | |
523 | &all_limit, &expanded_inputs.files, | |
524 | base_index, nullptr); | |
525 | expanded_inputs_size = TotalCompensatedFileSize(expanded_inputs.files); | |
526 | if (expanded_inputs.size() > inputs->size() && | |
527 | output_level_inputs_size + expanded_inputs_size < limit && | |
528 | !AreFilesInCompaction(expanded_inputs.files)) { | |
529 | expand_inputs = true; | |
530 | } | |
531 | } | |
532 | if (expand_inputs) { | |
533 | ROCKS_LOG_INFO(ioptions_.info_log, | |
534 | "[%s] Expanding@%d %" ROCKSDB_PRIszt "+%" ROCKSDB_PRIszt | |
535 | "(%" PRIu64 "+%" PRIu64 " bytes) to %" ROCKSDB_PRIszt | |
11fdf7f2 | 536 | "+%" ROCKSDB_PRIszt " (%" PRIu64 "+%" PRIu64 " bytes)\n", |
7c673cae FG |
537 | cf_name.c_str(), input_level, inputs->size(), |
538 | output_level_inputs->size(), inputs_size, | |
539 | output_level_inputs_size, expanded_inputs.size(), | |
540 | output_level_inputs->size(), expanded_inputs_size, | |
541 | output_level_inputs_size); | |
542 | inputs->files = expanded_inputs.files; | |
543 | } | |
544 | } | |
545 | return true; | |
546 | } | |
547 | ||
548 | void CompactionPicker::GetGrandparents( | |
549 | VersionStorageInfo* vstorage, const CompactionInputFiles& inputs, | |
550 | const CompactionInputFiles& output_level_inputs, | |
551 | std::vector<FileMetaData*>* grandparents) { | |
552 | InternalKey start, limit; | |
553 | GetRange(inputs, output_level_inputs, &start, &limit); | |
554 | // Compute the set of grandparent files that overlap this compaction | |
555 | // (parent == level+1; grandparent == level+2) | |
556 | if (output_level_inputs.level + 1 < NumberLevels()) { | |
557 | vstorage->GetOverlappingInputs(output_level_inputs.level + 1, &start, | |
558 | &limit, grandparents); | |
559 | } | |
560 | } | |
561 | ||
562 | Compaction* CompactionPicker::CompactRange( | |
563 | const std::string& cf_name, const MutableCFOptions& mutable_cf_options, | |
20effc67 TL |
564 | const MutableDBOptions& mutable_db_options, VersionStorageInfo* vstorage, |
565 | int input_level, int output_level, | |
f67539c2 TL |
566 | const CompactRangeOptions& compact_range_options, const InternalKey* begin, |
567 | const InternalKey* end, InternalKey** compaction_end, bool* manual_conflict, | |
568 | uint64_t max_file_num_to_ignore) { | |
7c673cae FG |
569 | // CompactionPickerFIFO has its own implementation of compact range |
570 | assert(ioptions_.compaction_style != kCompactionStyleFIFO); | |
571 | ||
572 | if (input_level == ColumnFamilyData::kCompactAllLevels) { | |
573 | assert(ioptions_.compaction_style == kCompactionStyleUniversal); | |
574 | ||
575 | // Universal compaction with more than one level always compacts all the | |
576 | // files together to the last level. | |
577 | assert(vstorage->num_levels() > 1); | |
578 | // DBImpl::CompactRange() set output level to be the last level | |
11fdf7f2 TL |
579 | if (ioptions_.allow_ingest_behind) { |
580 | assert(output_level == vstorage->num_levels() - 2); | |
581 | } else { | |
582 | assert(output_level == vstorage->num_levels() - 1); | |
583 | } | |
7c673cae FG |
584 | // DBImpl::RunManualCompaction will make full range for universal compaction |
585 | assert(begin == nullptr); | |
586 | assert(end == nullptr); | |
587 | *compaction_end = nullptr; | |
588 | ||
589 | int start_level = 0; | |
590 | for (; start_level < vstorage->num_levels() && | |
591 | vstorage->NumLevelFiles(start_level) == 0; | |
592 | start_level++) { | |
593 | } | |
594 | if (start_level == vstorage->num_levels()) { | |
595 | return nullptr; | |
596 | } | |
597 | ||
598 | if ((start_level == 0) && (!level0_compactions_in_progress_.empty())) { | |
599 | *manual_conflict = true; | |
600 | // Only one level 0 compaction allowed | |
601 | return nullptr; | |
602 | } | |
603 | ||
604 | std::vector<CompactionInputFiles> inputs(vstorage->num_levels() - | |
605 | start_level); | |
606 | for (int level = start_level; level < vstorage->num_levels(); level++) { | |
607 | inputs[level - start_level].level = level; | |
608 | auto& files = inputs[level - start_level].files; | |
609 | for (FileMetaData* f : vstorage->LevelFiles(level)) { | |
610 | files.push_back(f); | |
611 | } | |
612 | if (AreFilesInCompaction(files)) { | |
613 | *manual_conflict = true; | |
614 | return nullptr; | |
615 | } | |
616 | } | |
617 | ||
618 | // 2 non-exclusive manual compactions could run at the same time producing | |
619 | // overlaping outputs in the same level. | |
620 | if (FilesRangeOverlapWithCompaction(inputs, output_level)) { | |
621 | // This compaction output could potentially conflict with the output | |
622 | // of a currently running compaction, we cannot run it. | |
623 | *manual_conflict = true; | |
624 | return nullptr; | |
625 | } | |
626 | ||
627 | Compaction* c = new Compaction( | |
20effc67 TL |
628 | vstorage, ioptions_, mutable_cf_options, mutable_db_options, |
629 | std::move(inputs), output_level, | |
11fdf7f2 TL |
630 | MaxFileSizeForLevel(mutable_cf_options, output_level, |
631 | ioptions_.compaction_style), | |
f67539c2 TL |
632 | /* max_compaction_bytes */ LLONG_MAX, |
633 | compact_range_options.target_path_id, | |
7c673cae FG |
634 | GetCompressionType(ioptions_, vstorage, mutable_cf_options, |
635 | output_level, 1), | |
20effc67 | 636 | GetCompressionOptions(mutable_cf_options, vstorage, output_level), |
f67539c2 TL |
637 | compact_range_options.max_subcompactions, /* grandparents */ {}, |
638 | /* is manual */ true); | |
7c673cae | 639 | RegisterCompaction(c); |
20effc67 | 640 | vstorage->ComputeCompactionScore(ioptions_, mutable_cf_options); |
7c673cae FG |
641 | return c; |
642 | } | |
643 | ||
644 | CompactionInputFiles inputs; | |
645 | inputs.level = input_level; | |
646 | bool covering_the_whole_range = true; | |
647 | ||
648 | // All files are 'overlapping' in universal style compaction. | |
649 | // We have to compact the entire range in one shot. | |
650 | if (ioptions_.compaction_style == kCompactionStyleUniversal) { | |
651 | begin = nullptr; | |
652 | end = nullptr; | |
653 | } | |
654 | ||
655 | vstorage->GetOverlappingInputs(input_level, begin, end, &inputs.files); | |
656 | if (inputs.empty()) { | |
657 | return nullptr; | |
658 | } | |
659 | ||
660 | if ((input_level == 0) && (!level0_compactions_in_progress_.empty())) { | |
661 | // Only one level 0 compaction allowed | |
662 | TEST_SYNC_POINT("CompactionPicker::CompactRange:Conflict"); | |
663 | *manual_conflict = true; | |
664 | return nullptr; | |
665 | } | |
666 | ||
667 | // Avoid compacting too much in one shot in case the range is large. | |
668 | // But we cannot do this for level-0 since level-0 files can overlap | |
669 | // and we must not pick one file and drop another older file if the | |
670 | // two files overlap. | |
671 | if (input_level > 0) { | |
672 | const uint64_t limit = mutable_cf_options.max_compaction_bytes; | |
673 | uint64_t total = 0; | |
674 | for (size_t i = 0; i + 1 < inputs.size(); ++i) { | |
675 | uint64_t s = inputs[i]->compensated_file_size; | |
676 | total += s; | |
677 | if (total >= limit) { | |
7c673cae FG |
678 | covering_the_whole_range = false; |
679 | inputs.files.resize(i + 1); | |
680 | break; | |
681 | } | |
682 | } | |
683 | } | |
f67539c2 TL |
684 | assert(compact_range_options.target_path_id < |
685 | static_cast<uint32_t>(ioptions_.cf_paths.size())); | |
686 | ||
687 | // for BOTTOM LEVEL compaction only, use max_file_num_to_ignore to filter out | |
688 | // files that are created during the current compaction. | |
689 | if (compact_range_options.bottommost_level_compaction == | |
690 | BottommostLevelCompaction::kForceOptimized && | |
691 | max_file_num_to_ignore != port::kMaxUint64) { | |
692 | assert(input_level == output_level); | |
693 | // inputs_shrunk holds a continuous subset of input files which were all | |
694 | // created before the current manual compaction | |
695 | std::vector<FileMetaData*> inputs_shrunk; | |
696 | size_t skip_input_index = inputs.size(); | |
697 | for (size_t i = 0; i < inputs.size(); ++i) { | |
698 | if (inputs[i]->fd.GetNumber() < max_file_num_to_ignore) { | |
699 | inputs_shrunk.push_back(inputs[i]); | |
700 | } else if (!inputs_shrunk.empty()) { | |
701 | // inputs[i] was created during the current manual compaction and | |
702 | // need to be skipped | |
703 | skip_input_index = i; | |
704 | break; | |
705 | } | |
706 | } | |
707 | if (inputs_shrunk.empty()) { | |
708 | return nullptr; | |
709 | } | |
710 | if (inputs.size() != inputs_shrunk.size()) { | |
711 | inputs.files.swap(inputs_shrunk); | |
712 | } | |
713 | // set covering_the_whole_range to false if there is any file that need to | |
714 | // be compacted in the range of inputs[skip_input_index+1, inputs.size()) | |
715 | for (size_t i = skip_input_index + 1; i < inputs.size(); ++i) { | |
716 | if (inputs[i]->fd.GetNumber() < max_file_num_to_ignore) { | |
717 | covering_the_whole_range = false; | |
718 | } | |
719 | } | |
720 | } | |
7c673cae | 721 | |
11fdf7f2 TL |
722 | InternalKey key_storage; |
723 | InternalKey* next_smallest = &key_storage; | |
724 | if (ExpandInputsToCleanCut(cf_name, vstorage, &inputs, &next_smallest) == | |
725 | false) { | |
7c673cae FG |
726 | // manual compaction is now multi-threaded, so it can |
727 | // happen that ExpandWhileOverlapping fails | |
728 | // we handle it higher in RunManualCompaction | |
729 | *manual_conflict = true; | |
730 | return nullptr; | |
731 | } | |
732 | ||
11fdf7f2 | 733 | if (covering_the_whole_range || !next_smallest) { |
7c673cae | 734 | *compaction_end = nullptr; |
11fdf7f2 TL |
735 | } else { |
736 | **compaction_end = *next_smallest; | |
7c673cae FG |
737 | } |
738 | ||
739 | CompactionInputFiles output_level_inputs; | |
740 | if (output_level == ColumnFamilyData::kCompactToBaseLevel) { | |
741 | assert(input_level == 0); | |
742 | output_level = vstorage->base_level(); | |
743 | assert(output_level > 0); | |
744 | } | |
745 | output_level_inputs.level = output_level; | |
746 | if (input_level != output_level) { | |
747 | int parent_index = -1; | |
748 | if (!SetupOtherInputs(cf_name, mutable_cf_options, vstorage, &inputs, | |
749 | &output_level_inputs, &parent_index, -1)) { | |
750 | // manual compaction is now multi-threaded, so it can | |
751 | // happen that SetupOtherInputs fails | |
752 | // we handle it higher in RunManualCompaction | |
753 | *manual_conflict = true; | |
754 | return nullptr; | |
755 | } | |
756 | } | |
757 | ||
758 | std::vector<CompactionInputFiles> compaction_inputs({inputs}); | |
759 | if (!output_level_inputs.empty()) { | |
760 | compaction_inputs.push_back(output_level_inputs); | |
761 | } | |
762 | for (size_t i = 0; i < compaction_inputs.size(); i++) { | |
763 | if (AreFilesInCompaction(compaction_inputs[i].files)) { | |
764 | *manual_conflict = true; | |
765 | return nullptr; | |
766 | } | |
767 | } | |
768 | ||
769 | // 2 non-exclusive manual compactions could run at the same time producing | |
770 | // overlaping outputs in the same level. | |
771 | if (FilesRangeOverlapWithCompaction(compaction_inputs, output_level)) { | |
772 | // This compaction output could potentially conflict with the output | |
773 | // of a currently running compaction, we cannot run it. | |
774 | *manual_conflict = true; | |
775 | return nullptr; | |
776 | } | |
777 | ||
778 | std::vector<FileMetaData*> grandparents; | |
779 | GetGrandparents(vstorage, inputs, output_level_inputs, &grandparents); | |
780 | Compaction* compaction = new Compaction( | |
20effc67 TL |
781 | vstorage, ioptions_, mutable_cf_options, mutable_db_options, |
782 | std::move(compaction_inputs), output_level, | |
11fdf7f2 TL |
783 | MaxFileSizeForLevel(mutable_cf_options, output_level, |
784 | ioptions_.compaction_style, vstorage->base_level(), | |
785 | ioptions_.level_compaction_dynamic_level_bytes), | |
f67539c2 TL |
786 | mutable_cf_options.max_compaction_bytes, |
787 | compact_range_options.target_path_id, | |
7c673cae FG |
788 | GetCompressionType(ioptions_, vstorage, mutable_cf_options, output_level, |
789 | vstorage->base_level()), | |
20effc67 | 790 | GetCompressionOptions(mutable_cf_options, vstorage, output_level), |
f67539c2 | 791 | compact_range_options.max_subcompactions, std::move(grandparents), |
11fdf7f2 | 792 | /* is manual compaction */ true); |
7c673cae FG |
793 | |
794 | TEST_SYNC_POINT_CALLBACK("CompactionPicker::CompactRange:Return", compaction); | |
795 | RegisterCompaction(compaction); | |
796 | ||
797 | // Creating a compaction influences the compaction score because the score | |
798 | // takes running compactions into account (by skipping files that are already | |
799 | // being compacted). Since we just changed compaction score, we recalculate it | |
800 | // here | |
801 | vstorage->ComputeCompactionScore(ioptions_, mutable_cf_options); | |
802 | ||
803 | return compaction; | |
804 | } | |
805 | ||
806 | #ifndef ROCKSDB_LITE | |
807 | namespace { | |
808 | // Test whether two files have overlapping key-ranges. | |
809 | bool HaveOverlappingKeyRanges(const Comparator* c, const SstFileMetaData& a, | |
810 | const SstFileMetaData& b) { | |
811 | if (c->Compare(a.smallestkey, b.smallestkey) >= 0) { | |
812 | if (c->Compare(a.smallestkey, b.largestkey) <= 0) { | |
813 | // b.smallestkey <= a.smallestkey <= b.largestkey | |
814 | return true; | |
815 | } | |
816 | } else if (c->Compare(a.largestkey, b.smallestkey) >= 0) { | |
817 | // a.smallestkey < b.smallestkey <= a.largestkey | |
818 | return true; | |
819 | } | |
820 | if (c->Compare(a.largestkey, b.largestkey) <= 0) { | |
821 | if (c->Compare(a.largestkey, b.smallestkey) >= 0) { | |
822 | // b.smallestkey <= a.largestkey <= b.largestkey | |
823 | return true; | |
824 | } | |
825 | } else if (c->Compare(a.smallestkey, b.largestkey) <= 0) { | |
826 | // a.smallestkey <= b.largestkey < a.largestkey | |
827 | return true; | |
828 | } | |
829 | return false; | |
830 | } | |
831 | } // namespace | |
832 | ||
833 | Status CompactionPicker::SanitizeCompactionInputFilesForAllLevels( | |
834 | std::unordered_set<uint64_t>* input_files, | |
835 | const ColumnFamilyMetaData& cf_meta, const int output_level) const { | |
836 | auto& levels = cf_meta.levels; | |
837 | auto comparator = icmp_->user_comparator(); | |
838 | ||
7c673cae FG |
839 | // TODO(yhchiang): add is_adjustable to CompactionOptions |
840 | ||
841 | // the smallest and largest key of the current compaction input | |
842 | std::string smallestkey; | |
843 | std::string largestkey; | |
844 | // a flag for initializing smallest and largest key | |
845 | bool is_first = false; | |
846 | const int kNotFound = -1; | |
847 | ||
848 | // For each level, it does the following things: | |
849 | // 1. Find the first and the last compaction input files | |
850 | // in the current level. | |
851 | // 2. Include all files between the first and the last | |
852 | // compaction input files. | |
853 | // 3. Update the compaction key-range. | |
854 | // 4. For all remaining levels, include files that have | |
855 | // overlapping key-range with the compaction key-range. | |
856 | for (int l = 0; l <= output_level; ++l) { | |
857 | auto& current_files = levels[l].files; | |
858 | int first_included = static_cast<int>(current_files.size()); | |
859 | int last_included = kNotFound; | |
860 | ||
861 | // identify the first and the last compaction input files | |
862 | // in the current level. | |
863 | for (size_t f = 0; f < current_files.size(); ++f) { | |
864 | if (input_files->find(TableFileNameToNumber(current_files[f].name)) != | |
865 | input_files->end()) { | |
866 | first_included = std::min(first_included, static_cast<int>(f)); | |
867 | last_included = std::max(last_included, static_cast<int>(f)); | |
868 | if (is_first == false) { | |
869 | smallestkey = current_files[f].smallestkey; | |
870 | largestkey = current_files[f].largestkey; | |
871 | is_first = true; | |
872 | } | |
873 | } | |
874 | } | |
875 | if (last_included == kNotFound) { | |
876 | continue; | |
877 | } | |
878 | ||
879 | if (l != 0) { | |
880 | // expend the compaction input of the current level if it | |
881 | // has overlapping key-range with other non-compaction input | |
882 | // files in the same level. | |
883 | while (first_included > 0) { | |
884 | if (comparator->Compare(current_files[first_included - 1].largestkey, | |
885 | current_files[first_included].smallestkey) < | |
886 | 0) { | |
887 | break; | |
888 | } | |
889 | first_included--; | |
890 | } | |
891 | ||
892 | while (last_included < static_cast<int>(current_files.size()) - 1) { | |
893 | if (comparator->Compare(current_files[last_included + 1].smallestkey, | |
894 | current_files[last_included].largestkey) > 0) { | |
895 | break; | |
896 | } | |
897 | last_included++; | |
898 | } | |
11fdf7f2 TL |
899 | } else if (output_level > 0) { |
900 | last_included = static_cast<int>(current_files.size() - 1); | |
7c673cae FG |
901 | } |
902 | ||
903 | // include all files between the first and the last compaction input files. | |
904 | for (int f = first_included; f <= last_included; ++f) { | |
905 | if (current_files[f].being_compacted) { | |
906 | return Status::Aborted("Necessary compaction input file " + | |
907 | current_files[f].name + | |
908 | " is currently being compacted."); | |
909 | } | |
910 | input_files->insert(TableFileNameToNumber(current_files[f].name)); | |
911 | } | |
912 | ||
913 | // update smallest and largest key | |
914 | if (l == 0) { | |
915 | for (int f = first_included; f <= last_included; ++f) { | |
916 | if (comparator->Compare(smallestkey, current_files[f].smallestkey) > | |
917 | 0) { | |
918 | smallestkey = current_files[f].smallestkey; | |
919 | } | |
920 | if (comparator->Compare(largestkey, current_files[f].largestkey) < 0) { | |
921 | largestkey = current_files[f].largestkey; | |
922 | } | |
923 | } | |
924 | } else { | |
925 | if (comparator->Compare(smallestkey, | |
926 | current_files[first_included].smallestkey) > 0) { | |
927 | smallestkey = current_files[first_included].smallestkey; | |
928 | } | |
929 | if (comparator->Compare(largestkey, | |
930 | current_files[last_included].largestkey) < 0) { | |
931 | largestkey = current_files[last_included].largestkey; | |
932 | } | |
933 | } | |
934 | ||
935 | SstFileMetaData aggregated_file_meta; | |
936 | aggregated_file_meta.smallestkey = smallestkey; | |
937 | aggregated_file_meta.largestkey = largestkey; | |
938 | ||
939 | // For all lower levels, include all overlapping files. | |
940 | // We need to add overlapping files from the current level too because even | |
941 | // if there no input_files in level l, we would still need to add files | |
942 | // which overlap with the range containing the input_files in levels 0 to l | |
943 | // Level 0 doesn't need to be handled this way because files are sorted by | |
944 | // time and not by key | |
945 | for (int m = std::max(l, 1); m <= output_level; ++m) { | |
946 | for (auto& next_lv_file : levels[m].files) { | |
947 | if (HaveOverlappingKeyRanges(comparator, aggregated_file_meta, | |
948 | next_lv_file)) { | |
949 | if (next_lv_file.being_compacted) { | |
950 | return Status::Aborted( | |
951 | "File " + next_lv_file.name + | |
952 | " that has overlapping key range with one of the compaction " | |
953 | " input file is currently being compacted."); | |
954 | } | |
955 | input_files->insert(TableFileNameToNumber(next_lv_file.name)); | |
956 | } | |
957 | } | |
958 | } | |
959 | } | |
11fdf7f2 TL |
960 | if (RangeOverlapWithCompaction(smallestkey, largestkey, output_level)) { |
961 | return Status::Aborted( | |
962 | "A running compaction is writing to the same output level in an " | |
963 | "overlapping key range"); | |
964 | } | |
7c673cae FG |
965 | return Status::OK(); |
966 | } | |
967 | ||
968 | Status CompactionPicker::SanitizeCompactionInputFiles( | |
969 | std::unordered_set<uint64_t>* input_files, | |
970 | const ColumnFamilyMetaData& cf_meta, const int output_level) const { | |
971 | assert(static_cast<int>(cf_meta.levels.size()) - 1 == | |
972 | cf_meta.levels[cf_meta.levels.size() - 1].level); | |
973 | if (output_level >= static_cast<int>(cf_meta.levels.size())) { | |
974 | return Status::InvalidArgument( | |
975 | "Output level for column family " + cf_meta.name + | |
976 | " must between [0, " + | |
977 | ToString(cf_meta.levels[cf_meta.levels.size() - 1].level) + "]."); | |
978 | } | |
979 | ||
980 | if (output_level > MaxOutputLevel()) { | |
981 | return Status::InvalidArgument( | |
982 | "Exceed the maximum output level defined by " | |
983 | "the current compaction algorithm --- " + | |
984 | ToString(MaxOutputLevel())); | |
985 | } | |
986 | ||
987 | if (output_level < 0) { | |
988 | return Status::InvalidArgument("Output level cannot be negative."); | |
989 | } | |
990 | ||
991 | if (input_files->size() == 0) { | |
992 | return Status::InvalidArgument( | |
993 | "A compaction must contain at least one file."); | |
994 | } | |
995 | ||
996 | Status s = SanitizeCompactionInputFilesForAllLevels(input_files, cf_meta, | |
997 | output_level); | |
998 | ||
999 | if (!s.ok()) { | |
1000 | return s; | |
1001 | } | |
1002 | ||
1003 | // for all input files, check whether the file number matches | |
1004 | // any currently-existing files. | |
1005 | for (auto file_num : *input_files) { | |
1006 | bool found = false; | |
494da23a TL |
1007 | for (const auto& level_meta : cf_meta.levels) { |
1008 | for (const auto& file_meta : level_meta.files) { | |
7c673cae FG |
1009 | if (file_num == TableFileNameToNumber(file_meta.name)) { |
1010 | if (file_meta.being_compacted) { | |
1011 | return Status::Aborted("Specified compaction input file " + | |
1012 | MakeTableFileName("", file_num) + | |
1013 | " is already being compacted."); | |
1014 | } | |
1015 | found = true; | |
1016 | break; | |
1017 | } | |
1018 | } | |
1019 | if (found) { | |
1020 | break; | |
1021 | } | |
1022 | } | |
1023 | if (!found) { | |
1024 | return Status::InvalidArgument( | |
1025 | "Specified compaction input file " + MakeTableFileName("", file_num) + | |
1026 | " does not exist in column family " + cf_meta.name + "."); | |
1027 | } | |
1028 | } | |
1029 | ||
1030 | return Status::OK(); | |
1031 | } | |
1032 | #endif // !ROCKSDB_LITE | |
1033 | ||
1034 | void CompactionPicker::RegisterCompaction(Compaction* c) { | |
1035 | if (c == nullptr) { | |
1036 | return; | |
1037 | } | |
1038 | assert(ioptions_.compaction_style != kCompactionStyleLevel || | |
1039 | c->output_level() == 0 || | |
1040 | !FilesRangeOverlapWithCompaction(*c->inputs(), c->output_level())); | |
1041 | if (c->start_level() == 0 || | |
1042 | ioptions_.compaction_style == kCompactionStyleUniversal) { | |
1043 | level0_compactions_in_progress_.insert(c); | |
1044 | } | |
1045 | compactions_in_progress_.insert(c); | |
20effc67 TL |
1046 | TEST_SYNC_POINT_CALLBACK("CompactionPicker::RegisterCompaction:Registered", |
1047 | c); | |
7c673cae FG |
1048 | } |
1049 | ||
1050 | void CompactionPicker::UnregisterCompaction(Compaction* c) { | |
1051 | if (c == nullptr) { | |
1052 | return; | |
1053 | } | |
1054 | if (c->start_level() == 0 || | |
1055 | ioptions_.compaction_style == kCompactionStyleUniversal) { | |
1056 | level0_compactions_in_progress_.erase(c); | |
1057 | } | |
1058 | compactions_in_progress_.erase(c); | |
1059 | } | |
1060 | ||
11fdf7f2 TL |
1061 | void CompactionPicker::PickFilesMarkedForCompaction( |
1062 | const std::string& cf_name, VersionStorageInfo* vstorage, int* start_level, | |
1063 | int* output_level, CompactionInputFiles* start_level_inputs) { | |
1064 | if (vstorage->FilesMarkedForCompaction().empty()) { | |
1065 | return; | |
1066 | } | |
1067 | ||
1068 | auto continuation = [&, cf_name](std::pair<int, FileMetaData*> level_file) { | |
1069 | // If it's being compacted it has nothing to do here. | |
1070 | // If this assert() fails that means that some function marked some | |
1071 | // files as being_compacted, but didn't call ComputeCompactionScore() | |
1072 | assert(!level_file.second->being_compacted); | |
1073 | *start_level = level_file.first; | |
1074 | *output_level = | |
1075 | (*start_level == 0) ? vstorage->base_level() : *start_level + 1; | |
1076 | ||
1077 | if (*start_level == 0 && !level0_compactions_in_progress()->empty()) { | |
1078 | return false; | |
1079 | } | |
1080 | ||
1081 | start_level_inputs->files = {level_file.second}; | |
1082 | start_level_inputs->level = *start_level; | |
1083 | return ExpandInputsToCleanCut(cf_name, vstorage, start_level_inputs); | |
1084 | }; | |
1085 | ||
1086 | // take a chance on a random file first | |
1087 | Random64 rnd(/* seed */ reinterpret_cast<uint64_t>(vstorage)); | |
1088 | size_t random_file_index = static_cast<size_t>(rnd.Uniform( | |
1089 | static_cast<uint64_t>(vstorage->FilesMarkedForCompaction().size()))); | |
20effc67 TL |
1090 | TEST_SYNC_POINT_CALLBACK("CompactionPicker::PickFilesMarkedForCompaction", |
1091 | &random_file_index); | |
11fdf7f2 TL |
1092 | |
1093 | if (continuation(vstorage->FilesMarkedForCompaction()[random_file_index])) { | |
1094 | // found the compaction! | |
1095 | return; | |
1096 | } | |
1097 | ||
1098 | for (auto& level_file : vstorage->FilesMarkedForCompaction()) { | |
1099 | if (continuation(level_file)) { | |
1100 | // found the compaction! | |
1101 | return; | |
1102 | } | |
1103 | } | |
1104 | start_level_inputs->files.clear(); | |
1105 | } | |
1106 | ||
1107 | bool CompactionPicker::GetOverlappingL0Files( | |
1108 | VersionStorageInfo* vstorage, CompactionInputFiles* start_level_inputs, | |
1109 | int output_level, int* parent_index) { | |
1110 | // Two level 0 compaction won't run at the same time, so don't need to worry | |
1111 | // about files on level 0 being compacted. | |
1112 | assert(level0_compactions_in_progress()->empty()); | |
1113 | InternalKey smallest, largest; | |
1114 | GetRange(*start_level_inputs, &smallest, &largest); | |
1115 | // Note that the next call will discard the file we placed in | |
1116 | // c->inputs_[0] earlier and replace it with an overlapping set | |
1117 | // which will include the picked file. | |
1118 | start_level_inputs->files.clear(); | |
1119 | vstorage->GetOverlappingInputs(0, &smallest, &largest, | |
1120 | &(start_level_inputs->files)); | |
1121 | ||
1122 | // If we include more L0 files in the same compaction run it can | |
1123 | // cause the 'smallest' and 'largest' key to get extended to a | |
1124 | // larger range. So, re-invoke GetRange to get the new key range | |
1125 | GetRange(*start_level_inputs, &smallest, &largest); | |
1126 | if (IsRangeInCompaction(vstorage, &smallest, &largest, output_level, | |
1127 | parent_index)) { | |
1128 | return false; | |
1129 | } | |
1130 | assert(!start_level_inputs->files.empty()); | |
1131 | ||
1132 | return true; | |
1133 | } | |
1134 | ||
f67539c2 | 1135 | } // namespace ROCKSDB_NAMESPACE |