Skip to content

Commit 8fd8feb

Browse files
authored
Make transaction pool metrics per-shard (#9185)
As there is actually 1 transaction pool per shard and their metrics mix together. See #9174 (comment) for some details.
1 parent d584d02 commit 8fd8feb

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

chain/chunks/src/client.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pub struct ShardedTransactionPool {
4444

4545
impl ShardedTransactionPool {
4646
pub fn new(rng_seed: RngSeed, pool_size_limit: Option<u64>) -> Self {
47-
TransactionPool::init_metrics();
4847
Self { tx_pools: HashMap::new(), rng_seed, pool_size_limit }
4948
}
5049

@@ -80,7 +79,11 @@ impl ShardedTransactionPool {
8079

8180
fn pool_for_shard(&mut self, shard_id: ShardId) -> &mut TransactionPool {
8281
self.tx_pools.entry(shard_id).or_insert_with(|| {
83-
TransactionPool::new(Self::random_seed(&self.rng_seed, shard_id), self.pool_size_limit)
82+
TransactionPool::new(
83+
Self::random_seed(&self.rng_seed, shard_id),
84+
self.pool_size_limit,
85+
&shard_id.to_string(),
86+
)
8487
})
8588
}
8689

chain/pool/src/lib.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
44
use crate::types::{PoolIterator, PoolKey, TransactionGroup};
55
use borsh::BorshSerialize;
66
use near_crypto::PublicKey;
7+
use near_o11y::metrics::prometheus::core::{AtomicI64, GenericGauge};
78
use near_primitives::epoch_manager::RngSeed;
89
use near_primitives::hash::{hash, CryptoHash};
910
use near_primitives::transaction::SignedTransaction;
@@ -39,26 +40,37 @@ pub struct TransactionPool {
3940
total_transaction_size_limit: Option<u64>,
4041
/// Total size of transactions in the pool measured in bytes.
4142
total_transaction_size: u64,
43+
/// Metrics tracked for transaction pool.
44+
transaction_pool_count_metric: GenericGauge<AtomicI64>,
45+
transaction_pool_size_metric: GenericGauge<AtomicI64>,
4246
}
4347

4448
impl TransactionPool {
45-
pub fn new(key_seed: RngSeed, total_transaction_size_limit: Option<u64>) -> Self {
49+
pub fn new(
50+
key_seed: RngSeed,
51+
total_transaction_size_limit: Option<u64>,
52+
metrics_label: &str,
53+
) -> Self {
54+
let transaction_pool_count_metric =
55+
metrics::TRANSACTION_POOL_COUNT.with_label_values(&[metrics_label]);
56+
let transaction_pool_size_metric =
57+
metrics::TRANSACTION_POOL_SIZE.with_label_values(&[metrics_label]);
58+
// A `get()` call initializes a metric even if its value is zero.
59+
transaction_pool_count_metric.get();
60+
transaction_pool_size_metric.get();
61+
4662
Self {
4763
key_seed,
4864
transactions: BTreeMap::new(),
4965
unique_transactions: HashSet::new(),
5066
last_used_key: CryptoHash::default(),
5167
total_transaction_size_limit,
5268
total_transaction_size: 0,
69+
transaction_pool_count_metric,
70+
transaction_pool_size_metric,
5371
}
5472
}
5573

56-
pub fn init_metrics() {
57-
// A `get()` call initializes a metric even if its value is zero.
58-
metrics::TRANSACTION_POOL_COUNT.get();
59-
metrics::TRANSACTION_POOL_SIZE.get();
60-
}
61-
6274
fn key(&self, account_id: &AccountId, public_key: &PublicKey) -> PoolKey {
6375
let mut v = public_key.try_to_vec().unwrap();
6476
v.extend_from_slice(&self.key_seed);
@@ -98,8 +110,8 @@ impl TransactionPool {
98110
.or_insert_with(Vec::new)
99111
.push(signed_transaction);
100112

101-
metrics::TRANSACTION_POOL_COUNT.inc();
102-
metrics::TRANSACTION_POOL_SIZE.set(self.total_transaction_size as i64);
113+
self.transaction_pool_count_metric.inc();
114+
self.transaction_pool_size_metric.set(self.total_transaction_size as i64);
103115
InsertTransactionResult::Success
104116
}
105117

@@ -150,8 +162,8 @@ impl TransactionPool {
150162
}
151163

152164
// We can update metrics only once for the whole batch of transactions.
153-
metrics::TRANSACTION_POOL_COUNT.set(self.unique_transactions.len() as i64);
154-
metrics::TRANSACTION_POOL_SIZE.set(self.total_transaction_size as i64);
165+
self.transaction_pool_count_metric.set(self.unique_transactions.len() as i64);
166+
self.transaction_pool_size_metric.set(self.total_transaction_size as i64);
155167
}
156168

157169
/// Returns the number of unique transactions in the pool.
@@ -231,7 +243,7 @@ impl<'a> PoolIterator for PoolIteratorWrapper<'a> {
231243
if sorted_group.transactions.is_empty() {
232244
for hash in sorted_group.removed_transaction_hashes {
233245
if self.pool.unique_transactions.remove(&hash) {
234-
metrics::TRANSACTION_POOL_COUNT.dec();
246+
self.pool.transaction_pool_count_metric.dec();
235247
}
236248
}
237249
} else {
@@ -266,8 +278,8 @@ impl<'a> Drop for PoolIteratorWrapper<'a> {
266278
}
267279
}
268280
// We can update metrics only once for the whole batch of transactions.
269-
metrics::TRANSACTION_POOL_COUNT.set(self.pool.unique_transactions.len() as i64);
270-
metrics::TRANSACTION_POOL_SIZE.set(self.pool.transaction_size() as i64);
281+
self.pool.transaction_pool_count_metric.set(self.pool.unique_transactions.len() as i64);
282+
self.pool.transaction_pool_size_metric.set(self.pool.transaction_size() as i64);
271283
}
272284
}
273285

@@ -313,7 +325,7 @@ mod tests {
313325
mut transactions: Vec<SignedTransaction>,
314326
expected_weight: u32,
315327
) -> (Vec<u64>, TransactionPool) {
316-
let mut pool = TransactionPool::new(TEST_SEED, None);
328+
let mut pool = TransactionPool::new(TEST_SEED, None, "");
317329
let mut rng = thread_rng();
318330
transactions.shuffle(&mut rng);
319331
for tx in transactions {
@@ -424,7 +436,7 @@ mod tests {
424436
})
425437
.collect::<Vec<_>>();
426438

427-
let mut pool = TransactionPool::new(TEST_SEED, None);
439+
let mut pool = TransactionPool::new(TEST_SEED, None, "");
428440
let mut rng = thread_rng();
429441
transactions.shuffle(&mut rng);
430442
for tx in transactions.clone() {
@@ -536,7 +548,7 @@ mod tests {
536548

537549
#[test]
538550
fn test_transaction_pool_size() {
539-
let mut pool = TransactionPool::new(TEST_SEED, None);
551+
let mut pool = TransactionPool::new(TEST_SEED, None, "");
540552
let transactions = generate_transactions("alice.near", "alice.near", 1, 100);
541553
let mut total_transaction_size = 0;
542554
// Adding transactions increases the size.
@@ -560,7 +572,7 @@ mod tests {
560572
// Each transaction is at least 1 byte in size, so the last transaction will not fit.
561573
let pool_size_limit =
562574
transactions.iter().map(|tx| tx.get_size()).sum::<u64>().checked_sub(1).unwrap();
563-
let mut pool = TransactionPool::new(TEST_SEED, Some(pool_size_limit));
575+
let mut pool = TransactionPool::new(TEST_SEED, Some(pool_size_limit), "");
564576
for (i, tx) in transactions.iter().cloned().enumerate() {
565577
if i + 1 < transactions.len() {
566578
assert_eq!(pool.insert_transaction(tx), InsertTransactionResult::Success);

chain/pool/src/metrics.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
use near_o11y::metrics::IntGauge;
1+
use near_o11y::metrics::IntGaugeVec;
22
use once_cell::sync::Lazy;
33

4-
pub static TRANSACTION_POOL_COUNT: Lazy<IntGauge> = Lazy::new(|| {
5-
near_o11y::metrics::try_create_int_gauge(
4+
pub static TRANSACTION_POOL_COUNT: Lazy<IntGaugeVec> = Lazy::new(|| {
5+
near_o11y::metrics::try_create_int_gauge_vec(
66
"near_transaction_pool_entries",
7-
"Total number of transactions currently in the pools tracked by the node",
7+
"Total number of transactions currently tracked by the node in a given shard pool",
8+
&["shard_id"],
89
)
910
.unwrap()
1011
});
1112

12-
pub static TRANSACTION_POOL_SIZE: Lazy<IntGauge> = Lazy::new(|| {
13-
near_o11y::metrics::try_create_int_gauge(
13+
pub static TRANSACTION_POOL_SIZE: Lazy<IntGaugeVec> = Lazy::new(|| {
14+
near_o11y::metrics::try_create_int_gauge_vec(
1415
"near_transaction_pool_size",
15-
"Total size in bytes of transactions currently in the pools tracked by the node",
16+
"Total size in bytes of transactions currently tracked by the node in a given shard pool",
17+
&["shard_id"],
1618
)
1719
.unwrap()
1820
});

0 commit comments

Comments
 (0)