Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metrics related to the epoch, sync state and node version #6004

Merged
merged 30 commits into from Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
19fb3fa
Add a number of new new metrics:
nikurt Jan 6, 2022
af02977
Merge remote-tracking branch 'origin/master' into nikurt-metrics
nikurt Jan 6, 2022
d45e1f5
Merge remote-tracking branch 'origin/master' into nikurt-metrics
nikurt Jan 6, 2022
f55f36e
Minor: change metric name and reorder enum_to_int conversion.
nikurt Jan 7, 2022
8d7b75a
Merge remote-tracking branch 'origin/master' into nikurt-metrics
nikurt Jan 7, 2022
6803a90
Minor: change metric name and reorder enum_to_int conversion.
nikurt Jan 7, 2022
06797e5
Merge remote-tracking branch 'origin/master' into nikurt-metrics
nikurt Jan 11, 2022
d7995e0
Rename `cast_to_into` to `repr`
nikurt Jan 11, 2022
ad63c4b
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 11, 2022
9d9278a
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 11, 2022
f72268e
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 11, 2022
cf267ad
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 11, 2022
259d447
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 11, 2022
bd7c036
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 11, 2022
e2c13d9
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 11, 2022
25e7e16
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 12, 2022
cbf2149
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 12, 2022
d908135
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 12, 2022
3681535
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 12, 2022
8b16834
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 12, 2022
5e8f839
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 12, 2022
51724fb
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 12, 2022
231d678
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 12, 2022
16d5832
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 12, 2022
3a8b622
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 13, 2022
63f5245
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 13, 2022
c7a5a0d
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 13, 2022
9ec6ce7
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 13, 2022
17deccf
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 13, 2022
d507892
Merge refs/heads/master into nikurt-metrics
near-bulldozer[bot] Jan 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions chain/chain/src/chain.rs
Expand Up @@ -4397,6 +4397,7 @@ impl<'a> ChainUpdate<'a> {
let tip = Tip::from_header(header);
self.chain_store_update.save_header_head_if_not_challenged(&tip)?;
debug!(target: "chain", "Header head updated to {} at {}", tip.last_block_hash, tip.height);
metrics::HEADER_HEIGHT_HEAD.set(tip.height as i64);
nikurt marked this conversation as resolved.
Show resolved Hide resolved

Ok(Some(tip))
} else {
Expand Down
3 changes: 3 additions & 0 deletions chain/chain/src/metrics.rs
Expand Up @@ -38,3 +38,6 @@ pub static VALIDATOR_ACTIVE_TOTAL: Lazy<IntGauge> = Lazy::new(|| {
});
pub static NUM_ORPHANS: Lazy<IntGauge> =
Lazy::new(|| try_create_int_gauge("near_num_orphans", "Number of orphan blocks.").unwrap());
pub static HEADER_HEIGHT_HEAD: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge("near_header_height_head", "Height of the header head").unwrap()
});
20 changes: 17 additions & 3 deletions chain/chain/src/test_utils.rs
Expand Up @@ -16,7 +16,7 @@ use near_primitives::block_header::{Approval, ApprovalInner};
use near_primitives::challenge::ChallengesResult;
use near_primitives::epoch_manager::block_info::BlockInfo;
use near_primitives::epoch_manager::epoch_info::EpochInfo;
use near_primitives::errors::InvalidTxError;
use near_primitives::errors::{EpochError, InvalidTxError};
use near_primitives::hash::{hash, CryptoHash};
use near_primitives::receipt::{ActionReceipt, Receipt, ReceiptEnum};
use near_primitives::serialize::to_base;
Expand All @@ -30,8 +30,8 @@ use near_primitives::transaction::{
};
use near_primitives::types::validator_stake::{ValidatorStake, ValidatorStakeIter};
use near_primitives::types::{
AccountId, ApprovalStake, Balance, BlockHeight, EpochId, Gas, Nonce, NumBlocks, NumShards,
ShardId, StateChangesForSplitStates, StateRoot, StateRootNode,
AccountId, ApprovalStake, Balance, BlockHeight, EpochHeight, EpochId, Gas, Nonce, NumBlocks,
NumShards, ShardId, StateChangesForSplitStates, StateRoot, StateRootNode,
};
use near_primitives::validator_signer::InMemoryValidatorSigner;
use near_primitives::version::{ProtocolVersion, PROTOCOL_VERSION};
Expand Down Expand Up @@ -1204,6 +1204,20 @@ impl RuntimeAdapter for KeyValueRuntime {
) -> Result<HashMap<ShardUId, StateRoot>, Error> {
Ok(HashMap::new())
}

