Skip to content

Commit

Permalink
Do timeout lock for beacon state metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Jun 28, 2022
1 parent 1d67297 commit 4ffdb41
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions beacon_node/beacon_chain/src/canonical_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ impl<T> CanonicalHeadRwLock<T> {
self.0.read()
}

pub fn try_read_for(&self, timeout: Duration) -> Option<RwLockReadGuard<T>> {
self.0.try_read_for(timeout)
}

pub fn write(&self) -> RwLockWriteGuard<T> {
self.0.write()
}
Expand Down
14 changes: 10 additions & 4 deletions beacon_node/beacon_chain/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::observed_attesters::SlotSubcommitteeIndex;
use crate::types::consts::altair::SYNC_COMMITTEE_SUBNET_COUNT;
use crate::{BeaconChain, BeaconChainError, BeaconChainTypes};
use crate::{BeaconChain, BeaconChainTypes};
use lazy_static::lazy_static;
pub use lighthouse_metrics::*;
use slot_clock::SlotClock;
Expand All @@ -10,6 +10,9 @@ use types::{BeaconState, Epoch, EthSpec, Hash256, Slot};
/// The maximum time to wait for the snapshot cache lock during a metrics scrape.
const SNAPSHOT_CACHE_TIMEOUT: Duration = Duration::from_millis(100);

/// The time to wait for the canonical head lock before just proceeding with other metrics.
const CANONICAL_HEAD_LOCK_TIMEOUT: Duration = Duration::from_secs(1);

lazy_static! {
/*
* Block Processing
Expand Down Expand Up @@ -927,10 +930,13 @@ lazy_static! {
/// Scrape the `beacon_chain` for metrics that are not constantly updated (e.g., the present slot,
/// head state info, etc) and update the Prometheus `DEFAULT_REGISTRY`.
pub fn scrape_for_metrics<T: BeaconChainTypes>(beacon_chain: &BeaconChain<T>) {
let _ = beacon_chain.with_head(|head| {
if let Some(canonical_head) = beacon_chain
.canonical_head
.try_read_for(CANONICAL_HEAD_LOCK_TIMEOUT)
{
let head = &canonical_head.head_snapshot;
scrape_head_state(&head.beacon_state, head.beacon_state_root());
Ok::<_, BeaconChainError>(())
});
}

if let Some(slot) = beacon_chain.slot_clock.now() {
scrape_attestation_observation(slot, beacon_chain);
Expand Down

0 comments on commit 4ffdb41

Please sign in to comment.