Upgrade to tokio 0.3 (#1839)

## Description

This PR updates Lighthouse to tokio 0.3. It includes a number of dependency updates and some structural changes as to how we create and spawn tasks.

This also brings with it a number of various improvements:

- Discv5 update
- Libp2p update
- Fix for recompilation issues
- Improved UPnP port mapping handling
- Futures dependency update
- Log downgrade to traces for rejecting peers when we've reached our max



Co-authored-by: blacktemplar <blacktemplar@a1.net>
This commit is contained in:
Age Manning
2020-11-28 05:30:57 +00:00
parent 5a3b94cbb4
commit a567f788bd
81 changed files with 3666 additions and 2762 deletions

View File

@@ -5,7 +5,7 @@ authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
[dependencies]
tokio = { version = "0.2.22", features = ["macros"] }
tokio = { version = "0.3.2", features = ["macros", "rt", "rt-multi-thread" ] }
slog = { version = "2.5.2", features = ["max_level_trace"] }
sloggers = "1.0.1"
types = { "path" = "../../consensus/types" }
@@ -16,7 +16,7 @@ logging = { path = "../../common/logging" }
slog-term = "2.6.0"
slog-async = "2.5.0"
ctrlc = { version = "3.1.6", features = ["termination"] }
futures = "0.3.5"
futures = "0.3.7"
parking_lot = "0.11.0"
slog-json = "2.3.0"
exit-future = "0.2.0"

View File

@@ -15,12 +15,13 @@ use futures::channel::{
};
use futures::{future, StreamExt};
use slog::{error, info, o, Drain, Level, Logger};
use slog::{error, info, o, warn, Drain, Level, Logger};
use sloggers::{null::NullLoggerBuilder, Build};
use std::cell::RefCell;
use std::ffi::OsStr;
use std::fs::{rename as FsRename, OpenOptions};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use task_executor::TaskExecutor;
use tokio::runtime::{Builder as RuntimeBuilder, Runtime};
@@ -29,11 +30,11 @@ use types::{EthSpec, MainnetEthSpec, MinimalEthSpec, V012LegacyEthSpec};
pub const ETH2_CONFIG_FILENAME: &str = "eth2-spec.toml";
const LOG_CHANNEL_SIZE: usize = 2048;
/// The maximum time in seconds the client will wait for all internal tasks to shutdown.
const MAXIMUM_SHUTDOWN_TIME: u64 = 3;
const MAXIMUM_SHUTDOWN_TIME: u64 = 15;
/// Builds an `Environment`.
pub struct EnvironmentBuilder<E: EthSpec> {
runtime: Option<Runtime>,
runtime: Option<Arc<Runtime>>,
log: Option<Logger>,
eth_spec_instance: E,
eth2_config: Eth2Config,
@@ -84,28 +85,12 @@ impl<E: EthSpec> EnvironmentBuilder<E> {
///
/// The `Runtime` used is just the standard tokio runtime.
pub fn multi_threaded_tokio_runtime(mut self) -> Result<Self, String> {
self.runtime = Some(
RuntimeBuilder::new()
.threaded_scheduler()
self.runtime = Some(Arc::new(
RuntimeBuilder::new_multi_thread()
.enable_all()
.build()
.map_err(|e| format!("Failed to start runtime: {:?}", e))?,
);
Ok(self)
}
/// Specifies that a single-threaded tokio runtime should be used. Ideal for testing purposes
/// where tests are already multi-threaded.
///
/// This can solve problems if "too many open files" errors are thrown during tests.
pub fn single_thread_tokio_runtime(mut self) -> Result<Self, String> {
self.runtime = Some(
RuntimeBuilder::new()
.basic_scheduler()
.enable_all()
.build()
.map_err(|e| format!("Failed to start runtime: {:?}", e))?,
);
));
Ok(self)
}
@@ -329,7 +314,7 @@ impl<E: EthSpec> RuntimeContext<E> {
/// An environment where Lighthouse services can run. Used to start a production beacon node or
/// validator client, or to run tests that involve logging and async task execution.
pub struct Environment<E: EthSpec> {
runtime: Runtime,
runtime: Arc<Runtime>,
/// Receiver side of an internal shutdown signal.
signal_rx: Option<Receiver<&'static str>>,
/// Sender to request shutting down.
@@ -347,15 +332,15 @@ impl<E: EthSpec> Environment<E> {
///
/// Useful in the rare scenarios where it's necessary to block the current thread until a task
/// is finished (e.g., during testing).
pub fn runtime(&mut self) -> &mut Runtime {
&mut self.runtime
pub fn runtime(&self) -> &Arc<Runtime> {
&self.runtime
}
/// Returns a `Context` where no "service" has been added to the logger output.
pub fn core_context(&mut self) -> RuntimeContext<E> {
RuntimeContext {
executor: TaskExecutor::new(
self.runtime().handle().clone(),
Arc::downgrade(self.runtime()),
self.exit.clone(),
self.log.clone(),
self.signal_tx.clone(),
@@ -369,7 +354,7 @@ impl<E: EthSpec> Environment<E> {
pub fn service_context(&mut self, service_name: String) -> RuntimeContext<E> {
RuntimeContext {
executor: TaskExecutor::new(
self.runtime().handle().clone(),
Arc::downgrade(self.runtime()),
self.exit.clone(),
self.log.new(o!("service" => service_name)),
self.signal_tx.clone(),
@@ -425,8 +410,16 @@ impl<E: EthSpec> Environment<E> {
/// Shutdown the `tokio` runtime when all tasks are idle.
pub fn shutdown_on_idle(self) {
self.runtime
.shutdown_timeout(std::time::Duration::from_secs(MAXIMUM_SHUTDOWN_TIME))
match Arc::try_unwrap(self.runtime) {
Ok(runtime) => {
runtime.shutdown_timeout(std::time::Duration::from_secs(MAXIMUM_SHUTDOWN_TIME))
}
Err(e) => warn!(
self.log,
"Failed to obtain runtime access to shutdown gracefully";
"error" => ?e
),
}
}
/// Fire exit signal which shuts down all spawned services

View File

@@ -7,7 +7,7 @@ use types::{V012LegacyEthSpec, YamlConfig};
fn builder() -> EnvironmentBuilder<V012LegacyEthSpec> {
EnvironmentBuilder::v012_legacy()
.single_thread_tokio_runtime()
.multi_threaded_tokio_runtime()
.expect("should set runtime")
.null_logger()
.expect("should set logger")