fn get_protocol_upgrade_block_height(
&self,
_block_hash: CryptoHash,
) -> Result<Option<BlockHeight>, EpochError> {
Ok(None)
}

fn get_epoch_height_from_prev_block(
&self,
_prev_block_hash: &CryptoHash,
) -> Result<EpochHeight, Error> {
Ok(0)
}
}

pub fn setup() -> (Chain, Arc<KeyValueRuntime>, Arc<InMemoryValidatorSigner>) {
Expand Down
17 changes: 14 additions & 3 deletions chain/chain/src/types.rs
Expand Up @@ -16,16 +16,16 @@ use near_primitives::challenge::{ChallengesResult, SlashedValidator};
use near_primitives::checked_feature;
use near_primitives::epoch_manager::block_info::BlockInfo;
use near_primitives::epoch_manager::epoch_info::EpochInfo;
use near_primitives::errors::InvalidTxError;
use near_primitives::errors::{EpochError, InvalidTxError};
use near_primitives::hash::{hash, CryptoHash};
use near_primitives::merkle::{merklize, MerklePath};
use near_primitives::receipt::Receipt;
use near_primitives::sharding::{ChunkHash, ReceiptList, ShardChunkHeader};
use near_primitives::transaction::{ExecutionOutcomeWithId, SignedTransaction};
use near_primitives::types::validator_stake::{ValidatorStake, ValidatorStakeIter};
use near_primitives::types::{
AccountId, ApprovalStake, Balance, BlockHeight, BlockHeightDelta, EpochId, Gas, MerkleHash,
NumBlocks, ShardId, StateChangesForSplitStates, StateRoot, StateRootNode,
AccountId, ApprovalStake, Balance, BlockHeight, BlockHeightDelta, EpochHeight, EpochId, Gas,
MerkleHash, NumBlocks, ShardId, StateChangesForSplitStates, StateRoot, StateRootNode,
};
use near_primitives::version::{
ProtocolVersion, MIN_GAS_PRICE_NEP_92, MIN_GAS_PRICE_NEP_92_FIX, MIN_PROTOCOL_VERSION_NEP_92,
Expand Down Expand Up @@ -516,6 +516,12 @@ pub trait RuntimeAdapter: Send + Sync {
/// Get epoch id given hash of previous block.
fn get_epoch_id_from_prev_block(&self, parent_hash: &CryptoHash) -> Result<EpochId, Error>;

/// Get epoch height given hash of previous block.
fn get_epoch_height_from_prev_block(
&self,
parent_hash: &CryptoHash,
) -> Result<EpochHeight, Error>;

/// Get next epoch id given hash of previous block.
fn get_next_epoch_id_from_prev_block(&self, parent_hash: &CryptoHash)
-> Result<EpochId, Error>;
Expand Down Expand Up @@ -792,6 +798,11 @@ pub trait RuntimeAdapter: Send + Sync {
})
.collect()
}

fn get_protocol_upgrade_block_height(
&self,
block_hash: CryptoHash,
) -> Result<Option<BlockHeight>, EpochError>;
}

/// The last known / checked height and time when we have processed it.
Expand Down
12 changes: 12 additions & 0 deletions chain/client-primitives/src/types.rs
Expand Up @@ -153,6 +153,18 @@ impl SyncStatus {
_ => true,
}
}

