From 05886f8dcba52a5ac7a206eb6177eb0dc47ada00 Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Sun, 26 Oct 2025 17:30:42 +0000 Subject: [PATCH] feat: Add option to control WAL compression Also enables zstd compression by default --- conduwuit-example.toml | 15 +++++++++++++++ src/core/config/mod.rs | 19 +++++++++++++++++++ src/database/engine/db_opts.rs | 20 ++++++++++++++++++-- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/conduwuit-example.toml b/conduwuit-example.toml index 4b50c067..aebe3be9 100644 --- a/conduwuit-example.toml +++ b/conduwuit-example.toml @@ -957,6 +957,21 @@ # #rocksdb_bottommost_compression = true +# Compression algorithm for RocksDB's Write-Ahead-Log (WAL). +# +# At present, only ZSTD compression is supported by RocksDB for WAL +# compression. Enabling this can reduce WAL size at the expense of some +# CPU usage during writes. +# +# The options are: +# - "none" = No compression +# - "zstd" = ZSTD compression +# +# For more information on WAL compression, see: +# https://github.com/facebook/rocksdb/wiki/WAL-Compression +# +#rocksdb_wal_compression = "zstd" + # Database recovery mode (for RocksDB WAL corruption). # # Use this option when the server reports corruption and refuses to start. diff --git a/src/core/config/mod.rs b/src/core/config/mod.rs index a5fdc320..fbb29357 100644 --- a/src/core/config/mod.rs +++ b/src/core/config/mod.rs @@ -1128,6 +1128,23 @@ pub struct Config { #[serde(default = "true_fn")] pub rocksdb_bottommost_compression: bool, + /// Compression algorithm for RocksDB's Write-Ahead-Log (WAL). + /// + /// At present, only ZSTD compression is supported by RocksDB for WAL + /// compression. Enabling this can reduce WAL size at the expense of some + /// CPU usage during writes. + /// + /// The options are: + /// - "none" = No compression + /// - "zstd" = ZSTD compression + /// + /// For more information on WAL compression, see: + /// https://github.com/facebook/rocksdb/wiki/WAL-Compression + /// + /// default: "zstd" + #[serde(default = "default_rocksdb_wal_compression")] + pub rocksdb_wal_compression: String, + /// Database recovery mode (for RocksDB WAL corruption). /// /// Use this option when the server reports corruption and refuses to start. @@ -2454,6 +2471,8 @@ fn default_rocksdb_compression_algo() -> String { .to_owned() } +fn default_rocksdb_wal_compression() -> String { "zstd".to_owned() } + /// Default RocksDB compression level is 32767, which is internally read by /// RocksDB as the default magic number and translated to the library's default /// compression level as they all differ. See their `kDefaultCompressionLevel`. diff --git a/src/database/engine/db_opts.rs b/src/database/engine/db_opts.rs index 91620ec4..d6092cbf 100644 --- a/src/database/engine/db_opts.rs +++ b/src/database/engine/db_opts.rs @@ -1,7 +1,9 @@ use std::{cmp, convert::TryFrom}; -use conduwuit::{Config, Result, utils}; -use rocksdb::{Cache, DBRecoveryMode, Env, LogLevel, Options, statistics::StatsLevel}; +use conduwuit::{Config, Result, utils, warn}; +use rocksdb::{ + Cache, DBCompressionType, DBRecoveryMode, Env, LogLevel, Options, statistics::StatsLevel, +}; use super::{cf_opts::cache_size_f64, logger::handle as handle_log}; @@ -58,6 +60,20 @@ pub(crate) fn db_options(config: &Config, env: &Env, row_cache: &Cache) -> Resul opts.set_max_total_wal_size(1024 * 1024 * 512); opts.set_writable_file_max_buffer_size(1024 * 1024 * 2); + // WAL compression + let wal_compression = match config.rocksdb_wal_compression.as_ref() { + | "zstd" => DBCompressionType::Zstd, + | "none" => DBCompressionType::None, + | value => { + warn!( + "Invalid rocksdb_wal_compression value '{value}'. Supported values are 'none' \ + or 'zstd'. Defaulting to 'none'." + ); + DBCompressionType::None + }, + }; + opts.set_wal_compression_type(wal_compression); + // Misc opts.set_disable_auto_compactions(!config.rocksdb_compaction); opts.create_missing_column_families(true);