Skip to main content

linera_storage_runtime/
common_options.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use linera_storage::{StorageCacheConfig, DEFAULT_CLEANUP_INTERVAL_SECS};
5use linera_views::lru_prefix_cache::StorageCacheConfig as ViewsStorageCacheConfig;
6
7/// Command-line options shared by all storage backends, controlling concurrency
8/// limits and cache sizes.
9#[derive(Clone, Debug, clap::Parser)]
10pub struct CommonStorageOptions {
11    /// The maximal number of simultaneous queries to the database
12    #[arg(long, global = true)]
13    pub storage_max_concurrent_queries: Option<usize>,
14
15    /// The maximal number of simultaneous stream queries to the database
16    #[arg(long, default_value = "10", global = true)]
17    pub storage_max_stream_queries: usize,
18
19    /// The maximal memory used in the storage cache.
20    #[arg(long, default_value = "10000000", global = true)]
21    pub storage_max_cache_size: usize,
22
23    /// The maximal size of a value entry in the storage cache.
24    #[arg(long, default_value = "1000000", global = true)]
25    pub storage_max_value_entry_size: usize,
26
27    /// The maximal size of a find-keys entry in the storage cache.
28    #[arg(long, default_value = "1000000", global = true)]
29    pub storage_max_find_keys_entry_size: usize,
30
31    /// The maximal size of a find-key-values entry in the storage cache.
32    #[arg(long, default_value = "1000000", global = true)]
33    pub storage_max_find_key_values_entry_size: usize,
34
35    /// The maximal number of entries in the storage cache.
36    #[arg(long, default_value = "1000", global = true)]
37    pub storage_max_cache_entries: usize,
38
39    /// The maximal memory used in the value cache.
40    #[arg(long, default_value = "10000000", global = true)]
41    pub storage_max_cache_value_size: usize,
42
43    /// The maximal memory used in the find_keys_by_prefix cache.
44    #[arg(long, default_value = "10000000", global = true)]
45    pub storage_max_cache_find_keys_size: usize,
46
47    /// The maximal memory used in the find_key_values_by_prefix cache.
48    #[arg(long, default_value = "10000000", global = true)]
49    pub storage_max_cache_find_key_values_size: usize,
50
51    /// The maximal number of entries in the blob cache.
52    #[arg(long, default_value = "1000", global = true)]
53    pub blob_cache_size: usize,
54
55    /// The maximal number of entries in the confirmed block cache.
56    #[arg(long, default_value = "1000", global = true)]
57    pub confirmed_block_cache_size: usize,
58
59    /// The maximal number of entries in the assembled certificate cache.
60    #[arg(long, default_value = "1000", global = true)]
61    pub certificate_cache_size: usize,
62
63    /// The maximal number of entries in the raw certificate cache.
64    #[arg(long, default_value = "1000", global = true)]
65    pub certificate_raw_cache_size: usize,
66
67    /// The maximal number of entries in the event cache.
68    #[arg(long, default_value = "1000", global = true)]
69    pub event_cache_size: usize,
70
71    /// The maximal number of entries in the block-hash-by-height cache.
72    #[arg(long, default_value = "1000", global = true)]
73    pub block_hash_by_height_cache_size: usize,
74
75    /// The maximal number of entries in the event-block-height cache.
76    #[arg(long, default_value = "1000", global = true)]
77    pub event_block_height_cache_size: usize,
78
79    /// Interval in seconds between weak reference cleanup sweeps in value caches.
80    #[arg(long, default_value_t = DEFAULT_CLEANUP_INTERVAL_SECS, global = true)]
81    pub cache_cleanup_interval_secs: u64,
82
83    /// The replication factor for the keyspace
84    #[arg(long, default_value = "1", global = true)]
85    pub storage_replication_factor: u32,
86}
87
88impl CommonStorageOptions {
89    /// Returns the options with their default values.
90    pub fn with_defaults() -> Self {
91        use clap::Parser as _;
92        Self::parse_from(std::iter::empty::<String>())
93    }
94
95    /// Builds the storage cache configuration from these options.
96    pub fn storage_cache_config(&self) -> StorageCacheConfig {
97        StorageCacheConfig {
98            blob_cache_size: self.blob_cache_size,
99            confirmed_block_cache_size: self.confirmed_block_cache_size,
100            certificate_cache_size: self.certificate_cache_size,
101            certificate_raw_cache_size: self.certificate_raw_cache_size,
102            event_cache_size: self.event_cache_size,
103            block_hash_by_height_cache_size: self.block_hash_by_height_cache_size,
104            event_block_height_cache_size: self.event_block_height_cache_size,
105            cache_cleanup_interval_secs: self.cache_cleanup_interval_secs,
106        }
107    }
108
109    /// Builds the views storage cache configuration from these options.
110    pub fn views_storage_cache_config(&self) -> ViewsStorageCacheConfig {
111        ViewsStorageCacheConfig {
112            max_cache_size: self.storage_max_cache_size,
113            max_value_entry_size: self.storage_max_value_entry_size,
114            max_find_keys_entry_size: self.storage_max_find_keys_entry_size,
115            max_find_key_values_entry_size: self.storage_max_find_key_values_entry_size,
116            max_cache_entries: self.storage_max_cache_entries,
117            max_cache_value_size: self.storage_max_cache_value_size,
118            max_cache_find_keys_size: self.storage_max_cache_find_keys_size,
119            max_cache_find_key_values_size: self.storage_max_cache_find_key_values_size,
120        }
121    }
122}