pub fn cast_to_int(&self) -> u8 {
nikurt marked this conversation as resolved.
Show resolved Hide resolved
match self {
SyncStatus::AwaitingPeers => 0,
SyncStatus::NoSync => 1,
nikurt marked this conversation as resolved.
Show resolved Hide resolved
SyncStatus::EpochSync { epoch_ord: _ } => 2,
SyncStatus::HeaderSync { current_height: _, highest_height: _ } => 3,
SyncStatus::StateSync(_, _) => 4,
SyncStatus::StateSyncDone => 5,
SyncStatus::BodySync { current_height: _, highest_height: _ } => 6,
}
}
}

/// Actor message requesting block by id or hash.
Expand Down
9 changes: 9 additions & 0 deletions chain/client/src/client_actor.rs
Expand Up @@ -1497,6 +1497,15 @@ impl ClientActor {
&act.node_id,
&act.network_info,
validator_info,
act.client
.runtime_adapter
.get_epoch_height_from_prev_block(&head.prev_block_hash)
.unwrap_or(0),
act.client
.runtime_adapter
.get_protocol_upgrade_block_height(head.last_block_hash)
.unwrap_or(None)
.unwrap_or(0),
);

act.log_summary(ctx);
Expand Down
11 changes: 9 additions & 2 deletions chain/client/src/info.rs
Expand Up @@ -15,9 +15,9 @@ use near_primitives::serialize::to_base;
use near_primitives::telemetry::{
TelemetryAgentInfo, TelemetryChainInfo, TelemetryInfo, TelemetrySystemInfo,
};
use near_primitives::types::{BlockHeight, Gas};
use near_primitives::types::{BlockHeight, EpochHeight, Gas};
use near_primitives::validator_signer::ValidatorSigner;
use near_primitives::version::Version;
use near_primitives::version::{Version, DB_VERSION, PROTOCOL_VERSION};
use near_telemetry::{telemetry, TelemetryActor};

use crate::{metrics, SyncStatus};
Expand Down Expand Up @@ -88,6 +88,8 @@ impl InfoHelper {
node_id: &PeerId,
network_info: &NetworkInfo,
validator_info: Option<ValidatorInfoHelper>,
epoch_height: EpochHeight,
protocol_upgrade_block_height: BlockHeight,
) {
let (cpu_usage, memory_usage) = if let Some(pid) = self.pid {
if self.sys.refresh_process(pid) {
Expand Down Expand Up @@ -168,6 +170,10 @@ impl InfoHelper {
(metrics::MEMORY_USAGE.set((memory_usage * 1024) as i64));
let teragas = 1_000_000_000_000u64;
(metrics::AVG_TGAS_USAGE.set((avg_gas_used as f64 / teragas as f64).round() as i64));
(metrics::EPOCH_HEIGHT.set(epoch_height as i64));
(metrics::PROTOCOL_UPGRADE_BLOCK_HEIGHT.set(protocol_upgrade_block_height as i64));
(metrics::NODE_PROTOCOL_VERSION.set(PROTOCOL_VERSION as i64));
(metrics::NODE_DB_VERSION.set(DB_VERSION as i64));

self.started = Clock::instant();
self.num_blocks_processed = 0;
Expand Down Expand Up @@ -211,6 +217,7 @@ fn display_sync_status(
head: &Tip,
genesis_height: BlockHeight,
) -> String {
metrics::SYNC_STATUS.set(sync_status.cast_to_int() as i64);
match sync_status {
SyncStatus::AwaitingPeers => format!("#{:>8} Waiting for peers", head.height),
SyncStatus::NoSync => format!("#{:>8} {:>44}", head.height, head.last_block_hash),
Expand Down
20 changes: 20 additions & 0 deletions chain/client/src/metrics.rs
Expand Up @@ -75,3 +75,23 @@ pub static BLOCKS_AHEAD_OF_HEAD: Lazy<IntGauge> = Lazy::new(|| {
)
.unwrap()
});
pub static SYNC_STATUS: Lazy<IntGauge> =
Lazy::new(|| try_create_int_gauge("near_sync_status", "Node sync status").unwrap());
pub static EPOCH_HEIGHT: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge("near_epoch_height", "Height of the epoch at the head of the blockchain")
.unwrap()
});
pub static PROTOCOL_UPGRADE_BLOCK_HEIGHT: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge(
"near_protocol_upgrade_block_height",
"Estimated block height of the protocol upgrade",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, what's the purpose for this metric?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It lets validators set alerts about an imminent protocol upgrade. Multiple validators expressed interest in this alert.

)
.unwrap()
});
pub static NODE_PROTOCOL_VERSION: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge("near_node_protocol_version", "Max protocol version supported by the node")
.unwrap()
});
pub static NODE_DB_VERSION: Lazy<IntGauge> = Lazy::new(|| {
try_create_int_gauge("near_node_db_version", "DB version used by the node").unwrap()
});
20 changes: 20 additions & 0 deletions chain/epoch_manager/src/lib.rs
Expand Up @@ -1475,6 +1475,26 @@ impl EpochManager {
self.epoch_info_aggregator = Some(aggregator);
Ok(())
}

