Skip to main content

risingwave_storage/
opts.rs

1// Copyright 2023 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use risingwave_common::config::storage::FileCacheRuntimeConfig;
16use risingwave_common::config::streaming::CacheRefillPolicy;
17use risingwave_common::config::{
18    EvictionConfig, ObjectStoreConfig, RwConfig, StorageMemoryConfig, extract_storage_memory_config,
19};
20use risingwave_common::system_param::reader::{SystemParamsRead, SystemParamsReader};
21use risingwave_common::system_param::system_params_for_test;
22
23#[derive(Clone, Debug)]
24pub struct StorageOpts {
25    /// The size of parallel task for one compact/flush job.
26    pub parallel_compact_size_mb: u32,
27    /// Target size of the Sstable.
28    pub sstable_size_mb: u32,
29    /// Minimal target size of the Sstable to store data of different state-table in independent files as soon as possible.
30    pub min_sstable_size_mb: u32,
31    /// Size of each block in bytes in SST.
32    pub block_size_kb: u32,
33    /// Deprecated and ignored by SST filter builders; kept for backward compatibility.
34    pub bloom_false_positive: f64,
35    /// parallelism while syncing share buffers into L0 SST. Should NOT be 0.
36    pub share_buffers_sync_parallelism: u32,
37    /// Worker threads number of dedicated tokio runtime for share buffer compaction. 0 means use
38    /// tokio's default value (number of CPU core).
39    pub share_buffer_compaction_worker_threads_number: u32,
40    /// Maximum shared buffer size, writes attempting to exceed the capacity will stall until there
41    /// is enough space.
42    pub shared_buffer_capacity_mb: usize,
43    /// The shared buffer will start flushing data to object when the ratio of memory usage to the
44    /// shared buffer capacity exceed such ratio.
45    pub shared_buffer_flush_ratio: f32,
46    /// The minimum total flush size of shared buffer spill. When a shared buffer spill is trigger,
47    /// the total flush size across multiple epochs should be at least higher than this size.
48    pub shared_buffer_min_batch_flush_size_mb: usize,
49    /// Remote directory for storing data and metadata objects.
50    pub data_directory: String,
51    /// Whether to enable write conflict detection
52    pub write_conflict_detection_enabled: bool,
53    /// Capacity of sstable block cache.
54    pub block_cache_capacity_mb: usize,
55    /// the number of block-cache shard. Less shard means that more concurrent-conflict.
56    pub block_cache_shard_num: usize,
57    /// Eviction config for block cache.
58    pub block_cache_eviction_config: EvictionConfig,
59    /// Capacity of sstable meta cache.
60    pub meta_cache_capacity_mb: usize,
61    /// the number of meta-cache shard. Less shard means that more concurrent-conflict.
62    pub meta_cache_shard_num: usize,
63    /// Eviction config for meta cache.
64    pub meta_cache_eviction_config: EvictionConfig,
65    /// Number of lifecycle lock shards in the Pin Cache.
66    pub pin_cache_shard_num: usize,
67    /// max memory usage for large query.
68    pub prefetch_buffer_capacity_mb: usize,
69
70    pub max_cached_recent_versions_number: usize,
71
72    pub max_prefetch_block_number: usize,
73
74    pub sstable_meta_fetch_concurrency: usize,
75
76    pub disable_remote_compactor: bool,
77    /// Number of tasks shared buffer can upload in parallel.
78    pub share_buffer_upload_concurrency: usize,
79    /// Capacity of sstable meta cache.
80    pub compactor_memory_limit_mb: usize,
81    /// compactor streaming iterator recreate timeout.
82    /// deprecated
83    pub compact_iter_recreate_timeout_ms: u64,
84    /// Number of SST ids fetched from meta per RPC
85    pub sstable_id_remote_fetch_number: u32,
86    /// Whether to enable streaming upload for sstable.
87    pub min_sst_size_for_streaming_upload: u64,
88    pub max_concurrent_compaction_task_number: u64,
89    pub max_version_pinning_duration_sec: u64,
90    pub compactor_iter_max_io_retry_times: usize,
91
92    /// If set, block metadata keys will be shortened when their length exceeds this threshold.
93    pub shorten_block_meta_key_threshold: Option<usize>,
94
95    pub data_file_cache_dir: String,
96    pub data_file_cache_direct_io: bool,
97    pub data_file_cache_capacity_mb: usize,
98    pub data_file_cache_file_capacity_mb: usize,
99    pub data_file_cache_flushers: usize,
100    pub data_file_cache_reclaimers: usize,
101    pub data_file_cache_recover_mode: foyer::RecoverMode,
102    pub data_file_cache_recover_concurrency: usize,
103    pub data_file_cache_indexer_shards: usize,
104    pub data_file_cache_compression: foyer::Compression,
105    pub data_file_cache_flush_buffer_threshold_mb: usize,
106    pub data_file_cache_submit_queue_size_threshold_mb: usize,
107    pub data_file_cache_fifo_probation_ratio: f64,
108    pub data_file_cache_blob_index_size_kb: usize,
109    pub data_file_cache_runtime_config: FileCacheRuntimeConfig,
110    pub data_file_cache_throttle: foyer::Throttle,
111
112    pub cache_refill_data_refill_levels: Vec<u32>,
113    pub cache_refill_timeout_ms: u64,
114    pub cache_refill_meta_refill_concurrency: usize,
115    pub cache_refill_concurrency: usize,
116    pub cache_refill_recent_filter_shards: usize,
117    pub cache_refill_recent_filter_layers: usize,
118    pub cache_refill_recent_filter_rotate_interval_ms: usize,
119    pub cache_refill_unit: usize,
120    pub cache_refill_threshold: f64,
121    pub cache_refill_skip_recent_filter: bool,
122    pub cache_refill_skip_inheritance_filter: bool,
123    pub cache_refill_table_cache_refill_default_policy: CacheRefillPolicy,
124
125    pub meta_file_cache_dir: String,
126    pub meta_file_cache_direct_io: bool,
127    pub meta_file_cache_capacity_mb: usize,
128    pub meta_file_cache_file_capacity_mb: usize,
129    pub meta_file_cache_flushers: usize,
130    pub meta_file_cache_reclaimers: usize,
131    pub meta_file_cache_recover_mode: foyer::RecoverMode,
132    pub meta_file_cache_recover_concurrency: usize,
133    pub meta_file_cache_indexer_shards: usize,
134    pub meta_file_cache_compression: foyer::Compression,
135    pub meta_file_cache_flush_buffer_threshold_mb: usize,
136    pub meta_file_cache_submit_queue_size_threshold_mb: usize,
137    pub meta_file_cache_fifo_probation_ratio: f64,
138    pub meta_file_cache_blob_index_size_kb: usize,
139    pub meta_file_cache_runtime_config: FileCacheRuntimeConfig,
140    pub meta_file_cache_throttle: foyer::Throttle,
141    pub sst_skip_bloom_filter_in_serde: bool,
142
143    pub vector_file_block_size_kb: usize,
144    pub vector_block_cache_capacity_mb: usize,
145    pub vector_block_cache_shard_num: usize,
146    pub vector_block_cache_eviction_config: EvictionConfig,
147    pub vector_meta_cache_capacity_mb: usize,
148    pub vector_meta_cache_shard_num: usize,
149    pub vector_meta_cache_eviction_config: EvictionConfig,
150
151    /// The storage url for storing backups.
152    pub backup_storage_url: String,
153    /// The storage directory for storing backups.
154    pub backup_storage_directory: String,
155    /// max time which wait for preload. 0 represent do not do any preload.
156    pub max_preload_wait_time_mill: u64,
157
158    pub compactor_max_sst_key_count: u64,
159    pub compactor_max_task_multiplier: f32,
160    pub compactor_max_sst_size: u64,
161    /// enable `FastCompactorRunner`.
162    pub enable_fast_compaction: bool,
163    pub check_compaction_result: bool,
164    pub compactor_fast_max_compact_delete_ratio: u32,
165    pub compactor_fast_max_compact_task_size: u64,
166
167    pub mem_table_spill_threshold: usize,
168
169    pub compactor_concurrent_uploading_sst_count: Option<usize>,
170
171    pub compactor_max_overlap_sst_count: usize,
172
173    /// The maximum number of meta files that can be preloaded.
174    pub compactor_max_preload_meta_file_count: usize,
175
176    pub object_store_config: ObjectStoreConfig,
177    pub time_travel_version_cache_capacity: u64,
178    pub table_change_log_cache_capacity: u64,
179
180    pub iceberg_compaction_enable_validate: bool,
181    pub iceberg_compaction_max_record_batch_rows: usize,
182    pub iceberg_compaction_write_parquet_max_row_group_rows: usize,
183    pub iceberg_compaction_min_size_per_partition_mb: u32,
184    pub iceberg_compaction_max_file_count_per_partition: u32,
185    pub iceberg_compaction_target_binpack_group_size_mb: Option<u64>,
186    pub iceberg_compaction_min_group_size_mb: Option<u64>,
187    pub iceberg_compaction_min_group_file_count: Option<usize>,
188
189    /// The ratio of iceberg compaction max parallelism to the number of CPU cores
190    pub iceberg_compaction_task_parallelism_ratio: f32,
191    /// Whether to enable heuristic output parallelism in iceberg compaction.
192    pub iceberg_compaction_enable_heuristic_output_parallelism: bool,
193    /// Maximum number of concurrent file close operations
194    pub iceberg_compaction_max_concurrent_closes: usize,
195    /// Whether to enable dynamic size estimation for iceberg compaction.
196    pub iceberg_compaction_enable_dynamic_size_estimation: bool,
197    /// The smoothing factor for size estimation in iceberg compaction.(default: 0.3)
198    pub iceberg_compaction_size_estimation_smoothing_factor: f64,
199    /// Multiplier for pending waiting parallelism budget for iceberg compaction task queue.
200    pub iceberg_compaction_pending_parallelism_budget_multiplier: f32,
201    /// Maximum number of Iceberg compaction tasks requested in one pull.
202    pub iceberg_compaction_max_pull_task_count: u32,
203    /// Pull interval for iceberg compaction task requests in milliseconds.
204    pub iceberg_compaction_pull_interval_ms: u64,
205    /// Whether to enable prefetch for iceberg compaction.
206    pub iceberg_compaction_enable_prefetch: bool,
207}
208
209impl Default for StorageOpts {
210    fn default() -> Self {
211        let c = RwConfig::default();
212        let p = system_params_for_test();
213        let s = extract_storage_memory_config(&c);
214        Self::from((&c, &p.into(), &s))
215    }
216}
217
218impl From<(&RwConfig, &SystemParamsReader, &StorageMemoryConfig)> for StorageOpts {
219    fn from((c, p, s): (&RwConfig, &SystemParamsReader, &StorageMemoryConfig)) -> Self {
220        let mut data_file_cache_throttle = c.storage.data_file_cache.throttle.clone();
221        if data_file_cache_throttle.write_throughput.is_none() {
222            data_file_cache_throttle = data_file_cache_throttle.with_write_throughput(
223                c.storage.data_file_cache.insert_rate_limit_mb * 1024 * 1024,
224            );
225        }
226        let mut meta_file_cache_throttle = c.storage.meta_file_cache.throttle.clone();
227        if meta_file_cache_throttle.write_throughput.is_none() {
228            meta_file_cache_throttle = meta_file_cache_throttle.with_write_throughput(
229                c.storage.meta_file_cache.insert_rate_limit_mb * 1024 * 1024,
230            );
231        }
232
233        Self {
234            parallel_compact_size_mb: p.parallel_compact_size_mb(),
235            sstable_size_mb: p.sstable_size_mb(),
236            min_sstable_size_mb: c.storage.min_sstable_size_mb,
237            block_size_kb: p.block_size_kb(),
238            bloom_false_positive: p.bloom_false_positive(),
239            share_buffers_sync_parallelism: c.storage.share_buffers_sync_parallelism,
240            share_buffer_compaction_worker_threads_number: c
241                .storage
242                .share_buffer_compaction_worker_threads_number,
243            shared_buffer_capacity_mb: s.shared_buffer_capacity_mb,
244            shared_buffer_flush_ratio: c.storage.shared_buffer_flush_ratio,
245            shared_buffer_min_batch_flush_size_mb: c.storage.shared_buffer_min_batch_flush_size_mb,
246            data_directory: p.data_directory().to_owned(),
247            write_conflict_detection_enabled: c.storage.write_conflict_detection_enabled,
248            block_cache_capacity_mb: s.block_cache_capacity_mb,
249            block_cache_shard_num: s.block_cache_shard_num,
250            block_cache_eviction_config: s.block_cache_eviction_config.clone(),
251            meta_cache_capacity_mb: s.meta_cache_capacity_mb,
252            meta_cache_shard_num: s.meta_cache_shard_num,
253            meta_cache_eviction_config: s.meta_cache_eviction_config.clone(),
254            pin_cache_shard_num: c.storage.cache.pin_cache_shard_num,
255            prefetch_buffer_capacity_mb: s.prefetch_buffer_capacity_mb,
256            max_cached_recent_versions_number: c.storage.max_cached_recent_versions_number,
257            max_prefetch_block_number: c.storage.max_prefetch_block_number,
258            sstable_meta_fetch_concurrency: c.storage.sstable_meta_fetch_concurrency,
259            disable_remote_compactor: c.storage.disable_remote_compactor,
260            share_buffer_upload_concurrency: c.storage.share_buffer_upload_concurrency,
261            compactor_memory_limit_mb: s.compactor_memory_limit_mb,
262            sstable_id_remote_fetch_number: c.storage.sstable_id_remote_fetch_number,
263            min_sst_size_for_streaming_upload: c.storage.min_sst_size_for_streaming_upload,
264            max_concurrent_compaction_task_number: c.storage.max_concurrent_compaction_task_number,
265            max_version_pinning_duration_sec: c.storage.max_version_pinning_duration_sec,
266            data_file_cache_dir: c.storage.data_file_cache.dir.clone(),
267            data_file_cache_direct_io: c.storage.data_file_cache.direct_io,
268            data_file_cache_capacity_mb: c.storage.data_file_cache.capacity_mb,
269            data_file_cache_file_capacity_mb: c.storage.data_file_cache.file_capacity_mb,
270            data_file_cache_flushers: c.storage.data_file_cache.flushers,
271            data_file_cache_reclaimers: c.storage.data_file_cache.reclaimers,
272            data_file_cache_recover_mode: c.storage.data_file_cache.recover_mode,
273            data_file_cache_recover_concurrency: c.storage.data_file_cache.recover_concurrency,
274            data_file_cache_indexer_shards: c.storage.data_file_cache.indexer_shards,
275            data_file_cache_compression: c.storage.data_file_cache.compression,
276            data_file_cache_flush_buffer_threshold_mb: s.block_file_cache_flush_buffer_threshold_mb,
277            data_file_cache_submit_queue_size_threshold_mb: c
278                .storage
279                .data_file_cache
280                .submit_queue_size_threshold_mb,
281            data_file_cache_fifo_probation_ratio: c.storage.data_file_cache.fifo_probation_ratio,
282            data_file_cache_blob_index_size_kb: c.storage.data_file_cache.blob_index_size_kb,
283            data_file_cache_runtime_config: c.storage.data_file_cache.runtime_config.clone(),
284            data_file_cache_throttle,
285            meta_file_cache_dir: c.storage.meta_file_cache.dir.clone(),
286            meta_file_cache_direct_io: c.storage.meta_file_cache.direct_io,
287            meta_file_cache_capacity_mb: c.storage.meta_file_cache.capacity_mb,
288            meta_file_cache_file_capacity_mb: c.storage.meta_file_cache.file_capacity_mb,
289            meta_file_cache_flushers: c.storage.meta_file_cache.flushers,
290            meta_file_cache_reclaimers: c.storage.meta_file_cache.reclaimers,
291            meta_file_cache_recover_mode: c.storage.meta_file_cache.recover_mode,
292            meta_file_cache_recover_concurrency: c.storage.meta_file_cache.recover_concurrency,
293            meta_file_cache_indexer_shards: c.storage.meta_file_cache.indexer_shards,
294            meta_file_cache_compression: c.storage.meta_file_cache.compression,
295            meta_file_cache_flush_buffer_threshold_mb: s.meta_file_cache_flush_buffer_threshold_mb,
296            meta_file_cache_submit_queue_size_threshold_mb: c
297                .storage
298                .meta_file_cache
299                .submit_queue_size_threshold_mb,
300            meta_file_cache_fifo_probation_ratio: c.storage.meta_file_cache.fifo_probation_ratio,
301            meta_file_cache_blob_index_size_kb: c.storage.meta_file_cache.blob_index_size_kb,
302            meta_file_cache_runtime_config: c.storage.meta_file_cache.runtime_config.clone(),
303            meta_file_cache_throttle,
304            sst_skip_bloom_filter_in_serde: c.storage.sst_skip_bloom_filter_in_serde,
305            cache_refill_data_refill_levels: c.storage.cache_refill.data_refill_levels.clone(),
306            cache_refill_timeout_ms: c.storage.cache_refill.timeout_ms,
307            cache_refill_meta_refill_concurrency: c.storage.cache_refill.meta_refill_concurrency,
308            cache_refill_concurrency: c.storage.cache_refill.concurrency,
309            cache_refill_recent_filter_shards: c.storage.cache_refill.recent_filter_shards,
310            cache_refill_recent_filter_layers: c.storage.cache_refill.recent_filter_layers,
311            cache_refill_recent_filter_rotate_interval_ms: c
312                .storage
313                .cache_refill
314                .recent_filter_rotate_interval_ms,
315            cache_refill_unit: c.storage.cache_refill.unit,
316            cache_refill_threshold: c.storage.cache_refill.threshold,
317            cache_refill_skip_recent_filter: c.storage.cache_refill.skip_recent_filter,
318            cache_refill_skip_inheritance_filter: c.storage.cache_refill.skip_inheritance_filter,
319            cache_refill_table_cache_refill_default_policy: c
320                .streaming
321                .developer
322                .cache_refill_policy,
323            max_preload_wait_time_mill: c.storage.max_preload_wait_time_mill,
324            compact_iter_recreate_timeout_ms: c.storage.compact_iter_recreate_timeout_ms,
325
326            backup_storage_url: p.backup_storage_url().to_owned(),
327            backup_storage_directory: p.backup_storage_directory().to_owned(),
328            compactor_max_sst_key_count: c.storage.compactor_max_sst_key_count,
329            compactor_max_task_multiplier: c.storage.compactor_max_task_multiplier,
330            compactor_max_sst_size: c.storage.compactor_max_sst_size,
331            enable_fast_compaction: c.storage.enable_fast_compaction,
332            check_compaction_result: c.storage.check_compaction_result,
333            mem_table_spill_threshold: c.storage.mem_table_spill_threshold,
334            object_store_config: c.storage.object_store.clone(),
335            compactor_fast_max_compact_delete_ratio: c
336                .storage
337                .compactor_fast_max_compact_delete_ratio,
338            compactor_fast_max_compact_task_size: c.storage.compactor_fast_max_compact_task_size,
339            compactor_iter_max_io_retry_times: c.storage.compactor_iter_max_io_retry_times,
340            shorten_block_meta_key_threshold: c.storage.shorten_block_meta_key_threshold,
341            compactor_concurrent_uploading_sst_count: c
342                .storage
343                .compactor_concurrent_uploading_sst_count,
344            time_travel_version_cache_capacity: c.storage.time_travel_version_cache_capacity,
345            table_change_log_cache_capacity: c.storage.table_change_log_cache_capacity,
346            compactor_max_overlap_sst_count: c.storage.compactor_max_overlap_sst_count,
347            compactor_max_preload_meta_file_count: c.storage.compactor_max_preload_meta_file_count,
348
349            iceberg_compaction_enable_validate: c.storage.iceberg_compaction_enable_validate,
350            iceberg_compaction_max_record_batch_rows: c
351                .storage
352                .iceberg_compaction_max_record_batch_rows,
353            #[expect(deprecated)]
354            iceberg_compaction_write_parquet_max_row_group_rows: c
355                .storage
356                .iceberg_compaction_write_parquet_max_row_group_rows,
357            iceberg_compaction_min_size_per_partition_mb: c
358                .storage
359                .iceberg_compaction_min_size_per_partition_mb,
360            iceberg_compaction_max_file_count_per_partition: c
361                .storage
362                .iceberg_compaction_max_file_count_per_partition,
363            iceberg_compaction_task_parallelism_ratio: c
364                .storage
365                .iceberg_compaction_task_parallelism_ratio,
366            iceberg_compaction_enable_heuristic_output_parallelism: c
367                .storage
368                .iceberg_compaction_enable_heuristic_output_parallelism,
369            iceberg_compaction_max_concurrent_closes: c
370                .storage
371                .iceberg_compaction_max_concurrent_closes,
372            iceberg_compaction_enable_dynamic_size_estimation: c
373                .storage
374                .iceberg_compaction_enable_dynamic_size_estimation,
375            iceberg_compaction_size_estimation_smoothing_factor: c
376                .storage
377                .iceberg_compaction_size_estimation_smoothing_factor,
378            iceberg_compaction_pending_parallelism_budget_multiplier: c
379                .storage
380                .iceberg_compaction_pending_parallelism_budget_multiplier,
381            iceberg_compaction_max_pull_task_count: c
382                .storage
383                .iceberg_compaction_max_pull_task_count,
384            iceberg_compaction_pull_interval_ms: c.storage.iceberg_compaction_pull_interval_ms,
385            iceberg_compaction_enable_prefetch: c.storage.iceberg_compaction_enable_prefetch,
386            iceberg_compaction_target_binpack_group_size_mb: c
387                .storage
388                .iceberg_compaction_target_binpack_group_size_mb,
389            iceberg_compaction_min_group_size_mb: c.storage.iceberg_compaction_min_group_size_mb,
390            iceberg_compaction_min_group_file_count: c
391                .storage
392                .iceberg_compaction_min_group_file_count,
393            vector_file_block_size_kb: c.storage.vector_file_block_size_kb,
394            vector_block_cache_capacity_mb: s.vector_block_cache_capacity_mb,
395            vector_block_cache_shard_num: s.vector_block_cache_shard_num,
396            vector_block_cache_eviction_config: s.vector_block_cache_eviction_config.clone(),
397            vector_meta_cache_capacity_mb: s.vector_meta_cache_capacity_mb,
398            vector_meta_cache_shard_num: s.vector_meta_cache_shard_num,
399            vector_meta_cache_eviction_config: s.vector_meta_cache_eviction_config.clone(),
400        }
401    }
402}
403
404#[cfg(test)]
405mod tests {
406    use super::*;
407
408    #[test]
409    fn test_file_cache_submit_queue_size_threshold() {
410        let mut config = RwConfig::default();
411        config
412            .storage
413            .data_file_cache
414            .submit_queue_size_threshold_mb = 256;
415        config
416            .storage
417            .meta_file_cache
418            .submit_queue_size_threshold_mb = 32;
419        let system_params = system_params_for_test();
420        let storage_memory_config = extract_storage_memory_config(&config);
421
422        let opts = StorageOpts::from((&config, &system_params.into(), &storage_memory_config));
423
424        assert_eq!(opts.data_file_cache_submit_queue_size_threshold_mb, 256);
425        assert_eq!(opts.meta_file_cache_submit_queue_size_threshold_mb, 32);
426    }
427}