Skip to content

Commit

Permalink
Add pluggable compression
Browse files Browse the repository at this point in the history
  • Loading branch information
lucagiac81 committed Oct 6, 2023
1 parent 374251f commit ce06833
Show file tree
Hide file tree
Showing 33 changed files with 995 additions and 94 deletions.
2 changes: 1 addition & 1 deletion db/blob/blob_file_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

#include "rocksdb/advanced_options.h"
#include "rocksdb/compression_type.h"
#include "rocksdb/compressor.h"
#include "rocksdb/env.h"
#include "rocksdb/rocksdb_namespace.h"
#include "rocksdb/types.h"
#include "util/compressor.h"

namespace ROCKSDB_NAMESPACE {

Expand Down
1 change: 0 additions & 1 deletion db/blob/blob_file_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "rocksdb/compression_type.h"
#include "rocksdb/rocksdb_namespace.h"
#include "util/autovector.h"
#include "util/compressor.h"

namespace ROCKSDB_NAMESPACE {

Expand Down
2 changes: 1 addition & 1 deletion db/blob/blob_read_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <cinttypes>

#include "rocksdb/compression_type.h"
#include "rocksdb/compressor.h"
#include "rocksdb/slice.h"
#include "rocksdb/status.h"
#include "util/autovector.h"
#include "util/compressor.h"

namespace ROCKSDB_NAMESPACE {

Expand Down
2 changes: 1 addition & 1 deletion db/blob/blob_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include "db/blob/blob_file_cache.h"
#include "db/blob/blob_read_request.h"
#include "rocksdb/cache.h"
#include "rocksdb/compressor.h"
#include "rocksdb/rocksdb_namespace.h"
#include "table/block_based/cachable_entry.h"
#include "util/autovector.h"
#include "util/compressor.h"

namespace ROCKSDB_NAMESPACE {

Expand Down
2 changes: 1 addition & 1 deletion db/compact_files_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

#include "db/db_impl/db_impl.h"
#include "port/port.h"
#include "rocksdb/compressor.h"
#include "rocksdb/db.h"
#include "rocksdb/env.h"
#include "test_util/sync_point.h"
#include "test_util/testharness.h"
#include "util/cast_util.h"
#include "util/compressor.h"
#include "util/string_util.h"

namespace ROCKSDB_NAMESPACE {
Expand Down
2 changes: 1 addition & 1 deletion db/db_basic_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "db/db_test_util.h"
#include "options/options_helper.h"
#include "port/stack_trace.h"
#include "rocksdb/compressor.h"
#include "rocksdb/filter_policy.h"
#include "rocksdb/flush_block_policy.h"
#include "rocksdb/merge_operator.h"
Expand All @@ -21,7 +22,6 @@
#include "table/block_based/block_based_table_reader.h"
#include "table/block_based/block_builder.h"
#include "test_util/sync_point.h"
#include "util/compressor.h"
#include "util/file_checksum_helper.h"
#include "util/random.h"
#include "utilities/counted_fs.h"
Expand Down
2 changes: 1 addition & 1 deletion db/db_block_cache_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
#include "db/db_test_util.h"
#include "env/unique_id_gen.h"
#include "port/stack_trace.h"
#include "rocksdb/compressor.h"
#include "rocksdb/persistent_cache.h"
#include "rocksdb/statistics.h"
#include "rocksdb/table.h"
#include "rocksdb/table_properties.h"
#include "table/block_based/block_based_table_reader.h"
#include "table/unique_id_impl.h"
#include "util/compressor.h"
#include "util/defer.h"
#include "util/hash.h"
#include "util/math.h"
Expand Down
23 changes: 22 additions & 1 deletion db/db_options_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,8 @@ TEST_F(DBOptionsTest, ChangeCompression) {
compression_used = CompressionType::kLZ4Compression;
compacted = false;
ASSERT_OK(dbfull()->SetOptions(
{{"bottommost_compression", "kSnappyCompression"},
{{"bottommost_compressor", "nullptr"},
{"bottommost_compression", "kSnappyCompression"},
{"bottommost_compression_opts", "0:6:0:0:4:true"}}));
ASSERT_OK(Put("foo", "foofoofoo"));
ASSERT_OK(Put("bar", "foofoofoo"));
Expand All @@ -1378,6 +1379,26 @@ TEST_F(DBOptionsTest, ChangeCompression) {
ASSERT_EQ(32767, compression_opt_used.level);
// Right now parallel_level is not yet allowed to be changed.

if (!Zlib_Supported()) {
return;
}
compression_used = CompressionType::kLZ4Compression;
compacted = false;
ASSERT_OK(dbfull()->SetOptions(
{{"bottommost_compressor", "nullptr"},
{"bottommost_compression", "kZlibCompression"},
{"bottommost_compression_opts", "0:6:0:0:4:true"}}));
ASSERT_OK(Put("foo", "foofoofoo"));
ASSERT_OK(Put("bar", "foofoofoo"));
ASSERT_OK(Flush());
ASSERT_OK(Put("foo", "foofoofoo"));
ASSERT_OK(Put("bar", "foofoofoo"));
ASSERT_OK(Flush());
ASSERT_OK(dbfull()->TEST_WaitForCompact());
ASSERT_TRUE(compacted);
ASSERT_EQ(CompressionType::kZlibCompression, compression_used);
ASSERT_EQ(6, compression_opt_used.level);

SyncPoint::GetInstance()->DisableProcessing();
}

Expand Down
2 changes: 1 addition & 1 deletion db/db_test2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "options/options_helper.h"
#include "port/port.h"
#include "port/stack_trace.h"
#include "rocksdb/compressor.h"
#include "rocksdb/experimental.h"
#include "rocksdb/iostats_context.h"
#include "rocksdb/persistent_cache.h"
Expand All @@ -26,7 +27,6 @@
#include "rocksdb/utilities/replayer.h"
#include "rocksdb/wal_filter.h"
#include "test_util/testutil.h"
#include "util/compressor.h"
#include "util/random.h"
#include "utilities/fault_injection_env.h"

Expand Down
27 changes: 27 additions & 0 deletions include/rocksdb/advanced_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace ROCKSDB_NAMESPACE {

class Compressor;
class Slice;
class SliceTransform;
class TablePropertiesCollectorFactory;
Expand Down Expand Up @@ -572,6 +573,19 @@ struct AdvancedColumnFamilyOptions {
// Dynamically changeable through SetOptions() API
std::vector<CompressionType> compression_per_level;

// Similar to compression_per_level, but the algorithms are encapsulated in
// Compressor objects. This adds the ability to select custom compressors,
// beyond the built-in ones provided through CompressionType.
//
// If compressor_per_level is specified (not empty), it overrides
// compression_per_level.
//
// If compressor_per_level is not specified (empty),
// compression_per_level is applied as described for that option.
//
// Default: empty
std::vector<std::shared_ptr<Compressor>> compressor_per_level;

// Number of levels for this database
int num_levels = 7;

Expand Down Expand Up @@ -1070,6 +1084,19 @@ struct AdvancedColumnFamilyOptions {
// Dynamically changeable through the SetOptions() API
CompressionType blob_compression_type = kNoCompression;

// Similar to blob_compression_type, but the algorithm is encapsulated in a
// Compressor class. This adds the ability to select plugin compressors,
// beyond the built-in ones provided through CompressionType.
//
// If blob_compressor is specified (not null), it overrides
// blob_compression_type.
//
// If blob_compressor is not specified (null), blob_compression_type is
// applied.
//
// Default: nullptr
std::shared_ptr<Compressor> blob_compressor = nullptr;

// Enables garbage collection of blobs. Blob GC is performed as part of
// compaction. Valid blobs residing in blob files older than a cutoff get
// relocated to new files as they are encountered during compaction, which
Expand Down
4 changes: 4 additions & 0 deletions include/rocksdb/compression_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ enum CompressionType : unsigned char {
// eventually remove the option from the public API.
kZSTDNotFinalCompression = 0x40,

// Used as block compression type identifier when using a non built-in
// compressor.
kPluginCompression = 0x41,

// kDisableCompressionOption is used to disable some compression options.
kDisableCompressionOption = 0xff,
};
Expand Down
4 changes: 3 additions & 1 deletion util/compressor.h → include/rocksdb/compressor.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ class Compressor : public Customizable {
static std::vector<std::string> GetDictSupported();

// Get the numeric type associated with this compressor
virtual CompressionType GetCompressionType() const = 0;
virtual CompressionType GetCompressionType() const {
return kPluginCompression;
};

// Whether the compressor is supported.
// For example, a compressor can implement this method to verify its
Expand Down
43 changes: 43 additions & 0 deletions include/rocksdb/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@ struct ColumnFamilyOptions : public AdvancedColumnFamilyOptions {
// Dynamically changeable through SetOptions() API
CompressionType compression;

// Similar to compression, but the algorithm is encapsulated in a Compressor
// class. This adds the ability to select plugin compressors, beyond the
// built-in ones provided through CompressionType.
//
// If compressor is specified (not null), it overrides
// compression/compression_opts (the compressor includes values for its
// options).
//
// If compressor is not specified (null), compression/compression_opts are
// applied as described for those options.
//
// Default: nullptr
std::shared_ptr<Compressor> compressor = nullptr;

// Compression algorithm that will be used for the bottommost level that
// contain files. The behavior for num_levels = 1 is not well defined.
// Right now, with num_levels = 1, all compaction outputs will use
Expand All @@ -222,6 +236,22 @@ struct ColumnFamilyOptions : public AdvancedColumnFamilyOptions {
// Default: kDisableCompressionOption (Disabled)
CompressionType bottommost_compression = kDisableCompressionOption;

// Similar to bottommost_compression, but the algorithm is encapsulated in a
// Compressor object. This adds the ability to select custom compressors,
// beyond the built-in ones provided through CompressionType.
//
// If bottommost_compressor is specified (not null), it overrides
// bottommost_compression/compression_opts/bottommost_compression_opts (the
// compressor includes values for its options).
//
// If bottommost_compressor is not specified (null),
// bottommost_compression/compression_opts/bottommost_compression_opts are
// applied as described for those options.
//
// Default: nullptr (equivalent to bottommost_compression =
// kDisableCompressionOption)
std::shared_ptr<Compressor> bottommost_compressor = nullptr;

// different options for compression algorithms used by bottommost_compression
// if it is enabled. To enable it, please see the definition of
// CompressionOptions. Behavior for num_levels = 1 is the same as
Expand Down Expand Up @@ -1877,6 +1907,18 @@ struct CompactionOptions {
// according to the `ColumnFamilyOptions`, taking into account the output
// level if `compression_per_level` is specified.
CompressionType compression;

// Similar to compression, but the algorithm is encapsulated in a Compressor
// class. This adds the ability to select plugin compressors, beyond the
// built-in ones provided through CompressionType.
//
// If compressor is specified (not nullptr), it overrides compression.
//
// If compressor is not specified (nullptr), compression is applied.
//
// Default: nullptr
std::shared_ptr<Compressor> compressor;

// Compaction will create files of size `output_file_size_limit`.
// Default: MAX, which means that compaction will create a single file
uint64_t output_file_size_limit;
Expand All @@ -1885,6 +1927,7 @@ struct CompactionOptions {

CompactionOptions()
: compression(kSnappyCompression),
compressor(nullptr),
output_file_size_limit(std::numeric_limits<uint64_t>::max()),
max_subcompactions(0) {}
};
Expand Down
Loading

0 comments on commit ce06833

Please sign in to comment.