2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2014-01-16 01:22:34 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2020-07-24 22:43:14 +02:00
|
|
|
#include "db/compaction/compaction.h"
|
|
|
|
|
2019-06-06 22:52:39 +02:00
|
|
|
#include <cinttypes>
|
2014-05-14 21:13:50 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2014-02-01 01:45:20 +01:00
|
|
|
#include "db/column_family.h"
|
2016-05-17 22:11:56 +02:00
|
|
|
#include "rocksdb/compaction_filter.h"
|
2020-07-24 22:43:14 +02:00
|
|
|
#include "rocksdb/sst_partitioner.h"
|
2019-05-30 20:21:38 +02:00
|
|
|
#include "test_util/sync_point.h"
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "util/string_util.h"
|
2014-01-16 01:22:34 +01:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-01-16 01:22:34 +01:00
|
|
|
|
2018-10-10 00:15:27 +02:00
|
|
|
const uint64_t kRangeTombstoneSentinel =
|
|
|
|
PackSequenceAndType(kMaxSequenceNumber, kTypeRangeDeletion);
|
|
|
|
|
|
|
|
int sstableKeyCompare(const Comparator* user_cmp, const InternalKey& a,
|
|
|
|
const InternalKey& b) {
|
2020-04-11 01:03:33 +02:00
|
|
|
auto c = user_cmp->CompareWithoutTimestamp(a.user_key(), b.user_key());
|
2018-10-10 00:15:27 +02:00
|
|
|
if (c != 0) {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
auto a_footer = ExtractInternalKeyFooter(a.Encode());
|
|
|
|
auto b_footer = ExtractInternalKeyFooter(b.Encode());
|
|
|
|
if (a_footer == kRangeTombstoneSentinel) {
|
|
|
|
if (b_footer != kRangeTombstoneSentinel) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (b_footer == kRangeTombstoneSentinel) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sstableKeyCompare(const Comparator* user_cmp, const InternalKey* a,
|
|
|
|
const InternalKey& b) {
|
|
|
|
if (a == nullptr) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return sstableKeyCompare(user_cmp, *a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sstableKeyCompare(const Comparator* user_cmp, const InternalKey& a,
|
|
|
|
const InternalKey* b) {
|
|
|
|
if (b == nullptr) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return sstableKeyCompare(user_cmp, a, *b);
|
|
|
|
}
|
|
|
|
|
2014-08-07 19:05:04 +02:00
|
|
|
uint64_t TotalFileSize(const std::vector<FileMetaData*>& files) {
|
2014-01-16 01:22:34 +01:00
|
|
|
uint64_t sum = 0;
|
|
|
|
for (size_t i = 0; i < files.size() && files[i]; i++) {
|
2014-06-14 00:54:19 +02:00
|
|
|
sum += files[i]->fd.GetFileSize();
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2014-11-06 20:14:28 +01:00
|
|
|
void Compaction::SetInputVersion(Version* _input_version) {
|
|
|
|
input_version_ = _input_version;
|
2014-10-27 23:49:46 +01:00
|
|
|
cfd_ = input_version_->cfd();
|
|
|
|
|
|
|
|
cfd_->Ref();
|
|
|
|
input_version_->Ref();
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
edit_.SetColumnFamily(cfd_->GetID());
|
2014-10-27 23:49:46 +01:00
|
|
|
}
|
|
|
|
|
2015-09-09 01:18:14 +02:00
|
|
|
void Compaction::GetBoundaryKeys(
|
|
|
|
VersionStorageInfo* vstorage,
|
|
|
|
const std::vector<CompactionInputFiles>& inputs, Slice* smallest_user_key,
|
|
|
|
Slice* largest_user_key) {
|
|
|
|
bool initialized = false;
|
|
|
|
const Comparator* ucmp = vstorage->InternalComparator()->user_comparator();
|
2015-12-16 00:26:20 +01:00
|
|
|
for (size_t i = 0; i < inputs.size(); ++i) {
|
2015-09-09 01:18:14 +02:00
|
|
|
if (inputs[i].files.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-06 02:40:18 +02:00
|
|
|
if (inputs[i].level == 0) {
|
|
|
|
// we need to consider all files on level 0
|
|
|
|
for (const auto* f : inputs[i].files) {
|
|
|
|
const Slice& start_user_key = f->smallest.user_key();
|
|
|
|
if (!initialized ||
|
|
|
|
ucmp->Compare(start_user_key, *smallest_user_key) < 0) {
|
|
|
|
*smallest_user_key = start_user_key;
|
|
|
|
}
|
|
|
|
const Slice& end_user_key = f->largest.user_key();
|
|
|
|
if (!initialized ||
|
|
|
|
ucmp->Compare(end_user_key, *largest_user_key) > 0) {
|
|
|
|
*largest_user_key = end_user_key;
|
|
|
|
}
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we only need to consider the first and last file
|
|
|
|
const Slice& start_user_key = inputs[i].files[0]->smallest.user_key();
|
|
|
|
if (!initialized ||
|
|
|
|
ucmp->Compare(start_user_key, *smallest_user_key) < 0) {
|
|
|
|
*smallest_user_key = start_user_key;
|
|
|
|
}
|
|
|
|
const Slice& end_user_key = inputs[i].files.back()->largest.user_key();
|
|
|
|
if (!initialized || ucmp->Compare(end_user_key, *largest_user_key) > 0) {
|
|
|
|
*largest_user_key = end_user_key;
|
|
|
|
}
|
|
|
|
initialized = true;
|
2015-09-09 01:18:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 00:15:27 +02:00
|
|
|
std::vector<CompactionInputFiles> Compaction::PopulateWithAtomicBoundaries(
|
|
|
|
VersionStorageInfo* vstorage, std::vector<CompactionInputFiles> inputs) {
|
|
|
|
const Comparator* ucmp = vstorage->InternalComparator()->user_comparator();
|
|
|
|
for (size_t i = 0; i < inputs.size(); i++) {
|
|
|
|
if (inputs[i].level == 0 || inputs[i].files.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
inputs[i].atomic_compaction_unit_boundaries.reserve(inputs[i].files.size());
|
|
|
|
AtomicCompactionUnitBoundary cur_boundary;
|
|
|
|
size_t first_atomic_idx = 0;
|
|
|
|
auto add_unit_boundary = [&](size_t to) {
|
|
|
|
if (first_atomic_idx == to) return;
|
|
|
|
for (size_t k = first_atomic_idx; k < to; k++) {
|
|
|
|
inputs[i].atomic_compaction_unit_boundaries.push_back(cur_boundary);
|
|
|
|
}
|
|
|
|
first_atomic_idx = to;
|
|
|
|
};
|
|
|
|
for (size_t j = 0; j < inputs[i].files.size(); j++) {
|
|
|
|
const auto* f = inputs[i].files[j];
|
|
|
|
if (j == 0) {
|
|
|
|
// First file in a level.
|
|
|
|
cur_boundary.smallest = &f->smallest;
|
|
|
|
cur_boundary.largest = &f->largest;
|
|
|
|
} else if (sstableKeyCompare(ucmp, *cur_boundary.largest, f->smallest) ==
|
|
|
|
0) {
|
|
|
|
// SSTs overlap but the end key of the previous file was not
|
|
|
|
// artificially extended by a range tombstone. Extend the current
|
|
|
|
// boundary.
|
|
|
|
cur_boundary.largest = &f->largest;
|
|
|
|
} else {
|
|
|
|
// Atomic compaction unit has ended.
|
|
|
|
add_unit_boundary(j);
|
|
|
|
cur_boundary.smallest = &f->smallest;
|
|
|
|
cur_boundary.largest = &f->largest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_unit_boundary(inputs[i].files.size());
|
|
|
|
assert(inputs[i].files.size() ==
|
|
|
|
inputs[i].atomic_compaction_unit_boundaries.size());
|
|
|
|
}
|
|
|
|
return inputs;
|
|
|
|
}
|
|
|
|
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
// helper function to determine if compaction is creating files at the
|
|
|
|
// bottommost level
|
|
|
|
bool Compaction::IsBottommostLevel(
|
|
|
|
int output_level, VersionStorageInfo* vstorage,
|
|
|
|
const std::vector<CompactionInputFiles>& inputs) {
|
2017-10-26 01:24:29 +02:00
|
|
|
int output_l0_idx;
|
|
|
|
if (output_level == 0) {
|
|
|
|
output_l0_idx = 0;
|
|
|
|
for (const auto* file : vstorage->LevelFiles(0)) {
|
|
|
|
if (inputs[0].files.back() == file) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++output_l0_idx;
|
|
|
|
}
|
|
|
|
assert(static_cast<size_t>(output_l0_idx) < vstorage->LevelFiles(0).size());
|
|
|
|
} else {
|
|
|
|
output_l0_idx = -1;
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
}
|
2015-09-09 01:18:14 +02:00
|
|
|
Slice smallest_key, largest_key;
|
|
|
|
GetBoundaryKeys(vstorage, inputs, &smallest_key, &largest_key);
|
2017-10-26 01:24:29 +02:00
|
|
|
return !vstorage->RangeMightExistAfterSortedRun(smallest_key, largest_key,
|
|
|
|
output_level, output_l0_idx);
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
}
|
|
|
|
|
2015-09-09 01:18:14 +02:00
|
|
|
// test function to validate the functionality of IsBottommostLevel()
|
|
|
|
// function -- determines if compaction with inputs and storage is bottommost
|
|
|
|
bool Compaction::TEST_IsBottommostLevel(
|
|
|
|
int output_level, VersionStorageInfo* vstorage,
|
|
|
|
const std::vector<CompactionInputFiles>& inputs) {
|
|
|
|
return IsBottommostLevel(output_level, vstorage, inputs);
|
|
|
|
}
|
|
|
|
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
bool Compaction::IsFullCompaction(
|
|
|
|
VersionStorageInfo* vstorage,
|
|
|
|
const std::vector<CompactionInputFiles>& inputs) {
|
2015-11-15 19:49:14 +01:00
|
|
|
size_t num_files_in_compaction = 0;
|
|
|
|
size_t total_num_files = 0;
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
for (int l = 0; l < vstorage->num_levels(); l++) {
|
|
|
|
total_num_files += vstorage->NumLevelFiles(l);
|
2014-07-17 03:12:17 +02:00
|
|
|
}
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
for (size_t i = 0; i < inputs.size(); i++) {
|
|
|
|
num_files_in_compaction += inputs[i].size();
|
|
|
|
}
|
|
|
|
return num_files_in_compaction == total_num_files;
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
|
|
|
|
2021-05-05 22:59:21 +02:00
|
|
|
Compaction::Compaction(
|
|
|
|
VersionStorageInfo* vstorage, const ImmutableOptions& _immutable_options,
|
|
|
|
const MutableCFOptions& _mutable_cf_options,
|
|
|
|
const MutableDBOptions& _mutable_db_options,
|
|
|
|
std::vector<CompactionInputFiles> _inputs, int _output_level,
|
|
|
|
uint64_t _target_file_size, uint64_t _max_compaction_bytes,
|
|
|
|
uint32_t _output_path_id, CompressionType _compression,
|
|
|
|
CompressionOptions _compression_opts, uint32_t _max_subcompactions,
|
|
|
|
std::vector<FileMetaData*> _grandparents, bool _manual_compaction,
|
|
|
|
double _score, bool _deletion_compaction,
|
|
|
|
CompactionReason _compaction_reason)
|
2016-12-07 20:42:49 +01:00
|
|
|
: input_vstorage_(vstorage),
|
|
|
|
start_level_(_inputs[0].level),
|
2014-11-08 00:22:10 +01:00
|
|
|
output_level_(_output_level),
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
max_output_file_size_(_target_file_size),
|
2016-06-17 01:02:52 +02:00
|
|
|
max_compaction_bytes_(_max_compaction_bytes),
|
2018-04-27 20:48:21 +02:00
|
|
|
max_subcompactions_(_max_subcompactions),
|
2021-05-05 22:59:21 +02:00
|
|
|
immutable_options_(_immutable_options),
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
mutable_cf_options_(_mutable_cf_options),
|
2014-11-08 00:22:10 +01:00
|
|
|
input_version_(nullptr),
|
2014-11-08 00:08:12 +01:00
|
|
|
number_levels_(vstorage->num_levels()),
|
CompactFiles, EventListener and GetDatabaseMetaData
Summary:
This diff adds three sets of APIs to RocksDB.
= GetColumnFamilyMetaData =
* This APIs allow users to obtain the current state of a RocksDB instance on one column family.
* See GetColumnFamilyMetaData in include/rocksdb/db.h
= EventListener =
* A virtual class that allows users to implement a set of
call-back functions which will be called when specific
events of a RocksDB instance happens.
* To register EventListener, simply insert an EventListener to ColumnFamilyOptions::listeners
= CompactFiles =
* CompactFiles API inputs a set of file numbers and an output level, and RocksDB
will try to compact those files into the specified level.
= Example =
* Example code can be found in example/compact_files_example.cc, which implements
a simple external compactor using EventListener, GetColumnFamilyMetaData, and
CompactFiles API.
Test Plan:
listener_test
compactor_test
example/compact_files_example
export ROCKSDB_TESTS=CompactFiles
db_test
export ROCKSDB_TESTS=MetaData
db_test
Reviewers: ljin, igor, rven, sdong
Reviewed By: sdong
Subscribers: MarkCallaghan, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D24705
2014-11-07 23:45:18 +01:00
|
|
|
cfd_(nullptr),
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
output_path_id_(_output_path_id),
|
|
|
|
output_compression_(_compression),
|
2018-06-28 02:34:07 +02:00
|
|
|
output_compression_opts_(_compression_opts),
|
2014-11-08 00:22:10 +01:00
|
|
|
deletion_compaction_(_deletion_compaction),
|
2018-10-10 00:15:27 +02:00
|
|
|
inputs_(PopulateWithAtomicBoundaries(vstorage, std::move(_inputs))),
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
grandparents_(std::move(_grandparents)),
|
|
|
|
score_(_score),
|
|
|
|
bottommost_level_(IsBottommostLevel(output_level_, vstorage, inputs_)),
|
|
|
|
is_full_compaction_(IsFullCompaction(vstorage, inputs_)),
|
2015-12-22 20:37:19 +01:00
|
|
|
is_manual_compaction_(_manual_compaction),
|
2017-07-31 23:22:37 +02:00
|
|
|
is_trivial_move_(false),
|
2015-12-22 20:37:19 +01:00
|
|
|
compaction_reason_(_compaction_reason) {
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
MarkFilesBeingCompacted(true);
|
2015-12-22 20:37:19 +01:00
|
|
|
if (is_manual_compaction_) {
|
|
|
|
compaction_reason_ = CompactionReason::kManualCompaction;
|
|
|
|
}
|
2018-04-27 20:48:21 +02:00
|
|
|
if (max_subcompactions_ == 0) {
|
2020-07-23 03:31:25 +02:00
|
|
|
max_subcompactions_ = _mutable_db_options.max_subcompactions;
|
2018-04-27 20:48:21 +02:00
|
|
|
}
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
for (size_t i = 1; i < inputs_.size(); ++i) {
|
|
|
|
assert(inputs_[i].level > inputs_[i - 1].level);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// setup input_levels_
|
|
|
|
{
|
|
|
|
input_levels_.resize(num_input_levels());
|
|
|
|
for (size_t which = 0; which < num_input_levels(); which++) {
|
|
|
|
DoGenerateLevelFilesBrief(&input_levels_[which], inputs_[which].files,
|
|
|
|
&arena_);
|
|
|
|
}
|
CompactFiles, EventListener and GetDatabaseMetaData
Summary:
This diff adds three sets of APIs to RocksDB.
= GetColumnFamilyMetaData =
* This APIs allow users to obtain the current state of a RocksDB instance on one column family.
* See GetColumnFamilyMetaData in include/rocksdb/db.h
= EventListener =
* A virtual class that allows users to implement a set of
call-back functions which will be called when specific
events of a RocksDB instance happens.
* To register EventListener, simply insert an EventListener to ColumnFamilyOptions::listeners
= CompactFiles =
* CompactFiles API inputs a set of file numbers and an output level, and RocksDB
will try to compact those files into the specified level.
= Example =
* Example code can be found in example/compact_files_example.cc, which implements
a simple external compactor using EventListener, GetColumnFamilyMetaData, and
CompactFiles API.
Test Plan:
listener_test
compactor_test
example/compact_files_example
export ROCKSDB_TESTS=CompactFiles
db_test
export ROCKSDB_TESTS=MetaData
db_test
Reviewers: ljin, igor, rven, sdong
Reviewed By: sdong
Subscribers: MarkCallaghan, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D24705
2014-11-07 23:45:18 +01:00
|
|
|
}
|
2015-12-10 02:28:46 +01:00
|
|
|
|
2016-06-22 02:57:59 +02:00
|
|
|
GetBoundaryKeys(vstorage, inputs_, &smallest_user_key_, &largest_user_key_);
|
CompactFiles, EventListener and GetDatabaseMetaData
Summary:
This diff adds three sets of APIs to RocksDB.
= GetColumnFamilyMetaData =
* This APIs allow users to obtain the current state of a RocksDB instance on one column family.
* See GetColumnFamilyMetaData in include/rocksdb/db.h
= EventListener =
* A virtual class that allows users to implement a set of
call-back functions which will be called when specific
events of a RocksDB instance happens.
* To register EventListener, simply insert an EventListener to ColumnFamilyOptions::listeners
= CompactFiles =
* CompactFiles API inputs a set of file numbers and an output level, and RocksDB
will try to compact those files into the specified level.
= Example =
* Example code can be found in example/compact_files_example.cc, which implements
a simple external compactor using EventListener, GetColumnFamilyMetaData, and
CompactFiles API.
Test Plan:
listener_test
compactor_test
example/compact_files_example
export ROCKSDB_TESTS=CompactFiles
db_test
export ROCKSDB_TESTS=MetaData
db_test
Reviewers: ljin, igor, rven, sdong
Reviewed By: sdong
Subscribers: MarkCallaghan, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D24705
2014-11-07 23:45:18 +01:00
|
|
|
}
|
|
|
|
|
2014-01-16 01:22:34 +01:00
|
|
|
Compaction::~Compaction() {
|
|
|
|
if (input_version_ != nullptr) {
|
|
|
|
input_version_->Unref();
|
|
|
|
}
|
2014-02-11 02:04:44 +01:00
|
|
|
if (cfd_ != nullptr) {
|
2019-12-13 04:02:51 +01:00
|
|
|
cfd_->UnrefAndTryDelete();
|
2014-02-11 02:04:44 +01:00
|
|
|
}
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
|
|
|
|
2015-04-02 20:06:30 +02:00
|
|
|
bool Compaction::InputCompressionMatchesOutput() const {
|
2016-12-07 20:42:49 +01:00
|
|
|
int base_level = input_vstorage_->base_level();
|
2021-05-05 22:59:21 +02:00
|
|
|
bool matches = (GetCompressionType(immutable_options_, input_vstorage_,
|
2016-12-07 20:42:49 +01:00
|
|
|
mutable_cf_options_, start_level_,
|
|
|
|
base_level) == output_compression_);
|
2015-04-02 20:06:30 +02:00
|
|
|
if (matches) {
|
|
|
|
TEST_SYNC_POINT("Compaction::InputCompressionMatchesOutput:Matches");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
TEST_SYNC_POINT("Compaction::InputCompressionMatchesOutput:DidntMatch");
|
2015-05-07 07:50:35 +02:00
|
|
|
return matches;
|
2015-04-02 20:06:30 +02:00
|
|
|
}
|
|
|
|
|
2014-01-16 01:22:34 +01:00
|
|
|
bool Compaction::IsTrivialMove() const {
|
|
|
|
// Avoid a move if there is lots of overlapping grandparent data.
|
|
|
|
// Otherwise, the move could create a parent file that will require
|
|
|
|
// a very expensive merge later on.
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
// If start_level_== output_level_, the purpose is to force compaction
|
Allowing L0 -> L1 trivial move on sorted data
Summary:
This diff updates the logic of how we do trivial move, now trivial move can run on any number of files in input level as long as they are not overlapping
The conditions for trivial move have been updated
Introduced conditions:
- Trivial move cannot happen if we have a compaction filter (except if the compaction is not manual)
- Input level files cannot be overlapping
Removed conditions:
- Trivial move only run when the compaction is not manual
- Input level should can contain only 1 file
More context on what tests failed because of Trivial move
```
DBTest.CompactionsGenerateMultipleFiles
This test is expecting compaction on a file in L0 to generate multiple files in L1, this test will fail with trivial move because we end up with one file in L1
```
```
DBTest.NoSpaceCompactRange
This test expect compaction to fail when we force environment to report running out of space, of course this is not valid in trivial move situation
because trivial move does not need any extra space, and did not check for that
```
```
DBTest.DropWrites
Similar to DBTest.NoSpaceCompactRange
```
```
DBTest.DeleteObsoleteFilesPendingOutputs
This test expect that a file in L2 is deleted after it's moved to L3, this is not valid with trivial move because although the file was moved it is now used by L3
```
```
CuckooTableDBTest.CompactionIntoMultipleFiles
Same as DBTest.CompactionsGenerateMultipleFiles
```
This diff is based on a work by @sdong https://reviews.facebook.net/D34149
Test Plan: make -j64 check
Reviewers: rven, sdong, igor
Reviewed By: igor
Subscribers: yhchiang, ott, march, dhruba, sdong
Differential Revision: https://reviews.facebook.net/D34797
2015-06-05 01:51:25 +02:00
|
|
|
// filter to be applied to that level, and thus cannot be a trivial move.
|
|
|
|
|
|
|
|
// Check if start level have files with overlapping ranges
|
2016-12-07 20:42:49 +01:00
|
|
|
if (start_level_ == 0 && input_vstorage_->level0_non_overlapping() == false) {
|
Allowing L0 -> L1 trivial move on sorted data
Summary:
This diff updates the logic of how we do trivial move, now trivial move can run on any number of files in input level as long as they are not overlapping
The conditions for trivial move have been updated
Introduced conditions:
- Trivial move cannot happen if we have a compaction filter (except if the compaction is not manual)
- Input level files cannot be overlapping
Removed conditions:
- Trivial move only run when the compaction is not manual
- Input level should can contain only 1 file
More context on what tests failed because of Trivial move
```
DBTest.CompactionsGenerateMultipleFiles
This test is expecting compaction on a file in L0 to generate multiple files in L1, this test will fail with trivial move because we end up with one file in L1
```
```
DBTest.NoSpaceCompactRange
This test expect compaction to fail when we force environment to report running out of space, of course this is not valid in trivial move situation
because trivial move does not need any extra space, and did not check for that
```
```
DBTest.DropWrites
Similar to DBTest.NoSpaceCompactRange
```
```
DBTest.DeleteObsoleteFilesPendingOutputs
This test expect that a file in L2 is deleted after it's moved to L3, this is not valid with trivial move because although the file was moved it is now used by L3
```
```
CuckooTableDBTest.CompactionIntoMultipleFiles
Same as DBTest.CompactionsGenerateMultipleFiles
```
This diff is based on a work by @sdong https://reviews.facebook.net/D34149
Test Plan: make -j64 check
Reviewers: rven, sdong, igor
Reviewed By: igor
Subscribers: yhchiang, ott, march, dhruba, sdong
Differential Revision: https://reviews.facebook.net/D34797
2015-06-05 01:51:25 +02:00
|
|
|
// We cannot move files from L0 to L1 if the files are overlapping
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_manual_compaction_ &&
|
2021-05-05 22:59:21 +02:00
|
|
|
(immutable_options_.compaction_filter != nullptr ||
|
|
|
|
immutable_options_.compaction_filter_factory != nullptr)) {
|
Allowing L0 -> L1 trivial move on sorted data
Summary:
This diff updates the logic of how we do trivial move, now trivial move can run on any number of files in input level as long as they are not overlapping
The conditions for trivial move have been updated
Introduced conditions:
- Trivial move cannot happen if we have a compaction filter (except if the compaction is not manual)
- Input level files cannot be overlapping
Removed conditions:
- Trivial move only run when the compaction is not manual
- Input level should can contain only 1 file
More context on what tests failed because of Trivial move
```
DBTest.CompactionsGenerateMultipleFiles
This test is expecting compaction on a file in L0 to generate multiple files in L1, this test will fail with trivial move because we end up with one file in L1
```
```
DBTest.NoSpaceCompactRange
This test expect compaction to fail when we force environment to report running out of space, of course this is not valid in trivial move situation
because trivial move does not need any extra space, and did not check for that
```
```
DBTest.DropWrites
Similar to DBTest.NoSpaceCompactRange
```
```
DBTest.DeleteObsoleteFilesPendingOutputs
This test expect that a file in L2 is deleted after it's moved to L3, this is not valid with trivial move because although the file was moved it is now used by L3
```
```
CuckooTableDBTest.CompactionIntoMultipleFiles
Same as DBTest.CompactionsGenerateMultipleFiles
```
This diff is based on a work by @sdong https://reviews.facebook.net/D34149
Test Plan: make -j64 check
Reviewers: rven, sdong, igor
Reviewed By: igor
Subscribers: yhchiang, ott, march, dhruba, sdong
Differential Revision: https://reviews.facebook.net/D34797
2015-06-05 01:51:25 +02:00
|
|
|
// This is a manual compaction and we have a compaction filter that should
|
|
|
|
// be executed, we cannot do a trivial move
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-07 23:18:55 +02:00
|
|
|
// Used in universal compaction, where trivial move can be done if the
|
|
|
|
// input files are non overlapping
|
2017-12-11 22:12:12 +01:00
|
|
|
if ((mutable_cf_options_.compaction_options_universal.allow_trivial_move) &&
|
2015-07-27 23:25:57 +02:00
|
|
|
(output_level_ != 0)) {
|
2015-07-07 23:18:55 +02:00
|
|
|
return is_trivial_move_;
|
|
|
|
}
|
|
|
|
|
2016-12-07 20:42:49 +01:00
|
|
|
if (!(start_level_ != output_level_ && num_input_levels() == 1 &&
|
2015-07-09 00:21:10 +02:00
|
|
|
input(0, 0)->fd.GetPathId() == output_path_id() &&
|
2016-12-07 20:42:49 +01:00
|
|
|
InputCompressionMatchesOutput())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert inputs_.size() == 1
|
|
|
|
|
2020-07-24 22:43:14 +02:00
|
|
|
std::unique_ptr<SstPartitioner> partitioner = CreateSstPartitioner();
|
|
|
|
|
2016-12-07 20:42:49 +01:00
|
|
|
for (const auto& file : inputs_.front().files) {
|
|
|
|
std::vector<FileMetaData*> file_grand_parents;
|
|
|
|
if (output_level_ + 1 >= number_levels_) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
input_vstorage_->GetOverlappingInputs(output_level_ + 1, &file->smallest,
|
|
|
|
&file->largest, &file_grand_parents);
|
|
|
|
const auto compaction_size =
|
|
|
|
file->fd.GetFileSize() + TotalFileSize(file_grand_parents);
|
|
|
|
if (compaction_size > max_compaction_bytes_) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-24 22:43:14 +02:00
|
|
|
|
|
|
|
if (partitioner.get() != nullptr) {
|
|
|
|
if (!partitioner->CanDoTrivialMove(file->smallest.user_key(),
|
|
|
|
file->largest.user_key())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 20:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
|
|
|
|
2014-11-06 20:14:28 +01:00
|
|
|
void Compaction::AddInputDeletions(VersionEdit* out_edit) {
|
2014-11-11 22:47:22 +01:00
|
|
|
for (size_t which = 0; which < num_input_levels(); which++) {
|
2014-01-16 01:22:34 +01:00
|
|
|
for (size_t i = 0; i < inputs_[which].size(); i++) {
|
2014-11-06 20:14:28 +01:00
|
|
|
out_edit->DeleteFile(level(which), inputs_[which][i]->fd.GetNumber());
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 20:06:23 +02:00
|
|
|
bool Compaction::KeyNotExistsBeyondOutputLevel(
|
|
|
|
const Slice& user_key, std::vector<size_t>* level_ptrs) const {
|
2014-10-27 23:49:46 +01:00
|
|
|
assert(input_version_ != nullptr);
|
2015-08-18 20:06:23 +02:00
|
|
|
assert(level_ptrs != nullptr);
|
|
|
|
assert(level_ptrs->size() == static_cast<size_t>(number_levels_));
|
2017-10-28 00:43:42 +02:00
|
|
|
if (bottommost_level_) {
|
|
|
|
return true;
|
|
|
|
} else if (output_level_ != 0 &&
|
|
|
|
cfd_->ioptions()->compaction_style == kCompactionStyleLevel) {
|
2017-08-15 21:51:41 +02:00
|
|
|
// Maybe use binary search to find right entry instead of linear search?
|
|
|
|
const Comparator* user_cmp = cfd_->user_comparator();
|
|
|
|
for (int lvl = output_level_ + 1; lvl < number_levels_; lvl++) {
|
|
|
|
const std::vector<FileMetaData*>& files =
|
|
|
|
input_vstorage_->LevelFiles(lvl);
|
|
|
|
for (; level_ptrs->at(lvl) < files.size(); level_ptrs->at(lvl)++) {
|
|
|
|
auto* f = files[level_ptrs->at(lvl)];
|
|
|
|
if (user_cmp->Compare(user_key, f->largest.user_key()) <= 0) {
|
|
|
|
// We've advanced far enough
|
Allow compaction iterator to perform garbage collection (#7556)
Summary:
Add a threshold timestamp, full_history_ts_low_ of type `std::string*` to
`CompactionIterator`, so that RocksDB can also perform garbage collection during
compaction.
* If full_history_ts_low_ is nullptr, then compaction iterator does not perform
GC, preserving all timestamp history for all keys. Compaction iterator will
treat user key with different timestamps as different user keys.
* If full_history_ts_low_ is not nullptr, then compaction iterator performs
GC. GC will look at keys older than `*full_history_ts_low_` and determine their
eligibility based on factors including snapshots.
Current rules of GC:
* If an internal key is in the same snapshot as a previous counterpart
with the same user key, and this key is eligible for GC, and the key is
not single-delete or merge operand, then this key can be dropped. Note
that the previous internal key cannot be a merge operand either.
* If a tombstone is the most recent one in the earliest snapshot and it
is eligible for GC, and keyNotExistsBeyondLevel() is true, then this
tombstone can be dropped.
* If a tombstone is the most recent one in a snapshot and it is eligible
for GC, and the compaction is at bottommost level, then all other older
internal keys of the same user key must also be eligible for GC, thus
can be dropped
* Single-delete, delete-range and merge are not currently supported.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7556
Test Plan: make check
Reviewed By: ltamasi
Differential Revision: D24507728
Pulled By: riversand963
fbshipit-source-id: 3c09c7301f41eed76dfcf4d1527e68cf6e0a8bb3
2020-10-24 07:58:05 +02:00
|
|
|
// In the presence of user-defined timestamp, we may need to handle
|
|
|
|
// the case in which f->smallest.user_key() (including ts) has the
|
|
|
|
// same user key, but the ts part is smaller. If so,
|
|
|
|
// Compare(user_key, f->smallest.user_key()) returns -1.
|
|
|
|
// That's why we need CompareWithoutTimestamp().
|
|
|
|
if (user_cmp->CompareWithoutTimestamp(user_key,
|
|
|
|
f->smallest.user_key()) >= 0) {
|
2019-01-24 02:58:14 +01:00
|
|
|
// Key falls in this file's range, so it may
|
|
|
|
// exist beyond output level
|
2017-08-15 21:51:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-15 21:51:41 +02:00
|
|
|
return true;
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
2017-10-28 00:43:42 +02:00
|
|
|
return false;
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mark (or clear) each file that is being compacted
|
2014-07-17 23:36:41 +02:00
|
|
|
void Compaction::MarkFilesBeingCompacted(bool mark_as_compacted) {
|
2014-11-11 22:47:22 +01:00
|
|
|
for (size_t i = 0; i < num_input_levels(); i++) {
|
2015-12-16 00:26:20 +01:00
|
|
|
for (size_t j = 0; j < inputs_[i].size(); j++) {
|
2016-05-17 22:11:56 +02:00
|
|
|
assert(mark_as_compacted ? !inputs_[i][j]->being_compacted
|
|
|
|
: inputs_[i][j]->being_compacted);
|
2014-07-17 23:36:41 +02:00
|
|
|
inputs_[i][j]->being_compacted = mark_as_compacted;
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
options.level_compaction_dynamic_level_bytes to allow RocksDB to pick size bases of levels dynamically.
Summary:
When having fixed max_bytes_for_level_base, the ratio of size of largest level and the second one can range from 0 to the multiplier. This makes LSM tree frequently irregular and unpredictable. It can also cause poor space amplification in some cases.
In this improvement (proposed by Igor Kabiljo), we introduce a parameter option.level_compaction_use_dynamic_max_bytes. When turning it on, RocksDB is free to pick a level base in the range of (options.max_bytes_for_level_base/options.max_bytes_for_level_multiplier, options.max_bytes_for_level_base] so that real level ratios are close to options.max_bytes_for_level_multiplier.
Test Plan: New unit tests and pass tests suites including valgrind.
Reviewers: MarkCallaghan, rven, yhchiang, igor, ikabiljo
Reviewed By: ikabiljo
Subscribers: yoshinorim, ikabiljo, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D31437
2015-02-05 20:44:17 +01:00
|
|
|
// Sample output:
|
|
|
|
// If compacting 3 L0 files, 2 L3 files and 1 L4 file, and outputting to L5,
|
|
|
|
// print: "3@0 + 2@3 + 1@4 files to L5"
|
|
|
|
const char* Compaction::InputLevelSummary(
|
|
|
|
InputLevelSummaryBuffer* scratch) const {
|
|
|
|
int len = 0;
|
|
|
|
bool is_first = true;
|
|
|
|
for (auto& input_level : inputs_) {
|
|
|
|
if (input_level.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!is_first) {
|
|
|
|
len +=
|
|
|
|
snprintf(scratch->buffer + len, sizeof(scratch->buffer) - len, " + ");
|
2018-10-05 23:49:01 +02:00
|
|
|
len = std::min(len, static_cast<int>(sizeof(scratch->buffer)));
|
options.level_compaction_dynamic_level_bytes to allow RocksDB to pick size bases of levels dynamically.
Summary:
When having fixed max_bytes_for_level_base, the ratio of size of largest level and the second one can range from 0 to the multiplier. This makes LSM tree frequently irregular and unpredictable. It can also cause poor space amplification in some cases.
In this improvement (proposed by Igor Kabiljo), we introduce a parameter option.level_compaction_use_dynamic_max_bytes. When turning it on, RocksDB is free to pick a level base in the range of (options.max_bytes_for_level_base/options.max_bytes_for_level_multiplier, options.max_bytes_for_level_base] so that real level ratios are close to options.max_bytes_for_level_multiplier.
Test Plan: New unit tests and pass tests suites including valgrind.
Reviewers: MarkCallaghan, rven, yhchiang, igor, ikabiljo
Reviewed By: ikabiljo
Subscribers: yoshinorim, ikabiljo, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D31437
2015-02-05 20:44:17 +01:00
|
|
|
} else {
|
|
|
|
is_first = false;
|
|
|
|
}
|
|
|
|
len += snprintf(scratch->buffer + len, sizeof(scratch->buffer) - len,
|
2015-07-13 21:11:05 +02:00
|
|
|
"%" ROCKSDB_PRIszt "@%d", input_level.size(),
|
|
|
|
input_level.level);
|
2018-10-05 23:49:01 +02:00
|
|
|
len = std::min(len, static_cast<int>(sizeof(scratch->buffer)));
|
options.level_compaction_dynamic_level_bytes to allow RocksDB to pick size bases of levels dynamically.
Summary:
When having fixed max_bytes_for_level_base, the ratio of size of largest level and the second one can range from 0 to the multiplier. This makes LSM tree frequently irregular and unpredictable. It can also cause poor space amplification in some cases.
In this improvement (proposed by Igor Kabiljo), we introduce a parameter option.level_compaction_use_dynamic_max_bytes. When turning it on, RocksDB is free to pick a level base in the range of (options.max_bytes_for_level_base/options.max_bytes_for_level_multiplier, options.max_bytes_for_level_base] so that real level ratios are close to options.max_bytes_for_level_multiplier.
Test Plan: New unit tests and pass tests suites including valgrind.
Reviewers: MarkCallaghan, rven, yhchiang, igor, ikabiljo
Reviewed By: ikabiljo
Subscribers: yoshinorim, ikabiljo, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D31437
2015-02-05 20:44:17 +01:00
|
|
|
}
|
|
|
|
snprintf(scratch->buffer + len, sizeof(scratch->buffer) - len,
|
|
|
|
" files to L%d", output_level());
|
|
|
|
|
|
|
|
return scratch->buffer;
|
|
|
|
}
|
|
|
|
|
Include bunch of more events into EventLogger
Summary:
Added these events:
* Recovery start, finish and also when recovery creates a file
* Trivial move
* Compaction start, finish and when compaction creates a file
* Flush start, finish
Also includes small fix to EventLogger
Also added option ROCKSDB_PRINT_EVENTS_TO_STDOUT which is useful when we debug things. I've spent far too much time chasing LOG files.
Still didn't get sst table properties in JSON. They are written very deeply into the stack. I'll address in separate diff.
TODO:
* Write specification. Let's first use this for a while and figure out what's good data to put here, too. After that we'll write spec
* Write tools that parse and analyze LOGs. This can be in python or go. Good intern task.
Test Plan: Ran db_bench with ROCKSDB_PRINT_EVENTS_TO_STDOUT. Here's the output: https://phabricator.fb.com/P19811976
Reviewers: sdong, yhchiang, rven, MarkCallaghan, kradhakrishnan, anthony
Reviewed By: anthony
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D37521
2015-04-28 00:20:02 +02:00
|
|
|
uint64_t Compaction::CalculateTotalInputSize() const {
|
|
|
|
uint64_t size = 0;
|
|
|
|
for (auto& input_level : inputs_) {
|
|
|
|
for (auto f : input_level.files) {
|
|
|
|
size += f->fd.GetFileSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2014-02-01 01:45:20 +01:00
|
|
|
void Compaction::ReleaseCompactionFiles(Status status) {
|
Make Compaction class easier to use
Summary:
The goal of this diff is to make Compaction class easier to use. This should also make new compaction algorithms easier to write (like CompactFiles from @yhchiang and dynamic leveled and multi-leveled universal from @sdong).
Here are couple of things demonstrating that Compaction class is hard to use:
1. we have two constructors of Compaction class
2. there's this thing called grandparents_, but it appears to only be setup for leveled compaction and not compactfiles
3. it's easy to introduce a subtle and dangerous bug like this: D36225
4. SetupBottomMostLevel() is hard to understand and it shouldn't be. See this comment: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction.cc#L236-L241. It also made it harder for @yhchiang to write CompactFiles, as evidenced by this: https://github.com/facebook/rocksdb/blob/afbafeaeaebfd27a0f3e992fee8e0c57d07658fa/db/compaction_picker.cc#L204-L210
The problem is that we create Compaction object, which holds a lot of state, and then pass it around to some functions. After those functions are done mutating, then we call couple of functions on Compaction object, like SetupBottommostLevel() and MarkFilesBeingCompacted(). It is very hard to see what's happening with all that Compaction's state while it's travelling across different functions. If you're writing a new PickCompaction() function you need to try really hard to understand what are all the functions you need to run on Compaction object and what state you need to setup.
My proposed solution is to make important parts of Compaction immutable after construction. PickCompaction() should calculate compaction inputs and then pass them onto Compaction object once they are finalized. That makes it easy to create a new compaction -- just provide all the parameters to the constructor and you're done. No need to call confusing functions after you created your object.
This diff doesn't fully achieve that goal, but it comes pretty close. Here are some of the changes:
* have one Compaction constructor instead of two.
* inputs_ is constant after construction
* MarkFilesBeingCompacted() is now private to Compaction class and automatically called on construction/destruction.
* SetupBottommostLevel() is gone. Compaction figures it out on its own based on the input.
* CompactionPicker's functions are not passing around Compaction object anymore. They are only passing around the state that they need.
Test Plan:
make check
make asan_check
make valgrind_check
Reviewers: rven, anthony, sdong, yhchiang
Reviewed By: yhchiang
Subscribers: sdong, yhchiang, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D36687
2015-04-11 00:01:54 +02:00
|
|
|
MarkFilesBeingCompacted(false);
|
2014-02-01 01:45:20 +01:00
|
|
|
cfd_->compaction_picker()->ReleaseCompactionFiles(this, status);
|
|
|
|
}
|
|
|
|
|
2014-01-16 01:22:34 +01:00
|
|
|
void Compaction::ResetNextCompactionIndex() {
|
2014-10-27 23:49:46 +01:00
|
|
|
assert(input_version_ != nullptr);
|
2016-12-07 20:42:49 +01:00
|
|
|
input_vstorage_->ResetNextCompactionIndex(start_level_);
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
|
|
|
|
2014-05-14 21:13:50 +02:00
|
|
|
namespace {
|
|
|
|
int InputSummary(const std::vector<FileMetaData*>& files, char* output,
|
|
|
|
int len) {
|
2014-03-20 00:01:25 +01:00
|
|
|
*output = '\0';
|
2014-01-16 01:22:34 +01:00
|
|
|
int write = 0;
|
2015-12-16 00:26:20 +01:00
|
|
|
for (size_t i = 0; i < files.size(); i++) {
|
2014-01-16 01:22:34 +01:00
|
|
|
int sz = len - write;
|
2013-12-23 17:54:50 +01:00
|
|
|
int ret;
|
|
|
|
char sztxt[16];
|
2014-06-14 00:54:19 +02:00
|
|
|
AppendHumanBytes(files.at(i)->fd.GetFileSize(), sztxt, 16);
|
|
|
|
ret = snprintf(output + write, sz, "%" PRIu64 "(%s) ",
|
|
|
|
files.at(i)->fd.GetNumber(), sztxt);
|
2014-05-14 21:13:50 +02:00
|
|
|
if (ret < 0 || ret >= sz) break;
|
2014-01-16 01:22:34 +01:00
|
|
|
write += ret;
|
|
|
|
}
|
2014-05-14 21:13:50 +02:00
|
|
|
// if files.size() is non-zero, overwrite the last space
|
|
|
|
return write - !!files.size();
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
2014-05-14 21:13:50 +02:00
|
|
|
} // namespace
|
2014-01-16 01:22:34 +01:00
|
|
|
|
|
|
|
void Compaction::Summary(char* output, int len) {
|
2014-05-14 21:13:50 +02:00
|
|
|
int write =
|
2016-05-17 22:11:56 +02:00
|
|
|
snprintf(output, len, "Base version %" PRIu64 " Base level %d, inputs: [",
|
|
|
|
input_version_->GetVersionNumber(), start_level_);
|
2014-01-25 23:12:24 +01:00
|
|
|
if (write < 0 || write >= len) {
|
2014-01-16 01:22:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-11 22:47:22 +01:00
|
|
|
for (size_t level_iter = 0; level_iter < num_input_levels(); ++level_iter) {
|
2014-11-06 20:14:28 +01:00
|
|
|
if (level_iter > 0) {
|
2014-07-17 23:36:41 +02:00
|
|
|
write += snprintf(output + write, len - write, "], [");
|
|
|
|
if (write < 0 || write >= len) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-11-06 20:14:28 +01:00
|
|
|
write +=
|
|
|
|
InputSummary(inputs_[level_iter].files, output + write, len - write);
|
2014-07-17 23:36:41 +02:00
|
|
|
if (write < 0 || write >= len) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
|
|
|
|
2014-05-14 21:13:50 +02:00
|
|
|
snprintf(output + write, len - write, "]");
|
2014-01-16 01:22:34 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 03:36:39 +01:00
|
|
|
uint64_t Compaction::OutputFilePreallocationSize() const {
|
2014-06-05 22:19:35 +02:00
|
|
|
uint64_t preallocation_size = 0;
|
|
|
|
|
2018-01-23 01:36:00 +01:00
|
|
|
for (const auto& level_files : inputs_) {
|
|
|
|
for (const auto& file : level_files.files) {
|
|
|
|
preallocation_size += file->fd.GetFileSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 02:40:29 +02:00
|
|
|
if (max_output_file_size_ != port::kMaxUint64 &&
|
2021-05-05 22:59:21 +02:00
|
|
|
(immutable_options_.compaction_style == kCompactionStyleLevel ||
|
2017-05-05 02:40:29 +02:00
|
|
|
output_level() > 0)) {
|
2018-01-23 01:36:00 +01:00
|
|
|
preallocation_size = std::min(max_output_file_size_, preallocation_size);
|
2014-06-05 22:19:35 +02:00
|
|
|
}
|
2018-01-23 01:36:00 +01:00
|
|
|
|
2014-06-05 22:19:35 +02:00
|
|
|
// Over-estimate slightly so we don't end up just barely crossing
|
|
|
|
// the threshold
|
2021-03-26 05:17:17 +01:00
|
|
|
// No point to preallocate more than 1GB.
|
2018-01-23 01:36:00 +01:00
|
|
|
return std::min(uint64_t{1073741824},
|
|
|
|
preallocation_size + (preallocation_size / 10));
|
2014-06-05 22:19:35 +02:00
|
|
|
}
|
|
|
|
|
Allowing L0 -> L1 trivial move on sorted data
Summary:
This diff updates the logic of how we do trivial move, now trivial move can run on any number of files in input level as long as they are not overlapping
The conditions for trivial move have been updated
Introduced conditions:
- Trivial move cannot happen if we have a compaction filter (except if the compaction is not manual)
- Input level files cannot be overlapping
Removed conditions:
- Trivial move only run when the compaction is not manual
- Input level should can contain only 1 file
More context on what tests failed because of Trivial move
```
DBTest.CompactionsGenerateMultipleFiles
This test is expecting compaction on a file in L0 to generate multiple files in L1, this test will fail with trivial move because we end up with one file in L1
```
```
DBTest.NoSpaceCompactRange
This test expect compaction to fail when we force environment to report running out of space, of course this is not valid in trivial move situation
because trivial move does not need any extra space, and did not check for that
```
```
DBTest.DropWrites
Similar to DBTest.NoSpaceCompactRange
```
```
DBTest.DeleteObsoleteFilesPendingOutputs
This test expect that a file in L2 is deleted after it's moved to L3, this is not valid with trivial move because although the file was moved it is now used by L3
```
```
CuckooTableDBTest.CompactionIntoMultipleFiles
Same as DBTest.CompactionsGenerateMultipleFiles
```
This diff is based on a work by @sdong https://reviews.facebook.net/D34149
Test Plan: make -j64 check
Reviewers: rven, sdong, igor
Reviewed By: igor
Subscribers: yhchiang, ott, march, dhruba, sdong
Differential Revision: https://reviews.facebook.net/D34797
2015-06-05 01:51:25 +02:00
|
|
|
std::unique_ptr<CompactionFilter> Compaction::CreateCompactionFilter() const {
|
2015-06-09 01:34:26 +02:00
|
|
|
if (!cfd_->ioptions()->compaction_filter_factory) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-05-08 01:00:37 +02:00
|
|
|
if (!cfd_->ioptions()
|
|
|
|
->compaction_filter_factory->ShouldFilterTableFileCreation(
|
|
|
|
TableFileCreationReason::kCompaction)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
Allowing L0 -> L1 trivial move on sorted data
Summary:
This diff updates the logic of how we do trivial move, now trivial move can run on any number of files in input level as long as they are not overlapping
The conditions for trivial move have been updated
Introduced conditions:
- Trivial move cannot happen if we have a compaction filter (except if the compaction is not manual)
- Input level files cannot be overlapping
Removed conditions:
- Trivial move only run when the compaction is not manual
- Input level should can contain only 1 file
More context on what tests failed because of Trivial move
```
DBTest.CompactionsGenerateMultipleFiles
This test is expecting compaction on a file in L0 to generate multiple files in L1, this test will fail with trivial move because we end up with one file in L1
```
```
DBTest.NoSpaceCompactRange
This test expect compaction to fail when we force environment to report running out of space, of course this is not valid in trivial move situation
because trivial move does not need any extra space, and did not check for that
```
```
DBTest.DropWrites
Similar to DBTest.NoSpaceCompactRange
```
```
DBTest.DeleteObsoleteFilesPendingOutputs
This test expect that a file in L2 is deleted after it's moved to L3, this is not valid with trivial move because although the file was moved it is now used by L3
```
```
CuckooTableDBTest.CompactionIntoMultipleFiles
Same as DBTest.CompactionsGenerateMultipleFiles
```
This diff is based on a work by @sdong https://reviews.facebook.net/D34149
Test Plan: make -j64 check
Reviewers: rven, sdong, igor
Reviewed By: igor
Subscribers: yhchiang, ott, march, dhruba, sdong
Differential Revision: https://reviews.facebook.net/D34797
2015-06-05 01:51:25 +02:00
|
|
|
CompactionFilter::Context context;
|
|
|
|
context.is_full_compaction = is_full_compaction_;
|
|
|
|
context.is_manual_compaction = is_manual_compaction_;
|
2015-10-08 03:10:39 +02:00
|
|
|
context.column_family_id = cfd_->GetID();
|
2021-05-08 01:00:37 +02:00
|
|
|
context.reason = TableFileCreationReason::kCompaction;
|
Allowing L0 -> L1 trivial move on sorted data
Summary:
This diff updates the logic of how we do trivial move, now trivial move can run on any number of files in input level as long as they are not overlapping
The conditions for trivial move have been updated
Introduced conditions:
- Trivial move cannot happen if we have a compaction filter (except if the compaction is not manual)
- Input level files cannot be overlapping
Removed conditions:
- Trivial move only run when the compaction is not manual
- Input level should can contain only 1 file
More context on what tests failed because of Trivial move
```
DBTest.CompactionsGenerateMultipleFiles
This test is expecting compaction on a file in L0 to generate multiple files in L1, this test will fail with trivial move because we end up with one file in L1
```
```
DBTest.NoSpaceCompactRange
This test expect compaction to fail when we force environment to report running out of space, of course this is not valid in trivial move situation
because trivial move does not need any extra space, and did not check for that
```
```
DBTest.DropWrites
Similar to DBTest.NoSpaceCompactRange
```
```
DBTest.DeleteObsoleteFilesPendingOutputs
This test expect that a file in L2 is deleted after it's moved to L3, this is not valid with trivial move because although the file was moved it is now used by L3
```
```
CuckooTableDBTest.CompactionIntoMultipleFiles
Same as DBTest.CompactionsGenerateMultipleFiles
```
This diff is based on a work by @sdong https://reviews.facebook.net/D34149
Test Plan: make -j64 check
Reviewers: rven, sdong, igor
Reviewed By: igor
Subscribers: yhchiang, ott, march, dhruba, sdong
Differential Revision: https://reviews.facebook.net/D34797
2015-06-05 01:51:25 +02:00
|
|
|
return cfd_->ioptions()->compaction_filter_factory->CreateCompactionFilter(
|
|
|
|
context);
|
|
|
|
}
|
|
|
|
|
2020-07-24 22:43:14 +02:00
|
|
|
std::unique_ptr<SstPartitioner> Compaction::CreateSstPartitioner() const {
|
2021-05-05 22:59:21 +02:00
|
|
|
if (!immutable_options_.sst_partitioner_factory) {
|
2020-07-24 22:43:14 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SstPartitioner::Context context;
|
|
|
|
context.is_full_compaction = is_full_compaction_;
|
|
|
|
context.is_manual_compaction = is_manual_compaction_;
|
|
|
|
context.output_level = output_level_;
|
|
|
|
context.smallest_user_key = smallest_user_key_;
|
|
|
|
context.largest_user_key = largest_user_key_;
|
2021-05-05 22:59:21 +02:00
|
|
|
return immutable_options_.sst_partitioner_factory->CreatePartitioner(context);
|
2020-07-24 22:43:14 +02:00
|
|
|
}
|
|
|
|
|
2015-09-10 22:50:00 +02:00
|
|
|
bool Compaction::IsOutputLevelEmpty() const {
|
|
|
|
return inputs_.back().level != output_level_ || inputs_.back().empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Compaction::ShouldFormSubcompactions() const {
|
2018-04-27 20:48:21 +02:00
|
|
|
if (max_subcompactions_ <= 1 || cfd_ == nullptr) {
|
2015-09-10 22:50:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-12 21:08:30 +02:00
|
|
|
|
|
|
|
// Note: the subcompaction boundary picking logic does not currently guarantee
|
|
|
|
// that all user keys that differ only by timestamp get processed by the same
|
|
|
|
// subcompaction.
|
|
|
|
if (cfd_->user_comparator()->timestamp_size() > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-10 22:50:00 +02:00
|
|
|
if (cfd_->ioptions()->compaction_style == kCompactionStyleLevel) {
|
2018-03-06 21:41:37 +01:00
|
|
|
return (start_level_ == 0 || is_manual_compaction_) && output_level_ > 0 &&
|
|
|
|
!IsOutputLevelEmpty();
|
2015-09-10 22:50:00 +02:00
|
|
|
} else if (cfd_->ioptions()->compaction_style == kCompactionStyleUniversal) {
|
|
|
|
return number_levels_ > 1 && output_level_ > 0;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 01:01:21 +01:00
|
|
|
uint64_t Compaction::MinInputFileOldestAncesterTime() const {
|
|
|
|
uint64_t min_oldest_ancester_time = port::kMaxUint64;
|
2020-01-11 04:01:00 +01:00
|
|
|
for (const auto& level_files : inputs_) {
|
|
|
|
for (const auto& file : level_files.files) {
|
|
|
|
uint64_t oldest_ancester_time = file->TryGetOldestAncesterTime();
|
|
|
|
if (oldest_ancester_time != 0) {
|
|
|
|
min_oldest_ancester_time =
|
|
|
|
std::min(min_oldest_ancester_time, oldest_ancester_time);
|
|
|
|
}
|
2017-06-28 02:02:20 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-23 01:01:21 +01:00
|
|
|
return min_oldest_ancester_time;
|
2017-06-28 02:02:20 +02:00
|
|
|
}
|
|
|
|
|
2018-05-04 01:35:46 +02:00
|
|
|
int Compaction::GetInputBaseLevel() const {
|
|
|
|
return input_vstorage_->base_level();
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|