Use tokio runtime handle instead of TaskExecutor abstraction (#9737)

* Use tokio runtime handle instead of TaskExecutor abstraction

Before this pr we had the `TaskExecutor` abstraction which theoretically
allowed that any futures executor could have been used. However, this
was never tested and is currently not really required. Anyone running a
node currently only used tokio and nothing else (because this was hard
coded in CLI). So, this pr removes the `TaskExecutor` abstraction and
relies directly on the tokio runtime handle.

Besides this changes, this pr also makes sure that the http and ws rpc
server use the same tokio runtime. This fixes a panic that occurred when
you drop the rpc servers inside an async function (tokio doesn't like
that a tokio runtime is dropped in the async context of another tokio
runtime).

As we don't use any custom runtime in the http rpc server anymore, this
pr also removes the `rpc-http-threads` cli argument. If external parties
complain that there aren't enough threads for the rpc server, we could
bring support for increasing the thread count of the tokio runtime.

* FMT

* Fix try runtime

* Fix integration tests and some other optimizations

* Remove warnings
This commit is contained in:
Bastian Köcher
2021-09-12 14:29:11 +02:00
committed by GitHub
parent be69e4d2b2
commit c09d52ead7
31 changed files with 197 additions and 302 deletions
+17 -16
View File
@@ -18,7 +18,7 @@
//! Service integration test utils.
use futures::{task::Poll, Future, FutureExt, TryFutureExt as _};
use futures::{task::Poll, Future, TryFutureExt as _};
use log::{debug, info};
use parking_lot::Mutex;
use sc_client_api::{Backend, CallExecutor};
@@ -30,7 +30,7 @@ use sc_service::{
client::Client,
config::{BasePath, DatabaseSource, KeystoreConfig},
ChainSpecExtension, Configuration, Error, GenericChainSpec, KeepBlocks, Role, RuntimeGenesis,
SpawnTaskHandle, TaskExecutor, TaskManager, TransactionStorageMode,
SpawnTaskHandle, TaskManager, TransactionStorageMode,
};
use sc_transaction_pool_api::TransactionPool;
use sp_blockchain::HeaderBackend;
@@ -55,6 +55,16 @@ struct TestNet<G, E, F, L, U> {
nodes: usize,
}
impl<G, E, F, L, U> Drop for TestNet<G, E, F, L, U> {
fn drop(&mut self) {
// Drop the nodes before dropping the runtime, as the runtime otherwise waits for all
// futures to be ended and we run into a dead lock.
self.full_nodes.drain(..);
self.light_nodes.drain(..);
self.authority_nodes.drain(..);
}
}
pub trait TestNetNode:
Clone + Future<Output = Result<(), sc_service::Error>> + Send + 'static
{
@@ -200,7 +210,7 @@ fn node_config<
index: usize,
spec: &GenericChainSpec<G, E>,
role: Role,
task_executor: TaskExecutor,
tokio_handle: tokio::runtime::Handle,
key_seed: Option<String>,
base_port: u16,
root: &TempDir,
@@ -229,7 +239,7 @@ fn node_config<
impl_name: String::from("network-test-impl"),
impl_version: String::from("0.1"),
role,
task_executor,
tokio_handle,
transaction_pool: Default::default(),
network: network_config,
keystore_remote: Default::default(),
@@ -248,7 +258,6 @@ fn node_config<
rpc_ipc: None,
rpc_ws: None,
rpc_ws_max_connections: None,
rpc_http_threads: None,
rpc_cors: None,
rpc_methods: Default::default(),
rpc_max_payload: None,
@@ -308,21 +317,13 @@ where
authorities: impl Iterator<Item = (String, impl FnOnce(Configuration) -> Result<(F, U), Error>)>,
) {
let handle = self.runtime.handle().clone();
let task_executor: TaskExecutor = {
let executor = handle.clone();
(move |fut: Pin<Box<dyn futures::Future<Output = ()> + Send>>, _| {
executor.spawn(fut.unit_error());
async {}
})
.into()
};
for (key, authority) in authorities {
let node_config = node_config(
self.nodes,
&self.chain_spec,
Role::Authority,
task_executor.clone(),
handle.clone(),
Some(key),
self.base_port,
&temp,
@@ -343,7 +344,7 @@ where
self.nodes,
&self.chain_spec,
Role::Full,
task_executor.clone(),
handle.clone(),
None,
self.base_port,
&temp,
@@ -363,7 +364,7 @@ where
self.nodes,
&self.chain_spec,
Role::Light,
task_executor.clone(),
handle.clone(),
None,
self.base_port,
&temp,