diff options
| -rw-r--r-- | CHANGELOG.md | 7 | ||||
| -rw-r--r-- | docs/operations.md | 30 | ||||
| -rw-r--r-- | docs/transports.md | 27 | ||||
| -rw-r--r-- | src/config/mod.rs | 28 | ||||
| -rw-r--r-- | src/lib.rs | 18 | ||||
| -rw-r--r-- | src/main.rs | 7 | ||||
| -rw-r--r-- | src/simtest/identity.rs | 1 | ||||
| -rw-r--r-- | src/transport/libp2p/behaviour.rs | 16 | ||||
| -rw-r--r-- | src/transport/libp2p/config.rs | 4 | ||||
| -rw-r--r-- | src/transport/libp2p/tests.rs | 3 |
10 files changed, 134 insertions, 7 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 2000c11..fe0ba3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,13 @@ it from the release before ([`docs/operations.md`](docs/operations.md), ([`docs/configuration.md`](docs/configuration.md), [Importing values](docs/configuration.md#importing-values)). +* `--libp2p-ping-interval`: how often libp2p pings each connection, 15 s as before + unless given, `0` for once as it opens; and `--worker-threads`: how many threads take + the node's work in turns, as many as the machine has cores unless given. Both for + nodes on machines that should idle + ([`docs/operations.md`](docs/operations.md), [An idle node](docs/operations.md#an-idle-node); + [`docs/transports.md`](docs/transports.md), [Pings](docs/transports.md#pings)). + * The `config` crate takes variables of the program that uses it: `${var:NAME}`, or `$(var:NAME)`, is replaced by the one of that name (`Variables`, `Importer::fetch_with`), under the rules of every placeholder; an unknown one is an diff --git a/docs/operations.md b/docs/operations.md index 0777c52..e3355e5 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -331,6 +331,36 @@ And from the log, at warn or error: * `Peer speaks another wire version: …; ignoring its messages`: a node on another build; the message names both versions, and whether to upgrade this node or that one. +## An idle node + +A node that nothing is written to still wakes: for its keepalives (`--keepalive`, two +per neighbour and interval), for what it checks once a minute (its entry in the member +list, whom it reaches, its clock against its peers'), and for its transports' own +timers. Each time, some of its threads wake. On a machine that should sleep between +those (a laptop, a small server that does other things), two options make it less: + +* **`--worker-threads COUNT`**: how many threads take the node's work in turns. As + many as the machine has cores unless given; whatever wakes the node may wake several + of them, as they hand work to each other. What takes long (reading and saving the + document, judging a store's events, a remake's snapshot) runs on threads of its own + beside them, started as needed and ended when idle, so few worker threads don't make + those wait. The count is set as the node starts: a restart changes it. + * **2** suits most nodes: a member of a swarm with little writing, on a machine with + other work. + * **The default** suits a node that serves many API clients or streams at once, or + relays for many peers: there, more threads take more at a time. + * **1** works, but one piece of work that takes long holds up everything else, + keepalives among it: not below 2 without a reason. +* **`--libp2p-ping-interval DURATION`**: how often libp2p pings each connection; `0` + for once only ([`transports.md`](transports.md), [Pings](transports.md#pings)). + +Measured here, three nodes on one machine of 12 cores over libp2p (TCP), nothing +written, each node's thread wakeups a second over a minute and a half: 4 to 6 as +shipped; 3 to 5 with two worker threads or without pings; 2 to 3 with both. The +figures move from one minute to the next, so take them for their order, not their +digits. `top -H -p PID` shows the threads (`tokio-rt-worker`, and `futures-timer`, +libp2p's), and `/proc/PID/task/*/status` their `voluntary_ctxt_switches`. + ## What a node uses * **Memory**: the document with its history (Automerge holds more in memory than diff --git a/docs/transports.md b/docs/transports.md index 2b1b07e..b3a01b7 100644 --- a/docs/transports.md +++ b/docs/transports.md @@ -244,6 +244,33 @@ traffic for long. It also makes the node's peer ID and addresses known to the DH to anyone. Without it, nothing public is used. (For Mycelium, public peers are in `--mycelium-peers`: none unless given.) +### Pings + +libp2p pings every open connection, once as it opens and then every +`--libp2p-ping-interval` (15 s unless given; `0` for never again). continuum uses the +pings for one thing: the round trip of each connection, by which it keeps the fastest +of several to one peer, the shortest each measured counting. They keep no connection +open and close none: a connection nobody uses for five minutes, or for three +`--keepalive` intervals where that is longer, is closed whatever was pinged, and whether a +peer answers is told by continuum's own keepalives, on whatever transport. Other +libp2p nodes' pings are answered whatever the interval. + +So a longer interval, or none, costs only how well a connection's round trip is known +after its first ping: a first ping that came back late (a busy moment) is not put +right by a later one, and the slower of two connections may be kept. What it spares +is a wakeup of the node per connection and interval, and over TCP the pings of the +stream multiplexer (Yamux), which measures its own round trip only while a connection +is used. QUIC keeps its connections open by itself, every few seconds, whatever this +says. + +* **Leave it at 15 s** where round trips matter and vary: nodes with several paths to + each other of different speed (a LAN and a relay, ethernet and WLAN) that come and + go. +* **A few minutes** (`5m`) is a middle: the round trip is put right within that time. +* **`0`** on nodes that reach each other one way (servers at fixed addresses), and + wherever a machine should be left idle + ([`operations.md`](operations.md), [An idle node](operations.md#an-idle-node)). + ### On the LAN With `--libp2p-mdns`, a node finds libp2p peers on its LAN (multicast DNS, IPv4): it diff --git a/src/config/mod.rs b/src/config/mod.rs index 1b9a439..6f9bd98 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -504,6 +504,13 @@ pub(crate) const OPTIONS: &[OptionSpec] = &[ "Longest wait between HELLOs to a member that doesn't answer (dormant, or unanswered HELLOs): they start at 10 keepalives and double up to this. It bounds how long a returning member, or the other side of a partition, takes to be noticed. Default: 60 keepalives (1h at 1m). Shorter than 10 keepalives, it is every wait.", ), option( + "worker-threads", + Kind::Integer, + "COUNT", + None, + "How many threads take the node's work in turns (at least 1): as many as the machine has cores unless given. What takes long (reading and saving the document, judging a store's events, a remake's snapshot) runs on threads of its own beside them, started as needed. Fewer wake less often on a node that is idle most of the time; set as the node starts, and changed only by a restart (docs/operations.md, Worker threads).", + ), + option( "neighbours", Kind::Integer, "COUNT", @@ -637,6 +644,13 @@ pub(crate) const OPTIONS: &[OptionSpec] = &[ "With the libp2p transport: find nodes on the LAN (multicast DNS, IPv4). Those that speak continuum are seeds while joining, so nodes on one LAN find each other without configured seeds; a swarm key keeps a new node from joining another swarm on the same LAN.", ), option( + "libp2p-ping-interval", + Kind::Duration, + "DURATION", + Some("15s"), + "With the libp2p transport: how often each open connection is pinged, for its round trip: of several connections to one peer the fastest is kept, by the shortest round trip each measured. Every connection is pinged once as it opens, whatever this says; 0 leaves it at that. The pings keep no connection open and close none: continuum's own keepalives tell whether a peer answers. A longer interval, or none, spares a node that is idle most of the time the wakeups (docs/transports.md, Pings).", + ), + option( "libp2p-upnp", Kind::Flag, "BOOL", @@ -961,6 +975,8 @@ pub(crate) struct Settings { serialize_with = "crate::units::serialize_optional_duration" )] pub(crate) dormant_probe_max: Option<Duration>, + #[serde(default)] + pub(crate) worker_threads: Option<usize>, #[serde(default = "default_neighbours")] pub(crate) neighbours: usize, #[serde( @@ -1012,6 +1028,12 @@ pub(crate) struct Settings { #[serde(default)] #[cfg_attr(not(feature = "libp2p"), allow(dead_code))] pub(crate) libp2p_upnp: bool, + #[serde( + default = "default_libp2p_ping_interval", + with = "crate::units::duration_option" + )] + #[cfg_attr(not(feature = "libp2p"), allow(dead_code))] + pub(crate) libp2p_ping_interval: Duration, #[serde(default)] #[cfg_attr(not(feature = "libp2p"), allow(dead_code))] pub(crate) libp2p_ignore: Vec<String>, @@ -1099,6 +1121,9 @@ fn default_keepalive() -> Duration { fn default_stream_heartbeat() -> Duration { Duration::from_secs(30) } +fn default_libp2p_ping_interval() -> Duration { + Duration::from_secs(15) +} fn default_neighbours() -> usize { 12 } @@ -1284,6 +1309,9 @@ impl Settings { if self.debug_epoch.is_some() && !self.debug_api { bail!("debug-epoch needs debug-api"); } + if self.worker_threads == Some(0) { + bail!("worker-threads must be at least 1"); + } if self.neighbours < 2 { bail!("neighbours must be at least 2"); } @@ -77,8 +77,9 @@ fn voter(args: &config::Settings) -> Result<Option<federation::held::Voter>> { })) } -/// Runs a node, as its arguments and configuration say, until it stops. -pub async fn run() -> Result<()> { +/// Runs a node, as its arguments and configuration say, until it stops: on a runtime +/// of as many worker threads as `--worker-threads` says, or as the machine has cores. +pub fn main() -> Result<()> { logging::init(); if let Some((file, create)) = config::asked_node_id() { @@ -98,6 +99,19 @@ pub async fn run() -> Result<()> { ); return Ok(()); } + let mut runtime = tokio::runtime::Builder::new_multi_thread(); + runtime.enable_all(); + if let Some(threads) = config.settings.worker_threads { + runtime.worker_threads(threads); + } + runtime + .build() + .context("Starting the runtime")? + .block_on(run(config)) +} + +/// Runs the node `config` describes, until it stops. +async fn run(config: config::Loaded) -> Result<()> { let args = &config.settings; if let Some(format) = logging::LogFormat::parse(&args.log_format) { logging::set_format(format); diff --git a/src/main.rs b/src/main.rs index 00ab098..9a7e295 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ -//! A continuum node: the library's (`continuum::run`). +//! A continuum node: the library's (`continuum::main`). -#[tokio::main] -async fn main() -> anyhow::Result<()> { - continuum::run().await +fn main() -> anyhow::Result<()> { + continuum::main() } diff --git a/src/simtest/identity.rs b/src/simtest/identity.rs index f1a1b05..0df40f9 100644 --- a/src/simtest/identity.rs +++ b/src/simtest/identity.rs @@ -226,6 +226,7 @@ async fn nodes_over_libp2p() { continuum: true, mdns: false, upnp: false, + ping_interval: std::time::Duration::from_secs(15), ignore: Vec::new(), prefer: Vec::new(), internet_hosts: Vec::new(), diff --git a/src/transport/libp2p/behaviour.rs b/src/transport/libp2p/behaviour.rs index 0b9be6b..33155d0 100644 --- a/src/transport/libp2p/behaviour.rs +++ b/src/transport/libp2p/behaviour.rs @@ -187,6 +187,8 @@ pub(super) struct Features { pub(super) relay: bool, pub(super) mdns: bool, pub(super) upnp: bool, + /// How often an open connection is pinged, after the ping as it opens. + pub(super) ping_interval: Duration, pub(super) public: bool, /// How long a connection may idle (`idle_timeout`). pub(super) idle_timeout: Duration, @@ -198,12 +200,21 @@ impl Features { relay: config.relay, mdns: config.mdns, upnp: config.upnp, + // None: so long that none comes. A connection's first ping goes as it opens. + ping_interval: if config.ping_interval.is_zero() { + NO_PINGS + } else { + config.ping_interval + }, public: config.public, idle_timeout: idle_timeout(config.keepalive), } } } +/// The interval of pings that never come (`--libp2p-ping-interval 0`): ten years. +const NO_PINGS: Duration = Duration::from_secs(10 * 365 * 24 * 3600); + /// The IPFS DHT's protocol, and its bootstrap nodes (all at `IPFS_BOOTSTRAP_ADDRESS`). pub(super) const IPFS_DHT: StreamProtocol = StreamProtocol::new("/ipfs/kad/1.0.0"); @@ -230,6 +241,7 @@ impl Behaviour { relay, mdns, upnp, + ping_interval, public, idle_timeout: _, } = features; @@ -286,7 +298,9 @@ impl Behaviour { relay_client, relay: relay.into(), dcutr: HolePunching::new(peer_id, ignored), - ping: ::libp2p::ping::Behaviour::new(::libp2p::ping::Config::new()), + ping: ::libp2p::ping::Behaviour::new( + ::libp2p::ping::Config::new().with_interval(ping_interval), + ), autonat: ::libp2p::autonat::Behaviour::new( peer_id, ::libp2p::autonat::Config { diff --git a/src/transport/libp2p/config.rs b/src/transport/libp2p/config.rs index 712f2e2..70a8561 100644 --- a/src/transport/libp2p/config.rs +++ b/src/transport/libp2p/config.rs @@ -21,6 +21,9 @@ pub(crate) struct Libp2pConfig { pub(crate) mdns: bool, /// Ask the router to forward our listen ports (UPnP), and publish where. pub(crate) upnp: bool, + /// How often each open connection is pinged after the ping as it opens; zero for + /// never (`--libp2p-ping-interval`). + pub(crate) ping_interval: std::time::Duration, /// Address ranges neither published nor dialled, nor connected over (`Ignoring`). pub(crate) ignore: Vec<IpRange>, /// Address ranges that matter most, best first (`Book::preference`). @@ -136,6 +139,7 @@ impl Libp2pConfig { continuum: !relay_only, mdns: settings.libp2p_mdns, upnp: settings.libp2p_upnp, + ping_interval: settings.libp2p_ping_interval, ignore: parse_ranges("libp2p-ignore", &settings.libp2p_ignore)?, prefer: parse_ranges("libp2p-prefer", &settings.libp2p_prefer)?, internet_hosts: settings.libp2p_internet_hosts.clone(), diff --git a/src/transport/libp2p/tests.rs b/src/transport/libp2p/tests.rs index 93f251f..7c9f0f0 100644 --- a/src/transport/libp2p/tests.rs +++ b/src/transport/libp2p/tests.rs @@ -41,6 +41,7 @@ fn memory_node_with( continuum: true, mdns: false, upnp: false, + ping_interval: std::time::Duration::from_secs(15), ignore: Vec::new(), prefer: Vec::new(), internet_hosts: Vec::new(), @@ -231,6 +232,7 @@ async fn announced_addresses_are_published_instead() { continuum: true, mdns: false, upnp: false, + ping_interval: std::time::Duration::from_secs(15), ignore: Vec::new(), prefer: Vec::new(), internet_hosts: Vec::new(), @@ -473,6 +475,7 @@ async fn a_relay_that_is_no_node() { continuum: false, mdns: false, upnp: false, + ping_interval: std::time::Duration::from_secs(15), ignore: Vec::new(), prefer: Vec::new(), internet_hosts: Vec::new(), |
