Skip to content

Commit

Permalink
Add latency metrics to eviction pools routines
Browse files Browse the repository at this point in the history
  • Loading branch information
yohkaz committed Jan 23, 2025
1 parent b263b5c commit f8f7ca1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
42 changes: 41 additions & 1 deletion crates/transaction-pool/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Transaction pool metrics.
use reth_metrics::{
metrics::{Counter, Gauge},
metrics::{Counter, Gauge, Histogram},
Metrics,
};

Expand Down Expand Up @@ -123,3 +123,43 @@ pub struct AllTransactionsMetrics {
/// The current base fee
pub(crate) base_fee: Gauge,
}

/// Blob pool eviction metrics
#[derive(Metrics, Clone)]
#[metrics(scope = "transaction_pool")]
pub struct BlobEvictionMetrics {
/// Counter for the number of blob transactions evicted
pub(crate) blob_transactions_evicted: Counter,
/// Blob eviction routine time
pub(crate) blob_eviction_duration_seconds: Histogram,
}

/// Pending pool eviction metrics
#[derive(Metrics, Clone)]
#[metrics(scope = "transaction_pool")]
pub struct PendingEvictionMetrics {
/// Counter for the number of pending transactions evicted
pub(crate) pending_transactions_evicted: Counter,
/// Pending eviction routine time
pub(crate) pending_eviction_duration_seconds: Histogram,
}

/// Parked pool eviction metrics
#[derive(Metrics, Clone)]
#[metrics(scope = "transaction_pool")]
pub struct ParkedEvictionMetrics {
/// Counter for the number of parked transactions evicted
pub(crate) parked_transactions_evicted: Counter,
/// Parked eviction routine time
pub(crate) parked_eviction_duration_seconds: Histogram,
}

/// Tx pool eviction metrics
#[derive(Metrics, Clone)]
#[metrics(scope = "transaction_pool")]
pub struct TxEvictionMetrics {
/// Counter for the number of transactions evicted
pub(crate) tx_transactions_evicted: Counter,
/// Transaction eviction routine time
pub(crate) tx_eviction_duration_seconds: Histogram,
}
11 changes: 9 additions & 2 deletions crates/transaction-pool/src/pool/blob.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::txpool::PendingFees;
use crate::{
identifier::TransactionId, pool::size::SizeTracker, traits::BestTransactionsAttributes,
PoolTransaction, SubPoolLimit, ValidPoolTransaction,
identifier::TransactionId, metrics::BlobEvictionMetrics, pool::size::SizeTracker,
traits::BestTransactionsAttributes, PoolTransaction, SubPoolLimit, ValidPoolTransaction,
};
use std::{
cmp::Ordering,
Expand Down Expand Up @@ -31,6 +31,8 @@ pub(crate) struct BlobTransactions<T: PoolTransaction> {
///
/// See also [`PoolTransaction::size`].
size_of: SizeTracker,
/// Metrics for evictions
eviction_metrics: BlobEvictionMetrics,
}

// === impl BlobTransactions ===
Expand Down Expand Up @@ -221,6 +223,7 @@ impl<T: PoolTransaction> BlobTransactions<T> {
&mut self,
limit: SubPoolLimit,
) -> Vec<Arc<ValidPoolTransaction<T>>> {
let start = std::time::Instant::now();
let mut removed = Vec::new();

while self.exceeds(&limit) {
Expand All @@ -229,6 +232,9 @@ impl<T: PoolTransaction> BlobTransactions<T> {
removed.push(self.remove_transaction(&id).expect("transaction exists"));
}

self.eviction_metrics.blob_eviction_duration_seconds.record(start.elapsed());
self.eviction_metrics.blob_transactions_evicted.increment(removed.len() as u64);

removed
}

Expand Down Expand Up @@ -257,6 +263,7 @@ impl<T: PoolTransaction> Default for BlobTransactions<T> {
all: Default::default(),
size_of: Default::default(),
pending_fees: Default::default(),
eviction_metrics: Default::default(),
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/transaction-pool/src/pool/parked.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
identifier::{SenderId, TransactionId},
metrics::ParkedEvictionMetrics,
pool::size::SizeTracker,
PoolTransaction, SubPoolLimit, ValidPoolTransaction, TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER,
};
Expand Down Expand Up @@ -45,6 +46,8 @@ pub struct ParkedPool<T: ParkedOrd> {
///
/// See also [`PoolTransaction::size`].
size_of: SizeTracker,
/// Eviction metrics
eviction_metrics: ParkedEvictionMetrics,
}

// === impl ParkedPool ===
Expand Down Expand Up @@ -188,6 +191,7 @@ impl<T: ParkedOrd> ParkedPool<T> {
&mut self,
limit: SubPoolLimit,
) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
let start = std::time::Instant::now();
if !self.exceeds(&limit) {
// if we are below the limits, we don't need to drop anything
return Vec::new()
Expand All @@ -213,6 +217,9 @@ impl<T: ParkedOrd> ParkedPool<T> {
}
}

self.eviction_metrics.parked_eviction_duration_seconds.record(start.elapsed());
self.eviction_metrics.parked_transactions_evicted.increment(removed.len() as u64);

removed
}

Expand Down Expand Up @@ -333,6 +340,7 @@ impl<T: ParkedOrd> Default for ParkedPool<T> {
last_sender_submission: Default::default(),
sender_transaction_count: Default::default(),
size_of: Default::default(),
eviction_metrics: Default::default(),
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/transaction-pool/src/pool/pending.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
identifier::{SenderId, TransactionId},
metrics::PendingEvictionMetrics,
pool::{
best::{BestTransactions, BestTransactionsWithFees},
size::SizeTracker,
Expand Down Expand Up @@ -48,6 +49,8 @@ pub struct PendingPool<T: TransactionOrdering> {
/// Used to broadcast new transactions that have been added to the `PendingPool` to existing
/// `static_files` of this pool.
new_transaction_notifier: broadcast::Sender<PendingTransaction<T>>,
/// Metrics for evictions
eviction_metrics: PendingEvictionMetrics,
}

// === impl PendingPool ===
Expand All @@ -64,6 +67,7 @@ impl<T: TransactionOrdering> PendingPool<T> {
highest_nonces: Default::default(),
size_of: Default::default(),
new_transaction_notifier,
eviction_metrics: Default::default(),
}
}

Expand Down Expand Up @@ -468,6 +472,7 @@ impl<T: TransactionOrdering> PendingPool<T> {
&mut self,
limit: SubPoolLimit,
) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
let start = std::time::Instant::now();
let mut removed = Vec::new();
// return early if the pool is already under the limits
if !self.exceeds(&limit) {
Expand All @@ -484,6 +489,9 @@ impl<T: TransactionOrdering> PendingPool<T> {
// pool to be under the limit
self.remove_to_limit(&limit, true, &mut removed);

self.eviction_metrics.pending_eviction_duration_seconds.record(start.elapsed());
self.eviction_metrics.pending_transactions_evicted.increment(removed.len() as u64);

removed
}

Expand Down
9 changes: 8 additions & 1 deletion crates/transaction-pool/src/pool/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
config::{LocalTransactionConfig, TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER},
error::{Eip4844PoolTransactionError, InvalidPoolTransactionError, PoolError, PoolErrorKind},
identifier::{SenderId, TransactionId},
metrics::{AllTransactionsMetrics, TxPoolMetrics},
metrics::{AllTransactionsMetrics, TxEvictionMetrics, TxPoolMetrics},
pool::{
best::BestTransactions,
blob::BlobTransactions,
Expand Down Expand Up @@ -114,6 +114,8 @@ pub struct TxPool<T: TransactionOrdering> {
metrics: TxPoolMetrics,
/// The last update kind that was applied to the pool.
latest_update_kind: Option<PoolUpdateKind>,
/// Metrics for evictions
eviction_metrics: TxEvictionMetrics,
}

// === impl TxPool ===
Expand All @@ -131,6 +133,7 @@ impl<T: TransactionOrdering> TxPool<T> {
config,
metrics: Default::default(),
latest_update_kind: None,
eviction_metrics: Default::default(),
}
}

Expand Down Expand Up @@ -956,6 +959,7 @@ impl<T: TransactionOrdering> TxPool<T> {
///
/// This returns all transactions that were removed from the entire pool.
pub(crate) fn discard_worst(&mut self) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
let start = std::time::Instant::now();
let mut removed = Vec::new();

// Helper macro that discards the worst transactions for the pools
Expand Down Expand Up @@ -1013,6 +1017,9 @@ impl<T: TransactionOrdering> TxPool<T> {
]
);

self.eviction_metrics.tx_eviction_duration_seconds.record(start.elapsed());
self.eviction_metrics.tx_transactions_evicted.increment(removed.len() as u64);

removed
}

Expand Down

0 comments on commit f8f7ca1

Please sign in to comment.