Add a maximum queued height metric to the finalized state (#1262)

* Add a maximum queued height metric to the finalized state
And rename all the finalized state metrics to contain "finalized".

* Use i32 and -1 instead of Option<Height>
Co-authored-by: Jane Lusby <jlusby42@gmail.com>
This commit is contained in:
teor 2020-11-13 09:49:55 +10:00 committed by GitHub
parent 96b7572bb5
commit 4e07719a7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 3 deletions

View File

@ -38,6 +38,7 @@ use self::sled_format::TransactionLocation;
pub struct FinalizedState {
/// Queued blocks that arrived out of order, indexed by their parent block hash.
queued_by_prev_hash: HashMap<block::Hash, QueuedBlock>,
max_queued_height: i64,
hash_by_height: sled::Tree,
height_by_hash: sled::Tree,
@ -67,6 +68,7 @@ impl FinalizedState {
let new_state = Self {
queued_by_prev_hash: HashMap::new(),
max_queued_height: -1,
hash_by_height: db.open_tree(b"hash_by_height").unwrap(),
height_by_hash: db.open_tree(b"height_by_hash").unwrap(),
block_by_height: db.open_tree(b"block_by_height").unwrap(),
@ -173,6 +175,7 @@ impl FinalizedState {
/// queued block (and any of its descendants) can be committed to the state.
pub fn queue_and_commit_finalized_blocks(&mut self, queued_block: QueuedBlock) {
let prev_hash = queued_block.block.header.previous_block_hash;
let height = queued_block.block.coinbase_height().unwrap();
self.queued_by_prev_hash.insert(prev_hash, queued_block);
while let Some(queued_block) = self.queued_by_prev_hash.remove(&self.finalized_tip_hash()) {
@ -181,12 +184,21 @@ impl FinalizedState {
.coinbase_height()
.expect("valid blocks must have a height");
self.commit_finalized(queued_block);
metrics::counter!("state.committed.block.count", 1);
metrics::gauge!("state.committed.block.height", height.0 as _);
metrics::counter!("state.finalized.committed.block.count", 1);
metrics::gauge!("state.finalized.committed.block.height", height.0 as _);
}
if self.queued_by_prev_hash.is_empty() {
// use -1 as a sentinel value for "None", because 0 is a valid height
self.max_queued_height = -1;
} else {
self.max_queued_height = std::cmp::max(self.max_queued_height, height.0 as _);
}
metrics::gauge!("state.finalized.queued.max.height", self.max_queued_height);
metrics::gauge!(
"state.queued.block.count",
"state.finalized.queued.block.count",
self.queued_by_prev_hash.len() as _
);
}