Skip to content

Commit 3ab9d3a

Browse files
authored
Add a cli option for the snapshot cache size (#5270)
* Add a cli option for snapshot cache size * Remove junk * Make snapshot_cache module public * lint * Update docs
1 parent de6ede1 commit 3ab9d3a

File tree

8 files changed

+42
-4
lines changed

8 files changed

+42
-4
lines changed

beacon_node/beacon_chain/src/builder.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::light_client_server_cache::LightClientServerCache;
1212
use crate::migrate::{BackgroundMigrator, MigratorConfig};
1313
use crate::persisted_beacon_chain::PersistedBeaconChain;
1414
use crate::shuffling_cache::{BlockShufflingIds, ShufflingCache};
15-
use crate::snapshot_cache::{SnapshotCache, DEFAULT_SNAPSHOT_CACHE_SIZE};
15+
use crate::snapshot_cache::SnapshotCache;
1616
use crate::timeout_rw_lock::TimeoutRwLock;
1717
use crate::validator_monitor::{ValidatorMonitor, ValidatorMonitorConfig};
1818
use crate::validator_pubkey_cache::ValidatorPubkeyCache;
@@ -870,6 +870,7 @@ where
870870
let head_for_snapshot_cache = head_snapshot.clone();
871871
let canonical_head = CanonicalHead::new(fork_choice, Arc::new(head_snapshot));
872872
let shuffling_cache_size = self.chain_config.shuffling_cache_size;
873+
let snapshot_cache_size = self.chain_config.snapshot_cache_size;
873874

874875
// Calculate the weak subjectivity point in which to backfill blocks to.
875876
let genesis_backfill_slot = if self.chain_config.genesis_backfill {
@@ -946,7 +947,7 @@ where
946947
event_handler: self.event_handler,
947948
head_tracker,
948949
snapshot_cache: TimeoutRwLock::new(SnapshotCache::new(
949-
DEFAULT_SNAPSHOT_CACHE_SIZE,
950+
snapshot_cache_size,
950951
head_for_snapshot_cache,
951952
)),
952953
shuffling_cache: TimeoutRwLock::new(ShufflingCache::new(

beacon_node/beacon_chain/src/chain_config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ pub struct ChainConfig {
7272
pub optimistic_finalized_sync: bool,
7373
/// The size of the shuffling cache,
7474
pub shuffling_cache_size: usize,
75+
/// The size of the snapshot cache.
76+
pub snapshot_cache_size: usize,
7577
/// If using a weak-subjectivity sync, whether we should download blocks all the way back to
7678
/// genesis.
7779
pub genesis_backfill: bool,
@@ -112,6 +114,7 @@ impl Default for ChainConfig {
112114
// This value isn't actually read except in tests.
113115
optimistic_finalized_sync: true,
114116
shuffling_cache_size: crate::shuffling_cache::DEFAULT_CACHE_SIZE,
117+
snapshot_cache_size: crate::snapshot_cache::DEFAULT_SNAPSHOT_CACHE_SIZE,
115118
genesis_backfill: false,
116119
always_prepare_payload: false,
117120
progressive_balances_mode: ProgressiveBalancesMode::Fast,

beacon_node/beacon_chain/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ mod pre_finalization_cache;
5050
pub mod proposer_prep_service;
5151
pub mod schema_change;
5252
pub mod shuffling_cache;
53-
mod snapshot_cache;
53+
pub mod snapshot_cache;
5454
pub mod state_advance_timer;
5555
pub mod sync_committee_rewards;
5656
pub mod sync_committee_verification;

beacon_node/beacon_chain/src/snapshot_cache.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use types::{
99
};
1010

1111
/// The default size of the cache.
12-
pub const DEFAULT_SNAPSHOT_CACHE_SIZE: usize = 4;
12+
pub const DEFAULT_SNAPSHOT_CACHE_SIZE: usize = 3;
1313

1414
/// The minimum block delay to clone the state in the cache instead of removing it.
1515
/// This helps keep block processing fast during re-orgs from late blocks.
@@ -174,6 +174,7 @@ impl<T: EthSpec> SnapshotCache<T> {
174174
self.snapshots.iter().map(|s| s.beacon_block_root).collect()
175175
}
176176

177+
#[allow(clippy::len_without_is_empty)]
177178
/// The number of snapshots contained in `self`.
178179
pub fn len(&self) -> usize {
179180
self.snapshots.len()

beacon_node/src/cli.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,13 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
622622
.help("Specifies how many states from the freezer database should cache in memory [default: 1]")
623623
.takes_value(true)
624624
)
625+
.arg(
626+
Arg::with_name("state-cache-size")
627+
.long("state-cache-size")
628+
.value_name("STATE_CACHE_SIZE")
629+
.help("Specifies the size of the snapshot cache [default: 3]")
630+
.takes_value(true)
631+
)
625632
/*
626633
* Execution Layer Integration
627634
*/

beacon_node/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ pub fn get_config<E: EthSpec>(
170170
if let Some(cache_size) = clap_utils::parse_optional(cli_args, "shuffling-cache-size")? {
171171
client_config.chain.shuffling_cache_size = cache_size;
172172
}
173+
if let Some(cache_size) = clap_utils::parse_optional(cli_args, "state-cache-size")? {
174+
client_config.chain.snapshot_cache_size = cache_size;
175+
}
173176

174177
/*
175178
* Prometheus metrics HTTP server

book/src/help_bn.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@ OPTIONS:
461461
--slots-per-restore-point <SLOT_COUNT>
462462
Specifies how often a freezer DB restore point should be stored. Cannot be changed after initialization.
463463
[default: 8192 (mainnet) or 64 (minimal)]
464+
--state-cache-size <STATE_CACHE_SIZE>
465+
Specifies the size of the snapshot cache [default: 3]
466+
464467
--suggested-fee-recipient <SUGGESTED-FEE-RECIPIENT>
465468
Emergency fallback fee recipient for use in case the validator client does not have one configured. You
466469
should set this flag on the validator client instead of (or in addition to) setting it here.

lighthouse/tests/beacon_node.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,26 @@ fn shuffling_cache_set() {
172172
.with_config(|config| assert_eq!(config.chain.shuffling_cache_size, 500));
173173
}
174174

175+
#[test]
176+
fn snapshot_cache_default() {
177+
CommandLineTest::new()
178+
.run_with_zero_port()
179+
.with_config(|config| {
180+
assert_eq!(
181+
config.chain.snapshot_cache_size,
182+
beacon_node::beacon_chain::snapshot_cache::DEFAULT_SNAPSHOT_CACHE_SIZE
183+
)
184+
});
185+
}
186+
187+
#[test]
188+
fn snapshot_cache_set() {
189+
CommandLineTest::new()
190+
.flag("state-cache-size", Some("500"))
191+
.run_with_zero_port()
192+
.with_config(|config| assert_eq!(config.chain.snapshot_cache_size, 500));
193+
}
194+
175195
#[test]
176196
fn fork_choice_before_proposal_timeout_default() {
177197
CommandLineTest::new()

0 commit comments

Comments
 (0)