mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 10:31:04 +00:00
Reorganising the repository - external renames and moves (#4074)
* Adding first rough ouline of the repository structure * Remove old CI stuff * add title * formatting fixes * move node-exits job's script to scripts dir * Move docs into subdir * move to bin * move maintainence scripts, configs and helpers into its own dir * add .local to ignore * move core->client * start up 'test' area * move test client * move test runtime * make test move compile * Add dependencies rule enforcement. * Fix indexing. * Update docs to reflect latest changes * Moving /srml->/paint * update docs * move client/sr-* -> primitives/ * clean old readme * remove old broken code in rhd * update lock * Step 1. * starting to untangle client * Fix after merge. * start splitting out client interfaces * move children and blockchain interfaces * Move trie and state-machine to primitives. * Fix WASM builds. * fixing broken imports * more interface moves * move backend and light to interfaces * move CallExecutor * move cli off client * moving around more interfaces * re-add consensus crates into the mix * fix subkey path * relieve client from executor * starting to pull out client from grandpa * move is_decendent_of out of client * grandpa still depends on client directly * lemme tests pass * rename srml->paint * Make it compile. * rename interfaces->client-api * Move keyring to primitives. * fixup libp2p dep * fix broken use * allow dependency enforcement to fail * move fork-tree * Moving wasm-builder * make env * move build-script-utils * fixup broken crate depdencies and names * fix imports for authority discovery * fix typo * update cargo.lock * fixing imports * Fix paths and add missing crates * re-add missing crates
This commit is contained in:
committed by
Bastian Köcher
parent
becc3b0a4f
commit
60e5011c72
@@ -0,0 +1,287 @@
|
||||
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Telemetry utilities.
|
||||
//!
|
||||
//! Calling `init_telemetry` registers a global `slog` logger using `slog_scope::set_global_logger`.
|
||||
//! After that, calling `slog_scope::with_logger` will return a logger that sends information to
|
||||
//! the telemetry endpoints. The `telemetry!` macro is a short-cut for calling
|
||||
//! `slog_scope::with_logger` followed with `slog_log!`.
|
||||
//!
|
||||
//! Note that you are supposed to only ever use `telemetry!` and not `slog_scope::with_logger` at
|
||||
//! the moment. Substate may eventually be reworked to get proper `slog` support, including sending
|
||||
//! information to the telemetry.
|
||||
//!
|
||||
//! The [`Telemetry`] struct implements `Stream` and must be polled regularly (or sent to a
|
||||
//! background thread/task) in order for the telemetry to properly function. Dropping the object
|
||||
//! will also deregister the global logger and replace it with a logger that discards messages.
|
||||
//! The `Stream` generates [`TelemetryEvent`]s.
|
||||
//!
|
||||
//! > **Note**: Cloning the [`Telemetry`] and polling from multiple clones has an unspecified behaviour.
|
||||
//!
|
||||
//! # Example
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use futures::prelude::*;
|
||||
//!
|
||||
//! let telemetry = substrate_telemetry::init_telemetry(substrate_telemetry::TelemetryConfig {
|
||||
//! endpoints: substrate_telemetry::TelemetryEndpoints::new(vec![
|
||||
//! // The `0` is the maximum verbosity level of messages to send to this endpoint.
|
||||
//! ("wss://example.com".into(), 0)
|
||||
//! ]),
|
||||
//! // Can be used to pass an external implementation of WebSockets.
|
||||
//! wasm_external_transport: None,
|
||||
//! });
|
||||
//!
|
||||
//! // The `telemetry` object implements `Stream` and must be processed.
|
||||
//! std::thread::spawn(move || {
|
||||
//! futures::executor::block_on(telemetry.for_each(|_| future::ready(())));
|
||||
//! });
|
||||
//!
|
||||
//! // Sends a message on the telemetry.
|
||||
//! substrate_telemetry::telemetry!(substrate_telemetry::SUBSTRATE_INFO; "test";
|
||||
//! "foo" => "bar",
|
||||
//! )
|
||||
//! ```
|
||||
//!
|
||||
|
||||
use futures::{prelude::*, channel::mpsc};
|
||||
use libp2p::{Multiaddr, wasm_ext};
|
||||
use log::warn;
|
||||
use parking_lot::Mutex;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::{pin::Pin, sync::Arc, task::{Context, Poll}, time::{Duration, Instant}};
|
||||
|
||||
pub use slog_scope::with_logger;
|
||||
pub use slog;
|
||||
|
||||
mod worker;
|
||||
|
||||
/// Configuration for telemetry.
|
||||
pub struct TelemetryConfig {
|
||||
/// Collection of telemetry WebSocket servers with a corresponding verbosity level.
|
||||
pub endpoints: TelemetryEndpoints,
|
||||
|
||||
/// Optional external implementation of a libp2p transport. Used in WASM contexts where we need
|
||||
/// some binding between the networking provided by the operating system or environment and
|
||||
/// libp2p.
|
||||
///
|
||||
/// This parameter exists whatever the target platform is, but it is expected to be set to
|
||||
/// `Some` only when compiling for WASM.
|
||||
///
|
||||
/// > **Important**: Each individual call to `write` corresponds to one message. There is no
|
||||
/// > internal buffering going on. In the context of WebSockets, each `write`
|
||||
/// > must be one individual WebSockets frame.
|
||||
pub wasm_external_transport: Option<wasm_ext::ExtTransport>,
|
||||
}
|
||||
|
||||
/// List of telemetry servers we want to talk to. Contains the URL of the server, and the
|
||||
/// maximum verbosity level.
|
||||
///
|
||||
/// The URL string can be either a URL or a multiaddress.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct TelemetryEndpoints(Vec<(String, u8)>);
|
||||
|
||||
impl TelemetryEndpoints {
|
||||
pub fn new(endpoints: Vec<(String, u8)>) -> Self {
|
||||
TelemetryEndpoints(endpoints)
|
||||
}
|
||||
}
|
||||
|
||||
/// Log levels.
|
||||
pub const SUBSTRATE_DEBUG: &str = "9";
|
||||
pub const SUBSTRATE_INFO: &str = "0";
|
||||
|
||||
pub const CONSENSUS_TRACE: &str = "9";
|
||||
pub const CONSENSUS_DEBUG: &str = "5";
|
||||
pub const CONSENSUS_WARN: &str = "4";
|
||||
pub const CONSENSUS_INFO: &str = "1";
|
||||
|
||||
/// Telemetry object. Implements `Future` and must be polled regularly.
|
||||
/// Contains an `Arc` and can be cloned and pass around. Only one clone needs to be polled
|
||||
/// regularly and should be polled regularly.
|
||||
/// Dropping all the clones unregisters the telemetry.
|
||||
#[derive(Clone)]
|
||||
pub struct Telemetry {
|
||||
inner: Arc<Mutex<TelemetryInner>>,
|
||||
/// Slog guard so that we don't get deregistered.
|
||||
_guard: Arc<slog_scope::GlobalLoggerGuard>,
|
||||
}
|
||||
|
||||
/// Behind the `Mutex` in `Telemetry`.
|
||||
///
|
||||
/// Note that ideally we wouldn't have to make the `Telemetry` clonable, as that would remove the
|
||||
/// need for a `Mutex`. However there is currently a weird hack in place in `substrate-service`
|
||||
/// where we extract the telemetry registration so that it continues running during the shutdown
|
||||
/// process.
|
||||
struct TelemetryInner {
|
||||
/// Worker for the telemetry.
|
||||
worker: worker::TelemetryWorker,
|
||||
/// Receives log entries for them to be dispatched to the worker.
|
||||
receiver: mpsc::Receiver<slog_async::AsyncRecord>,
|
||||
}
|
||||
|
||||
/// Implements `slog::Drain`.
|
||||
struct TelemetryDrain {
|
||||
/// Sends log entries.
|
||||
sender: std::panic::AssertUnwindSafe<mpsc::Sender<slog_async::AsyncRecord>>,
|
||||
}
|
||||
|
||||
/// Initializes the telemetry. See the crate root documentation for more information.
|
||||
///
|
||||
/// Please be careful to not call this function twice in the same program. The `slog` crate
|
||||
/// doesn't provide any way of knowing whether a global logger has already been registered.
|
||||
pub fn init_telemetry(config: TelemetryConfig) -> Telemetry {
|
||||
// Build the list of telemetry endpoints.
|
||||
let mut endpoints = Vec::new();
|
||||
for &(ref url, verbosity) in &config.endpoints.0 {
|
||||
match url_to_multiaddr(url) {
|
||||
Ok(addr) => endpoints.push((addr, verbosity)),
|
||||
Err(err) => warn!(target: "telemetry", "Invalid telemetry URL {}: {}", url, err),
|
||||
}
|
||||
}
|
||||
|
||||
let (sender, receiver) = mpsc::channel(16);
|
||||
let guard = {
|
||||
let logger = TelemetryDrain { sender: std::panic::AssertUnwindSafe(sender) };
|
||||
let root = slog::Logger::root(slog::Drain::fuse(logger), slog::o!());
|
||||
slog_scope::set_global_logger(root)
|
||||
};
|
||||
|
||||
Telemetry {
|
||||
inner: Arc::new(Mutex::new(TelemetryInner {
|
||||
worker: worker::TelemetryWorker::new(endpoints, config.wasm_external_transport),
|
||||
receiver,
|
||||
})),
|
||||
_guard: Arc::new(guard),
|
||||
}
|
||||
}
|
||||
|
||||
/// Event generated when polling the worker.
|
||||
#[derive(Debug)]
|
||||
pub enum TelemetryEvent {
|
||||
/// We have established a connection to one of the telemetry endpoint, either for the first
|
||||
/// time or after having been disconnected earlier.
|
||||
Connected,
|
||||
}
|
||||
|
||||
impl Stream for Telemetry {
|
||||
type Item = TelemetryEvent;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
||||
let before = Instant::now();
|
||||
|
||||
// Because the `Telemetry` is clonable, we need to put the actual fields behind a `Mutex`.
|
||||
// However, the user is only ever supposed to poll from one instance of `Telemetry`, while
|
||||
// the other instances are used only for RAII purposes.
|
||||
// We assume that the user is following this advice and therefore that the `Mutex` is only
|
||||
// ever locked once at a time.
|
||||
let mut inner = match self.inner.try_lock() {
|
||||
Some(l) => l,
|
||||
None => {
|
||||
warn!(
|
||||
target: "telemetry",
|
||||
"The telemetry seems to be polled multiple times simultaneously"
|
||||
);
|
||||
// Returning `Pending` here means that we may never get polled again, but this is
|
||||
// ok because we're in a situation where something else is actually currently doing
|
||||
// the polling.
|
||||
return Poll::Pending;
|
||||
}
|
||||
};
|
||||
|
||||
let mut has_connected = false;
|
||||
|
||||
// The polling pattern is: poll the worker so that it processes its queue, then add one
|
||||
// message from the receiver (if possible), then poll the worker again, and so on.
|
||||
loop {
|
||||
while let Poll::Ready(event) = inner.worker.poll(cx) {
|
||||
// Right now we only have one possible event. This line is here in order to not
|
||||
// forget to handle any possible new event type.
|
||||
let worker::TelemetryWorkerEvent::Connected = event;
|
||||
has_connected = true;
|
||||
}
|
||||
|
||||
if let Poll::Ready(Some(log_entry)) = Stream::poll_next(Pin::new(&mut inner.receiver), cx) {
|
||||
log_entry.as_record_values(|rec, val| { let _ = inner.worker.log(rec, val); });
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if before.elapsed() > Duration::from_millis(200) {
|
||||
warn!(target: "telemetry", "Polling the telemetry took more than 200ms");
|
||||
}
|
||||
|
||||
if has_connected {
|
||||
Poll::Ready(Some(TelemetryEvent::Connected))
|
||||
} else {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl slog::Drain for TelemetryDrain {
|
||||
type Ok = ();
|
||||
type Err = ();
|
||||
|
||||
fn log(&self, record: &slog::Record, values: &slog::OwnedKVList) -> Result<Self::Ok, Self::Err> {
|
||||
let before = Instant::now();
|
||||
|
||||
let serialized = slog_async::AsyncRecord::from(record, values);
|
||||
// Note: interestingly, `try_send` requires a `&mut` because it modifies some internal value, while `clone()`
|
||||
// is lock-free.
|
||||
if let Err(err) = self.sender.clone().try_send(serialized) {
|
||||
warn!(target: "telemetry", "Ignored telemetry message because of error on channel: {:?}", err);
|
||||
}
|
||||
|
||||
if before.elapsed() > Duration::from_millis(50) {
|
||||
warn!(target: "telemetry", "Writing a telemetry log took more than 50ms");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses a WebSocket URL into a libp2p `Multiaddr`.
|
||||
fn url_to_multiaddr(url: &str) -> Result<Multiaddr, libp2p::multiaddr::Error> {
|
||||
// First, assume that we have a `Multiaddr`.
|
||||
let parse_error = match url.parse() {
|
||||
Ok(ma) => return Ok(ma),
|
||||
Err(err) => err,
|
||||
};
|
||||
|
||||
// If not, try the `ws://path/url` format.
|
||||
if let Ok(ma) = libp2p::multiaddr::from_url(url) {
|
||||
return Ok(ma)
|
||||
}
|
||||
|
||||
// If we have no clue about the format of that string, assume that we were expecting a
|
||||
// `Multiaddr`.
|
||||
Err(parse_error)
|
||||
}
|
||||
|
||||
/// Translates to `slog_scope::info`, but contains an additional verbosity
|
||||
/// parameter which the log record is tagged with. Additionally the verbosity
|
||||
/// parameter is added to the record as a key-value pair.
|
||||
#[macro_export]
|
||||
macro_rules! telemetry {
|
||||
( $a:expr; $b:expr; $( $t:tt )* ) => {
|
||||
$crate::with_logger(|l| {
|
||||
$crate::slog::slog_info!(l, #$a, $b; $($t)* )
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Contains the object that makes the telemetry work.
|
||||
//!
|
||||
//! # Usage
|
||||
//!
|
||||
//! - Create a `TelemetryWorker` with `TelemetryWorker::new`.
|
||||
//! - Send messages to the telemetry with `TelemetryWorker::send_message`. Messages will only be
|
||||
//! sent to the appropriate targets. Messages may be ignored if the target happens to be
|
||||
//! temporarily unreachable.
|
||||
//! - You must appropriately poll the worker with `TelemetryWorker::poll`. Polling will/may produce
|
||||
//! events indicating what happened since the latest polling.
|
||||
//!
|
||||
|
||||
use bytes::BytesMut;
|
||||
use futures::compat::Compat01As03Sink;
|
||||
use libp2p::{core::transport::OptionalTransport, core::ConnectedPoint, Multiaddr, Transport, wasm_ext};
|
||||
use log::{trace, warn, error};
|
||||
use slog::Drain;
|
||||
use std::{io, pin::Pin, task::Context, task::Poll, time};
|
||||
|
||||
mod node;
|
||||
|
||||
/// Timeout after which a connection attempt is considered failed. Includes the WebSocket HTTP
|
||||
/// upgrading.
|
||||
const CONNECT_TIMEOUT: time::Duration = time::Duration::from_secs(20);
|
||||
|
||||
/// Event generated when polling the worker.
|
||||
#[derive(Debug)]
|
||||
pub enum TelemetryWorkerEvent {
|
||||
/// We have established a connection to one of the telemetry endpoint, either for the first
|
||||
/// time or after having been disconnected earlier.
|
||||
Connected,
|
||||
}
|
||||
|
||||
/// Telemetry processing machine.
|
||||
#[derive(Debug)]
|
||||
pub struct TelemetryWorker {
|
||||
/// List of nodes with their maximum verbosity level.
|
||||
nodes: Vec<(node::Node<WsTrans>, u8)>,
|
||||
}
|
||||
|
||||
/// The pile of libp2p transports.
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
type WsTrans = libp2p::core::transport::timeout::TransportTimeout<
|
||||
libp2p::core::transport::map::Map<
|
||||
libp2p::core::transport::OrTransport<
|
||||
libp2p::core::transport::map::Map<
|
||||
OptionalTransport<wasm_ext::ExtTransport>,
|
||||
fn(wasm_ext::Connection, ConnectedPoint) -> StreamSink<wasm_ext::Connection>
|
||||
>,
|
||||
libp2p::websocket::framed::WsConfig<libp2p::dns::DnsConfig<libp2p::tcp::TcpConfig>>
|
||||
>,
|
||||
fn(libp2p::core::either::EitherOutput<StreamSink<wasm_ext::Connection>,
|
||||
libp2p::websocket::framed::BytesConnection<libp2p::tcp::TcpTransStream>>, ConnectedPoint)
|
||||
-> Compat01As03Sink<libp2p::core::either::EitherOutput<StreamSink<wasm_ext::Connection>,
|
||||
libp2p::websocket::framed::BytesConnection<libp2p::tcp::TcpTransStream>>, BytesMut>
|
||||
>
|
||||
>;
|
||||
#[cfg(target_os = "unknown")]
|
||||
type WsTrans = libp2p::core::transport::timeout::TransportTimeout<
|
||||
libp2p::core::transport::map::Map<
|
||||
libp2p::core::transport::map::Map<
|
||||
OptionalTransport<wasm_ext::ExtTransport>,
|
||||
fn(wasm_ext::Connection, ConnectedPoint) -> StreamSink<wasm_ext::Connection>
|
||||
>,
|
||||
fn(StreamSink<wasm_ext::Connection>, ConnectedPoint)
|
||||
-> Compat01As03Sink<StreamSink<wasm_ext::Connection>, BytesMut>
|
||||
>
|
||||
>;
|
||||
|
||||
impl TelemetryWorker {
|
||||
/// Builds a new `TelemetryWorker`.
|
||||
///
|
||||
/// The endpoints must be a list of targets, plus a verbosity level. When you send a message
|
||||
/// to the telemetry, only the targets whose verbosity is higher than the verbosity of the
|
||||
/// message will receive it.
|
||||
pub fn new(
|
||||
endpoints: impl IntoIterator<Item = (Multiaddr, u8)>,
|
||||
wasm_external_transport: impl Into<Option<wasm_ext::ExtTransport>>
|
||||
) -> Self {
|
||||
let transport = match wasm_external_transport.into() {
|
||||
Some(t) => OptionalTransport::some(t),
|
||||
None => OptionalTransport::none()
|
||||
}.map((|inner, _| StreamSink(inner)) as fn(_, _) -> _);
|
||||
|
||||
// The main transport is the `wasm_external_transport`, but if we're on desktop we add
|
||||
// support for TCP+WebSocket+DNS as a fallback. In practice, you're not expected to pass
|
||||
// an external transport on desktop and the fallback is used all the time.
|
||||
#[cfg(not(target_os = "unknown"))]
|
||||
let transport = transport.or_transport({
|
||||
let inner = libp2p::dns::DnsConfig::new(libp2p::tcp::TcpConfig::new());
|
||||
libp2p::websocket::framed::WsConfig::new(inner)
|
||||
});
|
||||
|
||||
let transport = transport
|
||||
.map((|inner, _| Compat01As03Sink::new(inner)) as fn(_, _) -> _)
|
||||
.timeout(CONNECT_TIMEOUT);
|
||||
|
||||
TelemetryWorker {
|
||||
nodes: endpoints.into_iter().map(|(addr, verbosity)| {
|
||||
let node = node::Node::new(transport.clone(), addr);
|
||||
(node, verbosity)
|
||||
}).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// Polls the worker for events that happened.
|
||||
pub fn poll(&mut self, cx: &mut Context) -> Poll<TelemetryWorkerEvent> {
|
||||
for (node, _) in &mut self.nodes {
|
||||
loop {
|
||||
match node::Node::poll(Pin::new(node), cx) {
|
||||
Poll::Ready(node::NodeEvent::Connected) =>
|
||||
return Poll::Ready(TelemetryWorkerEvent::Connected),
|
||||
Poll::Ready(node::NodeEvent::Disconnected(_)) => continue,
|
||||
Poll::Pending => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Poll::Pending
|
||||
}
|
||||
|
||||
/// Equivalent to `slog::Drain::log`, but takes `self` by `&mut` instead, which is more convenient.
|
||||
///
|
||||
/// Keep in mind that you should call `TelemetryWorker::poll` in order to process the messages.
|
||||
/// You should call this function right after calling `slog::Drain::log`.
|
||||
pub fn log(&mut self, record: &slog::Record, values: &slog::OwnedKVList) -> Result<(), ()> {
|
||||
let msg_verbosity = match record.tag().parse::<u8>() {
|
||||
Ok(v) => v,
|
||||
Err(err) => {
|
||||
warn!(target: "telemetry", "Failed to parse telemetry tag {:?}: {:?}",
|
||||
record.tag(), err);
|
||||
return Err(())
|
||||
}
|
||||
};
|
||||
|
||||
// None of the nodes want that verbosity, so just return without doing any serialization.
|
||||
if self.nodes.iter().all(|(_, node_max_verbosity)| msg_verbosity > *node_max_verbosity) {
|
||||
trace!(
|
||||
target: "telemetry",
|
||||
"Skipping log entry because verbosity {:?} is too high for all endpoints",
|
||||
msg_verbosity
|
||||
);
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
// Turn the message into JSON.
|
||||
let serialized = {
|
||||
let mut out = Vec::new();
|
||||
slog_json::Json::default(&mut out).log(record, values).map_err(|_| ())?;
|
||||
out
|
||||
};
|
||||
|
||||
for (node, node_max_verbosity) in &mut self.nodes {
|
||||
if msg_verbosity > *node_max_verbosity {
|
||||
trace!(target: "telemetry", "Skipping {:?} for log entry with verbosity {:?}",
|
||||
node.addr(), msg_verbosity);
|
||||
continue;
|
||||
}
|
||||
|
||||
// `send_message` returns an error if we're not connected, which we silently ignore.
|
||||
let _ = node.send_message(serialized.clone());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Wraps around an `AsyncWrite` and implements `Sink`. Guarantees that each item being sent maps
|
||||
/// to one call of `write`.
|
||||
///
|
||||
/// For some context, we put this object around the `wasm_ext::ExtTransport` in order to make sure
|
||||
/// that each telemetry message maps to one single call to `write` in the WASM FFI.
|
||||
struct StreamSink<T>(T);
|
||||
|
||||
impl<T: tokio_io::AsyncRead> futures01::Stream for StreamSink<T> {
|
||||
type Item = BytesMut;
|
||||
type Error = io::Error;
|
||||
|
||||
fn poll(&mut self) -> futures01::Poll<Option<Self::Item>, Self::Error> {
|
||||
let mut buf = [0; 128];
|
||||
Ok(self.0.poll_read(&mut buf)?
|
||||
.map(|n|
|
||||
if n == 0 {
|
||||
None
|
||||
} else {
|
||||
let buf: BytesMut = buf[..n].into();
|
||||
Some(buf)
|
||||
}
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: tokio_io::AsyncWrite> futures01::Sink for StreamSink<T> {
|
||||
type SinkItem = BytesMut;
|
||||
type SinkError = io::Error;
|
||||
|
||||
fn start_send(&mut self, item: Self::SinkItem)
|
||||
-> Result<futures01::AsyncSink<Self::SinkItem>, io::Error> {
|
||||
match self.0.write(&item[..]) {
|
||||
Ok(n) if n == item.len() => Ok(futures01::AsyncSink::Ready),
|
||||
Ok(_) => {
|
||||
error!(target: "telemetry",
|
||||
"Detected some internal buffering happening in the telemetry");
|
||||
Err(io::Error::new(io::ErrorKind::Other, "Internal buffering detected"))
|
||||
},
|
||||
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock =>
|
||||
Ok(futures01::AsyncSink::NotReady(item)),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_complete(&mut self) -> futures01::Poll<(), io::Error> {
|
||||
match self.0.flush() {
|
||||
Ok(()) => Ok(futures01::Async::Ready(())),
|
||||
Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => Ok(futures01::Async::NotReady),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,299 @@
|
||||
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Contains the `Node` struct, which handles communications with a single telemetry endpoint.
|
||||
|
||||
use bytes::BytesMut;
|
||||
use futures::{prelude::*, compat::{Future01CompatExt as _, Compat01As03}};
|
||||
use futures_timer::Delay;
|
||||
use libp2p::Multiaddr;
|
||||
use libp2p::core::transport::Transport;
|
||||
use log::{trace, debug, warn, error};
|
||||
use rand::Rng as _;
|
||||
use std::{collections::VecDeque, fmt, mem, pin::Pin, task::Context, task::Poll, time::Duration};
|
||||
|
||||
/// Maximum number of pending telemetry messages.
|
||||
const MAX_PENDING: usize = 10;
|
||||
|
||||
/// Handler for a single telemetry node.
|
||||
pub struct Node<TTrans: Transport> {
|
||||
/// Address of the node.
|
||||
addr: Multiaddr,
|
||||
/// State of the connection.
|
||||
socket: NodeSocket<TTrans>,
|
||||
/// Transport used to establish new connections.
|
||||
transport: TTrans,
|
||||
}
|
||||
|
||||
enum NodeSocket<TTrans: Transport> {
|
||||
/// We're connected to the node. This is the normal state.
|
||||
Connected(NodeSocketConnected<TTrans>),
|
||||
/// We are currently dialing the node.
|
||||
Dialing(Compat01As03<TTrans::Dial>),
|
||||
/// A new connection should be started as soon as possible.
|
||||
ReconnectNow,
|
||||
/// Waiting before attempting to dial again.
|
||||
WaitingReconnect(Delay),
|
||||
/// Temporary transition state.
|
||||
Poisoned,
|
||||
}
|
||||
|
||||
struct NodeSocketConnected<TTrans: Transport> {
|
||||
/// Where to send data.
|
||||
sink: TTrans::Output,
|
||||
/// Queue of packets to send.
|
||||
pending: VecDeque<BytesMut>,
|
||||
/// If true, we need to flush the sink.
|
||||
need_flush: bool,
|
||||
/// A timeout for the socket to write data.
|
||||
timeout: Option<Delay>,
|
||||
}
|
||||
|
||||
/// Event that can happen with this node.
|
||||
#[derive(Debug)]
|
||||
pub enum NodeEvent<TSinkErr> {
|
||||
/// We are now connected to this node.
|
||||
Connected,
|
||||
/// We are now disconnected from this node.
|
||||
Disconnected(ConnectionError<TSinkErr>),
|
||||
}
|
||||
|
||||
/// Reason for disconnecting from a node.
|
||||
#[derive(Debug)]
|
||||
pub enum ConnectionError<TSinkErr> {
|
||||
/// The connection timed-out.
|
||||
Timeout,
|
||||
/// The sink errored.
|
||||
Sink(TSinkErr),
|
||||
}
|
||||
|
||||
impl<TTrans: Transport> Node<TTrans> {
|
||||
/// Builds a new node handler.
|
||||
pub fn new(transport: TTrans, addr: Multiaddr) -> Self {
|
||||
Node {
|
||||
addr,
|
||||
socket: NodeSocket::ReconnectNow,
|
||||
transport,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the address that was passed to `new`.
|
||||
pub fn addr(&self) -> &Multiaddr {
|
||||
&self.addr
|
||||
}
|
||||
}
|
||||
|
||||
impl<TTrans: Transport, TSinkErr> Node<TTrans>
|
||||
where TTrans: Clone + Unpin, TTrans::Dial: Unpin,
|
||||
TTrans::Output: Sink<BytesMut, Error = TSinkErr>
|
||||
+ Stream<Item=Result<BytesMut, TSinkErr>>
|
||||
+ Unpin,
|
||||
TSinkErr: fmt::Debug
|
||||
{
|
||||
/// Sends a WebSocket frame to the node. Returns an error if we are not connected to the node.
|
||||
///
|
||||
/// After calling this method, you should call `poll` in order for it to be properly processed.
|
||||
pub fn send_message(&mut self, payload: Vec<u8>) -> Result<(), ()> {
|
||||
if let NodeSocket::Connected(NodeSocketConnected { pending, .. }) = &mut self.socket {
|
||||
if pending.len() <= MAX_PENDING {
|
||||
trace!(target: "telemetry", "Adding log entry to queue for {:?}", self.addr);
|
||||
pending.push_back(payload.into());
|
||||
Ok(())
|
||||
} else {
|
||||
warn!(target: "telemetry", "Rejected log entry because queue is full for {:?}",
|
||||
self.addr);
|
||||
Err(())
|
||||
}
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Polls the node for updates. Must be performed regularly.
|
||||
pub fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<NodeEvent<TSinkErr>> {
|
||||
let mut socket = mem::replace(&mut self.socket, NodeSocket::Poisoned);
|
||||
self.socket = loop {
|
||||
match socket {
|
||||
NodeSocket::Connected(mut conn) => {
|
||||
match NodeSocketConnected::poll(Pin::new(&mut conn), cx, &self.addr) {
|
||||
Poll::Ready(Ok(v)) => match v {},
|
||||
Poll::Pending => {
|
||||
break NodeSocket::Connected(conn)
|
||||
},
|
||||
Poll::Ready(Err(err)) => {
|
||||
warn!(target: "telemetry", "Disconnected from {}: {:?}", self.addr, err);
|
||||
let timeout = gen_rand_reconnect_delay();
|
||||
self.socket = NodeSocket::WaitingReconnect(timeout);
|
||||
return Poll::Ready(NodeEvent::Disconnected(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
NodeSocket::Dialing(mut s) => match Future::poll(Pin::new(&mut s), cx) {
|
||||
Poll::Ready(Ok(sink)) => {
|
||||
debug!(target: "telemetry", "Connected to {}", self.addr);
|
||||
let conn = NodeSocketConnected {
|
||||
sink,
|
||||
pending: VecDeque::new(),
|
||||
need_flush: false,
|
||||
timeout: None,
|
||||
};
|
||||
self.socket = NodeSocket::Connected(conn);
|
||||
return Poll::Ready(NodeEvent::Connected)
|
||||
},
|
||||
Poll::Pending => break NodeSocket::Dialing(s),
|
||||
Poll::Ready(Err(err)) => {
|
||||
warn!(target: "telemetry", "Error while dialing {}: {:?}", self.addr, err);
|
||||
let timeout = gen_rand_reconnect_delay();
|
||||
socket = NodeSocket::WaitingReconnect(timeout);
|
||||
}
|
||||
}
|
||||
NodeSocket::ReconnectNow => match self.transport.clone().dial(self.addr.clone()) {
|
||||
Ok(d) => {
|
||||
debug!(target: "telemetry", "Started dialing {}", self.addr);
|
||||
socket = NodeSocket::Dialing(d.compat());
|
||||
}
|
||||
Err(err) => {
|
||||
warn!(target: "telemetry", "Error while dialing {}: {:?}", self.addr, err);
|
||||
let timeout = gen_rand_reconnect_delay();
|
||||
socket = NodeSocket::WaitingReconnect(timeout);
|
||||
}
|
||||
}
|
||||
NodeSocket::WaitingReconnect(mut s) =>
|
||||
if let Poll::Ready(_) = Future::poll(Pin::new(&mut s), cx) {
|
||||
socket = NodeSocket::ReconnectNow;
|
||||
} else {
|
||||
break NodeSocket::WaitingReconnect(s)
|
||||
}
|
||||
NodeSocket::Poisoned => {
|
||||
error!(target: "telemetry", "Poisoned connection with {}", self.addr);
|
||||
break NodeSocket::Poisoned
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates a `Delay` object with a random timeout.
|
||||
///
|
||||
/// If there are general connection issues, not all endpoints should be synchronized in their
|
||||
/// re-connection time.
|
||||
fn gen_rand_reconnect_delay() -> Delay {
|
||||
let random_delay = rand::thread_rng().gen_range(5, 10);
|
||||
Delay::new(Duration::from_secs(random_delay))
|
||||
}
|
||||
|
||||
impl<TTrans: Transport, TSinkErr> NodeSocketConnected<TTrans>
|
||||
where TTrans::Output: Sink<BytesMut, Error = TSinkErr>
|
||||
+ Stream<Item=Result<BytesMut, TSinkErr>>
|
||||
+ Unpin
|
||||
{
|
||||
/// Processes the queue of messages for the connected socket.
|
||||
///
|
||||
/// The address is passed for logging purposes only.
|
||||
fn poll(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context,
|
||||
my_addr: &Multiaddr,
|
||||
) -> Poll<Result<futures::never::Never, ConnectionError<TSinkErr>>> {
|
||||
|
||||
while let Some(item) = self.pending.pop_front() {
|
||||
if let Poll::Ready(_) = Sink::poll_ready(Pin::new(&mut self.sink), cx) {
|
||||
let item_len = item.len();
|
||||
if let Err(err) = Sink::start_send(Pin::new(&mut self.sink), item) {
|
||||
self.timeout = None;
|
||||
return Poll::Ready(Err(ConnectionError::Sink(err)))
|
||||
}
|
||||
trace!(
|
||||
target: "telemetry", "Successfully sent {:?} bytes message to {}",
|
||||
item_len, my_addr
|
||||
);
|
||||
self.need_flush = true;
|
||||
|
||||
} else {
|
||||
self.pending.push_front(item);
|
||||
if self.timeout.is_none() {
|
||||
self.timeout = Some(Delay::new(Duration::from_secs(10)));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if self.need_flush {
|
||||
match Sink::poll_flush(Pin::new(&mut self.sink), cx) {
|
||||
Poll::Pending => {
|
||||
if self.timeout.is_none() {
|
||||
self.timeout = Some(Delay::new(Duration::from_secs(10)));
|
||||
}
|
||||
},
|
||||
Poll::Ready(Err(err)) => {
|
||||
self.timeout = None;
|
||||
return Poll::Ready(Err(ConnectionError::Sink(err)))
|
||||
},
|
||||
Poll::Ready(Ok(())) => {
|
||||
self.timeout = None;
|
||||
self.need_flush = false;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(timeout) = self.timeout.as_mut() {
|
||||
match Future::poll(Pin::new(timeout), cx) {
|
||||
Poll::Pending => {},
|
||||
Poll::Ready(Err(err)) => {
|
||||
self.timeout = None;
|
||||
warn!(target: "telemetry", "Connection timeout error for {} {:?}", my_addr, err);
|
||||
}
|
||||
Poll::Ready(Ok(_)) => {
|
||||
self.timeout = None;
|
||||
return Poll::Ready(Err(ConnectionError::Timeout))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match Stream::poll_next(Pin::new(&mut self.sink), cx) {
|
||||
Poll::Ready(Some(Ok(_))) => {
|
||||
// We poll the telemetry `Stream` because the underlying implementation relies on
|
||||
// this in order to answer PINGs.
|
||||
// We don't do anything with incoming messages, however.
|
||||
},
|
||||
Poll::Ready(Some(Err(err))) => {
|
||||
return Poll::Ready(Err(ConnectionError::Sink(err)))
|
||||
},
|
||||
Poll::Pending | Poll::Ready(None) => {},
|
||||
}
|
||||
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
|
||||
impl<TTrans: Transport> fmt::Debug for Node<TTrans> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let state = match self.socket {
|
||||
NodeSocket::Connected(_) => "Connected",
|
||||
NodeSocket::Dialing(_) => "Dialing",
|
||||
NodeSocket::ReconnectNow => "Pending reconnect",
|
||||
NodeSocket::WaitingReconnect(_) => "Pending reconnect",
|
||||
NodeSocket::Poisoned => "Poisoned",
|
||||
};
|
||||
|
||||
f.debug_struct("Node")
|
||||
.field("addr", &self.addr)
|
||||
.field("state", &state)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user