Skip to content

Commit

Permalink
fix(metrics): pass the epoch manager to spawn_trie_metrics_loop (#12811)
Browse files Browse the repository at this point in the history
This function creates its own epoch manager with
new_from_genesis_config(), but this is not how the main epoch manager is
created at startup, which is done with new_arc_handle(). These result in
epoch managers that return different EpochConfigs for a given protocol
version, since only the second one initializes an epoch config store
from the genesis config, and the first one generates epoch configs with
generate_epoch_config(). In this case it leads to a crash if the shard
layouts are different because the call to
`ShardUId::from_shard_id_and_layout()` will see the wrong shard IDs and
crash at `assert!(shard_layout.shard_ids().any(|i| i == shard_id));`
  • Loading branch information
marcelo-gonzalez authored Jan 27, 2025
1 parent 5baf443 commit 8097202
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
12 changes: 7 additions & 5 deletions nearcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,19 @@ pub fn start_with_config_and_synchronization(
None
};

let epoch_manager = EpochManager::new_arc_handle(
storage.get_hot_store(),
&config.genesis.config,
Some(home_dir),
);

let trie_metrics_arbiter = spawn_trie_metrics_loop(
config.clone(),
storage.get_hot_store(),
config.client_config.log_summary_period,
epoch_manager.clone(),
)?;

let epoch_manager = EpochManager::new_arc_handle(
storage.get_hot_store(),
&config.genesis.config,
Some(home_dir),
);
let genesis_epoch_config = epoch_manager.get_epoch_config(&EpochId::default())?;
// Initialize genesis_state in store either from genesis config or dump before other components.
// We only initialize if the genesis state is not already initialized in store.
Expand Down
13 changes: 8 additions & 5 deletions nearcore/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::NearConfig;
use actix_rt::ArbiterHandle;
use near_async::time::Duration;
use near_chain::{Block, ChainStore, ChainStoreAccess};
use near_epoch_manager::EpochManager;
use near_epoch_manager::EpochManagerAdapter;
use near_o11y::metrics::{
exponential_buckets, try_create_histogram_vec, try_create_int_counter_vec,
try_create_int_gauge, try_create_int_gauge_vec, HistogramVec, IntCounterVec, IntGauge,
Expand Down Expand Up @@ -107,15 +107,17 @@ fn log_trie_item(key: Vec<u8>, value: Vec<u8>) {
}
}

fn export_postponed_receipt_count(near_config: &NearConfig, store: &Store) -> anyhow::Result<()> {
fn export_postponed_receipt_count(
near_config: &NearConfig,
store: &Store,
epoch_manager: &dyn EpochManagerAdapter,
) -> anyhow::Result<()> {
let chain_store = ChainStore::new(
store.clone(),
near_config.genesis.config.genesis_height,
near_config.client_config.save_trie_changes,
near_config.genesis.config.transaction_validity_period,
);
let epoch_manager =
EpochManager::new_from_genesis_config(store.clone(), &near_config.genesis.config)?;

let head = chain_store.final_head()?;
let block = chain_store.get_block(&head.last_block_hash)?;
Expand Down Expand Up @@ -194,6 +196,7 @@ pub fn spawn_trie_metrics_loop(
near_config: NearConfig,
store: Store,
period: Duration,
epoch_manager: Arc<dyn EpochManagerAdapter>,
) -> anyhow::Result<ArbiterHandle> {
tracing::debug!(target:"metrics", "Spawning the trie metrics loop.");
let arbiter = actix_rt::Arbiter::new();
Expand All @@ -208,7 +211,7 @@ pub fn spawn_trie_metrics_loop(
interval.tick().await;

let start_time = std::time::Instant::now();
let result = export_postponed_receipt_count(&near_config, &store);
let result = export_postponed_receipt_count(&near_config, &store, epoch_manager.as_ref());
if let Err(err) = result {
tracing::error!(target: "metrics", "Error when exporting postponed receipts count {err}.");
};
Expand Down

0 comments on commit 8097202

Please sign in to comment.