fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
385 lines
15 KiB
385 lines
15 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
7 years ago
|
// 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).
|
||
11 years ago
|
//
|
||
|
// 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.
|
||
|
|
||
|
#pragma once
|
||
8 years ago
|
#include "db/version_set.h"
|
||
6 years ago
|
#include "memory/arena.h"
|
||
8 years ago
|
#include "options/cf_options.h"
|
||
11 years ago
|
#include "util/autovector.h"
|
||
11 years ago
|
|
||
|
namespace rocksdb {
|
||
6 years ago
|
// The file contains class Compaction, as well as some helper functions
|
||
|
// and data structures used by the class.
|
||
11 years ago
|
|
||
6 years ago
|
// Utility for comparing sstable boundary keys. Returns -1 if either a or b is
|
||
|
// null which provides the property that a==null indicates a key that is less
|
||
|
// than any key and b==null indicates a key that is greater than any key. Note
|
||
|
// that the comparison is performed primarily on the user-key portion of the
|
||
|
// key. If the user-keys compare equal, an additional test is made to sort
|
||
|
// range tombstone sentinel keys before other keys with the same user-key. The
|
||
|
// result is that 2 user-keys will compare equal if they differ purely on
|
||
|
// their sequence number and value, but the range tombstone sentinel for that
|
||
|
// user-key will compare not equal. This is necessary because the range
|
||
|
// tombstone sentinel key is set as the largest key for an sstable even though
|
||
|
// that key never appears in the database. We don't want adjacent sstables to
|
||
|
// be considered overlapping if they are separated by the range tombstone
|
||
|
// sentinel.
|
||
|
int sstableKeyCompare(const Comparator* user_cmp, const InternalKey& a,
|
||
|
const InternalKey& b);
|
||
|
int sstableKeyCompare(const Comparator* user_cmp, const InternalKey* a,
|
||
|
const InternalKey& b);
|
||
|
int sstableKeyCompare(const Comparator* user_cmp, const InternalKey& a,
|
||
|
const InternalKey* b);
|
||
|
|
||
|
// An AtomicCompactionUnitBoundary represents a range of keys [smallest,
|
||
|
// largest] that exactly spans one ore more neighbouring SSTs on the same
|
||
|
// level. Every pair of SSTs in this range "overlap" (i.e., the largest
|
||
|
// user key of one file is the smallest user key of the next file). These
|
||
|
// boundaries are propagated down to RangeDelAggregator during compaction
|
||
|
// to provide safe truncation boundaries for range tombstones.
|
||
|
struct AtomicCompactionUnitBoundary {
|
||
|
const InternalKey* smallest = nullptr;
|
||
|
const InternalKey* largest = nullptr;
|
||
|
};
|
||
|
|
||
10 years ago
|
// The structure that manages compaction input files associated
|
||
|
// with the same physical level.
|
||
10 years ago
|
struct CompactionInputFiles {
|
||
|
int level;
|
||
|
std::vector<FileMetaData*> files;
|
||
6 years ago
|
std::vector<AtomicCompactionUnitBoundary> atomic_compaction_unit_boundaries;
|
||
10 years ago
|
inline bool empty() const { return files.empty(); }
|
||
|
inline size_t size() const { return files.size(); }
|
||
|
inline void clear() { files.clear(); }
|
||
10 years ago
|
inline FileMetaData* operator[](size_t i) const { return files[i]; }
|
||
10 years ago
|
};
|
||
|
|
||
11 years ago
|
class Version;
|
||
11 years ago
|
class ColumnFamilyData;
|
||
10 years ago
|
class VersionStorageInfo;
|
||
10 years ago
|
class CompactionFilter;
|
||
11 years ago
|
|
||
6 years ago
|
// A Compaction encapsulates metadata about a compaction.
|
||
11 years ago
|
class Compaction {
|
||
|
public:
|
||
10 years ago
|
Compaction(VersionStorageInfo* input_version,
|
||
8 years ago
|
const ImmutableCFOptions& immutable_cf_options,
|
||
10 years ago
|
const MutableCFOptions& mutable_cf_options,
|
||
|
std::vector<CompactionInputFiles> inputs, int output_level,
|
||
9 years ago
|
uint64_t target_file_size, uint64_t max_compaction_bytes,
|
||
10 years ago
|
uint32_t output_path_id, CompressionType compression,
|
||
6 years ago
|
CompressionOptions compression_opts, uint32_t max_subcompactions,
|
||
10 years ago
|
std::vector<FileMetaData*> grandparents,
|
||
|
bool manual_compaction = false, double score = -1,
|
||
9 years ago
|
bool deletion_compaction = false,
|
||
|
CompactionReason compaction_reason = CompactionReason::kUnknown);
|
||
10 years ago
|
|
||
11 years ago
|
// No copying allowed
|
||
|
Compaction(const Compaction&) = delete;
|
||
|
void operator=(const Compaction&) = delete;
|
||
|
|
||
11 years ago
|
~Compaction();
|
||
|
|
||
10 years ago
|
// Returns the level associated to the specified compaction input level.
|
||
10 years ago
|
// If compaction_input_level is not specified, then input_level is set to 0.
|
||
10 years ago
|
int level(size_t compaction_input_level = 0) const {
|
||
10 years ago
|
return inputs_[compaction_input_level].level;
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
int start_level() const { return start_level_; }
|
||
|
|
||
11 years ago
|
// Outputs will go to this level
|
||
10 years ago
|
int output_level() const { return output_level_; }
|
||
|
|
||
|
// Returns the number of input levels in this compaction.
|
||
10 years ago
|
size_t num_input_levels() const { return inputs_.size(); }
|
||
11 years ago
|
|
||
|
// Return the object that holds the edits to the descriptor done
|
||
|
// by this compaction.
|
||
10 years ago
|
VersionEdit* edit() { return &edit_; }
|
||
11 years ago
|
|
||
10 years ago
|
// Returns the number of input files associated to the specified
|
||
|
// compaction input level.
|
||
|
// The function will return 0 if when "compaction_input_level" < 0
|
||
|
// or "compaction_input_level" >= "num_input_levels()".
|
||
10 years ago
|
size_t num_input_files(size_t compaction_input_level) const {
|
||
10 years ago
|
if (compaction_input_level < inputs_.size()) {
|
||
10 years ago
|
return inputs_[compaction_input_level].size();
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// Returns input version of the compaction
|
||
|
Version* input_version() const { return input_version_; }
|
||
|
|
||
10 years ago
|
// Returns the ColumnFamilyData associated with the compaction.
|
||
11 years ago
|
ColumnFamilyData* column_family_data() const { return cfd_; }
|
||
|
|
||
10 years ago
|
// Returns the file meta data of the 'i'th input file at the
|
||
|
// specified compaction input level.
|
||
|
// REQUIREMENT: "compaction_input_level" must be >= 0 and
|
||
|
// < "input_levels()"
|
||
10 years ago
|
FileMetaData* input(size_t compaction_input_level, size_t i) const {
|
||
10 years ago
|
assert(compaction_input_level < inputs_.size());
|
||
10 years ago
|
return inputs_[compaction_input_level][i];
|
||
|
}
|
||
11 years ago
|
|
||
6 years ago
|
const std::vector<AtomicCompactionUnitBoundary>* boundaries(
|
||
|
size_t compaction_input_level) const {
|
||
|
assert(compaction_input_level < inputs_.size());
|
||
|
return &inputs_[compaction_input_level].atomic_compaction_unit_boundaries;
|
||
|
}
|
||
|
|
||
10 years ago
|
// Returns the list of file meta data of the specified compaction
|
||
|
// input level.
|
||
|
// REQUIREMENT: "compaction_input_level" must be >= 0 and
|
||
|
// < "input_levels()"
|
||
8 years ago
|
const std::vector<FileMetaData*>* inputs(
|
||
|
size_t compaction_input_level) const {
|
||
10 years ago
|
assert(compaction_input_level < inputs_.size());
|
||
10 years ago
|
return &inputs_[compaction_input_level].files;
|
||
10 years ago
|
}
|
||
11 years ago
|
|
||
8 years ago
|
const std::vector<CompactionInputFiles>* inputs() { return &inputs_; }
|
||
|
|
||
10 years ago
|
// Returns the LevelFilesBrief of the specified compaction input level.
|
||
9 years ago
|
const LevelFilesBrief* input_levels(size_t compaction_input_level) const {
|
||
10 years ago
|
return &input_levels_[compaction_input_level];
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// Maximum size of files to build during this compaction.
|
||
10 years ago
|
uint64_t max_output_file_size() const { return max_output_file_size_; }
|
||
11 years ago
|
|
||
11 years ago
|
// What compression for output
|
||
10 years ago
|
CompressionType output_compression() const { return output_compression_; }
|
||
11 years ago
|
|
||
6 years ago
|
// What compression options for output
|
||
|
CompressionOptions output_compression_opts() const {
|
||
|
return output_compression_opts_;
|
||
|
}
|
||
|
|
||
11 years ago
|
// Whether need to write output file to second DB path.
|
||
10 years ago
|
uint32_t output_path_id() const { return output_path_id_; }
|
||
11 years ago
|
|
||
11 years ago
|
// Is this a trivial compaction that can be implemented by just
|
||
11 years ago
|
// moving a single input file to the next level (no merging or splitting)
|
||
|
bool IsTrivialMove() const;
|
||
|
|
||
10 years ago
|
// If true, then the compaction can be done by simply deleting input files.
|
||
9 years ago
|
bool deletion_compaction() const { return deletion_compaction_; }
|
||
11 years ago
|
|
||
11 years ago
|
// Add all inputs to this compaction as delete operations to *edit.
|
||
|
void AddInputDeletions(VersionEdit* edit);
|
||
|
|
||
10 years ago
|
// Returns true if the available information we have guarantees that
|
||
|
// the input "user_key" does not exist in any level beyond "output_level()".
|
||
9 years ago
|
bool KeyNotExistsBeyondOutputLevel(const Slice& user_key,
|
||
|
std::vector<size_t>* level_ptrs) const;
|
||
11 years ago
|
|
||
11 years ago
|
// Clear all files to indicate that they are not being compacted
|
||
|
// Delete this compaction from the list of running compactions.
|
||
9 years ago
|
//
|
||
|
// Requirement: DB mutex held
|
||
11 years ago
|
void ReleaseCompactionFiles(Status status);
|
||
|
|
||
10 years ago
|
// Returns the summary of the compaction in "output" with maximum "len"
|
||
|
// in bytes. The caller is responsible for the memory management of
|
||
|
// "output".
|
||
11 years ago
|
void Summary(char* output, int len);
|
||
|
|
||
|
// Return the score that was used to pick this compaction run.
|
||
|
double score() const { return score_; }
|
||
|
|
||
|
// Is this compaction creating a file in the bottom most level?
|
||
9 years ago
|
bool bottommost_level() const { return bottommost_level_; }
|
||
11 years ago
|
|
||
|
// Does this compaction include all sst files?
|
||
9 years ago
|
bool is_full_compaction() const { return is_full_compaction_; }
|
||
11 years ago
|
|
||
11 years ago
|
// Was this compaction triggered manually by the client?
|
||
9 years ago
|
bool is_manual_compaction() const { return is_manual_compaction_; }
|
||
11 years ago
|
|
||
10 years ago
|
// Used when allow_trivial_move option is set in
|
||
|
// Universal compaction. If all the input files are
|
||
|
// non overlapping, then is_trivial_move_ variable
|
||
|
// will be set true, else false
|
||
|
void set_is_trivial_move(bool trivial_move) {
|
||
|
is_trivial_move_ = trivial_move;
|
||
|
}
|
||
|
|
||
|
// Used when allow_trivial_move option is set in
|
||
|
// Universal compaction. Returns true, if the input files
|
||
|
// are non-overlapping and can be trivially moved.
|
||
9 years ago
|
bool is_trivial_move() const { return is_trivial_move_; }
|
||
10 years ago
|
|
||
9 years ago
|
// How many total levels are there?
|
||
|
int number_levels() const { return number_levels_; }
|
||
|
|
||
8 years ago
|
// Return the ImmutableCFOptions that should be used throughout the compaction
|
||
|
// procedure
|
||
|
const ImmutableCFOptions* immutable_cf_options() const {
|
||
|
return &immutable_cf_options_;
|
||
|
}
|
||
|
|
||
10 years ago
|
// Return the MutableCFOptions that should be used throughout the compaction
|
||
|
// procedure
|
||
9 years ago
|
const MutableCFOptions* mutable_cf_options() const {
|
||
|
return &mutable_cf_options_;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
// Returns the size in bytes that the output file should be preallocated to.
|
||
11 years ago
|
// In level compaction, that is max_file_size_. In universal compaction, that
|
||
10 years ago
|
// is the sum of all input file sizes.
|
||
9 years ago
|
uint64_t OutputFilePreallocationSize() const;
|
||
11 years ago
|
|
||
10 years ago
|
void SetInputVersion(Version* input_version);
|
||
|
|
||
10 years ago
|
struct InputLevelSummaryBuffer {
|
||
|
char buffer[128];
|
||
|
};
|
||
|
|
||
|
const char* InputLevelSummary(InputLevelSummaryBuffer* scratch) const;
|
||
|
|
||
10 years ago
|
uint64_t CalculateTotalInputSize() const;
|
||
|
|
||
10 years ago
|
// In case of compaction error, reset the nextIndex that is used
|
||
|
// to pick up the next file to be compacted from files_by_size_
|
||
|
void ResetNextCompactionIndex();
|
||
|
|
||
10 years ago
|
// Create a CompactionFilter from compaction_filter_factory
|
||
|
std::unique_ptr<CompactionFilter> CreateCompactionFilter() const;
|
||
|
|
||
9 years ago
|
// Is the input level corresponding to output_level_ empty?
|
||
|
bool IsOutputLevelEmpty() const;
|
||
9 years ago
|
|
||
9 years ago
|
// Should this compaction be broken up into smaller ones run in parallel?
|
||
|
bool ShouldFormSubcompactions() const;
|
||
9 years ago
|
|
||
9 years ago
|
// test function to validate the functionality of IsBottommostLevel()
|
||
|
// function -- determines if compaction with inputs and storage is bottommost
|
||
|
static bool TEST_IsBottommostLevel(
|
||
|
int output_level, VersionStorageInfo* vstorage,
|
||
|
const std::vector<CompactionInputFiles>& inputs);
|
||
|
|
||
9 years ago
|
TablePropertiesCollection GetOutputTableProperties() const {
|
||
|
return output_table_properties_;
|
||
|
}
|
||
|
|
||
|
void SetOutputTableProperties(TablePropertiesCollection tp) {
|
||
|
output_table_properties_ = std::move(tp);
|
||
|
}
|
||
|
|
||
9 years ago
|
Slice GetSmallestUserKey() const { return smallest_user_key_; }
|
||
|
|
||
9 years ago
|
Slice GetLargestUserKey() const { return largest_user_key_; }
|
||
|
|
||
7 years ago
|
int GetInputBaseLevel() const;
|
||
|
|
||
9 years ago
|
CompactionReason compaction_reason() { return compaction_reason_; }
|
||
|
|
||
9 years ago
|
const std::vector<FileMetaData*>& grandparents() const {
|
||
|
return grandparents_;
|
||
|
}
|
||
|
|
||
9 years ago
|
uint64_t max_compaction_bytes() const { return max_compaction_bytes_; }
|
||
9 years ago
|
|
||
7 years ago
|
uint32_t max_subcompactions() const { return max_subcompactions_; }
|
||
|
|
||
8 years ago
|
uint64_t MaxInputFileCreationTime() const;
|
||
|
|
||
11 years ago
|
private:
|
||
10 years ago
|
// mark (or clear) all files that are being compacted
|
||
|
void MarkFilesBeingCompacted(bool mark_as_compacted);
|
||
11 years ago
|
|
||
9 years ago
|
// get the smallest and largest key present in files to be compacted
|
||
|
static void GetBoundaryKeys(VersionStorageInfo* vstorage,
|
||
|
const std::vector<CompactionInputFiles>& inputs,
|
||
|
Slice* smallest_key, Slice* largest_key);
|
||
|
|
||
6 years ago
|
// Get the atomic file boundaries for all files in the compaction. Necessary
|
||
|
// in order to avoid the scenario described in
|
||
|
// https://github.com/facebook/rocksdb/pull/4432#discussion_r221072219 and plumb
|
||
|
// down appropriate key boundaries to RangeDelAggregator during compaction.
|
||
|
static std::vector<CompactionInputFiles> PopulateWithAtomicBoundaries(
|
||
|
VersionStorageInfo* vstorage, std::vector<CompactionInputFiles> inputs);
|
||
|
|
||
10 years ago
|
// helper function to determine if compaction with inputs and storage is
|
||
|
// bottommost
|
||
|
static bool IsBottommostLevel(
|
||
|
int output_level, VersionStorageInfo* vstorage,
|
||
|
const std::vector<CompactionInputFiles>& inputs);
|
||
9 years ago
|
|
||
10 years ago
|
static bool IsFullCompaction(VersionStorageInfo* vstorage,
|
||
|
const std::vector<CompactionInputFiles>& inputs);
|
||
11 years ago
|
|
||
8 years ago
|
VersionStorageInfo* input_vstorage_;
|
||
|
|
||
10 years ago
|
const int start_level_; // the lowest level to be compacted
|
||
|
const int output_level_; // levels to which output files are stored
|
||
11 years ago
|
uint64_t max_output_file_size_;
|
||
9 years ago
|
uint64_t max_compaction_bytes_;
|
||
7 years ago
|
uint32_t max_subcompactions_;
|
||
8 years ago
|
const ImmutableCFOptions immutable_cf_options_;
|
||
|
const MutableCFOptions mutable_cf_options_;
|
||
11 years ago
|
Version* input_version_;
|
||
10 years ago
|
VersionEdit edit_;
|
||
|
const int number_levels_;
|
||
11 years ago
|
ColumnFamilyData* cfd_;
|
||
11 years ago
|
Arena arena_; // Arena used to allocate space for file_levels_
|
||
11 years ago
|
|
||
10 years ago
|
const uint32_t output_path_id_;
|
||
11 years ago
|
CompressionType output_compression_;
|
||
6 years ago
|
CompressionOptions output_compression_opts_;
|
||
10 years ago
|
// If true, then the comaction can be done by simply deleting input files.
|
||
10 years ago
|
const bool deletion_compaction_;
|
||
11 years ago
|
|
||
10 years ago
|
// Compaction input files organized by level. Constant after construction
|
||
|
const std::vector<CompactionInputFiles> inputs_;
|
||
11 years ago
|
|
||
11 years ago
|
// A copy of inputs_, organized more closely in memory
|
||
10 years ago
|
autovector<LevelFilesBrief, 2> input_levels_;
|
||
11 years ago
|
|
||
9 years ago
|
// State used to check for number of overlapping grandparent files
|
||
10 years ago
|
// (grandparent == "output_level_ + 1")
|
||
11 years ago
|
std::vector<FileMetaData*> grandparents_;
|
||
10 years ago
|
const double score_; // score that was used to pick this compaction.
|
||
11 years ago
|
|
||
|
// Is this compaction creating a file in the bottom most level?
|
||
10 years ago
|
const bool bottommost_level_;
|
||
11 years ago
|
// Does this compaction include all sst files?
|
||
10 years ago
|
const bool is_full_compaction_;
|
||
11 years ago
|
|
||
11 years ago
|
// Is this compaction requested by the client?
|
||
10 years ago
|
const bool is_manual_compaction_;
|
||
11 years ago
|
|
||
10 years ago
|
// True if we can do trivial move in Universal multi level
|
||
|
// compaction
|
||
|
bool is_trivial_move_;
|
||
|
|
||
10 years ago
|
// Does input compression match the output compression?
|
||
|
bool InputCompressionMatchesOutput() const;
|
||
9 years ago
|
|
||
|
// table properties of output files
|
||
|
TablePropertiesCollection output_table_properties_;
|
||
9 years ago
|
|
||
9 years ago
|
// smallest user keys in compaction
|
||
|
Slice smallest_user_key_;
|
||
|
|
||
9 years ago
|
// largest user keys in compaction
|
||
|
Slice largest_user_key_;
|
||
9 years ago
|
|
||
|
// Reason for compaction
|
||
|
CompactionReason compaction_reason_;
|
||
11 years ago
|
};
|
||
|
|
||
6 years ago
|
// Return sum of sizes of all files in `files`.
|
||
10 years ago
|
extern uint64_t TotalFileSize(const std::vector<FileMetaData*>& files);
|
||
|
|
||
11 years ago
|
} // namespace rocksdb
|