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    /// max memory usage for large query.
66    pub prefetch_buffer_capacity_mb: usize,
67
68    pub max_cached_recent_versions_number: usize,
69
70    pub max_prefetch_block_number: usize,
71
72    pub disable_remote_compactor: bool,
73    /// Number of tasks shared buffer can upload in parallel.
74    pub share_buffer_upload_concurrency: usize,
75    /// Capacity of sstable meta cache.
76    pub compactor_memory_limit_mb: usize,
77    /// compactor streaming iterator recreate timeout.
78    /// deprecated
79    pub compact_iter_recreate_timeout_ms: u64,
80    /// Number of SST ids fetched from meta per RPC
81    pub sstable_id_remote_fetch_number: u32,
82    /// Whether to enable streaming upload for sstable.
83    pub min_sst_size_for_streaming_upload: u64,
84    pub max_concurrent_compaction_task_number: u64,
85    pub max_version_pinning_duration_sec: u64,
86    pub compactor_iter_max_io_retry_times: usize,
87
88    /// If set, block metadata keys will be shortened when their length exceeds this threshold.
89    pub shorten_block_meta_key_threshold: Option<usize>,
90
91    pub data_file_cache_dir: String,
92    pub data_file_cache_capacity_mb: usize,
93    pub data_file_cache_file_capacity_mb: usize,
94    pub data_file_cache_flushers: usize,
95    pub data_file_cache_reclaimers: usize,
96    pub data_file_cache_recover_mode: foyer::RecoverMode,
97    pub data_file_cache_recover_concurrency: usize,
98    pub data_file_cache_indexer_shards: usize,
99    pub data_file_cache_compression: foyer::Compression,
100    pub data_file_cache_flush_buffer_threshold_mb: usize,
101    pub data_file_cache_submit_queue_size_threshold_mb: usize,
102    pub data_file_cache_fifo_probation_ratio: f64,
103    pub data_file_cache_blob_index_size_kb: usize,
104    pub data_file_cache_runtime_config: FileCacheRuntimeConfig,
105    pub data_file_cache_throttle: foyer::Throttle,
106
107    pub cache_refill_data_refill_levels: Vec<u32>,
108    pub cache_refill_timeout_ms: u64,
109    pub cache_refill_meta_refill_concurrency: usize,
110    pub cache_refill_concurrency: usize,
111    pub cache_refill_recent_filter_shards: usize,
112    pub cache_refill_recent_filter_layers: usize,
113    pub cache_refill_recent_filter_rotate_interval_ms: usize,
114    pub cache_refill_unit: usize,
115    pub cache_refill_threshold: f64,
116    pub cache_refill_skip_recent_filter: bool,
117    pub cache_refill_skip_inheritance_filter: bool,
118    pub cache_refill_table_cache_refill_default_policy: CacheRefillPolicy,
119
120    pub meta_file_cache_dir: String,
121    pub meta_file_cache_capacity_mb: usize,
122    pub meta_file_cache_file_capacity_mb: usize,
123    pub meta_file_cache_flushers: usize,
124    pub meta_file_cache_reclaimers: usize,
125    pub meta_file_cache_recover_mode: foyer::RecoverMode,
126    pub meta_file_cache_recover_concurrency: usize,
127    pub meta_file_cache_indexer_shards: usize,
128    pub meta_file_cache_compression: foyer::Compression,
129    pub meta_file_cache_flush_buffer_threshold_mb: usize,
130    pub meta_file_cache_submit_queue_size_threshold_mb: usize,
131    pub meta_file_cache_fifo_probation_ratio: f64,
132    pub meta_file_cache_blob_index_size_kb: usize,
133    pub meta_file_cache_runtime_config: FileCacheRuntimeConfig,
134    pub meta_file_cache_throttle: foyer::Throttle,
135    pub sst_skip_bloom_filter_in_serde: bool,
136
137    pub vector_file_block_size_kb: usize,
138    pub vector_block_cache_capacity_mb: usize,
139    pub vector_block_cache_shard_num: usize,
140    pub vector_block_cache_eviction_config: EvictionConfig,
141    pub vector_meta_cache_capacity_mb: usize,
142    pub vector_meta_cache_shard_num: usize,
143    pub vector_meta_cache_eviction_config: EvictionConfig,
144
145    /// The storage url for storing backups.
146    pub backup_storage_url: String,
147    /// The storage directory for storing backups.
148    pub backup_storage_directory: String,
149    /// max time which wait for preload. 0 represent do not do any preload.
150    pub max_preload_wait_time_mill: u64,
151
152    pub compactor_max_sst_key_count: u64,
153    pub compactor_max_task_multiplier: f32,
154    pub compactor_max_sst_size: u64,
155    /// enable `FastCompactorRunner`.
156    pub enable_fast_compaction: bool,
157    pub check_compaction_result: bool,
158    pub max_preload_io_retry_times: usize,
159    pub compactor_fast_max_compact_delete_ratio: u32,
160    pub compactor_fast_max_compact_task_size: u64,
161
162    pub mem_table_spill_threshold: usize,
163
164    pub compactor_concurrent_uploading_sst_count: Option<usize>,
165
166    pub compactor_max_overlap_sst_count: usize,
167
168    /// The maximum number of meta files that can be preloaded.
169    pub compactor_max_preload_meta_file_count: usize,
170
171    pub object_store_config: ObjectStoreConfig,
172    pub time_travel_version_cache_capacity: u64,
173    pub table_change_log_cache_capacity: u64,
174
175    pub iceberg_compaction_enable_validate: bool,
176    pub iceberg_compaction_max_record_batch_rows: usize,
177    pub iceberg_compaction_write_parquet_max_row_group_rows: usize,
178    pub iceberg_compaction_min_size_per_partition_mb: u32,
179    pub iceberg_compaction_max_file_count_per_partition: u32,
180    pub iceberg_compaction_target_binpack_group_size_mb: Option<u64>,
181    pub iceberg_compaction_min_group_size_mb: Option<u64>,
182    pub iceberg_compaction_min_group_file_count: Option<usize>,
183
184    /// The ratio of iceberg compaction max parallelism to the number of CPU cores
185    pub iceberg_compaction_task_parallelism_ratio: f32,
186    /// Whether to enable heuristic output parallelism in iceberg compaction.
187    pub iceberg_compaction_enable_heuristic_output_parallelism: bool,
188    /// Maximum number of concurrent file close operations
189    pub iceberg_compaction_max_concurrent_closes: usize,
190    /// Whether to enable dynamic size estimation for iceberg compaction.
191    pub iceberg_compaction_enable_dynamic_size_estimation: bool,
192    /// The smoothing factor for size estimation in iceberg compaction.(default: 0.3)
193    pub iceberg_compaction_size_estimation_smoothing_factor: f64,
194    /// Multiplier for pending waiting parallelism budget for iceberg compaction task queue.
195    pub iceberg_compaction_pending_parallelism_budget_multiplier: f32,
196    /// Maximum number of Iceberg compaction tasks requested in one pull.
197    pub iceberg_compaction_max_pull_task_count: u32,
198    /// Pull interval for iceberg compaction task requests in milliseconds.
199    pub iceberg_compaction_pull_interval_ms: u64,
200    /// Whether to enable prefetch for iceberg compaction.
201    pub iceberg_compaction_enable_prefetch: bool,
202}
203
204impl Default for StorageOpts {
205    fn default() -> Self {
206        let c = RwConfig::default();
207        let p = system_params_for_test();
208        let s = extract_storage_memory_config(&c);
209        Self::from((&c, &p.into(), &s))
210    }
211}
212
213impl From<(&RwConfig, &SystemParamsReader, &StorageMemoryConfig)> for StorageOpts {
214    fn from((c, p, s): (&RwConfig, &SystemParamsReader, &StorageMemoryConfig)) -> Self {
215        let mut data_file_cache_throttle = c.storage.data_file_cache.throttle.clone();
216        if data_file_cache_throttle.write_throughput.is_none() {
217            data_file_cache_throttle = data_file_cache_throttle.with_write_throughput(
218                c.storage.data_file_cache.insert_rate_limit_mb * 1024 * 1024,
219            );
220        }
221        let mut meta_file_cache_throttle = c.storage.meta_file_cache.throttle.clone();
222        if meta_file_cache_throttle.write_throughput.is_none() {
223            meta_file_cache_throttle = meta_file_cache_throttle.with_write_throughput(
224                c.storage.meta_file_cache.insert_rate_limit_mb * 1024 * 1024,
225            );
226        }
227
228        Self {
229            parallel_compact_size_mb: p.parallel_compact_size_mb(),
230            sstable_size_mb: p.sstable_size_mb(),
231            min_sstable_size_mb: c.storage.min_sstable_size_mb,
232            block_size_kb: p.block_size_kb(),
233            bloom_false_positive: p.bloom_false_positive(),
234            share_buffers_sync_parallelism: c.storage.share_buffers_sync_parallelism,
235            share_buffer_compaction_worker_threads_number: c
236                .storage
237                .share_buffer_compaction_worker_threads_number,
238            shared_buffer_capacity_mb: s.shared_buffer_capacity_mb,
239            shared_buffer_flush_ratio: c.storage.shared_buffer_flush_ratio,
240            shared_buffer_min_batch_flush_size_mb: c.storage.shared_buffer_min_batch_flush_size_mb,
241            data_directory: p.data_directory().to_owned(),
242            write_conflict_detection_enabled: c.storage.write_conflict_detection_enabled,
243            block_cache_capacity_mb: s.block_cache_capacity_mb,
244            block_cache_shard_num: s.block_cache_shard_num,
245            block_cache_eviction_config: s.block_cache_eviction_config.clone(),
246            meta_cache_capacity_mb: s.meta_cache_capacity_mb,
247            meta_cache_shard_num: s.meta_cache_shard_num,
248            meta_cache_eviction_config: s.meta_cache_eviction_config.clone(),
249            prefetch_buffer_capacity_mb: s.prefetch_buffer_capacity_mb,
250            max_cached_recent_versions_number: c.storage.max_cached_recent_versions_number,
251            max_prefetch_block_number: c.storage.max_prefetch_block_number,
252            disable_remote_compactor: c.storage.disable_remote_compactor,
253            share_buffer_upload_concurrency: c.storage.share_buffer_upload_concurrency,
254            compactor_memory_limit_mb: s.compactor_memory_limit_mb,
255            sstable_id_remote_fetch_number: c.storage.sstable_id_remote_fetch_number,
256            min_sst_size_for_streaming_upload: c.storage.min_sst_size_for_streaming_upload,
257            max_concurrent_compaction_task_number: c.storage.max_concurrent_compaction_task_number,
258            max_version_pinning_duration_sec: c.storage.max_version_pinning_duration_sec,
259            data_file_cache_dir: c.storage.data_file_cache.dir.clone(),
260            data_file_cache_capacity_mb: c.storage.data_file_cache.capacity_mb,
261            data_file_cache_file_capacity_mb: c.storage.data_file_cache.file_capacity_mb,
262            data_file_cache_flushers: c.storage.data_file_cache.flushers,
263            data_file_cache_reclaimers: c.storage.data_file_cache.reclaimers,
264            data_file_cache_recover_mode: c.storage.data_file_cache.recover_mode,
265            data_file_cache_recover_concurrency: c.storage.data_file_cache.recover_concurrency,
266            data_file_cache_indexer_shards: c.storage.data_file_cache.indexer_shards,
267            data_file_cache_compression: c.storage.data_file_cache.compression,
268            data_file_cache_flush_buffer_threshold_mb: s.block_file_cache_flush_buffer_threshold_mb,
269            data_file_cache_submit_queue_size_threshold_mb: c
270                .storage
271                .data_file_cache
272                .submit_queue_size_threshold_mb,
273            data_file_cache_fifo_probation_ratio: c.storage.data_file_cache.fifo_probation_ratio,
274            data_file_cache_blob_index_size_kb: c.storage.data_file_cache.blob_index_size_kb,
275            data_file_cache_runtime_config: c.storage.data_file_cache.runtime_config.clone(),
276            data_file_cache_throttle,
277            meta_file_cache_dir: c.storage.meta_file_cache.dir.clone(),
278            meta_file_cache_capacity_mb: c.storage.meta_file_cache.capacity_mb,
279            meta_file_cache_file_capacity_mb: c.storage.meta_file_cache.file_capacity_mb,
280            meta_file_cache_flushers: c.storage.meta_file_cache.flushers,
281            meta_file_cache_reclaimers: c.storage.meta_file_cache.reclaimers,
282            meta_file_cache_recover_mode: c.storage.meta_file_cache.recover_mode,
283            meta_file_cache_recover_concurrency: c.storage.meta_file_cache.recover_concurrency,
284            meta_file_cache_indexer_shards: c.storage.meta_file_cache.indexer_shards,
285            meta_file_cache_compression: c.storage.meta_file_cache.compression,
286            meta_file_cache_flush_buffer_threshold_mb: s.meta_file_cache_flush_buffer_threshold_mb,
287            meta_file_cache_submit_queue_size_threshold_mb: c
288                .storage
289                .meta_file_cache
290                .submit_queue_size_threshold_mb,
291            meta_file_cache_fifo_probation_ratio: c.storage.meta_file_cache.fifo_probation_ratio,
292            meta_file_cache_blob_index_size_kb: c.storage.meta_file_cache.blob_index_size_kb,
293            meta_file_cache_runtime_config: c.storage.meta_file_cache.runtime_config.clone(),
294            meta_file_cache_throttle,
295            sst_skip_bloom_filter_in_serde: c.storage.sst_skip_bloom_filter_in_serde,
296            cache_refill_data_refill_levels: c.storage.cache_refill.data_refill_levels.clone(),
297            cache_refill_timeout_ms: c.storage.cache_refill.timeout_ms,
298            cache_refill_meta_refill_concurrency: c.storage.cache_refill.meta_refill_concurrency,
299            cache_refill_concurrency: c.storage.cache_refill.concurrency,
300            cache_refill_recent_filter_shards: c.storage.cache_refill.recent_filter_shards,
301            cache_refill_recent_filter_layers: c.storage.cache_refill.recent_filter_layers,
302            cache_refill_recent_filter_rotate_interval_ms: c
303                .storage
304                .cache_refill
305                .recent_filter_rotate_interval_ms,
306            cache_refill_unit: c.storage.cache_refill.unit,
307            cache_refill_threshold: c.storage.cache_refill.threshold,
308            cache_refill_skip_recent_filter: c.storage.cache_refill.skip_recent_filter,
309            cache_refill_skip_inheritance_filter: c.storage.cache_refill.skip_inheritance_filter,
310            cache_refill_table_cache_refill_default_policy: c
311                .streaming
312                .developer
313                .cache_refill_policy,
314            max_preload_wait_time_mill: c.storage.max_preload_wait_time_mill,
315            compact_iter_recreate_timeout_ms: c.storage.compact_iter_recreate_timeout_ms,
316
317            max_preload_io_retry_times: c.storage.max_preload_io_retry_times,
318            backup_storage_url: p.backup_storage_url().to_owned(),
319            backup_storage_directory: p.backup_storage_directory().to_owned(),
320            compactor_max_sst_key_count: c.storage.compactor_max_sst_key_count,
321            compactor_max_task_multiplier: c.storage.compactor_max_task_multiplier,
322            compactor_max_sst_size: c.storage.compactor_max_sst_size,
323            enable_fast_compaction: c.storage.enable_fast_compaction,
324            check_compaction_result: c.storage.check_compaction_result,
325            mem_table_spill_threshold: c.storage.mem_table_spill_threshold,
326            object_store_config: c.storage.object_store.clone(),
327            compactor_fast_max_compact_delete_ratio: c
328                .storage
329                .compactor_fast_max_compact_delete_ratio,
330            compactor_fast_max_compact_task_size: c.storage.compactor_fast_max_compact_task_size,
331            compactor_iter_max_io_retry_times: c.storage.compactor_iter_max_io_retry_times,
332            shorten_block_meta_key_threshold: c.storage.shorten_block_meta_key_threshold,
333            compactor_concurrent_uploading_sst_count: c
334                .storage
335                .compactor_concurrent_uploading_sst_count,
336            time_travel_version_cache_capacity: c.storage.time_travel_version_cache_capacity,
337            table_change_log_cache_capacity: c.storage.table_change_log_cache_capacity,
338            compactor_max_overlap_sst_count: c.storage.compactor_max_overlap_sst_count,
339            compactor_max_preload_meta_file_count: c.storage.compactor_max_preload_meta_file_count,
340
341            iceberg_compaction_enable_validate: c.storage.iceberg_compaction_enable_validate,
342            iceberg_compaction_max_record_batch_rows: c
343                .storage
344                .iceberg_compaction_max_record_batch_rows,
345            #[expect(deprecated)]
346            iceberg_compaction_write_parquet_max_row_group_rows: c
347                .storage
348                .iceberg_compaction_write_parquet_max_row_group_rows,
349            iceberg_compaction_min_size_per_partition_mb: c
350                .storage
351                .iceberg_compaction_min_size_per_partition_mb,
352            iceberg_compaction_max_file_count_per_partition: c
353                .storage
354                .iceberg_compaction_max_file_count_per_partition,
355            iceberg_compaction_task_parallelism_ratio: c
356                .storage
357                .iceberg_compaction_task_parallelism_ratio,
358            iceberg_compaction_enable_heuristic_output_parallelism: c
359                .storage
360                .iceberg_compaction_enable_heuristic_output_parallelism,
361            iceberg_compaction_max_concurrent_closes: c
362                .storage
363                .iceberg_compaction_max_concurrent_closes,
364            iceberg_compaction_enable_dynamic_size_estimation: c
365                .storage
366                .iceberg_compaction_enable_dynamic_size_estimation,
367            iceberg_compaction_size_estimation_smoothing_factor: c
368                .storage
369                .iceberg_compaction_size_estimation_smoothing_factor,
370            iceberg_compaction_pending_parallelism_budget_multiplier: c
371                .storage
372                .iceberg_compaction_pending_parallelism_budget_multiplier,
373            iceberg_compaction_max_pull_task_count: c
374                .storage
375                .iceberg_compaction_max_pull_task_count,
376            iceberg_compaction_pull_interval_ms: c.storage.iceberg_compaction_pull_interval_ms,
377            iceberg_compaction_enable_prefetch: c.storage.iceberg_compaction_enable_prefetch,
378            iceberg_compaction_target_binpack_group_size_mb: c
379                .storage
380                .iceberg_compaction_target_binpack_group_size_mb,
381            iceberg_compaction_min_group_size_mb: c.storage.iceberg_compaction_min_group_size_mb,
382            iceberg_compaction_min_group_file_count: c
383                .storage
384                .iceberg_compaction_min_group_file_count,
385            vector_file_block_size_kb: c.storage.vector_file_block_size_kb,
386            vector_block_cache_capacity_mb: s.vector_block_cache_capacity_mb,
387            vector_block_cache_shard_num: s.vector_block_cache_shard_num,
388            vector_block_cache_eviction_config: s.vector_block_cache_eviction_config.clone(),
389            vector_meta_cache_capacity_mb: s.vector_meta_cache_capacity_mb,
390            vector_meta_cache_shard_num: s.vector_meta_cache_shard_num,
391            vector_meta_cache_eviction_config: s.vector_meta_cache_eviction_config.clone(),
392        }
393    }
394}
395
396#[cfg(test)]
397mod tests {
398    use super::*;
399
400    #[test]
401    fn test_file_cache_submit_queue_size_threshold() {
402        let mut config = RwConfig::default();
403        config
404            .storage
405            .data_file_cache
406            .submit_queue_size_threshold_mb = 256;
407        config
408            .storage
409            .meta_file_cache
410            .submit_queue_size_threshold_mb = 32;
411        let system_params = system_params_for_test();
412        let storage_memory_config = extract_storage_memory_config(&config);
413
414        let opts = StorageOpts::from((&config, &system_params.into(), &storage_memory_config));
415
416        assert_eq!(opts.data_file_cache_submit_queue_size_threshold_mb, 256);
417        assert_eq!(opts.meta_file_cache_submit_queue_size_threshold_mb, 32);
418    }
419}