Summary: This change builds on https://github.com/facebook/rocksdb/issues/13540 and https://github.com/facebook/rocksdb/issues/13626 in allowing a CompressionManager / Compressor / Decompressor to use a custom compression algorithm, with a distinct CompressionType. For background, review the API comments on CompressionManager and its CompatibilityName() function. Highlights: * Reserve and name 127 new CompressionTypes that can be used for custom compression algorithms / schemas. In many or most cases I expect the enumerators such as `kCustomCompression8F` to be used in user code rather than casting between integers and CompressionTypes, as I expect the supported custom compression algorithms to be identifiable / enumerable at compile time. * When using these custom compression types, a CompressionManager must use a CompatibilityName() other than the built-in one AND new format_version=7 (see below). * When building new SST files, track the full set of CompressionTypes actually used (usually just one aside from kNoCompression), using our efficient bitset SmallEnumSet, which supports fast iteration over the bits set to 1. Ideally, to support mixed or non-mixed compression algorithms in a file as efficiently as possible, we would know the set of CompressionTypes as SST file open time. * New schema for `TableProperties::compression_name` in format_version=7 to represent the CompressionManager's CompatibilityName(), the set of CompressionTypes used, and potentially more in the future, while keeping the data relatively human-readable. * It would be possible to do this without a new format_version, but then the only way to ensure incompatible versions fail is with an unsupported CompressionType tag, not with a compression_name property. Therefore, (a) I prefer not to put something misleading in the `compression_name` property (a built-in compression name) when there is nuance because of a CompressionManager, and (b) I prefer better, more consistent error messages that refer to either format_version or the CompressionManager's CompatibilityName(), rather than an unrecognized custom CompressionType value (which could have come from various CompressionManagers). * The current configured CompressionManager is passed in to TableReaders so that it (or one it knows about) can be used if it matches the CompatibilityName() used for compression in the SST file. Until the connection with ObjectRegistry is implemented, the only way to read files generated with a particular CompressionManager using custom compression algorithms is to configure it (or a known relative; see FindCompatibleCompressionManager()) in the ColumnFamilyOptions. * Optimized snappy compression with BuiltinDecompressorV2SnappyOnly, to offset some small added overheads with the new tracking. This is essentially an early part of the planned refactoring that will get rid of the old internal compression APIs. * Another small optimization in eliminating an unnecessary key copy in flush (builder.cc). * Fix some handling of named CompressionManagers in CompressionManager::CreateFromString() (problem seen in https://github.com/facebook/rocksdb/issues/13647) Smaller things: * Adds Name() and GetId() functions to Compressor for debugging/logging purposes. (Compressor and Decompressor are not expected to be Customizable because they are only instantiated by a CompressionManager.) * When using an explicit compression_manager, the GetId() of the CompressionManager and the Compressor used to build the file are stored as bonus entries in the compression_options table property. This table property is not parsed anywhere, so it is currently for human reading, but still could be parsed with the new underscore-prefixed bonus entries. IMHO, this is preferable to additional table properties, which would increase memory fragmentation in the TableProperties objects and likely take slightly more CPU on SST open and slightly more storage. * ReleaseWorkingArea() function from protected to public to make wrappers work, because of a quirk in C++ (vs. Java) in which you cannot access protected members of another instance of the same class (sigh) * Added `CompressionManager:: SupportsCompressionType()` for early options sanity checking. Follow-up before release: * Make format_version=7 official / supported * Stress test coverage Sooner than later: * Update tests for RoundRobinManager and SimpleMixedCompressionManager to take advantage of e.g. set of compression types in compression_name property * ObjectRegistry stuff * Refactor away old internal compression APIs Pull Request resolved: https://github.com/facebook/rocksdb/pull/13659 Test Plan: Basic unit test added. ## Performance ### SST write performance ``` SUFFIX=`tty | sed 's|/|_|g'`; for ARGS in "-compression_type=none" "-compression_type=snappy" "-compression_type=zstd" "-compression_type=snappy -verify_compression=1" "-compression_type=zstd -verify_compression=1" "-compression_type=zstd -compression_max_dict_bytes=8180"; do echo $ARGS; (for I in `seq 1 20`; do BIN=/dev/shm/dbbench${SUFFIX}.bin; rm -f $BIN; cp db_bench $BIN; $BIN -db=/dev/shm/dbbench$SUFFIX --benchmarks=fillseq -num=10000000 -compaction_style=2 -fifo_compaction_max_table_files_size_mb=1000 -fifo_compaction_allow_compaction=0 -disable_wal -write_buffer_size=12000000 -format_version=7 $ARGS 2>&1 | grep micros/op; done) | awk '{n++; sum += $5;} END { print int(sum / n); }'; done ``` Ops/sec, Before -> After, both fv=6: -compression_type=none 1894386 -> 1858403 (-2.0%) -compression_type=snappy 1859131 -> 1807469 (-2.8%) -compression_type=zstd 1191428 -> 1214374 (+1.9%) -compression_type=snappy -verify_compression=1 1861819 -> 1858342 (+0.2%) -compression_type=zstd -verify_compression=1 979435 -> 995870 (+1.6%) -compression_type=zstd -compression_max_dict_bytes=8180 905349 -> 940563 (+3.9%) Ops/sec, Before fv=6 -> After fv=7: -compression_type=none 1879365 -> 1836159 (-2.3%) -compression_type=snappy 1865460 -> 1830916 (-1.9%) -compression_type=zstd 1191428 -> 1210260 (+1.6%) -compression_type=snappy -verify_compression=1 1866756 -> 1818989 (-2.6%) -compression_type=zstd -verify_compression=1 982640 -> 997129 (+1.5%) -compression_type=zstd -compression_max_dict_bytes=8180 912608 -> 937248 (+2.7%) ### SST read performance Create DBs ``` for COMP in none snappy zstd; do echo $ARGS; ./db_bench -db=/dev/shm/dbbench-7-$COMP --benchmarks=fillseq,flush -num=10000000 -compaction_style=2 -fifo_compaction_max_table_files_size_mb=1000 -fifo_compaction_allow_compaction=0 -disable_wal -write_buffer_size=12000000 -compression_type=$COMP -format_version=7; done ``` And test ``` for COMP in none snappy zstd none; do echo $COMP; (for I in `seq 1 8`; do ./db_bench -readonly -db=/dev/shm/dbbench -7-$COMP --benchmarks=readrandom -num=10000000 -duration=20 -threads=8 2>&1 | grep micros/op; done ) | awk '{n++; sum += $5;} END { print int(sum / n); }'; done ``` Ops/sec, Before -> After (both fv=6) none 1491732 -> 1500209 (+0.6%) snappy 1157216 -> 1169202 (+1.0%) zstd 695414 -> 703719 (+1.2%) none (again) 1491787 -> 1528789 (+2.4%) Ops/sec, Before fv=6 -> After fv=7: none 1492278 -> 1508668 (+1.1%) snappy 1140769 -> 1152613 (+1.0%) zstd 696437 -> 696511 (+0.0%) none (again) 1500585 -> 1512037 (+0.7%) Overall, I think we can take the read CPU improvement in exchange for the hit (in some cases) on background write CPU Reviewed By: hx235 Differential Revision: D76520739 Pulled By: pdillinger fbshipit-source-id: e73bd72502ff85c8779cba313f26f7d1fd50be3a
211 lines
7.2 KiB
C++
211 lines
7.2 KiB
C++
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
// 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).
|
|
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
#include "proto/gen/db_operation.pb.h"
|
|
#include "rocksdb/file_system.h"
|
|
#include "rocksdb/sst_file_writer.h"
|
|
#include "src/libfuzzer/libfuzzer_macro.h"
|
|
#include "table/table_builder.h"
|
|
#include "table/table_reader.h"
|
|
#include "util.h"
|
|
|
|
using ROCKSDB_NAMESPACE::BytewiseComparator;
|
|
using ROCKSDB_NAMESPACE::Comparator;
|
|
using ROCKSDB_NAMESPACE::EnvOptions;
|
|
using ROCKSDB_NAMESPACE::ExternalSstFileInfo;
|
|
using ROCKSDB_NAMESPACE::FileOptions;
|
|
using ROCKSDB_NAMESPACE::FileSystem;
|
|
using ROCKSDB_NAMESPACE::ImmutableCFOptions;
|
|
using ROCKSDB_NAMESPACE::ImmutableOptions;
|
|
using ROCKSDB_NAMESPACE::InternalIterator;
|
|
using ROCKSDB_NAMESPACE::IOOptions;
|
|
using ROCKSDB_NAMESPACE::kMaxSequenceNumber;
|
|
using ROCKSDB_NAMESPACE::Options;
|
|
using ROCKSDB_NAMESPACE::ParsedInternalKey;
|
|
using ROCKSDB_NAMESPACE::ParseInternalKey;
|
|
using ROCKSDB_NAMESPACE::RandomAccessFileReader;
|
|
using ROCKSDB_NAMESPACE::ReadOptions;
|
|
using ROCKSDB_NAMESPACE::SstFileWriter;
|
|
using ROCKSDB_NAMESPACE::Status;
|
|
using ROCKSDB_NAMESPACE::TableReader;
|
|
using ROCKSDB_NAMESPACE::TableReaderCaller;
|
|
using ROCKSDB_NAMESPACE::TableReaderOptions;
|
|
using ROCKSDB_NAMESPACE::ValueType;
|
|
|
|
// Keys in SST file writer operations must be unique and in ascending order.
|
|
// For each DBOperation generated by the fuzzer, this function is called on
|
|
// it to deduplicate and sort the keys in the DBOperations.
|
|
protobuf_mutator::libfuzzer::PostProcessorRegistration<DBOperations> reg = {
|
|
[](DBOperations* input, unsigned int /* seed */) {
|
|
const Comparator* comparator = BytewiseComparator();
|
|
auto ops = input->mutable_operations();
|
|
|
|
// Make sure begin <= end for DELETE_RANGE.
|
|
for (DBOperation& op : *ops) {
|
|
if (op.type() == OpType::DELETE_RANGE) {
|
|
auto begin = op.key();
|
|
auto end = op.value();
|
|
if (comparator->Compare(begin, end) > 0) {
|
|
std::swap(begin, end);
|
|
op.set_key(begin);
|
|
op.set_value(end);
|
|
}
|
|
}
|
|
}
|
|
|
|
std::sort(ops->begin(), ops->end(),
|
|
[&comparator](const DBOperation& a, const DBOperation& b) {
|
|
return comparator->Compare(a.key(), b.key()) < 0;
|
|
});
|
|
|
|
auto last = std::unique(
|
|
ops->begin(), ops->end(),
|
|
[&comparator](const DBOperation& a, const DBOperation& b) {
|
|
return comparator->Compare(a.key(), b.key()) == 0;
|
|
});
|
|
ops->erase(last, ops->end());
|
|
}};
|
|
|
|
TableReader* NewTableReader(const std::string& sst_file_path,
|
|
const Options& options,
|
|
const EnvOptions& env_options,
|
|
const ImmutableCFOptions& cf_ioptions) {
|
|
// This code block is similar to SstFileReader::Open.
|
|
|
|
uint64_t file_size = 0;
|
|
std::unique_ptr<RandomAccessFileReader> file_reader;
|
|
std::unique_ptr<TableReader> table_reader;
|
|
const auto& fs = options.env->GetFileSystem();
|
|
FileOptions fopts(env_options);
|
|
Status s = options.env->GetFileSize(sst_file_path, &file_size);
|
|
if (s.ok()) {
|
|
s = RandomAccessFileReader::Create(fs, sst_file_path, fopts, &file_reader,
|
|
nullptr);
|
|
}
|
|
if (s.ok()) {
|
|
ImmutableOptions iopts(options, cf_ioptions);
|
|
TableReaderOptions t_opt(iopts, /*prefix_extractor=*/nullptr,
|
|
/*compression_manager=*/nullptr, env_options,
|
|
cf_ioptions.internal_comparator,
|
|
0 /* block_protection_bytes_per_key */);
|
|
t_opt.largest_seqno = kMaxSequenceNumber;
|
|
s = options.table_factory->NewTableReader(t_opt, std::move(file_reader),
|
|
file_size, &table_reader,
|
|
/*prefetch=*/false);
|
|
}
|
|
if (!s.ok()) {
|
|
std::cerr << "Failed to create TableReader for " << sst_file_path << ": "
|
|
<< s.ToString() << std::endl;
|
|
abort();
|
|
}
|
|
return table_reader.release();
|
|
}
|
|
|
|
ValueType ToValueType(OpType op_type) {
|
|
switch (op_type) {
|
|
case OpType::PUT:
|
|
return ValueType::kTypeValue;
|
|
case OpType::MERGE:
|
|
return ValueType::kTypeMerge;
|
|
case OpType::DELETE:
|
|
return ValueType::kTypeDeletion;
|
|
case OpType::DELETE_RANGE:
|
|
return ValueType::kTypeRangeDeletion;
|
|
default:
|
|
std::cerr << "Unknown operation type " << static_cast<int>(op_type)
|
|
<< std::endl;
|
|
abort();
|
|
}
|
|
}
|
|
|
|
// Fuzzes DB operations as input, let SstFileWriter generate a SST file
|
|
// according to the operations, then let TableReader read and check all the
|
|
// key-value pairs from the generated SST file.
|
|
DEFINE_PROTO_FUZZER(DBOperations& input) {
|
|
if (input.operations().empty()) {
|
|
return;
|
|
}
|
|
|
|
std::string sstfile;
|
|
{
|
|
auto fs = FileSystem::Default();
|
|
std::string dir;
|
|
IOOptions opt;
|
|
CHECK_OK(fs->GetTestDirectory(opt, &dir, nullptr));
|
|
sstfile = dir + "/SstFileWriterFuzzer.sst";
|
|
}
|
|
|
|
Options options;
|
|
EnvOptions env_options(options);
|
|
ImmutableCFOptions cf_ioptions(options);
|
|
|
|
// Generate sst file.
|
|
SstFileWriter writer(env_options, options);
|
|
CHECK_OK(writer.Open(sstfile));
|
|
for (const DBOperation& op : input.operations()) {
|
|
switch (op.type()) {
|
|
case OpType::PUT: {
|
|
CHECK_OK(writer.Put(op.key(), op.value()));
|
|
break;
|
|
}
|
|
case OpType::MERGE: {
|
|
CHECK_OK(writer.Merge(op.key(), op.value()));
|
|
break;
|
|
}
|
|
case OpType::DELETE: {
|
|
CHECK_OK(writer.Delete(op.key()));
|
|
break;
|
|
}
|
|
case OpType::DELETE_RANGE: {
|
|
CHECK_OK(writer.DeleteRange(op.key(), op.value()));
|
|
break;
|
|
}
|
|
default: {
|
|
std::cerr << "Unsupported operation" << static_cast<int>(op.type())
|
|
<< std::endl;
|
|
abort();
|
|
}
|
|
}
|
|
}
|
|
ExternalSstFileInfo info;
|
|
CHECK_OK(writer.Finish(&info));
|
|
|
|
// Iterate and verify key-value pairs.
|
|
std::unique_ptr<TableReader> table_reader(
|
|
::NewTableReader(sstfile, options, env_options, cf_ioptions));
|
|
ReadOptions roptions;
|
|
CHECK_OK(table_reader->VerifyChecksum(roptions,
|
|
TableReaderCaller::kUncategorized));
|
|
std::unique_ptr<InternalIterator> it(
|
|
table_reader->NewIterator(roptions, /*prefix_extractor=*/nullptr,
|
|
/*arena=*/nullptr, /*skip_filters=*/true,
|
|
TableReaderCaller::kUncategorized));
|
|
it->SeekToFirst();
|
|
for (const DBOperation& op : input.operations()) {
|
|
if (op.type() == OpType::DELETE_RANGE) {
|
|
// InternalIterator cannot iterate over DELETE_RANGE entries.
|
|
continue;
|
|
}
|
|
CHECK_TRUE(it->Valid());
|
|
ParsedInternalKey ikey;
|
|
CHECK_OK(ParseInternalKey(it->key(), &ikey, /*log_err_key=*/true));
|
|
CHECK_EQ(ikey.user_key.ToString(), op.key());
|
|
CHECK_EQ(ikey.sequence, 0);
|
|
CHECK_EQ(ikey.type, ToValueType(op.type()));
|
|
if (op.type() != OpType::DELETE) {
|
|
CHECK_EQ(op.value(), it->value().ToString());
|
|
}
|
|
it->Next();
|
|
}
|
|
CHECK_TRUE(!it->Valid());
|
|
|
|
// Delete sst file.
|
|
remove(sstfile.c_str());
|
|
}
|