|
| 1 | +//! RocksDB configuration presets for different use cases |
| 2 | +//! |
| 3 | +//! This module provides pre-configured RocksDB option sets optimized for different |
| 4 | +//! deployment scenarios. Based on Issue #681 and community testing. |
| 5 | +
|
| 6 | +use rocksdb::Options; |
| 7 | +use std::str::FromStr; |
| 8 | + |
| 9 | +/// Available RocksDB configuration presets |
| 10 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 11 | +pub enum RocksDbPreset { |
| 12 | + /// Default configuration - balanced for general use on SSD/NVMe |
| 13 | + /// - 64MB write buffer |
| 14 | + /// - Standard compression |
| 15 | + /// - Optimized for fast storage |
| 16 | + #[default] |
| 17 | + Default, |
| 18 | + |
| 19 | + /// Archive configuration - optimized for HDD storage |
| 20 | + /// - 256MB write buffer (4x default) |
| 21 | + /// - Aggressive compression (LZ4 + ZSTD) |
| 22 | + /// - BlobDB enabled for large values |
| 23 | + /// - Rate limiting to prevent I/O spikes |
| 24 | + /// - Optimized for sequential writes and reduced write amplification |
| 25 | + /// |
| 26 | + /// Based on Callidon's configuration from Issue #681. |
| 27 | + /// Recommended for archival nodes on HDD storage. |
| 28 | + Archive, |
| 29 | +} |
| 30 | + |
| 31 | +impl FromStr for RocksDbPreset { |
| 32 | + type Err = String; |
| 33 | + |
| 34 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 35 | + match s.to_lowercase().as_str() { |
| 36 | + "default" => Ok(Self::Default), |
| 37 | + "archive" => Ok(Self::Archive), |
| 38 | + _ => Err(format!("Unknown RocksDB preset: '{}'. Valid options: default, archive", s)), |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl std::fmt::Display for RocksDbPreset { |
| 44 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 45 | + match self { |
| 46 | + Self::Default => write!(f, "default"), |
| 47 | + Self::Archive => write!(f, "archive"), |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl RocksDbPreset { |
| 53 | + /// Apply the preset configuration to RocksDB options |
| 54 | + /// |
| 55 | + /// # Arguments |
| 56 | + /// * `opts` - RocksDB options to configure |
| 57 | + /// * `parallelism` - Number of background threads |
| 58 | + /// * `mem_budget` - Memory budget (only used for Default preset, Archive uses fixed 256MB) |
| 59 | + pub fn apply_to_options(&self, opts: &mut Options, parallelism: usize, mem_budget: usize) { |
| 60 | + match self { |
| 61 | + Self::Default => self.apply_default(opts, parallelism, mem_budget), |
| 62 | + Self::Archive => self.apply_archive(opts, parallelism), |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// Apply default preset configuration |
| 67 | + fn apply_default(&self, opts: &mut Options, parallelism: usize, mem_budget: usize) { |
| 68 | + if parallelism > 1 { |
| 69 | + opts.increase_parallelism(parallelism as i32); |
| 70 | + } |
| 71 | + |
| 72 | + // Use the provided memory budget (typically 64MB) |
| 73 | + opts.optimize_level_style_compaction(mem_budget); |
| 74 | + } |
| 75 | + |
| 76 | + /// Apply archive preset configuration (Callidon's HDD-optimized settings) |
| 77 | + fn apply_archive(&self, opts: &mut Options, parallelism: usize) { |
| 78 | + if parallelism > 1 { |
| 79 | + opts.increase_parallelism(parallelism as i32); |
| 80 | + } |
| 81 | + |
| 82 | + // Memory and write buffer settings (256MB for better batching on HDD) |
| 83 | + let write_buffer_size = 256 * 1024 * 1024; // 256MB |
| 84 | + |
| 85 | + // Optimize for level-style compaction with archive-appropriate memory |
| 86 | + // This sets up LSM tree parameters |
| 87 | + opts.optimize_level_style_compaction(write_buffer_size); |
| 88 | + |
| 89 | + // Re-set write_buffer_size after optimize_level_style_compaction() |
| 90 | + // because optimize_level_style_compaction() internally overrides it to size/4 |
| 91 | + opts.set_write_buffer_size(write_buffer_size); |
| 92 | + |
| 93 | + // LSM Tree Structure - Optimized for large (4TB+) archives |
| 94 | + // 256 MB SST files reduce file count dramatically (500K → 16K files for 4TB) |
| 95 | + opts.set_target_file_size_base(256 * 1024 * 1024); // 256 MB SST files |
| 96 | + opts.set_target_file_size_multiplier(1); // Same size across all levels |
| 97 | + opts.set_max_bytes_for_level_base(1024 * 1024 * 1024); // 1 GB L1 base |
| 98 | + opts.set_level_compaction_dynamic_level_bytes(true); // Minimize space amplification |
| 99 | + |
| 100 | + // Compaction settings |
| 101 | + // Trigger compaction when L0 has just 1 file (minimize write amplification) |
| 102 | + opts.set_level_zero_file_num_compaction_trigger(1); |
| 103 | + |
| 104 | + // Prioritize compacting older/smaller files first |
| 105 | + use rocksdb::CompactionPri; |
| 106 | + opts.set_compaction_pri(CompactionPri::OldestSmallestSeqFirst); |
| 107 | + |
| 108 | + // Read-ahead for compactions (4MB - good for sequential HDD reads) |
| 109 | + opts.set_compaction_readahead_size(4 * 1024 * 1024); |
| 110 | + |
| 111 | + // Compression strategy: LZ4 for all levels, ZSTD for bottommost |
| 112 | + use rocksdb::DBCompressionType; |
| 113 | + |
| 114 | + // Set default compression to LZ4 (fast) |
| 115 | + opts.set_compression_type(DBCompressionType::Lz4); |
| 116 | + |
| 117 | + // Enable bottommost level compression with maximum ZSTD level |
| 118 | + opts.set_bottommost_compression_type(DBCompressionType::Zstd); |
| 119 | + |
| 120 | + // ZSTD compression options for bottommost level |
| 121 | + // Larger dictionaries (64 KB) improve compression on large archives |
| 122 | + opts.set_compression_options( |
| 123 | + -1, // window_bits (let ZSTD choose optimal) |
| 124 | + 22, // level (maximum compression) |
| 125 | + 0, // strategy (default) |
| 126 | + 64 * 1024, // dict_bytes (64 KB dictionary) |
| 127 | + ); |
| 128 | + |
| 129 | + // Train ZSTD dictionaries on 8 MB of sample data (~125x dictionary size) |
| 130 | + opts.set_zstd_max_train_bytes(8 * 1024 * 1024); |
| 131 | + |
| 132 | + // Block-based table options for better caching |
| 133 | + use rocksdb::{BlockBasedOptions, Cache}; |
| 134 | + let mut block_opts = BlockBasedOptions::default(); |
| 135 | + |
| 136 | + // Partitioned Bloom filters (18 bits per key for better false-positive rate) |
| 137 | + block_opts.set_bloom_filter(18.0, false); // 18 bits per key |
| 138 | + block_opts.set_partition_filters(true); // Partition for large databases |
| 139 | + block_opts.set_format_version(5); // Latest format with optimizations |
| 140 | + block_opts.set_index_type(rocksdb::BlockBasedIndexType::TwoLevelIndexSearch); |
| 141 | + |
| 142 | + // Cache index and filter blocks in block cache for faster queries |
| 143 | + block_opts.set_cache_index_and_filter_blocks(true); |
| 144 | + |
| 145 | + // Block cache (2GB LRU cache for frequently accessed blocks) |
| 146 | + let cache = Cache::new_lru_cache(2 * 1024 * 1024 * 1024); // 2GB |
| 147 | + block_opts.set_block_cache(&cache); |
| 148 | + |
| 149 | + // Set block size (256KB - better for sequential HDD reads) |
| 150 | + block_opts.set_block_size(256 * 1024); |
| 151 | + |
| 152 | + opts.set_block_based_table_factory(&block_opts); |
| 153 | + |
| 154 | + // Rate limiting: prevent I/O spikes on HDD |
| 155 | + // 12 MB/s rate limit for background writes |
| 156 | + opts.set_ratelimiter(12 * 1024 * 1024, 100_000, 10); |
| 157 | + |
| 158 | + // Enable BlobDB for large values (reduces write amplification) |
| 159 | + opts.set_enable_blob_files(true); |
| 160 | + opts.set_min_blob_size(512); // Only values >512 bytes go to blob files |
| 161 | + opts.set_blob_file_size(256 * 1024 * 1024); // 256MB blob files |
| 162 | + opts.set_blob_compression_type(DBCompressionType::Zstd); // Compress blobs |
| 163 | + opts.set_enable_blob_gc(true); // Enable garbage collection |
| 164 | + opts.set_blob_gc_age_cutoff(0.9); // GC blobs when 90% old |
| 165 | + opts.set_blob_gc_force_threshold(0.1); // Force GC at 10% garbage |
| 166 | + opts.set_blob_compaction_readahead_size(8 * 1024 * 1024); // 8 MB blob readahead |
| 167 | + } |
| 168 | + |
| 169 | + /// Get a human-readable description of the preset |
| 170 | + pub fn description(&self) -> &'static str { |
| 171 | + match self { |
| 172 | + Self::Default => "Default preset - balanced for SSD/NVMe (64MB write buffer, standard compression)", |
| 173 | + Self::Archive => "Archive preset - optimized for HDD (256MB write buffer, BlobDB, aggressive compression, rate limiting)", |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /// Get the recommended use case for this preset |
| 178 | + pub fn use_case(&self) -> &'static str { |
| 179 | + match self { |
| 180 | + Self::Default => "General purpose nodes on SSD/NVMe storage", |
| 181 | + Self::Archive => "Archival nodes on HDD storage (--archival flag recommended)", |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /// Get memory requirements for this preset |
| 186 | + pub fn memory_requirements(&self) -> &'static str { |
| 187 | + match self { |
| 188 | + Self::Default => "~4GB minimum, scales with --ram-scale", |
| 189 | + Self::Archive => "~8GB minimum (256MB write buffer + 2GB cache + overhead), 16GB+ recommended", |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +#[cfg(test)] |
| 195 | +mod tests { |
| 196 | + use super::*; |
| 197 | + |
| 198 | + #[test] |
| 199 | + fn test_preset_from_str() { |
| 200 | + assert_eq!(RocksDbPreset::from_str("default").unwrap(), RocksDbPreset::Default); |
| 201 | + assert_eq!(RocksDbPreset::from_str("Default").unwrap(), RocksDbPreset::Default); |
| 202 | + assert_eq!(RocksDbPreset::from_str("archive").unwrap(), RocksDbPreset::Archive); |
| 203 | + assert_eq!(RocksDbPreset::from_str("ARCHIVE").unwrap(), RocksDbPreset::Archive); |
| 204 | + assert!(RocksDbPreset::from_str("unknown").is_err()); |
| 205 | + } |
| 206 | + |
| 207 | + #[test] |
| 208 | + fn test_preset_display() { |
| 209 | + assert_eq!(RocksDbPreset::Default.to_string(), "default"); |
| 210 | + assert_eq!(RocksDbPreset::Archive.to_string(), "archive"); |
| 211 | + } |
| 212 | + |
| 213 | + #[test] |
| 214 | + fn test_apply_presets() { |
| 215 | + let mut opts = Options::default(); |
| 216 | + |
| 217 | + // Test default preset |
| 218 | + RocksDbPreset::Default.apply_to_options(&mut opts, 4, 64 * 1024 * 1024); |
| 219 | + |
| 220 | + // Test archive preset |
| 221 | + RocksDbPreset::Archive.apply_to_options(&mut opts, 4, 64 * 1024 * 1024); |
| 222 | + } |
| 223 | +} |
0 commit comments