pub fn get_protocol_upgrade_block_height(
&mut self,
block_hash: CryptoHash,
) -> Result<Option<BlockHeight>, EpochError> {
let cur_epoch_info = self.get_epoch_info_from_hash(&block_hash)?.clone();
let next_epoch_id = self.get_next_epoch_id(&block_hash)?;
let next_epoch_info = self.get_epoch_info(&next_epoch_id)?.clone();
if cur_epoch_info.protocol_version() != next_epoch_info.protocol_version() {
let block_info = self.get_block_info(&block_hash)?.clone();
let epoch_length =
self.config.for_protocol_version(cur_epoch_info.protocol_version()).epoch_length;
let estimated_next_epoch_start =
self.get_block_info(block_info.epoch_first_block())?.height() + epoch_length;

Ok(Some(estimated_next_epoch_start))
} else {
Ok(None)
}
}
}

#[cfg(test)]
Expand Down
26 changes: 17 additions & 9 deletions nearcore/src/runtime/mod.rs
Expand Up @@ -232,15 +232,6 @@ impl NightshadeRuntime {
)
}

fn get_epoch_height_from_prev_block(
&self,
prev_block_hash: &CryptoHash,
) -> Result<EpochHeight, Error> {
let mut epoch_manager = self.epoch_manager.as_ref().write().expect(POISONED_LOCK_ERR);
let epoch_id = epoch_manager.get_epoch_id_from_prev_block(prev_block_hash)?;
epoch_manager.get_epoch_info(&epoch_id).map(|info| info.epoch_height()).map_err(Error::from)
}

pub fn get_epoch_id(&self, hash: &CryptoHash) -> Result<EpochId, Error> {
let mut epoch_manager = self.epoch_manager.as_ref().write().expect(POISONED_LOCK_ERR);
epoch_manager.get_epoch_id(hash).map_err(Error::from)
Expand Down Expand Up @@ -1835,6 +1826,23 @@ impl RuntimeAdapter for NightshadeRuntime {
let mut epoch_manager = self.epoch_manager.as_ref().write().expect(POISONED_LOCK_ERR);
Ok(epoch_manager.will_shard_layout_change(parent_hash)?)
}

fn get_epoch_height_from_prev_block(
&self,
prev_block_hash: &CryptoHash,
) -> Result<EpochHeight, Error> {
let mut epoch_manager = self.epoch_manager.as_ref().write().expect(POISONED_LOCK_ERR);
let epoch_id = epoch_manager.get_epoch_id_from_prev_block(prev_block_hash)?;
epoch_manager.get_epoch_info(&epoch_id).map(|info| info.epoch_height()).map_err(Error::from)
}

fn get_protocol_upgrade_block_height(
&self,
block_hash: CryptoHash,
) -> Result<Option<BlockHeight>, EpochError> {
let mut epoch_manager = self.epoch_manager.as_ref().write().expect(POISONED_LOCK_ERR);
epoch_manager.get_protocol_upgrade_block_height(block_hash)
}
}

impl node_runtime::adapter::ViewRuntimeAdapter for NightshadeRuntime {
Expand Down