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.
313 lines
14 KiB
313 lines
14 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 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
|
||
9 years ago
|
|
||
10 years ago
|
#include <memory>
|
||
|
#include <set>
|
||
9 years ago
|
#include <string>
|
||
10 years ago
|
#include <unordered_set>
|
||
9 years ago
|
#include <vector>
|
||
10 years ago
|
|
||
6 years ago
|
#include "db/compaction/compaction.h"
|
||
9 years ago
|
#include "db/version_set.h"
|
||
8 years ago
|
#include "options/cf_options.h"
|
||
11 years ago
|
#include "rocksdb/env.h"
|
||
9 years ago
|
#include "rocksdb/options.h"
|
||
|
#include "rocksdb/status.h"
|
||
11 years ago
|
|
||
|
namespace rocksdb {
|
||
|
|
||
6 years ago
|
// The file contains an abstract class CompactionPicker, and its two
|
||
|
// sub-classes LevelCompactionPicker and NullCompactionPicker, as
|
||
|
// well as some helper functions used by them.
|
||
|
|
||
11 years ago
|
class LogBuffer;
|
||
11 years ago
|
class Compaction;
|
||
10 years ago
|
class VersionStorageInfo;
|
||
10 years ago
|
struct CompactionInputFiles;
|
||
11 years ago
|
|
||
6 years ago
|
// An abstract class to pick compactions from an existing LSM-tree.
|
||
|
//
|
||
|
// Each compaction style inherits the class and implement the
|
||
|
// interface to form automatic compactions. If NeedCompaction() is true,
|
||
|
// then call PickCompaction() to find what files need to be compacted
|
||
|
// and where to put the output files.
|
||
|
//
|
||
|
// Non-virtual functions CompactRange() and CompactFiles() are used to
|
||
|
// pick files to compact based on users' DB::CompactRange() and
|
||
|
// DB::CompactFiles() requests, respectively. There is little
|
||
|
// compaction style specific logic for them.
|
||
11 years ago
|
class CompactionPicker {
|
||
|
public:
|
||
10 years ago
|
CompactionPicker(const ImmutableCFOptions& ioptions,
|
||
|
const InternalKeyComparator* icmp);
|
||
11 years ago
|
virtual ~CompactionPicker();
|
||
|
|
||
|
// Pick level and inputs for a new compaction.
|
||
|
// Returns nullptr if there is no compaction to be done.
|
||
|
// Otherwise returns a pointer to a heap-allocated object that
|
||
|
// describes the compaction. Caller should delete the result.
|
||
10 years ago
|
virtual Compaction* PickCompaction(const std::string& cf_name,
|
||
|
const MutableCFOptions& mutable_cf_options,
|
||
|
VersionStorageInfo* vstorage,
|
||
|
LogBuffer* log_buffer) = 0;
|
||
11 years ago
|
|
||
|
// Return a compaction object for compacting the range [begin,end] in
|
||
|
// the specified level. Returns nullptr if there is nothing in that
|
||
|
// level that overlaps the specified range. Caller should delete
|
||
|
// the result.
|
||
|
//
|
||
|
// The returned Compaction might not include the whole requested range.
|
||
|
// In that case, compaction_end will be set to the next key that needs
|
||
|
// compacting. In case the compaction will compact the whole range,
|
||
|
// compaction_end will be set to nullptr.
|
||
|
// Client is responsible for compaction_end storage -- when called,
|
||
|
// *compaction_end should point to valid InternalKey!
|
||
10 years ago
|
virtual Compaction* CompactRange(
|
||
10 years ago
|
const std::string& cf_name, const MutableCFOptions& mutable_cf_options,
|
||
|
VersionStorageInfo* vstorage, int input_level, int output_level,
|
||
6 years ago
|
const CompactRangeOptions& compact_range_options,
|
||
7 years ago
|
const InternalKey* begin, const InternalKey* end,
|
||
6 years ago
|
InternalKey** compaction_end, bool* manual_conflict,
|
||
|
uint64_t max_file_num_to_ignore);
|
||
11 years ago
|
|
||
10 years ago
|
// The maximum allowed output level. Default value is NumberLevels() - 1.
|
||
9 years ago
|
virtual int MaxOutputLevel() const { return NumberLevels() - 1; }
|
||
10 years ago
|
|
||
10 years ago
|
virtual bool NeedsCompaction(const VersionStorageInfo* vstorage) const = 0;
|
||
10 years ago
|
|
||
9 years ago
|
// Sanitize the input set of compaction input files.
|
||
|
// When the input parameters do not describe a valid compaction, the
|
||
|
// function will try to fix the input_files by adding necessary
|
||
|
// files. If it's not possible to conver an invalid input_files
|
||
|
// into a valid one by adding more files, the function will return a
|
||
|
// non-ok status with specific reason.
|
||
10 years ago
|
#ifndef ROCKSDB_LITE
|
||
9 years ago
|
Status SanitizeCompactionInputFiles(std::unordered_set<uint64_t>* input_files,
|
||
|
const ColumnFamilyMetaData& cf_meta,
|
||
|
const int output_level) const;
|
||
10 years ago
|
#endif // ROCKSDB_LITE
|
||
10 years ago
|
|
||
11 years ago
|
// Free up the files that participated in a compaction
|
||
9 years ago
|
//
|
||
|
// Requirement: DB mutex held
|
||
11 years ago
|
void ReleaseCompactionFiles(Compaction* c, Status status);
|
||
|
|
||
10 years ago
|
// Returns true if any one of the specified files are being compacted
|
||
8 years ago
|
bool AreFilesInCompaction(const std::vector<FileMetaData*>& files);
|
||
10 years ago
|
|
||
9 years ago
|
// Takes a list of CompactionInputFiles and returns a (manual) Compaction
|
||
|
// object.
|
||
7 years ago
|
//
|
||
|
// Caller must provide a set of input files that has been passed through
|
||
|
// `SanitizeCompactionInputFiles` earlier. The lock should not be released
|
||
|
// between that call and this one.
|
||
8 years ago
|
Compaction* CompactFiles(const CompactionOptions& compact_options,
|
||
|
const std::vector<CompactionInputFiles>& input_files,
|
||
|
int output_level, VersionStorageInfo* vstorage,
|
||
|
const MutableCFOptions& mutable_cf_options,
|
||
|
uint32_t output_path_id);
|
||
10 years ago
|
|
||
|
// Converts a set of compaction input file numbers into
|
||
|
// a list of CompactionInputFiles.
|
||
|
Status GetCompactionInputsFromFileNumbers(
|
||
10 years ago
|
std::vector<CompactionInputFiles>* input_files,
|
||
10 years ago
|
std::unordered_set<uint64_t>* input_set,
|
||
|
const VersionStorageInfo* vstorage,
|
||
|
const CompactionOptions& compact_options) const;
|
||
|
|
||
9 years ago
|
// Is there currently a compaction involving level 0 taking place
|
||
|
bool IsLevel0CompactionInProgress() const {
|
||
|
return !level0_compactions_in_progress_.empty();
|
||
|
}
|
||
|
|
||
8 years ago
|
// Return true if the passed key range overlap with a compaction output
|
||
|
// that is currently running.
|
||
|
bool RangeOverlapWithCompaction(const Slice& smallest_user_key,
|
||
|
const Slice& largest_user_key,
|
||
|
int level) const;
|
||
|
|
||
11 years ago
|
// Stores the minimal range that covers all entries in inputs in
|
||
|
// *smallest, *largest.
|
||
|
// REQUIRES: inputs is not empty
|
||
9 years ago
|
void GetRange(const CompactionInputFiles& inputs, InternalKey* smallest,
|
||
8 years ago
|
InternalKey* largest) const;
|
||
11 years ago
|
|
||
|
// Stores the minimal range that covers all entries in inputs1 and inputs2
|
||
|
// in *smallest, *largest.
|
||
|
// REQUIRES: inputs is not empty
|
||
10 years ago
|
void GetRange(const CompactionInputFiles& inputs1,
|
||
9 years ago
|
const CompactionInputFiles& inputs2, InternalKey* smallest,
|
||
8 years ago
|
InternalKey* largest) const;
|
||
|
|
||
|
// Stores the minimal range that covers all entries in inputs
|
||
|
// in *smallest, *largest.
|
||
|
// REQUIRES: inputs is not empty (at least on entry have one file)
|
||
|
void GetRange(const std::vector<CompactionInputFiles>& inputs,
|
||
|
InternalKey* smallest, InternalKey* largest) const;
|
||
11 years ago
|
|
||
8 years ago
|
int NumberLevels() const { return ioptions_.num_levels; }
|
||
|
|
||
11 years ago
|
// Add more files to the inputs on "level" to make sure that
|
||
|
// no newer version of a key is compacted to "level+1" while leaving an older
|
||
|
// version in a "level". Otherwise, any Get() will search "level" first,
|
||
|
// and will likely return an old/stale value for the key, since it always
|
||
|
// searches in increasing order of level to find the value. This could
|
||
|
// also scramble the order of merge operands. This function should be
|
||
|
// called any time a new Compaction is created, and its inputs_[0] are
|
||
|
// populated.
|
||
|
//
|
||
|
// Will return false if it is impossible to apply this compaction.
|
||
8 years ago
|
bool ExpandInputsToCleanCut(const std::string& cf_name,
|
||
10 years ago
|
VersionStorageInfo* vstorage,
|
||
6 years ago
|
CompactionInputFiles* inputs,
|
||
|
InternalKey** next_smallest = nullptr);
|
||
11 years ago
|
|
||
|
// Returns true if any one of the parent files are being compacted
|
||
8 years ago
|
bool IsRangeInCompaction(VersionStorageInfo* vstorage,
|
||
|
const InternalKey* smallest,
|
||
|
const InternalKey* largest, int level, int* index);
|
||
11 years ago
|
|
||
8 years ago
|
// Returns true if the key range that `inputs` files cover overlap with the
|
||
|
// key range of a currently running compaction.
|
||
|
bool FilesRangeOverlapWithCompaction(
|
||
|
const std::vector<CompactionInputFiles>& inputs, int level) const;
|
||
|
|
||
10 years ago
|
bool SetupOtherInputs(const std::string& cf_name,
|
||
10 years ago
|
const MutableCFOptions& mutable_cf_options,
|
||
10 years ago
|
VersionStorageInfo* vstorage,
|
||
|
CompactionInputFiles* inputs,
|
||
|
CompactionInputFiles* output_level_inputs,
|
||
|
int* parent_index, int base_index);
|
||
|
|
||
|
void GetGrandparents(VersionStorageInfo* vstorage,
|
||
|
const CompactionInputFiles& inputs,
|
||
|
const CompactionInputFiles& output_level_inputs,
|
||
|
std::vector<FileMetaData*>* grandparents);
|
||
10 years ago
|
|
||
7 years ago
|
void PickFilesMarkedForCompaction(const std::string& cf_name,
|
||
|
VersionStorageInfo* vstorage,
|
||
|
int* start_level, int* output_level,
|
||
|
CompactionInputFiles* start_level_inputs);
|
||
|
|
||
|
bool GetOverlappingL0Files(VersionStorageInfo* vstorage,
|
||
|
CompactionInputFiles* start_level_inputs,
|
||
|
int output_level, int* parent_index);
|
||
|
|
||
8 years ago
|
// Register this compaction in the set of running compactions
|
||
|
void RegisterCompaction(Compaction* c);
|
||
|
|
||
|
// Remove this compaction from the set of running compactions
|
||
|
void UnregisterCompaction(Compaction* c);
|
||
|
|
||
|
std::set<Compaction*>* level0_compactions_in_progress() {
|
||
|
return &level0_compactions_in_progress_;
|
||
|
}
|
||
|
std::unordered_set<Compaction*>* compactions_in_progress() {
|
||
|
return &compactions_in_progress_;
|
||
|
}
|
||
|
|
||
|
protected:
|
||
10 years ago
|
const ImmutableCFOptions& ioptions_;
|
||
11 years ago
|
|
||
9 years ago
|
// A helper function to SanitizeCompactionInputFiles() that
|
||
|
// sanitizes "input_files" by adding necessary files.
|
||
10 years ago
|
#ifndef ROCKSDB_LITE
|
||
10 years ago
|
virtual Status SanitizeCompactionInputFilesForAllLevels(
|
||
|
std::unordered_set<uint64_t>* input_files,
|
||
9 years ago
|
const ColumnFamilyMetaData& cf_meta, const int output_level) const;
|
||
10 years ago
|
#endif // ROCKSDB_LITE
|
||
10 years ago
|
|
||
10 years ago
|
// Keeps track of all compactions that are running on Level0.
|
||
8 years ago
|
// Protected by DB mutex
|
||
10 years ago
|
std::set<Compaction*> level0_compactions_in_progress_;
|
||
11 years ago
|
|
||
8 years ago
|
// Keeps track of all compactions that are running.
|
||
|
// Protected by DB mutex
|
||
|
std::unordered_set<Compaction*> compactions_in_progress_;
|
||
|
|
||
11 years ago
|
const InternalKeyComparator* const icmp_;
|
||
|
};
|
||
|
|
||
10 years ago
|
#ifndef ROCKSDB_LITE
|
||
6 years ago
|
// A dummy compaction that never triggers any automatic
|
||
|
// compaction.
|
||
10 years ago
|
class NullCompactionPicker : public CompactionPicker {
|
||
|
public:
|
||
|
NullCompactionPicker(const ImmutableCFOptions& ioptions,
|
||
9 years ago
|
const InternalKeyComparator* icmp)
|
||
|
: CompactionPicker(ioptions, icmp) {}
|
||
10 years ago
|
virtual ~NullCompactionPicker() {}
|
||
|
|
||
|
// Always return "nullptr"
|
||
7 years ago
|
Compaction* PickCompaction(const std::string& /*cf_name*/,
|
||
|
const MutableCFOptions& /*mutable_cf_options*/,
|
||
|
VersionStorageInfo* /*vstorage*/,
|
||
|
LogBuffer* /*log_buffer*/) override {
|
||
10 years ago
|
return nullptr;
|
||
|
}
|
||
|
|
||
|
// Always return "nullptr"
|
||
7 years ago
|
Compaction* CompactRange(const std::string& /*cf_name*/,
|
||
|
const MutableCFOptions& /*mutable_cf_options*/,
|
||
|
VersionStorageInfo* /*vstorage*/,
|
||
|
int /*input_level*/, int /*output_level*/,
|
||
6 years ago
|
const CompactRangeOptions& /*compact_range_options*/,
|
||
7 years ago
|
const InternalKey* /*begin*/,
|
||
|
const InternalKey* /*end*/,
|
||
|
InternalKey** /*compaction_end*/,
|
||
6 years ago
|
bool* /*manual_conflict*/,
|
||
|
uint64_t /*max_file_num_to_ignore*/) override {
|
||
10 years ago
|
return nullptr;
|
||
|
}
|
||
|
|
||
10 years ago
|
// Always returns false.
|
||
9 years ago
|
virtual bool NeedsCompaction(
|
||
7 years ago
|
const VersionStorageInfo* /*vstorage*/) const override {
|
||
10 years ago
|
return false;
|
||
|
}
|
||
11 years ago
|
};
|
||
10 years ago
|
#endif // !ROCKSDB_LITE
|
||
11 years ago
|
|
||
6 years ago
|
// Attempts to find an intra L0 compaction conforming to the given parameters.
|
||
|
//
|
||
|
// @param level_files Metadata for L0 files.
|
||
|
// @param min_files_to_compact Minimum number of files required to
|
||
|
// do the compaction.
|
||
|
// @param max_compact_bytes_per_del_file Maximum average size in bytes per
|
||
|
// file that is going to get deleted by
|
||
|
// the compaction.
|
||
|
// @param max_compaction_bytes Maximum total size in bytes (in terms
|
||
|
// of compensated file size) for files
|
||
|
// to be compacted.
|
||
|
// @param [out] comp_inputs If a compaction was found, will be
|
||
|
// initialized with corresponding input
|
||
|
// files. Cannot be nullptr.
|
||
|
//
|
||
|
// @return true iff compaction was found.
|
||
6 years ago
|
bool FindIntraL0Compaction(const std::vector<FileMetaData*>& level_files,
|
||
|
size_t min_files_to_compact,
|
||
|
uint64_t max_compact_bytes_per_del_file,
|
||
6 years ago
|
uint64_t max_compaction_bytes,
|
||
6 years ago
|
CompactionInputFiles* comp_inputs);
|
||
|
|
||
10 years ago
|
CompressionType GetCompressionType(const ImmutableCFOptions& ioptions,
|
||
9 years ago
|
const VersionStorageInfo* vstorage,
|
||
9 years ago
|
const MutableCFOptions& mutable_cf_options,
|
||
10 years ago
|
int level, int base_level,
|
||
|
const bool enable_compression = true);
|
||
|
|
||
7 years ago
|
CompressionOptions GetCompressionOptions(const ImmutableCFOptions& ioptions,
|
||
|
const VersionStorageInfo* vstorage,
|
||
|
int level,
|
||
|
const bool enable_compression = true);
|
||
|
|
||
11 years ago
|
} // namespace rocksdb
|