Skip to content

Commit

Permalink
Notify telemetry only every second about the tx pool status
Browse files Browse the repository at this point in the history
Before this was done for every imported transaction. When a lot of transactions got imported,
the import notification channel was filled. The underlying problem was that the `status` call
is read locking the `validated_pool` which will be write locked by the internal submitting logic.
Thus, the submitting and status reading was interferring which each other.
  • Loading branch information
bkchr committed Nov 21, 2024
1 parent d8ce550 commit 8159f80
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cumulus/client/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ workspace = true

[dependencies]
futures = { workspace = true }
futures-timer = { workspace = true }

# Substrate
sc-client-api = { workspace = true, default-features = true }
Expand Down
58 changes: 41 additions & 17 deletions substrate/client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
start_rpc_servers, BuildGenesisBlock, GenesisBlockBuilder, RpcHandlers, SpawnTaskHandle,
TaskManager, TransactionPoolAdapter,
};
use futures::{future::ready, FutureExt, StreamExt};
use futures::{select, FutureExt, StreamExt};
use jsonrpsee::RpcModule;
use log::info;
use prometheus_endpoint::Registry;
Expand Down Expand Up @@ -90,7 +90,11 @@ use sp_consensus::block_validation::{
use sp_core::traits::{CodeExecutor, SpawnNamed};
use sp_keystore::KeystorePtr;
use sp_runtime::traits::{Block as BlockT, BlockIdTo, NumberFor, Zero};
use std::{str::FromStr, sync::Arc, time::SystemTime};
use std::{
str::FromStr,
sync::Arc,
time::{Duration, SystemTime},
};

/// Full client type.
pub type TFullClient<TBl, TRtApi, TExec> =
Expand Down Expand Up @@ -577,22 +581,42 @@ pub async fn propagate_transaction_notifications<Block, ExPool>(
Block: BlockT,
ExPool: MaintainedTransactionPool<Block = Block, Hash = <Block as BlockT>::Hash>,
{
const TELEMETRY_INTERVAL: Duration = Duration::from_secs(1);

// transaction notifications
transaction_pool
.import_notification_stream()
.for_each(move |hash| {
tx_handler_controller.propagate_transaction(hash);
let status = transaction_pool.status();
telemetry!(
telemetry;
SUBSTRATE_INFO;
"txpool.import";
"ready" => status.ready,
"future" => status.future,
);
ready(())
})
.await;
let mut notifications = transaction_pool.import_notification_stream().fuse();
let mut timer = futures_timer::Delay::new(TELEMETRY_INTERVAL).fuse();
let mut tx_imported = false;

loop {
select! {
notification = notifications.next() => {
let Some(hash) = notification else { return };

tx_handler_controller.propagate_transaction(hash);

tx_imported = true;
},
_ = timer => {
timer = futures_timer::Delay::new(TELEMETRY_INTERVAL).fuse();

if !tx_imported {
continue;
}

tx_imported = false;
let status = transaction_pool.status();

telemetry!(
telemetry;
SUBSTRATE_INFO;
"txpool.import";
"ready" => status.ready,
"future" => status.future,
);
}
}
}
}

/// Initialize telemetry with provided configuration and return telemetry handle
Expand Down

0 comments on commit 8159f80

Please sign in to comment.