Skip to content

Commit 4b95f2c

Browse files
aborg-devNikolay Kurtov
authored andcommitted
Fix blips in the transaction pool size metric (#9174)
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
1 parent 20926fa commit 4b95f2c

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

chain/pool/src/lib.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,12 @@ impl<'a> PoolIterator for PoolIteratorWrapper<'a> {
218218
self.pool.last_used_key = key;
219219
let mut transactions =
220220
self.pool.transactions.remove(&key).expect("just checked existence");
221-
// See the comment in `insert_transaction` above where we increase the size for
222-
// reasoning why panicing here catches a logic error.
223-
self.pool.total_transaction_size = self
224-
.pool
225-
.total_transaction_size
226-
.checked_sub(transactions.iter().map(|tx| tx.get_size()).sum())
227-
.expect("Total transaction size dropped below zero");
228-
metrics::TRANSACTION_POOL_SIZE.set(self.pool.transaction_size() as i64);
229221
transactions.sort_by_key(|st| std::cmp::Reverse(st.transaction.nonce));
230222
self.sorted_groups.push_back(TransactionGroup {
231223
key,
232224
transactions,
233225
removed_transaction_hashes: vec![],
226+
removed_transaction_size: 0,
234227
});
235228
Some(self.sorted_groups.back_mut().expect("just pushed"))
236229
} else {
@@ -260,14 +253,15 @@ impl<'a> Drop for PoolIteratorWrapper<'a> {
260253
for hash in group.removed_transaction_hashes {
261254
self.pool.unique_transactions.remove(&hash);
262255
}
256+
// See the comment in `insert_transaction` where we increase the size for reasoning
257+
// why panicing here catches a logic error.
258+
self.pool.total_transaction_size = self
259+
.pool
260+
.total_transaction_size
261+
.checked_sub(group.removed_transaction_size)
262+
.expect("Total transaction size dropped below zero");
263+
263264
if !group.transactions.is_empty() {
264-
// See the comment in `insert_transaction` where we increase the size for reasoning
265-
// why panicing here catches a logic error.
266-
self.pool.total_transaction_size = self
267-
.pool
268-
.total_transaction_size
269-
.checked_add(group.transactions.iter().map(|tx| tx.get_size()).sum())
270-
.expect("Total transaction size is too large");
271265
self.pool.transactions.insert(group.key, group.transactions);
272266
}
273267
}

chain/pool/src/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub struct TransactionGroup {
2121
pub(crate) transactions: Vec<SignedTransaction>,
2222
/// Hashes of the transactions that were pulled from the group using `.next()`.
2323
pub(crate) removed_transaction_hashes: Vec<CryptoHash>,
24+
/// Total size of transactions that were pulled from the group using `.next()`.
25+
pub(crate) removed_transaction_size: u64,
2426
}
2527

2628
impl TransactionGroup {
@@ -29,6 +31,7 @@ impl TransactionGroup {
2931
pub fn next(&mut self) -> Option<SignedTransaction> {
3032
if let Some(tx) = self.transactions.pop() {
3133
self.removed_transaction_hashes.push(tx.get_hash());
34+
self.removed_transaction_size += tx.get_size();
3235
Some(tx)
3336
} else {
3437
None

0 commit comments

Comments
 (0)