diff --git a/kvrocks.conf b/kvrocks.conf index e6b17dae2ef..2949ef39b75 100644 --- a/kvrocks.conf +++ b/kvrocks.conf @@ -727,6 +727,29 @@ rocksdb.cache_index_and_filter_blocks yes # default snappy rocksdb.compression snappy +# Specify the compression level to use. It trades compression speed +# and ratio, might be useful when tuning for disk space. +# See details: https://github.com/facebook/rocksdb/wiki/Space-Tuning +# For zstd: valid range is from 1 (fastest) to 19 (best ratio), +# For zlib: valid range is from 1 (fastest) to 9 (best ratio), +# For lz4: adjusting the level influences the 'acceleration'. +# RocksDB sets a negative level to indicate acceleration directly, +# with more negative values indicating higher speed and less compression. +# Note: This setting is ignored for compression algorithms like Snappy that +# do not support variable compression levels. +# +# RocksDB Default: +# - zstd: 3 +# - zlib: Z_DEFAULT_COMPRESSION (currently -1) +# - kLZ4: -1 (i.e., `acceleration=1`; see `CompressionOptions::level` doc) +# For all others, RocksDB does not specify a compression level. +# If the compression type doesn't support the setting, it will be a no-op. +# +# Default: 32767 (RocksDB's generic default compression level. Internally +# it'll be translated to the default compression level specific to the +# compression library as mentioned above) +rocksdb.compression_level 32767 + # If non-zero, we perform bigger reads when doing compaction. If you're # running RocksDB on spinning disks, you should set this to at least 2MB. # That way RocksDB's compaction is doing sequential instead of random reads. diff --git a/src/config/config.cc b/src/config/config.cc index db7ee712a99..38de6595aba 100644 --- a/src/config/config.cc +++ b/src/config/config.cc @@ -190,6 +190,7 @@ Config::Config() { {"rocksdb.compression", false, new EnumField(&rocks_db.compression, compression_types, rocksdb::CompressionType::kNoCompression)}, + {"rocksdb.compression_level", true, new IntField(&rocks_db.compression_level, 32767, INT_MIN, INT_MAX)}, {"rocksdb.block_size", true, new IntField(&rocks_db.block_size, 16384, 0, INT_MAX)}, {"rocksdb.max_open_files", false, new IntField(&rocks_db.max_open_files, 8096, -1, INT_MAX)}, {"rocksdb.write_buffer_size", false, new IntField(&rocks_db.write_buffer_size, 64, 0, 4096)}, diff --git a/src/config/config.h b/src/config/config.h index a24c59e03fe..2ea2f553e67 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -199,6 +199,7 @@ struct Config { int level0_stop_writes_trigger; int level0_file_num_compaction_trigger; rocksdb::CompressionType compression; + int compression_level; bool disable_auto_compactions; bool enable_blob_files; int min_blob_size; diff --git a/src/storage/storage.cc b/src/storage/storage.cc index 94a6e64da8d..f42886b9640 100644 --- a/src/storage/storage.cc +++ b/src/storage/storage.cc @@ -165,6 +165,7 @@ rocksdb::Options Storage::InitRocksDBOptions() { options.min_write_buffer_number_to_merge = 2; options.write_buffer_size = config_->rocks_db.write_buffer_size * MiB; options.num_levels = 7; + options.compression_opts.level = config_->rocks_db.compression_level; options.compression_per_level.resize(options.num_levels); // only compress levels >= 2 for (int i = 0; i < options.num_levels; ++i) { diff --git a/tests/cppunit/config_test.cc b/tests/cppunit/config_test.cc index 49e7623bc1a..1c28f0bff65 100644 --- a/tests/cppunit/config_test.cc +++ b/tests/cppunit/config_test.cc @@ -126,6 +126,7 @@ TEST(Config, GetAndSet) { {"rocksdb.subkey_block_cache_size", "100"}, {"rocksdb.row_cache_size", "100"}, {"rocksdb.rate_limiter_auto_tuned", "yes"}, + {"rocksdb.compression_level", "32767"}, }; for (const auto &iter : immutable_cases) { s = config.Set(nullptr, iter.first, iter.second);