Add metrics for the non-finalized state and queue (#1263)
This commit is contained in:
parent
128643d81e
commit
41788c3f27
|
|
@ -50,11 +50,13 @@ impl NonFinalizedState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.update_metrics_for_chains();
|
||||||
|
|
||||||
// return the finalized block
|
// return the finalized block
|
||||||
finalized_block
|
finalized_block
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Commit block to the non-finalize state.
|
/// Commit block to the non-finalized state.
|
||||||
pub fn commit_block(&mut self, block: Arc<Block>) {
|
pub fn commit_block(&mut self, block: Arc<Block>) {
|
||||||
let parent_hash = block.header.previous_block_hash;
|
let parent_hash = block.header.previous_block_hash;
|
||||||
|
|
||||||
|
|
@ -68,16 +70,18 @@ impl NonFinalizedState {
|
||||||
})
|
})
|
||||||
.expect("commit_block is only called with blocks that are ready to be commited");
|
.expect("commit_block is only called with blocks that are ready to be commited");
|
||||||
|
|
||||||
parent_chain.push(block);
|
parent_chain.push(block.clone());
|
||||||
self.chain_set.insert(parent_chain);
|
self.chain_set.insert(parent_chain);
|
||||||
|
self.update_metrics_for_committed_block(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Commit block to the non-finalized state as a new chain where its parent
|
/// Commit block to the non-finalized state as a new chain where its parent
|
||||||
/// is the finalized tip.
|
/// is the finalized tip.
|
||||||
pub fn commit_new_chain(&mut self, block: Arc<Block>) {
|
pub fn commit_new_chain(&mut self, block: Arc<Block>) {
|
||||||
let mut chain = Chain::default();
|
let mut chain = Chain::default();
|
||||||
chain.push(block);
|
chain.push(block.clone());
|
||||||
self.chain_set.insert(Box::new(chain));
|
self.chain_set.insert(Box::new(chain));
|
||||||
|
self.update_metrics_for_committed_block(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the length of the non-finalized portion of the current best chain.
|
/// Returns the length of the non-finalized portion of the current best chain.
|
||||||
|
|
@ -182,4 +186,34 @@ impl NonFinalizedState {
|
||||||
.next_back()
|
.next_back()
|
||||||
.map(|box_chain| box_chain.deref())
|
.map(|box_chain| box_chain.deref())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update the metrics after `block` is committed
|
||||||
|
fn update_metrics_for_committed_block(&self, block: Arc<Block>) {
|
||||||
|
let height = block.coinbase_height().unwrap();
|
||||||
|
|
||||||
|
metrics::counter!("state.memory.committed.block.count", 1);
|
||||||
|
metrics::gauge!("state.memory.committed.block.height", height.0 as _);
|
||||||
|
|
||||||
|
if self
|
||||||
|
.best_chain()
|
||||||
|
.unwrap()
|
||||||
|
.blocks
|
||||||
|
.iter()
|
||||||
|
.next_back()
|
||||||
|
.unwrap()
|
||||||
|
.1
|
||||||
|
== &block
|
||||||
|
{
|
||||||
|
metrics::counter!("state.memory.best.committed.block.count", 1);
|
||||||
|
metrics::gauge!("state.memory.best.committed.block.height", height.0 as _);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.update_metrics_for_chains();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update the metrics after `self.chain_set` is modified
|
||||||
|
fn update_metrics_for_chains(&self) {
|
||||||
|
metrics::gauge!("state.memory.chain.count", self.chain_set.len() as _);
|
||||||
|
metrics::gauge!("state.memory.best.chain.length", self.best_chain_len() as _);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,7 @@ impl QueuedBlocks {
|
||||||
assert!(inserted, "hashes must be unique");
|
assert!(inserted, "hashes must be unique");
|
||||||
|
|
||||||
tracing::trace!(num_blocks = %self.blocks.len(), %parent_hash, ?new_height, "Finished queueing a new block");
|
tracing::trace!(num_blocks = %self.blocks.len(), %parent_hash, ?new_height, "Finished queueing a new block");
|
||||||
|
self.update_metrics();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Dequeue and return all blocks that were waiting for the arrival of
|
/// Dequeue and return all blocks that were waiting for the arrival of
|
||||||
|
|
@ -73,6 +74,7 @@ impl QueuedBlocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
tracing::trace!(num_blocks = %self.blocks.len(), "Finished dequeuing blocks waiting for parent hash",);
|
tracing::trace!(num_blocks = %self.blocks.len(), "Finished dequeuing blocks waiting for parent hash",);
|
||||||
|
self.update_metrics();
|
||||||
|
|
||||||
queued_children
|
queued_children
|
||||||
}
|
}
|
||||||
|
|
@ -99,10 +101,24 @@ impl QueuedBlocks {
|
||||||
.expect("parent is present")
|
.expect("parent is present")
|
||||||
.remove(&hash);
|
.remove(&hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tracing::trace!(num_blocks = %self.blocks.len(), "Finished pruning blocks at or beneath the finalized tip height",);
|
||||||
|
self.update_metrics();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return the queued block if it has already been registered
|
/// Return the queued block if it has already been registered
|
||||||
pub fn get_mut(&mut self, hash: &block::Hash) -> Option<&mut QueuedBlock> {
|
pub fn get_mut(&mut self, hash: &block::Hash) -> Option<&mut QueuedBlock> {
|
||||||
self.blocks.get_mut(&hash)
|
self.blocks.get_mut(&hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Update metrics after the queue is modified
|
||||||
|
fn update_metrics(&self) {
|
||||||
|
if let Some(max_height) = self.by_height.keys().next_back() {
|
||||||
|
metrics::gauge!("state.memory.queued.max.height", max_height.0 as i64);
|
||||||
|
} else {
|
||||||
|
// use -1 as a sentinel value for "None", because 0 is a valid height
|
||||||
|
metrics::gauge!("state.memory.queued.max.height", -1);
|
||||||
|
}
|
||||||
|
metrics::gauge!("state.memory.queued.block.count", self.blocks.len() as _);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue