From 6649c78749675d3db523553ca1a5ac20db284646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 09:16:40 +0200 Subject: [PATCH 01/17] Blank line for clarity & consistency --- crates/services/txpool_v2/src/pool.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/services/txpool_v2/src/pool.rs b/crates/services/txpool_v2/src/pool.rs index d82faa72bc6..05e3ca304ff 100644 --- a/crates/services/txpool_v2/src/pool.rs +++ b/crates/services/txpool_v2/src/pool.rs @@ -247,6 +247,7 @@ where .select_transaction_time_nanoseconds .observe(elapsed); } + // TODO: Use block space also (https://github.com/FuelLabs/fuel-core/issues/2133) /// Extract transactions for a block. /// Returns a list of transactions that were selected for the block From 7c79307de926c4f2873c3c21e588faa3b11c49b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 09:16:50 +0200 Subject: [PATCH 02/17] Rename metric `select_transaction_time_nanoseconds` to `select_transactions_time_nanoseconds` --- crates/metrics/src/txpool_metrics.rs | 10 +++++----- crates/services/txpool_v2/src/pool.rs | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/metrics/src/txpool_metrics.rs b/crates/metrics/src/txpool_metrics.rs index 2978825560f..79e21762398 100644 --- a/crates/metrics/src/txpool_metrics.rs +++ b/crates/metrics/src/txpool_metrics.rs @@ -25,14 +25,14 @@ pub struct TxPoolMetrics { /// Time actively spent by transaction insertion in the thread pool pub transaction_insertion_time_in_thread_pool_milliseconds: Histogram, /// How long it took for the selection algorithm to select transactions - pub select_transaction_time_nanoseconds: Histogram, + pub select_transactions_time_nanoseconds: Histogram, } impl Default for TxPoolMetrics { fn default() -> Self { let tx_size = Histogram::new(buckets(Buckets::TransactionSize)); let transaction_time_in_txpool_secs = Histogram::new(buckets(Buckets::Timing)); - let select_transaction_time_nanoseconds = + let select_transactions_time_nanoseconds = Histogram::new(buckets(Buckets::TimingCoarseGrained)); let transaction_insertion_time_in_thread_pool_milliseconds = Histogram::new(buckets(Buckets::TimingCoarseGrained)); @@ -48,7 +48,7 @@ impl Default for TxPoolMetrics { number_of_executable_transactions, transaction_time_in_txpool_secs, transaction_insertion_time_in_thread_pool_milliseconds, - select_transaction_time_nanoseconds, + select_transactions_time_nanoseconds, }; let mut registry = global_registry().registry.lock(); @@ -83,9 +83,9 @@ impl Default for TxPoolMetrics { ); registry.register( - "txpool_select_transaction_time_nanoseconds", + "txpool_select_transactions_time_nanoseconds", "The time it took to select transactions for inclusion in a block in nanoseconds", - metrics.select_transaction_time_nanoseconds.clone(), + metrics.select_transactions_time_nanoseconds.clone(), ); registry.register( diff --git a/crates/services/txpool_v2/src/pool.rs b/crates/services/txpool_v2/src/pool.rs index 05e3ca304ff..178e9c832c4 100644 --- a/crates/services/txpool_v2/src/pool.rs +++ b/crates/services/txpool_v2/src/pool.rs @@ -244,7 +244,7 @@ where fn record_select_transaction_time_in_nanoseconds(start: Instant) { let elapsed = start.elapsed().as_nanos() as f64; fuel_core_metrics::txpool_metrics::txpool_metrics() - .select_transaction_time_nanoseconds + .select_transactions_time_nanoseconds .observe(elapsed); } From da77cf99329aec61abdbefb713d263248cd4f78c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 09:17:39 +0200 Subject: [PATCH 03/17] Only calculate start time if metrics are enabled --- crates/services/txpool_v2/src/pool.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/services/txpool_v2/src/pool.rs b/crates/services/txpool_v2/src/pool.rs index 178e9c832c4..794ee2c502e 100644 --- a/crates/services/txpool_v2/src/pool.rs +++ b/crates/services/txpool_v2/src/pool.rs @@ -256,13 +256,12 @@ where &mut self, constraints: Constraints, ) -> Vec { - let start = std::time::Instant::now(); let metrics = self.config.metrics; + let maybe_start = metrics.then(|| std::time::Instant::now()); let best_txs = self .selection_algorithm .gather_best_txs(constraints, &mut self.storage); - - if metrics { + if let Some(start) = maybe_start { Self::record_select_transaction_time_in_nanoseconds(start) }; From 2c8554a5f0e8cfe2ce6099126364dbf59058f0cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 09:19:17 +0200 Subject: [PATCH 04/17] Satisfy Clippy --- crates/services/txpool_v2/src/pool.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/services/txpool_v2/src/pool.rs b/crates/services/txpool_v2/src/pool.rs index 794ee2c502e..3028eed22c7 100644 --- a/crates/services/txpool_v2/src/pool.rs +++ b/crates/services/txpool_v2/src/pool.rs @@ -257,7 +257,7 @@ where constraints: Constraints, ) -> Vec { let metrics = self.config.metrics; - let maybe_start = metrics.then(|| std::time::Instant::now()); + let maybe_start = metrics.then(std::time::Instant::now); let best_txs = self .selection_algorithm .gather_best_txs(constraints, &mut self.storage); From fec31bb864b8b55069d3504e380b2a47d43084f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 09:41:04 +0200 Subject: [PATCH 05/17] Slight optimization of metric collection in `extract_transactions_for_block()` --- crates/services/txpool_v2/src/pool.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/services/txpool_v2/src/pool.rs b/crates/services/txpool_v2/src/pool.rs index 3028eed22c7..4d57c0455da 100644 --- a/crates/services/txpool_v2/src/pool.rs +++ b/crates/services/txpool_v2/src/pool.rs @@ -265,16 +265,22 @@ where Self::record_select_transaction_time_in_nanoseconds(start) }; - best_txs - .into_iter() - .inspect(|storage_data| { - if metrics { - Self::record_transaction_time_in_txpool(storage_data) - } - }) + if metrics { + self.register_tx_removal(best_txs.into_iter().inspect(|storage_data| { + Self::record_transaction_time_in_txpool(storage_data) + })) + } else { + self.register_tx_removal(best_txs.into_iter()) + } + } + + fn register_tx_removal( + &mut self, + items: impl Iterator, + ) -> Vec { + items .map(|storage_entry| { self.update_components_and_caches_on_removal(iter::once(&storage_entry)); - storage_entry.transaction }) .collect::>() From 7737a144e5cb9417f36bbf1102143e6645878b04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 09:48:35 +0200 Subject: [PATCH 06/17] Report `tx_count` via `tx_id_to_storage_id.len()` --- crates/services/txpool_v2/src/pool.rs | 5 +++++ crates/services/txpool_v2/src/service.rs | 2 +- crates/services/txpool_v2/src/storage/graph.rs | 4 ---- crates/services/txpool_v2/src/storage/mod.rs | 3 --- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/crates/services/txpool_v2/src/pool.rs b/crates/services/txpool_v2/src/pool.rs index 4d57c0455da..5feaf782fff 100644 --- a/crates/services/txpool_v2/src/pool.rs +++ b/crates/services/txpool_v2/src/pool.rs @@ -89,6 +89,11 @@ impl Pool { && self.current_gas == 0 && self.current_bytes_size == 0 } + + /// Returns the number of transactions in the pool. + pub fn tx_count(&self) -> usize { + self.tx_id_to_storage_id.len() + } } impl Pool diff --git a/crates/services/txpool_v2/src/service.rs b/crates/services/txpool_v2/src/service.rs index 28a5aae3d93..da5c5462a62 100644 --- a/crates/services/txpool_v2/src/service.rs +++ b/crates/services/txpool_v2/src/service.rs @@ -230,7 +230,7 @@ where // TODO: move this to the Task struct if self.metrics { let pool = self.pool.read(); - let num_transactions = pool.storage.tx_count(); + let num_transactions = pool.tx_count(); let executable_txs = pool.selection_algorithm.number_of_executable_transactions(); diff --git a/crates/services/txpool_v2/src/storage/graph.rs b/crates/services/txpool_v2/src/storage/graph.rs index ace99937427..b3b40575331 100644 --- a/crates/services/txpool_v2/src/storage/graph.rs +++ b/crates/services/txpool_v2/src/storage/graph.rs @@ -619,10 +619,6 @@ impl Storage for GraphStorage { self.clear_cache(storage_entry); }) } - - fn tx_count(&self) -> usize { - self.graph.node_count() - } } impl RatioTipGasSelectionAlgorithmStorage for GraphStorage { diff --git a/crates/services/txpool_v2/src/storage/mod.rs b/crates/services/txpool_v2/src/storage/mod.rs index 949bea30698..38686eb6b9b 100644 --- a/crates/services/txpool_v2/src/storage/mod.rs +++ b/crates/services/txpool_v2/src/storage/mod.rs @@ -102,7 +102,4 @@ pub trait Storage { /// Remove a transaction from the storage. fn remove_transaction(&mut self, index: Self::StorageIndex) -> Option; - - /// Returns the number of transactions in the storage. - fn tx_count(&self) -> usize; } From 22592982129429be7002332911cd98cf222756fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 10:05:30 +0200 Subject: [PATCH 07/17] Remove the stray `InsertionResult` struct --- crates/types/src/services/txpool.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/crates/types/src/services/txpool.rs b/crates/types/src/services/txpool.rs index 55f0d6f96ca..c684a78b092 100644 --- a/crates/types/src/services/txpool.rs +++ b/crates/types/src/services/txpool.rs @@ -32,7 +32,6 @@ use crate::{ }, services::executor::TransactionExecutionResult, }; -use core::time::Duration; use fuel_vm_private::{ checked_transaction::CheckedTransaction, fuel_types::BlockHeight, @@ -319,18 +318,6 @@ impl From<&PoolTransaction> for CheckedTransaction { } } -/// The `removed` field contains the list of removed transactions during the insertion -/// of the `inserted` transaction. -#[derive(Debug, PartialEq, Eq)] -pub struct InsertionResult { - /// This was inserted - pub inserted: ArcPoolTx, - /// The time the transaction was inserted. - pub submitted_time: Duration, - /// These were removed during the insertion - pub removed: Vec, -} - /// The status of the transaction during its life from the tx pool until the block. #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Debug, PartialEq, Eq)] From adba071c42f64be085653258f6c63dc20b61aaa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 10:34:37 +0200 Subject: [PATCH 08/17] Update tx count metrics upon tx insertion --- crates/services/txpool_v2/src/service.rs | 28 +++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/crates/services/txpool_v2/src/service.rs b/crates/services/txpool_v2/src/service.rs index da5c5462a62..140e260633b 100644 --- a/crates/services/txpool_v2/src/service.rs +++ b/crates/services/txpool_v2/src/service.rs @@ -227,18 +227,6 @@ where View: TxPoolPersistentStorage, { async fn run(&mut self, watcher: &mut StateWatcher) -> anyhow::Result { - // TODO: move this to the Task struct - if self.metrics { - let pool = self.pool.read(); - let num_transactions = pool.tx_count(); - - let executable_txs = - pool.selection_algorithm.number_of_executable_transactions(); - - record_number_of_transactions_in_txpool(num_transactions); - record_number_of_executable_transactions_in_txpool(executable_txs); - } - tokio::select! { biased; @@ -442,7 +430,21 @@ where let result = verification.persistent_storage_provider.latest_view(); match result { - Ok(view) => pool.insert(tx, &view), + Ok(view) => { + let insertion_result = pool.insert(tx, &view); + if metrics { + let num_transactions = pool.tx_count(); + let executable_txs = pool + .selection_algorithm + .number_of_executable_transactions(); + + record_number_of_transactions_in_txpool(num_transactions); + record_number_of_executable_transactions_in_txpool( + executable_txs, + ); + } + insertion_result + } Err(err) => Err(Error::Database(format!("{:?}", err))), } }; From 27d03216a91d36b35fb0a2f347015042957a19fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 11:02:57 +0200 Subject: [PATCH 09/17] Different optimization of metric collection `in extract_transactions_for_block()` --- crates/services/txpool_v2/src/pool.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/crates/services/txpool_v2/src/pool.rs b/crates/services/txpool_v2/src/pool.rs index 5feaf782fff..995e1f5c4e8 100644 --- a/crates/services/txpool_v2/src/pool.rs +++ b/crates/services/txpool_v2/src/pool.rs @@ -271,19 +271,13 @@ where }; if metrics { - self.register_tx_removal(best_txs.into_iter().inspect(|storage_data| { + best_txs.iter().for_each(|storage_data| { Self::record_transaction_time_in_txpool(storage_data) - })) - } else { - self.register_tx_removal(best_txs.into_iter()) + }); } - } - fn register_tx_removal( - &mut self, - items: impl Iterator, - ) -> Vec { - items + best_txs + .into_iter() .map(|storage_entry| { self.update_components_and_caches_on_removal(iter::once(&storage_entry)); storage_entry.transaction From 82262eb819585f57eef77b85b70da8ca009a9f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 11:28:31 +0200 Subject: [PATCH 10/17] Update tx count metrics upon tx removal --- crates/services/txpool_v2/src/pool.rs | 30 ++++++++++++++++++++++-- crates/services/txpool_v2/src/service.rs | 28 +--------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/crates/services/txpool_v2/src/pool.rs b/crates/services/txpool_v2/src/pool.rs index 995e1f5c4e8..691adf3e75f 100644 --- a/crates/services/txpool_v2/src/pool.rs +++ b/crates/services/txpool_v2/src/pool.rs @@ -10,6 +10,7 @@ use std::{ }; use collisions::CollisionsExt; +use fuel_core_metrics::txpool_metrics::txpool_metrics; use fuel_core_types::{ fuel_tx::{ field::BlobId, @@ -111,6 +112,16 @@ where tx: ArcPoolTx, persistent_storage: &impl TxPoolPersistentStorage, ) -> Result, Error> { + let insertion_result = self.insert_inner(tx, persistent_storage); + self.register_transaction_counts(); + insertion_result + } + + fn insert_inner( + &mut self, + tx: std::sync::Arc, + persistent_storage: &impl TxPoolPersistentStorage, + ) -> Result>, Error> { let CanStoreTransaction { checked_transaction, transactions_to_remove, @@ -238,7 +249,7 @@ where fn record_transaction_time_in_txpool(tx: &StorageData) { if let Ok(elapsed) = tx.creation_instant.elapsed() { - fuel_core_metrics::txpool_metrics::txpool_metrics() + txpool_metrics() .transaction_time_in_txpool_secs .observe(elapsed.as_secs_f64()); } else { @@ -248,11 +259,25 @@ where fn record_select_transaction_time_in_nanoseconds(start: Instant) { let elapsed = start.elapsed().as_nanos() as f64; - fuel_core_metrics::txpool_metrics::txpool_metrics() + txpool_metrics() .select_transactions_time_nanoseconds .observe(elapsed); } + fn register_transaction_counts(&self) { + if self.config.metrics { + let num_transactions = self.tx_count(); + let executable_txs = + self.selection_algorithm.number_of_executable_transactions(); + txpool_metrics() + .number_of_transactions + .set(num_transactions as i64); + txpool_metrics() + .number_of_executable_transactions + .set(executable_txs as i64); + } + } + // TODO: Use block space also (https://github.com/FuelLabs/fuel-core/issues/2133) /// Extract transactions for a block. /// Returns a list of transactions that were selected for the block @@ -530,6 +555,7 @@ where self.selection_algorithm .on_removed_transaction(storage_entry); } + self.register_transaction_counts(); } } diff --git a/crates/services/txpool_v2/src/service.rs b/crates/services/txpool_v2/src/service.rs index 140e260633b..23009fb4e03 100644 --- a/crates/services/txpool_v2/src/service.rs +++ b/crates/services/txpool_v2/src/service.rs @@ -1,6 +1,5 @@ use crate::{ self as fuel_core_txpool, - selection_algorithms::SelectionAlgorithm, }; use fuel_core_metrics::txpool_metrics::txpool_metrics; @@ -430,21 +429,7 @@ where let result = verification.persistent_storage_provider.latest_view(); match result { - Ok(view) => { - let insertion_result = pool.insert(tx, &view); - if metrics { - let num_transactions = pool.tx_count(); - let executable_txs = pool - .selection_algorithm - .number_of_executable_transactions(); - - record_number_of_transactions_in_txpool(num_transactions); - record_number_of_executable_transactions_in_txpool( - executable_txs, - ); - } - insertion_result - } + Ok(view) => pool.insert(tx, &view), Err(err) => Err(Error::Database(format!("{:?}", err))), } }; @@ -701,17 +686,6 @@ fn record_tx_size(tx: &PoolTransaction) { txpool_metrics.tx_size.observe(size as f64); } -fn record_number_of_transactions_in_txpool(num_transactions: usize) { - txpool_metrics() - .number_of_transactions - .set(num_transactions as i64); -} -fn record_number_of_executable_transactions_in_txpool(executable_txs: usize) { - txpool_metrics() - .number_of_executable_transactions - .set(executable_txs as i64); -} - #[allow(clippy::too_many_arguments)] pub fn new_service< P2P, From 49a79b2ec0fca0da5e5ab1317afbd0ab230b8a93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 15:02:46 +0200 Subject: [PATCH 11/17] `transaction_insertion_time_in_thread_pool` and `select_transactions_time` use microseconds --- crates/metrics/src/txpool_metrics.rs | 24 ++++++++++++------------ crates/services/txpool_v2/src/pool.rs | 8 ++++---- crates/services/txpool_v2/src/service.rs | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/crates/metrics/src/txpool_metrics.rs b/crates/metrics/src/txpool_metrics.rs index 79e21762398..c7fddc501f0 100644 --- a/crates/metrics/src/txpool_metrics.rs +++ b/crates/metrics/src/txpool_metrics.rs @@ -23,18 +23,18 @@ pub struct TxPoolMetrics { /// Time of transactions in the txpool in seconds pub transaction_time_in_txpool_secs: Histogram, /// Time actively spent by transaction insertion in the thread pool - pub transaction_insertion_time_in_thread_pool_milliseconds: Histogram, + pub transaction_insertion_time_in_thread_pool_microseconds: Histogram, /// How long it took for the selection algorithm to select transactions - pub select_transactions_time_nanoseconds: Histogram, + pub select_transactions_time_microseconds: Histogram, } impl Default for TxPoolMetrics { fn default() -> Self { let tx_size = Histogram::new(buckets(Buckets::TransactionSize)); let transaction_time_in_txpool_secs = Histogram::new(buckets(Buckets::Timing)); - let select_transactions_time_nanoseconds = + let select_transactions_time_microseconds = Histogram::new(buckets(Buckets::TimingCoarseGrained)); - let transaction_insertion_time_in_thread_pool_milliseconds = + let transaction_insertion_time_in_thread_pool_microseconds = Histogram::new(buckets(Buckets::TimingCoarseGrained)); let number_of_transactions = Gauge::default(); @@ -47,8 +47,8 @@ impl Default for TxPoolMetrics { number_of_transactions_pending_verification, number_of_executable_transactions, transaction_time_in_txpool_secs, - transaction_insertion_time_in_thread_pool_milliseconds, - select_transactions_time_nanoseconds, + transaction_insertion_time_in_thread_pool_microseconds, + select_transactions_time_microseconds, }; let mut registry = global_registry().registry.lock(); @@ -83,16 +83,16 @@ impl Default for TxPoolMetrics { ); registry.register( - "txpool_select_transactions_time_nanoseconds", - "The time it took to select transactions for inclusion in a block in nanoseconds", - metrics.select_transactions_time_nanoseconds.clone(), + "txpool_select_transactions_time_microseconds", + "The time it took to select transactions for inclusion in a block in microseconds", + metrics.select_transactions_time_microseconds.clone(), ); registry.register( - "txpool_insert_transaction_time_milliseconds", - "The time it took to insert a transaction in the txpool in milliseconds", + "txpool_transaction_insertion_time_in_thread_pool_microseconds", + "The time it took to insert a transaction in the txpool in microseconds", metrics - .transaction_insertion_time_in_thread_pool_milliseconds + .transaction_insertion_time_in_thread_pool_microseconds .clone(), ); diff --git a/crates/services/txpool_v2/src/pool.rs b/crates/services/txpool_v2/src/pool.rs index 691adf3e75f..3e26d844a98 100644 --- a/crates/services/txpool_v2/src/pool.rs +++ b/crates/services/txpool_v2/src/pool.rs @@ -257,10 +257,10 @@ where } } - fn record_select_transaction_time_in_nanoseconds(start: Instant) { - let elapsed = start.elapsed().as_nanos() as f64; + fn record_select_transaction_time(start: Instant) { + let elapsed = start.elapsed().as_micros() as f64; txpool_metrics() - .select_transactions_time_nanoseconds + .select_transactions_time_microseconds .observe(elapsed); } @@ -292,7 +292,7 @@ where .selection_algorithm .gather_best_txs(constraints, &mut self.storage); if let Some(start) = maybe_start { - Self::record_select_transaction_time_in_nanoseconds(start) + Self::record_select_transaction_time(start) }; if metrics { diff --git a/crates/services/txpool_v2/src/service.rs b/crates/services/txpool_v2/src/service.rs index 23009fb4e03..538a50f6ee6 100644 --- a/crates/services/txpool_v2/src/service.rs +++ b/crates/services/txpool_v2/src/service.rs @@ -482,9 +482,9 @@ where .inc(); let start_time = tokio::time::Instant::now(); insert_transaction_thread_pool_op(); - let time_for_task_to_complete = start_time.elapsed().as_millis(); + let time_for_task_to_complete = start_time.elapsed().as_micros(); txpool_metrics - .transaction_insertion_time_in_thread_pool_milliseconds + .transaction_insertion_time_in_thread_pool_microseconds .observe(time_for_task_to_complete as f64); txpool_metrics .number_of_transactions_pending_verification From 9cfae1f16a457cfa7cd67da1d726e471c77b8bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 15:06:17 +0200 Subject: [PATCH 12/17] Update changelog --- CHANGELOG.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fede191b13b..10aaf931c20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,12 +6,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added +- [2321](https://github.com/FuelLabs/fuel-core/pull/2321): New metrics for the txpool: + - The size of transactions in the txpool (`txpool_tx_size`) + - The time spent by a transaction in the txpool in seconds (`txpool_tx_time_in_txpool_seconds`) + - The number of transactions in the txpool (`txpool_number_of_transactions`) + - The number of transactions pending verification before entering the txpool (`txpool_number_of_transactions_pending_verification`) + - The number of executable transactions in the txpool (`txpool_number_of_executable_transactions`) + - The time it took to select transactions for inclusion in a block in microseconds (`txpool_select_transactions_time_microseconds`) + - The time it took to insert a transaction in the txpool in microseconds (`transaction_insertion_time_in_thread_pool_microseconds`) + ### Fixed - [2366](https://github.com/FuelLabs/fuel-core/pull/2366): The `importer_gas_price_for_block` metric is properly collected. -- [2369](https://github.com/FuelLabs/fuel-core/pull/2369): The `transaction_insertion_time_in_thread_pool_milliseconds` metric is properly collected. - -### Added -- [2321](https://github.com/FuelLabs/fuel-core/pull/2321): New metrics for the txpool: "The size of transactions in the txpool" (`txpool_tx_size`), "The time spent by a transaction in the txpool in seconds" (`txpool_tx_time_in_txpool_seconds`), The number of transactions in the txpool (`txpool_number_of_transactions`), "The number of transactions pending verification before entering the txpool" (`txpool_number_of_transactions_pending_verification`), "The number of executable transactions in the txpool" (`txpool_number_of_executable_transactions`), "The time it took to select transactions for inclusion in a block in nanoseconds" (`txpool_select_transaction_time_nanoseconds`), The time it took to insert a transaction in the txpool in milliseconds (`txpool_insert_transaction_time_milliseconds`). ## [Version 0.40.0] From 959c4b9625dee9c6e92a179281d7c74d7d6dfc14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 15:25:24 +0200 Subject: [PATCH 13/17] Update `number_of_transactions_pending_verification` metric in correct place --- crates/services/txpool_v2/src/service.rs | 33 ++++++++++++------------ 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/crates/services/txpool_v2/src/service.rs b/crates/services/txpool_v2/src/service.rs index 538a50f6ee6..a7108faffa5 100644 --- a/crates/services/txpool_v2/src/service.rs +++ b/crates/services/txpool_v2/src/service.rs @@ -70,7 +70,6 @@ use fuel_core_types::{ }, txpool::{ ArcPoolTx, - PoolTransaction, TransactionStatus, }, }, @@ -379,6 +378,13 @@ where from_peer_info: Option, response_channel: Option>>, ) -> impl FnOnce() + Send + 'static { + let metrics = self.metrics; + if metrics { + txpool_metrics() + .number_of_transactions_pending_verification + .inc(); + } + let verification = self.verification.clone(); let pool = self.pool.clone(); let p2p = self.p2p.clone(); @@ -387,7 +393,6 @@ where let time_txs_submitted = self.pruner.time_txs_submitted.clone(); let tx_id = transaction.id(&self.chain_id); let utxo_validation = self.utxo_validation; - let metrics = self.metrics; let insert_transaction_thread_pool_op = move || { let current_height = *current_height.read(); @@ -404,6 +409,12 @@ where utxo_validation, ); + if metrics { + txpool_metrics() + .number_of_transactions_pending_verification + .dec(); + } + p2p.process_insertion_result(from_peer_info, &result); let checked_tx = match result { @@ -419,7 +430,8 @@ where }; if metrics { - record_tx_size(&checked_tx) + let size = checked_tx.metered_bytes_size(); + txpool_metrics().tx_size.observe(size as f64); }; let tx = Arc::new(checked_tx); @@ -476,19 +488,12 @@ where }; move || { if metrics { - let txpool_metrics = txpool_metrics(); - txpool_metrics - .number_of_transactions_pending_verification - .inc(); let start_time = tokio::time::Instant::now(); insert_transaction_thread_pool_op(); let time_for_task_to_complete = start_time.elapsed().as_micros(); - txpool_metrics + txpool_metrics() .transaction_insertion_time_in_thread_pool_microseconds .observe(time_for_task_to_complete as f64); - txpool_metrics - .number_of_transactions_pending_verification - .dec(); } else { insert_transaction_thread_pool_op(); } @@ -680,12 +685,6 @@ where } } -fn record_tx_size(tx: &PoolTransaction) { - let size = tx.metered_bytes_size(); - let txpool_metrics = txpool_metrics(); - txpool_metrics.tx_size.observe(size as f64); -} - #[allow(clippy::too_many_arguments)] pub fn new_service< P2P, From 58bf187b0f6d57a817b3d5a0573b02f9787be4c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 15:38:48 +0200 Subject: [PATCH 14/17] Update buckets for `select_transactions_time_microseconds` and `transaction_insertion_time_in_thread_pool_microseconds` metrics --- crates/metrics/src/buckets.rs | 45 +++++++++++++++++----------- crates/metrics/src/txpool_metrics.rs | 4 +-- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/crates/metrics/src/buckets.rs b/crates/metrics/src/buckets.rs index ccec479e85b..7e5150a4ffd 100644 --- a/crates/metrics/src/buckets.rs +++ b/crates/metrics/src/buckets.rs @@ -9,8 +9,9 @@ use strum_macros::EnumIter; #[cfg_attr(test, derive(EnumIter))] pub(crate) enum Buckets { Timing, - TimingCoarseGrained, TransactionSize, + TransactionInsertionTimeInThreadPool, + SelectTransactionsTime, } static BUCKETS: OnceLock>> = OnceLock::new(); pub(crate) fn buckets(b: Buckets) -> impl Iterator { @@ -36,22 +37,6 @@ fn initialize_buckets() -> HashMap> { 10.000, ], ), - ( - Buckets::TimingCoarseGrained, - vec![ - 5.0, - 10.0, - 25.0, - 50.0, - 100.0, - 250.0, - 500.0, - 1000.0, - 2500.0, - 5000.0, - 10000.0, - ], - ), ( // We consider blocks up to 256kb in size and single transaction can take any of this space. Buckets::TransactionSize, @@ -76,6 +61,32 @@ fn initialize_buckets() -> HashMap> { 256.0 * 1024.0, ] ), + ( + Buckets::TransactionInsertionTimeInThreadPool, + vec![ + 50.0, + 250.0, + 1000.0, + 10000.0, + 100000.0, + 300000.0, + 1_000_000.0, + 5_000_000.0, + ] + ), + ( + Buckets::SelectTransactionsTime, + vec![ + 50.0, + 250.0, + 1000.0, + 10000.0, + 100000.0, + 300000.0, + 1_000_000.0, + 5_000_000.0, + ] + ), ] .into_iter() .collect() diff --git a/crates/metrics/src/txpool_metrics.rs b/crates/metrics/src/txpool_metrics.rs index c7fddc501f0..94ac8b5fdc8 100644 --- a/crates/metrics/src/txpool_metrics.rs +++ b/crates/metrics/src/txpool_metrics.rs @@ -33,9 +33,9 @@ impl Default for TxPoolMetrics { let tx_size = Histogram::new(buckets(Buckets::TransactionSize)); let transaction_time_in_txpool_secs = Histogram::new(buckets(Buckets::Timing)); let select_transactions_time_microseconds = - Histogram::new(buckets(Buckets::TimingCoarseGrained)); + Histogram::new(buckets(Buckets::SelectTransactionsTime)); let transaction_insertion_time_in_thread_pool_microseconds = - Histogram::new(buckets(Buckets::TimingCoarseGrained)); + Histogram::new(buckets(Buckets::TransactionInsertionTimeInThreadPool)); let number_of_transactions = Gauge::default(); let number_of_transactions_pending_verification = Gauge::default(); From d46468c5f2fbc0344648b3bbac6946edb36cb133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 23 Oct 2024 15:43:56 +0200 Subject: [PATCH 15/17] Update buckets for `transaction_time_in_txpool_secs` metric --- crates/metrics/src/buckets.rs | 13 +++++++++++++ crates/metrics/src/txpool_metrics.rs | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/metrics/src/buckets.rs b/crates/metrics/src/buckets.rs index 7e5150a4ffd..c4fcb110556 100644 --- a/crates/metrics/src/buckets.rs +++ b/crates/metrics/src/buckets.rs @@ -12,6 +12,7 @@ pub(crate) enum Buckets { TransactionSize, TransactionInsertionTimeInThreadPool, SelectTransactionsTime, + TransactionTimeInTxpool, } static BUCKETS: OnceLock>> = OnceLock::new(); pub(crate) fn buckets(b: Buckets) -> impl Iterator { @@ -87,6 +88,18 @@ fn initialize_buckets() -> HashMap> { 5_000_000.0, ] ), + ( + Buckets::TransactionTimeInTxpool, + vec![ + 1.0, + 2.0, + 5.0, + 10.0, + 100.0, + 250.0, + 600.0 + ] + ), ] .into_iter() .collect() diff --git a/crates/metrics/src/txpool_metrics.rs b/crates/metrics/src/txpool_metrics.rs index 94ac8b5fdc8..28ac4d46288 100644 --- a/crates/metrics/src/txpool_metrics.rs +++ b/crates/metrics/src/txpool_metrics.rs @@ -31,7 +31,8 @@ pub struct TxPoolMetrics { impl Default for TxPoolMetrics { fn default() -> Self { let tx_size = Histogram::new(buckets(Buckets::TransactionSize)); - let transaction_time_in_txpool_secs = Histogram::new(buckets(Buckets::Timing)); + let transaction_time_in_txpool_secs = + Histogram::new(buckets(Buckets::TransactionTimeInTxpool)); let select_transactions_time_microseconds = Histogram::new(buckets(Buckets::SelectTransactionsTime)); let transaction_insertion_time_in_thread_pool_microseconds = From dadaaa6214160b9a331c55ac8bb13bc366a44433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 30 Oct 2024 10:10:26 +0100 Subject: [PATCH 16/17] Update changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e694eb5bfe..7fd8618e541 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Added -- [2321](https://github.com/FuelLabs/fuel-core/pull/2321): New metrics for the txpool: +- [2321](https://github.com/FuelLabs/fuel-core/pull/2321): New metrics for the TxPool: - The size of transactions in the txpool (`txpool_tx_size`) - The time spent by a transaction in the txpool in seconds (`txpool_tx_time_in_txpool_seconds`) - The number of transactions in the txpool (`txpool_number_of_transactions`) @@ -15,13 +15,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - The number of executable transactions in the txpool (`txpool_number_of_executable_transactions`) - The time it took to select transactions for inclusion in a block in microseconds (`txpool_select_transactions_time_microseconds`) - The time it took to insert a transaction in the txpool in microseconds (`transaction_insertion_time_in_thread_pool_microseconds`) +- [2385](https://github.com/FuelLabs/fuel-core/pull/2385): Added new histogram buckets for some of the TxPool metrics, optimize the way they are collected. - [2362](https://github.com/FuelLabs/fuel-core/pull/2362): Added a new request_response protocol version `/fuel/req_res/0.0.2`. In comparison with `/fuel/req/0.0.1`, which returns an empty response when a request cannot be fulfilled, this version returns more meaningful error codes. Nodes still support the version `0.0.1` of the protocol to guarantee backward compatibility with fuel-core nodes. Empty responses received from nodes using the old protocol `/fuel/req/0.0.1` are automatically converted into an error `ProtocolV1EmptyResponse` with error code 0, which is also the only error code implemented. More specific error codes will be added in the future. ### Fixed - [2366](https://github.com/FuelLabs/fuel-core/pull/2366): The `importer_gas_price_for_block` metric is properly collected. ### Changed - - [2378](https://github.com/FuelLabs/fuel-core/pull/2378): Use cached hash of the topic instead of calculating it on each publishing gossip message. ## [Version 0.40.0] From fbb0a5476a3aee4872966dad1d26e83cab427f3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Chabowski?= Date: Wed, 30 Oct 2024 15:36:05 +0100 Subject: [PATCH 17/17] Register `tx_size` metric on the actual insertion into TxPool --- crates/services/txpool_v2/src/pool.rs | 4 ++++ crates/services/txpool_v2/src/service.rs | 5 ----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/services/txpool_v2/src/pool.rs b/crates/services/txpool_v2/src/pool.rs index 3e26d844a98..be417dce5f4 100644 --- a/crates/services/txpool_v2/src/pool.rs +++ b/crates/services/txpool_v2/src/pool.rs @@ -162,6 +162,10 @@ where debug_assert!(!self.tx_id_to_storage_id.contains_key(&tx_id)); self.tx_id_to_storage_id.insert(tx_id, storage_id); + if self.config.metrics { + txpool_metrics().tx_size.observe(bytes_size as f64); + }; + let tx = Storage::get(&self.storage, &storage_id).expect("Transaction is set above"); self.collision_manager.on_stored_transaction(storage_id, tx); diff --git a/crates/services/txpool_v2/src/service.rs b/crates/services/txpool_v2/src/service.rs index a7108faffa5..c80952f7885 100644 --- a/crates/services/txpool_v2/src/service.rs +++ b/crates/services/txpool_v2/src/service.rs @@ -429,11 +429,6 @@ where } }; - if metrics { - let size = checked_tx.metered_bytes_size(); - txpool_metrics().tx_size.observe(size as f64); - }; - let tx = Arc::new(checked_tx); let result = {