Skip to content

Commit 5800c0c

Browse files
Add tracking of memory use in the caching. (#3635)
## Motivation The current tracking is limited by the maximal size of the cache. This is a limited way to control the size of the cache. ## Proposal Two additional constraints are added: * A maximal size of the total cache. * A maximal size of individual entries in the cache. * Those entries are added to the input parameters of all the relevant functions. ## Test Plan The CI. ## Release Plan Normal release plan. ## Links None.
1 parent a5b31fa commit 5800c0c

File tree

14 files changed

+225
-64
lines changed

14 files changed

+225
-64
lines changed

CLI.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,13 @@ A Byzantine-fault tolerant sidechain with low-latency finality and high throughp
125125
* `--max-stream-queries <MAX_STREAM_QUERIES>` — The maximal number of simultaneous stream queries to the database
126126

127127
Default value: `10`
128-
* `--cache-size <CACHE_SIZE>` — The maximal number of entries in the storage cache
128+
* `--max-cache-size <MAX_CACHE_SIZE>` — The maximal memory used in the storage cache
129+
130+
Default value: `10000000`
131+
* `--max-entry-size <MAX_ENTRY_SIZE>` — The maximal size of an entry in the storage cache
132+
133+
Default value: `1000000`
134+
* `--max-cache-entries <MAX_CACHE_ENTRIES>` — The maximal number of entries in the storage cache
129135

130136
Default value: `1000`
131137
* `--retry-delay-ms <RETRY_DELAY>` — Delay increment for retrying to connect to a validator

linera-client/src/client_options.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use linera_base::{
1919
};
2020
use linera_core::{client::BlanketMessagePolicy, DEFAULT_GRACE_PERIOD};
2121
use linera_execution::{ResourceControlPolicy, WasmRuntime, WithWasmDefault as _};
22-
use linera_views::store::CommonStoreConfig;
22+
use linera_views::{lru_caching::StorageCacheConfig, store::CommonStoreConfig};
2323

2424
#[cfg(feature = "fs")]
2525
use crate::config::GenesisConfig;
@@ -112,9 +112,17 @@ pub struct ClientOptions {
112112
#[arg(long, default_value = "10")]
113113
pub max_stream_queries: usize,
114114

115+
/// The maximal memory used in the storage cache.
116+
#[arg(long, default_value = "10000000")]
117+
pub max_cache_size: usize,
118+
119+
/// The maximal size of an entry in the storage cache.
120+
#[arg(long, default_value = "1000000")]
121+
pub max_entry_size: usize,
122+
115123
/// The maximal number of entries in the storage cache.
116124
#[arg(long, default_value = "1000")]
117-
pub cache_size: usize,
125+
pub max_cache_entries: usize,
118126

119127
/// Subcommand.
120128
#[command(subcommand)]
@@ -189,10 +197,15 @@ impl ClientOptions {
189197
}
190198

191199
fn common_config(&self) -> CommonStoreConfig {
200+
let storage_cache_config = StorageCacheConfig {
201+
max_cache_size: self.max_cache_size,
202+
max_entry_size: self.max_entry_size,
203+
max_cache_entries: self.max_cache_entries,
204+
};
192205
CommonStoreConfig {
193206
max_concurrent_queries: self.max_concurrent_queries,
194207
max_stream_queries: self.max_stream_queries,
195-
cache_size: self.cache_size,
208+
storage_cache_config,
196209
}
197210
}
198211

linera-client/src/storage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ impl StorageConfigNamespace {
453453
};
454454
let config = ServiceStoreConfig {
455455
inner_config,
456-
cache_size: common_config.cache_size,
456+
storage_cache_config: common_config.storage_cache_config,
457457
};
458458
Ok(StoreConfig::Service(config, namespace))
459459
}

linera-indexer/lib/src/rocks_db.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::path::PathBuf;
55

66
use clap::Parser as _;
77
use linera_views::{
8+
lru_caching::StorageCacheConfig,
89
rocks_db::{PathWithGuard, RocksDbSpawnMode, RocksDbStore, RocksDbStoreConfig},
910
store::{AdminKeyValueStore, CommonStoreConfig},
1011
};
@@ -20,28 +21,45 @@ pub struct RocksDbConfig {
2021
/// RocksDB storage path
2122
#[arg(long, default_value = "./indexer.db")]
2223
pub storage: PathBuf,
24+
2325
#[arg(long, default_value = "linera")]
2426
pub namespace: String,
27+
2528
/// The maximal number of simultaneous queries to the database
2629
#[arg(long)]
2730
max_concurrent_queries: Option<usize>,
31+
2832
/// The maximal number of simultaneous stream queries to the database
2933
#[arg(long, default_value = "10")]
3034
pub max_stream_queries: usize,
35+
36+
/// The maximal memory used in the storage cache.
37+
#[arg(long, default_value = "10000000")]
38+
pub max_cache_size: usize,
39+
40+
/// The maximal size of an entry in the storage cache.
41+
#[arg(long, default_value = "1000000")]
42+
pub max_entry_size: usize,
43+
3144
/// The maximal number of entries in the storage cache.
3245
#[arg(long, default_value = "1000")]
33-
cache_size: usize,
46+
pub max_cache_entries: usize,
3447
}
3548

3649
pub type RocksDbRunner = Runner<RocksDbStore, RocksDbConfig>;
3750

3851
impl RocksDbRunner {
3952
pub async fn load() -> Result<Self, IndexerError> {
4053
let config = IndexerConfig::<RocksDbConfig>::parse();
54+
let storage_cache_config = StorageCacheConfig {
55+
max_cache_size: config.client.max_cache_size,
56+
max_entry_size: config.client.max_entry_size,
57+
max_cache_entries: config.client.max_cache_entries,
58+
};
4159
let common_config = CommonStoreConfig {
4260
max_concurrent_queries: config.client.max_concurrent_queries,
4361
max_stream_queries: config.client.max_stream_queries,
44-
cache_size: config.client.cache_size,
62+
storage_cache_config,
4563
};
4664
let path_buf = config.client.storage.as_path().to_path_buf();
4765
let path_with_guard = PathWithGuard::new(path_buf);

linera-indexer/lib/src/scylla_db.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use linera_views::{
5+
lru_caching::StorageCacheConfig,
56
scylla_db::{ScyllaDbStore, ScyllaDbStoreConfig},
67
store::{AdminKeyValueStore, CommonStoreConfig},
78
};
@@ -17,28 +18,45 @@ pub struct ScyllaDbConfig {
1718
/// ScyllaDB address
1819
#[arg(long, default_value = "localhost:9042")]
1920
pub uri: String,
21+
2022
#[arg(long, default_value = "linera")]
2123
pub table: String,
24+
2225
/// The maximal number of simultaneous queries to the database
2326
#[arg(long)]
2427
max_concurrent_queries: Option<usize>,
28+
2529
/// The maximal number of simultaneous stream queries to the database
2630
#[arg(long, default_value = "10")]
2731
pub max_stream_queries: usize,
32+
33+
/// The maximal memory used in the storage cache.
34+
#[arg(long, default_value = "10000000")]
35+
pub max_cache_size: usize,
36+
37+
/// The maximal size of an entry in the storage cache.
38+
#[arg(long, default_value = "1000000")]
39+
pub max_entry_size: usize,
40+
2841
/// The maximal number of entries in the storage cache.
2942
#[arg(long, default_value = "1000")]
30-
cache_size: usize,
43+
pub max_cache_entries: usize,
3144
}
3245

3346
pub type ScyllaDbRunner = Runner<ScyllaDbStore, ScyllaDbConfig>;
3447

3548
impl ScyllaDbRunner {
3649
pub async fn load() -> Result<Self, IndexerError> {
3750
let config = <IndexerConfig<ScyllaDbConfig> as clap::Parser>::parse();
51+
let storage_cache_config = StorageCacheConfig {
52+
max_cache_size: config.client.max_cache_size,
53+
max_entry_size: config.client.max_entry_size,
54+
max_cache_entries: config.client.max_cache_entries,
55+
};
3856
let common_config = CommonStoreConfig {
3957
max_concurrent_queries: config.client.max_concurrent_queries,
4058
max_stream_queries: config.client.max_stream_queries,
41-
cache_size: config.client.cache_size,
59+
storage_cache_config,
4260
};
4361
let namespace = config.client.table.clone();
4462
let store_config = ScyllaDbStoreConfig::new(config.client.uri.clone(), common_config);

linera-service/src/proxy/main.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use linera_service::util;
2727
#[cfg(with_metrics)]
2828
use linera_service::{prometheus_server, pyroscope_server};
2929
use linera_storage::Storage;
30-
use linera_views::store::CommonStoreConfig;
30+
use linera_views::{lru_caching::StorageCacheConfig, store::CommonStoreConfig};
3131
use tokio::task::JoinSet;
3232
use tokio_util::sync::CancellationToken;
3333
use tracing::{error, info, instrument};
@@ -70,9 +70,17 @@ pub struct ProxyOptions {
7070
#[arg(long, default_value = "10")]
7171
max_stream_queries: usize,
7272

73+
/// The maximal memory used in the storage cache.
74+
#[arg(long, default_value = "10000000")]
75+
pub max_cache_size: usize,
76+
77+
/// The maximal size of an entry in the storage cache.
78+
#[arg(long, default_value = "1000000")]
79+
pub max_entry_size: usize,
80+
7381
/// The maximal number of entries in the storage cache.
7482
#[arg(long, default_value = "1000")]
75-
cache_size: usize,
83+
pub max_cache_entries: usize,
7684

7785
/// Path to the file describing the initial user chains (aka genesis state)
7886
#[arg(long = "genesis")]
@@ -395,10 +403,15 @@ fn main() -> Result<()> {
395403

396404
impl ProxyOptions {
397405
async fn run(&self) -> Result<()> {
406+
let storage_cache_config = StorageCacheConfig {
407+
max_cache_size: self.max_cache_size,
408+
max_entry_size: self.max_entry_size,
409+
max_cache_entries: self.max_cache_entries,
410+
};
398411
let common_config = CommonStoreConfig {
399412
max_concurrent_queries: self.max_concurrent_queries,
400413
max_stream_queries: self.max_stream_queries,
401-
cache_size: self.cache_size,
414+
storage_cache_config,
402415
};
403416
let full_storage_config = self.storage_config.add_common_config(common_config).await?;
404417
let genesis_config: GenesisConfig = util::read_json(&self.genesis_config_path)?;

linera-service/src/server.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use linera_service::util;
3737
#[cfg(with_metrics)]
3838
use linera_service::{prometheus_server, pyroscope_server};
3939
use linera_storage::Storage;
40-
use linera_views::store::CommonStoreConfig;
40+
use linera_views::{lru_caching::StorageCacheConfig, store::CommonStoreConfig};
4141
use serde::Deserialize;
4242
use tokio::task::JoinSet;
4343
use tokio_util::sync::CancellationToken;
@@ -414,9 +414,17 @@ enum ServerCommand {
414414
#[arg(long, default_value = "10")]
415415
max_stream_queries: usize,
416416

417+
/// The maximal memory used in the storage cache.
418+
#[arg(long, default_value = "10000000")]
419+
max_cache_size: usize,
420+
421+
/// The maximal size of an entry in the storage cache.
422+
#[arg(long, default_value = "1000000")]
423+
max_entry_size: usize,
424+
417425
/// The maximal number of entries in the storage cache.
418426
#[arg(long, default_value = "1000")]
419-
cache_size: usize,
427+
max_cache_entries: usize,
420428
},
421429

422430
/// Act as a trusted third-party and generate all server configurations
@@ -455,9 +463,17 @@ enum ServerCommand {
455463
#[arg(long, default_value = "10")]
456464
max_stream_queries: usize,
457465

466+
/// The maximal memory used in the storage cache.
467+
#[arg(long, default_value = "10000000")]
468+
max_cache_size: usize,
469+
470+
/// The maximal size of an entry in the storage cache.
471+
#[arg(long, default_value = "1000000")]
472+
max_entry_size: usize,
473+
458474
/// The maximal number of entries in the storage cache.
459475
#[arg(long, default_value = "1000")]
460-
cache_size: usize,
476+
max_cache_entries: usize,
461477
},
462478

463479
/// Replaces the configurations of the shards by following the given template.
@@ -567,7 +583,9 @@ async fn run(options: ServerOptions) {
567583
max_loaded_chains,
568584
max_concurrent_queries,
569585
max_stream_queries,
570-
cache_size,
586+
max_cache_size,
587+
max_entry_size,
588+
max_cache_entries,
571589
} => {
572590
linera_version::VERSION_INFO.log();
573591

@@ -585,10 +603,15 @@ async fn run(options: ServerOptions) {
585603
max_loaded_chains,
586604
};
587605
let wasm_runtime = wasm_runtime.with_wasm_default();
606+
let storage_cache_config = StorageCacheConfig {
607+
max_cache_size,
608+
max_entry_size,
609+
max_cache_entries,
610+
};
588611
let common_config = CommonStoreConfig {
589612
max_concurrent_queries,
590613
max_stream_queries,
591-
cache_size,
614+
storage_cache_config,
592615
};
593616
let full_storage_config = storage_config
594617
.add_common_config(common_config)
@@ -647,14 +670,21 @@ async fn run(options: ServerOptions) {
647670
genesis_config_path,
648671
max_concurrent_queries,
649672
max_stream_queries,
650-
cache_size,
673+
max_cache_size,
674+
max_entry_size,
675+
max_cache_entries,
651676
} => {
652677
let genesis_config: GenesisConfig =
653678
util::read_json(&genesis_config_path).expect("Failed to read initial chain config");
679+
let storage_cache_config = StorageCacheConfig {
680+
max_cache_size,
681+
max_entry_size,
682+
max_cache_entries,
683+
};
654684
let common_config = CommonStoreConfig {
655685
max_concurrent_queries,
656686
max_stream_queries,
657-
cache_size,
687+
storage_cache_config,
658688
};
659689
let full_storage_config = storage_config
660690
.add_common_config(common_config)

linera-views/src/backends/dynamo_db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ impl DynamoDbStoreConfig {
11991199
};
12001200
DynamoDbStoreConfig {
12011201
inner_config,
1202-
cache_size: common_config.cache_size,
1202+
storage_cache_config: common_config.storage_cache_config,
12031203
}
12041204
}
12051205
}

linera-views/src/backends/indexed_db.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use thiserror::Error;
1313
use crate::{
1414
batch::{Batch, WriteOperation},
1515
common::get_upper_bound_option,
16+
lru_caching::DEFAULT_STORAGE_CACHE_CONFIG,
1617
store::{
1718
CommonStoreConfig, KeyValueStoreError, LocalAdminKeyValueStore, LocalReadableKeyValueStore,
1819
LocalWritableKeyValueStore, WithError,
@@ -27,12 +28,12 @@ pub struct IndexedDbStoreConfig {
2728
}
2829

2930
impl IndexedDbStoreConfig {
30-
/// Creates a `IndexedDbStoreConfig`. `max_concurrent_queries` and `cache_size` are not used.
31+
/// Creates a `IndexedDbStoreConfig`. `max_concurrent_queries` and `storage_cache_config` are not used.
3132
pub fn new(max_stream_queries: usize) -> Self {
3233
let common_config = CommonStoreConfig {
3334
max_concurrent_queries: None,
3435
max_stream_queries,
35-
cache_size: 1000,
36+
storage_cache_config: DEFAULT_STORAGE_CACHE_CONFIG,
3637
};
3738
Self { common_config }
3839
}

0 commit comments

Comments
 (0)