Skip to content

Commit

Permalink
Fix blips in the transaction pool size metric (#9174)
Browse files Browse the repository at this point in the history
Right now the metric has noticeable blips due to rapid change when transactions are drawn from the pool: https://nearinc.grafana.net/goto/YZwyDllVR?orgId=1 To avoid this, we only decrease the metric after the pool iterator is dropped.

This is a part of #3284
  • Loading branch information
aborg-dev authored and nikurt committed Jun 13, 2023
1 parent 20926fa commit 4b95f2c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
24 changes: 9 additions & 15 deletions chain/pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,12 @@ impl<'a> PoolIterator for PoolIteratorWrapper<'a> {
self.pool.last_used_key = key;
let mut transactions =
self.pool.transactions.remove(&key).expect("just checked existence");
// See the comment in `insert_transaction` above where we increase the size for
// reasoning why panicing here catches a logic error.
self.pool.total_transaction_size = self
.pool
.total_transaction_size
.checked_sub(transactions.iter().map(|tx| tx.get_size()).sum())
.expect("Total transaction size dropped below zero");
metrics::TRANSACTION_POOL_SIZE.set(self.pool.transaction_size() as i64);
transactions.sort_by_key(|st| std::cmp::Reverse(st.transaction.nonce));
self.sorted_groups.push_back(TransactionGroup {
key,
transactions,
removed_transaction_hashes: vec![],
removed_transaction_size: 0,
});
Some(self.sorted_groups.back_mut().expect("just pushed"))
} else {
Expand Down Expand Up @@ -260,14 +253,15 @@ impl<'a> Drop for PoolIteratorWrapper<'a> {
for hash in group.removed_transaction_hashes {
self.pool.unique_transactions.remove(&hash);
}
// See the comment in `insert_transaction` where we increase the size for reasoning
// why panicing here catches a logic error.
self.pool.total_transaction_size = self
.pool
.total_transaction_size
.checked_sub(group.removed_transaction_size)
.expect("Total transaction size dropped below zero");

if !group.transactions.is_empty() {
// See the comment in `insert_transaction` where we increase the size for reasoning
// why panicing here catches a logic error.
self.pool.total_transaction_size = self
.pool
.total_transaction_size
.checked_add(group.transactions.iter().map(|tx| tx.get_size()).sum())
.expect("Total transaction size is too large");
self.pool.transactions.insert(group.key, group.transactions);
}
}
Expand Down
3 changes: 3 additions & 0 deletions chain/pool/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct TransactionGroup {
pub(crate) transactions: Vec<SignedTransaction>,
/// Hashes of the transactions that were pulled from the group using `.next()`.
pub(crate) removed_transaction_hashes: Vec<CryptoHash>,
/// Total size of transactions that were pulled from the group using `.next()`.
pub(crate) removed_transaction_size: u64,
}

impl TransactionGroup {
Expand All @@ -29,6 +31,7 @@ impl TransactionGroup {
pub fn next(&mut self) -> Option<SignedTransaction> {
if let Some(tx) = self.transactions.pop() {
self.removed_transaction_hashes.push(tx.get_hash());
self.removed_transaction_size += tx.get_size();
Some(tx)
} else {
None
Expand Down

0 comments on commit 4b95f2c

Please sign in to comment.