From 39e3c9a13bd2e197fccbf194b9c32a16a85ae1ae Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 13:50:07 +0100 Subject: [PATCH 01/92] Rework sc-telemetry to not use tracing internally --- Cargo.lock | 2 - client/telemetry/Cargo.toml | 2 - client/telemetry/src/layer.rs | 149 -------------------------- client/telemetry/src/lib.rs | 195 ++++++++++++++++++++-------------- client/telemetry/src/node.rs | 27 +++-- 5 files changed, 133 insertions(+), 242 deletions(-) delete mode 100644 client/telemetry/src/layer.rs diff --git a/Cargo.lock b/Cargo.lock index e3c9131a86e3b..7fa0cebb41c21 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7660,8 +7660,6 @@ dependencies = [ "serde_json", "sp-utils", "take_mut", - "tracing", - "tracing-subscriber", "void", "wasm-timer", ] diff --git a/client/telemetry/Cargo.toml b/client/telemetry/Cargo.toml index 23b6936ff4057..99fa00dc6db71 100644 --- a/client/telemetry/Cargo.toml +++ b/client/telemetry/Cargo.toml @@ -25,8 +25,6 @@ rand = "0.7.2" serde = { version = "1.0.101", features = ["derive"] } take_mut = "0.2.2" void = "1.0.2" -tracing = "0.1.10" -tracing-subscriber = "0.2.13" serde_json = "1.0.41" sp-utils = { version = "3.0.0", path = "../../primitives/utils" } chrono = "0.4.19" diff --git a/client/telemetry/src/layer.rs b/client/telemetry/src/layer.rs deleted file mode 100644 index 0ce3f97620da9..0000000000000 --- a/client/telemetry/src/layer.rs +++ /dev/null @@ -1,149 +0,0 @@ -// This file is part of Substrate. - -// Copyright (C) 2021 Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program 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. - -// This program 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 this program. If not, see . - -use crate::{initialize_transport, TelemetryWorker}; -use futures::channel::mpsc; -use libp2p::wasm_ext::ExtTransport; -use parking_lot::Mutex; -use std::convert::TryInto; -use std::io; -use tracing::{Event, Id, Subscriber}; -use tracing_subscriber::{layer::Context, registry::LookupSpan, Layer}; - -/// Span name used to report the telemetry. -pub const TELEMETRY_LOG_SPAN: &str = "telemetry-logger"; - -/// `Layer` that handles the logs for telemetries. -#[derive(Debug)] -pub struct TelemetryLayer(Mutex>); - -impl TelemetryLayer { - /// Create a new [`TelemetryLayer`] and [`TelemetryWorker`]. - /// - /// The `buffer_size` defaults to 16. - /// - /// The [`ExtTransport`] is used in WASM contexts where we need some binding between the - /// networking provided by the operating system or environment and libp2p. - /// - /// > **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 fn new( - buffer_size: Option, - telemetry_external_transport: Option, - ) -> io::Result<(Self, TelemetryWorker)> { - let transport = initialize_transport(telemetry_external_transport)?; - let worker = TelemetryWorker::new(buffer_size.unwrap_or(16), transport); - let sender = worker.message_sender(); - Ok((Self(Mutex::new(sender)), worker)) - } -} - -impl Layer for TelemetryLayer -where - S: Subscriber + for<'a> LookupSpan<'a>, -{ - fn on_event(&self, event: &Event<'_>, ctx: Context) { - if event.metadata().target() != TELEMETRY_LOG_SPAN { - return; - } - - if let Some(span) = ctx.lookup_current() { - let parents = span.parents(); - - if let Some(span) = std::iter::once(span) - .chain(parents) - .find(|x| x.name() == TELEMETRY_LOG_SPAN) - { - let id = span.id(); - let mut attrs = TelemetryAttrs::new(id.clone()); - let mut vis = TelemetryAttrsVisitor(&mut attrs); - event.record(&mut vis); - - if let TelemetryAttrs { - verbosity: Some(verbosity), - json: Some(json), - .. - } = attrs - { - match self.0.lock().try_send(( - id, - verbosity - .try_into() - .expect("telemetry log message verbosity are u8; qed"), - json, - )) { - Err(err) if err.is_full() => eprintln!("Telemetry buffer overflowed!"), - _ => {} - } - } else { - // NOTE: logging in this function doesn't work - eprintln!( - "missing fields in telemetry log: {:?}. This can happen if \ - `tracing::info_span!` is (mis-)used with the telemetry target \ - directly; you should use the `telemetry!` macro.", - event, - ); - } - } - } - } -} - -#[derive(Debug)] -struct TelemetryAttrs { - verbosity: Option, - json: Option, - id: Id, -} - -impl TelemetryAttrs { - fn new(id: Id) -> Self { - Self { - verbosity: None, - json: None, - id, - } - } -} - -#[derive(Debug)] -struct TelemetryAttrsVisitor<'a>(&'a mut TelemetryAttrs); - -impl<'a> tracing::field::Visit for TelemetryAttrsVisitor<'a> { - fn record_debug(&mut self, _field: &tracing::field::Field, _value: &dyn std::fmt::Debug) { - // noop - } - - fn record_u64(&mut self, field: &tracing::field::Field, value: u64) { - if field.name() == "verbosity" { - (*self.0).verbosity = Some(value); - } - } - - fn record_str(&mut self, field: &tracing::field::Field, value: &str) { - if field.name() == "json" { - (*self.0).json = Some(format!( - r#"{{"id":{},"ts":{:?},"payload":{}}}"#, - self.0.id.into_u64(), - chrono::Local::now().to_rfc3339().to_string(), - value, - )); - } - } -} diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index b398ee86de4ec..a4b0d6d77174d 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -39,61 +39,43 @@ use futures::{channel::mpsc, prelude::*}; use libp2p::Multiaddr; use log::{error, warn}; +use parking_lot::Mutex; use serde::Serialize; use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver}; use std::collections::HashMap; -use tracing::Id; +use std::io; +use std::iter; +use std::sync::Arc; pub use libp2p::wasm_ext::ExtTransport; pub use serde_json; -pub use tracing; mod endpoints; -mod layer; mod node; mod transport; pub use endpoints::*; -pub use layer::*; use node::*; use transport::*; /// Substrate DEBUG log level. -pub const SUBSTRATE_DEBUG: u8 = 9; +pub const SUBSTRATE_DEBUG: VerbosityLevel = 9; /// Substrate INFO log level. -pub const SUBSTRATE_INFO: u8 = 0; +pub const SUBSTRATE_INFO: VerbosityLevel = 0; /// Consensus TRACE log level. -pub const CONSENSUS_TRACE: u8 = 9; +pub const CONSENSUS_TRACE: VerbosityLevel = 9; /// Consensus DEBUG log level. -pub const CONSENSUS_DEBUG: u8 = 5; +pub const CONSENSUS_DEBUG: VerbosityLevel = 5; /// Consensus WARN log level. -pub const CONSENSUS_WARN: u8 = 4; +pub const CONSENSUS_WARN: VerbosityLevel = 4; /// Consensus INFO log level. -pub const CONSENSUS_INFO: u8 = 1; +pub const CONSENSUS_INFO: VerbosityLevel = 1; -pub(crate) type TelemetryMessage = (Id, u8, String); - -/// A handle representing a telemetry span, with the capability to enter the span if it exists. -#[derive(Debug, Clone)] -pub struct TelemetrySpan(tracing::Span); - -impl TelemetrySpan { - /// Enters this span, returning a guard that will exit the span when dropped. - pub fn enter(&self) -> tracing::span::Entered { - self.0.enter() - } - - /// Constructs a new [`TelemetrySpan`]. - pub fn new() -> Self { - Self(tracing::error_span!(TELEMETRY_LOG_SPAN)) - } - - /// Return a clone of the underlying `tracing::Span` instance. - pub fn span(&self) -> tracing::Span { - self.0.clone() - } -} +pub(crate) type Id = u64; +pub(crate) type VerbosityLevel = u8; +pub(crate) type TelemetryPayload = serde_json::Map; +pub(crate) type TelemetryMessage = (Id, VerbosityLevel, TelemetryPayload); /// Message sent when the connection (re-)establishes. #[derive(Debug, Serialize)] @@ -129,21 +111,26 @@ pub struct TelemetryWorker { message_sender: mpsc::Sender, register_receiver: mpsc::UnboundedReceiver, register_sender: mpsc::UnboundedSender, + id_generator: IdGenerator, transport: WsTrans, } impl TelemetryWorker { - pub(crate) fn new(buffer_size: usize, transport: WsTrans) -> Self { + /// Instantiate a new [`TelemetryWorker`] which can run in background. Only one is needed for + /// the process. + pub fn new(buffer_size: usize, transport: Option) -> io::Result { + let transport = initialize_transport(transport)?; let (message_sender, message_receiver) = mpsc::channel(buffer_size); let (register_sender, register_receiver) = mpsc::unbounded(); - Self { + Ok(Self { message_receiver, message_sender, register_receiver, register_sender, + id_generator: IdGenerator::new(), transport, - } + }) } /// Get a new [`TelemetryHandle`]. @@ -151,15 +138,12 @@ impl TelemetryWorker { /// This is used when you want to register with the [`TelemetryWorker`]. pub fn handle(&self) -> TelemetryHandle { TelemetryHandle { - message_sender: self.register_sender.clone(), + message_sender: self.message_sender.clone(), + register_sender: self.register_sender.clone(), + id_generator: self.id_generator.clone(), } } - /// Get a clone of the channel's `Sender` used to send telemetry events. - pub(crate) fn message_sender(&self) -> mpsc::Sender { - self.message_sender.clone() - } - /// Run the telemetry worker. /// /// This should be run in a background task. @@ -169,10 +153,11 @@ impl TelemetryWorker { message_sender: _, mut register_receiver, register_sender: _, + id_generator: _, transport, } = self; - let mut node_map: HashMap> = HashMap::new(); + let mut node_map: HashMap> = HashMap::new(); let mut node_pool: HashMap = HashMap::new(); loop { @@ -195,7 +180,7 @@ impl TelemetryWorker { async fn process_register( input: Option, node_pool: &mut HashMap>, - node_map: &mut HashMap>, + node_map: &mut HashMap>, transport: WsTrans, ) { let input = input.expect("the stream is never closed; qed"); @@ -212,7 +197,7 @@ impl TelemetryWorker { Ok(serde_json::Value::Object(mut value)) => { value.insert("msg".into(), "system.connected".into()); let mut obj = serde_json::Map::new(); - obj.insert("id".to_string(), id.into_u64().into()); + obj.insert("id".to_string(), id.into()); obj.insert("payload".to_string(), value.into()); Some(obj) } @@ -271,7 +256,7 @@ impl TelemetryWorker { async fn process_message( input: Option, node_pool: &mut HashMap>, - node_map: &HashMap>, + node_map: &HashMap>, ) { let (id, verbosity, message) = input.expect("the stream is never closed; qed"); @@ -281,11 +266,17 @@ impl TelemetryWorker { // This is a normal error because the telemetry span is entered before the telemetry // is initialized so it is possible that some messages in the beginning don't get // through. + // TODO probably never happen log::trace!( target: "telemetry", "Received telemetry log for unknown id ({:?}): {}", id, - message, + serde_json::to_string(&message) + .unwrap_or_else(|err| format!( + "could not be serialized ({}): {:?}", + err, + message, + )), ); return; }; @@ -309,7 +300,12 @@ impl TelemetryWorker { "Received message for unknown node ({}). This is a bug. \ Message sent: {}", addr, - message, + serde_json::to_string(&message) + .unwrap_or_else(|err| format!( + "could not be serialized ({}): {:?}", + err, + message, + )), ); } } @@ -319,7 +315,9 @@ impl TelemetryWorker { /// Handle to the [`TelemetryWorker`] thats allows initializing the telemetry for a Substrate node. #[derive(Debug, Clone)] pub struct TelemetryHandle { - message_sender: mpsc::UnboundedSender, + message_sender: mpsc::Sender, + register_sender: mpsc::UnboundedSender, + id_generator: IdGenerator, } impl TelemetryHandle { @@ -335,40 +333,63 @@ impl TelemetryHandle { /// (re-)establishes. pub fn start_telemetry( &mut self, - span: TelemetrySpan, endpoints: TelemetryEndpoints, connection_message: ConnectionMessage, - ) -> TelemetryConnectionNotifier { - let Self { message_sender } = self; + ) -> Option { + let Self { + message_sender, + register_sender, + id_generator, + } = self; let connection_notifier = TelemetryConnectionNotifier { - message_sender: message_sender.clone(), + register_sender: register_sender.clone(), addresses: endpoints.0.iter().map(|(addr, _)| addr.clone()).collect(), }; - match span.0.id() { - Some(id) => { - match message_sender.unbounded_send(Register::Telemetry { - id, - endpoints, - connection_message, - }) { - Ok(()) => {} - Err(err) => error!( - target: "telemetry", - "Could not initialize telemetry: \ - the telemetry is probably already running: {}", - err, - ), - } - } - None => error!( + let id = id_generator.next()?; + match register_sender.unbounded_send(Register::Telemetry { + id, + endpoints, + connection_message, + }) { + Ok(()) => {} + Err(err) => error!( target: "telemetry", - "Could not initialize telemetry: the span could not be entered", + "Could not initialize telemetry: \ + the telemetry is probably already running: {}", + err, ), } - connection_notifier + Some(Telemetry { + message_sender: message_sender.clone(), + id, + connection_notifier, + }) + } +} + +/// A telemetry instance that can be used to send telemetries. +#[derive(Debug, Clone)] +pub struct Telemetry { + message_sender: mpsc::Sender, + id: Id, + connection_notifier: TelemetryConnectionNotifier, +} + +impl Telemetry { + /// Send telemetries. + pub async fn send(&mut self, verbosity: VerbosityLevel, payload: TelemetryPayload) { + match self + .message_sender + .send((self.id, verbosity, payload)) + .await + { + Ok(()) => {} + Err(err) if err.is_full() => todo!("overflow"), + Err(_) => unreachable!(), + } } } @@ -376,7 +397,7 @@ impl TelemetryHandle { /// (re-)establishes. #[derive(Clone, Debug)] pub struct TelemetryConnectionNotifier { - message_sender: mpsc::UnboundedSender, + register_sender: mpsc::UnboundedSender, addresses: Vec, } @@ -387,7 +408,7 @@ impl TelemetryConnectionNotifier { /// [`TelemetryHandle::start_telemetry`]. pub fn on_connect_stream(&self) -> TracingUnboundedReceiver<()> { let (message_sender, message_receiver) = tracing_unbounded("mpsc_telemetry_on_connect"); - if let Err(err) = self.message_sender.unbounded_send(Register::Notifier { + if let Err(err) = self.register_sender.unbounded_send(Register::Notifier { addresses: self.addresses.clone(), connection_notifier: message_sender, }) { @@ -436,8 +457,8 @@ enum Register { /// ``` #[macro_export(local_inner_macros)] macro_rules! telemetry { - ( $verbosity:expr; $msg:expr; $( $t:tt )* ) => {{ - let verbosity: u8 = $verbosity; + ( $telemetry:expr, $verbosity:expr; $msg:expr; $( $t:tt )* ) => {{ + let verbosity: VerbosityLevel = $verbosity; match format_fields_to_json!($($t)*) { Err(err) => { $crate::tracing::error!( @@ -447,14 +468,8 @@ macro_rules! telemetry { ); }, Ok(mut json) => { - // NOTE: the span id will be added later in the JSON for the greater good json.insert("msg".into(), $msg.into()); - let serialized_json = $crate::serde_json::to_string(&json) - .expect("contains only string keys; qed"); - $crate::tracing::info!(target: $crate::TELEMETRY_LOG_SPAN, - verbosity, - json = serialized_json.as_str(), - ); + $telemetry.send(verbosity, json); }, } }}; @@ -495,3 +510,19 @@ macro_rules! format_fields_to_json { )* }}; } + +#[derive(Debug, Clone)] +struct IdGenerator(Arc Option>>>>); + +impl IdGenerator { + fn new() -> Self { + Self(Arc::new(Mutex::new(iter::successors( + Some(1), + Box::new(|n| n.checked_add(1)), + )))) + } + + fn next(&mut self) -> Option { + self.0.lock().next() + } +} diff --git a/client/telemetry/src/node.rs b/client/telemetry/src/node.rs index e47bc2f9634f7..a9c1d71889b0c 100644 --- a/client/telemetry/src/node.rs +++ b/client/telemetry/src/node.rs @@ -16,6 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use crate::TelemetryPayload; use futures::prelude::*; use libp2p::core::transport::Transport; use libp2p::Multiaddr; @@ -45,7 +46,7 @@ pub(crate) struct Node { /// Transport used to establish new connections. transport: TTrans, /// Messages that are sent when the connection (re-)establishes. - pub(crate) connection_messages: Vec>, + pub(crate) connection_messages: Vec, /// Notifier for when the connection (re-)establishes. pub(crate) telemetry_connection_notifier: Vec, } @@ -123,7 +124,7 @@ where pub(crate) enum Infallible {} -impl Sink for Node +impl Sink for Node where TTrans: Clone + Unpin, TTrans::Dial: Unpin, @@ -234,16 +235,28 @@ where Poll::Ready(Ok(())) } - fn start_send(mut self: Pin<&mut Self>, item: String) -> Result<(), Self::Error> { + fn start_send(mut self: Pin<&mut Self>, item: TelemetryPayload) -> Result<(), Self::Error> { match &mut self.socket { - NodeSocket::Connected(conn) => { - let _ = conn.sink.start_send_unpin(item.into()).expect("boo"); - } + NodeSocket::Connected(conn) => match serde_json::to_vec(&item) { + Ok(data) => { + let _ = conn.sink.start_send_unpin(data); + } + Err(err) => log::error!( + target: "telemetry", + "Could not serialize payload: {}", + err, + ), + }, _socket => { log::trace!( target: "telemetry", "Message has been discarded: {}", - item, + serde_json::to_string(&item) + .unwrap_or_else(|err| format!( + "could not be serialized ({}): {:?}", + err, + item, + )), ); } } From 7d941226fa3f026d3bf6f92de7132e9f78f1a165 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 11 Feb 2021 07:57:18 +0100 Subject: [PATCH 02/92] Issue with telemetry usage in ProfilingLayer --- client/telemetry/src/lib.rs | 8 +++++--- client/tracing/src/lib.rs | 16 ++++++++++------ client/tracing/src/logging/mod.rs | 6 +++++- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index a4b0d6d77174d..1859a7cf91907 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -72,8 +72,10 @@ pub const CONSENSUS_WARN: VerbosityLevel = 4; /// Consensus INFO log level. pub const CONSENSUS_INFO: VerbosityLevel = 1; +/// Telemetry message verbosity. +pub type VerbosityLevel = u8; + pub(crate) type Id = u64; -pub(crate) type VerbosityLevel = u8; pub(crate) type TelemetryPayload = serde_json::Map; pub(crate) type TelemetryMessage = (Id, VerbosityLevel, TelemetryPayload); @@ -457,8 +459,8 @@ enum Register { /// ``` #[macro_export(local_inner_macros)] macro_rules! telemetry { - ( $telemetry:expr, $verbosity:expr; $msg:expr; $( $t:tt )* ) => {{ - let verbosity: VerbosityLevel = $verbosity; + ( $telemetry:expr; $verbosity:expr; $msg:expr; $( $t:tt )* ) => {{ + let verbosity: $crate::VerbosityLevel = $verbosity; match format_fields_to_json!($($t)*) { Err(err) => { $crate::tracing::error!( diff --git a/client/tracing/src/lib.rs b/client/tracing/src/lib.rs index 2b0044a6f25b0..59c433062fd16 100644 --- a/client/tracing/src/lib.rs +++ b/client/tracing/src/lib.rs @@ -46,7 +46,7 @@ use tracing_subscriber::{ CurrentSpan, layer::{Layer, Context}, }; -use sc_telemetry::{telemetry, SUBSTRATE_INFO}; +use sc_telemetry::{telemetry, SUBSTRATE_INFO, Telemetry}; use sp_tracing::{WASM_NAME_KEY, WASM_TARGET_KEY, WASM_TRACE_IDENTIFIER}; #[doc(hidden)] @@ -211,11 +211,13 @@ impl ProfilingLayer { /// either with a level: "pallet=trace,frame=debug" /// or without: "pallet,frame" in which case the level defaults to `trace`. /// wasm_tracing indicates whether to enable wasm traces - pub fn new(receiver: TracingReceiver, targets: &str) -> Self { + pub fn new(receiver: TracingReceiver, targets: &str, telemetry: Telemetry) -> Self { match receiver { TracingReceiver::Log => Self::new_with_handler(Box::new(LogTraceHandler), targets), TracingReceiver::Telemetry => Self::new_with_handler( - Box::new(TelemetryTraceHandler), + Box::new(TelemetryTraceHandler { + telemetry, + }), targets, ), } @@ -395,11 +397,13 @@ impl TraceHandler for LogTraceHandler { /// TraceHandler for sending span data to telemetry, /// Please see telemetry documentation for details on how to specify endpoints and /// set the required telemetry level to activate tracing messages -pub struct TelemetryTraceHandler; +pub struct TelemetryTraceHandler { + telemetry: Telemetry, +} impl TraceHandler for TelemetryTraceHandler { fn handle_span(&self, span_datum: SpanDatum) { - telemetry!(SUBSTRATE_INFO; "tracing.profiling"; + telemetry!(self.telemetry; SUBSTRATE_INFO; "tracing.profiling"; "name" => span_datum.name, "target" => span_datum.target, "time" => span_datum.overall_time.as_nanos(), @@ -410,7 +414,7 @@ impl TraceHandler for TelemetryTraceHandler { } fn handle_event(&self, event: TraceEvent) { - telemetry!(SUBSTRATE_INFO; "tracing.event"; + telemetry!(self.telemetry; SUBSTRATE_INFO; "tracing.event"; "name" => event.name, "target" => event.target, "parent_id" => event.parent_id.as_ref().map(|i| i.into_u64()), diff --git a/client/tracing/src/logging/mod.rs b/client/tracing/src/logging/mod.rs index 5674b50cb98e2..15ad10a372615 100644 --- a/client/tracing/src/logging/mod.rs +++ b/client/tracing/src/logging/mod.rs @@ -280,7 +280,11 @@ impl LoggerBuilder { self.telemetry_external_transport, |builder| builder, )?; - let profiling = crate::ProfilingLayer::new(tracing_receiver, &profiling_targets); + let profiling = crate::ProfilingLayer::new( + tracing_receiver, + &profiling_targets, + telemetry, // TODO... for... what node?? + ); tracing::subscriber::set_global_default(subscriber.with(profiling))?; From 9aa9a6ae7a3c16282b4656cb674ab57c0494ecaf Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 11 Feb 2021 12:27:17 +0100 Subject: [PATCH 03/92] WIP --- client/telemetry/src/endpoints.rs | 3 +- client/tracing/src/lib.rs | 40 +--------------------- client/tracing/src/logging/event_format.rs | 4 --- client/tracing/src/logging/mod.rs | 20 +++++------ 4 files changed, 12 insertions(+), 55 deletions(-) diff --git a/client/telemetry/src/endpoints.rs b/client/telemetry/src/endpoints.rs index fe4fa23974a64..7d0338fb18e3c 100644 --- a/client/telemetry/src/endpoints.rs +++ b/client/telemetry/src/endpoints.rs @@ -25,8 +25,7 @@ use serde::{Deserialize, Deserializer, Serialize}; /// The URL string can be either a URL or a multiaddress. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] pub struct TelemetryEndpoints( - #[serde(deserialize_with = "url_or_multiaddr_deser")] - pub(crate) Vec<(Multiaddr, u8)>, + #[serde(deserialize_with = "url_or_multiaddr_deser")] pub(crate) Vec<(Multiaddr, u8)>, ); /// Custom deserializer for TelemetryEndpoints, used to convert urls or multiaddr to multiaddr. diff --git a/client/tracing/src/lib.rs b/client/tracing/src/lib.rs index 59c433062fd16..5f30c4d5ffcd1 100644 --- a/client/tracing/src/lib.rs +++ b/client/tracing/src/lib.rs @@ -46,7 +46,6 @@ use tracing_subscriber::{ CurrentSpan, layer::{Layer, Context}, }; -use sc_telemetry::{telemetry, SUBSTRATE_INFO, Telemetry}; use sp_tracing::{WASM_NAME_KEY, WASM_TARGET_KEY, WASM_TRACE_IDENTIFIER}; #[doc(hidden)] @@ -67,8 +66,6 @@ pub struct ProfilingLayer { pub enum TracingReceiver { /// Output to logger Log, - /// Output to telemetry - Telemetry, } impl Default for TracingReceiver { @@ -211,15 +208,9 @@ impl ProfilingLayer { /// either with a level: "pallet=trace,frame=debug" /// or without: "pallet,frame" in which case the level defaults to `trace`. /// wasm_tracing indicates whether to enable wasm traces - pub fn new(receiver: TracingReceiver, targets: &str, telemetry: Telemetry) -> Self { + pub fn new(receiver: TracingReceiver, targets: &str) -> Self { match receiver { TracingReceiver::Log => Self::new_with_handler(Box::new(LogTraceHandler), targets), - TracingReceiver::Telemetry => Self::new_with_handler( - Box::new(TelemetryTraceHandler { - telemetry, - }), - targets, - ), } } @@ -394,35 +385,6 @@ impl TraceHandler for LogTraceHandler { } } -/// TraceHandler for sending span data to telemetry, -/// Please see telemetry documentation for details on how to specify endpoints and -/// set the required telemetry level to activate tracing messages -pub struct TelemetryTraceHandler { - telemetry: Telemetry, -} - -impl TraceHandler for TelemetryTraceHandler { - fn handle_span(&self, span_datum: SpanDatum) { - telemetry!(self.telemetry; SUBSTRATE_INFO; "tracing.profiling"; - "name" => span_datum.name, - "target" => span_datum.target, - "time" => span_datum.overall_time.as_nanos(), - "id" => span_datum.id.into_u64(), - "parent_id" => span_datum.parent_id.as_ref().map(|i| i.into_u64()), - "values" => span_datum.values - ); - } - - fn handle_event(&self, event: TraceEvent) { - telemetry!(self.telemetry; SUBSTRATE_INFO; "tracing.event"; - "name" => event.name, - "target" => event.target, - "parent_id" => event.parent_id.as_ref().map(|i| i.into_u64()), - "values" => event.values - ); - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/client/tracing/src/logging/event_format.rs b/client/tracing/src/logging/event_format.rs index 37f9ed16ead7d..25fd2f3ba3d70 100644 --- a/client/tracing/src/logging/event_format.rs +++ b/client/tracing/src/logging/event_format.rs @@ -62,10 +62,6 @@ where S: Subscriber + for<'a> LookupSpan<'a>, N: for<'a> FormatFields<'a> + 'static, { - if event.metadata().target() == sc_telemetry::TELEMETRY_LOG_SPAN { - return Ok(()); - } - let writer = &mut MaybeColorWriter::new(self.enable_color, writer); let normalized_meta = event.normalized_metadata(); let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); diff --git a/client/tracing/src/logging/mod.rs b/client/tracing/src/logging/mod.rs index 15ad10a372615..3d3bb2819c996 100644 --- a/client/tracing/src/logging/mod.rs +++ b/client/tracing/src/logging/mod.rs @@ -33,12 +33,13 @@ use sc_telemetry::{ExtTransport, TelemetryWorker}; use std::io; use tracing::Subscriber; use tracing_subscriber::{ + filter::LevelFilter, fmt::time::ChronoLocal, fmt::{ format, FormatEvent, FormatFields, Formatter, Layer as FmtLayer, MakeWriter, SubscriberBuilder, }, - layer::{self, SubscriberExt}, filter::LevelFilter, + layer::{self, SubscriberExt}, registry::LookupSpan, EnvFilter, FmtSubscriber, Layer, Registry, }; @@ -164,8 +165,10 @@ where "%Y-%m-%d %H:%M:%S%.3f".to_string() }); - let (telemetry_layer, telemetry_worker) = - sc_telemetry::TelemetryLayer::new(telemetry_buffer_size, telemetry_external_transport)?; + let telemetry_worker = sc_telemetry::TelemetryWorker::new( + telemetry_buffer_size.unwrap_or(16), + telemetry_external_transport, + )?; let event_format = EventFormat { timer, display_target: !simple, @@ -187,7 +190,7 @@ where #[cfg(not(target_os = "unknown"))] let builder = builder_hook(builder); - let subscriber = builder.finish().with(PrefixLayer).with(telemetry_layer); + let subscriber = builder.finish().with(PrefixLayer); #[cfg(target_os = "unknown")] let subscriber = subscriber.with(ConsoleLogLayer::new(event_format)); @@ -280,11 +283,7 @@ impl LoggerBuilder { self.telemetry_external_transport, |builder| builder, )?; - let profiling = crate::ProfilingLayer::new( - tracing_receiver, - &profiling_targets, - telemetry, // TODO... for... what node?? - ); + let profiling = crate::ProfilingLayer::new(tracing_receiver, &profiling_targets); tracing::subscriber::set_global_default(subscriber.with(profiling))?; @@ -339,7 +338,8 @@ mod tests { #[test] fn test_logger_filters() { if env::var("RUN_TEST_LOGGER_FILTERS").is_ok() { - let test_directives = "afg=debug,sync=trace,client=warn,telemetry,something-with-dash=error"; + let test_directives = + "afg=debug,sync=trace,client=warn,telemetry,something-with-dash=error"; init_logger(&test_directives); tracing::dispatcher::get_default(|dispatcher| { From af2c532fac91fa64febf09c4c29cb0a012fe1e32 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 11 Feb 2021 15:19:55 +0100 Subject: [PATCH 04/92] WIP --- client/finality-grandpa/src/authorities.rs | 39 ++- client/finality-grandpa/src/aux_schema.rs | 2 +- .../src/communication/gossip.rs | 24 +- .../finality-grandpa/src/communication/mod.rs | 164 +++++++----- client/finality-grandpa/src/environment.rs | 253 ++++++++++-------- client/finality-grandpa/src/import.rs | 21 +- client/finality-grandpa/src/lib.rs | 72 +++-- client/finality-grandpa/src/observer.rs | 10 + client/telemetry/src/lib.rs | 50 ++-- 9 files changed, 381 insertions(+), 254 deletions(-) diff --git a/client/finality-grandpa/src/authorities.rs b/client/finality-grandpa/src/authorities.rs index 067f6dfc1ae65..2273917661608 100644 --- a/client/finality-grandpa/src/authorities.rs +++ b/client/finality-grandpa/src/authorities.rs @@ -23,7 +23,7 @@ use parking_lot::RwLock; use finality_grandpa::voter_set::VoterSet; use parity_scale_codec::{Encode, Decode}; use log::debug; -use sc_telemetry::{telemetry, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, Telemetry, CONSENSUS_INFO}; use sp_finality_grandpa::{AuthorityId, AuthorityList}; use std::cmp::Ord; @@ -43,8 +43,8 @@ pub enum Error { #[display(fmt = "Multiple pending forced authority set changes are not allowed.")] MultiplePendingForcedAuthoritySetChanges, #[display( - fmt = "A pending forced authority set change could not be applied since it must be applied after \ - the pending standard change at #{}", + fmt = "A pending forced authority set change could not be applied since it must be applied \ + after the pending standard change at #{}", _0 )] ForcedAuthoritySetChangeDependencyUnsatisfied(N), @@ -278,9 +278,13 @@ where let hash = pending.canon_hash.clone(); let number = pending.canon_height.clone(); - debug!(target: "afg", "Inserting potential standard set change signaled at block {:?} \ - (delayed by {:?} blocks).", - (&number, &hash), pending.delay); + debug!( + target: "afg", + "Inserting potential standard set change signaled at block {:?} (delayed by {:?} + blocks).", + (&number, &hash), + pending.delay, + ); self.pending_standard_changes.import( hash, @@ -289,8 +293,10 @@ where is_descendent_of, )?; - debug!(target: "afg", "There are now {} alternatives for the next pending standard change (roots), \ - and a total of {} pending standard changes (across all forks).", + debug!( + target: "afg", + "There are now {} alternatives for the next pending standard change (roots), and a + total of {} pending standard changes (across all forks).", self.pending_standard_changes.roots().count(), self.pending_standard_changes.iter().count(), ); @@ -326,9 +332,12 @@ where )) .unwrap_or_else(|i| i); - debug!(target: "afg", "Inserting potential forced set change at block {:?} \ - (delayed by {:?} blocks).", - (&pending.canon_height, &pending.canon_hash), pending.delay); + debug!( + target: "afg", + "Inserting potential forced set change at block {:?} (delayed by {:?} blocks).", + (&pending.canon_height, &pending.canon_hash), + pending.delay, + ); self.pending_forced_changes.insert(idx, pending); @@ -409,6 +418,7 @@ where best_number: N, is_descendent_of: &F, initial_sync: bool, + mut telemetry: Option<&mut Telemetry>, ) -> Result, Error> where F: Fn(&H, &H) -> Result, @@ -461,8 +471,7 @@ where ); telemetry!( - CONSENSUS_INFO; - "afg.applying_forced_authority_set_change"; + telemetry; CONSENSUS_INFO; "afg.applying_forced_authority_set_change"; "block" => ?change.canon_height ); @@ -505,6 +514,7 @@ where finalized_number: N, is_descendent_of: &F, initial_sync: bool, + mut telemetry: Option<&mut Telemetry>, ) -> Result, Error> where F: Fn(&H, &H) -> Result, @@ -544,7 +554,8 @@ where "👴 Applying authority set change scheduled at block #{:?}", change.canon_height, ); - telemetry!(CONSENSUS_INFO; "afg.applying_scheduled_authority_set_change"; + telemetry!( + telemetry; CONSENSUS_INFO; "afg.applying_scheduled_authority_set_change"; "block" => ?change.canon_height ); diff --git a/client/finality-grandpa/src/aux_schema.rs b/client/finality-grandpa/src/aux_schema.rs index 1ce3c7999f24c..43c45b9f10ae1 100644 --- a/client/finality-grandpa/src/aux_schema.rs +++ b/client/finality-grandpa/src/aux_schema.rs @@ -137,7 +137,7 @@ struct V2AuthoritySet { } pub(crate) fn load_decode( - backend: &B, + backend: &B, key: &[u8] ) -> ClientResult> { match backend.get_aux(key)? { diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index 9f5582e5cea6b..824ec8c88c9df 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -90,7 +90,7 @@ use sc_network::{ObservedRole, PeerId, ReputationChange}; use parity_scale_codec::{Encode, Decode}; use sp_finality_grandpa::AuthorityId; -use sc_telemetry::{telemetry, CONSENSUS_DEBUG}; +use sc_telemetry::{telemetry, Telemetry, CONSENSUS_DEBUG}; use log::{trace, debug}; use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; use prometheus_endpoint::{CounterVec, Opts, PrometheusError, register, Registry, U64}; @@ -744,7 +744,7 @@ impl Inner { fn note_set(&mut self, set_id: SetId, authorities: Vec) -> MaybeMessage { { let local_view = match self.local_view { - ref mut x @ None => x.get_or_insert(LocalView::new( + ref mut x @ None => x.get_or_insert(LocalView::new( set_id, Round(1), )), @@ -828,7 +828,10 @@ impl Inner { // ensure authority is part of the set. if !self.authorities.contains(&full.message.id) { debug!(target: "afg", "Message from unknown voter: {}", full.message.id); - telemetry!(CONSENSUS_DEBUG; "afg.bad_msg_signature"; "signature" => ?full.message.id); + telemetry!( + self.config.telemetry.clone(); CONSENSUS_DEBUG; "afg.bad_msg_signature"; + "signature" => ?full.message.id, + ); return Action::Discard(cost::UNKNOWN_VOTER); } @@ -840,7 +843,10 @@ impl Inner { full.set_id.0, ) { debug!(target: "afg", "Bad message signature {}", full.message.id); - telemetry!(CONSENSUS_DEBUG; "afg.bad_msg_signature"; "signature" => ?full.message.id); + telemetry!( + self.config.telemetry.clone(); CONSENSUS_DEBUG; "afg.bad_msg_signature"; + "signature" => ?full.message.id, + ); return Action::Discard(cost::BAD_SIGNATURE); } @@ -866,7 +872,7 @@ impl Inner { if full.message.precommits.len() != full.message.auth_data.len() || full.message.precommits.is_empty() { debug!(target: "afg", "Malformed compact commit"); - telemetry!(CONSENSUS_DEBUG; "afg.malformed_compact_commit"; + telemetry!(self.config.telemetry; CONSENSUS_DEBUG; "afg.malformed_compact_commit"; "precommits_len" => ?full.message.precommits.len(), "auth_data_len" => ?full.message.auth_data.len(), "precommits_is_empty" => ?full.message.precommits.is_empty(), @@ -1277,6 +1283,7 @@ pub(super) struct GossipValidator { set_state: environment::SharedVoterSetState, report_sender: TracingUnboundedSender, metrics: Option, + telemetry: Option, } impl GossipValidator { @@ -1287,6 +1294,7 @@ impl GossipValidator { config: crate::Config, set_state: environment::SharedVoterSetState, prometheus_registry: Option<&Registry>, + telemetry: Option, ) -> (GossipValidator, TracingUnboundedReceiver) { let metrics = match prometheus_registry.map(Metrics::register) { Some(Ok(metrics)) => Some(metrics), @@ -1303,6 +1311,7 @@ impl GossipValidator { set_state, report_sender: tx, metrics, + telemetry, }; (val, rx) @@ -1411,7 +1420,10 @@ impl GossipValidator { Err(e) => { message_name = None; debug!(target: "afg", "Error decoding message: {}", e); - telemetry!(CONSENSUS_DEBUG; "afg.err_decoding_msg"; "" => ""); + telemetry!( + self.telemetry.clone(); CONSENSUS_DEBUG; "afg.err_decoding_msg"; + "" => "", + ); let len = std::cmp::min(i32::max_value() as usize, data.len()) as i32; Action::Discard(Misbehavior::UndecodablePacket(len).cost()) diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index d502741465d23..cea4a7405a775 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -42,7 +42,7 @@ use sc_network::{NetworkService, ReputationChange}; use sc_network_gossip::{GossipEngine, Network as GossipNetwork}; use parity_scale_codec::{Encode, Decode}; use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT, NumberFor}; -use sc_telemetry::{telemetry, CONSENSUS_DEBUG, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, Telemetry, CONSENSUS_DEBUG, CONSENSUS_INFO}; use crate::{ CatchUp, Commit, CommunicationIn, CommunicationOutH, @@ -192,6 +192,8 @@ pub(crate) struct NetworkBridge> { // just an `UnboundedReceiver`, one could also switch to a multi-producer-*multi*-consumer // channel implementation. gossip_validator_report_stream: Arc>>, + + telemetry: Option, } impl> Unpin for NetworkBridge {} @@ -206,11 +208,13 @@ impl> NetworkBridge { config: crate::Config, set_state: crate::environment::SharedVoterSetState, prometheus_registry: Option<&Registry>, + telemetry: Option, ) -> Self { let (validator, report_stream) = GossipValidator::new( config, set_state.clone(), prometheus_registry, + telemetry.clone(), ); let validator = Arc::new(validator); @@ -268,6 +272,7 @@ impl> NetworkBridge { neighbor_sender: neighbor_packet_sender, neighbor_packet_worker: Arc::new(Mutex::new(neighbor_packet_worker)), gossip_validator_report_stream: Arc::new(Mutex::new(report_stream)), + telemetry, } } @@ -320,6 +325,7 @@ impl> NetworkBridge { }); let topic = round_topic::(round.0, set_id.0); + let mut telemetry = self.telemetry.clone(); let incoming = self.gossip_engine.lock().messages_for(topic) .filter_map(move |notification| { let decoded = GossipMessage::::decode(&mut ¬ification.message[..]); @@ -339,21 +345,21 @@ impl> NetworkBridge { if voters.len().get() <= TELEMETRY_VOTERS_LIMIT { match &msg.message.message { PrimaryPropose(propose) => { - telemetry!(CONSENSUS_INFO; "afg.received_propose"; + telemetry!(telemetry; CONSENSUS_INFO; "afg.received_propose"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?propose.target_number, "target_hash" => ?propose.target_hash, ); }, Prevote(prevote) => { - telemetry!(CONSENSUS_INFO; "afg.received_prevote"; + telemetry!(telemetry; CONSENSUS_INFO; "afg.received_prevote"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?prevote.target_number, "target_hash" => ?prevote.target_hash, ); }, Precommit(precommit) => { - telemetry!(CONSENSUS_INFO; "afg.received_precommit"; + telemetry!(telemetry; CONSENSUS_INFO; "afg.received_precommit"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?precommit.target_number, "target_hash" => ?precommit.target_hash, @@ -379,6 +385,7 @@ impl> NetworkBridge { network: self.gossip_engine.clone(), sender: tx, has_voted, + telemetry: self.telemetry.clone(), }; // Combine incoming votes from external GRANDPA nodes with outgoing @@ -412,6 +419,7 @@ impl> NetworkBridge { voters, self.validator.clone(), self.neighbor_sender.clone(), + self.telemetry.clone(), ); let outgoing = CommitsOut::::new( @@ -420,6 +428,7 @@ impl> NetworkBridge { is_voter, self.validator.clone(), self.neighbor_sender.clone(), + self.telemetry.clone(), ); let outgoing = outgoing.with(|out| { @@ -491,72 +500,77 @@ fn incoming_global( voters: Arc>, gossip_validator: Arc>, neighbor_sender: periodic::NeighborPacketSender, + telemetry: Option, ) -> impl Stream> { - let process_commit = move | - msg: FullCommitMessage, - mut notification: sc_network_gossip::TopicNotification, - gossip_engine: &Arc>>, - gossip_validator: &Arc>, - voters: &VoterSet, - | { - if voters.len().get() <= TELEMETRY_VOTERS_LIMIT { - let precommits_signed_by: Vec = - msg.message.auth_data.iter().map(move |(_, a)| { - format!("{}", a) - }).collect(); - - telemetry!(CONSENSUS_INFO; "afg.received_commit"; - "contains_precommits_signed_by" => ?precommits_signed_by, - "target_number" => ?msg.message.target_number.clone(), - "target_hash" => ?msg.message.target_hash.clone(), - ); - } - - if let Err(cost) = check_compact_commit::( - &msg.message, - voters, - msg.round, - msg.set_id, - ) { - if let Some(who) = notification.sender { - gossip_engine.lock().report(who, cost); + let mut process_commit = { + let mut telemetry = telemetry.clone(); + move | + msg: FullCommitMessage, + mut notification: sc_network_gossip::TopicNotification, + gossip_engine: &Arc>>, + gossip_validator: &Arc>, + voters: &VoterSet, + | { + if voters.len().get() <= TELEMETRY_VOTERS_LIMIT { + let precommits_signed_by: Vec = + msg.message.auth_data.iter().map(move |(_, a)| { + format!("{}", a) + }).collect(); + + telemetry!(telemetry; CONSENSUS_INFO; "afg.received_commit"; + "contains_precommits_signed_by" => ?precommits_signed_by, + "target_number" => ?msg.message.target_number.clone(), + "target_hash" => ?msg.message.target_hash.clone(), + ); } - return None; - } - - let round = msg.round; - let set_id = msg.set_id; - let commit = msg.message; - let finalized_number = commit.target_number; - let gossip_validator = gossip_validator.clone(); - let gossip_engine = gossip_engine.clone(); - let neighbor_sender = neighbor_sender.clone(); - let cb = move |outcome| match outcome { - voter::CommitProcessingOutcome::Good(_) => { - // if it checks out, gossip it. not accounting for - // any discrepancy between the actual ghost and the claimed - // finalized number. - gossip_validator.note_commit_finalized( - round, - set_id, - finalized_number, - |to, neighbor| neighbor_sender.send(to, neighbor), - ); + if let Err(cost) = check_compact_commit::( + &msg.message, + voters, + msg.round, + msg.set_id, + telemetry.as_mut(), + ) { + if let Some(who) = notification.sender { + gossip_engine.lock().report(who, cost); + } - gossip_engine.lock().gossip_message(topic, notification.message.clone(), false); + return None; } - voter::CommitProcessingOutcome::Bad(_) => { - // report peer and do not gossip. - if let Some(who) = notification.sender.take() { - gossip_engine.lock().report(who, cost::INVALID_COMMIT); + + let round = msg.round; + let set_id = msg.set_id; + let commit = msg.message; + let finalized_number = commit.target_number; + let gossip_validator = gossip_validator.clone(); + let gossip_engine = gossip_engine.clone(); + let neighbor_sender = neighbor_sender.clone(); + let cb = move |outcome| match outcome { + voter::CommitProcessingOutcome::Good(_) => { + // if it checks out, gossip it. not accounting for + // any discrepancy between the actual ghost and the claimed + // finalized number. + gossip_validator.note_commit_finalized( + round, + set_id, + finalized_number, + |to, neighbor| neighbor_sender.send(to, neighbor), + ); + + gossip_engine.lock().gossip_message(topic, notification.message.clone(), false); } - } - }; + voter::CommitProcessingOutcome::Bad(_) => { + // report peer and do not gossip. + if let Some(who) = notification.sender.take() { + gossip_engine.lock().report(who, cost::INVALID_COMMIT); + } + } + }; - let cb = voter::Callback::Work(Box::new(cb)); + let cb = voter::Callback::Work(Box::new(cb)); - Some(voter::CommunicationIn::Commit(round.0, commit, cb)) + Some(voter::CommunicationIn::Commit(round.0, commit, cb)) + } }; let process_catch_up = move | @@ -573,6 +587,7 @@ fn incoming_global( &msg.message, voters, msg.set_id, + telemetry.clone(), ) { if let Some(who) = notification.sender { gossip_engine.lock().report(who, cost); @@ -629,6 +644,7 @@ impl> Clone for NetworkBridge { neighbor_sender: self.neighbor_sender.clone(), neighbor_packet_worker: self.neighbor_packet_worker.clone(), gossip_validator_report_stream: self.gossip_validator_report_stream.clone(), + telemetry: self.telemetry.clone(), } } } @@ -655,6 +671,7 @@ pub(crate) struct OutgoingMessages { sender: mpsc::Sender>, network: Arc>>, has_voted: HasVoted, + telemetry: Option, } impl Unpin for OutgoingMessages {} @@ -717,7 +734,7 @@ impl Sink> for OutgoingMessages ); telemetry!( - CONSENSUS_DEBUG; "afg.announcing_blocks_to_voted_peers"; + self.telemetry.clone(); CONSENSUS_DEBUG; "afg.announcing_blocks_to_voted_peers"; "block" => ?target_hash, "round" => ?self.round, "set_id" => ?self.set_id, ); @@ -756,6 +773,7 @@ fn check_compact_commit( voters: &VoterSet, round: Round, set_id: SetId, + mut telemetry: Option<&mut Telemetry>, ) -> Result<(), ReputationChange> { // 4f + 1 = equivocations from f voters. let f = voters.total_weight() - voters.threshold(); @@ -797,7 +815,10 @@ fn check_compact_commit( &mut buf, ) { debug!(target: "afg", "Bad commit message signature {}", id); - telemetry!(CONSENSUS_DEBUG; "afg.bad_commit_msg_signature"; "id" => ?id); + telemetry!( + telemetry; CONSENSUS_DEBUG; "afg.bad_commit_msg_signature"; + "id" => ?id, + ); let cost = Misbehavior::BadCommitMessage { signatures_checked: i as i32, blocks_loaded: 0, @@ -817,6 +838,7 @@ fn check_catch_up( msg: &CatchUp, voters: &VoterSet, set_id: SetId, + telemetry: Option, ) -> Result<(), ReputationChange> { // 4f + 1 = equivocations from f voters. let f = voters.total_weight() - voters.threshold(); @@ -867,6 +889,7 @@ fn check_catch_up( set_id: SetIdNumber, mut signatures_checked: usize, buf: &mut Vec, + mut telemetry: Option, ) -> Result where B: BlockT, I: Iterator, &'a AuthorityId, &'a AuthoritySignature)>, @@ -885,7 +908,7 @@ fn check_catch_up( buf, ) { debug!(target: "afg", "Bad catch up message signature {}", id); - telemetry!(CONSENSUS_DEBUG; "afg.bad_catch_up_msg_signature"; "id" => ?id); + telemetry!(telemetry; CONSENSUS_DEBUG; "afg.bad_catch_up_msg_signature"; "id" => ?id); let cost = Misbehavior::BadCatchUpMessage { signatures_checked: signatures_checked as i32, @@ -909,6 +932,7 @@ fn check_catch_up( set_id.0, 0, &mut buf, + telemetry.clone(), )?; // check signatures on all contained precommits. @@ -920,6 +944,7 @@ fn check_catch_up( set_id.0, signatures_checked, &mut buf, + telemetry, )?; Ok(()) @@ -932,6 +957,7 @@ struct CommitsOut { is_voter: bool, gossip_validator: Arc>, neighbor_sender: periodic::NeighborPacketSender, + telemetry: Option, } impl CommitsOut { @@ -942,6 +968,7 @@ impl CommitsOut { is_voter: bool, gossip_validator: Arc>, neighbor_sender: periodic::NeighborPacketSender, + telemetry: Option, ) -> Self { CommitsOut { network, @@ -949,6 +976,7 @@ impl CommitsOut { is_voter, gossip_validator, neighbor_sender, + telemetry, } } } @@ -968,8 +996,10 @@ impl Sink<(RoundNumber, Commit)> for CommitsOut { let (round, commit) = input; let round = Round(round); - telemetry!(CONSENSUS_DEBUG; "afg.commit_issued"; - "target_number" => ?commit.target_number, "target_hash" => ?commit.target_hash, + telemetry!( + self.telemetry.clone(); CONSENSUS_DEBUG; "afg.commit_issued"; + "target_number" => ?commit.target_number, + "target_hash" => ?commit.target_hash, ); let (precommits, auth_data) = commit.precommits.into_iter() .map(|signed| (signed.precommit, (signed.signature, signed.id))) diff --git a/client/finality-grandpa/src/environment.rs b/client/finality-grandpa/src/environment.rs index 7925a674c2983..c643872ae336a 100644 --- a/client/finality-grandpa/src/environment.rs +++ b/client/finality-grandpa/src/environment.rs @@ -39,7 +39,7 @@ use sp_runtime::generic::BlockId; use sp_runtime::traits::{ Block as BlockT, Header as HeaderT, NumberFor, Zero, }; -use sc_telemetry::{telemetry, CONSENSUS_DEBUG, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, Telemetry, CONSENSUS_DEBUG, CONSENSUS_INFO}; use crate::{ local_authority_id, CommandOrError, Commit, Config, Error, NewAuthoritySet, Precommit, Prevote, @@ -445,6 +445,7 @@ pub(crate) struct Environment, SC, pub(crate) voting_rule: VR, pub(crate) metrics: Option, pub(crate) justification_sender: Option>, + pub(crate) telemetry: Option, pub(crate) _phantom: PhantomData, } @@ -890,8 +891,9 @@ where None => return Ok(()), }; - let report_prevote_metrics = |prevote: &Prevote| { - telemetry!(CONSENSUS_DEBUG; "afg.prevote_issued"; + let mut telemetry = self.telemetry.clone(); + let mut report_prevote_metrics = move |prevote: &Prevote| { + telemetry!(telemetry; CONSENSUS_DEBUG; "afg.prevote_issued"; "round" => round, "target_number" => ?prevote.target_number, "target_hash" => ?prevote.target_hash, @@ -949,8 +951,9 @@ where None => return Ok(()), }; - let report_precommit_metrics = |precommit: &Precommit| { - telemetry!(CONSENSUS_DEBUG; "afg.precommit_issued"; + let mut telemetry = self.telemetry.clone(); + let mut report_precommit_metrics = move |precommit: &Precommit| { + telemetry!(telemetry; CONSENSUS_DEBUG; "afg.precommit_issued"; "round" => round, "target_number" => ?precommit.target_number, "target_hash" => ?precommit.target_hash, @@ -1146,6 +1149,7 @@ where (round, commit).into(), false, self.justification_sender.as_ref(), + self.telemetry.clone(), ) } @@ -1210,6 +1214,7 @@ pub(crate) fn finalize_block( justification_or_commit: JustificationOrCommit, initial_sync: bool, justification_sender: Option<&GrandpaJustificationSender>, + telemetry: Option, ) -> Result<(), CommandOrError>> where Block: BlockT, @@ -1239,138 +1244,148 @@ where // FIXME #1483: clone only when changed let old_authority_set = authority_set.clone(); - let update_res: Result<_, Error> = client.lock_import_and_run(|import_op| { - let status = authority_set.apply_standard_changes( - hash, - number, - &is_descendent_of::(&*client, None), - initial_sync, - ).map_err(|e| Error::Safety(e.to_string()))?; - - // send a justification notification if a sender exists and in case of error log it. - fn notify_justification( - justification_sender: Option<&GrandpaJustificationSender>, - justification: impl FnOnce() -> Result, Error>, - ) { - if let Some(sender) = justification_sender { - if let Err(err) = sender.notify(justification) { - warn!(target: "afg", "Error creating justification for subscriber: {:?}", err); + let update_res: Result<_, Error> = client.lock_import_and_run({ + let mut telemetry = telemetry.clone(); + let mut authority_set = authority_set.clone(); + let client = client.clone(); + move |import_op| { + let status = authority_set.apply_standard_changes( + hash, + number, + &is_descendent_of::(&*client, None), + initial_sync, + telemetry.as_mut(), + ).map_err(|e| Error::Safety(e.to_string()))?; + + // send a justification notification if a sender exists and in case of error log it. + fn notify_justification( + justification_sender: Option<&GrandpaJustificationSender>, + justification: impl FnOnce() -> Result, Error>, + ) { + if let Some(sender) = justification_sender { + if let Err(err) = sender.notify(justification) { + warn!( + target: "afg", + "Error creating justification for subscriber: {:?}", + err, + ); + } } } - } - // NOTE: this code assumes that honest voters will never vote past a - // transition block, thus we don't have to worry about the case where - // we have a transition with `effective_block = N`, but we finalize - // `N+1`. this assumption is required to make sure we store - // justifications for transition blocks which will be requested by - // syncing clients. - let justification = match justification_or_commit { - JustificationOrCommit::Justification(justification) => { - notify_justification(justification_sender, || Ok(justification.clone())); - Some(justification.encode()) - }, - JustificationOrCommit::Commit((round_number, commit)) => { - let mut justification_required = - // justification is always required when block that enacts new authorities - // set is finalized - status.new_set_block.is_some(); - - // justification is required every N blocks to be able to prove blocks - // finalization to remote nodes - if !justification_required { - if let Some(justification_period) = justification_period { - let last_finalized_number = client.info().finalized_number; - justification_required = - (!last_finalized_number.is_zero() || number - last_finalized_number == justification_period) && - (last_finalized_number / justification_period != number / justification_period); + // NOTE: this code assumes that honest voters will never vote past a + // transition block, thus we don't have to worry about the case where + // we have a transition with `effective_block = N`, but we finalize + // `N+1`. this assumption is required to make sure we store + // justifications for transition blocks which will be requested by + // syncing clients. + let justification = match justification_or_commit { + JustificationOrCommit::Justification(justification) => { + notify_justification(justification_sender, || Ok(justification.clone())); + Some(justification.encode()) + }, + JustificationOrCommit::Commit((round_number, commit)) => { + let mut justification_required = + // justification is always required when block that enacts new authorities + // set is finalized + status.new_set_block.is_some(); + + // justification is required every N blocks to be able to prove blocks + // finalization to remote nodes + if !justification_required { + if let Some(justification_period) = justification_period { + let last_finalized_number = client.info().finalized_number; + justification_required = + (!last_finalized_number.is_zero() || number - last_finalized_number == justification_period) && + (last_finalized_number / justification_period != number / justification_period); + } } - } - // NOTE: the code below is a bit more verbose because we - // really want to avoid creating a justification if it isn't - // needed (e.g. if there's no subscribers), and also to avoid - // creating it twice. depending on the vote tree for the round, - // creating a justification might require multiple fetches of - // headers from the database. - let justification = || GrandpaJustification::from_commit( - &client, - round_number, - commit, - ); + // NOTE: the code below is a bit more verbose because we + // really want to avoid creating a justification if it isn't + // needed (e.g. if there's no subscribers), and also to avoid + // creating it twice. depending on the vote tree for the round, + // creating a justification might require multiple fetches of + // headers from the database. + let justification = || GrandpaJustification::from_commit( + &client, + round_number, + commit, + ); + + if justification_required { + let justification = justification()?; + notify_justification(justification_sender, || Ok(justification.clone())); + + Some(justification.encode()) + } else { + notify_justification(justification_sender, justification); - if justification_required { - let justification = justification()?; - notify_justification(justification_sender, || Ok(justification.clone())); + None + } + }, + }; - Some(justification.encode()) - } else { - notify_justification(justification_sender, justification); + debug!(target: "afg", "Finalizing blocks up to ({:?}, {})", number, hash); - None - } - }, - }; - - debug!(target: "afg", "Finalizing blocks up to ({:?}, {})", number, hash); + // ideally some handle to a synchronization oracle would be used + // to avoid unconditionally notifying. + client.apply_finality(import_op, BlockId::Hash(hash), justification, true).map_err(|e| { + warn!(target: "afg", "Error applying finality to block {:?}: {:?}", (hash, number), e); + e + })?; + telemetry!(telemetry; CONSENSUS_INFO; "afg.finalized_blocks_up_to"; + "number" => ?number, "hash" => ?hash, + ); - // ideally some handle to a synchronization oracle would be used - // to avoid unconditionally notifying. - client.apply_finality(import_op, BlockId::Hash(hash), justification, true).map_err(|e| { - warn!(target: "afg", "Error applying finality to block {:?}: {:?}", (hash, number), e); - e - })?; - telemetry!(CONSENSUS_INFO; "afg.finalized_blocks_up_to"; - "number" => ?number, "hash" => ?hash, - ); + let new_authorities = if let Some((canon_hash, canon_number)) = status.new_set_block { + // the authority set has changed. + let (new_id, set_ref) = authority_set.current(); - let new_authorities = if let Some((canon_hash, canon_number)) = status.new_set_block { - // the authority set has changed. - let (new_id, set_ref) = authority_set.current(); + if set_ref.len() > 16 { + afg_log!(initial_sync, + "👴 Applying GRANDPA set change to new set with {} authorities", + set_ref.len(), + ); + } else { + afg_log!(initial_sync, + "👴 Applying GRANDPA set change to new set {:?}", + set_ref, + ); + } - if set_ref.len() > 16 { - afg_log!(initial_sync, - "👴 Applying GRANDPA set change to new set with {} authorities", - set_ref.len(), + telemetry!(telemetry; CONSENSUS_INFO; "afg.generating_new_authority_set"; + "number" => ?canon_number, "hash" => ?canon_hash, + "authorities" => ?set_ref.to_vec(), + "set_id" => ?new_id, ); + Some(NewAuthoritySet { + canon_hash, + canon_number, + set_id: new_id, + authorities: set_ref.to_vec(), + }) } else { - afg_log!(initial_sync, - "👴 Applying GRANDPA set change to new set {:?}", - set_ref, - ); - } - - telemetry!(CONSENSUS_INFO; "afg.generating_new_authority_set"; - "number" => ?canon_number, "hash" => ?canon_hash, - "authorities" => ?set_ref.to_vec(), - "set_id" => ?new_id, - ); - Some(NewAuthoritySet { - canon_hash, - canon_number, - set_id: new_id, - authorities: set_ref.to_vec(), - }) - } else { - None - }; + None + }; - if status.changed { - let write_result = crate::aux_schema::update_authority_set::( - &authority_set, - new_authorities.as_ref(), - |insert| apply_aux(import_op, insert, &[]), - ); + if status.changed { + let write_result = crate::aux_schema::update_authority_set::( + &authority_set, + new_authorities.as_ref(), + |insert| apply_aux(import_op, insert, &[]), + ); - if let Err(e) = write_result { - warn!(target: "afg", "Failed to write updated authority set to disk. Bailing."); - warn!(target: "afg", "Node is in a potentially inconsistent state."); + if let Err(e) = write_result { + warn!(target: "afg", "Failed to write updated authority set to disk. Bailing."); + warn!(target: "afg", "Node is in a potentially inconsistent state."); - return Err(e.into()); + return Err(e.into()); + } } - } - Ok(new_authorities.map(VoterCommand::ChangeAuthorities)) + Ok(new_authorities.map(VoterCommand::ChangeAuthorities)) + } }); match update_res { diff --git a/client/finality-grandpa/src/import.rs b/client/finality-grandpa/src/import.rs index d7b83b8032909..2643e486b4de8 100644 --- a/client/finality-grandpa/src/import.rs +++ b/client/finality-grandpa/src/import.rs @@ -24,6 +24,7 @@ use parking_lot::RwLockWriteGuard; use sp_blockchain::{BlockStatus, well_known_cache_keys}; use sc_client_api::{backend::Backend, utils::is_descendent_of}; +use sc_telemetry::Telemetry; use sp_utils::mpsc::TracingUnboundedSender; use sp_api::TransactionFor; @@ -62,6 +63,7 @@ pub struct GrandpaBlockImport { send_voter_commands: TracingUnboundedSender>>, authority_set_hard_forks: HashMap>>, justification_sender: GrandpaJustificationSender, + telemetry: Option, _phantom: PhantomData, } @@ -76,6 +78,7 @@ impl Clone for send_voter_commands: self.send_voter_commands.clone(), authority_set_hard_forks: self.authority_set_hard_forks.clone(), justification_sender: self.justification_sender.clone(), + telemetry: self.telemetry.clone(), _phantom: PhantomData, } } @@ -334,7 +337,13 @@ where let applied_changes = { let forced_change_set = guard .as_mut() - .apply_forced_changes(hash, number, &is_descendent_of, initial_sync) + .apply_forced_changes( + hash, + number, + &is_descendent_of, + initial_sync, + self.telemetry.clone().as_mut(), + ) .map_err(|e| ConsensusError::ClientImport(e.to_string())) .map_err(ConsensusError::from)?; @@ -351,8 +360,11 @@ where let canon_hash = self.inner.header(BlockId::Number(canon_number)) .map_err(|e| ConsensusError::ClientImport(e.to_string()))? - .expect("the given block number is less or equal than the current best finalized number; \ - current best finalized number must exist in chain; qed.") + .expect( + "the given block number is less or equal than the current best + finalized number; current best finalized number must exist in + chain; qed." + ) .hash(); NewAuthoritySet { @@ -553,6 +565,7 @@ impl GrandpaBlockImport>>, authority_set_hard_forks: Vec<(SetId, PendingChange>)>, justification_sender: GrandpaJustificationSender, + telemetry: Option, ) -> GrandpaBlockImport { // check for and apply any forced authority set hard fork that applies // to the *current* authority set. @@ -596,6 +609,7 @@ impl GrandpaBlockImport, /// The keystore that manages the keys of this node. pub keystore: Option, + /// Telemetry instance. + pub telemetry: Option, } impl Config { @@ -452,6 +454,7 @@ pub struct LinkHalf { voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: GrandpaJustificationSender, justification_stream: GrandpaJustificationStream, + telemetry: Option, } impl LinkHalf { @@ -502,6 +505,7 @@ pub fn block_import( client: Arc, genesis_authorities_provider: &dyn GenesisAuthoritySetProvider, select_chain: SC, + telemetry: Option, ) -> Result< ( GrandpaBlockImport, @@ -519,6 +523,7 @@ where genesis_authorities_provider, select_chain, Default::default(), + telemetry, ) } @@ -532,6 +537,7 @@ pub fn block_import_with_authority_set_hard_forks genesis_authorities_provider: &dyn GenesisAuthoritySetProvider, select_chain: SC, authority_set_hard_forks: Vec<(SetId, (Block::Hash, NumberFor), AuthorityList)>, + telemetry: Option, ) -> Result< ( GrandpaBlockImport, @@ -551,13 +557,17 @@ where &*client, genesis_hash, >::zero(), - || { - let authorities = genesis_authorities_provider.get()?; - telemetry!(CONSENSUS_DEBUG; "afg.loading_authorities"; - "authorities_len" => ?authorities.len() - ); - Ok(authorities) - } + { + let mut telemetry = telemetry.clone(); + move || { + let authorities = genesis_authorities_provider.get()?; + telemetry!( + telemetry; CONSENSUS_DEBUG; "afg.loading_authorities"; + "authorities_len" => ?authorities.len() + ); + Ok(authorities) + } + }, )?; let (voter_commands_tx, voter_commands_rx) = tracing_unbounded("mpsc_grandpa_voter_command"); @@ -591,6 +601,7 @@ where voter_commands_tx, authority_set_hard_forks, justification_sender.clone(), + telemetry.clone(), ), LinkHalf { client, @@ -599,6 +610,7 @@ where voter_commands_rx, justification_sender, justification_stream, + telemetry, }, )) } @@ -661,14 +673,14 @@ pub struct GrandpaParams { /// `sc_network` crate, it is assumed that the Grandpa notifications protocol has been passed /// to the configuration of the networking. See [`grandpa_peers_set_config`]. pub network: N, - /// If supplied, can be used to hook on telemetry connection established events. - pub telemetry_on_connect: Option>, /// A voting rule used to potentially restrict target votes. pub voting_rule: VR, /// The prometheus metrics registry. pub prometheus_registry: Option, /// The voter state is exposed at an RPC endpoint. pub shared_voter_state: SharedVoterState, + /// Telemetry instance. + pub telemetry: Option, } /// Returns the configuration value to put in @@ -707,10 +719,10 @@ where mut config, link, network, - telemetry_on_connect, voting_rule, prometheus_registry, shared_voter_state, + telemetry, } = grandpa_params; // NOTE: we have recently removed `run_grandpa_observer` from the public @@ -726,6 +738,7 @@ where voter_commands_rx, justification_sender, justification_stream: _, + telemetry: _, // TODO: duplicate info?? } = link; let network = NetworkBridge::new( @@ -733,11 +746,16 @@ where config.clone(), persistent_data.set_state.clone(), prometheus_registry.as_ref(), + telemetry.clone(), ); let conf = config.clone(); - let telemetry_task = if let Some(telemetry_on_connect) = telemetry_on_connect { + let telemetry_task = if let Some(telemetry_on_connect) = telemetry + .as_ref() + .map(|x| x.on_connect_stream()) + { let authorities = persistent_data.authority_set.clone(); + let mut telemetry = telemetry.clone(); let events = telemetry_on_connect .for_each(move |_| { let current_authorities = authorities.current_authorities(); @@ -752,10 +770,11 @@ where let authorities = serde_json::to_string(&authorities).expect( "authorities is always at least an empty vector; \ - elements are always of type string", + elements are always of type string", ); - telemetry!(CONSENSUS_INFO; "afg.authority_set"; + telemetry!( + telemetry; CONSENSUS_INFO; "afg.authority_set"; "authority_id" => authority_id.to_string(), "authority_set_id" => ?set_id, "authorities" => authorities, @@ -779,6 +798,7 @@ where prometheus_registry, shared_voter_state, justification_sender, + telemetry, ); let voter_work = voter_work.map(|res| match res { @@ -817,7 +837,7 @@ struct VoterWork, SC, VR> { env: Arc>, voter_commands_rx: TracingUnboundedReceiver>>, network: NetworkBridge, - + telemetry: Option, /// Prometheus metrics. metrics: Option, } @@ -844,6 +864,7 @@ where prometheus_registry: Option, shared_voter_state: SharedVoterState, justification_sender: GrandpaJustificationSender, + telemetry: Option, ) -> Self { let metrics = match prometheus_registry.as_ref().map(Metrics::register) { Some(Ok(metrics)) => Some(metrics), @@ -867,6 +888,7 @@ where voter_set_state: persistent_data.set_state, metrics: metrics.as_ref().map(|m| m.environment.clone()), justification_sender: Some(justification_sender), + telemetry: telemetry.clone(), _phantom: PhantomData, }); @@ -878,6 +900,7 @@ where env, voter_commands_rx, network, + telemetry, metrics, }; work.rebuild_voter(); @@ -893,7 +916,8 @@ where let authority_id = local_authority_id(&self.env.voters, self.env.config.keystore.as_ref()) .unwrap_or_default(); - telemetry!(CONSENSUS_DEBUG; "afg.starting_new_voter"; + telemetry!( + self.telemetry.clone(); CONSENSUS_DEBUG; "afg.starting_new_voter"; "name" => ?self.env.config.name(), "set_id" => ?self.env.set_id, "authority_id" => authority_id.to_string(), @@ -912,7 +936,8 @@ where "authorities is always at least an empty vector; elements are always of type string", ); - telemetry!(CONSENSUS_INFO; "afg.authority_set"; + telemetry!( + self.telemetry.clone(); CONSENSUS_INFO; "afg.authority_set"; "number" => ?chain_info.finalized_number, "hash" => ?chain_info.finalized_hash, "authority_id" => authority_id.to_string(), @@ -972,7 +997,8 @@ where let voters: Vec = new.authorities.iter().map(move |(a, _)| { format!("{}", a) }).collect(); - telemetry!(CONSENSUS_INFO; "afg.voter_command_change_authorities"; + telemetry!( + self.telemetry; CONSENSUS_INFO; "afg.voter_command_change_authorities"; "number" => ?new.canon_number, "hash" => ?new.canon_hash, "voters" => ?voters, @@ -993,10 +1019,11 @@ where })?; let voters = Arc::new(VoterSet::new(new.authorities.into_iter()) - .expect("new authorities come from pending change; \ - pending change comes from `AuthoritySet`; \ - `AuthoritySet` validates authorities is non-empty and weights are non-zero; \ - qed." + .expect( + "new authorities come from pending change; \ + pending change comes from `AuthoritySet`; \ + `AuthoritySet` validates authorities is non-empty and weights are non-zero; \ + qed." ) ); @@ -1012,6 +1039,7 @@ where voting_rule: self.env.voting_rule.clone(), metrics: self.env.metrics.clone(), justification_sender: self.env.justification_sender.clone(), + telemetry: self.telemetry.clone(), _phantom: PhantomData, }); diff --git a/client/finality-grandpa/src/observer.rs b/client/finality-grandpa/src/observer.rs index 3054a9df61c56..6bf5a8efe8f6f 100644 --- a/client/finality-grandpa/src/observer.rs +++ b/client/finality-grandpa/src/observer.rs @@ -29,6 +29,7 @@ use log::{debug, info, warn}; use sp_keystore::SyncCryptoStorePtr; use sp_consensus::SelectChain; use sc_client_api::backend::Backend; +use sc_telemetry::Telemetry; use sp_utils::mpsc::TracingUnboundedReceiver; use sp_runtime::traits::{NumberFor, Block as BlockT}; use sp_blockchain::HeaderMetadata; @@ -67,6 +68,7 @@ fn grandpa_observer( last_finalized_number: NumberFor, commits: S, note_round: F, + telemetry: Option, ) -> impl Future>>> where NumberFor: BlockNumberOps, @@ -121,6 +123,7 @@ where (round, commit).into(), false, justification_sender.as_ref(), + telemetry.clone(), ) { Ok(_) => {}, Err(e) => return future::err(e), @@ -172,6 +175,7 @@ where persistent_data, voter_commands_rx, justification_sender, + telemetry, .. } = link; @@ -180,6 +184,7 @@ where config.clone(), persistent_data.set_state.clone(), None, + telemetry.clone(), ); let observer_work = ObserverWork::new( @@ -189,6 +194,7 @@ where config.keystore, voter_commands_rx, Some(justification_sender), + telemetry, ); let observer_work = observer_work @@ -210,6 +216,7 @@ struct ObserverWork> { keystore: Option, voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: Option>, + telemetry: Option, _phantom: PhantomData, } @@ -228,6 +235,7 @@ where keystore: Option, voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: Option>, + telemetry: Option, ) -> Self { let mut work = ObserverWork { @@ -240,6 +248,7 @@ where keystore: keystore.clone(), voter_commands_rx, justification_sender, + telemetry, _phantom: PhantomData, }; work.rebuild_observer(); @@ -289,6 +298,7 @@ where last_finalized_number, global_in, note_round, + self.telemetry.clone(), ); self.observer = Box::pin(observer); diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 1859a7cf91907..2593635f814c5 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -48,6 +48,7 @@ use std::iter; use std::sync::Arc; pub use libp2p::wasm_ext::ExtTransport; +pub use log; pub use serde_json; mod endpoints; @@ -382,17 +383,24 @@ pub struct Telemetry { impl Telemetry { /// Send telemetries. - pub async fn send(&mut self, verbosity: VerbosityLevel, payload: TelemetryPayload) { + pub fn send(&mut self, verbosity: VerbosityLevel, payload: TelemetryPayload) { match self .message_sender - .send((self.id, verbosity, payload)) - .await + .try_send((self.id, verbosity, payload)) { Ok(()) => {} Err(err) if err.is_full() => todo!("overflow"), Err(_) => unreachable!(), } } + + /// Get event stream for telemetry connection established events. + /// + /// This function will return an error if the telemetry has already been started by + /// [`TelemetryHandle::start_telemetry`]. + pub fn on_connect_stream(&self) -> TracingUnboundedReceiver<()> { + self.connection_notifier.on_connect_stream() + } } /// Used to create a stream of events with only one event: when a telemetry connection @@ -404,11 +412,7 @@ pub struct TelemetryConnectionNotifier { } impl TelemetryConnectionNotifier { - /// Get event stream for telemetry connection established events. - /// - /// This function will return an error if the telemetry has already been started by - /// [`TelemetryHandle::start_telemetry`]. - pub fn on_connect_stream(&self) -> TracingUnboundedReceiver<()> { + fn on_connect_stream(&self) -> TracingUnboundedReceiver<()> { let (message_sender, message_receiver) = tracing_unbounded("mpsc_telemetry_on_connect"); if let Err(err) = self.register_sender.unbounded_send(Register::Notifier { addresses: self.addresses.clone(), @@ -451,7 +455,7 @@ enum Register { /// # let authority_id = 42_u64; /// # let set_id = (43_u64, 44_u64); /// # let authorities = vec![45_u64]; -/// telemetry!(CONSENSUS_INFO; "afg.authority_set"; +/// telemetry!(telemetry; CONSENSUS_INFO; "afg.authority_set"; /// "authority_id" => authority_id.to_string(), /// "authority_set_id" => ?set_id, /// "authorities" => authorities, @@ -460,19 +464,21 @@ enum Register { #[macro_export(local_inner_macros)] macro_rules! telemetry { ( $telemetry:expr; $verbosity:expr; $msg:expr; $( $t:tt )* ) => {{ - let verbosity: $crate::VerbosityLevel = $verbosity; - match format_fields_to_json!($($t)*) { - Err(err) => { - $crate::tracing::error!( - target: "telemetry", - "Could not serialize value for telemetry: {}", - err, - ); - }, - Ok(mut json) => { - json.insert("msg".into(), $msg.into()); - $telemetry.send(verbosity, json); - }, + if let Some(telemetry) = $telemetry.as_mut() { + let verbosity: $crate::VerbosityLevel = $verbosity; + match format_fields_to_json!($($t)*) { + Err(err) => { + $crate::log::error!( + target: "telemetry", + "Could not serialize value for telemetry: {}", + err, + ); + }, + Ok(mut json) => { + json.insert("msg".into(), $msg.into()); + telemetry.send(verbosity, json); + }, + } } }}; } From 942addbbd14d83b404f439132e08e752985adf40 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 11 Feb 2021 15:39:21 +0100 Subject: [PATCH 05/92] WIP --- client/consensus/slots/src/lib.rs | 67 +++++++++++++++++++------------ 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index d851753921334..ef2b9e259a3f9 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -47,7 +47,7 @@ use sp_runtime::{ generic::BlockId, traits::{Block as BlockT, Header, HashFor, NumberFor} }; -use sc_telemetry::{telemetry, CONSENSUS_DEBUG, CONSENSUS_WARN, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, Telemetry, CONSENSUS_DEBUG, CONSENSUS_WARN, CONSENSUS_INFO}; /// The changes that need to applied to the storage to create the state for a block. /// @@ -78,7 +78,12 @@ pub trait SlotWorker { /// /// Returns a future that resolves to a [`SlotResult`] iff a block was successfully built in /// the slot. Otherwise `None` is returned. - fn on_slot(&mut self, chain_head: B::Header, slot_info: SlotInfo) -> Self::OnSlot; + fn on_slot( + &mut self, + chain_head: B::Header, + slot_info: SlotInfo, + telemetry: Option, + ) -> Self::OnSlot; } /// A skeleton implementation for `SlotWorker` which tries to claim a slot at @@ -205,6 +210,7 @@ pub trait SimpleSlotWorker { &mut self, chain_head: B::Header, slot_info: SlotInfo, + mut telemetry: Option, ) -> Pin>> + Send>> where >::Proposal: Unpin + Send + 'static, @@ -234,7 +240,7 @@ pub trait SimpleSlotWorker { warn!("Unable to fetch epoch data at block {:?}: {:?}", chain_head.hash(), err); telemetry!( - CONSENSUS_WARN; "slots.unable_fetching_authorities"; + telemetry; CONSENSUS_WARN; "slots.unable_fetching_authorities"; "slot" => ?chain_head.hash(), "err" => ?err, ); @@ -253,8 +259,7 @@ pub trait SimpleSlotWorker { { debug!(target: self.logging_target(), "Skipping proposal slot. Waiting for the network."); telemetry!( - CONSENSUS_DEBUG; - "slots.skipping_proposal_slot"; + telemetry; CONSENSUS_DEBUG; "slots.skipping_proposal_slot"; "authorities_len" => authorities_len, ); @@ -278,24 +283,25 @@ pub trait SimpleSlotWorker { ); telemetry!( - CONSENSUS_DEBUG; - "slots.starting_authorship"; + telemetry; CONSENSUS_DEBUG; "slots.starting_authorship"; "slot_num" => *slot, "timestamp" => timestamp, ); - let awaiting_proposer = self.proposer(&chain_head).map_err(move |err| { - warn!("Unable to author block in slot {:?}: {:?}", slot, err); + let awaiting_proposer = { + let mut telemetry = telemetry.clone(); + self.proposer(&chain_head).map_err(move |err| { + warn!("Unable to author block in slot {:?}: {:?}", slot, err); - telemetry!( - CONSENSUS_WARN; - "slots.unable_authoring_block"; - "slot" => *slot, - "err" => ?err - ); + telemetry!( + telemetry; CONSENSUS_WARN; "slots.unable_authoring_block"; + "slot" => *slot, + "err" => ?err + ); - err - }); + err + }) + }; let logs = self.pre_digest_data(slot, &claim); @@ -309,7 +315,8 @@ pub trait SimpleSlotWorker { RecordProof::No, ).map_err(|e| sp_consensus::Error::ClientImport(format!("{:?}", e)))); - let proposal_work = + let proposal_work = { + let mut telemetry = telemetry.clone(); futures::future::select(proposing, proposing_remaining).map(move |v| match v { Either::Left((b, _)) => b.map(|b| (b, claim)), Either::Right(_) => { @@ -318,14 +325,14 @@ pub trait SimpleSlotWorker { #[cfg(build_type="debug")] info!("👉 Recompile your node in `--release` mode to mitigate this problem."); telemetry!( - CONSENSUS_INFO; - "slots.discarding_proposal_took_too_long"; + telemetry; CONSENSUS_INFO; "slots.discarding_proposal_took_too_long"; "slot" => *slot, ); Err(sp_consensus::Error::ClientImport("Timeout in the Slots proposer".into())) }, - }); + }) + }; let block_import_params_maker = self.block_import_params(); let block_import = self.block_import(); @@ -354,7 +361,8 @@ pub trait SimpleSlotWorker { header_hash, ); - telemetry!(CONSENSUS_INFO; "slots.pre_sealed_block"; + telemetry!( + telemetry; CONSENSUS_INFO; "slots.pre_sealed_block"; "header_num" => ?header_num, "hash_now" => ?block_import_params.post_hash(), "hash_previously" => ?header_hash, @@ -369,7 +377,7 @@ pub trait SimpleSlotWorker { ); telemetry!( - CONSENSUS_WARN; "slots.err_with_block_built_on"; + telemetry; CONSENSUS_WARN; "slots.err_with_block_built_on"; "hash" => ?parent_hash, "err" => ?err, ); @@ -385,8 +393,13 @@ pub trait SimpleSlotWorker { impl> SlotWorker for T { type OnSlot = Pin>> + Send>>; - fn on_slot(&mut self, chain_head: B::Header, slot_info: SlotInfo) -> Self::OnSlot { - SimpleSlotWorker::on_slot(self, chain_head, slot_info) + fn on_slot( + &mut self, + chain_head: B::Header, + slot_info: SlotInfo, + telemetry: Option, + ) -> Self::OnSlot { + SimpleSlotWorker::on_slot(self, chain_head, slot_info, telemetry) } } @@ -411,6 +424,7 @@ pub fn start_slot_worker( inherent_data_providers: InherentDataProviders, timestamp_extractor: SC, can_author_with: CAW, + telemetry: Option, ) -> impl Future where B: BlockT, @@ -458,7 +472,8 @@ where Either::Right(future::ready(Ok(()))) } else { Either::Left( - worker.on_slot(chain_head, slot_info).then(|_| future::ready(Ok(()))) + worker.on_slot(chain_head, slot_info, telemetry.clone()) + .then(|_| future::ready(Ok(()))) ) } }).then(|res| { From 29bda72466c4c1283735160178360b682e3ec0e0 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 11 Feb 2021 15:42:36 +0100 Subject: [PATCH 06/92] WIP --- client/consensus/babe/src/lib.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/client/consensus/babe/src/lib.rs b/client/consensus/babe/src/lib.rs index 61be3a2f5e5bc..d37dc2a13a7bb 100644 --- a/client/consensus/babe/src/lib.rs +++ b/client/consensus/babe/src/lib.rs @@ -89,7 +89,7 @@ use sp_runtime::{ use sp_api::{ProvideRuntimeApi, NumberFor}; use parking_lot::Mutex; use sp_inherents::{InherentDataProviders, InherentData}; -use sc_telemetry::{telemetry, CONSENSUS_TRACE, CONSENSUS_DEBUG}; +use sc_telemetry::{telemetry, Telemetry, CONSENSUS_TRACE, CONSENSUS_DEBUG}; use sp_consensus::{ BlockImport, Environment, Proposer, BlockCheckParams, ForkChoiceStrategy, BlockImportParams, BlockOrigin, Error as ConsensusError, @@ -393,6 +393,9 @@ pub struct BabeParams { /// Checks if the current native implementation can author with a runtime at a given block. pub can_author_with: CAW, + + /// Telemetry instance. + pub telemetry: Option, } /// Start the babe worker. @@ -408,6 +411,7 @@ pub fn start_babe(BabeParams { backoff_authoring_blocks, babe_link, can_author_with, + telemetry, }: BabeParams) -> Result< BabeWorker, sp_consensus::Error, @@ -458,6 +462,7 @@ pub fn start_babe(BabeParams { inherent_data_providers, babe_link.time_source, can_author_with, + telemetry, ); Ok(BabeWorker { inner: Box::pin(inner), @@ -842,6 +847,7 @@ pub struct BabeVerifier { epoch_changes: SharedEpochChanges, time_source: TimeSource, can_author_with: CAW, + telemetry: Option, } impl BabeVerifier @@ -1069,7 +1075,7 @@ where trace!(target: "babe", "Checked {:?}; importing.", pre_header); telemetry!( - CONSENSUS_TRACE; + self.telemetry; CONSENSUS_TRACE; "babe.checked_and_importing"; "pre_header" => ?pre_header); @@ -1087,7 +1093,8 @@ where } CheckedHeader::Deferred(a, b) => { debug!(target: "babe", "Checking {:?} failed; {:?}, {:?}.", hash, a, b); - telemetry!(CONSENSUS_DEBUG; "babe.header_too_far_in_future"; + telemetry!( + self.telemetry; CONSENSUS_DEBUG; "babe.header_too_far_in_future"; "hash" => ?hash, "a" => ?a, "b" => ?b ); Err(Error::::TooFarInFuture(hash).into()) @@ -1494,6 +1501,7 @@ pub fn import_queue( spawner: &impl sp_core::traits::SpawnNamed, registry: Option<&Registry>, can_author_with: CAW, + telemetry: Option, ) -> ClientResult> where Inner: BlockImport> + Send + Sync + 'static, @@ -1513,6 +1521,7 @@ pub fn import_queue( epoch_changes: babe_link.epoch_changes, time_source: babe_link.time_source, can_author_with, + telemetry, }; Ok(BasicQueue::new( From faad15566fade1191cdcdb74af686b8616e04f6f Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 11 Feb 2021 16:23:42 +0100 Subject: [PATCH 07/92] WIP --- client/service/src/builder.rs | 48 +++++++++++++++-------------- client/service/src/client/client.rs | 10 ++++-- client/service/src/client/light.rs | 1 - client/service/src/metrics.rs | 14 ++++----- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 916929bff65df..ac65278f436cd 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -54,9 +54,8 @@ use std::sync::Arc; use wasm_timer::SystemTime; use sc_telemetry::{ telemetry, + Telemetry, ConnectionMessage, - TelemetryConnectionNotifier, - TelemetrySpan, SUBSTRATE_INFO, }; use sp_transaction_pool::MaintainedTransactionPool; @@ -492,10 +491,6 @@ pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend> { pub network_status_sinks: NetworkStatusSinks, /// A Sender for RPC requests. pub system_rpc_tx: TracingUnboundedSender>, - /// Telemetry span. - /// - /// This span needs to be entered **before** calling [`spawn_tasks()`]. - pub telemetry_span: Option, } /// Build a shared offchain workers instance. @@ -542,7 +537,7 @@ pub fn build_offchain_workers( /// Spawn the tasks that are required to run a node. pub fn spawn_tasks( params: SpawnTasksParams, -) -> Result<(RpcHandlers, Option), Error> +) -> Result<(RpcHandlers, Option), Error> where TCl: ProvideRuntimeApi + HeaderMetadata + Chain + BlockBackend + BlockIdTo + ProofProvider + @@ -564,7 +559,7 @@ pub fn spawn_tasks( let SpawnTasksParams { mut config, task_manager, - client, + mut client, on_demand, backend, keystore, @@ -574,7 +569,6 @@ pub fn spawn_tasks( network, network_status_sinks, system_rpc_tx, - telemetry_span, } = params; let chain_info = client.usage_info().chain; @@ -585,13 +579,16 @@ pub fn spawn_tasks( config.dev_key_seed.clone().map(|s| vec![s]).unwrap_or_default(), ).map_err(|e| Error::Application(Box::new(e)))?; - let telemetry_connection_notifier = init_telemetry( + let telemetry = init_telemetry( &mut config, - telemetry_span, network.clone(), client.clone(), ); + if let Some(telemetry) = telemetry.clone() { + client.set_telemetry(telemetry); + } + info!("📦 Highest known block at #{}", chain_info.best_number); let spawn_handle = task_manager.spawn_handle(); @@ -604,7 +601,7 @@ pub fn spawn_tasks( spawn_handle.spawn( "on-transaction-imported", - transaction_notifications(transaction_pool.clone(), network.clone()), + transaction_notifications(transaction_pool.clone(), network.clone(), telemetry.clone()), ); // Prometheus metrics. @@ -660,12 +657,13 @@ pub fn spawn_tasks( task_manager.keep_alive((config.base_path, rpc, rpc_handlers.clone())); - Ok((rpc_handlers, telemetry_connection_notifier)) + Ok((rpc_handlers, telemetry)) } async fn transaction_notifications( transaction_pool: Arc, network: Arc::Hash>>, + mut telemetry: Option, ) where TBl: BlockT, @@ -677,9 +675,10 @@ async fn transaction_notifications( .for_each(move |hash| { network.propagate_transaction(hash); let status = transaction_pool.status(); - telemetry!(SUBSTRATE_INFO; "txpool.import"; + telemetry!( + telemetry; SUBSTRATE_INFO; "txpool.import"; "ready" => status.ready, - "future" => status.future + "future" => status.future, ); ready(()) }) @@ -688,11 +687,9 @@ async fn transaction_notifications( fn init_telemetry>( config: &mut Configuration, - telemetry_span: Option, network: Arc::Hash>>, client: Arc, -) -> Option { - let telemetry_span = telemetry_span?; +) -> Option { let endpoints = config.telemetry_endpoints.clone()?; let genesis_hash = client.block_hash(Zero::zero()).ok().flatten().unwrap_or_default(); let connection_message = ConnectionMessage { @@ -711,11 +708,16 @@ fn init_telemetry>( config.telemetry_handle .as_mut() - .map(|handle| handle.start_telemetry( - telemetry_span, - endpoints, - connection_message, - )) + .and_then(|handle| { + let telemetry = handle.start_telemetry(endpoints, connection_message); + if telemetry.is_none() { + log::error!( + target: "telemetry", + "Could not initialize telemetry. The maximum number has been reached.", + ); + } + telemetry + }) } fn gen_handler( diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index b1ff0678ee9a4..54c102de6394f 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -35,7 +35,7 @@ use sp_core::{ }; #[cfg(feature="test-helpers")] use sp_keystore::SyncCryptoStorePtr; -use sc_telemetry::{telemetry, SUBSTRATE_INFO}; +use sc_telemetry::{telemetry, Telemetry, SUBSTRATE_INFO}; use sp_runtime::{ Justification, BuildStorage, generic::{BlockId, SignedBlock, DigestItem}, @@ -115,6 +115,7 @@ pub struct Client where Block: BlockT { block_rules: BlockRules, execution_extensions: ExecutionExtensions, config: ClientConfig, + telemetry: Option, _phantom: PhantomData, } @@ -326,6 +327,7 @@ impl Client where block_rules: BlockRules::new(fork_blocks, bad_blocks), execution_extensions, config, + telemetry: None, // TODO how to inject _phantom: Default::default(), }) } @@ -668,7 +670,8 @@ impl Client where if origin != BlockOrigin::NetworkInitialSync || rand::thread_rng().gen_bool(0.1) { - telemetry!(SUBSTRATE_INFO; "block.import"; + telemetry!( + self.telemetry.clone(); SUBSTRATE_INFO; "block.import"; "height" => height, "best" => ?hash, "origin" => ?origin @@ -988,7 +991,8 @@ impl Client where indicated in the tree route; qed" ); - telemetry!(SUBSTRATE_INFO; "notify.finalized"; + telemetry!( + self.telemetry.clone(); SUBSTRATE_INFO; "notify.finalized"; "height" => format!("{}", header.number()), "best" => ?last, ); diff --git a/client/service/src/client/light.rs b/client/service/src/client/light.rs index 5b5c0cb0eb38f..1020b0e52b2c9 100644 --- a/client/service/src/client/light.rs +++ b/client/service/src/client/light.rs @@ -31,7 +31,6 @@ use super::{call_executor::LocalCallExecutor, client::{Client, ClientConfig}}; use sc_client_api::light::Storage as BlockchainStorage; use sc_light::{Backend, GenesisCallExecutor}; - /// Create an instance of light client. pub fn new_light( backend: Arc>>, diff --git a/client/service/src/metrics.rs b/client/service/src/metrics.rs index 4fbfa4d77f08f..904a1602cc109 100644 --- a/client/service/src/metrics.rs +++ b/client/service/src/metrics.rs @@ -21,7 +21,7 @@ use std::{convert::TryFrom, time::SystemTime}; use crate::{NetworkStatus, NetworkState, NetworkStatusSinks, config::Configuration}; use futures_timer::Delay; use prometheus_endpoint::{register, Gauge, U64, Registry, PrometheusError, Opts, GaugeVec}; -use sc_telemetry::{telemetry, SUBSTRATE_INFO}; +use sc_telemetry::{telemetry, Telemetry, SUBSTRATE_INFO}; use sp_api::ProvideRuntimeApi; use sp_runtime::traits::{NumberFor, Block, SaturatedConversion, UniqueSaturatedInto}; use sp_transaction_pool::{PoolStatus, MaintainedTransactionPool}; @@ -112,6 +112,7 @@ pub struct MetricsService { last_update: Instant, last_total_bytes_inbound: u64, last_total_bytes_outbound: u64, + telemetry: Option, } impl MetricsService { @@ -123,6 +124,7 @@ impl MetricsService { last_total_bytes_inbound: 0, last_total_bytes_outbound: 0, last_update: Instant::now(), + telemetry: None, // TODO how to inject } } @@ -149,6 +151,7 @@ impl MetricsService { last_total_bytes_inbound: 0, last_total_bytes_outbound: 0, last_update: Instant::now(), + telemetry: None, }) } @@ -245,8 +248,7 @@ impl MetricsService { // Update/send metrics that are always available. telemetry!( - SUBSTRATE_INFO; - "system.interval"; + self.telemetry; SUBSTRATE_INFO; "system.interval"; "height" => best_number, "best" => ?best_hash, "txcount" => txpool_status.ready, @@ -307,8 +309,7 @@ impl MetricsService { }; telemetry!( - SUBSTRATE_INFO; - "system.interval"; + self.telemetry; SUBSTRATE_INFO; "system.interval"; "peers" => num_peers, "bandwidth_download" => avg_bytes_per_sec_inbound, "bandwidth_upload" => avg_bytes_per_sec_outbound, @@ -328,8 +329,7 @@ impl MetricsService { // Send network state information, if any. if let Some(net_state) = net_state { telemetry!( - SUBSTRATE_INFO; - "system.network_state"; + self.telemetry; SUBSTRATE_INFO; "system.network_state"; "state" => net_state, ); } From d09bef4b797328276e8839c2cbfb9e6ee71fd0e0 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 13:51:05 +0100 Subject: [PATCH 08/92] Oh no this is terrible --- client/service/src/builder.rs | 8 ++++---- client/service/src/client/client.rs | 26 ++++++++++++++++++-------- client/telemetry/src/lib.rs | 6 ++++++ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index ac65278f436cd..cf2691a80f3e1 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -54,8 +54,9 @@ use std::sync::Arc; use wasm_timer::SystemTime; use sc_telemetry::{ telemetry, - Telemetry, + ClientTelemetry, ConnectionMessage, + Telemetry, SUBSTRATE_INFO, }; use sp_transaction_pool::MaintainedTransactionPool; @@ -542,8 +543,7 @@ pub fn spawn_tasks( TCl: ProvideRuntimeApi + HeaderMetadata + Chain + BlockBackend + BlockIdTo + ProofProvider + HeaderBackend + BlockchainEvents + ExecutorProvider + UsageProvider + - StorageProvider + CallApiAt + - Send + 'static, + StorageProvider + CallApiAt + ClientTelemetry + Send + 'static, >::Api: sp_api::Metadata + sc_offchain::OffchainWorkerApi + @@ -559,7 +559,7 @@ pub fn spawn_tasks( let SpawnTasksParams { mut config, task_manager, - mut client, + client, on_demand, backend, keystore, diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index 54c102de6394f..5435129a689e7 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -35,7 +35,7 @@ use sp_core::{ }; #[cfg(feature="test-helpers")] use sp_keystore::SyncCryptoStorePtr; -use sc_telemetry::{telemetry, Telemetry, SUBSTRATE_INFO}; +use sc_telemetry::{telemetry, ClientTelemetry, Telemetry, SUBSTRATE_INFO}; use sp_runtime::{ Justification, BuildStorage, generic::{BlockId, SignedBlock, DigestItem}, @@ -115,7 +115,7 @@ pub struct Client where Block: BlockT { block_rules: BlockRules, execution_extensions: ExecutionExtensions, config: ClientConfig, - telemetry: Option, + telemetry: RwLock>, _phantom: PhantomData, } @@ -327,7 +327,7 @@ impl Client where block_rules: BlockRules::new(fork_blocks, bad_blocks), execution_extensions, config, - telemetry: None, // TODO how to inject + telemetry: RwLock::new(None), _phantom: Default::default(), }) } @@ -671,7 +671,7 @@ impl Client where rand::thread_rng().gen_bool(0.1) { telemetry!( - self.telemetry.clone(); SUBSTRATE_INFO; "block.import"; + self.telemetry.read().clone(); SUBSTRATE_INFO; "block.import"; "height" => height, "best" => ?hash, "origin" => ?origin @@ -992,7 +992,7 @@ impl Client where ); telemetry!( - self.telemetry.clone(); SUBSTRATE_INFO; "notify.finalized"; + self.telemetry.read().clone(); SUBSTRATE_INFO; "notify.finalized"; "height" => format!("{}", header.number()), "best" => ?last, ); @@ -1991,9 +1991,10 @@ impl backend::AuxStore for &Client } impl sp_consensus::block_validation::Chain for Client - where BE: backend::Backend, - E: CallExecutor, - B: BlockT + where + BE: backend::Backend, + E: CallExecutor, + B: BlockT, { fn block_status( &self, @@ -2002,3 +2003,12 @@ impl sp_consensus::block_validation::Chain for Client) } } + +impl ClientTelemetry for Client + where + B: BlockT, +{ + fn set_telemetry(&self, telemetry: Telemetry) { + *self.telemetry.write() = Some(telemetry); + } +} diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 2593635f814c5..0b47c11310724 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -534,3 +534,9 @@ impl IdGenerator { self.0.lock().next() } } + +/// TODO +pub trait ClientTelemetry { + /// TODO + fn set_telemetry(&self, telemetry: Telemetry); +} From 4f3d24b8182acc9c14e56472657abda31c47730e Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 12 Feb 2021 14:12:23 +0100 Subject: [PATCH 09/92] WIP --- Cargo.lock | 11 +++++++++++ client/cli/src/arg_enums.rs | 2 -- client/service/src/builder.rs | 11 +---------- client/telemetry/Cargo.toml | 1 + client/telemetry/src/lib.rs | 30 ++++++++++++------------------ 5 files changed, 25 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7fa0cebb41c21..9e04a4ec6c7b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2165,6 +2165,16 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" +[[package]] +name = "global_counter" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037cd38cf90be505d4f74eef395d027308d74b5dda77e328ad53244b30566a36" +dependencies = [ + "once_cell", + "parking_lot 0.10.2", +] + [[package]] name = "globset" version = "0.4.6" @@ -7651,6 +7661,7 @@ version = "3.0.0" dependencies = [ "chrono", "futures 0.3.12", + "global_counter", "libp2p", "log", "parking_lot 0.11.1", diff --git a/client/cli/src/arg_enums.rs b/client/cli/src/arg_enums.rs index 2ebfa38925e23..653a32354017d 100644 --- a/client/cli/src/arg_enums.rs +++ b/client/cli/src/arg_enums.rs @@ -64,7 +64,6 @@ arg_enum! { #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum TracingReceiver { Log, - Telemetry, } } @@ -72,7 +71,6 @@ impl Into for TracingReceiver { fn into(self) -> sc_tracing::TracingReceiver { match self { TracingReceiver::Log => sc_tracing::TracingReceiver::Log, - TracingReceiver::Telemetry => sc_tracing::TracingReceiver::Telemetry, } } } diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index cf2691a80f3e1..ef9c88764cfca 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -708,16 +708,7 @@ fn init_telemetry>( config.telemetry_handle .as_mut() - .and_then(|handle| { - let telemetry = handle.start_telemetry(endpoints, connection_message); - if telemetry.is_none() { - log::error!( - target: "telemetry", - "Could not initialize telemetry. The maximum number has been reached.", - ); - } - telemetry - }) + .map(|handle| handle.start_telemetry(endpoints, connection_message)) } fn gen_handler( diff --git a/client/telemetry/Cargo.toml b/client/telemetry/Cargo.toml index 99fa00dc6db71..84395a768d9f2 100644 --- a/client/telemetry/Cargo.toml +++ b/client/telemetry/Cargo.toml @@ -28,3 +28,4 @@ void = "1.0.2" serde_json = "1.0.41" sp-utils = { version = "3.0.0", path = "../../primitives/utils" } chrono = "0.4.19" +global_counter = "0.2.1" diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 0b47c11310724..fa173bc4bdce3 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -37,15 +37,13 @@ #![warn(missing_docs)] use futures::{channel::mpsc, prelude::*}; +use global_counter::primitive::exact::CounterU64; use libp2p::Multiaddr; use log::{error, warn}; -use parking_lot::Mutex; use serde::Serialize; use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver}; use std::collections::HashMap; use std::io; -use std::iter; -use std::sync::Arc; pub use libp2p::wasm_ext::ExtTransport; pub use log; @@ -73,6 +71,8 @@ pub const CONSENSUS_WARN: VerbosityLevel = 4; /// Consensus INFO log level. pub const CONSENSUS_INFO: VerbosityLevel = 1; +static TELEMETRY_ID_COUNTER: CounterU64 = CounterU64::new(1); + /// Telemetry message verbosity. pub type VerbosityLevel = u8; @@ -338,7 +338,7 @@ impl TelemetryHandle { &mut self, endpoints: TelemetryEndpoints, connection_message: ConnectionMessage, - ) -> Option { + ) -> Telemetry { let Self { message_sender, register_sender, @@ -350,7 +350,7 @@ impl TelemetryHandle { addresses: endpoints.0.iter().map(|(addr, _)| addr.clone()).collect(), }; - let id = id_generator.next()?; + let id = id_generator.next(); match register_sender.unbounded_send(Register::Telemetry { id, endpoints, @@ -365,11 +365,11 @@ impl TelemetryHandle { ), } - Some(Telemetry { + Telemetry { message_sender: message_sender.clone(), id, connection_notifier, - }) + } } } @@ -384,10 +384,7 @@ pub struct Telemetry { impl Telemetry { /// Send telemetries. pub fn send(&mut self, verbosity: VerbosityLevel, payload: TelemetryPayload) { - match self - .message_sender - .try_send((self.id, verbosity, payload)) - { + match self.message_sender.try_send((self.id, verbosity, payload)) { Ok(()) => {} Err(err) if err.is_full() => todo!("overflow"), Err(_) => unreachable!(), @@ -520,18 +517,15 @@ macro_rules! format_fields_to_json { } #[derive(Debug, Clone)] -struct IdGenerator(Arc Option>>>>); +struct IdGenerator; impl IdGenerator { fn new() -> Self { - Self(Arc::new(Mutex::new(iter::successors( - Some(1), - Box::new(|n| n.checked_add(1)), - )))) + Self } - fn next(&mut self) -> Option { - self.0.lock().next() + fn next(&mut self) -> Id { + TELEMETRY_ID_COUNTER.inc() } } From 6a8040f59b1d4c9deb327097be3c2a2891b85ffd Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 12 Feb 2021 14:15:48 +0100 Subject: [PATCH 10/92] WIP --- .../basic-authorship/src/basic_authorship.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/client/basic-authorship/src/basic_authorship.rs b/client/basic-authorship/src/basic_authorship.rs index 067695e5a84da..1a6e064b1f018 100644 --- a/client/basic-authorship/src/basic_authorship.rs +++ b/client/basic-authorship/src/basic_authorship.rs @@ -32,7 +32,7 @@ use sp_runtime::{ traits::{Block as BlockT, Hash as HashT, Header as HeaderT, DigestFor, BlakeTwo256}, }; use sp_transaction_pool::{TransactionPool, InPoolTransaction}; -use sc_telemetry::{telemetry, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, Telemetry, CONSENSUS_INFO}; use sc_block_builder::{BlockBuilderApi, BlockBuilderProvider}; use sp_api::{ProvideRuntimeApi, ApiExt}; use futures::{future, future::{Future, FutureExt}, channel::oneshot, select}; @@ -60,9 +60,10 @@ pub struct ProposerFactory { transaction_pool: Arc, /// Prometheus Link, metrics: PrometheusMetrics, + max_block_size: usize, + telemetry: Option, /// phantom member to pin the `Backend` type. _phantom: PhantomData, - max_block_size: usize, } impl ProposerFactory { @@ -71,14 +72,16 @@ impl ProposerFactory { client: Arc, transaction_pool: Arc, prometheus: Option<&PrometheusRegistry>, + telemetry: Option, ) -> Self { ProposerFactory { spawn_handle: Box::new(spawn_handle), client, transaction_pool, metrics: PrometheusMetrics::new(prometheus), - _phantom: PhantomData, max_block_size: DEFAULT_MAX_BLOCK_SIZE, + telemetry, + _phantom: PhantomData, } } @@ -121,8 +124,9 @@ impl ProposerFactory transaction_pool: self.transaction_pool.clone(), now, metrics: self.metrics.clone(), - _phantom: PhantomData, max_block_size: self.max_block_size, + telemetry: self.telemetry.clone(), + _phantom: PhantomData, }; proposer @@ -162,8 +166,9 @@ pub struct Proposer { transaction_pool: Arc, now: Box time::Instant + Send + Sync>, metrics: PrometheusMetrics, - _phantom: PhantomData, max_block_size: usize, + telemetry: Option, + _phantom: PhantomData, } impl sp_consensus::Proposer for @@ -343,7 +348,8 @@ impl Proposer .collect::>() .join(", ") ); - telemetry!(CONSENSUS_INFO; "prepared_block_for_proposing"; + telemetry!( + self.telemetry.clone(); CONSENSUS_INFO; "prepared_block_for_proposing"; "number" => ?block.header().number(), "hash" => ?::Hash::from(block.header().hash()), ); From 244982ae299f8d44eaf18598289e66e5ca83b860 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 12 Feb 2021 14:27:22 +0100 Subject: [PATCH 11/92] WIP --- bin/node/cli/src/service.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index df3802d3d802c..014b2e7246a69 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -34,7 +34,7 @@ use sp_runtime::traits::Block as BlockT; use futures::prelude::*; use sc_client_api::{ExecutorProvider, RemoteBackend}; use node_executor::Executor; -use sc_telemetry::{TelemetryConnectionNotifier, TelemetrySpan}; +use sc_telemetry::Telemetry; type FullClient = sc_service::TFullClient; type FullBackend = sc_service::TFullBackend; @@ -75,7 +75,10 @@ pub fn new_partial(config: &Configuration) -> Result), select_chain.clone(), + client.clone(), + &(client.clone() as Arc<_>), + select_chain.clone(), + telemetry.clone(), )?; let justification_import = grandpa_block_import.clone(); @@ -97,6 +100,7 @@ pub fn new_partial(config: &Configuration) -> Result Result<( - TaskManager, RpcHandlers, Option, Arc, + TaskManager, RpcHandlers, Option, Arc, Arc::Hash>>, Arc>> ), ServiceError> { @@ -387,6 +390,7 @@ pub fn new_light_base(mut config: Configuration) -> Result<( client.clone(), &(client.clone() as Arc<_>), select_chain.clone(), + telemetry.clone(), // TODO hmm how do I get the telemetry here... )?; let justification_import = grandpa_block_import.clone(); @@ -408,6 +412,7 @@ pub fn new_light_base(mut config: Configuration) -> Result<( &task_manager.spawn_handle(), config.prometheus_registry(), sp_consensus::NeverCanAuthor, + telemetry.clone(), // TODO hmm how do I get the telemetry here... )?; let (network, network_status_sinks, system_rpc_tx, network_starter) = @@ -437,10 +442,7 @@ pub fn new_light_base(mut config: Configuration) -> Result<( let rpc_extensions = node_rpc::create_light(light_deps); - let telemetry_span = TelemetrySpan::new(); - let _telemetry_span_entered = telemetry_span.enter(); - - let (rpc_handlers, telemetry_connection_notifier) = + let (rpc_handlers, telemetry) = sc_service::spawn_tasks(sc_service::SpawnTasksParams { on_demand: Some(on_demand), remote_blockchain: Some(backend.remote_blockchain()), @@ -451,13 +453,12 @@ pub fn new_light_base(mut config: Configuration) -> Result<( config, backend, network_status_sinks, system_rpc_tx, network: network.clone(), task_manager: &mut task_manager, - telemetry_span: Some(telemetry_span.clone()), })?; Ok(( task_manager, rpc_handlers, - telemetry_connection_notifier, + telemetry, client, network, transaction_pool, From 5fbc85c18f1016094e409c9688c934567a8d6f10 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 13:45:17 +0100 Subject: [PATCH 12/92] WIP --- bin/node/cli/src/service.rs | 19 ++-- client/service/src/builder.rs | 38 +++++--- client/service/src/client/client.rs | 23 +++-- client/service/src/client/light.rs | 3 + client/telemetry/src/lib.rs | 132 +++++++++++++++++----------- 5 files changed, 137 insertions(+), 78 deletions(-) diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index 014b2e7246a69..fb1d18aee31cc 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -34,7 +34,7 @@ use sp_runtime::traits::Block as BlockT; use futures::prelude::*; use sc_client_api::{ExecutorProvider, RemoteBackend}; use node_executor::Executor; -use sc_telemetry::Telemetry; +use sc_telemetry::ClientTelemetry; type FullClient = sc_service::TFullClient; type FullBackend = sc_service::TFullBackend; @@ -74,6 +74,7 @@ pub fn new_partial(config: &Configuration) -> Result), @@ -222,6 +223,7 @@ pub fn new_full_base( ); } + let telemetry = client.telemetry(); let role = config.role.clone(); let force_authoring = config.force_authoring; let backoff_authoring_blocks = @@ -230,7 +232,7 @@ pub fn new_full_base( let enable_grandpa = !config.disable_grandpa; let prometheus_registry = config.prometheus_registry().cloned(); - let (_rpc_handlers, telemetry) = sc_service::spawn_tasks( + let _rpc_handlers = sc_service::spawn_tasks( sc_service::SpawnTasksParams { config, backend: backend.clone(), @@ -367,7 +369,9 @@ pub fn new_full(config: Configuration) } pub fn new_light_base(mut config: Configuration) -> Result<( - TaskManager, RpcHandlers, Option, Arc, + TaskManager, + RpcHandlers, + Arc, Arc::Hash>>, Arc>> ), ServiceError> { @@ -386,11 +390,13 @@ pub fn new_light_base(mut config: Configuration) -> Result<( on_demand.clone(), )); + let telemetry = client.telemetry(); + let (grandpa_block_import, _) = grandpa::block_import( client.clone(), &(client.clone() as Arc<_>), select_chain.clone(), - telemetry.clone(), // TODO hmm how do I get the telemetry here... + telemetry.clone(), )?; let justification_import = grandpa_block_import.clone(); @@ -442,7 +448,7 @@ pub fn new_light_base(mut config: Configuration) -> Result<( let rpc_extensions = node_rpc::create_light(light_deps); - let (rpc_handlers, telemetry) = + let rpc_handlers = sc_service::spawn_tasks(sc_service::SpawnTasksParams { on_demand: Some(on_demand), remote_blockchain: Some(backend.remote_blockchain()), @@ -458,7 +464,6 @@ pub fn new_light_base(mut config: Configuration) -> Result<( Ok(( task_manager, rpc_handlers, - telemetry, client, network, transaction_pool, @@ -467,7 +472,7 @@ pub fn new_light_base(mut config: Configuration) -> Result<( /// Builds a new service for a light client. pub fn new_light(config: Configuration) -> Result { - new_light_base(config).map(|(task_manager, _, _, _, _, _)| { + new_light_base(config).map(|(task_manager, _, _, _, _)| { task_manager }) } diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index ef9c88764cfca..73abcce8d40a2 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -343,6 +343,11 @@ pub fn new_full_parts( Some(keystore_container.sync_keystore()), ); + let telemetry = match (config.telemetry_handle.clone(), config.telemetry_endpoints.clone()) { + (Some(mut handle), Some(endpoints)) => Some(handle.new_telemetry(endpoints)), + _ => None, + }; + new_client( db_config, executor, @@ -352,6 +357,7 @@ pub fn new_full_parts( extensions, Box::new(task_manager.spawn_handle()), config.prometheus_config.as_ref().map(|config| config.registry.clone()), + telemetry, ClientConfig { offchain_worker_enabled : config.offchain_worker.enabled, offchain_indexing_api: config.offchain_worker.indexing_enabled, @@ -409,12 +415,17 @@ pub fn new_light_parts( ); let on_demand = Arc::new(sc_network::config::OnDemand::new(fetch_checker)); let backend = sc_light::new_light_backend(light_blockchain); + let telemetry = match (config.telemetry_handle.clone(), config.telemetry_endpoints.clone()) { + (Some(mut handle), Some(endpoints)) => Some(handle.new_telemetry(endpoints)), + _ => None, + }; let client = Arc::new(light::new_light( backend.clone(), config.chain_spec.as_storage_builder(), executor, Box::new(task_manager.spawn_handle()), config.prometheus_config.as_ref().map(|config| config.registry.clone()), + telemetry, )?); Ok((client, backend, keystore_container, task_manager, on_demand)) @@ -430,6 +441,7 @@ pub fn new_client( execution_extensions: ExecutionExtensions, spawn_handle: Box, prometheus_registry: Option, + telemetry: Option, config: ClientConfig, ) -> Result<( crate::client::Client< @@ -459,6 +471,7 @@ pub fn new_client( bad_blocks, execution_extensions, prometheus_registry, + telemetry, config, )?, backend, @@ -538,7 +551,7 @@ pub fn build_offchain_workers( /// Spawn the tasks that are required to run a node. pub fn spawn_tasks( params: SpawnTasksParams, -) -> Result<(RpcHandlers, Option), Error> +) -> Result where TCl: ProvideRuntimeApi + HeaderMetadata + Chain + BlockBackend + BlockIdTo + ProofProvider + @@ -579,14 +592,15 @@ pub fn spawn_tasks( config.dev_key_seed.clone().map(|s| vec![s]).unwrap_or_default(), ).map_err(|e| Error::Application(Box::new(e)))?; - let telemetry = init_telemetry( - &mut config, - network.clone(), - client.clone(), - ); + let telemetry = client.telemetry(); if let Some(telemetry) = telemetry.clone() { - client.set_telemetry(telemetry); + init_telemetry( + &mut config, + network.clone(), + client.clone(), + telemetry.clone(), + ); } info!("📦 Highest known block at #{}", chain_info.best_number); @@ -657,7 +671,7 @@ pub fn spawn_tasks( task_manager.keep_alive((config.base_path, rpc, rpc_handlers.clone())); - Ok((rpc_handlers, telemetry)) + Ok(rpc_handlers) } async fn transaction_notifications( @@ -689,8 +703,8 @@ fn init_telemetry>( config: &mut Configuration, network: Arc::Hash>>, client: Arc, -) -> Option { - let endpoints = config.telemetry_endpoints.clone()?; + mut telemetry: Telemetry, +) { let genesis_hash = client.block_hash(Zero::zero()).ok().flatten().unwrap_or_default(); let connection_message = ConnectionMessage { name: config.network.node_name.to_owned(), @@ -706,9 +720,7 @@ fn init_telemetry>( network_id: network.local_peer_id().to_base58(), }; - config.telemetry_handle - .as_mut() - .map(|handle| handle.start_telemetry(endpoints, connection_message)) + telemetry.start_telemetry(connection_message) } fn gen_handler( diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index 5435129a689e7..c2bacfee8d806 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -115,7 +115,7 @@ pub struct Client where Block: BlockT { block_rules: BlockRules, execution_extensions: ExecutionExtensions, config: ClientConfig, - telemetry: RwLock>, + telemetry: Option, _phantom: PhantomData, } @@ -153,6 +153,7 @@ pub fn new_in_mem( genesis_storage: &S, keystore: Option, prometheus_registry: Option, + telemetry: Option, spawn_handle: Box, config: ClientConfig, ) -> sp_blockchain::Result( keystore, spawn_handle, prometheus_registry, + telemetry, config, ) } @@ -197,6 +199,7 @@ pub fn new_with_backend( keystore: Option, spawn_handle: Box, prometheus_registry: Option, + telemetry: Option, config: ClientConfig, ) -> sp_blockchain::Result, Block, RA>> where @@ -215,6 +218,7 @@ pub fn new_with_backend( Default::default(), extensions, prometheus_registry, + telemetry, config, ) } @@ -295,6 +299,7 @@ impl Client where bad_blocks: BadBlocks, execution_extensions: ExecutionExtensions, prometheus_registry: Option, + telemetry: Option, config: ClientConfig, ) -> sp_blockchain::Result { if backend.blockchain().header(BlockId::Number(Zero::zero()))?.is_none() { @@ -327,7 +332,7 @@ impl Client where block_rules: BlockRules::new(fork_blocks, bad_blocks), execution_extensions, config, - telemetry: RwLock::new(None), + telemetry, _phantom: Default::default(), }) } @@ -671,7 +676,7 @@ impl Client where rand::thread_rng().gen_bool(0.1) { telemetry!( - self.telemetry.read().clone(); SUBSTRATE_INFO; "block.import"; + self.telemetry.clone(); SUBSTRATE_INFO; "block.import"; "height" => height, "best" => ?hash, "origin" => ?origin @@ -988,11 +993,11 @@ impl Client where let header = self.header(&BlockId::Hash(*last))? .expect( "Header already known to exist in DB because it is \ - indicated in the tree route; qed" + indicated in the tree route; qed" ); telemetry!( - self.telemetry.read().clone(); SUBSTRATE_INFO; "notify.finalized"; + self.telemetry.clone(); SUBSTRATE_INFO; "notify.finalized"; "height" => format!("{}", header.number()), "best" => ?last, ); @@ -1002,7 +1007,7 @@ impl Client where let header = self.header(&BlockId::Hash(finalized_hash))? .expect( "Header already known to exist in DB because it is \ - indicated in the tree route; qed" + indicated in the tree route; qed" ); let notification = FinalityNotification { @@ -2006,9 +2011,11 @@ impl sp_consensus::block_validation::Chain for Client ClientTelemetry for Client where + BE: backend::Backend, + E: CallExecutor, B: BlockT, { - fn set_telemetry(&self, telemetry: Telemetry) { - *self.telemetry.write() = Some(telemetry); + fn telemetry(&self) -> Option { + self.telemetry.clone() } } diff --git a/client/service/src/client/light.rs b/client/service/src/client/light.rs index 1020b0e52b2c9..108ec46fb0433 100644 --- a/client/service/src/client/light.rs +++ b/client/service/src/client/light.rs @@ -22,6 +22,7 @@ use std::sync::Arc; use sc_executor::RuntimeInfo; use sp_core::traits::{CodeExecutor, SpawnNamed}; +use sc_telemetry::Telemetry; use sp_runtime::BuildStorage; use sp_runtime::traits::{Block as BlockT, HashFor}; use sp_blockchain::Result as ClientResult; @@ -38,6 +39,7 @@ pub fn new_light( code_executor: E, spawn_handle: Box, prometheus_registry: Option, + telemetry: Option, ) -> ClientResult< Client< Backend>, @@ -69,6 +71,7 @@ pub fn new_light( Default::default(), Default::default(), prometheus_registry, + telemetry, ClientConfig::default(), ) } diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index fa173bc4bdce3..df122c5d4c9a4 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -41,8 +41,9 @@ use global_counter::primitive::exact::CounterU64; use libp2p::Multiaddr; use log::{error, warn}; use serde::Serialize; -use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver}; +use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; use std::collections::HashMap; +use std::mem; use std::io; pub use libp2p::wasm_ext::ExtTransport; @@ -162,6 +163,7 @@ impl TelemetryWorker { let mut node_map: HashMap> = HashMap::new(); let mut node_pool: HashMap = HashMap::new(); + let mut pending_connection_notifications: Vec<_> = Vec::new(); loop { futures::select! { @@ -174,6 +176,7 @@ impl TelemetryWorker { init_payload, &mut node_pool, &mut node_map, + &mut pending_connection_notifications, transport.clone(), ).await, } @@ -184,6 +187,7 @@ impl TelemetryWorker { input: Option, node_pool: &mut HashMap>, node_map: &mut HashMap>, + pending_connection_notifications: &mut Vec<(Multiaddr, TracingUnboundedSender<()>)>, transport: WsTrans, ) { let input = input.expect("the stream is never closed; qed"); @@ -233,6 +237,16 @@ impl TelemetryWorker { }); node.connection_messages.extend(connection_message.clone()); + + let (matching, rest): (Vec<_>, Vec<_>) = + mem::take(pending_connection_notifications) + .into_iter().partition( + |(addr_b, _)| *addr_b == addr + ); + node.telemetry_connection_notifier.extend( + matching.into_iter().map(|(_, x)| x.clone()) + ); + let _ = mem::replace(pending_connection_notifications, rest); } } Register::Notifier { @@ -240,15 +254,15 @@ impl TelemetryWorker { connection_notifier, } => { for addr in addresses { + // If the Node has been initialized, we directly push the connection_notifier. + // Otherwise we push it to a queue that will be consumed when the connection + // initializes, thus ensuring that the connection notifier will be sent to the + // Node when it becomes available. if let Some(node) = node_pool.get_mut(&addr) { node.telemetry_connection_notifier .push(connection_notifier.clone()); } else { - log::error!( - target: "telemetry", - "Received connection notifier for unknown node ({}). This is a bug.", - addr, - ); + pending_connection_notifications.push((addr, connection_notifier.clone())); } } } @@ -324,51 +338,19 @@ pub struct TelemetryHandle { } impl TelemetryHandle { - /// Initialize the telemetry with the endpoints provided in argument for the current substrate - /// node. - /// - /// This method must be called during the substrate node initialization. - /// - /// The `endpoints` argument is a collection of telemetry WebSocket servers with a corresponding - /// verbosity level. - /// - /// The `connection_message` argument is a JSON object that is sent every time the connection - /// (re-)establishes. - pub fn start_telemetry( - &mut self, - endpoints: TelemetryEndpoints, - connection_message: ConnectionMessage, - ) -> Telemetry { - let Self { - message_sender, - register_sender, - id_generator, - } = self; - - let connection_notifier = TelemetryConnectionNotifier { - register_sender: register_sender.clone(), - addresses: endpoints.0.iter().map(|(addr, _)| addr.clone()).collect(), - }; - - let id = id_generator.next(); - match register_sender.unbounded_send(Register::Telemetry { - id, - endpoints, - connection_message, - }) { - Ok(()) => {} - Err(err) => error!( - target: "telemetry", - "Could not initialize telemetry: \ - the telemetry is probably already running: {}", - err, - ), - } + /// TODO + pub fn new_telemetry(&mut self, endpoints: TelemetryEndpoints) -> Telemetry { + let addresses = endpoints.0.iter().map(|(addr, _)| addr.clone()).collect(); Telemetry { - message_sender: message_sender.clone(), - id, - connection_notifier, + message_sender: self.message_sender.clone(), + register_sender: self.register_sender.clone(), + id: self.id_generator.next(), + connection_notifier: TelemetryConnectionNotifier { + register_sender: self.register_sender.clone(), + addresses, + }, + endpoints: Some(endpoints), } } } @@ -377,8 +359,10 @@ impl TelemetryHandle { #[derive(Debug, Clone)] pub struct Telemetry { message_sender: mpsc::Sender, + register_sender: mpsc::UnboundedSender, id: Id, connection_notifier: TelemetryConnectionNotifier, + endpoints: Option, } impl Telemetry { @@ -398,6 +382,54 @@ impl Telemetry { pub fn on_connect_stream(&self) -> TracingUnboundedReceiver<()> { self.connection_notifier.on_connect_stream() } + + /// Initialize the telemetry with the endpoints provided in argument for the current substrate + /// node. + /// + /// This method must be called during the substrate node initialization. + /// + /// The `endpoints` argument is a collection of telemetry WebSocket servers with a corresponding + /// verbosity level. + /// + /// The `connection_message` argument is a JSON object that is sent every time the connection + /// (re-)establishes. + pub fn start_telemetry( + &mut self, + connection_message: ConnectionMessage, + ) { + let Self { + message_sender: _, + register_sender, + id, + connection_notifier: _, + endpoints, + } = self; + + let endpoints = match endpoints.take() { + Some(x) => x, + None => { + error!( + target: "telemetry", + "This telemetry instance has already been initialized!", + ); + return; + } + }; + + match register_sender.unbounded_send(Register::Telemetry { + id: *id, + endpoints, + connection_message, + }) { + Ok(()) => {} + Err(err) => error!( + target: "telemetry", + "Could not initialize telemetry: \ + the telemetry is probably already running: {}", + err, + ) + } + } } /// Used to create a stream of events with only one event: when a telemetry connection @@ -532,5 +564,5 @@ impl IdGenerator { /// TODO pub trait ClientTelemetry { /// TODO - fn set_telemetry(&self, telemetry: Telemetry); + fn telemetry(&self) -> Option; } From e06d06d87701fd0ec5c74660a7e039b84c63e2a6 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 13:58:40 +0100 Subject: [PATCH 13/92] Rename TelemetryHandle to TelemetryWorkerHandle --- bin/node/cli/src/browser.rs | 4 ++-- client/cli/src/config.rs | 8 ++++---- client/cli/src/lib.rs | 6 +++--- client/cli/src/runner.rs | 10 +++++----- client/service/src/builder.rs | 4 ++-- client/service/src/config.rs | 2 +- client/service/test/src/lib.rs | 2 +- client/telemetry/src/lib.rs | 14 +++++++------- utils/browser/src/lib.rs | 6 +++--- 9 files changed, 28 insertions(+), 28 deletions(-) diff --git a/bin/node/cli/src/browser.rs b/bin/node/cli/src/browser.rs index 6c0a2f10d95e5..4f20ef7c7e570 100644 --- a/bin/node/cli/src/browser.rs +++ b/bin/node/cli/src/browser.rs @@ -44,10 +44,10 @@ async fn start_inner( None => crate::chain_spec::development_config(), }; - let telemetry_handle = telemetry_worker.handle(); + let telemetry_worker_handle = telemetry_worker.handle(); let config = browser_configuration( chain_spec, - Some(telemetry_handle), + Some(telemetry_worker_handle), ).await?; info!("Substrate browser node"); diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index 748e3b1012695..45fc6da0defa2 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -33,7 +33,7 @@ use sc_service::config::{ TaskExecutor, TelemetryEndpoints, TransactionPoolOptions, WasmExecutionMethod, }; use sc_service::{ChainSpec, TracingReceiver, KeepBlocks, TransactionStorageMode}; -use sc_telemetry::TelemetryHandle; +use sc_telemetry::TelemetryWorkerHandle; use sc_tracing::logging::LoggerBuilder; use std::net::SocketAddr; use std::path::PathBuf; @@ -470,7 +470,7 @@ pub trait CliConfiguration: Sized { &self, cli: &C, task_executor: TaskExecutor, - telemetry_handle: Option, + telemetry_worker_handle: Option, ) -> Result { let is_dev = self.is_dev()?; let chain_id = self.chain_id(is_dev)?; @@ -488,7 +488,7 @@ pub trait CliConfiguration: Sized { let max_runtime_instances = self.max_runtime_instances()?.unwrap_or(8); let is_validator = role.is_authority(); let (keystore_remote, keystore) = self.keystore_config(&config_dir)?; - let telemetry_endpoints = telemetry_handle + let telemetry_endpoints = telemetry_worker_handle .as_ref() .and_then(|_| self.telemetry_endpoints(&chain_spec).transpose()) .transpose()? @@ -548,7 +548,7 @@ pub trait CliConfiguration: Sized { role, base_path: Some(base_path), informant_output_format: Default::default(), - telemetry_handle, + telemetry_worker_handle, }) } diff --git a/client/cli/src/lib.rs b/client/cli/src/lib.rs index 602c53272ea59..dec71fd43bc82 100644 --- a/client/cli/src/lib.rs +++ b/client/cli/src/lib.rs @@ -37,7 +37,7 @@ pub use params::*; pub use runner::*; pub use sc_service::{ChainSpec, Role}; use sc_service::{Configuration, TaskExecutor}; -use sc_telemetry::TelemetryHandle; +use sc_telemetry::TelemetryWorkerHandle; pub use sc_tracing::logging::LoggerBuilder; pub use sp_version::RuntimeVersion; use std::io::Write; @@ -214,9 +214,9 @@ pub trait SubstrateCli: Sized { &self, command: &T, task_executor: TaskExecutor, - telemetry_handle: Option, + telemetry_worker_handle: Option, ) -> error::Result { - command.create_configuration(self, task_executor, telemetry_handle) + command.create_configuration(self, task_executor, telemetry_worker_handle) } /// Create a runner for the command provided in argument. This will create a Configuration and diff --git a/client/cli/src/runner.rs b/client/cli/src/runner.rs index 61a7fe9b01454..4d72606de32cf 100644 --- a/client/cli/src/runner.rs +++ b/client/cli/src/runner.rs @@ -25,7 +25,7 @@ use futures::select; use futures::{future, future::FutureExt, Future}; use log::info; use sc_service::{Configuration, TaskType, TaskManager}; -use sc_telemetry::{TelemetryHandle, TelemetryWorker}; +use sc_telemetry::{TelemetryWorkerHandle, TelemetryWorker}; use sp_utils::metrics::{TOKIO_THREADS_ALIVE, TOKIO_THREADS_TOTAL}; use std::marker::PhantomData; use sc_service::Error as ServiceError; @@ -138,13 +138,13 @@ impl Runner { } }; - let telemetry_handle = telemetry_worker.handle(); + let telemetry_worker_handle = telemetry_worker.handle(); Ok(Runner { config: command.create_configuration( cli, task_executor.into(), - Some(telemetry_handle), + Some(telemetry_worker_handle), )?, tokio_runtime, telemetry_worker, @@ -237,10 +237,10 @@ impl Runner { &mut self.config } - /// Get a new [`TelemetryHandle`]. + /// Get a new [`TelemetryWorkerHandle`]. /// /// This is used when you want to register with the [`TelemetryWorker`]. - pub fn telemetry_handle(&self) -> TelemetryHandle { + pub fn telemetry_worker_handle(&self) -> TelemetryWorkerHandle { self.telemetry_worker.handle() } } diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 73abcce8d40a2..c63676f5c2459 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -343,7 +343,7 @@ pub fn new_full_parts( Some(keystore_container.sync_keystore()), ); - let telemetry = match (config.telemetry_handle.clone(), config.telemetry_endpoints.clone()) { + let telemetry = match (config.telemetry_worker_handle.clone(), config.telemetry_endpoints.clone()) { (Some(mut handle), Some(endpoints)) => Some(handle.new_telemetry(endpoints)), _ => None, }; @@ -415,7 +415,7 @@ pub fn new_light_parts( ); let on_demand = Arc::new(sc_network::config::OnDemand::new(fetch_checker)); let backend = sc_light::new_light_backend(light_blockchain); - let telemetry = match (config.telemetry_handle.clone(), config.telemetry_endpoints.clone()) { + let telemetry = match (config.telemetry_worker_handle.clone(), config.telemetry_endpoints.clone()) { (Some(mut handle), Some(endpoints)) => Some(handle.new_telemetry(endpoints)), _ => None, }; diff --git a/client/service/src/config.rs b/client/service/src/config.rs index 4f0d426bdba42..f683eefed1a58 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -100,7 +100,7 @@ pub struct Configuration { /// /// This is a handle to a `TelemetryWorker` instance. It is used to initialize the telemetry for /// a substrate node. - pub telemetry_handle: Option, + pub telemetry_worker_handle: Option, /// The default number of 64KB pages to allocate for Wasm execution pub default_heap_pages: Option, /// Should offchain workers be executed. diff --git a/client/service/test/src/lib.rs b/client/service/test/src/lib.rs index 6c99f83d4c517..e92efa0b119c2 100644 --- a/client/service/test/src/lib.rs +++ b/client/service/test/src/lib.rs @@ -267,7 +267,7 @@ fn node_config TelemetryHandle { - TelemetryHandle { + pub fn handle(&self) -> TelemetryWorkerHandle { + TelemetryWorkerHandle { message_sender: self.message_sender.clone(), register_sender: self.register_sender.clone(), id_generator: self.id_generator.clone(), @@ -331,13 +331,13 @@ impl TelemetryWorker { /// Handle to the [`TelemetryWorker`] thats allows initializing the telemetry for a Substrate node. #[derive(Debug, Clone)] -pub struct TelemetryHandle { +pub struct TelemetryWorkerHandle { message_sender: mpsc::Sender, register_sender: mpsc::UnboundedSender, id_generator: IdGenerator, } -impl TelemetryHandle { +impl TelemetryWorkerHandle { /// TODO pub fn new_telemetry(&mut self, endpoints: TelemetryEndpoints) -> Telemetry { let addresses = endpoints.0.iter().map(|(addr, _)| addr.clone()).collect(); @@ -378,7 +378,7 @@ impl Telemetry { /// Get event stream for telemetry connection established events. /// /// This function will return an error if the telemetry has already been started by - /// [`TelemetryHandle::start_telemetry`]. + /// [`TelemetryWorkerHandle::start_telemetry`]. pub fn on_connect_stream(&self) -> TracingUnboundedReceiver<()> { self.connection_notifier.on_connect_stream() } diff --git a/utils/browser/src/lib.rs b/utils/browser/src/lib.rs index b72f2e973b682..bed4c83024dfe 100644 --- a/utils/browser/src/lib.rs +++ b/utils/browser/src/lib.rs @@ -24,7 +24,7 @@ use sc_service::{ GenericChainSpec, RuntimeGenesis, KeepBlocks, TransactionStorageMode, }; -use sc_telemetry::TelemetryHandle; +use sc_telemetry::TelemetryWorkerHandle; use sc_tracing::logging::LoggerBuilder; use wasm_bindgen::prelude::*; use futures::{ @@ -51,7 +51,7 @@ pub fn init_logging_and_telemetry( /// This configuration contains good defaults for a browser light client. pub async fn browser_configuration( chain_spec: GenericChainSpec, - telemetry_handle: Option, + telemetry_worker_handle: Option, ) -> Result> where G: RuntimeGenesis + 'static, @@ -82,7 +82,7 @@ where async {} }).into(), telemetry_external_transport: Some(transport), - telemetry_handle, + telemetry_worker_handle, role: Role::Light, database: { info!("Opening Indexed DB database '{}'...", name); From e3a4c643f6677e566109b48a50e28ee1ed924eb3 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 14:09:46 +0100 Subject: [PATCH 14/92] Rename Telemetry to TelemetryHandle --- bin/node-template/node/src/chain_spec.rs | 4 ++-- .../basic-authorship/src/basic_authorship.rs | 8 ++++---- client/chain-spec/src/chain_spec.rs | 2 +- client/chain-spec/src/lib.rs | 2 +- client/cli/src/commands/run_cmd.rs | 2 +- client/consensus/babe/src/lib.rs | 10 +++++----- client/consensus/slots/src/lib.rs | 10 +++++----- client/finality-grandpa/src/authorities.rs | 6 +++--- .../src/communication/gossip.rs | 6 +++--- .../finality-grandpa/src/communication/mod.rs | 20 +++++++++---------- client/finality-grandpa/src/environment.rs | 6 +++--- client/finality-grandpa/src/import.rs | 6 +++--- client/finality-grandpa/src/lib.rs | 20 +++++++++---------- client/finality-grandpa/src/observer.rs | 8 ++++---- client/service/src/builder.rs | 8 ++++---- client/service/src/client/client.rs | 12 +++++------ client/service/src/client/light.rs | 4 ++-- client/service/src/config.rs | 4 ++-- client/service/src/metrics.rs | 4 ++-- client/telemetry/src/endpoints.rs | 2 +- client/telemetry/src/lib.rs | 20 +++++++++---------- client/tracing/src/lib.rs | 2 +- 22 files changed, 83 insertions(+), 83 deletions(-) diff --git a/bin/node-template/node/src/chain_spec.rs b/bin/node-template/node/src/chain_spec.rs index c5451e81f20c1..b8a1dd17aef90 100644 --- a/bin/node-template/node/src/chain_spec.rs +++ b/bin/node-template/node/src/chain_spec.rs @@ -66,7 +66,7 @@ pub fn development_config() -> Result { ), // Bootnodes vec![], - // Telemetry + // TelemetryHandle None, // Protocol ID None, @@ -114,7 +114,7 @@ pub fn local_testnet_config() -> Result { ), // Bootnodes vec![], - // Telemetry + // TelemetryHandle None, // Protocol ID None, diff --git a/client/basic-authorship/src/basic_authorship.rs b/client/basic-authorship/src/basic_authorship.rs index 1a6e064b1f018..0c9c4a98d70fe 100644 --- a/client/basic-authorship/src/basic_authorship.rs +++ b/client/basic-authorship/src/basic_authorship.rs @@ -32,7 +32,7 @@ use sp_runtime::{ traits::{Block as BlockT, Hash as HashT, Header as HeaderT, DigestFor, BlakeTwo256}, }; use sp_transaction_pool::{TransactionPool, InPoolTransaction}; -use sc_telemetry::{telemetry, Telemetry, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO}; use sc_block_builder::{BlockBuilderApi, BlockBuilderProvider}; use sp_api::{ProvideRuntimeApi, ApiExt}; use futures::{future, future::{Future, FutureExt}, channel::oneshot, select}; @@ -61,7 +61,7 @@ pub struct ProposerFactory { /// Prometheus Link, metrics: PrometheusMetrics, max_block_size: usize, - telemetry: Option, + telemetry: Option, /// phantom member to pin the `Backend` type. _phantom: PhantomData, } @@ -72,7 +72,7 @@ impl ProposerFactory { client: Arc, transaction_pool: Arc, prometheus: Option<&PrometheusRegistry>, - telemetry: Option, + telemetry: Option, ) -> Self { ProposerFactory { spawn_handle: Box::new(spawn_handle), @@ -167,7 +167,7 @@ pub struct Proposer { now: Box time::Instant + Send + Sync>, metrics: PrometheusMetrics, max_block_size: usize, - telemetry: Option, + telemetry: Option, _phantom: PhantomData, } diff --git a/client/chain-spec/src/chain_spec.rs b/client/chain-spec/src/chain_spec.rs index 2faf95568290e..09158bc986229 100644 --- a/client/chain-spec/src/chain_spec.rs +++ b/client/chain-spec/src/chain_spec.rs @@ -198,7 +198,7 @@ impl ChainSpec { &self.client_spec.id } - /// Telemetry endpoints (if any) + /// TelemetryHandle endpoints (if any) pub fn telemetry_endpoints(&self) -> &Option { &self.client_spec.telemetry_endpoints } diff --git a/client/chain-spec/src/lib.rs b/client/chain-spec/src/lib.rs index ee4f757f8cf0e..2a1b1a553ff41 100644 --- a/client/chain-spec/src/lib.rs +++ b/client/chain-spec/src/lib.rs @@ -137,7 +137,7 @@ pub trait ChainSpec: BuildStorage + Send + Sync { fn chain_type(&self) -> ChainType; /// A list of bootnode addresses. fn boot_nodes(&self) -> &[MultiaddrWithPeerId]; - /// Telemetry endpoints (if any) + /// TelemetryHandle endpoints (if any) fn telemetry_endpoints(&self) -> &Option; /// Network protocol id. fn protocol_id(&self) -> Option<&str>; diff --git a/client/cli/src/commands/run_cmd.rs b/client/cli/src/commands/run_cmd.rs index bb6f77819d7af..67303d927b96e 100644 --- a/client/cli/src/commands/run_cmd.rs +++ b/client/cli/src/commands/run_cmd.rs @@ -149,7 +149,7 @@ pub struct RunCmd { /// Disable connecting to the Substrate telemetry server. /// - /// Telemetry is on by default on global chains. + /// TelemetryHandle is on by default on global chains. #[structopt(long = "no-telemetry")] pub no_telemetry: bool, diff --git a/client/consensus/babe/src/lib.rs b/client/consensus/babe/src/lib.rs index d37dc2a13a7bb..9d94d346f88f0 100644 --- a/client/consensus/babe/src/lib.rs +++ b/client/consensus/babe/src/lib.rs @@ -89,7 +89,7 @@ use sp_runtime::{ use sp_api::{ProvideRuntimeApi, NumberFor}; use parking_lot::Mutex; use sp_inherents::{InherentDataProviders, InherentData}; -use sc_telemetry::{telemetry, Telemetry, CONSENSUS_TRACE, CONSENSUS_DEBUG}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_TRACE, CONSENSUS_DEBUG}; use sp_consensus::{ BlockImport, Environment, Proposer, BlockCheckParams, ForkChoiceStrategy, BlockImportParams, BlockOrigin, Error as ConsensusError, @@ -394,8 +394,8 @@ pub struct BabeParams { /// Checks if the current native implementation can author with a runtime at a given block. pub can_author_with: CAW, - /// Telemetry instance. - pub telemetry: Option, + /// TelemetryHandle instance. + pub telemetry: Option, } /// Start the babe worker. @@ -847,7 +847,7 @@ pub struct BabeVerifier { epoch_changes: SharedEpochChanges, time_source: TimeSource, can_author_with: CAW, - telemetry: Option, + telemetry: Option, } impl BabeVerifier @@ -1501,7 +1501,7 @@ pub fn import_queue( spawner: &impl sp_core::traits::SpawnNamed, registry: Option<&Registry>, can_author_with: CAW, - telemetry: Option, + telemetry: Option, ) -> ClientResult> where Inner: BlockImport> + Send + Sync + 'static, diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index ef2b9e259a3f9..8d041d457985a 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -47,7 +47,7 @@ use sp_runtime::{ generic::BlockId, traits::{Block as BlockT, Header, HashFor, NumberFor} }; -use sc_telemetry::{telemetry, Telemetry, CONSENSUS_DEBUG, CONSENSUS_WARN, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG, CONSENSUS_WARN, CONSENSUS_INFO}; /// The changes that need to applied to the storage to create the state for a block. /// @@ -82,7 +82,7 @@ pub trait SlotWorker { &mut self, chain_head: B::Header, slot_info: SlotInfo, - telemetry: Option, + telemetry: Option, ) -> Self::OnSlot; } @@ -210,7 +210,7 @@ pub trait SimpleSlotWorker { &mut self, chain_head: B::Header, slot_info: SlotInfo, - mut telemetry: Option, + mut telemetry: Option, ) -> Pin>> + Send>> where >::Proposal: Unpin + Send + 'static, @@ -397,7 +397,7 @@ impl> SlotWorker for T { &mut self, chain_head: B::Header, slot_info: SlotInfo, - telemetry: Option, + telemetry: Option, ) -> Self::OnSlot { SimpleSlotWorker::on_slot(self, chain_head, slot_info, telemetry) } @@ -424,7 +424,7 @@ pub fn start_slot_worker( inherent_data_providers: InherentDataProviders, timestamp_extractor: SC, can_author_with: CAW, - telemetry: Option, + telemetry: Option, ) -> impl Future where B: BlockT, diff --git a/client/finality-grandpa/src/authorities.rs b/client/finality-grandpa/src/authorities.rs index 2273917661608..70b1ca2ceacc0 100644 --- a/client/finality-grandpa/src/authorities.rs +++ b/client/finality-grandpa/src/authorities.rs @@ -23,7 +23,7 @@ use parking_lot::RwLock; use finality_grandpa::voter_set::VoterSet; use parity_scale_codec::{Encode, Decode}; use log::debug; -use sc_telemetry::{telemetry, Telemetry, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO}; use sp_finality_grandpa::{AuthorityId, AuthorityList}; use std::cmp::Ord; @@ -418,7 +418,7 @@ where best_number: N, is_descendent_of: &F, initial_sync: bool, - mut telemetry: Option<&mut Telemetry>, + mut telemetry: Option<&mut TelemetryHandle>, ) -> Result, Error> where F: Fn(&H, &H) -> Result, @@ -514,7 +514,7 @@ where finalized_number: N, is_descendent_of: &F, initial_sync: bool, - mut telemetry: Option<&mut Telemetry>, + mut telemetry: Option<&mut TelemetryHandle>, ) -> Result, Error> where F: Fn(&H, &H) -> Result, diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index 824ec8c88c9df..71ff8bc1f1c82 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -90,7 +90,7 @@ use sc_network::{ObservedRole, PeerId, ReputationChange}; use parity_scale_codec::{Encode, Decode}; use sp_finality_grandpa::AuthorityId; -use sc_telemetry::{telemetry, Telemetry, CONSENSUS_DEBUG}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG}; use log::{trace, debug}; use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; use prometheus_endpoint::{CounterVec, Opts, PrometheusError, register, Registry, U64}; @@ -1283,7 +1283,7 @@ pub(super) struct GossipValidator { set_state: environment::SharedVoterSetState, report_sender: TracingUnboundedSender, metrics: Option, - telemetry: Option, + telemetry: Option, } impl GossipValidator { @@ -1294,7 +1294,7 @@ impl GossipValidator { config: crate::Config, set_state: environment::SharedVoterSetState, prometheus_registry: Option<&Registry>, - telemetry: Option, + telemetry: Option, ) -> (GossipValidator, TracingUnboundedReceiver) { let metrics = match prometheus_registry.map(Metrics::register) { Some(Ok(metrics)) => Some(metrics), diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index cea4a7405a775..a5b5a85a09666 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -42,7 +42,7 @@ use sc_network::{NetworkService, ReputationChange}; use sc_network_gossip::{GossipEngine, Network as GossipNetwork}; use parity_scale_codec::{Encode, Decode}; use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT, NumberFor}; -use sc_telemetry::{telemetry, Telemetry, CONSENSUS_DEBUG, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG, CONSENSUS_INFO}; use crate::{ CatchUp, Commit, CommunicationIn, CommunicationOutH, @@ -193,7 +193,7 @@ pub(crate) struct NetworkBridge> { // channel implementation. gossip_validator_report_stream: Arc>>, - telemetry: Option, + telemetry: Option, } impl> Unpin for NetworkBridge {} @@ -208,7 +208,7 @@ impl> NetworkBridge { config: crate::Config, set_state: crate::environment::SharedVoterSetState, prometheus_registry: Option<&Registry>, - telemetry: Option, + telemetry: Option, ) -> Self { let (validator, report_stream) = GossipValidator::new( config, @@ -500,7 +500,7 @@ fn incoming_global( voters: Arc>, gossip_validator: Arc>, neighbor_sender: periodic::NeighborPacketSender, - telemetry: Option, + telemetry: Option, ) -> impl Stream> { let mut process_commit = { let mut telemetry = telemetry.clone(); @@ -671,7 +671,7 @@ pub(crate) struct OutgoingMessages { sender: mpsc::Sender>, network: Arc>>, has_voted: HasVoted, - telemetry: Option, + telemetry: Option, } impl Unpin for OutgoingMessages {} @@ -773,7 +773,7 @@ fn check_compact_commit( voters: &VoterSet, round: Round, set_id: SetId, - mut telemetry: Option<&mut Telemetry>, + mut telemetry: Option<&mut TelemetryHandle>, ) -> Result<(), ReputationChange> { // 4f + 1 = equivocations from f voters. let f = voters.total_weight() - voters.threshold(); @@ -838,7 +838,7 @@ fn check_catch_up( msg: &CatchUp, voters: &VoterSet, set_id: SetId, - telemetry: Option, + telemetry: Option, ) -> Result<(), ReputationChange> { // 4f + 1 = equivocations from f voters. let f = voters.total_weight() - voters.threshold(); @@ -889,7 +889,7 @@ fn check_catch_up( set_id: SetIdNumber, mut signatures_checked: usize, buf: &mut Vec, - mut telemetry: Option, + mut telemetry: Option, ) -> Result where B: BlockT, I: Iterator, &'a AuthorityId, &'a AuthoritySignature)>, @@ -957,7 +957,7 @@ struct CommitsOut { is_voter: bool, gossip_validator: Arc>, neighbor_sender: periodic::NeighborPacketSender, - telemetry: Option, + telemetry: Option, } impl CommitsOut { @@ -968,7 +968,7 @@ impl CommitsOut { is_voter: bool, gossip_validator: Arc>, neighbor_sender: periodic::NeighborPacketSender, - telemetry: Option, + telemetry: Option, ) -> Self { CommitsOut { network, diff --git a/client/finality-grandpa/src/environment.rs b/client/finality-grandpa/src/environment.rs index c643872ae336a..ddd66742052c2 100644 --- a/client/finality-grandpa/src/environment.rs +++ b/client/finality-grandpa/src/environment.rs @@ -39,7 +39,7 @@ use sp_runtime::generic::BlockId; use sp_runtime::traits::{ Block as BlockT, Header as HeaderT, NumberFor, Zero, }; -use sc_telemetry::{telemetry, Telemetry, CONSENSUS_DEBUG, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG, CONSENSUS_INFO}; use crate::{ local_authority_id, CommandOrError, Commit, Config, Error, NewAuthoritySet, Precommit, Prevote, @@ -445,7 +445,7 @@ pub(crate) struct Environment, SC, pub(crate) voting_rule: VR, pub(crate) metrics: Option, pub(crate) justification_sender: Option>, - pub(crate) telemetry: Option, + pub(crate) telemetry: Option, pub(crate) _phantom: PhantomData, } @@ -1214,7 +1214,7 @@ pub(crate) fn finalize_block( justification_or_commit: JustificationOrCommit, initial_sync: bool, justification_sender: Option<&GrandpaJustificationSender>, - telemetry: Option, + telemetry: Option, ) -> Result<(), CommandOrError>> where Block: BlockT, diff --git a/client/finality-grandpa/src/import.rs b/client/finality-grandpa/src/import.rs index 2643e486b4de8..9126047439d82 100644 --- a/client/finality-grandpa/src/import.rs +++ b/client/finality-grandpa/src/import.rs @@ -24,7 +24,7 @@ use parking_lot::RwLockWriteGuard; use sp_blockchain::{BlockStatus, well_known_cache_keys}; use sc_client_api::{backend::Backend, utils::is_descendent_of}; -use sc_telemetry::Telemetry; +use sc_telemetry::TelemetryHandle; use sp_utils::mpsc::TracingUnboundedSender; use sp_api::TransactionFor; @@ -63,7 +63,7 @@ pub struct GrandpaBlockImport { send_voter_commands: TracingUnboundedSender>>, authority_set_hard_forks: HashMap>>, justification_sender: GrandpaJustificationSender, - telemetry: Option, + telemetry: Option, _phantom: PhantomData, } @@ -565,7 +565,7 @@ impl GrandpaBlockImport>>, authority_set_hard_forks: Vec<(SetId, PendingChange>)>, justification_sender: GrandpaJustificationSender, - telemetry: Option, + telemetry: Option, ) -> GrandpaBlockImport { // check for and apply any forced authority set hard fork that applies // to the *current* authority set. diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index 26ce12605f45e..40e85607ac1e5 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -79,7 +79,7 @@ use sp_core::{ use sp_keystore::{SyncCryptoStorePtr, SyncCryptoStore}; use sp_application_crypto::AppKey; use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver}; -use sc_telemetry::{telemetry, Telemetry, CONSENSUS_INFO, CONSENSUS_DEBUG}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO, CONSENSUS_DEBUG}; use parking_lot::RwLock; use finality_grandpa::Error as GrandpaError; @@ -271,8 +271,8 @@ pub struct Config { pub name: Option, /// The keystore that manages the keys of this node. pub keystore: Option, - /// Telemetry instance. - pub telemetry: Option, + /// TelemetryHandle instance. + pub telemetry: Option, } impl Config { @@ -454,7 +454,7 @@ pub struct LinkHalf { voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: GrandpaJustificationSender, justification_stream: GrandpaJustificationStream, - telemetry: Option, + telemetry: Option, } impl LinkHalf { @@ -505,7 +505,7 @@ pub fn block_import( client: Arc, genesis_authorities_provider: &dyn GenesisAuthoritySetProvider, select_chain: SC, - telemetry: Option, + telemetry: Option, ) -> Result< ( GrandpaBlockImport, @@ -537,7 +537,7 @@ pub fn block_import_with_authority_set_hard_forks genesis_authorities_provider: &dyn GenesisAuthoritySetProvider, select_chain: SC, authority_set_hard_forks: Vec<(SetId, (Block::Hash, NumberFor), AuthorityList)>, - telemetry: Option, + telemetry: Option, ) -> Result< ( GrandpaBlockImport, @@ -679,8 +679,8 @@ pub struct GrandpaParams { pub prometheus_registry: Option, /// The voter state is exposed at an RPC endpoint. pub shared_voter_state: SharedVoterState, - /// Telemetry instance. - pub telemetry: Option, + /// TelemetryHandle instance. + pub telemetry: Option, } /// Returns the configuration value to put in @@ -837,7 +837,7 @@ struct VoterWork, SC, VR> { env: Arc>, voter_commands_rx: TracingUnboundedReceiver>>, network: NetworkBridge, - telemetry: Option, + telemetry: Option, /// Prometheus metrics. metrics: Option, } @@ -864,7 +864,7 @@ where prometheus_registry: Option, shared_voter_state: SharedVoterState, justification_sender: GrandpaJustificationSender, - telemetry: Option, + telemetry: Option, ) -> Self { let metrics = match prometheus_registry.as_ref().map(Metrics::register) { Some(Ok(metrics)) => Some(metrics), diff --git a/client/finality-grandpa/src/observer.rs b/client/finality-grandpa/src/observer.rs index 6bf5a8efe8f6f..ce482b0e86c2e 100644 --- a/client/finality-grandpa/src/observer.rs +++ b/client/finality-grandpa/src/observer.rs @@ -29,7 +29,7 @@ use log::{debug, info, warn}; use sp_keystore::SyncCryptoStorePtr; use sp_consensus::SelectChain; use sc_client_api::backend::Backend; -use sc_telemetry::Telemetry; +use sc_telemetry::TelemetryHandle; use sp_utils::mpsc::TracingUnboundedReceiver; use sp_runtime::traits::{NumberFor, Block as BlockT}; use sp_blockchain::HeaderMetadata; @@ -68,7 +68,7 @@ fn grandpa_observer( last_finalized_number: NumberFor, commits: S, note_round: F, - telemetry: Option, + telemetry: Option, ) -> impl Future>>> where NumberFor: BlockNumberOps, @@ -216,7 +216,7 @@ struct ObserverWork> { keystore: Option, voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: Option>, - telemetry: Option, + telemetry: Option, _phantom: PhantomData, } @@ -235,7 +235,7 @@ where keystore: Option, voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: Option>, - telemetry: Option, + telemetry: Option, ) -> Self { let mut work = ObserverWork { diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index c63676f5c2459..a48b7983fe2aa 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -56,7 +56,7 @@ use sc_telemetry::{ telemetry, ClientTelemetry, ConnectionMessage, - Telemetry, + TelemetryHandle, SUBSTRATE_INFO, }; use sp_transaction_pool::MaintainedTransactionPool; @@ -441,7 +441,7 @@ pub fn new_client( execution_extensions: ExecutionExtensions, spawn_handle: Box, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, config: ClientConfig, ) -> Result<( crate::client::Client< @@ -677,7 +677,7 @@ pub fn spawn_tasks( async fn transaction_notifications( transaction_pool: Arc, network: Arc::Hash>>, - mut telemetry: Option, + mut telemetry: Option, ) where TBl: BlockT, @@ -703,7 +703,7 @@ fn init_telemetry>( config: &mut Configuration, network: Arc::Hash>>, client: Arc, - mut telemetry: Telemetry, + mut telemetry: TelemetryHandle, ) { let genesis_hash = client.block_hash(Zero::zero()).ok().flatten().unwrap_or_default(); let connection_message = ConnectionMessage { diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index c2bacfee8d806..09fd09814ff1f 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -35,7 +35,7 @@ use sp_core::{ }; #[cfg(feature="test-helpers")] use sp_keystore::SyncCryptoStorePtr; -use sc_telemetry::{telemetry, ClientTelemetry, Telemetry, SUBSTRATE_INFO}; +use sc_telemetry::{telemetry, ClientTelemetry, TelemetryHandle, SUBSTRATE_INFO}; use sp_runtime::{ Justification, BuildStorage, generic::{BlockId, SignedBlock, DigestItem}, @@ -115,7 +115,7 @@ pub struct Client where Block: BlockT { block_rules: BlockRules, execution_extensions: ExecutionExtensions, config: ClientConfig, - telemetry: Option, + telemetry: Option, _phantom: PhantomData, } @@ -153,7 +153,7 @@ pub fn new_in_mem( genesis_storage: &S, keystore: Option, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, spawn_handle: Box, config: ClientConfig, ) -> sp_blockchain::Result( keystore: Option, spawn_handle: Box, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, config: ClientConfig, ) -> sp_blockchain::Result, Block, RA>> where @@ -299,7 +299,7 @@ impl Client where bad_blocks: BadBlocks, execution_extensions: ExecutionExtensions, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, config: ClientConfig, ) -> sp_blockchain::Result { if backend.blockchain().header(BlockId::Number(Zero::zero()))?.is_none() { @@ -2015,7 +2015,7 @@ impl ClientTelemetry for Client E: CallExecutor, B: BlockT, { - fn telemetry(&self) -> Option { + fn telemetry(&self) -> Option { self.telemetry.clone() } } diff --git a/client/service/src/client/light.rs b/client/service/src/client/light.rs index 108ec46fb0433..3b29a0e1a92ca 100644 --- a/client/service/src/client/light.rs +++ b/client/service/src/client/light.rs @@ -22,7 +22,7 @@ use std::sync::Arc; use sc_executor::RuntimeInfo; use sp_core::traits::{CodeExecutor, SpawnNamed}; -use sc_telemetry::Telemetry; +use sc_telemetry::TelemetryHandle; use sp_runtime::BuildStorage; use sp_runtime::traits::{Block as BlockT, HashFor}; use sp_blockchain::Result as ClientResult; @@ -39,7 +39,7 @@ pub fn new_light( code_executor: E, spawn_handle: Box, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, ) -> ClientResult< Client< Backend>, diff --git a/client/service/src/config.rs b/client/service/src/config.rs index f683eefed1a58..9564c3129a704 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -91,12 +91,12 @@ pub struct Configuration { pub rpc_methods: RpcMethods, /// Prometheus endpoint configuration. `None` if disabled. pub prometheus_config: Option, - /// Telemetry service URL. `None` if disabled. + /// TelemetryHandle service URL. `None` if disabled. pub telemetry_endpoints: Option, /// External WASM transport for the telemetry. If `Some`, when connection to a telemetry /// endpoint, this transport will be tried in priority before all others. pub telemetry_external_transport: Option, - /// Telemetry handle. + /// TelemetryHandle handle. /// /// This is a handle to a `TelemetryWorker` instance. It is used to initialize the telemetry for /// a substrate node. diff --git a/client/service/src/metrics.rs b/client/service/src/metrics.rs index 904a1602cc109..a913af131ea15 100644 --- a/client/service/src/metrics.rs +++ b/client/service/src/metrics.rs @@ -21,7 +21,7 @@ use std::{convert::TryFrom, time::SystemTime}; use crate::{NetworkStatus, NetworkState, NetworkStatusSinks, config::Configuration}; use futures_timer::Delay; use prometheus_endpoint::{register, Gauge, U64, Registry, PrometheusError, Opts, GaugeVec}; -use sc_telemetry::{telemetry, Telemetry, SUBSTRATE_INFO}; +use sc_telemetry::{telemetry, TelemetryHandle, SUBSTRATE_INFO}; use sp_api::ProvideRuntimeApi; use sp_runtime::traits::{NumberFor, Block, SaturatedConversion, UniqueSaturatedInto}; use sp_transaction_pool::{PoolStatus, MaintainedTransactionPool}; @@ -112,7 +112,7 @@ pub struct MetricsService { last_update: Instant, last_total_bytes_inbound: u64, last_total_bytes_outbound: u64, - telemetry: Option, + telemetry: Option, } impl MetricsService { diff --git a/client/telemetry/src/endpoints.rs b/client/telemetry/src/endpoints.rs index 7d0338fb18e3c..a58934d1561ab 100644 --- a/client/telemetry/src/endpoints.rs +++ b/client/telemetry/src/endpoints.rs @@ -92,7 +92,7 @@ mod tests { ("/ip4/80.123.90.4/tcp/5432".into(), 4), ]; let telem = - TelemetryEndpoints::new(endp.clone()).expect("Telemetry endpoint should be valid"); + TelemetryEndpoints::new(endp.clone()).expect("TelemetryHandle endpoint should be valid"); let mut res: Vec<(Multiaddr, u8)> = vec![]; for (a, b) in endp.iter() { res.push(( diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index cfba252394098..cf848ab3b98c1 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -74,7 +74,7 @@ pub const CONSENSUS_INFO: VerbosityLevel = 1; static TELEMETRY_ID_COUNTER: CounterU64 = CounterU64::new(1); -/// Telemetry message verbosity. +/// TelemetryHandle message verbosity. pub type VerbosityLevel = u8; pub(crate) type Id = u64; @@ -104,7 +104,7 @@ pub struct ConnectionMessage { pub network_id: String, } -/// Telemetry worker. +/// TelemetryHandle worker. /// /// It should run as a background task using the [`TelemetryWorker::run`] method. This method /// will consume the object and any further attempts of initializing a new telemetry through its @@ -193,7 +193,7 @@ impl TelemetryWorker { let input = input.expect("the stream is never closed; qed"); match input { - Register::Telemetry { + Register::TelemetryHandle { id, endpoints, connection_message, @@ -339,10 +339,10 @@ pub struct TelemetryWorkerHandle { impl TelemetryWorkerHandle { /// TODO - pub fn new_telemetry(&mut self, endpoints: TelemetryEndpoints) -> Telemetry { + pub fn new_telemetry(&mut self, endpoints: TelemetryEndpoints) -> TelemetryHandle { let addresses = endpoints.0.iter().map(|(addr, _)| addr.clone()).collect(); - Telemetry { + TelemetryHandle { message_sender: self.message_sender.clone(), register_sender: self.register_sender.clone(), id: self.id_generator.next(), @@ -357,7 +357,7 @@ impl TelemetryWorkerHandle { /// A telemetry instance that can be used to send telemetries. #[derive(Debug, Clone)] -pub struct Telemetry { +pub struct TelemetryHandle { message_sender: mpsc::Sender, register_sender: mpsc::UnboundedSender, id: Id, @@ -365,7 +365,7 @@ pub struct Telemetry { endpoints: Option, } -impl Telemetry { +impl TelemetryHandle { /// Send telemetries. pub fn send(&mut self, verbosity: VerbosityLevel, payload: TelemetryPayload) { match self.message_sender.try_send((self.id, verbosity, payload)) { @@ -416,7 +416,7 @@ impl Telemetry { } }; - match register_sender.unbounded_send(Register::Telemetry { + match register_sender.unbounded_send(Register::TelemetryHandle { id: *id, endpoints, connection_message, @@ -460,7 +460,7 @@ impl TelemetryConnectionNotifier { #[derive(Debug)] enum Register { - Telemetry { + TelemetryHandle { id: Id, endpoints: TelemetryEndpoints, connection_message: ConnectionMessage, @@ -564,5 +564,5 @@ impl IdGenerator { /// TODO pub trait ClientTelemetry { /// TODO - fn telemetry(&self) -> Option; + fn telemetry(&self) -> Option; } diff --git a/client/tracing/src/lib.rs b/client/tracing/src/lib.rs index 5f30c4d5ffcd1..89b8c330ef411 100644 --- a/client/tracing/src/lib.rs +++ b/client/tracing/src/lib.rs @@ -24,7 +24,7 @@ //! //! See `sp-tracing` for examples on how to use tracing. //! -//! Currently we provide `Log` (default), `Telemetry` variants for `Receiver` +//! Currently we provide `Log` (default), `TelemetryHandle` variants for `Receiver` #![warn(missing_docs)] From c9a60aab7d630fad7aa61eeebcc3fc291b30fc2f Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 14:47:19 +0100 Subject: [PATCH 15/92] WIP --- client/service/src/builder.rs | 41 +++++++-------- client/service/src/client/client.rs | 31 +++++++---- client/service/src/client/light.rs | 4 +- client/telemetry/src/lib.rs | 81 ++++++++++++++++++----------- 4 files changed, 92 insertions(+), 65 deletions(-) diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index a48b7983fe2aa..39fee55abc5d7 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -56,6 +56,7 @@ use sc_telemetry::{ telemetry, ClientTelemetry, ConnectionMessage, + Telemetry, TelemetryHandle, SUBSTRATE_INFO, }; @@ -213,17 +214,17 @@ pub type TLightClientWithBackend = Client< >; trait AsCryptoStoreRef { - fn keystore_ref(&self) -> Arc; - fn sync_keystore_ref(&self) -> Arc; + fn keystore_ref(&self) -> Arc; + fn sync_keystore_ref(&self) -> Arc; } impl AsCryptoStoreRef for Arc where T: CryptoStore + SyncCryptoStore + 'static { - fn keystore_ref(&self) -> Arc { - self.clone() - } - fn sync_keystore_ref(&self) -> Arc { - self.clone() - } + fn keystore_ref(&self) -> Arc { + self.clone() + } + fn sync_keystore_ref(&self) -> Arc { + self.clone() + } } /// Construct and hold different layers of Keystore wrappers @@ -441,7 +442,7 @@ pub fn new_client( execution_extensions: ExecutionExtensions, spawn_handle: Box, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, config: ClientConfig, ) -> Result<( crate::client::Client< @@ -592,16 +593,11 @@ pub fn spawn_tasks( config.dev_key_seed.clone().map(|s| vec![s]).unwrap_or_default(), ).map_err(|e| Error::Application(Box::new(e)))?; - let telemetry = client.telemetry(); - - if let Some(telemetry) = telemetry.clone() { - init_telemetry( - &mut config, - network.clone(), - client.clone(), - telemetry.clone(), - ); - } + init_telemetry( + &mut config, + network.clone(), + client.clone(), + ); info!("📦 Highest known block at #{}", chain_info.best_number); @@ -615,7 +611,7 @@ pub fn spawn_tasks( spawn_handle.spawn( "on-transaction-imported", - transaction_notifications(transaction_pool.clone(), network.clone(), telemetry.clone()), + transaction_notifications(transaction_pool.clone(), network.clone(), client.telemetry()), ); // Prometheus metrics. @@ -699,11 +695,10 @@ async fn transaction_notifications( .await; } -fn init_telemetry>( +fn init_telemetry + ClientTelemetry>( config: &mut Configuration, network: Arc::Hash>>, client: Arc, - mut telemetry: TelemetryHandle, ) { let genesis_hash = client.block_hash(Zero::zero()).ok().flatten().unwrap_or_default(); let connection_message = ConnectionMessage { @@ -720,7 +715,7 @@ fn init_telemetry>( network_id: network.local_peer_id().to_base58(), }; - telemetry.start_telemetry(connection_message) + client.start_telemetry(connection_message) } fn gen_handler( diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index 09fd09814ff1f..c892d6d1fc45f 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -35,7 +35,14 @@ use sp_core::{ }; #[cfg(feature="test-helpers")] use sp_keystore::SyncCryptoStorePtr; -use sc_telemetry::{telemetry, ClientTelemetry, TelemetryHandle, SUBSTRATE_INFO}; +use sc_telemetry::{ + telemetry, + ClientTelemetry, + ConnectionMessage, + Telemetry, + TelemetryHandle, + SUBSTRATE_INFO, +}; use sp_runtime::{ Justification, BuildStorage, generic::{BlockId, SignedBlock, DigestItem}, @@ -115,7 +122,7 @@ pub struct Client where Block: BlockT { block_rules: BlockRules, execution_extensions: ExecutionExtensions, config: ClientConfig, - telemetry: Option, + telemetry: RwLock>, _phantom: PhantomData, } @@ -153,7 +160,7 @@ pub fn new_in_mem( genesis_storage: &S, keystore: Option, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, spawn_handle: Box, config: ClientConfig, ) -> sp_blockchain::Result( keystore: Option, spawn_handle: Box, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, config: ClientConfig, ) -> sp_blockchain::Result, Block, RA>> where @@ -299,7 +306,7 @@ impl Client where bad_blocks: BadBlocks, execution_extensions: ExecutionExtensions, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, config: ClientConfig, ) -> sp_blockchain::Result { if backend.blockchain().header(BlockId::Number(Zero::zero()))?.is_none() { @@ -332,7 +339,7 @@ impl Client where block_rules: BlockRules::new(fork_blocks, bad_blocks), execution_extensions, config, - telemetry, + telemetry: RwLock::new(telemetry), _phantom: Default::default(), }) } @@ -676,7 +683,7 @@ impl Client where rand::thread_rng().gen_bool(0.1) { telemetry!( - self.telemetry.clone(); SUBSTRATE_INFO; "block.import"; + self.telemetry(); SUBSTRATE_INFO; "block.import"; "height" => height, "best" => ?hash, "origin" => ?origin @@ -997,7 +1004,7 @@ impl Client where ); telemetry!( - self.telemetry.clone(); SUBSTRATE_INFO; "notify.finalized"; + self.telemetry(); SUBSTRATE_INFO; "notify.finalized"; "height" => format!("{}", header.number()), "best" => ?last, ); @@ -2016,6 +2023,12 @@ impl ClientTelemetry for Client B: BlockT, { fn telemetry(&self) -> Option { - self.telemetry.clone() + self.telemetry.read().as_ref().map(|x| x.handle()) + } + + fn start_telemetry(&self, connection_message: ConnectionMessage) { + if let Some(telemetry) = self.telemetry.write().as_mut() { + telemetry.start_telemetry(connection_message); + } } } diff --git a/client/service/src/client/light.rs b/client/service/src/client/light.rs index 3b29a0e1a92ca..108ec46fb0433 100644 --- a/client/service/src/client/light.rs +++ b/client/service/src/client/light.rs @@ -22,7 +22,7 @@ use std::sync::Arc; use sc_executor::RuntimeInfo; use sp_core::traits::{CodeExecutor, SpawnNamed}; -use sc_telemetry::TelemetryHandle; +use sc_telemetry::Telemetry; use sp_runtime::BuildStorage; use sp_runtime::traits::{Block as BlockT, HashFor}; use sp_blockchain::Result as ClientResult; @@ -39,7 +39,7 @@ pub fn new_light( code_executor: E, spawn_handle: Box, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, ) -> ClientResult< Client< Backend>, diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index cf848ab3b98c1..3b7159c479cc8 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -74,7 +74,7 @@ pub const CONSENSUS_INFO: VerbosityLevel = 1; static TELEMETRY_ID_COUNTER: CounterU64 = CounterU64::new(1); -/// TelemetryHandle message verbosity. +/// Telemetry message verbosity. pub type VerbosityLevel = u8; pub(crate) type Id = u64; @@ -104,7 +104,7 @@ pub struct ConnectionMessage { pub network_id: String, } -/// TelemetryHandle worker. +/// Telemetry worker. /// /// It should run as a background task using the [`TelemetryWorker::run`] method. This method /// will consume the object and any further attempts of initializing a new telemetry through its @@ -193,7 +193,7 @@ impl TelemetryWorker { let input = input.expect("the stream is never closed; qed"); match input { - Register::TelemetryHandle { + Register::Telemetry { id, endpoints, connection_message, @@ -339,10 +339,10 @@ pub struct TelemetryWorkerHandle { impl TelemetryWorkerHandle { /// TODO - pub fn new_telemetry(&mut self, endpoints: TelemetryEndpoints) -> TelemetryHandle { + pub fn new_telemetry(&mut self, endpoints: TelemetryEndpoints) -> Telemetry { let addresses = endpoints.0.iter().map(|(addr, _)| addr.clone()).collect(); - TelemetryHandle { + Telemetry { message_sender: self.message_sender.clone(), register_sender: self.register_sender.clone(), id: self.id_generator.next(), @@ -356,8 +356,8 @@ impl TelemetryWorkerHandle { } /// A telemetry instance that can be used to send telemetries. -#[derive(Debug, Clone)] -pub struct TelemetryHandle { +#[derive(Debug)] +pub struct Telemetry { message_sender: mpsc::Sender, register_sender: mpsc::UnboundedSender, id: Id, @@ -365,24 +365,7 @@ pub struct TelemetryHandle { endpoints: Option, } -impl TelemetryHandle { - /// Send telemetries. - pub fn send(&mut self, verbosity: VerbosityLevel, payload: TelemetryPayload) { - match self.message_sender.try_send((self.id, verbosity, payload)) { - Ok(()) => {} - Err(err) if err.is_full() => todo!("overflow"), - Err(_) => unreachable!(), - } - } - - /// Get event stream for telemetry connection established events. - /// - /// This function will return an error if the telemetry has already been started by - /// [`TelemetryWorkerHandle::start_telemetry`]. - pub fn on_connect_stream(&self) -> TracingUnboundedReceiver<()> { - self.connection_notifier.on_connect_stream() - } - +impl Telemetry { /// Initialize the telemetry with the endpoints provided in argument for the current substrate /// node. /// @@ -393,10 +376,7 @@ impl TelemetryHandle { /// /// The `connection_message` argument is a JSON object that is sent every time the connection /// (re-)establishes. - pub fn start_telemetry( - &mut self, - connection_message: ConnectionMessage, - ) { + pub fn start_telemetry(&mut self, connection_message: ConnectionMessage) { let Self { message_sender: _, register_sender, @@ -416,7 +396,7 @@ impl TelemetryHandle { } }; - match register_sender.unbounded_send(Register::TelemetryHandle { + match register_sender.unbounded_send(Register::Telemetry { id: *id, endpoints, connection_message, @@ -430,6 +410,42 @@ impl TelemetryHandle { ) } } + + /// TODO + pub fn handle(&self) -> TelemetryHandle { + TelemetryHandle { + message_sender: self.message_sender.clone(), + id: self.id, + connection_notifier: self.connection_notifier.clone(), + } + } +} + +/// TODO +#[derive(Debug, Clone)] +pub struct TelemetryHandle { + message_sender: mpsc::Sender, + id: Id, + connection_notifier: TelemetryConnectionNotifier, +} + +impl TelemetryHandle { + /// Send telemetries. + pub fn send(&mut self, verbosity: VerbosityLevel, payload: TelemetryPayload) { + match self.message_sender.try_send((self.id, verbosity, payload)) { + Ok(()) => {} + Err(err) if err.is_full() => todo!("overflow"), + Err(_) => unreachable!(), + } + } + + /// Get event stream for telemetry connection established events. + /// + /// This function will return an error if the telemetry has already been started by + /// [`TelemetryWorkerHandle::start_telemetry`]. + pub fn on_connect_stream(&self) -> TracingUnboundedReceiver<()> { + self.connection_notifier.on_connect_stream() + } } /// Used to create a stream of events with only one event: when a telemetry connection @@ -460,7 +476,7 @@ impl TelemetryConnectionNotifier { #[derive(Debug)] enum Register { - TelemetryHandle { + Telemetry { id: Id, endpoints: TelemetryEndpoints, connection_message: ConnectionMessage, @@ -565,4 +581,7 @@ impl IdGenerator { pub trait ClientTelemetry { /// TODO fn telemetry(&self) -> Option; + + /// TODO + fn start_telemetry(&self, connection_message: ConnectionMessage); } From 1b099fe5bd58a46a5c44eed37258fc9bb4f03c57 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 14:48:54 +0100 Subject: [PATCH 16/92] CLEANUP --- bin/node-template/node/src/chain_spec.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/node-template/node/src/chain_spec.rs b/bin/node-template/node/src/chain_spec.rs index b8a1dd17aef90..c5451e81f20c1 100644 --- a/bin/node-template/node/src/chain_spec.rs +++ b/bin/node-template/node/src/chain_spec.rs @@ -66,7 +66,7 @@ pub fn development_config() -> Result { ), // Bootnodes vec![], - // TelemetryHandle + // Telemetry None, // Protocol ID None, @@ -114,7 +114,7 @@ pub fn local_testnet_config() -> Result { ), // Bootnodes vec![], - // TelemetryHandle + // Telemetry None, // Protocol ID None, From 98ccd2ab2ba07f3ad3e5a835138c01daf5b787a3 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 14:52:39 +0100 Subject: [PATCH 17/92] CLEANUP --- bin/node/cli/src/service.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index fb1d18aee31cc..86a6efdec0a17 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -74,12 +74,11 @@ pub fn new_partial(config: &Configuration) -> Result), select_chain.clone(), - telemetry.clone(), + client.telemetry(), )?; let justification_import = grandpa_block_import.clone(); @@ -101,7 +100,7 @@ pub fn new_partial(config: &Configuration) -> Result Result<( on_demand.clone(), )); - let telemetry = client.telemetry(); - let (grandpa_block_import, _) = grandpa::block_import( client.clone(), &(client.clone() as Arc<_>), select_chain.clone(), - telemetry.clone(), + client.telemetry(), )?; let justification_import = grandpa_block_import.clone(); @@ -418,7 +414,7 @@ pub fn new_light_base(mut config: Configuration) -> Result<( &task_manager.spawn_handle(), config.prometheus_registry(), sp_consensus::NeverCanAuthor, - telemetry.clone(), // TODO hmm how do I get the telemetry here... + client.telemetry(), // TODO hmm how do I get the telemetry here... )?; let (network, network_status_sinks, system_rpc_tx, network_starter) = From d1055f45c2b44008474da979e081c89e4189bc69 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 14:58:07 +0100 Subject: [PATCH 18/92] CLEANUP --- client/service/src/client/client.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index c892d6d1fc45f..6f6e51f210bbb 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -122,7 +122,8 @@ pub struct Client where Block: BlockT { block_rules: BlockRules, execution_extensions: ExecutionExtensions, config: ClientConfig, - telemetry: RwLock>, + telemetry: Mutex>, + telemetry_handle: Option, _phantom: PhantomData, } @@ -339,7 +340,8 @@ impl Client where block_rules: BlockRules::new(fork_blocks, bad_blocks), execution_extensions, config, - telemetry: RwLock::new(telemetry), + telemetry_handle: telemetry.as_ref().map(|x| x.handle()), + telemetry: Mutex::new(telemetry), _phantom: Default::default(), }) } @@ -2023,11 +2025,11 @@ impl ClientTelemetry for Client B: BlockT, { fn telemetry(&self) -> Option { - self.telemetry.read().as_ref().map(|x| x.handle()) + self.telemetry_handle.clone() } fn start_telemetry(&self, connection_message: ConnectionMessage) { - if let Some(telemetry) = self.telemetry.write().as_mut() { + if let Some(telemetry) = self.telemetry.lock().as_mut() { telemetry.start_telemetry(connection_message); } } From 280bd256e1fb646e018f7474f47775899901ec8f Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 15:04:56 +0100 Subject: [PATCH 19/92] CLEANUP --- bin/node/cli/src/service.rs | 2 -- client/basic-authorship/src/basic_authorship.rs | 12 +++++++----- client/consensus/babe/src/lib.rs | 15 ++++++--------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index 86a6efdec0a17..1470b37c0e7e6 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -257,7 +257,6 @@ pub fn new_full_base( client.clone(), transaction_pool.clone(), prometheus_registry.as_ref(), - client.telemetry(), ); let can_author_with = @@ -275,7 +274,6 @@ pub fn new_full_base( backoff_authoring_blocks, babe_link, can_author_with, - telemetry: client.telemetry(), }; let babe = sc_consensus_babe::start_babe(babe_config)?; diff --git a/client/basic-authorship/src/basic_authorship.rs b/client/basic-authorship/src/basic_authorship.rs index 0c9c4a98d70fe..6c12de7b52f0f 100644 --- a/client/basic-authorship/src/basic_authorship.rs +++ b/client/basic-authorship/src/basic_authorship.rs @@ -32,7 +32,7 @@ use sp_runtime::{ traits::{Block as BlockT, Hash as HashT, Header as HeaderT, DigestFor, BlakeTwo256}, }; use sp_transaction_pool::{TransactionPool, InPoolTransaction}; -use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, ClientTelemetry, TelemetryHandle, CONSENSUS_INFO}; use sc_block_builder::{BlockBuilderApi, BlockBuilderProvider}; use sp_api::{ProvideRuntimeApi, ApiExt}; use futures::{future, future::{Future, FutureExt}, channel::oneshot, select}; @@ -66,21 +66,23 @@ pub struct ProposerFactory { _phantom: PhantomData, } -impl ProposerFactory { +impl ProposerFactory +where + C: ClientTelemetry, +{ pub fn new( spawn_handle: impl SpawnNamed + 'static, client: Arc, transaction_pool: Arc, prometheus: Option<&PrometheusRegistry>, - telemetry: Option, ) -> Self { ProposerFactory { spawn_handle: Box::new(spawn_handle), - client, transaction_pool, metrics: PrometheusMetrics::new(prometheus), max_block_size: DEFAULT_MAX_BLOCK_SIZE, - telemetry, + telemetry: client.telemetry(), + client, _phantom: PhantomData, } } diff --git a/client/consensus/babe/src/lib.rs b/client/consensus/babe/src/lib.rs index 9d94d346f88f0..41be23c6a823e 100644 --- a/client/consensus/babe/src/lib.rs +++ b/client/consensus/babe/src/lib.rs @@ -89,7 +89,7 @@ use sp_runtime::{ use sp_api::{ProvideRuntimeApi, NumberFor}; use parking_lot::Mutex; use sp_inherents::{InherentDataProviders, InherentData}; -use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_TRACE, CONSENSUS_DEBUG}; +use sc_telemetry::{telemetry, ClientTelemetry, TelemetryHandle, CONSENSUS_TRACE, CONSENSUS_DEBUG}; use sp_consensus::{ BlockImport, Environment, Proposer, BlockCheckParams, ForkChoiceStrategy, BlockImportParams, BlockOrigin, Error as ConsensusError, @@ -358,7 +358,7 @@ impl std::ops::Deref for Config { } /// Parameters for BABE. -pub struct BabeParams { +pub struct BabeParams { /// The keystore that manages the keys of the node. pub keystore: SyncCryptoStorePtr, @@ -393,9 +393,6 @@ pub struct BabeParams { /// Checks if the current native implementation can author with a runtime at a given block. pub can_author_with: CAW, - - /// TelemetryHandle instance. - pub telemetry: Option, } /// Start the babe worker. @@ -411,14 +408,14 @@ pub fn start_babe(BabeParams { backoff_authoring_blocks, babe_link, can_author_with, - telemetry, }: BabeParams) -> Result< BabeWorker, sp_consensus::Error, > where B: BlockT, C: ProvideRuntimeApi + ProvideCache + ProvideUncles + BlockchainEvents - + HeaderBackend + HeaderMetadata + Send + Sync + 'static, + + HeaderBackend + HeaderMetadata + ClientTelemetry + + Send + Sync + 'static, C::Api: BabeApi, SC: SelectChain + 'static, E: Environment + Send + Sync + 'static, @@ -448,7 +445,7 @@ pub fn start_babe(BabeParams { register_babe_inherent_data_provider(&inherent_data_providers, config.slot_duration())?; sc_consensus_uncles::register_uncles_inherent_data_provider( - client, + client.clone(), select_chain.clone(), &inherent_data_providers, )?; @@ -462,7 +459,7 @@ pub fn start_babe(BabeParams { inherent_data_providers, babe_link.time_source, can_author_with, - telemetry, + client.telemetry(), ); Ok(BabeWorker { inner: Box::pin(inner), From 14bdec1431b4ef2588c58c69d609679713082653 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 15:08:11 +0100 Subject: [PATCH 20/92] CLEANUP --- client/chain-spec/src/chain_spec.rs | 2 +- client/chain-spec/src/lib.rs | 2 +- client/cli/src/commands/run_cmd.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/client/chain-spec/src/chain_spec.rs b/client/chain-spec/src/chain_spec.rs index 09158bc986229..2faf95568290e 100644 --- a/client/chain-spec/src/chain_spec.rs +++ b/client/chain-spec/src/chain_spec.rs @@ -198,7 +198,7 @@ impl ChainSpec { &self.client_spec.id } - /// TelemetryHandle endpoints (if any) + /// Telemetry endpoints (if any) pub fn telemetry_endpoints(&self) -> &Option { &self.client_spec.telemetry_endpoints } diff --git a/client/chain-spec/src/lib.rs b/client/chain-spec/src/lib.rs index 2a1b1a553ff41..ee4f757f8cf0e 100644 --- a/client/chain-spec/src/lib.rs +++ b/client/chain-spec/src/lib.rs @@ -137,7 +137,7 @@ pub trait ChainSpec: BuildStorage + Send + Sync { fn chain_type(&self) -> ChainType; /// A list of bootnode addresses. fn boot_nodes(&self) -> &[MultiaddrWithPeerId]; - /// TelemetryHandle endpoints (if any) + /// Telemetry endpoints (if any) fn telemetry_endpoints(&self) -> &Option; /// Network protocol id. fn protocol_id(&self) -> Option<&str>; diff --git a/client/cli/src/commands/run_cmd.rs b/client/cli/src/commands/run_cmd.rs index 67303d927b96e..bb6f77819d7af 100644 --- a/client/cli/src/commands/run_cmd.rs +++ b/client/cli/src/commands/run_cmd.rs @@ -149,7 +149,7 @@ pub struct RunCmd { /// Disable connecting to the Substrate telemetry server. /// - /// TelemetryHandle is on by default on global chains. + /// Telemetry is on by default on global chains. #[structopt(long = "no-telemetry")] pub no_telemetry: bool, From 9ed97f1f95f418284c64ecedff122e9a19a6e78c Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 15:26:22 +0100 Subject: [PATCH 21/92] CLEANUP --- bin/node/cli/src/service.rs | 4 ---- client/consensus/babe/src/lib.rs | 10 +++++----- client/finality-grandpa/src/authorities.rs | 2 +- client/finality-grandpa/src/import.rs | 18 +++++++----------- client/finality-grandpa/src/lib.rs | 15 ++++----------- client/finality-grandpa/src/observer.rs | 11 +++++------ 6 files changed, 22 insertions(+), 38 deletions(-) diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index 1470b37c0e7e6..8689176d0a806 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -78,7 +78,6 @@ pub fn new_partial(config: &Configuration) -> Result), select_chain.clone(), - client.telemetry(), )?; let justification_import = grandpa_block_import.clone(); @@ -100,7 +99,6 @@ pub fn new_partial(config: &Configuration) -> Result Result<( client.clone(), &(client.clone() as Arc<_>), select_chain.clone(), - client.telemetry(), )?; let justification_import = grandpa_block_import.clone(); @@ -412,7 +409,6 @@ pub fn new_light_base(mut config: Configuration) -> Result<( &task_manager.spawn_handle(), config.prometheus_registry(), sp_consensus::NeverCanAuthor, - client.telemetry(), // TODO hmm how do I get the telemetry here... )?; let (network, network_status_sinks, system_rpc_tx, network_starter) = diff --git a/client/consensus/babe/src/lib.rs b/client/consensus/babe/src/lib.rs index 41be23c6a823e..15d8b487007ea 100644 --- a/client/consensus/babe/src/lib.rs +++ b/client/consensus/babe/src/lib.rs @@ -1498,12 +1498,12 @@ pub fn import_queue( spawner: &impl sp_core::traits::SpawnNamed, registry: Option<&Registry>, can_author_with: CAW, - telemetry: Option, ) -> ClientResult> where Inner: BlockImport> + Send + Sync + 'static, - Client: ProvideRuntimeApi + ProvideCache + Send + Sync + AuxStore + 'static, - Client: HeaderBackend + HeaderMetadata, + Client: ProvideRuntimeApi + ProvideCache + HeaderBackend + + HeaderMetadata + AuxStore + ClientTelemetry + + Send + Sync + 'static, Client::Api: BlockBuilderApi + BabeApi + ApiExt, SelectChain: sp_consensus::SelectChain + 'static, CAW: CanAuthorWith + Send + Sync + 'static, @@ -1511,14 +1511,14 @@ pub fn import_queue( register_babe_inherent_data_provider(&inherent_data_providers, babe_link.config.slot_duration)?; let verifier = BabeVerifier { - client, select_chain, inherent_data_providers, config: babe_link.config, epoch_changes: babe_link.epoch_changes, time_source: babe_link.time_source, can_author_with, - telemetry, + telemetry: client.telemetry(), + client, }; Ok(BasicQueue::new( diff --git a/client/finality-grandpa/src/authorities.rs b/client/finality-grandpa/src/authorities.rs index 70b1ca2ceacc0..d98b8af449049 100644 --- a/client/finality-grandpa/src/authorities.rs +++ b/client/finality-grandpa/src/authorities.rs @@ -418,7 +418,7 @@ where best_number: N, is_descendent_of: &F, initial_sync: bool, - mut telemetry: Option<&mut TelemetryHandle>, + mut telemetry: Option, ) -> Result, Error> where F: Fn(&H, &H) -> Result, diff --git a/client/finality-grandpa/src/import.rs b/client/finality-grandpa/src/import.rs index 9126047439d82..4a3d281fbf74f 100644 --- a/client/finality-grandpa/src/import.rs +++ b/client/finality-grandpa/src/import.rs @@ -24,7 +24,7 @@ use parking_lot::RwLockWriteGuard; use sp_blockchain::{BlockStatus, well_known_cache_keys}; use sc_client_api::{backend::Backend, utils::is_descendent_of}; -use sc_telemetry::TelemetryHandle; +use sc_telemetry::ClientTelemetry; use sp_utils::mpsc::TracingUnboundedSender; use sp_api::TransactionFor; @@ -63,7 +63,6 @@ pub struct GrandpaBlockImport { send_voter_commands: TracingUnboundedSender>>, authority_set_hard_forks: HashMap>>, justification_sender: GrandpaJustificationSender, - telemetry: Option, _phantom: PhantomData, } @@ -78,7 +77,6 @@ impl Clone for send_voter_commands: self.send_voter_commands.clone(), authority_set_hard_forks: self.authority_set_hard_forks.clone(), justification_sender: self.justification_sender.clone(), - telemetry: self.telemetry.clone(), _phantom: PhantomData, } } @@ -89,7 +87,7 @@ impl JustificationImport NumberFor: finality_grandpa::BlockNumberOps, DigestFor: Encode, BE: Backend, - Client: crate::ClientForGrandpa, + Client: crate::ClientForGrandpa + ClientTelemetry, SC: SelectChain, { type Error = ConsensusError; @@ -221,7 +219,7 @@ where NumberFor: finality_grandpa::BlockNumberOps, DigestFor: Encode, BE: Backend, - Client: crate::ClientForGrandpa, + Client: crate::ClientForGrandpa + ClientTelemetry, { // check for a new authority set change. fn check_new_change( @@ -342,7 +340,7 @@ where number, &is_descendent_of, initial_sync, - self.telemetry.clone().as_mut(), + self.inner.telemetry(), ) .map_err(|e| ConsensusError::ClientImport(e.to_string())) .map_err(ConsensusError::from)?; @@ -418,7 +416,7 @@ impl BlockImport NumberFor: finality_grandpa::BlockNumberOps, DigestFor: Encode, BE: Backend, - Client: crate::ClientForGrandpa, + Client: crate::ClientForGrandpa + ClientTelemetry, for<'a> &'a Client: BlockImport>, { @@ -565,7 +563,6 @@ impl GrandpaBlockImport>>, authority_set_hard_forks: Vec<(SetId, PendingChange>)>, justification_sender: GrandpaJustificationSender, - telemetry: Option, ) -> GrandpaBlockImport { // check for and apply any forced authority set hard fork that applies // to the *current* authority set. @@ -609,7 +606,6 @@ impl GrandpaBlockImport GrandpaBlockImport GrandpaBlockImport where BE: Backend, - Client: crate::ClientForGrandpa, + Client: crate::ClientForGrandpa + ClientTelemetry, NumberFor: finality_grandpa::BlockNumberOps, { /// Import a block justification and finalize the block. @@ -654,7 +650,7 @@ where justification.into(), initial_sync, Some(&self.justification_sender), - self.telemetry.clone(), + self.inner.telemetry(), ); match result { diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index 40e85607ac1e5..4465942f04959 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -79,7 +79,7 @@ use sp_core::{ use sp_keystore::{SyncCryptoStorePtr, SyncCryptoStore}; use sp_application_crypto::AppKey; use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver}; -use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO, CONSENSUS_DEBUG}; +use sc_telemetry::{telemetry, ClientTelemetry, TelemetryHandle, CONSENSUS_INFO, CONSENSUS_DEBUG}; use parking_lot::RwLock; use finality_grandpa::Error as GrandpaError; @@ -454,7 +454,6 @@ pub struct LinkHalf { voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: GrandpaJustificationSender, justification_stream: GrandpaJustificationStream, - telemetry: Option, } impl LinkHalf { @@ -505,7 +504,6 @@ pub fn block_import( client: Arc, genesis_authorities_provider: &dyn GenesisAuthoritySetProvider, select_chain: SC, - telemetry: Option, ) -> Result< ( GrandpaBlockImport, @@ -516,14 +514,13 @@ pub fn block_import( where SC: SelectChain, BE: Backend + 'static, - Client: ClientForGrandpa + 'static, + Client: ClientForGrandpa + ClientTelemetry + 'static, { block_import_with_authority_set_hard_forks( client, genesis_authorities_provider, select_chain, Default::default(), - telemetry, ) } @@ -537,7 +534,6 @@ pub fn block_import_with_authority_set_hard_forks genesis_authorities_provider: &dyn GenesisAuthoritySetProvider, select_chain: SC, authority_set_hard_forks: Vec<(SetId, (Block::Hash, NumberFor), AuthorityList)>, - telemetry: Option, ) -> Result< ( GrandpaBlockImport, @@ -548,7 +544,7 @@ pub fn block_import_with_authority_set_hard_forks where SC: SelectChain, BE: Backend + 'static, - Client: ClientForGrandpa + 'static, + Client: ClientForGrandpa + ClientTelemetry + 'static, { let chain_info = client.info(); let genesis_hash = chain_info.genesis_hash; @@ -558,7 +554,7 @@ where genesis_hash, >::zero(), { - let mut telemetry = telemetry.clone(); + let mut telemetry = client.telemetry(); move || { let authorities = genesis_authorities_provider.get()?; telemetry!( @@ -601,7 +597,6 @@ where voter_commands_tx, authority_set_hard_forks, justification_sender.clone(), - telemetry.clone(), ), LinkHalf { client, @@ -610,7 +605,6 @@ where voter_commands_rx, justification_sender, justification_stream, - telemetry, }, )) } @@ -738,7 +732,6 @@ where voter_commands_rx, justification_sender, justification_stream: _, - telemetry: _, // TODO: duplicate info?? } = link; let network = NetworkBridge::new( diff --git a/client/finality-grandpa/src/observer.rs b/client/finality-grandpa/src/observer.rs index ce482b0e86c2e..513b2dc501cc7 100644 --- a/client/finality-grandpa/src/observer.rs +++ b/client/finality-grandpa/src/observer.rs @@ -29,7 +29,7 @@ use log::{debug, info, warn}; use sp_keystore::SyncCryptoStorePtr; use sp_consensus::SelectChain; use sc_client_api::backend::Backend; -use sc_telemetry::TelemetryHandle; +use sc_telemetry::{ClientTelemetry, TelemetryHandle}; use sp_utils::mpsc::TracingUnboundedReceiver; use sp_runtime::traits::{NumberFor, Block as BlockT}; use sp_blockchain::HeaderMetadata; @@ -167,7 +167,7 @@ where N: NetworkT + Send + Clone + 'static, SC: SelectChain + 'static, NumberFor: BlockNumberOps, - Client: crate::ClientForGrandpa + 'static, + Client: crate::ClientForGrandpa + ClientTelemetry + 'static, { let LinkHalf { client, @@ -175,7 +175,6 @@ where persistent_data, voter_commands_rx, justification_sender, - telemetry, .. } = link; @@ -184,17 +183,17 @@ where config.clone(), persistent_data.set_state.clone(), None, - telemetry.clone(), + client.telemetry(), ); let observer_work = ObserverWork::new( - client, + client.clone(), network, persistent_data, config.keystore, voter_commands_rx, Some(justification_sender), - telemetry, + client.telemetry(), ); let observer_work = observer_work From b88989dc0a16fbb2787b5230999dba8757517907 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 15:33:11 +0100 Subject: [PATCH 22/92] CLEANUP --- client/service/src/client/client.rs | 4 ++-- client/service/src/config.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index 6f6e51f210bbb..be05d44c02f0f 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -36,10 +36,10 @@ use sp_core::{ #[cfg(feature="test-helpers")] use sp_keystore::SyncCryptoStorePtr; use sc_telemetry::{ - telemetry, + telemetry, ClientTelemetry, ConnectionMessage, - Telemetry, + Telemetry, TelemetryHandle, SUBSTRATE_INFO, }; diff --git a/client/service/src/config.rs b/client/service/src/config.rs index 9564c3129a704..f683eefed1a58 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -91,12 +91,12 @@ pub struct Configuration { pub rpc_methods: RpcMethods, /// Prometheus endpoint configuration. `None` if disabled. pub prometheus_config: Option, - /// TelemetryHandle service URL. `None` if disabled. + /// Telemetry service URL. `None` if disabled. pub telemetry_endpoints: Option, /// External WASM transport for the telemetry. If `Some`, when connection to a telemetry /// endpoint, this transport will be tried in priority before all others. pub telemetry_external_transport: Option, - /// TelemetryHandle handle. + /// Telemetry handle. /// /// This is a handle to a `TelemetryWorker` instance. It is used to initialize the telemetry for /// a substrate node. From 420f3573b250d37fc103d383753979a0891b69ba Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 15:37:03 +0100 Subject: [PATCH 23/92] Fix todo --- client/service/src/builder.rs | 4 ++-- client/service/src/metrics.rs | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 39fee55abc5d7..a8e8af13e3049 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -619,7 +619,7 @@ pub fn spawn_tasks( config.prometheus_config.clone() { // Set static metrics. - let metrics = MetricsService::with_prometheus(®istry, &config)?; + let metrics = MetricsService::with_prometheus(client.telemetry(), ®istry, &config)?; spawn_handle.spawn( "prometheus-endpoint", prometheus_endpoint::init_prometheus(port, registry).map(drop) @@ -627,7 +627,7 @@ pub fn spawn_tasks( metrics } else { - MetricsService::new() + MetricsService::new(client.telemetry()) }; // Periodically updated metrics and telemetry updates. diff --git a/client/service/src/metrics.rs b/client/service/src/metrics.rs index a913af131ea15..dcdd3975ef521 100644 --- a/client/service/src/metrics.rs +++ b/client/service/src/metrics.rs @@ -118,19 +118,20 @@ pub struct MetricsService { impl MetricsService { /// Creates a `MetricsService` that only sends information /// to the telemetry. - pub fn new() -> Self { + pub fn new(telemetry: Option) -> Self { MetricsService { metrics: None, last_total_bytes_inbound: 0, last_total_bytes_outbound: 0, last_update: Instant::now(), - telemetry: None, // TODO how to inject + telemetry, } } /// Creates a `MetricsService` that sends metrics /// to prometheus alongside the telemetry. pub fn with_prometheus( + telemetry: Option, registry: &Registry, config: &Configuration, ) -> Result { @@ -151,7 +152,7 @@ impl MetricsService { last_total_bytes_inbound: 0, last_total_bytes_outbound: 0, last_update: Instant::now(), - telemetry: None, + telemetry, }) } From 391efa2e61828371cfbde5d98b24bf67ca254d4e Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 15:41:10 +0100 Subject: [PATCH 24/92] CLEANUP --- client/telemetry/src/endpoints.rs | 5 +++-- client/telemetry/src/lib.rs | 18 +++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/client/telemetry/src/endpoints.rs b/client/telemetry/src/endpoints.rs index a58934d1561ab..fe4fa23974a64 100644 --- a/client/telemetry/src/endpoints.rs +++ b/client/telemetry/src/endpoints.rs @@ -25,7 +25,8 @@ use serde::{Deserialize, Deserializer, Serialize}; /// The URL string can be either a URL or a multiaddress. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] pub struct TelemetryEndpoints( - #[serde(deserialize_with = "url_or_multiaddr_deser")] pub(crate) Vec<(Multiaddr, u8)>, + #[serde(deserialize_with = "url_or_multiaddr_deser")] + pub(crate) Vec<(Multiaddr, u8)>, ); /// Custom deserializer for TelemetryEndpoints, used to convert urls or multiaddr to multiaddr. @@ -92,7 +93,7 @@ mod tests { ("/ip4/80.123.90.4/tcp/5432".into(), 4), ]; let telem = - TelemetryEndpoints::new(endp.clone()).expect("TelemetryHandle endpoint should be valid"); + TelemetryEndpoints::new(endp.clone()).expect("Telemetry endpoint should be valid"); let mut res: Vec<(Multiaddr, u8)> = vec![]; for (a, b) in endp.iter() { res.push(( diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 3b7159c479cc8..f2857e3f2fdb3 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -411,14 +411,14 @@ impl Telemetry { } } - /// TODO - pub fn handle(&self) -> TelemetryHandle { - TelemetryHandle { - message_sender: self.message_sender.clone(), - id: self.id, - connection_notifier: self.connection_notifier.clone(), - } - } + /// TODO + pub fn handle(&self) -> TelemetryHandle { + TelemetryHandle { + message_sender: self.message_sender.clone(), + id: self.id, + connection_notifier: self.connection_notifier.clone(), + } + } } /// TODO @@ -582,6 +582,6 @@ pub trait ClientTelemetry { /// TODO fn telemetry(&self) -> Option; - /// TODO + /// TODO fn start_telemetry(&self, connection_message: ConnectionMessage); } From 69aa51652653975119fbc80df9582cf0c619d4be Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 15:43:17 +0100 Subject: [PATCH 25/92] fmt --- client/telemetry/src/lib.rs | 14 ++++++-------- client/tracing/src/logging/mod.rs | 7 +++---- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index f2857e3f2fdb3..3b1587a027396 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -43,8 +43,8 @@ use log::{error, warn}; use serde::Serialize; use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; use std::collections::HashMap; -use std::mem; use std::io; +use std::mem; pub use libp2p::wasm_ext::ExtTransport; pub use log; @@ -240,12 +240,10 @@ impl TelemetryWorker { let (matching, rest): (Vec<_>, Vec<_>) = mem::take(pending_connection_notifications) - .into_iter().partition( - |(addr_b, _)| *addr_b == addr - ); - node.telemetry_connection_notifier.extend( - matching.into_iter().map(|(_, x)| x.clone()) - ); + .into_iter() + .partition(|(addr_b, _)| *addr_b == addr); + node.telemetry_connection_notifier + .extend(matching.into_iter().map(|(_, x)| x.clone())); let _ = mem::replace(pending_connection_notifications, rest); } } @@ -407,7 +405,7 @@ impl Telemetry { "Could not initialize telemetry: \ the telemetry is probably already running: {}", err, - ) + ), } } diff --git a/client/tracing/src/logging/mod.rs b/client/tracing/src/logging/mod.rs index 3d3bb2819c996..3fbdc13341216 100644 --- a/client/tracing/src/logging/mod.rs +++ b/client/tracing/src/logging/mod.rs @@ -131,10 +131,9 @@ where if let Some(profiling_targets) = profiling_targets { env_filter = parse_user_directives(env_filter, profiling_targets)?; - env_filter = env_filter - .add_directive( - parse_default_directive("sc_tracing=trace").expect("provided directive is valid") - ); + env_filter = env_filter.add_directive( + parse_default_directive("sc_tracing=trace").expect("provided directive is valid"), + ); } let max_level_hint = Layer::::max_level_hint(&env_filter); From 91485413a9f5ecf2f85ca4f8d65c4f3426dea101 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 16:03:01 +0100 Subject: [PATCH 26/92] Adapt more code --- client/consensus/aura/src/lib.rs | 49 ++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index eb3c2e93e7044..fbfe3f0e81fda 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -66,7 +66,7 @@ use sp_inherents::{InherentDataProviders, InherentData}; use sp_timestamp::{ TimestampInherentData, InherentType as TimestampInherent, InherentError as TIError }; -use sc_telemetry::{telemetry, CONSENSUS_TRACE, CONSENSUS_DEBUG, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, ClientTelemetry, CONSENSUS_TRACE, CONSENSUS_DEBUG, CONSENSUS_INFO}; use sc_consensus_slots::{ CheckedHeader, SlotInfo, SlotCompatible, StorageChanges, check_equivocation, @@ -151,7 +151,8 @@ pub fn start_aura( can_author_with: CAW, ) -> Result, sp_consensus::Error> where B: BlockT, - C: ProvideRuntimeApi + BlockOf + ProvideCache + AuxStore + HeaderBackend + Send + Sync, + C: ProvideRuntimeApi + BlockOf + ProvideCache + AuxStore + HeaderBackend + Send + + ClientTelemetry + Sync, C::Api: AuraApi>, SC: SelectChain, E: Environment + Send + Sync + 'static, @@ -166,7 +167,7 @@ pub fn start_aura( BS: BackoffAuthoringBlocksStrategy> + Send + 'static, { let worker = AuraWorker { - client, + client: client.clone(), block_import: Arc::new(Mutex::new(block_import)), env, keystore, @@ -187,6 +188,7 @@ pub fn start_aura( inherent_data_providers, AuraSlotCompatible, can_author_with, + client.telemetry(), )) } @@ -505,6 +507,7 @@ pub struct AuraVerifier { } impl AuraVerifier where + C: ClientTelemetry, P: Send + Sync + 'static, CAW: Send + Sync + 'static, { @@ -553,8 +556,9 @@ impl AuraVerifier where "halting for block {} seconds in the future", diff ); - telemetry!(CONSENSUS_INFO; "aura.halting_for_future_block"; - "diff" => ?diff + telemetry!( + self.client.telemetry(); CONSENSUS_INFO; "aura.halting_for_future_block"; + "diff" => ?diff, ); thread::sleep(Duration::from_secs(diff)); Ok(()) @@ -572,12 +576,13 @@ impl AuraVerifier where #[forbid(deprecated)] impl Verifier for AuraVerifier where - C: ProvideRuntimeApi + - Send + - Sync + - sc_client_api::backend::AuxStore + - ProvideCache + - BlockOf, + C: ProvideRuntimeApi + + sc_client_api::backend::AuxStore + + ProvideCache + + BlockOf + + ClientTelemetry + + Send + + Sync, C::Api: BlockBuilderApi + AuraApi> + ApiExt, DigestItemFor: CompatibleDigestItem

, P: Pair + Send + Sync + 'static, @@ -643,7 +648,10 @@ impl Verifier for AuraVerifier where } trace!(target: "aura", "Checked {:?}; importing.", pre_header); - telemetry!(CONSENSUS_TRACE; "aura.checked_and_importing"; "pre_header" => ?pre_header); + telemetry!( + self.client.telemetry(); CONSENSUS_TRACE; "aura.checked_and_importing"; + "pre_header" => ?pre_header, + ); // Look for an authorities-change log. let maybe_keys = pre_header.digest() @@ -670,8 +678,11 @@ impl Verifier for AuraVerifier where } CheckedHeader::Deferred(a, b) => { debug!(target: "aura", "Checking {:?} failed; {:?}, {:?}.", hash, a, b); - telemetry!(CONSENSUS_DEBUG; "aura.header_too_far_in_future"; - "hash" => ?hash, "a" => ?a, "b" => ?b + telemetry!( + self.client.telemetry(); CONSENSUS_DEBUG; "aura.header_too_far_in_future"; + "hash" => ?hash, + "a" => ?a, + "b" => ?b, ); Err(format!("Header {:?} rejected: too far in the future", hash)) } @@ -843,7 +854,15 @@ pub fn import_queue( ) -> Result, sp_consensus::Error> where B: BlockT, C::Api: BlockBuilderApi + AuraApi> + ApiExt, - C: 'static + ProvideRuntimeApi + BlockOf + ProvideCache + Send + Sync + AuxStore + HeaderBackend, + C: ProvideRuntimeApi + + BlockOf + + ProvideCache + + AuxStore + + HeaderBackend + + ClientTelemetry + + Send + + Sync + + 'static, I: BlockImport> + Send + Sync + 'static, DigestItemFor: CompatibleDigestItem

, P: Pair + Send + Sync + 'static, From 8210e35ea892384f317416990e876779c8368179 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 16:04:52 +0100 Subject: [PATCH 27/92] Adapt more code --- bin/node-template/node/src/service.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/bin/node-template/node/src/service.rs b/bin/node-template/node/src/service.rs index 552705f299b84..feed6cdd04e3b 100644 --- a/bin/node-template/node/src/service.rs +++ b/bin/node-template/node/src/service.rs @@ -11,7 +11,7 @@ pub use sc_executor::NativeExecutor; use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair}; use sc_finality_grandpa::SharedVoterState; use sc_keystore::LocalKeystore; -use sc_telemetry::TelemetrySpan; +use sc_telemetry::ClientTelemetry; // Our native executor instance. native_executor_instance!( @@ -163,10 +163,7 @@ pub fn new_full(mut config: Configuration) -> Result }) }; - let telemetry_span = TelemetrySpan::new(); - let _telemetry_span_entered = telemetry_span.enter(); - - let (_rpc_handlers, telemetry_connection_notifier) = sc_service::spawn_tasks( + let _rpc_handlers = sc_service::spawn_tasks( sc_service::SpawnTasksParams { network: network.clone(), client: client.clone(), @@ -180,7 +177,6 @@ pub fn new_full(mut config: Configuration) -> Result network_status_sinks, system_rpc_tx, config, - telemetry_span: Some(telemetry_span.clone()), }, )?; @@ -230,6 +226,7 @@ pub fn new_full(mut config: Configuration) -> Result observer_enabled: false, keystore, is_authority: role.is_authority(), + telemetry: client.telemetry(), }; if enable_grandpa { @@ -243,10 +240,10 @@ pub fn new_full(mut config: Configuration) -> Result config: grandpa_config, link: grandpa_link, network, - telemetry_on_connect: telemetry_connection_notifier.map(|x| x.on_connect_stream()), voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(), prometheus_registry, shared_voter_state: SharedVoterState::empty(), + telemetry: client.telemetry(), }; // the GRANDPA voter task is considered infallible, i.e. @@ -317,9 +314,6 @@ pub fn new_light(mut config: Configuration) -> Result ); } - let telemetry_span = TelemetrySpan::new(); - let _telemetry_span_entered = telemetry_span.enter(); - sc_service::spawn_tasks(sc_service::SpawnTasksParams { remote_blockchain: Some(backend.remote_blockchain()), transaction_pool, @@ -333,7 +327,6 @@ pub fn new_light(mut config: Configuration) -> Result network, network_status_sinks, system_rpc_tx, - telemetry_span: Some(telemetry_span.clone()), })?; network_starter.start_network(); From a7bec3d4fe9924996f100e27733ca8fdbb049ecb Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 16:11:25 +0100 Subject: [PATCH 28/92] Adapt even MORE code --- bin/node/testing/src/bench.rs | 1 + client/service/src/client/call_executor.rs | 1 + client/service/src/task_manager/tests.rs | 100 +-------------------- client/service/test/src/client/mod.rs | 1 + test-utils/client/src/lib.rs | 1 + 5 files changed, 5 insertions(+), 99 deletions(-) diff --git a/bin/node/testing/src/bench.rs b/bin/node/testing/src/bench.rs index a6f65b86a0e27..c84ad63749874 100644 --- a/bin/node/testing/src/bench.rs +++ b/bin/node/testing/src/bench.rs @@ -426,6 +426,7 @@ impl BenchDb { ExecutionExtensions::new(profile.into_execution_strategies(), None), Box::new(task_executor.clone()), None, + None, Default::default(), ).expect("Should not fail"); diff --git a/client/service/src/client/call_executor.rs b/client/service/src/client/call_executor.rs index 8c7ca645b0ffe..176c68096e97c 100644 --- a/client/service/src/client/call_executor.rs +++ b/client/service/src/client/call_executor.rs @@ -368,6 +368,7 @@ mod tests { None, Box::new(TaskExecutor::new()), None, + None, Default::default(), ).expect("Creates a client"); diff --git a/client/service/src/task_manager/tests.rs b/client/service/src/task_manager/tests.rs index 762348ba9fa5d..09768a19339f2 100644 --- a/client/service/src/task_manager/tests.rs +++ b/client/service/src/task_manager/tests.rs @@ -20,14 +20,7 @@ use crate::config::TaskExecutor; use crate::task_manager::TaskManager; use futures::{future::FutureExt, pin_mut, select}; use parking_lot::Mutex; -use sc_telemetry::TelemetrySpan; -use std::{any::Any, env, sync::Arc, time::Duration}; -use tracing::{event::Event, span::Id, subscriber::Subscriber}; -use tracing_subscriber::{ - layer::{Context, SubscriberExt}, - registry::LookupSpan, - Layer, -}; +use std::{any::Any, sync::Arc, time::Duration}; #[derive(Clone, Debug)] struct DropTester(Arc>); @@ -317,94 +310,3 @@ fn ensure_task_manager_future_continues_when_childs_not_essential_task_fails() { runtime.block_on(task_manager.clean_shutdown()); assert_eq!(drop_tester, 0); } - -struct TestLayer { - spans_found: Arc>>>, -} - -impl Layer for TestLayer -where - S: Subscriber + for<'a> LookupSpan<'a>, -{ - fn on_event(&self, _: &Event<'_>, ctx: Context) { - let mut spans_found = self.spans_found.lock(); - - if spans_found.is_some() { - panic!("on_event called multiple times"); - } - - *spans_found = Some(ctx.scope().map(|x| x.id()).collect()); - } -} - -fn setup_subscriber() -> ( - impl Subscriber + for<'a> LookupSpan<'a>, - Arc>>>, -) { - let spans_found = Arc::new(Mutex::new(Default::default())); - let layer = TestLayer { - spans_found: spans_found.clone(), - }; - let subscriber = tracing_subscriber::fmt().finish().with(layer); - (subscriber, spans_found) -} - -/// This is not an actual test, it is used by the `telemetry_span_is_forwarded_to_task` test. -/// The given test will call the test executable and only execute this one test that -/// test that the telemetry span and the prefix span are forwarded correctly. This needs to be done -/// in a separate process to avoid interfering with the other tests. -#[test] -fn subprocess_telemetry_span_is_forwarded_to_task() { - if env::var("SUBPROCESS_TEST").is_err() { - return; - } - - let (subscriber, spans_found) = setup_subscriber(); - tracing_log::LogTracer::init().unwrap(); - let _sub_guard = tracing::subscriber::set_global_default(subscriber); - - let mut runtime = tokio::runtime::Runtime::new().unwrap(); - - let prefix_span = tracing::info_span!("prefix"); - let _enter_prefix_span = prefix_span.enter(); - - let telemetry_span = TelemetrySpan::new(); - let _enter_telemetry_span = telemetry_span.enter(); - - let handle = runtime.handle().clone(); - let task_executor = TaskExecutor::from(move |fut, _| handle.spawn(fut).map(|_| ())); - let task_manager = new_task_manager(task_executor); - - let (sender, receiver) = futures::channel::oneshot::channel(); - - task_manager.spawn_handle().spawn( - "log-something", - async move { - log::info!("boo!"); - sender.send(()).unwrap(); - } - .boxed(), - ); - - runtime.block_on(receiver).unwrap(); - runtime.block_on(task_manager.clean_shutdown()); - - let spans = spans_found.lock().take().unwrap(); - assert_eq!(2, spans.len()); - - assert_eq!(spans[0], prefix_span.id().unwrap()); - assert_eq!(spans[1], telemetry_span.span().id().unwrap()); -} - -#[test] -fn telemetry_span_is_forwarded_to_task() { - let executable = env::current_exe().unwrap(); - let output = std::process::Command::new(executable) - .env("SUBPROCESS_TEST", "1") - .args(&["--nocapture", "subprocess_telemetry_span_is_forwarded_to_task"]) - .output() - .unwrap(); - println!("{}", String::from_utf8(output.stdout).unwrap()); - eprintln!("{}", String::from_utf8(output.stderr).unwrap()); - assert!(output.status.success()); -} diff --git a/client/service/test/src/client/mod.rs b/client/service/test/src/client/mod.rs index 66b6aae12c2f9..c44cce4334b6d 100644 --- a/client/service/test/src/client/mod.rs +++ b/client/service/test/src/client/mod.rs @@ -1741,6 +1741,7 @@ fn cleans_up_closed_notification_sinks_on_block_import() { &substrate_test_runtime_client::GenesisParameters::default().genesis_storage(), None, None, + None, Box::new(TaskExecutor::new()), Default::default(), ) diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index bf5b2b6a04143..1c3ad02b2625e 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -227,6 +227,7 @@ impl TestClientBuilder Date: Wed, 17 Feb 2021 16:35:21 +0100 Subject: [PATCH 29/92] Adapt more code... --- Cargo.lock | 2 -- bin/node/cli/src/browser.rs | 2 +- bin/node/cli/src/chain_spec.rs | 2 +- bin/node/cli/src/service.rs | 11 +++--- client/consensus/babe/src/tests.rs | 1 + client/finality-grandpa/src/authorities.rs | 34 +++++++++++-------- .../src/communication/gossip.rs | 20 +++++++++-- .../src/communication/tests.rs | 2 ++ client/finality-grandpa/src/finality_proof.rs | 3 -- client/finality-grandpa/src/observer.rs | 1 + client/finality-grandpa/src/tests.rs | 24 +++++++++---- client/service/Cargo.toml | 2 -- 12 files changed, 66 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9e04a4ec6c7b5..907971a92b289 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7581,8 +7581,6 @@ dependencies = [ "tokio 0.2.25", "tracing", "tracing-futures", - "tracing-log", - "tracing-subscriber", "wasm-timer", ] diff --git a/bin/node/cli/src/browser.rs b/bin/node/cli/src/browser.rs index 4f20ef7c7e570..a187b181d699a 100644 --- a/bin/node/cli/src/browser.rs +++ b/bin/node/cli/src/browser.rs @@ -60,7 +60,7 @@ async fn start_inner( // Create the service. This is the most heavy initialization step. let (task_manager, rpc_handlers) = crate::service::new_light_base(config) - .map(|(components, rpc_handlers, _, _, _, _)| (components, rpc_handlers)) + .map(|(components, rpc_handlers, _, _, _)| (components, rpc_handlers)) .map_err(|e| format!("{:?}", e))?; task_manager.spawn_handle().spawn("telemetry", telemetry_worker.run()); diff --git a/bin/node/cli/src/chain_spec.rs b/bin/node/cli/src/chain_spec.rs index 7de9cfd0b6aa5..7bee74d6c677e 100644 --- a/bin/node/cli/src/chain_spec.rs +++ b/bin/node/cli/src/chain_spec.rs @@ -441,7 +441,7 @@ pub(crate) mod tests { Ok(sc_service_test::TestNetComponents::new(task_manager, client, network, transaction_pool)) }, |config| { - let (keep_alive, _, _, client, network, transaction_pool) = new_light_base(config)?; + let (keep_alive, _, client, network, transaction_pool) = new_light_base(config)?; Ok(sc_service_test::TestNetComponents::new(keep_alive, client, network, transaction_pool)) } ); diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index 8689176d0a806..c8957973e84bf 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -544,7 +544,7 @@ mod tests { Ok((node, (inherent_data_providers, setup_handles.unwrap()))) }, |config| { - let (keep_alive, _, _, client, network, transaction_pool) = new_light_base(config)?; + let (keep_alive, _, client, network, transaction_pool) = new_light_base(config)?; Ok(sc_service_test::TestNetComponents::new(keep_alive, client, network, transaction_pool)) }, |service, &mut (ref inherent_data_providers, (ref mut block_import, ref babe_link))| { @@ -621,9 +621,10 @@ mod tests { sp_consensus_babe::AuthorityId::ID, &alice.to_public_crypto_pair(), &to_sign, - ).unwrap() - .try_into() - .unwrap(); + ) + .unwrap() + .try_into() + .unwrap(); let item = ::babe_seal( signature, ); @@ -702,7 +703,7 @@ mod tests { Ok(sc_service_test::TestNetComponents::new(task_manager, client, network, transaction_pool)) }, |config| { - let (keep_alive, _, _, client, network, transaction_pool) = new_light_base(config)?; + let (keep_alive, _, client, network, transaction_pool) = new_light_base(config)?; Ok(sc_service_test::TestNetComponents::new(keep_alive, client, network, transaction_pool)) }, vec![ diff --git a/client/consensus/babe/src/tests.rs b/client/consensus/babe/src/tests.rs index 9d03a3266d615..5d91dcf170a3d 100644 --- a/client/consensus/babe/src/tests.rs +++ b/client/consensus/babe/src/tests.rs @@ -320,6 +320,7 @@ impl TestNetFactory for BabeTestNet { epoch_changes: data.link.epoch_changes.clone(), time_source: data.link.time_source.clone(), can_author_with: AlwaysCanAuthor, + telemetry: None, }, mutator: MUTATOR.with(|m| m.borrow().clone()), } diff --git a/client/finality-grandpa/src/authorities.rs b/client/finality-grandpa/src/authorities.rs index d98b8af449049..56a2b1126eb80 100644 --- a/client/finality-grandpa/src/authorities.rs +++ b/client/finality-grandpa/src/authorities.rs @@ -884,6 +884,7 @@ mod tests { _ => unreachable!(), }), false, + None, ).unwrap(); assert!(status.changed); @@ -903,6 +904,7 @@ mod tests { _ => unreachable!(), }), false, + None, ).unwrap(); assert!(status.changed); @@ -961,7 +963,7 @@ mod tests { // trying to finalize past `change_c` without finalizing `change_a` first assert!(matches!( - authorities.apply_standard_changes("hash_d", 40, &is_descendent_of, false), + authorities.apply_standard_changes("hash_d", 40, &is_descendent_of, false, None), Err(Error::ForkTree(fork_tree::Error::UnfinalizedAncestor)) )); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges::empty()); @@ -971,6 +973,7 @@ mod tests { 15, &is_descendent_of, false, + None, ).unwrap(); assert!(status.changed); @@ -986,6 +989,7 @@ mod tests { 40, &is_descendent_of, false, + None, ).unwrap(); assert!(status.changed); @@ -1128,7 +1132,7 @@ mod tests { // too early and there's no forced changes to apply. assert!( authorities - .apply_forced_changes("hash_a10", 10, &static_is_descendent_of(true), false) + .apply_forced_changes("hash_a10", 10, &static_is_descendent_of(true), false, None) .unwrap() .is_none() ); @@ -1136,7 +1140,7 @@ mod tests { // too late. assert!( authorities - .apply_forced_changes("hash_a16", 16, &is_descendent_of_a, false) + .apply_forced_changes("hash_a16", 16, &is_descendent_of_a, false, None) .unwrap() .is_none() ); @@ -1144,7 +1148,7 @@ mod tests { // on time -- chooses the right change for this fork. assert_eq!( authorities - .apply_forced_changes("hash_a15", 15, &is_descendent_of_a, false) + .apply_forced_changes("hash_a15", 15, &is_descendent_of_a, false, None) .unwrap() .unwrap(), ( @@ -1192,7 +1196,7 @@ mod tests { // it should be enacted at the same block that signaled it assert!( authorities - .apply_forced_changes("hash_a", 5, &static_is_descendent_of(false), false) + .apply_forced_changes("hash_a", 5, &static_is_descendent_of(false), false, None) .unwrap() .is_some() ); @@ -1259,27 +1263,27 @@ mod tests { // the forced change cannot be applied since the pending changes it depends on // have not been applied yet. assert!(matches!( - authorities.apply_forced_changes("hash_d45", 45, &static_is_descendent_of(true), false), + authorities.apply_forced_changes("hash_d45", 45, &static_is_descendent_of(true), false, None), Err(Error::ForcedAuthoritySetChangeDependencyUnsatisfied(15)) )); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges::empty()); // we apply the first pending standard change at #15 authorities - .apply_standard_changes("hash_a15", 15, &static_is_descendent_of(true), false) + .apply_standard_changes("hash_a15", 15, &static_is_descendent_of(true), false, None) .unwrap(); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15)])); // but the forced change still depends on the next standard change assert!(matches!( - authorities.apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false), + authorities.apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false, None), Err(Error::ForcedAuthoritySetChangeDependencyUnsatisfied(20)) )); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15)])); // we apply the pending standard change at #20 authorities - .apply_standard_changes("hash_b", 20, &static_is_descendent_of(true), false) + .apply_standard_changes("hash_b", 20, &static_is_descendent_of(true), false, None) .unwrap(); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15), (1, 20)])); @@ -1288,7 +1292,7 @@ mod tests { // at #35. subsequent forced changes on the same branch must be kept assert_eq!( authorities - .apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false) + .apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false, None) .unwrap() .unwrap(), ( @@ -1385,7 +1389,7 @@ mod tests { // we apply the change at A0 which should prune it and the fork at B authorities - .apply_standard_changes("hash_a0", 5, &is_descendent_of, false) + .apply_standard_changes("hash_a0", 5, &is_descendent_of, false, None) .unwrap(); // the next change is now at A1 (#10) @@ -1573,14 +1577,14 @@ mod tests { // applying the standard change at A should not prune anything // other then the change that was applied authorities - .apply_standard_changes("A", 5, &is_descendent_of, false) + .apply_standard_changes("A", 5, &is_descendent_of, false, None) .unwrap(); assert_eq!(authorities.pending_changes().count(), 6); // same for B authorities - .apply_standard_changes("B", 10, &is_descendent_of, false) + .apply_standard_changes("B", 10, &is_descendent_of, false, None) .unwrap(); assert_eq!(authorities.pending_changes().count(), 5); @@ -1589,7 +1593,7 @@ mod tests { // finalizing C2 should clear all forced changes authorities - .apply_standard_changes("C2", 15, &is_descendent_of, false) + .apply_standard_changes("C2", 15, &is_descendent_of, false, None) .unwrap(); assert_eq!(authorities.pending_forced_changes.len(), 0); @@ -1597,7 +1601,7 @@ mod tests { // finalizing C0 should clear all forced changes but D let mut authorities = authorities2; authorities - .apply_standard_changes("C0", 15, &is_descendent_of, false) + .apply_standard_changes("C0", 15, &is_descendent_of, false, None) .unwrap(); assert_eq!(authorities.pending_forced_changes.len(), 1); diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index 71ff8bc1f1c82..7413315b58461 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -1642,6 +1642,7 @@ mod tests { name: None, is_authority: true, observer_enabled: true, + telemetry: None, } } @@ -1809,6 +1810,7 @@ mod tests { config(), voter_set_state(), None, + None, ); let set_id = 1; @@ -1845,6 +1847,7 @@ mod tests { config(), voter_set_state(), None, + None, ); let set_id = 1; let auth = AuthorityId::from_slice(&[1u8; 32]); @@ -1890,6 +1893,7 @@ mod tests { config(), voter_set_state(), None, + None, ); let set_id = 1; @@ -1959,6 +1963,7 @@ mod tests { config(), set_state.clone(), None, + None, ); let set_id = 1; @@ -2014,6 +2019,7 @@ mod tests { config(), set_state.clone(), None, + None, ); // the validator starts at set id 2 @@ -2094,6 +2100,7 @@ mod tests { config(), voter_set_state(), None, + None, ); // the validator starts at set id 1. @@ -2168,6 +2175,7 @@ mod tests { config, voter_set_state(), None, + None, ); // the validator starts at set id 1. @@ -2202,6 +2210,7 @@ mod tests { config(), voter_set_state(), None, + None, ); // the validator starts at set id 1. @@ -2262,6 +2271,7 @@ mod tests { config, voter_set_state(), None, + None, ); // the validator starts at set id 1. @@ -2301,6 +2311,7 @@ mod tests { config(), voter_set_state(), None, + None, ); // the validator starts at set id 1. @@ -2334,6 +2345,7 @@ mod tests { config, voter_set_state(), None, + None, ); // the validator start at set id 0 @@ -2413,6 +2425,7 @@ mod tests { config(), voter_set_state(), None, + None, ); // the validator start at set id 0 @@ -2453,6 +2466,7 @@ mod tests { config, voter_set_state(), None, + None, ); // the validator start at set id 0 @@ -2502,7 +2516,7 @@ mod tests { #[test] fn only_gossip_commits_to_peers_on_same_set() { - let (val, _) = GossipValidator::::new(config(), voter_set_state(), None); + let (val, _) = GossipValidator::::new(config(), voter_set_state(), None, None); // the validator start at set id 1 val.note_set(SetId(1), Vec::new(), |_, _| {}); @@ -2580,7 +2594,7 @@ mod tests { #[test] fn expire_commits_from_older_rounds() { - let (val, _) = GossipValidator::::new(config(), voter_set_state(), None); + let (val, _) = GossipValidator::::new(config(), voter_set_state(), None, None); let commit = |round, set_id, target_number| { let commit = finality_grandpa::CompactCommit { @@ -2631,7 +2645,7 @@ mod tests { #[test] fn allow_noting_different_authorities_for_same_set() { - let (val, _) = GossipValidator::::new(config(), voter_set_state(), None); + let (val, _) = GossipValidator::::new(config(), voter_set_state(), None, None); let a1 = vec![AuthorityId::from_slice(&[0; 32])]; val.note_set(SetId(1), a1.clone(), |_, _| {}); diff --git a/client/finality-grandpa/src/communication/tests.rs b/client/finality-grandpa/src/communication/tests.rs index 4abea991cec37..dc37a1615f415 100644 --- a/client/finality-grandpa/src/communication/tests.rs +++ b/client/finality-grandpa/src/communication/tests.rs @@ -139,6 +139,7 @@ fn config() -> crate::Config { name: None, is_authority: true, observer_enabled: true, + telemetry: None, } } @@ -189,6 +190,7 @@ pub(crate) fn make_test_network() -> ( config(), voter_set_state(), None, + None, ); ( diff --git a/client/finality-grandpa/src/finality_proof.rs b/client/finality-grandpa/src/finality_proof.rs index e1e424472ff98..d9e0e578a8571 100644 --- a/client/finality-grandpa/src/finality_proof.rs +++ b/client/finality-grandpa/src/finality_proof.rs @@ -496,9 +496,6 @@ where .map_err(|_| ClientError::JustificationDecode)?; justification.verify(current_set_id, ¤t_authorities)?; - use sc_telemetry::{telemetry, CONSENSUS_INFO}; - telemetry!(CONSENSUS_INFO; "afg.finality_proof_ok"; - "finalized_header_hash" => ?proof.block); Ok(proof) } diff --git a/client/finality-grandpa/src/observer.rs b/client/finality-grandpa/src/observer.rs index 513b2dc501cc7..783896691616e 100644 --- a/client/finality-grandpa/src/observer.rs +++ b/client/finality-grandpa/src/observer.rs @@ -438,6 +438,7 @@ mod tests { None, voter_command_rx, None, + None, ); // Trigger a reputation change through the gossip validator. diff --git a/client/finality-grandpa/src/tests.rs b/client/finality-grandpa/src/tests.rs index 921b49db61c25..2cda1e52e92db 100644 --- a/client/finality-grandpa/src/tests.rs +++ b/client/finality-grandpa/src/tests.rs @@ -252,13 +252,14 @@ fn initialize_grandpa( name: Some(format!("peer#{}", peer_id)), is_authority: true, observer_enabled: true, + telemetry: None, }, link, network: net_service, - telemetry_on_connect: None, voting_rule: (), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), + telemetry: None, }; let voter = run_grandpa_voter(grandpa_params).expect("all in order with client and network"); @@ -395,13 +396,14 @@ fn finalize_3_voters_1_full_observer() { name: Some(format!("peer#{}", peer_id)), is_authority: true, observer_enabled: true, + telemetry: None, }, link: link, network: net_service, - telemetry_on_connect: None, voting_rule: (), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), + telemetry: None, }; run_grandpa_voter(grandpa_params).expect("all in order with client and network") @@ -488,13 +490,14 @@ fn transition_3_voters_twice_1_full_observer() { name: Some(format!("peer#{}", peer_id)), is_authority: true, observer_enabled: true, + telemetry: None, }, link, network: net_service, - telemetry_on_connect: None, voting_rule: (), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), + telemetry: None, }; voters.push(run_grandpa_voter(grandpa_params).expect("all in order with client and network")); @@ -921,6 +924,7 @@ fn voter_persists_its_votes() { name: Some(format!("peer#{}", 1)), is_authority: true, observer_enabled: true, + telemetry: None, }; let set_state = { @@ -939,6 +943,7 @@ fn voter_persists_its_votes() { config.clone(), set_state, None, + None, ) }; @@ -964,13 +969,14 @@ fn voter_persists_its_votes() { name: Some(format!("peer#{}", 0)), is_authority: true, observer_enabled: true, + telemetry: None, }, link, network: net_service, - telemetry_on_connect: None, voting_rule: VotingRulesBuilder::default().build(), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), + telemetry: None, }; run_grandpa_voter(grandpa_params).expect("all in order with client and network") @@ -1006,13 +1012,14 @@ fn voter_persists_its_votes() { name: Some(format!("peer#{}", 0)), is_authority: true, observer_enabled: true, + telemetry: None, }, link, network: net_service, - telemetry_on_connect: None, voting_rule: VotingRulesBuilder::default().build(), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), + telemetry: None, }; run_grandpa_voter(grandpa_params) @@ -1165,6 +1172,7 @@ fn finalize_3_voters_1_light_observer() { name: Some("observer".to_string()), is_authority: false, observer_enabled: true, + telemetry: None, }, net.peers[3].data.lock().take().expect("link initialized at startup; qed"), net.peers[3].network_service().clone(), @@ -1206,13 +1214,14 @@ fn voter_catches_up_to_latest_round_when_behind() { name: Some(format!("peer#{}", peer_id)), is_authority: true, observer_enabled: true, + telemetry: None, }, link, network: net.lock().peer(peer_id).network_service().clone(), - telemetry_on_connect: None, voting_rule: (), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), + telemetry: None, }; Box::pin(run_grandpa_voter(grandpa_params).expect("all in order with client and network")) @@ -1328,6 +1337,7 @@ where name: None, is_authority: true, observer_enabled: true, + telemetry: None, }; let network = NetworkBridge::new( @@ -1335,6 +1345,7 @@ where config.clone(), set_state.clone(), None, + None, ); Environment { @@ -1349,6 +1360,7 @@ where voting_rule, metrics: None, justification_sender: None, + telemetry: None, _phantom: PhantomData, } } diff --git a/client/service/Cargo.toml b/client/service/Cargo.toml index c6119695ace71..d402564be727c 100644 --- a/client/service/Cargo.toml +++ b/client/service/Cargo.toml @@ -91,5 +91,3 @@ grandpa = { version = "0.9.0", package = "sc-finality-grandpa", path = "../final grandpa-primitives = { version = "3.0.0", package = "sp-finality-grandpa", path = "../../primitives/finality-grandpa" } tokio = { version = "0.2.25", default-features = false } async-std = { version = "1.6.5", default-features = false } -tracing-subscriber = "0.2.15" -tracing-log = "0.1.1" From eb180deb842288ccc9e75ee1430003353f1bf6a4 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 17 Feb 2021 16:41:40 +0100 Subject: [PATCH 30/92] Doc --- client/telemetry/src/lib.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 3b1587a027396..3e92678b25e14 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -278,10 +278,8 @@ impl TelemetryWorker { let nodes = if let Some(nodes) = node_map.get(&id) { nodes } else { - // This is a normal error because the telemetry span is entered before the telemetry - // is initialized so it is possible that some messages in the beginning don't get - // through. - // TODO probably never happen + // This is a normal error because the telemetry ID exists sooner than the telemetry is + // initialized. log::trace!( target: "telemetry", "Received telemetry log for unknown id ({:?}): {}", @@ -336,7 +334,7 @@ pub struct TelemetryWorkerHandle { } impl TelemetryWorkerHandle { - /// TODO + /// Instantiate a new [`Telemetry`] object which is normally stored in the Substrate's Client. pub fn new_telemetry(&mut self, endpoints: TelemetryEndpoints) -> Telemetry { let addresses = endpoints.0.iter().map(|(addr, _)| addr.clone()).collect(); @@ -409,7 +407,7 @@ impl Telemetry { } } - /// TODO + /// Make a new clonable handle to this [`Telemetry`]. This is used for reporting telemetries. pub fn handle(&self) -> TelemetryHandle { TelemetryHandle { message_sender: self.message_sender.clone(), @@ -419,7 +417,7 @@ impl Telemetry { } } -/// TODO +/// Handle to a [`Telemetry`]. Used to report telemetries. #[derive(Debug, Clone)] pub struct TelemetryHandle { message_sender: mpsc::Sender, @@ -575,11 +573,12 @@ impl IdGenerator { } } -/// TODO +/// A trait used by the Substrate's Client to get a [`TelemetryHandle`] or to initialize the +/// [`Telemetry`]. pub trait ClientTelemetry { - /// TODO + /// Get a clonable handle to the telemetry if it exists. fn telemetry(&self) -> Option; - /// TODO + /// Initialize the [`Telemetry`]. fn start_telemetry(&self, connection_message: ConnectionMessage); } From f3f7c78e79ac4913404b47b2a34912730a1bf414 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 18 Feb 2021 11:30:20 +0100 Subject: [PATCH 31/92] Instantiate telemetry worker from sc-cli instead of sc-tracing --- Cargo.lock | 1 - client/cli/src/config.rs | 10 ++---- client/cli/src/lib.rs | 4 +-- client/cli/src/runner.rs | 5 ++- client/tracing/Cargo.toml | 1 - client/tracing/src/logging/mod.rs | 53 +++++++------------------------ 6 files changed, 20 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 907971a92b289..1184caed682ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7686,7 +7686,6 @@ dependencies = [ "parking_lot 0.11.1", "regex", "rustc-hash", - "sc-telemetry", "sc-tracing-proc-macro", "serde", "serde_json", diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index 45fc6da0defa2..8fbb2fb0571fc 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -579,16 +579,12 @@ pub trait CliConfiguration: Sized { /// 1. Sets the panic handler /// 2. Initializes the logger /// 3. Raises the FD limit - fn init(&self) -> Result { + fn init(&self) -> Result<()> { sp_panic_handler::set(&C::support_url(), &C::impl_version()); let mut logger = LoggerBuilder::new(self.log_filters()?); logger.with_log_reloading(!self.is_log_filter_reloading_disabled()?); - if let Some(transport) = self.telemetry_external_transport()? { - logger.with_transport(transport); - } - if let Some(tracing_targets) = self.tracing_targets()? { let tracing_receiver = self.tracing_receiver()?; logger.with_profiling(tracing_receiver, tracing_targets); @@ -598,7 +594,7 @@ pub trait CliConfiguration: Sized { logger.with_colors(false); } - let telemetry_worker = logger.init()?; + logger.init()?; if let Some(new_limit) = fdlimit::raise_fd_limit() { if new_limit < RECOMMENDED_OPEN_FILE_DESCRIPTOR_LIMIT { @@ -610,7 +606,7 @@ pub trait CliConfiguration: Sized { } } - Ok(telemetry_worker) + Ok(()) } } diff --git a/client/cli/src/lib.rs b/client/cli/src/lib.rs index dec71fd43bc82..093c3871483e6 100644 --- a/client/cli/src/lib.rs +++ b/client/cli/src/lib.rs @@ -222,8 +222,8 @@ pub trait SubstrateCli: Sized { /// Create a runner for the command provided in argument. This will create a Configuration and /// a tokio runtime fn create_runner(&self, command: &T) -> error::Result> { - let telemetry_worker = command.init::()?; - Runner::new(self, command, telemetry_worker) + command.init::()?; + Runner::new(self, command) } /// Native runtime version. diff --git a/client/cli/src/runner.rs b/client/cli/src/runner.rs index 4d72606de32cf..2396543bf054b 100644 --- a/client/cli/src/runner.rs +++ b/client/cli/src/runner.rs @@ -124,7 +124,6 @@ impl Runner { pub fn new( cli: &C, command: &T, - telemetry_worker: TelemetryWorker, ) -> Result> { let tokio_runtime = build_runtime()?; let runtime_handle = tokio_runtime.handle().clone(); @@ -138,6 +137,10 @@ impl Runner { } }; + let telemetry_worker = sc_telemetry::TelemetryWorker::new( + 16, + command.telemetry_external_transport()?, + )?; let telemetry_worker_handle = telemetry_worker.handle(); Ok(Runner { diff --git a/client/tracing/Cargo.toml b/client/tracing/Cargo.toml index 34aa9d9d4e7f0..a119fb48b34e2 100644 --- a/client/tracing/Cargo.toml +++ b/client/tracing/Cargo.toml @@ -30,7 +30,6 @@ tracing-core = "0.1.17" tracing-log = "0.1.1" tracing-subscriber = "0.2.15" sp-tracing = { version = "3.0.0", path = "../../primitives/tracing" } -sc-telemetry = { version = "3.0.0", path = "../telemetry" } sc-tracing-proc-macro = { version = "3.0.0", path = "./proc-macro" } [target.'cfg(target_os = "unknown")'.dependencies] diff --git a/client/tracing/src/logging/mod.rs b/client/tracing/src/logging/mod.rs index 3fbdc13341216..9186c61e57747 100644 --- a/client/tracing/src/logging/mod.rs +++ b/client/tracing/src/logging/mod.rs @@ -29,7 +29,6 @@ mod layers; pub use directives::*; pub use sc_tracing_proc_macro::*; -use sc_telemetry::{ExtTransport, TelemetryWorker}; use std::io; use tracing::Subscriber; use tracing_subscriber::{ @@ -76,8 +75,6 @@ fn prepare_subscriber( directives: &str, profiling_targets: Option<&str>, force_colors: Option, - telemetry_buffer_size: Option, - telemetry_external_transport: Option, builder_hook: impl Fn( SubscriberBuilder< format::DefaultFields, @@ -86,7 +83,7 @@ fn prepare_subscriber( fn() -> std::io::Stderr, >, ) -> SubscriberBuilder, -) -> Result<(impl Subscriber + for<'a> LookupSpan<'a>, TelemetryWorker)> +) -> Result LookupSpan<'a>> where N: for<'writer> FormatFields<'writer> + 'static, E: FormatEvent + 'static, @@ -164,10 +161,6 @@ where "%Y-%m-%d %H:%M:%S%.3f".to_string() }); - let telemetry_worker = sc_telemetry::TelemetryWorker::new( - telemetry_buffer_size.unwrap_or(16), - telemetry_external_transport, - )?; let event_format = EventFormat { timer, display_target: !simple, @@ -194,15 +187,13 @@ where #[cfg(target_os = "unknown")] let subscriber = subscriber.with(ConsoleLogLayer::new(event_format)); - Ok((subscriber, telemetry_worker)) + Ok(subscriber) } /// A builder that is used to initialize the global logger. pub struct LoggerBuilder { directives: String, profiling: Option<(crate::TracingReceiver, String)>, - telemetry_buffer_size: Option, - telemetry_external_transport: Option, log_reloading: bool, force_colors: Option, } @@ -213,8 +204,6 @@ impl LoggerBuilder { Self { directives: directives.into(), profiling: None, - telemetry_buffer_size: None, - telemetry_external_transport: None, log_reloading: true, force_colors: None, } @@ -236,18 +225,6 @@ impl LoggerBuilder { self } - /// Set a custom buffer size for the telemetry. - pub fn with_telemetry_buffer_size(&mut self, buffer_size: usize) -> &mut Self { - self.telemetry_buffer_size = Some(buffer_size); - self - } - - /// Set a custom network transport (used for the telemetry). - pub fn with_transport(&mut self, transport: ExtTransport) -> &mut Self { - self.telemetry_external_transport = Some(transport); - self - } - /// Force enable/disable colors. pub fn with_colors(&mut self, enable: bool) -> &mut Self { self.force_colors = Some(enable); @@ -257,64 +234,56 @@ impl LoggerBuilder { /// Initialize the global logger /// /// This sets various global logging and tracing instances and thus may only be called once. - pub fn init(self) -> Result { + pub fn init(self) -> Result<()> { if let Some((tracing_receiver, profiling_targets)) = self.profiling { if self.log_reloading { - let (subscriber, telemetry_worker) = prepare_subscriber( + let subscriber = prepare_subscriber( &self.directives, Some(&profiling_targets), self.force_colors, - self.telemetry_buffer_size, - self.telemetry_external_transport, |builder| enable_log_reloading!(builder), )?; let profiling = crate::ProfilingLayer::new(tracing_receiver, &profiling_targets); tracing::subscriber::set_global_default(subscriber.with(profiling))?; - Ok(telemetry_worker) + Ok(()) } else { - let (subscriber, telemetry_worker) = prepare_subscriber( + let subscriber = prepare_subscriber( &self.directives, Some(&profiling_targets), self.force_colors, - self.telemetry_buffer_size, - self.telemetry_external_transport, |builder| builder, )?; let profiling = crate::ProfilingLayer::new(tracing_receiver, &profiling_targets); tracing::subscriber::set_global_default(subscriber.with(profiling))?; - Ok(telemetry_worker) + Ok(()) } } else { if self.log_reloading { - let (subscriber, telemetry_worker) = prepare_subscriber( + let subscriber = prepare_subscriber( &self.directives, None, self.force_colors, - self.telemetry_buffer_size, - self.telemetry_external_transport, |builder| enable_log_reloading!(builder), )?; tracing::subscriber::set_global_default(subscriber)?; - Ok(telemetry_worker) + Ok(()) } else { - let (subscriber, telemetry_worker) = prepare_subscriber( + let subscriber = prepare_subscriber( &self.directives, None, self.force_colors, - self.telemetry_buffer_size, - self.telemetry_external_transport, |builder| builder, )?; tracing::subscriber::set_global_default(subscriber)?; - Ok(telemetry_worker) + Ok(()) } } } From 985eb93f2b4a6bfa1f63c742b07324c889831029 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 18 Feb 2021 11:34:17 +0100 Subject: [PATCH 32/92] Update doc --- client/tracing/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/tracing/src/lib.rs b/client/tracing/src/lib.rs index 89b8c330ef411..41947d4c0ed8e 100644 --- a/client/tracing/src/lib.rs +++ b/client/tracing/src/lib.rs @@ -24,7 +24,7 @@ //! //! See `sp-tracing` for examples on how to use tracing. //! -//! Currently we provide `Log` (default), `TelemetryHandle` variants for `Receiver` +//! Currently we only provide `Log` (default). #![warn(missing_docs)] From 4eca8bf858a40076ca4f19393fc7d0a18bb01a2c Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 18 Feb 2021 11:35:15 +0100 Subject: [PATCH 33/92] Rename send() to send_telemetry() --- client/telemetry/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 3e92678b25e14..0c874f7439ab7 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -427,7 +427,7 @@ pub struct TelemetryHandle { impl TelemetryHandle { /// Send telemetries. - pub fn send(&mut self, verbosity: VerbosityLevel, payload: TelemetryPayload) { + pub fn send_telemetry(&mut self, verbosity: VerbosityLevel, payload: TelemetryPayload) { match self.message_sender.try_send((self.id, verbosity, payload)) { Ok(()) => {} Err(err) if err.is_full() => todo!("overflow"), @@ -517,7 +517,7 @@ macro_rules! telemetry { }, Ok(mut json) => { json.insert("msg".into(), $msg.into()); - telemetry.send(verbosity, json); + telemetry.send_telemetry(verbosity, json); }, } } From dabdb149faf098f47e5066ad86ee9fb895db9a18 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 18 Feb 2021 11:39:48 +0100 Subject: [PATCH 34/92] Fix doc --- client/telemetry/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 0c874f7439ab7..c13a0c985de6c 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -438,7 +438,7 @@ impl TelemetryHandle { /// Get event stream for telemetry connection established events. /// /// This function will return an error if the telemetry has already been started by - /// [`TelemetryWorkerHandle::start_telemetry`]. + /// [`Telemetry::start_telemetry`]. pub fn on_connect_stream(&self) -> TracingUnboundedReceiver<()> { self.connection_notifier.on_connect_stream() } From 3f9e73e35a9d9a3654c31bc3bf563c0af33d9c69 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 18 Feb 2021 11:50:18 +0100 Subject: [PATCH 35/92] Remove IdGenerator and global variable --- client/telemetry/src/lib.rs | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index c13a0c985de6c..33759ffea59e5 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -45,6 +45,7 @@ use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnbound use std::collections::HashMap; use std::io; use std::mem; +use std::sync::Arc; pub use libp2p::wasm_ext::ExtTransport; pub use log; @@ -72,8 +73,6 @@ pub const CONSENSUS_WARN: VerbosityLevel = 4; /// Consensus INFO log level. pub const CONSENSUS_INFO: VerbosityLevel = 1; -static TELEMETRY_ID_COUNTER: CounterU64 = CounterU64::new(1); - /// Telemetry message verbosity. pub type VerbosityLevel = u8; @@ -115,7 +114,7 @@ pub struct TelemetryWorker { message_sender: mpsc::Sender, register_receiver: mpsc::UnboundedReceiver, register_sender: mpsc::UnboundedSender, - id_generator: IdGenerator, + id_generator: Arc, transport: WsTrans, } @@ -132,7 +131,7 @@ impl TelemetryWorker { message_sender, register_receiver, register_sender, - id_generator: IdGenerator::new(), + id_generator: Arc::new(CounterU64::new(1)), transport, }) } @@ -330,7 +329,7 @@ impl TelemetryWorker { pub struct TelemetryWorkerHandle { message_sender: mpsc::Sender, register_sender: mpsc::UnboundedSender, - id_generator: IdGenerator, + id_generator: Arc, } impl TelemetryWorkerHandle { @@ -341,7 +340,7 @@ impl TelemetryWorkerHandle { Telemetry { message_sender: self.message_sender.clone(), register_sender: self.register_sender.clone(), - id: self.id_generator.next(), + id: self.id_generator.inc(), connection_notifier: TelemetryConnectionNotifier { register_sender: self.register_sender.clone(), addresses, @@ -560,19 +559,6 @@ macro_rules! format_fields_to_json { }}; } -#[derive(Debug, Clone)] -struct IdGenerator; - -impl IdGenerator { - fn new() -> Self { - Self - } - - fn next(&mut self) -> Id { - TELEMETRY_ID_COUNTER.inc() - } -} - /// A trait used by the Substrate's Client to get a [`TelemetryHandle`] or to initialize the /// [`Telemetry`]. pub trait ClientTelemetry { From eaeb79a305c0fa60f6f812d846679eabd1428759 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 18 Feb 2021 12:12:04 +0100 Subject: [PATCH 36/92] Pass the telemetry worker handle down from the runner instead of the config --- bin/node/cli/src/command.rs | 15 ++++++------- bin/node/cli/src/service.rs | 37 +++++++++++++++++++++++---------- bin/node/inspect/src/command.rs | 2 +- client/cli/src/config.rs | 1 - client/service/src/builder.rs | 10 ++++++--- client/service/src/config.rs | 5 ----- 6 files changed, 42 insertions(+), 28 deletions(-) diff --git a/bin/node/cli/src/command.rs b/bin/node/cli/src/command.rs index 461930a613d97..dfdc57cafb357 100644 --- a/bin/node/cli/src/command.rs +++ b/bin/node/cli/src/command.rs @@ -75,10 +75,11 @@ pub fn run() -> Result<()> { match &cli.subcommand { None => { let runner = cli.create_runner(&cli.run)?; + let telemetry_worker_handle = runner.telemetry_worker_handle(); runner.run_node_until_exit(|config| async move { match config.role { - Role::Light => service::new_light(config), - _ => service::new_full(config), + Role::Light => service::new_light(config, Some(telemetry_worker_handle)), + _ => service::new_full(config, Some(telemetry_worker_handle)), }.map_err(sc_cli::Error::Service) }) } @@ -109,7 +110,7 @@ pub fn run() -> Result<()> { let runner = cli.create_runner(cmd)?; runner.async_run(|config| { let PartialComponents { client, task_manager, import_queue, ..} - = new_partial(&config)?; + = new_partial(&config, None)?; Ok((cmd.run(client, import_queue), task_manager)) }) }, @@ -117,7 +118,7 @@ pub fn run() -> Result<()> { let runner = cli.create_runner(cmd)?; runner.async_run(|config| { let PartialComponents { client, task_manager, ..} - = new_partial(&config)?; + = new_partial(&config, None)?; Ok((cmd.run(client, config.database), task_manager)) }) }, @@ -125,7 +126,7 @@ pub fn run() -> Result<()> { let runner = cli.create_runner(cmd)?; runner.async_run(|config| { let PartialComponents { client, task_manager, ..} - = new_partial(&config)?; + = new_partial(&config, None)?; Ok((cmd.run(client, config.chain_spec), task_manager)) }) }, @@ -133,7 +134,7 @@ pub fn run() -> Result<()> { let runner = cli.create_runner(cmd)?; runner.async_run(|config| { let PartialComponents { client, task_manager, import_queue, ..} - = new_partial(&config)?; + = new_partial(&config, None)?; Ok((cmd.run(client, import_queue), task_manager)) }) }, @@ -145,7 +146,7 @@ pub fn run() -> Result<()> { let runner = cli.create_runner(cmd)?; runner.async_run(|config| { let PartialComponents { client, task_manager, backend, ..} - = new_partial(&config)?; + = new_partial(&config, None)?; Ok((cmd.run(client, backend), task_manager)) }) }, diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index c8957973e84bf..ed319eba0ede7 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -34,7 +34,7 @@ use sp_runtime::traits::Block as BlockT; use futures::prelude::*; use sc_client_api::{ExecutorProvider, RemoteBackend}; use node_executor::Executor; -use sc_telemetry::ClientTelemetry; +use sc_telemetry::{ClientTelemetry, TelemetryWorkerHandle}; type FullClient = sc_service::TFullClient; type FullBackend = sc_service::TFullBackend; @@ -43,7 +43,10 @@ type FullGrandpaBlockImport = grandpa::GrandpaBlockImport; type LightClient = sc_service::TLightClient; -pub fn new_partial(config: &Configuration) -> Result, +) -> Result, sc_transaction_pool::FullPool, @@ -61,7 +64,7 @@ pub fn new_partial(config: &Configuration) -> Result, ServiceError> { let (client, backend, keystore_container, task_manager) = - sc_service::new_full_parts::(&config)?; + sc_service::new_full_parts::(&config, telemetry_worker_handle)?; let client = Arc::new(client); let select_chain = sc_consensus::LongestChain::new(backend.clone()); @@ -177,6 +180,7 @@ pub struct NewFullBase { /// Creates a full service from the configuration. pub fn new_full_base( mut config: Configuration, + telemetry_worker_handle: Option, with_startup_data: impl FnOnce( &sc_consensus_babe::BabeBlockImport, &sc_consensus_babe::BabeLink, @@ -192,7 +196,7 @@ pub fn new_full_base( transaction_pool, inherent_data_providers, other: (rpc_extensions_builder, import_setup, rpc_setup), - } = new_partial(&config)?; + } = new_partial(&config, telemetry_worker_handle)?; let shared_voter_state = rpc_setup; @@ -355,14 +359,19 @@ pub fn new_full_base( } /// Builds a new service for a full client. -pub fn new_full(config: Configuration) --> Result { - new_full_base(config, |_, _| ()).map(|NewFullBase { task_manager, .. }| { +pub fn new_full( + config: Configuration, + telemetry_worker_handle: Option, +) -> Result { + new_full_base(config, telemetry_worker_handle, |_, _| ()).map(|NewFullBase { task_manager, .. }| { task_manager }) } -pub fn new_light_base(mut config: Configuration) -> Result<( +pub fn new_light_base( + mut config: Configuration, + telemetry_worker_handle: Option, +) -> Result<( TaskManager, RpcHandlers, Arc, @@ -370,7 +379,10 @@ pub fn new_light_base(mut config: Configuration) -> Result<( Arc>> ), ServiceError> { let (client, backend, keystore_container, mut task_manager, on_demand) = - sc_service::new_light_parts::(&config)?; + sc_service::new_light_parts::( + &config, + telemetry_worker_handle, + )?; config.network.extra_sets.push(grandpa::grandpa_peers_set_config()); @@ -461,8 +473,11 @@ pub fn new_light_base(mut config: Configuration) -> Result<( } /// Builds a new service for a light client. -pub fn new_light(config: Configuration) -> Result { - new_light_base(config).map(|(task_manager, _, _, _, _)| { +pub fn new_light( + config: Configuration, + telemetry_worker_handle: Option, +) -> Result { + new_light_base(config, telemetry_worker_handle).map(|(task_manager, _, _, _, _)| { task_manager }) } diff --git a/bin/node/inspect/src/command.rs b/bin/node/inspect/src/command.rs index a1a9c947a561b..9c14a71375f5f 100644 --- a/bin/node/inspect/src/command.rs +++ b/bin/node/inspect/src/command.rs @@ -34,7 +34,7 @@ impl InspectCmd { RA: Send + Sync + 'static, EX: NativeExecutionDispatch + 'static, { - let client = new_full_client::(&config)?; + let client = new_full_client::(&config, None)?; let inspect = Inspector::::new(client); match &self.command { diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index 8fbb2fb0571fc..6aeebc6ca7913 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -548,7 +548,6 @@ pub trait CliConfiguration: Sized { role, base_path: Some(base_path), informant_output_format: Default::default(), - telemetry_worker_handle, }) } diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index a8e8af13e3049..2649b658dbfb0 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -58,6 +58,7 @@ use sc_telemetry::{ ConnectionMessage, Telemetry, TelemetryHandle, + TelemetryWorkerHandle, SUBSTRATE_INFO, }; use sp_transaction_pool::MaintainedTransactionPool; @@ -292,16 +293,18 @@ impl KeystoreContainer { /// Creates a new full client for the given config. pub fn new_full_client( config: &Configuration, + telemetry_worker_handle: Option, ) -> Result, Error> where TBl: BlockT, TExecDisp: NativeExecutionDispatch + 'static, { - new_full_parts(config).map(|parts| parts.0) + new_full_parts(config, telemetry_worker_handle).map(|parts| parts.0) } /// Create the initial parts of a full node. pub fn new_full_parts( config: &Configuration, + telemetry_worker_handle: Option, ) -> Result, Error> where TBl: BlockT, TExecDisp: NativeExecutionDispatch + 'static, @@ -344,7 +347,7 @@ pub fn new_full_parts( Some(keystore_container.sync_keystore()), ); - let telemetry = match (config.telemetry_worker_handle.clone(), config.telemetry_endpoints.clone()) { + let telemetry = match (telemetry_worker_handle, config.telemetry_endpoints.clone()) { (Some(mut handle), Some(endpoints)) => Some(handle.new_telemetry(endpoints)), _ => None, }; @@ -378,6 +381,7 @@ pub fn new_full_parts( /// Create the initial parts of a light node. pub fn new_light_parts( config: &Configuration, + telemetry_worker_handle: Option, ) -> Result, Error> where TBl: BlockT, TExecDisp: NativeExecutionDispatch + 'static, @@ -416,7 +420,7 @@ pub fn new_light_parts( ); let on_demand = Arc::new(sc_network::config::OnDemand::new(fetch_checker)); let backend = sc_light::new_light_backend(light_blockchain); - let telemetry = match (config.telemetry_worker_handle.clone(), config.telemetry_endpoints.clone()) { + let telemetry = match (telemetry_worker_handle, config.telemetry_endpoints.clone()) { (Some(mut handle), Some(endpoints)) => Some(handle.new_telemetry(endpoints)), _ => None, }; diff --git a/client/service/src/config.rs b/client/service/src/config.rs index f683eefed1a58..f82a877545e8c 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -96,11 +96,6 @@ pub struct Configuration { /// External WASM transport for the telemetry. If `Some`, when connection to a telemetry /// endpoint, this transport will be tried in priority before all others. pub telemetry_external_transport: Option, - /// Telemetry handle. - /// - /// This is a handle to a `TelemetryWorker` instance. It is used to initialize the telemetry for - /// a substrate node. - pub telemetry_worker_handle: Option, /// The default number of 64KB pages to allocate for Wasm execution pub default_heap_pages: Option, /// Should offchain workers be executed. From 6f0563f90a57a2198db3f70c25d7d5636552e283 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 18 Feb 2021 12:40:15 +0100 Subject: [PATCH 37/92] Moving TelemetryWorker creation to the user --- bin/node/cli/src/command.rs | 15 +++++++-------- bin/node/cli/src/service.rs | 22 +++++++++++----------- client/cli/src/config.rs | 9 +-------- client/cli/src/lib.rs | 4 +--- client/cli/src/runner.rs | 18 ------------------ 5 files changed, 20 insertions(+), 48 deletions(-) diff --git a/bin/node/cli/src/command.rs b/bin/node/cli/src/command.rs index dfdc57cafb357..461930a613d97 100644 --- a/bin/node/cli/src/command.rs +++ b/bin/node/cli/src/command.rs @@ -75,11 +75,10 @@ pub fn run() -> Result<()> { match &cli.subcommand { None => { let runner = cli.create_runner(&cli.run)?; - let telemetry_worker_handle = runner.telemetry_worker_handle(); runner.run_node_until_exit(|config| async move { match config.role { - Role::Light => service::new_light(config, Some(telemetry_worker_handle)), - _ => service::new_full(config, Some(telemetry_worker_handle)), + Role::Light => service::new_light(config), + _ => service::new_full(config), }.map_err(sc_cli::Error::Service) }) } @@ -110,7 +109,7 @@ pub fn run() -> Result<()> { let runner = cli.create_runner(cmd)?; runner.async_run(|config| { let PartialComponents { client, task_manager, import_queue, ..} - = new_partial(&config, None)?; + = new_partial(&config)?; Ok((cmd.run(client, import_queue), task_manager)) }) }, @@ -118,7 +117,7 @@ pub fn run() -> Result<()> { let runner = cli.create_runner(cmd)?; runner.async_run(|config| { let PartialComponents { client, task_manager, ..} - = new_partial(&config, None)?; + = new_partial(&config)?; Ok((cmd.run(client, config.database), task_manager)) }) }, @@ -126,7 +125,7 @@ pub fn run() -> Result<()> { let runner = cli.create_runner(cmd)?; runner.async_run(|config| { let PartialComponents { client, task_manager, ..} - = new_partial(&config, None)?; + = new_partial(&config)?; Ok((cmd.run(client, config.chain_spec), task_manager)) }) }, @@ -134,7 +133,7 @@ pub fn run() -> Result<()> { let runner = cli.create_runner(cmd)?; runner.async_run(|config| { let PartialComponents { client, task_manager, import_queue, ..} - = new_partial(&config, None)?; + = new_partial(&config)?; Ok((cmd.run(client, import_queue), task_manager)) }) }, @@ -146,7 +145,7 @@ pub fn run() -> Result<()> { let runner = cli.create_runner(cmd)?; runner.async_run(|config| { let PartialComponents { client, task_manager, backend, ..} - = new_partial(&config, None)?; + = new_partial(&config)?; Ok((cmd.run(client, backend), task_manager)) }) }, diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index ed319eba0ede7..7724b6c4e694f 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -34,7 +34,7 @@ use sp_runtime::traits::Block as BlockT; use futures::prelude::*; use sc_client_api::{ExecutorProvider, RemoteBackend}; use node_executor::Executor; -use sc_telemetry::{ClientTelemetry, TelemetryWorkerHandle}; +use sc_telemetry::{ClientTelemetry, TelemetryWorker}; type FullClient = sc_service::TFullClient; type FullBackend = sc_service::TFullBackend; @@ -45,7 +45,6 @@ type LightClient = sc_service::TLightClient; pub fn new_partial( config: &Configuration, - telemetry_worker_handle: Option, ) -> Result, @@ -63,8 +62,12 @@ pub fn new_partial( grandpa::SharedVoterState, ) >, ServiceError> { + let telemetry_worker = TelemetryWorker::new(16, None)?; let (client, backend, keystore_container, task_manager) = - sc_service::new_full_parts::(&config, telemetry_worker_handle)?; + sc_service::new_full_parts::( + &config, + Some(telemetry_worker.handle()), + )?; let client = Arc::new(client); let select_chain = sc_consensus::LongestChain::new(backend.clone()); @@ -180,7 +183,6 @@ pub struct NewFullBase { /// Creates a full service from the configuration. pub fn new_full_base( mut config: Configuration, - telemetry_worker_handle: Option, with_startup_data: impl FnOnce( &sc_consensus_babe::BabeBlockImport, &sc_consensus_babe::BabeLink, @@ -196,7 +198,7 @@ pub fn new_full_base( transaction_pool, inherent_data_providers, other: (rpc_extensions_builder, import_setup, rpc_setup), - } = new_partial(&config, telemetry_worker_handle)?; + } = new_partial(&config)?; let shared_voter_state = rpc_setup; @@ -361,16 +363,14 @@ pub fn new_full_base( /// Builds a new service for a full client. pub fn new_full( config: Configuration, - telemetry_worker_handle: Option, ) -> Result { - new_full_base(config, telemetry_worker_handle, |_, _| ()).map(|NewFullBase { task_manager, .. }| { + new_full_base(config, |_, _| ()).map(|NewFullBase { task_manager, .. }| { task_manager }) } pub fn new_light_base( mut config: Configuration, - telemetry_worker_handle: Option, ) -> Result<( TaskManager, RpcHandlers, @@ -378,10 +378,11 @@ pub fn new_light_base( Arc::Hash>>, Arc>> ), ServiceError> { + let telemetry_worker = TelemetryWorker::new(16, None)?; let (client, backend, keystore_container, mut task_manager, on_demand) = sc_service::new_light_parts::( &config, - telemetry_worker_handle, + Some(telemetry_worker.handle()), )?; config.network.extra_sets.push(grandpa::grandpa_peers_set_config()); @@ -475,9 +476,8 @@ pub fn new_light_base( /// Builds a new service for a light client. pub fn new_light( config: Configuration, - telemetry_worker_handle: Option, ) -> Result { - new_light_base(config, telemetry_worker_handle).map(|(task_manager, _, _, _, _)| { + new_light_base(config).map(|(task_manager, _, _, _, _)| { task_manager }) } diff --git a/client/cli/src/config.rs b/client/cli/src/config.rs index 6aeebc6ca7913..289d6dc7cc39f 100644 --- a/client/cli/src/config.rs +++ b/client/cli/src/config.rs @@ -33,7 +33,6 @@ use sc_service::config::{ TaskExecutor, TelemetryEndpoints, TransactionPoolOptions, WasmExecutionMethod, }; use sc_service::{ChainSpec, TracingReceiver, KeepBlocks, TransactionStorageMode}; -use sc_telemetry::TelemetryWorkerHandle; use sc_tracing::logging::LoggerBuilder; use std::net::SocketAddr; use std::path::PathBuf; @@ -470,7 +469,6 @@ pub trait CliConfiguration: Sized { &self, cli: &C, task_executor: TaskExecutor, - telemetry_worker_handle: Option, ) -> Result { let is_dev = self.is_dev()?; let chain_id = self.chain_id(is_dev)?; @@ -488,12 +486,7 @@ pub trait CliConfiguration: Sized { let max_runtime_instances = self.max_runtime_instances()?.unwrap_or(8); let is_validator = role.is_authority(); let (keystore_remote, keystore) = self.keystore_config(&config_dir)?; - let telemetry_endpoints = telemetry_worker_handle - .as_ref() - .and_then(|_| self.telemetry_endpoints(&chain_spec).transpose()) - .transpose()? - // Don't initialise telemetry if `telemetry_endpoints` == Some([]) - .filter(|x| !x.is_empty()); + let telemetry_endpoints = self.telemetry_endpoints(&chain_spec)?; let unsafe_pruning = self .import_params() diff --git a/client/cli/src/lib.rs b/client/cli/src/lib.rs index 093c3871483e6..f81c5160ca82b 100644 --- a/client/cli/src/lib.rs +++ b/client/cli/src/lib.rs @@ -37,7 +37,6 @@ pub use params::*; pub use runner::*; pub use sc_service::{ChainSpec, Role}; use sc_service::{Configuration, TaskExecutor}; -use sc_telemetry::TelemetryWorkerHandle; pub use sc_tracing::logging::LoggerBuilder; pub use sp_version::RuntimeVersion; use std::io::Write; @@ -214,9 +213,8 @@ pub trait SubstrateCli: Sized { &self, command: &T, task_executor: TaskExecutor, - telemetry_worker_handle: Option, ) -> error::Result { - command.create_configuration(self, task_executor, telemetry_worker_handle) + command.create_configuration(self, task_executor) } /// Create a runner for the command provided in argument. This will create a Configuration and diff --git a/client/cli/src/runner.rs b/client/cli/src/runner.rs index 2396543bf054b..b512588a204c8 100644 --- a/client/cli/src/runner.rs +++ b/client/cli/src/runner.rs @@ -25,7 +25,6 @@ use futures::select; use futures::{future, future::FutureExt, Future}; use log::info; use sc_service::{Configuration, TaskType, TaskManager}; -use sc_telemetry::{TelemetryWorkerHandle, TelemetryWorker}; use sp_utils::metrics::{TOKIO_THREADS_ALIVE, TOKIO_THREADS_TOTAL}; use std::marker::PhantomData; use sc_service::Error as ServiceError; @@ -115,7 +114,6 @@ where pub struct Runner { config: Configuration, tokio_runtime: tokio::runtime::Runtime, - telemetry_worker: TelemetryWorker, phantom: PhantomData, } @@ -137,20 +135,12 @@ impl Runner { } }; - let telemetry_worker = sc_telemetry::TelemetryWorker::new( - 16, - command.telemetry_external_transport()?, - )?; - let telemetry_worker_handle = telemetry_worker.handle(); - Ok(Runner { config: command.create_configuration( cli, task_executor.into(), - Some(telemetry_worker_handle), )?, tokio_runtime, - telemetry_worker, phantom: PhantomData, }) } @@ -200,7 +190,6 @@ impl Runner { { self.print_node_infos(); let mut task_manager = self.tokio_runtime.block_on(initialize(self.config))?; - task_manager.spawn_handle().spawn("telemetry_worker", self.telemetry_worker.run()); let res = self.tokio_runtime.block_on(main(task_manager.future().fuse())); self.tokio_runtime.block_on(task_manager.clean_shutdown()); Ok(res?) @@ -239,11 +228,4 @@ impl Runner { pub fn config_mut(&mut self) -> &mut Configuration { &mut self.config } - - /// Get a new [`TelemetryWorkerHandle`]. - /// - /// This is used when you want to register with the [`TelemetryWorker`]. - pub fn telemetry_worker_handle(&self) -> TelemetryWorkerHandle { - self.telemetry_worker.handle() - } } From 81f7df82077949fe79fbdd5d34fe936e16c5a889 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 19 Feb 2021 11:09:26 +0100 Subject: [PATCH 38/92] Make Tracing bounded channel and use it in telemetry --- client/telemetry/src/lib.rs | 9 +- client/telemetry/src/node.rs | 8 +- primitives/utils/src/mpsc.rs | 195 ++++++++++++++++++++++++++++++++++- 3 files changed, 202 insertions(+), 10 deletions(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 33759ffea59e5..3c9c14d29f362 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -41,7 +41,6 @@ use global_counter::primitive::exact::CounterU64; use libp2p::Multiaddr; use log::{error, warn}; use serde::Serialize; -use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; use std::collections::HashMap; use std::io; use std::mem; @@ -186,7 +185,7 @@ impl TelemetryWorker { input: Option, node_pool: &mut HashMap>, node_map: &mut HashMap>, - pending_connection_notifications: &mut Vec<(Multiaddr, TracingUnboundedSender<()>)>, + pending_connection_notifications: &mut Vec<(Multiaddr, ConnectionNotifierSender)>, transport: WsTrans, ) { let input = input.expect("the stream is never closed; qed"); @@ -438,7 +437,7 @@ impl TelemetryHandle { /// /// This function will return an error if the telemetry has already been started by /// [`Telemetry::start_telemetry`]. - pub fn on_connect_stream(&self) -> TracingUnboundedReceiver<()> { + pub fn on_connect_stream(&self) -> ConnectionNotifierReceiver { self.connection_notifier.on_connect_stream() } } @@ -452,8 +451,8 @@ pub struct TelemetryConnectionNotifier { } impl TelemetryConnectionNotifier { - fn on_connect_stream(&self) -> TracingUnboundedReceiver<()> { - let (message_sender, message_receiver) = tracing_unbounded("mpsc_telemetry_on_connect"); + fn on_connect_stream(&self) -> ConnectionNotifierReceiver { + let (message_sender, message_receiver) = connection_notifier_channel(); if let Err(err) = self.register_sender.unbounded_send(Register::Notifier { addresses: self.addresses.clone(), connection_notifier: message_sender, diff --git a/client/telemetry/src/node.rs b/client/telemetry/src/node.rs index a9c1d71889b0c..0ecc9d6007e3b 100644 --- a/client/telemetry/src/node.rs +++ b/client/telemetry/src/node.rs @@ -24,7 +24,13 @@ use rand::Rng as _; use std::{fmt, mem, pin::Pin, task::Context, task::Poll, time::Duration}; use wasm_timer::Delay; -pub(crate) type ConnectionNotifierSender = sp_utils::mpsc::TracingUnboundedSender<()>; +pub(crate) type ConnectionNotifierSender = sp_utils::mpsc::TracingSender<()>; +pub(crate) type ConnectionNotifierReceiver = sp_utils::mpsc::TracingReceiver<()>; + +pub(crate) fn connection_notifier_channel() -> (ConnectionNotifierSender, ConnectionNotifierReceiver) +{ + sp_utils::mpsc::tracing_channel("mpsc_telemetry_on_connect", 0) +} /// Handler for a single telemetry node. /// diff --git a/primitives/utils/src/mpsc.rs b/primitives/utils/src/mpsc.rs index b033a5527d84a..66f891cc83b6f 100644 --- a/primitives/utils/src/mpsc.rs +++ b/primitives/utils/src/mpsc.rs @@ -20,14 +20,24 @@ #[cfg(not(feature = "metered"))] mod inner { // just aliased, non performance implications - use futures::channel::mpsc::{self, UnboundedReceiver, UnboundedSender}; + use futures::channel::mpsc::{self, UnboundedReceiver, UnboundedSender, Receiver, Sender}; pub type TracingUnboundedSender = UnboundedSender; pub type TracingUnboundedReceiver = UnboundedReceiver; + pub type TracingSender = Sender; + pub type TracingReceiver = Receiver; /// Alias `mpsc::unbounded` pub fn tracing_unbounded(_key: &'static str) ->(TracingUnboundedSender, TracingUnboundedReceiver) { mpsc::unbounded() } + + /// Alias `mpsc::channel` + pub fn tracing_channel( + _key: &'static str, + buffer: usize, + ) -> (TracingUnboundedSender, TracingUnboundedReceiver) { + mpsc::channel() + } } @@ -35,7 +45,7 @@ mod inner { mod inner { //tracing implementation use futures::channel::mpsc::{self, - UnboundedReceiver, UnboundedSender, + UnboundedReceiver, UnboundedSender, Receiver, Sender, TryRecvError, TrySendError, SendError }; use futures::{sink::Sink, task::{Poll, Context}, stream::{Stream, FusedStream}}; @@ -167,7 +177,7 @@ mod inner { Poll::Ready(msg) => { if msg.is_some() { UNBOUNDED_CHANNELS_COUNTER.with_label_values(&[s.0, "received"]).inc(); - } + } Poll::Ready(msg) } Poll::Pending => { @@ -246,6 +256,183 @@ mod inner { Poll::Ready(Ok(())) } } + + /// Wrapper Type around `Sender` that increases the global + /// measure when a message is added + #[derive(Debug)] + pub struct TracingSender(&'static str, Sender); + + // Strangely, deriving `Clone` requires that `T` is also `Clone`. + impl Clone for TracingSender { + fn clone(&self) -> Self { + Self(self.0, self.1.clone()) + } + } + + /// Wrapper Type around `Receiver` that decreases the global + /// measure when a message is polled + #[derive(Debug)] + pub struct TracingReceiver(&'static str, Receiver); + + /// Wrapper around `mpsc::unbounded` that tracks the in- and outflow via + /// `UNBOUNDED_CHANNELS_COUNTER` + pub fn tracing_channel( + key: &'static str, + buffer: usize, + ) -> (TracingSender, TracingReceiver) { + let (s, r) = mpsc::channel(buffer); + (TracingSender(key, s), TracingReceiver(key,r)) + } + + impl TracingSender { + /// Proxy function to mpsc::Sender + pub fn poll_ready(&mut self, ctx: &mut Context) -> Poll> { + self.1.poll_ready(ctx) + } + + /// Proxy function to mpsc::Sender + pub fn is_closed(&self) -> bool { + self.1.is_closed() + } + + /// Proxy function to mpsc::Sender + pub fn close_channel(&mut self) { + self.1.close_channel() + } + + /// Proxy function to mpsc::Sender + pub fn disconnect(&mut self) { + self.1.disconnect() + } + + /// Proxy function to mpsc::Sender + pub fn start_send(&mut self, msg: T) -> Result<(), SendError> { + self.1.start_send(msg) + } + + /// Proxy function to mpsc::Sender + pub fn try_send(&mut self, msg: T) -> Result<(), TrySendError> { + self.1.try_send(msg).map(|s|{ + UNBOUNDED_CHANNELS_COUNTER.with_label_values(&[self.0, &"send"]).inc(); + s + }) + } + + /// Proxy function to mpsc::Sender + pub fn same_receiver(&self, other: &Sender) -> bool { + self.1.same_receiver(other) + } + } + + impl TracingReceiver { + + fn consume(&mut self) { + // consume all items, make sure to reflect the updated count + let mut count = 0; + loop { + if self.1.is_terminated() { + break; + } + + match self.try_next() { + Ok(Some(..)) => count += 1, + _ => break + } + } + // and discount the messages + if count > 0 { + UNBOUNDED_CHANNELS_COUNTER.with_label_values(&[self.0, &"dropped"]).inc_by(count); + } + + } + + /// Proxy function to mpsc::Receiver + /// that consumes all messages first and updates the counter + pub fn close(&mut self) { + self.consume(); + self.1.close() + } + + /// Proxy function to mpsc::Receiver + /// that discounts the messages taken out + pub fn try_next(&mut self) -> Result, TryRecvError> { + self.1.try_next().map(|s| { + if s.is_some() { + UNBOUNDED_CHANNELS_COUNTER.with_label_values(&[self.0, &"received"]).inc(); + } + s + }) + } + } + + impl Drop for TracingReceiver { + fn drop(&mut self) { + self.consume(); + } + } + + impl Unpin for TracingReceiver {} + + impl Stream for TracingReceiver { + type Item = T; + + fn poll_next( + self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll> { + let s = self.get_mut(); + match Pin::new(&mut s.1).poll_next(cx) { + Poll::Ready(msg) => { + if msg.is_some() { + UNBOUNDED_CHANNELS_COUNTER.with_label_values(&[s.0, "received"]).inc(); + } + Poll::Ready(msg) + } + Poll::Pending => { + Poll::Pending + } + } + } + } + + impl FusedStream for TracingReceiver { + fn is_terminated(&self) -> bool { + self.1.is_terminated() + } + } + + impl Sink for TracingSender { + type Error = SendError; + + fn poll_ready( + mut self: Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll> { + TracingSender::poll_ready(&mut *self, cx) + } + + fn start_send( + mut self: Pin<&mut Self>, + msg: T, + ) -> Result<(), Self::Error> { + TracingSender::start_send(&mut *self, msg) + } + + fn poll_flush( + self: Pin<&mut Self>, + _: &mut Context<'_>, + ) -> Poll> { + Poll::Ready(Ok(())) + } + + fn poll_close( + mut self: Pin<&mut Self>, + _: &mut Context<'_>, + ) -> Poll> { + self.disconnect(); + Poll::Ready(Ok(())) + } + } } -pub use inner::{tracing_unbounded, TracingUnboundedSender, TracingUnboundedReceiver}; +pub use inner::*; From 6dd32f4664523dcd579d8abb483199da9bf9b4c6 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 19 Feb 2021 11:26:35 +0100 Subject: [PATCH 39/92] start_telemetry now return errors instead of logging --- Cargo.lock | 1 + client/service/src/builder.rs | 4 +-- client/service/src/client/client.rs | 5 ++-- client/service/src/error.rs | 3 +++ client/telemetry/Cargo.toml | 1 + client/telemetry/src/error.rs | 15 +++++++++++ client/telemetry/src/lib.rs | 40 ++++++++++++----------------- 7 files changed, 42 insertions(+), 27 deletions(-) create mode 100644 client/telemetry/src/error.rs diff --git a/Cargo.lock b/Cargo.lock index 1184caed682ce..3f564aa1853d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7669,6 +7669,7 @@ dependencies = [ "serde_json", "sp-utils", "take_mut", + "thiserror", "void", "wasm-timer", ] diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 2649b658dbfb0..8339452c9ecc3 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -601,7 +601,7 @@ pub fn spawn_tasks( &mut config, network.clone(), client.clone(), - ); + )?; info!("📦 Highest known block at #{}", chain_info.best_number); @@ -703,7 +703,7 @@ fn init_telemetry + ClientTelemetry>( config: &mut Configuration, network: Arc::Hash>>, client: Arc, -) { +) -> sc_telemetry::Result<()> { let genesis_hash = client.block_hash(Zero::zero()).ok().flatten().unwrap_or_default(); let connection_message = ConnectionMessage { name: config.network.node_name.to_owned(), diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index be05d44c02f0f..d1cc2f1eb9935 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -2028,9 +2028,10 @@ impl ClientTelemetry for Client self.telemetry_handle.clone() } - fn start_telemetry(&self, connection_message: ConnectionMessage) { + fn start_telemetry(&self, connection_message: ConnectionMessage) -> sc_telemetry::Result<()> { if let Some(telemetry) = self.telemetry.lock().as_mut() { - telemetry.start_telemetry(connection_message); + telemetry.start_telemetry(connection_message)?; } + Ok(()) } } diff --git a/client/service/src/error.rs b/client/service/src/error.rs index caa54700da916..9c653219ca130 100644 --- a/client/service/src/error.rs +++ b/client/service/src/error.rs @@ -46,6 +46,9 @@ pub enum Error { #[error(transparent)] Keystore(#[from] sc_keystore::Error), + #[error(transparent)] + Telemetry(#[from] sc_telemetry::Error), + #[error("Best chain selection strategy (SelectChain) is not provided.")] SelectChainRequired, diff --git a/client/telemetry/Cargo.toml b/client/telemetry/Cargo.toml index 84395a768d9f2..5e95387ba4ddf 100644 --- a/client/telemetry/Cargo.toml +++ b/client/telemetry/Cargo.toml @@ -29,3 +29,4 @@ serde_json = "1.0.41" sp-utils = { version = "3.0.0", path = "../../primitives/utils" } chrono = "0.4.19" global_counter = "0.2.1" +thiserror = "1.0.21" diff --git a/client/telemetry/src/error.rs b/client/telemetry/src/error.rs new file mode 100644 index 0000000000000..c729ca4aaf14e --- /dev/null +++ b/client/telemetry/src/error.rs @@ -0,0 +1,15 @@ +use thiserror::Error; + +#[allow(missing_docs)] +#[derive(Debug, Error)] +pub enum Error { + #[error("IO Error")] + IoError(#[from] std::io::Error), + #[error("This telemetry instance has already been initialized!")] + TelemetryAlreadyInitialized, + #[error("The telemetry worker has been dropped already.")] + TelemetryWorkerDropped, +} + +#[allow(missing_docs)] +pub type Result = std::result::Result; diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 3c9c14d29f362..9b549e1e319ca 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -42,7 +42,6 @@ use libp2p::Multiaddr; use log::{error, warn}; use serde::Serialize; use std::collections::HashMap; -use std::io; use std::mem; use std::sync::Arc; @@ -51,10 +50,12 @@ pub use log; pub use serde_json; mod endpoints; +mod error; mod node; mod transport; pub use endpoints::*; +pub use error::*; use node::*; use transport::*; @@ -120,7 +121,7 @@ pub struct TelemetryWorker { impl TelemetryWorker { /// Instantiate a new [`TelemetryWorker`] which can run in background. Only one is needed for /// the process. - pub fn new(buffer_size: usize, transport: Option) -> io::Result { + pub fn new(buffer_size: usize, transport: Option) -> Result { let transport = initialize_transport(transport)?; let (message_sender, message_receiver) = mpsc::channel(buffer_size); let (register_sender, register_receiver) = mpsc::unbounded(); @@ -370,7 +371,7 @@ impl Telemetry { /// /// The `connection_message` argument is a JSON object that is sent every time the connection /// (re-)establishes. - pub fn start_telemetry(&mut self, connection_message: ConnectionMessage) { + pub fn start_telemetry(&mut self, connection_message: ConnectionMessage) -> Result<()> { let Self { message_sender: _, register_sender, @@ -381,28 +382,21 @@ impl Telemetry { let endpoints = match endpoints.take() { Some(x) => x, - None => { - error!( - target: "telemetry", - "This telemetry instance has already been initialized!", - ); - return; - } + None => return Err(Error::TelemetryAlreadyInitialized), }; - match register_sender.unbounded_send(Register::Telemetry { - id: *id, - endpoints, - connection_message, - }) { - Ok(()) => {} - Err(err) => error!( - target: "telemetry", - "Could not initialize telemetry: \ - the telemetry is probably already running: {}", - err, - ), + if register_sender + .unbounded_send(Register::Telemetry { + id: *id, + endpoints, + connection_message, + }) + .is_err() + { + return Err(Error::TelemetryWorkerDropped); } + + Ok(()) } /// Make a new clonable handle to this [`Telemetry`]. This is used for reporting telemetries. @@ -565,5 +559,5 @@ pub trait ClientTelemetry { fn telemetry(&self) -> Option; /// Initialize the [`Telemetry`]. - fn start_telemetry(&self, connection_message: ConnectionMessage); + fn start_telemetry(&self, connection_message: ConnectionMessage) -> Result<()>; } From c7139fd057490418ba9e1b8dbe947df9491d6f4d Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 19 Feb 2021 11:51:03 +0100 Subject: [PATCH 40/92] Automatic split line --- client/basic-authorship/src/basic_authorship.rs | 4 +++- client/consensus/aura/src/lib.rs | 4 +++- client/consensus/babe/src/lib.rs | 3 ++- client/consensus/slots/src/lib.rs | 4 +++- client/finality-grandpa/src/authorities.rs | 4 +++- client/finality-grandpa/src/communication/gossip.rs | 4 +++- client/finality-grandpa/src/communication/mod.rs | 4 +++- client/finality-grandpa/src/lib.rs | 4 +++- client/service/src/builder.rs | 4 +++- client/service/src/client/client.rs | 4 +++- client/service/src/metrics.rs | 4 +++- 11 files changed, 32 insertions(+), 11 deletions(-) diff --git a/client/basic-authorship/src/basic_authorship.rs b/client/basic-authorship/src/basic_authorship.rs index 6c12de7b52f0f..f8853417933c9 100644 --- a/client/basic-authorship/src/basic_authorship.rs +++ b/client/basic-authorship/src/basic_authorship.rs @@ -351,7 +351,9 @@ impl Proposer .join(", ") ); telemetry!( - self.telemetry.clone(); CONSENSUS_INFO; "prepared_block_for_proposing"; + self.telemetry.clone(); + CONSENSUS_INFO; + "prepared_block_for_proposing"; "number" => ?block.header().number(), "hash" => ?::Hash::from(block.header().hash()), ); diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index fbfe3f0e81fda..434324ebaa10f 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -557,7 +557,9 @@ impl AuraVerifier where diff ); telemetry!( - self.client.telemetry(); CONSENSUS_INFO; "aura.halting_for_future_block"; + self.client.telemetry(); + CONSENSUS_INFO; + "aura.halting_for_future_block"; "diff" => ?diff, ); thread::sleep(Duration::from_secs(diff)); diff --git a/client/consensus/babe/src/lib.rs b/client/consensus/babe/src/lib.rs index 15d8b487007ea..f619ff1e21aa8 100644 --- a/client/consensus/babe/src/lib.rs +++ b/client/consensus/babe/src/lib.rs @@ -1072,7 +1072,8 @@ where trace!(target: "babe", "Checked {:?}; importing.", pre_header); telemetry!( - self.telemetry; CONSENSUS_TRACE; + self.telemetry; + CONSENSUS_TRACE; "babe.checked_and_importing"; "pre_header" => ?pre_header); diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index 8d041d457985a..2c2ecbd35a540 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -240,7 +240,9 @@ pub trait SimpleSlotWorker { warn!("Unable to fetch epoch data at block {:?}: {:?}", chain_head.hash(), err); telemetry!( - telemetry; CONSENSUS_WARN; "slots.unable_fetching_authorities"; + telemetry; + CONSENSUS_WARN; + "slots.unable_fetching_authorities"; "slot" => ?chain_head.hash(), "err" => ?err, ); diff --git a/client/finality-grandpa/src/authorities.rs b/client/finality-grandpa/src/authorities.rs index 56a2b1126eb80..e22e1b070b9c6 100644 --- a/client/finality-grandpa/src/authorities.rs +++ b/client/finality-grandpa/src/authorities.rs @@ -471,7 +471,9 @@ where ); telemetry!( - telemetry; CONSENSUS_INFO; "afg.applying_forced_authority_set_change"; + telemetry; + CONSENSUS_INFO; + "afg.applying_forced_authority_set_change"; "block" => ?change.canon_height ); diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index 7413315b58461..67d80e8d09859 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -829,7 +829,9 @@ impl Inner { if !self.authorities.contains(&full.message.id) { debug!(target: "afg", "Message from unknown voter: {}", full.message.id); telemetry!( - self.config.telemetry.clone(); CONSENSUS_DEBUG; "afg.bad_msg_signature"; + self.config.telemetry.clone(); + CONSENSUS_DEBUG; + "afg.bad_msg_signature"; "signature" => ?full.message.id, ); return Action::Discard(cost::UNKNOWN_VOTER); diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index a5b5a85a09666..d3faba48803e6 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -734,7 +734,9 @@ impl Sink> for OutgoingMessages ); telemetry!( - self.telemetry.clone(); CONSENSUS_DEBUG; "afg.announcing_blocks_to_voted_peers"; + self.telemetry.clone(); + CONSENSUS_DEBUG; + "afg.announcing_blocks_to_voted_peers"; "block" => ?target_hash, "round" => ?self.round, "set_id" => ?self.set_id, ); diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index 4465942f04959..289aee45fe35f 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -558,7 +558,9 @@ where move || { let authorities = genesis_authorities_provider.get()?; telemetry!( - telemetry; CONSENSUS_DEBUG; "afg.loading_authorities"; + telemetry; + CONSENSUS_DEBUG; + "afg.loading_authorities"; "authorities_len" => ?authorities.len() ); Ok(authorities) diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 8339452c9ecc3..066fd4153fe27 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -690,7 +690,9 @@ async fn transaction_notifications( network.propagate_transaction(hash); let status = transaction_pool.status(); telemetry!( - telemetry; SUBSTRATE_INFO; "txpool.import"; + telemetry; + SUBSTRATE_INFO; + "txpool.import"; "ready" => status.ready, "future" => status.future, ); diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index d1cc2f1eb9935..dec0aeb71c1ce 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -685,7 +685,9 @@ impl Client where rand::thread_rng().gen_bool(0.1) { telemetry!( - self.telemetry(); SUBSTRATE_INFO; "block.import"; + self.telemetry(); + SUBSTRATE_INFO; + "block.import"; "height" => height, "best" => ?hash, "origin" => ?origin diff --git a/client/service/src/metrics.rs b/client/service/src/metrics.rs index dcdd3975ef521..21232f05a3590 100644 --- a/client/service/src/metrics.rs +++ b/client/service/src/metrics.rs @@ -249,7 +249,9 @@ impl MetricsService { // Update/send metrics that are always available. telemetry!( - self.telemetry; SUBSTRATE_INFO; "system.interval"; + self.telemetry; + SUBSTRATE_INFO; + "system.interval"; "height" => best_number, "best" => ?best_hash, "txcount" => txpool_status.ready, From 993040b3f39b50745d29ae9d3815d0f0e292c1c8 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 19 Feb 2021 11:52:52 +0100 Subject: [PATCH 41/92] Forgot /g at the end again... --- client/consensus/aura/src/lib.rs | 8 +++++-- client/consensus/babe/src/lib.rs | 4 +++- client/consensus/slots/src/lib.rs | 24 ++++++++++++++----- client/finality-grandpa/src/authorities.rs | 4 +++- .../src/communication/gossip.rs | 8 +++++-- .../finality-grandpa/src/communication/mod.rs | 8 +++++-- client/finality-grandpa/src/lib.rs | 16 +++++++++---- client/service/src/client/client.rs | 4 +++- client/service/src/metrics.rs | 8 +++++-- 9 files changed, 63 insertions(+), 21 deletions(-) diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 434324ebaa10f..484e463f6ad16 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -651,7 +651,9 @@ impl Verifier for AuraVerifier where trace!(target: "aura", "Checked {:?}; importing.", pre_header); telemetry!( - self.client.telemetry(); CONSENSUS_TRACE; "aura.checked_and_importing"; + self.client.telemetry(); + CONSENSUS_TRACE; + "aura.checked_and_importing"; "pre_header" => ?pre_header, ); @@ -681,7 +683,9 @@ impl Verifier for AuraVerifier where CheckedHeader::Deferred(a, b) => { debug!(target: "aura", "Checking {:?} failed; {:?}, {:?}.", hash, a, b); telemetry!( - self.client.telemetry(); CONSENSUS_DEBUG; "aura.header_too_far_in_future"; + self.client.telemetry(); + CONSENSUS_DEBUG; + "aura.header_too_far_in_future"; "hash" => ?hash, "a" => ?a, "b" => ?b, diff --git a/client/consensus/babe/src/lib.rs b/client/consensus/babe/src/lib.rs index f619ff1e21aa8..55207b0535397 100644 --- a/client/consensus/babe/src/lib.rs +++ b/client/consensus/babe/src/lib.rs @@ -1092,7 +1092,9 @@ where CheckedHeader::Deferred(a, b) => { debug!(target: "babe", "Checking {:?} failed; {:?}, {:?}.", hash, a, b); telemetry!( - self.telemetry; CONSENSUS_DEBUG; "babe.header_too_far_in_future"; + self.telemetry; + CONSENSUS_DEBUG; + "babe.header_too_far_in_future"; "hash" => ?hash, "a" => ?a, "b" => ?b ); Err(Error::::TooFarInFuture(hash).into()) diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index 2c2ecbd35a540..75cf3ae65586e 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -261,7 +261,9 @@ pub trait SimpleSlotWorker { { debug!(target: self.logging_target(), "Skipping proposal slot. Waiting for the network."); telemetry!( - telemetry; CONSENSUS_DEBUG; "slots.skipping_proposal_slot"; + telemetry; + CONSENSUS_DEBUG; + "slots.skipping_proposal_slot"; "authorities_len" => authorities_len, ); @@ -285,7 +287,9 @@ pub trait SimpleSlotWorker { ); telemetry!( - telemetry; CONSENSUS_DEBUG; "slots.starting_authorship"; + telemetry; + CONSENSUS_DEBUG; + "slots.starting_authorship"; "slot_num" => *slot, "timestamp" => timestamp, ); @@ -296,7 +300,9 @@ pub trait SimpleSlotWorker { warn!("Unable to author block in slot {:?}: {:?}", slot, err); telemetry!( - telemetry; CONSENSUS_WARN; "slots.unable_authoring_block"; + telemetry; + CONSENSUS_WARN; + "slots.unable_authoring_block"; "slot" => *slot, "err" => ?err ); @@ -327,7 +333,9 @@ pub trait SimpleSlotWorker { #[cfg(build_type="debug")] info!("👉 Recompile your node in `--release` mode to mitigate this problem."); telemetry!( - telemetry; CONSENSUS_INFO; "slots.discarding_proposal_took_too_long"; + telemetry; + CONSENSUS_INFO; + "slots.discarding_proposal_took_too_long"; "slot" => *slot, ); @@ -364,7 +372,9 @@ pub trait SimpleSlotWorker { ); telemetry!( - telemetry; CONSENSUS_INFO; "slots.pre_sealed_block"; + telemetry; + CONSENSUS_INFO; + "slots.pre_sealed_block"; "header_num" => ?header_num, "hash_now" => ?block_import_params.post_hash(), "hash_previously" => ?header_hash, @@ -379,7 +389,9 @@ pub trait SimpleSlotWorker { ); telemetry!( - telemetry; CONSENSUS_WARN; "slots.err_with_block_built_on"; + telemetry; + CONSENSUS_WARN; + "slots.err_with_block_built_on"; "hash" => ?parent_hash, "err" => ?err, ); diff --git a/client/finality-grandpa/src/authorities.rs b/client/finality-grandpa/src/authorities.rs index e22e1b070b9c6..5699af36ea073 100644 --- a/client/finality-grandpa/src/authorities.rs +++ b/client/finality-grandpa/src/authorities.rs @@ -557,7 +557,9 @@ where change.canon_height, ); telemetry!( - telemetry; CONSENSUS_INFO; "afg.applying_scheduled_authority_set_change"; + telemetry; + CONSENSUS_INFO; + "afg.applying_scheduled_authority_set_change"; "block" => ?change.canon_height ); diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index 67d80e8d09859..289da43c688e7 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -846,7 +846,9 @@ impl Inner { ) { debug!(target: "afg", "Bad message signature {}", full.message.id); telemetry!( - self.config.telemetry.clone(); CONSENSUS_DEBUG; "afg.bad_msg_signature"; + self.config.telemetry.clone(); + CONSENSUS_DEBUG; + "afg.bad_msg_signature"; "signature" => ?full.message.id, ); return Action::Discard(cost::BAD_SIGNATURE); @@ -1423,7 +1425,9 @@ impl GossipValidator { message_name = None; debug!(target: "afg", "Error decoding message: {}", e); telemetry!( - self.telemetry.clone(); CONSENSUS_DEBUG; "afg.err_decoding_msg"; + self.telemetry.clone(); + CONSENSUS_DEBUG; + "afg.err_decoding_msg"; "" => "", ); diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index d3faba48803e6..85c6de485f975 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -818,7 +818,9 @@ fn check_compact_commit( ) { debug!(target: "afg", "Bad commit message signature {}", id); telemetry!( - telemetry; CONSENSUS_DEBUG; "afg.bad_commit_msg_signature"; + telemetry; + CONSENSUS_DEBUG; + "afg.bad_commit_msg_signature"; "id" => ?id, ); let cost = Misbehavior::BadCommitMessage { @@ -999,7 +1001,9 @@ impl Sink<(RoundNumber, Commit)> for CommitsOut { let round = Round(round); telemetry!( - self.telemetry.clone(); CONSENSUS_DEBUG; "afg.commit_issued"; + self.telemetry.clone(); + CONSENSUS_DEBUG; + "afg.commit_issued"; "target_number" => ?commit.target_number, "target_hash" => ?commit.target_hash, ); diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index 289aee45fe35f..4033d4095412e 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -769,7 +769,9 @@ where ); telemetry!( - telemetry; CONSENSUS_INFO; "afg.authority_set"; + telemetry; + CONSENSUS_INFO; + "afg.authority_set"; "authority_id" => authority_id.to_string(), "authority_set_id" => ?set_id, "authorities" => authorities, @@ -912,7 +914,9 @@ where .unwrap_or_default(); telemetry!( - self.telemetry.clone(); CONSENSUS_DEBUG; "afg.starting_new_voter"; + self.telemetry.clone(); + CONSENSUS_DEBUG; + "afg.starting_new_voter"; "name" => ?self.env.config.name(), "set_id" => ?self.env.set_id, "authority_id" => authority_id.to_string(), @@ -932,7 +936,9 @@ where ); telemetry!( - self.telemetry.clone(); CONSENSUS_INFO; "afg.authority_set"; + self.telemetry.clone(); + CONSENSUS_INFO; + "afg.authority_set"; "number" => ?chain_info.finalized_number, "hash" => ?chain_info.finalized_hash, "authority_id" => authority_id.to_string(), @@ -993,7 +999,9 @@ where format!("{}", a) }).collect(); telemetry!( - self.telemetry; CONSENSUS_INFO; "afg.voter_command_change_authorities"; + self.telemetry; + CONSENSUS_INFO; + "afg.voter_command_change_authorities"; "number" => ?new.canon_number, "hash" => ?new.canon_hash, "voters" => ?voters, diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index dec0aeb71c1ce..f53e808d6a354 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -1008,7 +1008,9 @@ impl Client where ); telemetry!( - self.telemetry(); SUBSTRATE_INFO; "notify.finalized"; + self.telemetry(); + SUBSTRATE_INFO; + "notify.finalized"; "height" => format!("{}", header.number()), "best" => ?last, ); diff --git a/client/service/src/metrics.rs b/client/service/src/metrics.rs index 21232f05a3590..43e5b8eaaded7 100644 --- a/client/service/src/metrics.rs +++ b/client/service/src/metrics.rs @@ -312,7 +312,9 @@ impl MetricsService { }; telemetry!( - self.telemetry; SUBSTRATE_INFO; "system.interval"; + self.telemetry; + SUBSTRATE_INFO; + "system.interval"; "peers" => num_peers, "bandwidth_download" => avg_bytes_per_sec_inbound, "bandwidth_upload" => avg_bytes_per_sec_outbound, @@ -332,7 +334,9 @@ impl MetricsService { // Send network state information, if any. if let Some(net_state) = net_state { telemetry!( - self.telemetry; SUBSTRATE_INFO; "system.network_state"; + self.telemetry; + SUBSTRATE_INFO; + "system.network_state"; "state" => net_state, ); } From 5581a8551a954f5bbc6935179da8bcdc6334500f Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 19 Feb 2021 11:58:26 +0100 Subject: [PATCH 42/92] Missed replacements --- .../src/communication/gossip.rs | 5 +++- .../finality-grandpa/src/communication/mod.rs | 27 +++++++++++++++---- client/finality-grandpa/src/environment.rs | 20 +++++++++++--- client/telemetry/src/lib.rs | 11 +++++--- 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index 289da43c688e7..c2ecc45690379 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -876,7 +876,10 @@ impl Inner { if full.message.precommits.len() != full.message.auth_data.len() || full.message.precommits.is_empty() { debug!(target: "afg", "Malformed compact commit"); - telemetry!(self.config.telemetry; CONSENSUS_DEBUG; "afg.malformed_compact_commit"; + telemetry!( + self.config.telemetry; + CONSENSUS_DEBUG; + "afg.malformed_compact_commit"; "precommits_len" => ?full.message.precommits.len(), "auth_data_len" => ?full.message.auth_data.len(), "precommits_is_empty" => ?full.message.precommits.is_empty(), diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index 85c6de485f975..39f798a187ca5 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -345,21 +345,30 @@ impl> NetworkBridge { if voters.len().get() <= TELEMETRY_VOTERS_LIMIT { match &msg.message.message { PrimaryPropose(propose) => { - telemetry!(telemetry; CONSENSUS_INFO; "afg.received_propose"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.received_propose"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?propose.target_number, "target_hash" => ?propose.target_hash, ); }, Prevote(prevote) => { - telemetry!(telemetry; CONSENSUS_INFO; "afg.received_prevote"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.received_prevote"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?prevote.target_number, "target_hash" => ?prevote.target_hash, ); }, Precommit(precommit) => { - telemetry!(telemetry; CONSENSUS_INFO; "afg.received_precommit"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.received_precommit"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?precommit.target_number, "target_hash" => ?precommit.target_hash, @@ -517,7 +526,10 @@ fn incoming_global( format!("{}", a) }).collect(); - telemetry!(telemetry; CONSENSUS_INFO; "afg.received_commit"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.received_commit"; "contains_precommits_signed_by" => ?precommits_signed_by, "target_number" => ?msg.message.target_number.clone(), "target_hash" => ?msg.message.target_hash.clone(), @@ -912,7 +924,12 @@ fn check_catch_up( buf, ) { debug!(target: "afg", "Bad catch up message signature {}", id); - telemetry!(telemetry; CONSENSUS_DEBUG; "afg.bad_catch_up_msg_signature"; "id" => ?id); + telemetry!( + telemetry; + CONSENSUS_DEBUG; + "afg.bad_catch_up_msg_signature"; + "id" => ?id, + ); let cost = Misbehavior::BadCatchUpMessage { signatures_checked: signatures_checked as i32, diff --git a/client/finality-grandpa/src/environment.rs b/client/finality-grandpa/src/environment.rs index ddd66742052c2..58af02cfdd5ca 100644 --- a/client/finality-grandpa/src/environment.rs +++ b/client/finality-grandpa/src/environment.rs @@ -893,7 +893,10 @@ where let mut telemetry = self.telemetry.clone(); let mut report_prevote_metrics = move |prevote: &Prevote| { - telemetry!(telemetry; CONSENSUS_DEBUG; "afg.prevote_issued"; + telemetry!( + telemetry; + CONSENSUS_DEBUG; + "afg.prevote_issued"; "round" => round, "target_number" => ?prevote.target_number, "target_hash" => ?prevote.target_hash, @@ -953,7 +956,10 @@ where let mut telemetry = self.telemetry.clone(); let mut report_precommit_metrics = move |precommit: &Precommit| { - telemetry!(telemetry; CONSENSUS_DEBUG; "afg.precommit_issued"; + telemetry!( + telemetry; + CONSENSUS_DEBUG; + "afg.precommit_issued"; "round" => round, "target_number" => ?precommit.target_number, "target_hash" => ?precommit.target_hash, @@ -1334,7 +1340,10 @@ where warn!(target: "afg", "Error applying finality to block {:?}: {:?}", (hash, number), e); e })?; - telemetry!(telemetry; CONSENSUS_INFO; "afg.finalized_blocks_up_to"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.finalized_blocks_up_to"; "number" => ?number, "hash" => ?hash, ); @@ -1354,7 +1363,10 @@ where ); } - telemetry!(telemetry; CONSENSUS_INFO; "afg.generating_new_authority_set"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.generating_new_authority_set"; "number" => ?canon_number, "hash" => ?canon_hash, "authorities" => ?set_ref.to_vec(), "set_id" => ?new_id, diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 9b549e1e319ca..6dc20c63aed6c 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -488,10 +488,13 @@ enum Register { /// # let authority_id = 42_u64; /// # let set_id = (43_u64, 44_u64); /// # let authorities = vec![45_u64]; -/// telemetry!(telemetry; CONSENSUS_INFO; "afg.authority_set"; -/// "authority_id" => authority_id.to_string(), -/// "authority_set_id" => ?set_id, -/// "authorities" => authorities, +/// telemetry!( +/// telemetry; +/// CONSENSUS_INFO; +/// "afg.authority_set"; +/// "authority_id" => authority_id.to_string(), +/// "authority_set_id" => ?set_id, +/// "authorities" => authorities, /// ); /// ``` #[macro_export(local_inner_macros)] From 54390e74626db09f8627a7f5a2c7d268ad789222 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 19 Feb 2021 12:25:21 +0100 Subject: [PATCH 43/92] Use telemetry in trait fn instead of fn argument --- client/consensus/babe/src/lib.rs | 8 ++++++-- client/consensus/slots/src/lib.rs | 12 ++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/client/consensus/babe/src/lib.rs b/client/consensus/babe/src/lib.rs index 55207b0535397..6bbd5e78da7d5 100644 --- a/client/consensus/babe/src/lib.rs +++ b/client/consensus/babe/src/lib.rs @@ -459,7 +459,6 @@ pub fn start_babe(BabeParams { inherent_data_providers, babe_link.time_source, can_author_with, - client.telemetry(), ); Ok(BabeWorker { inner: Box::pin(inner), @@ -524,7 +523,8 @@ where C: ProvideRuntimeApi + ProvideCache + HeaderBackend + - HeaderMetadata, + HeaderMetadata + + ClientTelemetry, C::Api: BabeApi, E: Environment, E::Proposer: Proposer>, @@ -702,6 +702,10 @@ where })) } + fn telemetry(&self) -> Option { + self.client.telemetry() + } + fn proposing_remaining_duration( &self, parent_head: &B::Header, diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index 75cf3ae65586e..d92d0988c41c1 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -82,7 +82,6 @@ pub trait SlotWorker { &mut self, chain_head: B::Header, slot_info: SlotInfo, - telemetry: Option, ) -> Self::OnSlot; } @@ -186,6 +185,9 @@ pub trait SimpleSlotWorker { /// Returns a `Proposer` to author on top of the given block. fn proposer(&mut self, block: &B::Header) -> Self::CreateProposer; + /// Retruns a `TelemetryHandle` if any. + fn telemetry(&self) -> Option; + /// Remaining duration of the slot. fn slot_remaining_duration(&self, slot_info: &SlotInfo) -> Duration { let now = Instant::now(); @@ -210,12 +212,12 @@ pub trait SimpleSlotWorker { &mut self, chain_head: B::Header, slot_info: SlotInfo, - mut telemetry: Option, ) -> Pin>> + Send>> where >::Proposal: Unpin + Send + 'static, { let (timestamp, slot) = (slot_info.timestamp, slot_info.slot); + let mut telemetry = self.telemetry(); let slot_remaining_duration = self.slot_remaining_duration(&slot_info); let proposing_remaining_duration = self.proposing_remaining_duration(&chain_head, &slot_info); @@ -411,9 +413,8 @@ impl> SlotWorker for T { &mut self, chain_head: B::Header, slot_info: SlotInfo, - telemetry: Option, ) -> Self::OnSlot { - SimpleSlotWorker::on_slot(self, chain_head, slot_info, telemetry) + SimpleSlotWorker::on_slot(self, chain_head, slot_info) } } @@ -438,7 +439,6 @@ pub fn start_slot_worker( inherent_data_providers: InherentDataProviders, timestamp_extractor: SC, can_author_with: CAW, - telemetry: Option, ) -> impl Future where B: BlockT, @@ -486,7 +486,7 @@ where Either::Right(future::ready(Ok(()))) } else { Either::Left( - worker.on_slot(chain_head, slot_info, telemetry.clone()) + worker.on_slot(chain_head, slot_info) .then(|_| future::ready(Ok(()))) ) } From 30d11b53a33e2ca662e3735b7aff9d9b5cd52b36 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 19 Feb 2021 13:21:26 +0100 Subject: [PATCH 44/92] Remove ClientTelemetry --- bin/node/cli/src/service.rs | 35 ++++++++--- .../basic-authorship/src/basic_authorship.rs | 10 ++-- client/consensus/babe/src/lib.rs | 22 ++++--- client/finality-grandpa/src/import.rs | 18 +++--- client/finality-grandpa/src/lib.rs | 15 +++-- client/finality-grandpa/src/observer.rs | 11 ++-- client/service/src/builder.rs | 60 ++++++++++--------- client/service/src/client/client.rs | 37 +++--------- client/service/src/client/light.rs | 4 +- client/telemetry/src/lib.rs | 10 ---- 10 files changed, 115 insertions(+), 107 deletions(-) diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index 7724b6c4e694f..cdf2580c012c1 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -34,7 +34,7 @@ use sp_runtime::traits::Block as BlockT; use futures::prelude::*; use sc_client_api::{ExecutorProvider, RemoteBackend}; use node_executor::Executor; -use sc_telemetry::{ClientTelemetry, TelemetryWorker}; +use sc_telemetry::{Telemetry, TelemetryWorker}; type FullClient = sc_service::TFullClient; type FullBackend = sc_service::TFullBackend; @@ -60,16 +60,23 @@ pub fn new_partial( sc_consensus_babe::BabeLink, ), grandpa::SharedVoterState, + Option, ) >, ServiceError> { let telemetry_worker = TelemetryWorker::new(16, None)?; + let telemetry = config.telemetry_endpoints.clone() + .filter(|x| !x.is_empty()) + .map(|endpoints| telemetry_worker.handle().new_telemetry(endpoints)); + let (client, backend, keystore_container, task_manager) = sc_service::new_full_parts::( &config, - Some(telemetry_worker.handle()), + telemetry.as_ref().map(|x| x.handle()), )?; let client = Arc::new(client); + task_manager.spawn_handle().spawn("telemetry", telemetry_worker.run()); + let select_chain = sc_consensus::LongestChain::new(backend.clone()); let transaction_pool = sc_transaction_pool::BasicPool::new_full( @@ -84,6 +91,7 @@ pub fn new_partial( client.clone(), &(client.clone() as Arc<_>), select_chain.clone(), + telemetry.as_ref().map(|x| x.handle()), )?; let justification_import = grandpa_block_import.clone(); @@ -105,6 +113,7 @@ pub fn new_partial( &task_manager.spawn_handle(), config.prometheus_registry(), sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()), + telemetry.as_ref().map(|x| x.handle()), )?; let import_setup = (block_import, grandpa_link, babe_link); @@ -167,7 +176,7 @@ pub fn new_partial( import_queue, transaction_pool, inherent_data_providers, - other: (rpc_extensions_builder, import_setup, rpc_setup), + other: (rpc_extensions_builder, import_setup, rpc_setup, telemetry), }) } @@ -197,7 +206,7 @@ pub fn new_full_base( select_chain, transaction_pool, inherent_data_providers, - other: (rpc_extensions_builder, import_setup, rpc_setup), + other: (rpc_extensions_builder, import_setup, rpc_setup, mut telemetry), } = new_partial(&config)?; let shared_voter_state = rpc_setup; @@ -248,6 +257,7 @@ pub fn new_full_base( remote_blockchain: None, network_status_sinks: network_status_sinks.clone(), system_rpc_tx, + telemetry: telemetry.as_mut(), }, )?; @@ -261,6 +271,7 @@ pub fn new_full_base( client.clone(), transaction_pool.clone(), prometheus_registry.as_ref(), + telemetry.as_ref().map(|x| x.handle()), ); let can_author_with = @@ -278,6 +289,7 @@ pub fn new_full_base( backoff_authoring_blocks, babe_link, can_author_with, + telemetry: telemetry.as_ref().map(|x| x.handle()), }; let babe = sc_consensus_babe::start_babe(babe_config)?; @@ -321,7 +333,7 @@ pub fn new_full_base( observer_enabled: false, keystore, is_authority: role.is_authority(), - telemetry: client.telemetry(), + telemetry: telemetry.as_ref().map(|x| x.handle()), }; if enable_grandpa { @@ -335,7 +347,7 @@ pub fn new_full_base( config, link: grandpa_link, network: network.clone(), - telemetry: client.telemetry(), + telemetry: telemetry.as_ref().map(|x| x.handle()), voting_rule: grandpa::VotingRulesBuilder::default().build(), prometheus_registry, shared_voter_state, @@ -379,12 +391,18 @@ pub fn new_light_base( Arc>> ), ServiceError> { let telemetry_worker = TelemetryWorker::new(16, None)?; + let mut telemetry = config.telemetry_endpoints.clone() + .filter(|x| !x.is_empty()) + .map(|endpoints| telemetry_worker.handle().new_telemetry(endpoints)); + let (client, backend, keystore_container, mut task_manager, on_demand) = sc_service::new_light_parts::( &config, - Some(telemetry_worker.handle()), + telemetry.as_ref().map(|x| x.handle()), )?; + task_manager.spawn_handle().spawn("telemetry", telemetry_worker.run()); + config.network.extra_sets.push(grandpa::grandpa_peers_set_config()); let select_chain = sc_consensus::LongestChain::new(backend.clone()); @@ -401,6 +419,7 @@ pub fn new_light_base( client.clone(), &(client.clone() as Arc<_>), select_chain.clone(), + telemetry.as_ref().map(|x| x.handle()), )?; let justification_import = grandpa_block_import.clone(); @@ -422,6 +441,7 @@ pub fn new_light_base( &task_manager.spawn_handle(), config.prometheus_registry(), sp_consensus::NeverCanAuthor, + telemetry.as_ref().map(|x| x.handle()), )?; let (network, network_status_sinks, system_rpc_tx, network_starter) = @@ -462,6 +482,7 @@ pub fn new_light_base( config, backend, network_status_sinks, system_rpc_tx, network: network.clone(), task_manager: &mut task_manager, + telemetry: telemetry.as_mut(), })?; Ok(( diff --git a/client/basic-authorship/src/basic_authorship.rs b/client/basic-authorship/src/basic_authorship.rs index f8853417933c9..c309ec656881b 100644 --- a/client/basic-authorship/src/basic_authorship.rs +++ b/client/basic-authorship/src/basic_authorship.rs @@ -32,7 +32,7 @@ use sp_runtime::{ traits::{Block as BlockT, Hash as HashT, Header as HeaderT, DigestFor, BlakeTwo256}, }; use sp_transaction_pool::{TransactionPool, InPoolTransaction}; -use sc_telemetry::{telemetry, ClientTelemetry, TelemetryHandle, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO}; use sc_block_builder::{BlockBuilderApi, BlockBuilderProvider}; use sp_api::{ProvideRuntimeApi, ApiExt}; use futures::{future, future::{Future, FutureExt}, channel::oneshot, select}; @@ -66,22 +66,20 @@ pub struct ProposerFactory { _phantom: PhantomData, } -impl ProposerFactory -where - C: ClientTelemetry, -{ +impl ProposerFactory { pub fn new( spawn_handle: impl SpawnNamed + 'static, client: Arc, transaction_pool: Arc, prometheus: Option<&PrometheusRegistry>, + telemetry: Option, ) -> Self { ProposerFactory { spawn_handle: Box::new(spawn_handle), transaction_pool, metrics: PrometheusMetrics::new(prometheus), max_block_size: DEFAULT_MAX_BLOCK_SIZE, - telemetry: client.telemetry(), + telemetry, client, _phantom: PhantomData, } diff --git a/client/consensus/babe/src/lib.rs b/client/consensus/babe/src/lib.rs index 6bbd5e78da7d5..0dd86fc0372bc 100644 --- a/client/consensus/babe/src/lib.rs +++ b/client/consensus/babe/src/lib.rs @@ -89,7 +89,7 @@ use sp_runtime::{ use sp_api::{ProvideRuntimeApi, NumberFor}; use parking_lot::Mutex; use sp_inherents::{InherentDataProviders, InherentData}; -use sc_telemetry::{telemetry, ClientTelemetry, TelemetryHandle, CONSENSUS_TRACE, CONSENSUS_DEBUG}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_TRACE, CONSENSUS_DEBUG}; use sp_consensus::{ BlockImport, Environment, Proposer, BlockCheckParams, ForkChoiceStrategy, BlockImportParams, BlockOrigin, Error as ConsensusError, @@ -358,7 +358,7 @@ impl std::ops::Deref for Config { } /// Parameters for BABE. -pub struct BabeParams { +pub struct BabeParams { /// The keystore that manages the keys of the node. pub keystore: SyncCryptoStorePtr, @@ -393,6 +393,9 @@ pub struct BabeParams { /// Checks if the current native implementation can author with a runtime at a given block. pub can_author_with: CAW, + + /// Handle use to report telemetries. + pub telemetry: Option, } /// Start the babe worker. @@ -408,13 +411,14 @@ pub fn start_babe(BabeParams { backoff_authoring_blocks, babe_link, can_author_with, + telemetry, }: BabeParams) -> Result< BabeWorker, sp_consensus::Error, > where B: BlockT, C: ProvideRuntimeApi + ProvideCache + ProvideUncles + BlockchainEvents - + HeaderBackend + HeaderMetadata + ClientTelemetry + + HeaderBackend + HeaderMetadata + Send + Sync + 'static, C::Api: BabeApi, SC: SelectChain + 'static, @@ -441,6 +445,7 @@ pub fn start_babe(BabeParams { epoch_changes: babe_link.epoch_changes.clone(), slot_notification_sinks: slot_notification_sinks.clone(), config: config.clone(), + telemetry, }; register_babe_inherent_data_provider(&inherent_data_providers, config.slot_duration())?; @@ -514,6 +519,7 @@ struct BabeSlotWorker { epoch_changes: SharedEpochChanges, slot_notification_sinks: SlotNotificationSinks, config: Config, + telemetry: Option, } impl sc_consensus_slots::SimpleSlotWorker @@ -523,8 +529,7 @@ where C: ProvideRuntimeApi + ProvideCache + HeaderBackend + - HeaderMetadata + - ClientTelemetry, + HeaderMetadata, C::Api: BabeApi, E: Environment, E::Proposer: Proposer>, @@ -703,7 +708,7 @@ where } fn telemetry(&self) -> Option { - self.client.telemetry() + self.telemetry.clone() } fn proposing_remaining_duration( @@ -1505,11 +1510,12 @@ pub fn import_queue( spawner: &impl sp_core::traits::SpawnNamed, registry: Option<&Registry>, can_author_with: CAW, + telemetry: Option, ) -> ClientResult> where Inner: BlockImport> + Send + Sync + 'static, Client: ProvideRuntimeApi + ProvideCache + HeaderBackend - + HeaderMetadata + AuxStore + ClientTelemetry + + HeaderMetadata + AuxStore + Send + Sync + 'static, Client::Api: BlockBuilderApi + BabeApi + ApiExt, SelectChain: sp_consensus::SelectChain + 'static, @@ -1524,7 +1530,7 @@ pub fn import_queue( epoch_changes: babe_link.epoch_changes, time_source: babe_link.time_source, can_author_with, - telemetry: client.telemetry(), + telemetry, client, }; diff --git a/client/finality-grandpa/src/import.rs b/client/finality-grandpa/src/import.rs index 4a3d281fbf74f..73a9a30bc7ba6 100644 --- a/client/finality-grandpa/src/import.rs +++ b/client/finality-grandpa/src/import.rs @@ -24,7 +24,7 @@ use parking_lot::RwLockWriteGuard; use sp_blockchain::{BlockStatus, well_known_cache_keys}; use sc_client_api::{backend::Backend, utils::is_descendent_of}; -use sc_telemetry::ClientTelemetry; +use sc_telemetry::TelemetryHandle; use sp_utils::mpsc::TracingUnboundedSender; use sp_api::TransactionFor; @@ -63,6 +63,7 @@ pub struct GrandpaBlockImport { send_voter_commands: TracingUnboundedSender>>, authority_set_hard_forks: HashMap>>, justification_sender: GrandpaJustificationSender, + telemetry: Option, _phantom: PhantomData, } @@ -77,6 +78,7 @@ impl Clone for send_voter_commands: self.send_voter_commands.clone(), authority_set_hard_forks: self.authority_set_hard_forks.clone(), justification_sender: self.justification_sender.clone(), + telemetry: self.telemetry.clone(), _phantom: PhantomData, } } @@ -87,7 +89,7 @@ impl JustificationImport NumberFor: finality_grandpa::BlockNumberOps, DigestFor: Encode, BE: Backend, - Client: crate::ClientForGrandpa + ClientTelemetry, + Client: crate::ClientForGrandpa, SC: SelectChain, { type Error = ConsensusError; @@ -219,7 +221,7 @@ where NumberFor: finality_grandpa::BlockNumberOps, DigestFor: Encode, BE: Backend, - Client: crate::ClientForGrandpa + ClientTelemetry, + Client: crate::ClientForGrandpa, { // check for a new authority set change. fn check_new_change( @@ -340,7 +342,7 @@ where number, &is_descendent_of, initial_sync, - self.inner.telemetry(), + self.telemetry.clone(), ) .map_err(|e| ConsensusError::ClientImport(e.to_string())) .map_err(ConsensusError::from)?; @@ -416,7 +418,7 @@ impl BlockImport NumberFor: finality_grandpa::BlockNumberOps, DigestFor: Encode, BE: Backend, - Client: crate::ClientForGrandpa + ClientTelemetry, + Client: crate::ClientForGrandpa, for<'a> &'a Client: BlockImport>, { @@ -563,6 +565,7 @@ impl GrandpaBlockImport>>, authority_set_hard_forks: Vec<(SetId, PendingChange>)>, justification_sender: GrandpaJustificationSender, + telemetry: Option, ) -> GrandpaBlockImport { // check for and apply any forced authority set hard fork that applies // to the *current* authority set. @@ -606,6 +609,7 @@ impl GrandpaBlockImport GrandpaBlockImport GrandpaBlockImport where BE: Backend, - Client: crate::ClientForGrandpa + ClientTelemetry, + Client: crate::ClientForGrandpa, NumberFor: finality_grandpa::BlockNumberOps, { /// Import a block justification and finalize the block. @@ -650,7 +654,7 @@ where justification.into(), initial_sync, Some(&self.justification_sender), - self.inner.telemetry(), + self.telemetry.clone(), ); match result { diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index 4033d4095412e..6d122896bd7f7 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -79,7 +79,7 @@ use sp_core::{ use sp_keystore::{SyncCryptoStorePtr, SyncCryptoStore}; use sp_application_crypto::AppKey; use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver}; -use sc_telemetry::{telemetry, ClientTelemetry, TelemetryHandle, CONSENSUS_INFO, CONSENSUS_DEBUG}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO, CONSENSUS_DEBUG}; use parking_lot::RwLock; use finality_grandpa::Error as GrandpaError; @@ -454,6 +454,7 @@ pub struct LinkHalf { voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: GrandpaJustificationSender, justification_stream: GrandpaJustificationStream, + telemetry: Option, } impl LinkHalf { @@ -504,6 +505,7 @@ pub fn block_import( client: Arc, genesis_authorities_provider: &dyn GenesisAuthoritySetProvider, select_chain: SC, + telemetry: Option, ) -> Result< ( GrandpaBlockImport, @@ -514,13 +516,14 @@ pub fn block_import( where SC: SelectChain, BE: Backend + 'static, - Client: ClientForGrandpa + ClientTelemetry + 'static, + Client: ClientForGrandpa + 'static, { block_import_with_authority_set_hard_forks( client, genesis_authorities_provider, select_chain, Default::default(), + telemetry, ) } @@ -534,6 +537,7 @@ pub fn block_import_with_authority_set_hard_forks genesis_authorities_provider: &dyn GenesisAuthoritySetProvider, select_chain: SC, authority_set_hard_forks: Vec<(SetId, (Block::Hash, NumberFor), AuthorityList)>, + telemetry: Option, ) -> Result< ( GrandpaBlockImport, @@ -544,7 +548,7 @@ pub fn block_import_with_authority_set_hard_forks where SC: SelectChain, BE: Backend + 'static, - Client: ClientForGrandpa + ClientTelemetry + 'static, + Client: ClientForGrandpa + 'static, { let chain_info = client.info(); let genesis_hash = chain_info.genesis_hash; @@ -554,7 +558,7 @@ where genesis_hash, >::zero(), { - let mut telemetry = client.telemetry(); + let mut telemetry = telemetry.clone(); move || { let authorities = genesis_authorities_provider.get()?; telemetry!( @@ -599,6 +603,7 @@ where voter_commands_tx, authority_set_hard_forks, justification_sender.clone(), + telemetry.clone(), ), LinkHalf { client, @@ -607,6 +612,7 @@ where voter_commands_rx, justification_sender, justification_stream, + telemetry, }, )) } @@ -734,6 +740,7 @@ where voter_commands_rx, justification_sender, justification_stream: _, + telemetry: _, } = link; let network = NetworkBridge::new( diff --git a/client/finality-grandpa/src/observer.rs b/client/finality-grandpa/src/observer.rs index 783896691616e..c0eab15e4f455 100644 --- a/client/finality-grandpa/src/observer.rs +++ b/client/finality-grandpa/src/observer.rs @@ -29,7 +29,7 @@ use log::{debug, info, warn}; use sp_keystore::SyncCryptoStorePtr; use sp_consensus::SelectChain; use sc_client_api::backend::Backend; -use sc_telemetry::{ClientTelemetry, TelemetryHandle}; +use sc_telemetry::TelemetryHandle; use sp_utils::mpsc::TracingUnboundedReceiver; use sp_runtime::traits::{NumberFor, Block as BlockT}; use sp_blockchain::HeaderMetadata; @@ -167,7 +167,7 @@ where N: NetworkT + Send + Clone + 'static, SC: SelectChain + 'static, NumberFor: BlockNumberOps, - Client: crate::ClientForGrandpa + ClientTelemetry + 'static, + Client: crate::ClientForGrandpa + 'static, { let LinkHalf { client, @@ -175,7 +175,8 @@ where persistent_data, voter_commands_rx, justification_sender, - .. + justification_stream: _, + telemetry, } = link; let network = NetworkBridge::new( @@ -183,7 +184,7 @@ where config.clone(), persistent_data.set_state.clone(), None, - client.telemetry(), + telemetry.clone(), ); let observer_work = ObserverWork::new( @@ -193,7 +194,7 @@ where config.keystore, voter_commands_rx, Some(justification_sender), - client.telemetry(), + telemetry.clone(), ); let observer_work = observer_work diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 066fd4153fe27..4a91d2fa07428 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -54,11 +54,9 @@ use std::sync::Arc; use wasm_timer::SystemTime; use sc_telemetry::{ telemetry, - ClientTelemetry, ConnectionMessage, Telemetry, TelemetryHandle, - TelemetryWorkerHandle, SUBSTRATE_INFO, }; use sp_transaction_pool::MaintainedTransactionPool; @@ -293,18 +291,18 @@ impl KeystoreContainer { /// Creates a new full client for the given config. pub fn new_full_client( config: &Configuration, - telemetry_worker_handle: Option, + telemetry: Option, ) -> Result, Error> where TBl: BlockT, TExecDisp: NativeExecutionDispatch + 'static, { - new_full_parts(config, telemetry_worker_handle).map(|parts| parts.0) + new_full_parts(config, telemetry).map(|parts| parts.0) } /// Create the initial parts of a full node. pub fn new_full_parts( config: &Configuration, - telemetry_worker_handle: Option, + telemetry: Option, ) -> Result, Error> where TBl: BlockT, TExecDisp: NativeExecutionDispatch + 'static, @@ -347,11 +345,6 @@ pub fn new_full_parts( Some(keystore_container.sync_keystore()), ); - let telemetry = match (telemetry_worker_handle, config.telemetry_endpoints.clone()) { - (Some(mut handle), Some(endpoints)) => Some(handle.new_telemetry(endpoints)), - _ => None, - }; - new_client( db_config, executor, @@ -381,7 +374,7 @@ pub fn new_full_parts( /// Create the initial parts of a light node. pub fn new_light_parts( config: &Configuration, - telemetry_worker_handle: Option, + telemetry: Option, ) -> Result, Error> where TBl: BlockT, TExecDisp: NativeExecutionDispatch + 'static, @@ -420,10 +413,6 @@ pub fn new_light_parts( ); let on_demand = Arc::new(sc_network::config::OnDemand::new(fetch_checker)); let backend = sc_light::new_light_backend(light_blockchain); - let telemetry = match (telemetry_worker_handle, config.telemetry_endpoints.clone()) { - (Some(mut handle), Some(endpoints)) => Some(handle.new_telemetry(endpoints)), - _ => None, - }; let client = Arc::new(light::new_light( backend.clone(), config.chain_spec.as_storage_builder(), @@ -446,7 +435,7 @@ pub fn new_client( execution_extensions: ExecutionExtensions, spawn_handle: Box, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, config: ClientConfig, ) -> Result<( crate::client::Client< @@ -510,6 +499,8 @@ pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend> { pub network_status_sinks: NetworkStatusSinks, /// A Sender for RPC requests. pub system_rpc_tx: TracingUnboundedSender>, + /// Telemetry instance for this node. + pub telemetry: Option<&'a mut Telemetry>, } /// Build a shared offchain workers instance. @@ -561,7 +552,7 @@ pub fn spawn_tasks( TCl: ProvideRuntimeApi + HeaderMetadata + Chain + BlockBackend + BlockIdTo + ProofProvider + HeaderBackend + BlockchainEvents + ExecutorProvider + UsageProvider + - StorageProvider + CallApiAt + ClientTelemetry + Send + 'static, + StorageProvider + CallApiAt + Send + 'static, >::Api: sp_api::Metadata + sc_offchain::OffchainWorkerApi + @@ -587,6 +578,7 @@ pub fn spawn_tasks( network, network_status_sinks, system_rpc_tx, + telemetry, } = params; let chain_info = client.usage_info().chain; @@ -597,11 +589,16 @@ pub fn spawn_tasks( config.dev_key_seed.clone().map(|s| vec![s]).unwrap_or_default(), ).map_err(|e| Error::Application(Box::new(e)))?; - init_telemetry( - &mut config, - network.clone(), - client.clone(), - )?; + let telemetry = telemetry + .map(|telemetry| { + init_telemetry( + &mut config, + network.clone(), + client.clone(), + telemetry, + ) + }) + .transpose()?; info!("📦 Highest known block at #{}", chain_info.best_number); @@ -615,7 +612,11 @@ pub fn spawn_tasks( spawn_handle.spawn( "on-transaction-imported", - transaction_notifications(transaction_pool.clone(), network.clone(), client.telemetry()), + transaction_notifications( + transaction_pool.clone(), + network.clone(), + telemetry.clone(), + ), ); // Prometheus metrics. @@ -623,7 +624,7 @@ pub fn spawn_tasks( config.prometheus_config.clone() { // Set static metrics. - let metrics = MetricsService::with_prometheus(client.telemetry(), ®istry, &config)?; + let metrics = MetricsService::with_prometheus(telemetry.clone(), ®istry, &config)?; spawn_handle.spawn( "prometheus-endpoint", prometheus_endpoint::init_prometheus(port, registry).map(drop) @@ -631,7 +632,7 @@ pub fn spawn_tasks( metrics } else { - MetricsService::new(client.telemetry()) + MetricsService::new(telemetry.clone()) }; // Periodically updated metrics and telemetry updates. @@ -701,11 +702,12 @@ async fn transaction_notifications( .await; } -fn init_telemetry + ClientTelemetry>( +fn init_telemetry>( config: &mut Configuration, network: Arc::Hash>>, client: Arc, -) -> sc_telemetry::Result<()> { + telemetry: &mut Telemetry, +) -> sc_telemetry::Result { let genesis_hash = client.block_hash(Zero::zero()).ok().flatten().unwrap_or_default(); let connection_message = ConnectionMessage { name: config.network.node_name.to_owned(), @@ -721,7 +723,9 @@ fn init_telemetry + ClientTelemetry>( network_id: network.local_peer_id().to_base58(), }; - client.start_telemetry(connection_message) + telemetry.start_telemetry(connection_message)?; + + Ok(telemetry.handle()) } fn gen_handler( diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index f53e808d6a354..e1bf9673bae2a 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -37,9 +37,6 @@ use sp_core::{ use sp_keystore::SyncCryptoStorePtr; use sc_telemetry::{ telemetry, - ClientTelemetry, - ConnectionMessage, - Telemetry, TelemetryHandle, SUBSTRATE_INFO, }; @@ -122,8 +119,7 @@ pub struct Client where Block: BlockT { block_rules: BlockRules, execution_extensions: ExecutionExtensions, config: ClientConfig, - telemetry: Mutex>, - telemetry_handle: Option, + telemetry: Option, _phantom: PhantomData, } @@ -161,7 +157,7 @@ pub fn new_in_mem( genesis_storage: &S, keystore: Option, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, spawn_handle: Box, config: ClientConfig, ) -> sp_blockchain::Result( keystore: Option, spawn_handle: Box, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, config: ClientConfig, ) -> sp_blockchain::Result, Block, RA>> where @@ -307,7 +303,7 @@ impl Client where bad_blocks: BadBlocks, execution_extensions: ExecutionExtensions, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, config: ClientConfig, ) -> sp_blockchain::Result { if backend.blockchain().header(BlockId::Number(Zero::zero()))?.is_none() { @@ -340,8 +336,7 @@ impl Client where block_rules: BlockRules::new(fork_blocks, bad_blocks), execution_extensions, config, - telemetry_handle: telemetry.as_ref().map(|x| x.handle()), - telemetry: Mutex::new(telemetry), + telemetry, _phantom: Default::default(), }) } @@ -685,7 +680,7 @@ impl Client where rand::thread_rng().gen_bool(0.1) { telemetry!( - self.telemetry(); + self.telemetry.clone(); SUBSTRATE_INFO; "block.import"; "height" => height, @@ -1008,7 +1003,7 @@ impl Client where ); telemetry!( - self.telemetry(); + self.telemetry.clone(); SUBSTRATE_INFO; "notify.finalized"; "height" => format!("{}", header.number()), @@ -2021,21 +2016,3 @@ impl sp_consensus::block_validation::Chain for Client) } } - -impl ClientTelemetry for Client - where - BE: backend::Backend, - E: CallExecutor, - B: BlockT, -{ - fn telemetry(&self) -> Option { - self.telemetry_handle.clone() - } - - fn start_telemetry(&self, connection_message: ConnectionMessage) -> sc_telemetry::Result<()> { - if let Some(telemetry) = self.telemetry.lock().as_mut() { - telemetry.start_telemetry(connection_message)?; - } - Ok(()) - } -} diff --git a/client/service/src/client/light.rs b/client/service/src/client/light.rs index 108ec46fb0433..3b29a0e1a92ca 100644 --- a/client/service/src/client/light.rs +++ b/client/service/src/client/light.rs @@ -22,7 +22,7 @@ use std::sync::Arc; use sc_executor::RuntimeInfo; use sp_core::traits::{CodeExecutor, SpawnNamed}; -use sc_telemetry::Telemetry; +use sc_telemetry::TelemetryHandle; use sp_runtime::BuildStorage; use sp_runtime::traits::{Block as BlockT, HashFor}; use sp_blockchain::Result as ClientResult; @@ -39,7 +39,7 @@ pub fn new_light( code_executor: E, spawn_handle: Box, prometheus_registry: Option, - telemetry: Option, + telemetry: Option, ) -> ClientResult< Client< Backend>, diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 6dc20c63aed6c..fbbfac52ec1da 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -554,13 +554,3 @@ macro_rules! format_fields_to_json { )* }}; } - -/// A trait used by the Substrate's Client to get a [`TelemetryHandle`] or to initialize the -/// [`Telemetry`]. -pub trait ClientTelemetry { - /// Get a clonable handle to the telemetry if it exists. - fn telemetry(&self) -> Option; - - /// Initialize the [`Telemetry`]. - fn start_telemetry(&self, connection_message: ConnectionMessage) -> Result<()>; -} From 147d5d4c034e8a7eec91fb1e7cee502473686403 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 19 Feb 2021 13:40:05 +0100 Subject: [PATCH 45/92] Adapt code --- bin/node-template/node/src/service.rs | 47 ++++++++++++++++++++++----- client/consensus/aura/src/lib.rs | 25 ++++++++------ 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/bin/node-template/node/src/service.rs b/bin/node-template/node/src/service.rs index feed6cdd04e3b..5401180e84468 100644 --- a/bin/node-template/node/src/service.rs +++ b/bin/node-template/node/src/service.rs @@ -11,7 +11,7 @@ pub use sc_executor::NativeExecutor; use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair}; use sc_finality_grandpa::SharedVoterState; use sc_keystore::LocalKeystore; -use sc_telemetry::ClientTelemetry; +use sc_telemetry::{Telemetry, TelemetryWorker}; // Our native executor instance. native_executor_instance!( @@ -37,6 +37,7 @@ pub fn new_partial(config: &Configuration) -> Result, sc_finality_grandpa::LinkHalf, + Option, ) >, ServiceError> { if config.keystore_remote.is_some() { @@ -45,10 +46,20 @@ pub fn new_partial(config: &Configuration) -> Result(&config)?; + sc_service::new_full_parts::( + &config, + telemetry.as_ref().map(|x| x.handle()), + )?; let client = Arc::new(client); + task_manager.spawn_handle().spawn("telemetry", telemetry_worker.run()); + let select_chain = sc_consensus::LongestChain::new(backend.clone()); let transaction_pool = sc_transaction_pool::BasicPool::new_full( @@ -60,7 +71,10 @@ pub fn new_partial(config: &Configuration) -> Result), select_chain.clone(), + client.clone(), + &(client.clone() as Arc<_>), + select_chain.clone(), + telemetry.as_ref().map(|x| x.handle()), )?; let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new( @@ -76,6 +90,7 @@ pub fn new_partial(config: &Configuration) -> Result Result Result select_chain, transaction_pool, inherent_data_providers, - other: (block_import, grandpa_link), + other: (block_import, grandpa_link, mut telemetry), } = new_partial(&config)?; if let Some(url) = &config.keystore_remote { @@ -177,6 +192,7 @@ pub fn new_full(mut config: Configuration) -> Result network_status_sinks, system_rpc_tx, config, + telemetry: telemetry.as_mut(), }, )?; @@ -186,6 +202,7 @@ pub fn new_full(mut config: Configuration) -> Result client.clone(), transaction_pool, prometheus_registry.as_ref(), + telemetry.as_ref().map(|x| x.handle()), ); let can_author_with = @@ -203,6 +220,7 @@ pub fn new_full(mut config: Configuration) -> Result backoff_authoring_blocks, keystore_container.sync_keystore(), can_author_with, + telemetry.as_ref().map(|x| x.handle()), )?; // the AURA authoring task is considered essential, i.e. if it @@ -226,7 +244,7 @@ pub fn new_full(mut config: Configuration) -> Result observer_enabled: false, keystore, is_authority: role.is_authority(), - telemetry: client.telemetry(), + telemetry: telemetry.as_ref().map(|x| x.handle()), }; if enable_grandpa { @@ -243,7 +261,7 @@ pub fn new_full(mut config: Configuration) -> Result voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(), prometheus_registry, shared_voter_state: SharedVoterState::empty(), - telemetry: client.telemetry(), + telemetry: telemetry.as_ref().map(|x| x.handle()), }; // the GRANDPA voter task is considered infallible, i.e. @@ -260,8 +278,18 @@ pub fn new_full(mut config: Configuration) -> Result /// Builds a new service for a light client. pub fn new_light(mut config: Configuration) -> Result { + let telemetry_worker = TelemetryWorker::new(16, None)?; + let mut telemetry = config.telemetry_endpoints.clone() + .filter(|x| !x.is_empty()) + .map(|endpoints| telemetry_worker.handle().new_telemetry(endpoints)); + let (client, backend, keystore_container, mut task_manager, on_demand) = - sc_service::new_light_parts::(&config)?; + sc_service::new_light_parts::( + &config, + telemetry.as_ref().map(|x| x.handle()), + )?; + + task_manager.spawn_handle().spawn("telemetry", telemetry_worker.run()); config.network.extra_sets.push(sc_finality_grandpa::grandpa_peers_set_config()); @@ -279,6 +307,7 @@ pub fn new_light(mut config: Configuration) -> Result client.clone(), &(client.clone() as Arc<_>), select_chain.clone(), + telemetry.as_ref().map(|x| x.handle()), )?; let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new( @@ -295,6 +324,7 @@ pub fn new_light(mut config: Configuration) -> Result &task_manager.spawn_handle(), config.prometheus_registry(), sp_consensus::NeverCanAuthor, + telemetry.as_ref().map(|x| x.handle()), )?; let (network, network_status_sinks, system_rpc_tx, network_starter) = @@ -327,6 +357,7 @@ pub fn new_light(mut config: Configuration) -> Result network, network_status_sinks, system_rpc_tx, + telemetry: telemetry.as_mut(), })?; network_starter.start_network(); diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 484e463f6ad16..42a5d94e06d22 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -66,7 +66,7 @@ use sp_inherents::{InherentDataProviders, InherentData}; use sp_timestamp::{ TimestampInherentData, InherentType as TimestampInherent, InherentError as TIError }; -use sc_telemetry::{telemetry, ClientTelemetry, CONSENSUS_TRACE, CONSENSUS_DEBUG, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_TRACE, CONSENSUS_DEBUG, CONSENSUS_INFO}; use sc_consensus_slots::{ CheckedHeader, SlotInfo, SlotCompatible, StorageChanges, check_equivocation, @@ -149,10 +149,10 @@ pub fn start_aura( backoff_authoring_blocks: Option, keystore: SyncCryptoStorePtr, can_author_with: CAW, + telemetry: Option, ) -> Result, sp_consensus::Error> where B: BlockT, - C: ProvideRuntimeApi + BlockOf + ProvideCache + AuxStore + HeaderBackend + Send - + ClientTelemetry + Sync, + C: ProvideRuntimeApi + BlockOf + ProvideCache + AuxStore + HeaderBackend + Send + Sync, C::Api: AuraApi>, SC: SelectChain, E: Environment + Send + Sync + 'static, @@ -174,6 +174,7 @@ pub fn start_aura( sync_oracle: sync_oracle.clone(), force_authoring, backoff_authoring_blocks, + telemetry, _key_type: PhantomData::

, }; register_aura_inherent_data_provider( @@ -188,7 +189,6 @@ pub fn start_aura( inherent_data_providers, AuraSlotCompatible, can_author_with, - client.telemetry(), )) } @@ -200,6 +200,7 @@ struct AuraWorker { sync_oracle: SO, force_authoring: bool, backoff_authoring_blocks: Option, + telemetry: Option, _key_type: PhantomData

, } @@ -348,6 +349,10 @@ where })) } + fn telemetry(&self) -> Option { + self.telemetry.clone() + } + fn proposing_remaining_duration( &self, head: &B::Header, @@ -504,10 +509,10 @@ pub struct AuraVerifier { phantom: PhantomData

, inherent_data_providers: sp_inherents::InherentDataProviders, can_author_with: CAW, + telemetry: Option, } impl AuraVerifier where - C: ClientTelemetry, P: Send + Sync + 'static, CAW: Send + Sync + 'static, { @@ -557,7 +562,7 @@ impl AuraVerifier where diff ); telemetry!( - self.client.telemetry(); + self.telemetry.clone(); CONSENSUS_INFO; "aura.halting_for_future_block"; "diff" => ?diff, @@ -582,7 +587,6 @@ impl Verifier for AuraVerifier where + sc_client_api::backend::AuxStore + ProvideCache + BlockOf - + ClientTelemetry + Send + Sync, C::Api: BlockBuilderApi + AuraApi> + ApiExt, @@ -651,7 +655,7 @@ impl Verifier for AuraVerifier where trace!(target: "aura", "Checked {:?}; importing.", pre_header); telemetry!( - self.client.telemetry(); + self.telemetry; CONSENSUS_TRACE; "aura.checked_and_importing"; "pre_header" => ?pre_header, @@ -683,7 +687,7 @@ impl Verifier for AuraVerifier where CheckedHeader::Deferred(a, b) => { debug!(target: "aura", "Checking {:?} failed; {:?}, {:?}.", hash, a, b); telemetry!( - self.client.telemetry(); + self.telemetry; CONSENSUS_DEBUG; "aura.header_too_far_in_future"; "hash" => ?hash, @@ -857,6 +861,7 @@ pub fn import_queue( spawner: &S, registry: Option<&Registry>, can_author_with: CAW, + telemetry: Option, ) -> Result, sp_consensus::Error> where B: BlockT, C::Api: BlockBuilderApi + AuraApi> + ApiExt, @@ -865,7 +870,6 @@ pub fn import_queue( + ProvideCache + AuxStore + HeaderBackend - + ClientTelemetry + Send + Sync + 'static, @@ -885,6 +889,7 @@ pub fn import_queue( inherent_data_providers, phantom: PhantomData, can_author_with, + telemetry, }; Ok(BasicQueue::new( From f7082119cdfec3a37187a47ceaab15980c79d4bb Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 24 Feb 2021 13:25:26 +0100 Subject: [PATCH 46/92] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/consensus/slots/src/lib.rs | 2 +- client/telemetry/src/error.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index 60413e2475e36..3972ef86133ff 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -182,7 +182,7 @@ pub trait SimpleSlotWorker { /// Returns a `Proposer` to author on top of the given block. fn proposer(&mut self, block: &B::Header) -> Self::CreateProposer; - /// Retruns a `TelemetryHandle` if any. + /// Returns a [`TelemetryHandle`] if any. fn telemetry(&self) -> Option; /// Remaining duration of the slot. diff --git a/client/telemetry/src/error.rs b/client/telemetry/src/error.rs index c729ca4aaf14e..910fd2eadb069 100644 --- a/client/telemetry/src/error.rs +++ b/client/telemetry/src/error.rs @@ -1,7 +1,6 @@ -use thiserror::Error; #[allow(missing_docs)] -#[derive(Debug, Error)] +#[derive(Debug, thiserror::Error)] pub enum Error { #[error("IO Error")] IoError(#[from] std::io::Error), From 1401067b573af7909769bbe7cfaa26ea7ca797e3 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 24 Feb 2021 13:27:01 +0100 Subject: [PATCH 47/92] Added missing license --- client/telemetry/src/error.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/client/telemetry/src/error.rs b/client/telemetry/src/error.rs index 910fd2eadb069..90a8018f4e1d3 100644 --- a/client/telemetry/src/error.rs +++ b/client/telemetry/src/error.rs @@ -1,3 +1,20 @@ +// This file is part of Substrate. + +// Copyright (C) 2021 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program 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. + +// This program 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 this program. If not, see . #[allow(missing_docs)] #[derive(Debug, thiserror::Error)] From afd6056938631cc9bc48bd1be6da31ffbe470e9b Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 24 Feb 2021 13:27:44 +0100 Subject: [PATCH 48/92] Fix indentation with spaces --- client/consensus/slots/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index 3972ef86133ff..c82d2dd915c8a 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -214,7 +214,7 @@ pub trait SimpleSlotWorker { >::Proposal: Unpin + Send + 'static, { let (timestamp, slot) = (slot_info.timestamp, slot_info.slot); - let mut telemetry = self.telemetry(); + let mut telemetry = self.telemetry(); let slot_remaining_duration = self.slot_remaining_duration(&slot_info); let proposing_remaining_duration = self.proposing_remaining_duration(&chain_head, &slot_info); From 526d877ce70d1582974eebca23f98b6e4e3c231d Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 24 Feb 2021 13:30:28 +0100 Subject: [PATCH 49/92] Improve doc --- client/telemetry/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index fbbfac52ec1da..e646b3e16c82a 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -489,7 +489,7 @@ enum Register { /// # let set_id = (43_u64, 44_u64); /// # let authorities = vec![45_u64]; /// telemetry!( -/// telemetry; +/// telemetry; // an `Option` /// CONSENSUS_INFO; /// "afg.authority_set"; /// "authority_id" => authority_id.to_string(), From fc1ce16525e8dfd22dd2d458ca371ce0530cfb4e Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 24 Feb 2021 17:03:52 +0100 Subject: [PATCH 50/92] Use Mutex and make Telemetry work with immutable --- client/basic-authorship/src/basic_authorship.rs | 2 +- client/consensus/aura/src/lib.rs | 2 +- client/consensus/slots/src/lib.rs | 6 +++--- client/finality-grandpa/src/authorities.rs | 4 ++-- client/finality-grandpa/src/communication/gossip.rs | 6 +++--- client/finality-grandpa/src/communication/mod.rs | 10 +++++----- client/finality-grandpa/src/environment.rs | 8 ++++---- client/finality-grandpa/src/lib.rs | 8 ++++---- client/service/src/builder.rs | 2 +- client/service/src/client/client.rs | 4 ++-- client/telemetry/src/lib.rs | 11 ++++++----- 11 files changed, 32 insertions(+), 31 deletions(-) diff --git a/client/basic-authorship/src/basic_authorship.rs b/client/basic-authorship/src/basic_authorship.rs index c309ec656881b..abe44c948ec5e 100644 --- a/client/basic-authorship/src/basic_authorship.rs +++ b/client/basic-authorship/src/basic_authorship.rs @@ -349,7 +349,7 @@ impl Proposer .join(", ") ); telemetry!( - self.telemetry.clone(); + self.telemetry; CONSENSUS_INFO; "prepared_block_for_proposing"; "number" => ?block.header().number(), diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 42a5d94e06d22..28ae7872fe21f 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -562,7 +562,7 @@ impl AuraVerifier where diff ); telemetry!( - self.telemetry.clone(); + self.telemetry; CONSENSUS_INFO; "aura.halting_for_future_block"; "diff" => ?diff, diff --git a/client/consensus/slots/src/lib.rs b/client/consensus/slots/src/lib.rs index c82d2dd915c8a..0cd7e88eca433 100644 --- a/client/consensus/slots/src/lib.rs +++ b/client/consensus/slots/src/lib.rs @@ -214,7 +214,7 @@ pub trait SimpleSlotWorker { >::Proposal: Unpin + Send + 'static, { let (timestamp, slot) = (slot_info.timestamp, slot_info.slot); - let mut telemetry = self.telemetry(); + let telemetry = self.telemetry(); let slot_remaining_duration = self.slot_remaining_duration(&slot_info); let proposing_remaining_duration = self.proposing_remaining_duration(&chain_head, &slot_info); @@ -294,7 +294,7 @@ pub trait SimpleSlotWorker { ); let awaiting_proposer = { - let mut telemetry = telemetry.clone(); + let telemetry = telemetry.clone(); self.proposer(&chain_head).map_err(move |err| { warn!("Unable to author block in slot {:?}: {:?}", slot, err); @@ -323,7 +323,7 @@ pub trait SimpleSlotWorker { ).map_err(|e| sp_consensus::Error::ClientImport(format!("{:?}", e)))); let proposal_work = { - let mut telemetry = telemetry.clone(); + let telemetry = telemetry.clone(); futures::future::select(proposing, proposing_remaining).map(move |v| match v { Either::Left((b, _)) => b.map(|b| (b, claim)), Either::Right(_) => { diff --git a/client/finality-grandpa/src/authorities.rs b/client/finality-grandpa/src/authorities.rs index 5699af36ea073..0bb168803f89c 100644 --- a/client/finality-grandpa/src/authorities.rs +++ b/client/finality-grandpa/src/authorities.rs @@ -418,7 +418,7 @@ where best_number: N, is_descendent_of: &F, initial_sync: bool, - mut telemetry: Option, + telemetry: Option, ) -> Result, Error> where F: Fn(&H, &H) -> Result, @@ -516,7 +516,7 @@ where finalized_number: N, is_descendent_of: &F, initial_sync: bool, - mut telemetry: Option<&mut TelemetryHandle>, + telemetry: Option<&mut TelemetryHandle>, ) -> Result, Error> where F: Fn(&H, &H) -> Result, diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index c2ecc45690379..a6c51f7eeee72 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -829,7 +829,7 @@ impl Inner { if !self.authorities.contains(&full.message.id) { debug!(target: "afg", "Message from unknown voter: {}", full.message.id); telemetry!( - self.config.telemetry.clone(); + self.config.telemetry; CONSENSUS_DEBUG; "afg.bad_msg_signature"; "signature" => ?full.message.id, @@ -846,7 +846,7 @@ impl Inner { ) { debug!(target: "afg", "Bad message signature {}", full.message.id); telemetry!( - self.config.telemetry.clone(); + self.config.telemetry; CONSENSUS_DEBUG; "afg.bad_msg_signature"; "signature" => ?full.message.id, @@ -1428,7 +1428,7 @@ impl GossipValidator { message_name = None; debug!(target: "afg", "Error decoding message: {}", e); telemetry!( - self.telemetry.clone(); + self.telemetry; CONSENSUS_DEBUG; "afg.err_decoding_msg"; "" => "", diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index 39f798a187ca5..5953dbe3b6a9c 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -325,7 +325,7 @@ impl> NetworkBridge { }); let topic = round_topic::(round.0, set_id.0); - let mut telemetry = self.telemetry.clone(); + let telemetry = self.telemetry.clone(); let incoming = self.gossip_engine.lock().messages_for(topic) .filter_map(move |notification| { let decoded = GossipMessage::::decode(&mut ¬ification.message[..]); @@ -746,7 +746,7 @@ impl Sink> for OutgoingMessages ); telemetry!( - self.telemetry.clone(); + self.telemetry; CONSENSUS_DEBUG; "afg.announcing_blocks_to_voted_peers"; "block" => ?target_hash, "round" => ?self.round, "set_id" => ?self.set_id, @@ -787,7 +787,7 @@ fn check_compact_commit( voters: &VoterSet, round: Round, set_id: SetId, - mut telemetry: Option<&mut TelemetryHandle>, + telemetry: Option<&mut TelemetryHandle>, ) -> Result<(), ReputationChange> { // 4f + 1 = equivocations from f voters. let f = voters.total_weight() - voters.threshold(); @@ -905,7 +905,7 @@ fn check_catch_up( set_id: SetIdNumber, mut signatures_checked: usize, buf: &mut Vec, - mut telemetry: Option, + telemetry: Option, ) -> Result where B: BlockT, I: Iterator, &'a AuthorityId, &'a AuthoritySignature)>, @@ -1018,7 +1018,7 @@ impl Sink<(RoundNumber, Commit)> for CommitsOut { let round = Round(round); telemetry!( - self.telemetry.clone(); + self.telemetry; CONSENSUS_DEBUG; "afg.commit_issued"; "target_number" => ?commit.target_number, diff --git a/client/finality-grandpa/src/environment.rs b/client/finality-grandpa/src/environment.rs index 58af02cfdd5ca..bdba8a970145a 100644 --- a/client/finality-grandpa/src/environment.rs +++ b/client/finality-grandpa/src/environment.rs @@ -891,8 +891,8 @@ where None => return Ok(()), }; - let mut telemetry = self.telemetry.clone(); - let mut report_prevote_metrics = move |prevote: &Prevote| { + let telemetry = self.telemetry.clone(); + let report_prevote_metrics = move |prevote: &Prevote| { telemetry!( telemetry; CONSENSUS_DEBUG; @@ -954,8 +954,8 @@ where None => return Ok(()), }; - let mut telemetry = self.telemetry.clone(); - let mut report_precommit_metrics = move |precommit: &Precommit| { + let telemetry = self.telemetry.clone(); + let report_precommit_metrics = move |precommit: &Precommit| { telemetry!( telemetry; CONSENSUS_DEBUG; diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index 6d122896bd7f7..ab42a98ff1c84 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -558,7 +558,7 @@ where genesis_hash, >::zero(), { - let mut telemetry = telemetry.clone(); + let telemetry = telemetry.clone(); move || { let authorities = genesis_authorities_provider.get()?; telemetry!( @@ -757,7 +757,7 @@ where .map(|x| x.on_connect_stream()) { let authorities = persistent_data.authority_set.clone(); - let mut telemetry = telemetry.clone(); + let telemetry = telemetry.clone(); let events = telemetry_on_connect .for_each(move |_| { let current_authorities = authorities.current_authorities(); @@ -921,7 +921,7 @@ where .unwrap_or_default(); telemetry!( - self.telemetry.clone(); + self.telemetry; CONSENSUS_DEBUG; "afg.starting_new_voter"; "name" => ?self.env.config.name(), @@ -943,7 +943,7 @@ where ); telemetry!( - self.telemetry.clone(); + self.telemetry; CONSENSUS_INFO; "afg.authority_set"; "number" => ?chain_info.finalized_number, diff --git a/client/service/src/builder.rs b/client/service/src/builder.rs index 61d4f84578fc2..c2af54e388f85 100644 --- a/client/service/src/builder.rs +++ b/client/service/src/builder.rs @@ -678,7 +678,7 @@ pub fn spawn_tasks( async fn transaction_notifications( transaction_pool: Arc, network: Arc::Hash>>, - mut telemetry: Option, + telemetry: Option, ) where TBl: BlockT, diff --git a/client/service/src/client/client.rs b/client/service/src/client/client.rs index e1bf9673bae2a..aa4e7175d14a0 100644 --- a/client/service/src/client/client.rs +++ b/client/service/src/client/client.rs @@ -680,7 +680,7 @@ impl Client where rand::thread_rng().gen_bool(0.1) { telemetry!( - self.telemetry.clone(); + self.telemetry; SUBSTRATE_INFO; "block.import"; "height" => height, @@ -1003,7 +1003,7 @@ impl Client where ); telemetry!( - self.telemetry.clone(); + self.telemetry; SUBSTRATE_INFO; "notify.finalized"; "height" => format!("{}", header.number()), diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index e646b3e16c82a..9cc41843403ce 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -41,6 +41,7 @@ use global_counter::primitive::exact::CounterU64; use libp2p::Multiaddr; use log::{error, warn}; use serde::Serialize; +use parking_lot::Mutex; use std::collections::HashMap; use std::mem; use std::sync::Arc; @@ -402,7 +403,7 @@ impl Telemetry { /// Make a new clonable handle to this [`Telemetry`]. This is used for reporting telemetries. pub fn handle(&self) -> TelemetryHandle { TelemetryHandle { - message_sender: self.message_sender.clone(), + message_sender: Arc::new(Mutex::new(self.message_sender.clone())), id: self.id, connection_notifier: self.connection_notifier.clone(), } @@ -412,15 +413,15 @@ impl Telemetry { /// Handle to a [`Telemetry`]. Used to report telemetries. #[derive(Debug, Clone)] pub struct TelemetryHandle { - message_sender: mpsc::Sender, + message_sender: Arc>>, id: Id, connection_notifier: TelemetryConnectionNotifier, } impl TelemetryHandle { /// Send telemetries. - pub fn send_telemetry(&mut self, verbosity: VerbosityLevel, payload: TelemetryPayload) { - match self.message_sender.try_send((self.id, verbosity, payload)) { + pub fn send_telemetry(&self, verbosity: VerbosityLevel, payload: TelemetryPayload) { + match self.message_sender.lock().try_send((self.id, verbosity, payload)) { Ok(()) => {} Err(err) if err.is_full() => todo!("overflow"), Err(_) => unreachable!(), @@ -500,7 +501,7 @@ enum Register { #[macro_export(local_inner_macros)] macro_rules! telemetry { ( $telemetry:expr; $verbosity:expr; $msg:expr; $( $t:tt )* ) => {{ - if let Some(telemetry) = $telemetry.as_mut() { + if let Some(telemetry) = $telemetry.as_ref() { let verbosity: $crate::VerbosityLevel = $verbosity; match format_fields_to_json!($($t)*) { Err(err) => { From c1fac062d3cfcdab323d2abc9fa3922b0c6d9e4c Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 24 Feb 2021 17:14:52 +0100 Subject: [PATCH 51/92] Optimization? --- client/telemetry/src/lib.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 9cc41843403ce..c6588d133eb7d 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -42,6 +42,7 @@ use libp2p::Multiaddr; use log::{error, warn}; use serde::Serialize; use parking_lot::Mutex; +use std::cell::RefCell; use std::collections::HashMap; use std::mem; use std::sync::Arc; @@ -421,11 +422,23 @@ pub struct TelemetryHandle { impl TelemetryHandle { /// Send telemetries. pub fn send_telemetry(&self, verbosity: VerbosityLevel, payload: TelemetryPayload) { - match self.message_sender.lock().try_send((self.id, verbosity, payload)) { - Ok(()) => {} - Err(err) if err.is_full() => todo!("overflow"), - Err(_) => unreachable!(), + thread_local! { + static SENDER: RefCell>> = RefCell::new(None); } + + SENDER.with(|sender| { + let mut sender = sender.borrow_mut(); + + if sender.is_none() { + sender.replace(self.message_sender.lock().clone()); + } + + match sender.as_mut().unwrap().try_send((self.id, verbosity, payload)) { + Ok(()) => {} + Err(err) if err.is_full() => todo!("overflow"), + Err(_) => unreachable!(), + } + }) } /// Get event stream for telemetry connection established events. From 8aebf81ad0ebaea7e5fe5c6211965efdbf4f9c01 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 25 Feb 2021 12:27:35 +0100 Subject: [PATCH 52/92] Replace code using mem with Vec::retain --- client/telemetry/src/lib.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index c6588d133eb7d..192bbe36938d7 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -44,7 +44,6 @@ use serde::Serialize; use parking_lot::Mutex; use std::cell::RefCell; use std::collections::HashMap; -use std::mem; use std::sync::Arc; pub use libp2p::wasm_ext::ExtTransport; @@ -239,13 +238,16 @@ impl TelemetryWorker { node.connection_messages.extend(connection_message.clone()); - let (matching, rest): (Vec<_>, Vec<_>) = - mem::take(pending_connection_notifications) - .into_iter() - .partition(|(addr_b, _)| *addr_b == addr); - node.telemetry_connection_notifier - .extend(matching.into_iter().map(|(_, x)| x.clone())); - let _ = mem::replace(pending_connection_notifications, rest); + pending_connection_notifications.retain( + |(addr_b, connection_message)| { + if *addr_b == addr { + node.telemetry_connection_notifier.push(connection_message.clone()); + false + } else { + true + } + } + ); } } Register::Notifier { From e3066644df9d209a9ef875ab32d5816cf3d01f46 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 25 Feb 2021 15:01:30 +0100 Subject: [PATCH 53/92] Revert "Optimization?" This reverts commit c1fac062d3cfcdab323d2abc9fa3922b0c6d9e4c. --- client/telemetry/src/lib.rs | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 192bbe36938d7..37ebf986ee2d6 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -42,7 +42,6 @@ use libp2p::Multiaddr; use log::{error, warn}; use serde::Serialize; use parking_lot::Mutex; -use std::cell::RefCell; use std::collections::HashMap; use std::sync::Arc; @@ -424,23 +423,11 @@ pub struct TelemetryHandle { impl TelemetryHandle { /// Send telemetries. pub fn send_telemetry(&self, verbosity: VerbosityLevel, payload: TelemetryPayload) { - thread_local! { - static SENDER: RefCell>> = RefCell::new(None); + match self.message_sender.lock().try_send((self.id, verbosity, payload)) { + Ok(()) => {} + Err(err) if err.is_full() => todo!("overflow"), + Err(_) => unreachable!(), } - - SENDER.with(|sender| { - let mut sender = sender.borrow_mut(); - - if sender.is_none() { - sender.replace(self.message_sender.lock().clone()); - } - - match sender.as_mut().unwrap().try_send((self.id, verbosity, payload)) { - Ok(()) => {} - Err(err) if err.is_full() => todo!("overflow"), - Err(_) => unreachable!(), - } - }) } /// Get event stream for telemetry connection established events. From a95c2c49f06de6dad6ec2bf564b504dec91f58cf Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 25 Feb 2021 15:03:53 +0100 Subject: [PATCH 54/92] Fixing stuff --- client/basic-authorship/src/basic_authorship.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/basic-authorship/src/basic_authorship.rs b/client/basic-authorship/src/basic_authorship.rs index 59cc32c9c2957..9f6f970d5a425 100644 --- a/client/basic-authorship/src/basic_authorship.rs +++ b/client/basic-authorship/src/basic_authorship.rs @@ -98,14 +98,16 @@ impl ProposerFactory { client: Arc, transaction_pool: Arc, prometheus: Option<&PrometheusRegistry>, + telemetry: Option, ) -> Self { ProposerFactory { spawn_handle: Box::new(spawn_handle), client, transaction_pool, metrics: PrometheusMetrics::new(prometheus), - _phantom: PhantomData, max_block_size: DEFAULT_MAX_BLOCK_SIZE, + telemetry, + _phantom: PhantomData, } } } From ab5179eaf79d472a365f0edfdc91bba166d8759f Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 25 Feb 2021 15:32:26 +0100 Subject: [PATCH 55/92] Fixing other stuff --- client/service/test/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/client/service/test/src/lib.rs b/client/service/test/src/lib.rs index e92efa0b119c2..a80c53a8c21c5 100644 --- a/client/service/test/src/lib.rs +++ b/client/service/test/src/lib.rs @@ -267,7 +267,6 @@ fn node_config Date: Thu, 25 Feb 2021 16:12:06 +0100 Subject: [PATCH 56/92] Fix more stuff --- bin/node/cli/src/service.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index a8d8c387afe9f..e77e2e78b4efc 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -611,6 +611,7 @@ mod tests { service.client(), service.transaction_pool(), None, + None, ); let epoch_descriptor = babe_link.epoch_changes().lock().epoch_descriptor_for_child_of( From 0587055ff5d28529d64144ddc8e1247abe18dd69 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 25 Feb 2021 18:38:19 +0100 Subject: [PATCH 57/92] Fix browser telemetry --- Cargo.lock | 4 +++- bin/node/cli/Cargo.toml | 2 ++ bin/node/cli/src/browser.rs | 12 +++--------- bin/node/cli/src/service.rs | 9 ++++++++- utils/browser/Cargo.toml | 1 - utils/browser/src/lib.rs | 12 ++---------- 6 files changed, 18 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 60fd3c8063268..288b82a371111 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "Inflector" version = "0.11.4" @@ -3920,6 +3922,7 @@ dependencies = [ "frame-system", "futures 0.3.12", "hex-literal", + "libp2p-wasm-ext", "log", "nix", "node-executor", @@ -9139,7 +9142,6 @@ dependencies = [ "sc-informant", "sc-network", "sc-service", - "sc-telemetry", "sc-tracing", "sp-database", "wasm-bindgen", diff --git a/bin/node/cli/Cargo.toml b/bin/node/cli/Cargo.toml index 4aa73e2f70607..c984c47a3a8eb 100644 --- a/bin/node/cli/Cargo.toml +++ b/bin/node/cli/Cargo.toml @@ -105,6 +105,7 @@ try-runtime-cli = { version = "0.9.0", optional = true, path = "../../../utils/f wasm-bindgen = { version = "0.2.57", optional = true } wasm-bindgen-futures = { version = "0.4.18", optional = true } browser-utils = { package = "substrate-browser-utils", path = "../../../utils/browser", optional = true, version = "0.9.0"} +libp2p-wasm-ext = { version = "0.27", features = ["websocket"], optional = true } [target.'cfg(target_arch="x86_64")'.dependencies] node-executor = { version = "2.0.0", path = "../executor", features = [ "wasmtime" ] } @@ -148,6 +149,7 @@ browser = [ "browser-utils", "wasm-bindgen", "wasm-bindgen-futures", + "libp2p-wasm-ext", ] cli = [ "node-executor/wasmi-errno", diff --git a/bin/node/cli/src/browser.rs b/bin/node/cli/src/browser.rs index a187b181d699a..49ac309d42abc 100644 --- a/bin/node/cli/src/browser.rs +++ b/bin/node/cli/src/browser.rs @@ -21,7 +21,7 @@ use log::info; use wasm_bindgen::prelude::*; use browser_utils::{ Client, - browser_configuration, init_logging_and_telemetry, set_console_error_panic_hook, + browser_configuration, init_logging, set_console_error_panic_hook, }; /// Starts the client. @@ -37,18 +37,14 @@ async fn start_inner( log_directives: String, ) -> Result> { set_console_error_panic_hook(); - let telemetry_worker = init_logging_and_telemetry(&log_directives)?; + init_logging(&log_directives)?; let chain_spec = match chain_spec { Some(chain_spec) => ChainSpec::from_json_bytes(chain_spec.as_bytes().to_vec()) .map_err(|e| format!("{:?}", e))?, None => crate::chain_spec::development_config(), }; - let telemetry_worker_handle = telemetry_worker.handle(); - let config = browser_configuration( - chain_spec, - Some(telemetry_worker_handle), - ).await?; + let config = browser_configuration(chain_spec).await?; info!("Substrate browser node"); info!("✌️ version {}", config.impl_version); @@ -63,7 +59,5 @@ async fn start_inner( .map(|(components, rpc_handlers, _, _, _)| (components, rpc_handlers)) .map_err(|e| format!("{:?}", e))?; - task_manager.spawn_handle().spawn("telemetry", telemetry_worker.run()); - Ok(browser_utils::start_client(task_manager, rpc_handlers)) } diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index e77e2e78b4efc..7f336647e9342 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -395,7 +395,14 @@ pub fn new_light_base( Arc::Hash>>, Arc>> ), ServiceError> { - let telemetry_worker = TelemetryWorker::new(16, None)?; + #[cfg(browser)] + let transport = Some( + sc_telemetry::ExtTransport::new(libp2p_wasm_ext::ffi::websocket_transport()) + ); + #[cfg(not(browser))] + let transport = None; + + let telemetry_worker = TelemetryWorker::new(16, transport)?; let mut telemetry = config.telemetry_endpoints.clone() .filter(|x| !x.is_empty()) .map(|endpoints| telemetry_worker.handle().new_telemetry(endpoints)); diff --git a/utils/browser/Cargo.toml b/utils/browser/Cargo.toml index d7051620fcfd9..764d2d18a61b7 100644 --- a/utils/browser/Cargo.toml +++ b/utils/browser/Cargo.toml @@ -27,7 +27,6 @@ sc-informant = { version = "0.9.0", path = "../../client/informant" } sc-service = { version = "0.9.0", path = "../../client/service", default-features = false } sc-network = { path = "../../client/network", version = "0.9.0"} sc-chain-spec = { path = "../../client/chain-spec", version = "3.0.0"} -sc-telemetry = { path = "../../client/telemetry", version = "3.0.0"} sc-tracing = { path = "../../client/tracing", version = "3.0.0"} # Imported just for the `wasm-bindgen` feature diff --git a/utils/browser/src/lib.rs b/utils/browser/src/lib.rs index bed4c83024dfe..f7e5537c43b5c 100644 --- a/utils/browser/src/lib.rs +++ b/utils/browser/src/lib.rs @@ -24,7 +24,6 @@ use sc_service::{ GenericChainSpec, RuntimeGenesis, KeepBlocks, TransactionStorageMode, }; -use sc_telemetry::TelemetryWorkerHandle; use sc_tracing::logging::LoggerBuilder; use wasm_bindgen::prelude::*; use futures::{ @@ -37,13 +36,8 @@ use libp2p_wasm_ext::{ExtTransport, ffi}; pub use console_error_panic_hook::set_once as set_console_error_panic_hook; /// Initialize the logger and return a `TelemetryWorker` and a wasm `ExtTransport`. -pub fn init_logging_and_telemetry( - pattern: &str, -) -> Result { - let transport = ExtTransport::new(ffi::websocket_transport()); - let mut logger = LoggerBuilder::new(pattern); - logger.with_transport(transport); - logger.init() +pub fn init_logging(pattern: &str) -> Result<(), sc_tracing::logging::Error> { + LoggerBuilder::new(pattern).init() } /// Create a service configuration from a chain spec. @@ -51,7 +45,6 @@ pub fn init_logging_and_telemetry( /// This configuration contains good defaults for a browser light client. pub async fn browser_configuration( chain_spec: GenericChainSpec, - telemetry_worker_handle: Option, ) -> Result> where G: RuntimeGenesis + 'static, @@ -82,7 +75,6 @@ where async {} }).into(), telemetry_external_transport: Some(transport), - telemetry_worker_handle, role: Role::Light, database: { info!("Opening Indexed DB database '{}'...", name); From ee8eae659974b2cde7b19bf634d1aed9452c4876 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 26 Feb 2021 12:46:21 +0100 Subject: [PATCH 58/92] Temporary changes --- bin/node/cli/browser-demo/build.sh | 2 +- bin/node/cli/src/browser.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bin/node/cli/browser-demo/build.sh b/bin/node/cli/browser-demo/build.sh index 8840106daeb5c..544b16333aeb2 100755 --- a/bin/node/cli/browser-demo/build.sh +++ b/bin/node/cli/browser-demo/build.sh @@ -2,4 +2,4 @@ set -e -x cargo +nightly build --release -p node-cli --target wasm32-unknown-unknown --no-default-features --features browser -Z features=itarget wasm-bindgen ../../../../target/wasm32-unknown-unknown/release/node_cli.wasm --out-dir pkg --target web -python -m http.server 8000 +python -m http.server 8080 diff --git a/bin/node/cli/src/browser.rs b/bin/node/cli/src/browser.rs index 49ac309d42abc..cfc7e50b2b4ee 100644 --- a/bin/node/cli/src/browser.rs +++ b/bin/node/cli/src/browser.rs @@ -44,7 +44,8 @@ async fn start_inner( None => crate::chain_spec::development_config(), }; - let config = browser_configuration(chain_spec).await?; + let mut config = browser_configuration(chain_spec).await?; + config.telemetry_endpoints = Some(sc_telemetry::TelemetryEndpoints::new(vec![("ws://127.0.0.1:8000/submit".to_string(), 10)]).unwrap()); info!("Substrate browser node"); info!("✌️ version {}", config.impl_version); From 0f6bc8f1ab0a8e5867f5615ecc2cb24c7b924872 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 26 Feb 2021 13:46:06 +0100 Subject: [PATCH 59/92] WIP --- bin/node/cli/src/service.rs | 4 ++-- client/telemetry/src/lib.rs | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index 7f336647e9342..96c089945aded 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -395,11 +395,11 @@ pub fn new_light_base( Arc::Hash>>, Arc>> ), ServiceError> { - #[cfg(browser)] + #[cfg(feature = "browser")] let transport = Some( sc_telemetry::ExtTransport::new(libp2p_wasm_ext::ffi::websocket_transport()) ); - #[cfg(not(browser))] + #[cfg(not(feature = "browser"))] let transport = None; let telemetry_worker = TelemetryWorker::new(16, transport)?; diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 37ebf986ee2d6..f479519e29862 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -425,7 +425,10 @@ impl TelemetryHandle { pub fn send_telemetry(&self, verbosity: VerbosityLevel, payload: TelemetryPayload) { match self.message_sender.lock().try_send((self.id, verbosity, payload)) { Ok(()) => {} - Err(err) if err.is_full() => todo!("overflow"), + Err(err) if err.is_full() => log::trace!( + target: "telemetry", + "buffer overflow", + ), Err(_) => unreachable!(), } } From 3e2c06696b8fe07fcb3b0853bfdc5c300ec2bdd7 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 26 Feb 2021 13:46:09 +0100 Subject: [PATCH 60/92] debug --- client/telemetry/src/node.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/client/telemetry/src/node.rs b/client/telemetry/src/node.rs index 0ecc9d6007e3b..bc1c20032b7f7 100644 --- a/client/telemetry/src/node.rs +++ b/client/telemetry/src/node.rs @@ -245,6 +245,11 @@ where match &mut self.socket { NodeSocket::Connected(conn) => match serde_json::to_vec(&item) { Ok(data) => { + log::error!( + target: "telemetry", + "sent: {}", + serde_json::to_string(&item).unwrap(), + ); let _ = conn.sink.start_send_unpin(data); } Err(err) => log::error!( From 7ac7728f5d06b6a59dc26fb73d8083176bfc5a99 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 26 Feb 2021 14:36:44 +0100 Subject: [PATCH 61/92] Forgot to wrap the payload --- client/telemetry/src/lib.rs | 8 +++++++- client/telemetry/src/node.rs | 5 ----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index f479519e29862..4e6bb4c2a8ec6 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -275,7 +275,13 @@ impl TelemetryWorker { node_pool: &mut HashMap>, node_map: &HashMap>, ) { - let (id, verbosity, message) = input.expect("the stream is never closed; qed"); + let (id, verbosity, payload) = input.expect("the stream is never closed; qed"); + + let ts = chrono::Local::now().to_rfc3339().to_string(); + let mut message = serde_json::Map::new(); + message.insert("id".into(), id.into()); + message.insert("ts".into(), ts.into()); + message.insert("payload".into(), payload.into()); let nodes = if let Some(nodes) = node_map.get(&id) { nodes diff --git a/client/telemetry/src/node.rs b/client/telemetry/src/node.rs index bc1c20032b7f7..0ecc9d6007e3b 100644 --- a/client/telemetry/src/node.rs +++ b/client/telemetry/src/node.rs @@ -245,11 +245,6 @@ where match &mut self.socket { NodeSocket::Connected(conn) => match serde_json::to_vec(&item) { Ok(data) => { - log::error!( - target: "telemetry", - "sent: {}", - serde_json::to_string(&item).unwrap(), - ); let _ = conn.sink.start_send_unpin(data); } Err(err) => log::error!( From 49541abb658c4c7fa15126c971b3d30dcf338a99 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 26 Feb 2021 14:37:08 +0100 Subject: [PATCH 62/92] Revert "Temporary changes" This reverts commit ee8eae659974b2cde7b19bf634d1aed9452c4876. --- bin/node/cli/browser-demo/build.sh | 2 +- bin/node/cli/src/browser.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/bin/node/cli/browser-demo/build.sh b/bin/node/cli/browser-demo/build.sh index 544b16333aeb2..8840106daeb5c 100755 --- a/bin/node/cli/browser-demo/build.sh +++ b/bin/node/cli/browser-demo/build.sh @@ -2,4 +2,4 @@ set -e -x cargo +nightly build --release -p node-cli --target wasm32-unknown-unknown --no-default-features --features browser -Z features=itarget wasm-bindgen ../../../../target/wasm32-unknown-unknown/release/node_cli.wasm --out-dir pkg --target web -python -m http.server 8080 +python -m http.server 8000 diff --git a/bin/node/cli/src/browser.rs b/bin/node/cli/src/browser.rs index cfc7e50b2b4ee..49ac309d42abc 100644 --- a/bin/node/cli/src/browser.rs +++ b/bin/node/cli/src/browser.rs @@ -44,8 +44,7 @@ async fn start_inner( None => crate::chain_spec::development_config(), }; - let mut config = browser_configuration(chain_spec).await?; - config.telemetry_endpoints = Some(sc_telemetry::TelemetryEndpoints::new(vec![("ws://127.0.0.1:8000/submit".to_string(), 10)]).unwrap()); + let config = browser_configuration(chain_spec).await?; info!("Substrate browser node"); info!("✌️ version {}", config.impl_version); From 133770f71307fbfc0f805cb5a8c7e1f571b60fb0 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 26 Feb 2021 15:07:19 +0100 Subject: [PATCH 63/92] Fix some tests --- client/basic-authorship/src/basic_authorship.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/client/basic-authorship/src/basic_authorship.rs b/client/basic-authorship/src/basic_authorship.rs index 9f6f970d5a425..93ee4fc1445de 100644 --- a/client/basic-authorship/src/basic_authorship.rs +++ b/client/basic-authorship/src/basic_authorship.rs @@ -471,6 +471,7 @@ mod tests { client.clone(), txpool.clone(), None, + None, ); let cell = Mutex::new((false, time::Instant::now())); @@ -518,6 +519,7 @@ mod tests { client.clone(), txpool.clone(), None, + None, ); let cell = Mutex::new((false, time::Instant::now())); @@ -574,6 +576,7 @@ mod tests { client.clone(), txpool.clone(), None, + None, ); let proposer = proposer_factory.init_with_now( @@ -649,6 +652,7 @@ mod tests { client.clone(), txpool.clone(), None, + None, ); let mut propose_block = | client: &TestClient, From 7bb9bf5d1a6222694f8b844aebccf88f45b0171e Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 26 Feb 2021 15:08:09 +0100 Subject: [PATCH 64/92] Fix doc test --- client/basic-authorship/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/client/basic-authorship/src/lib.rs b/client/basic-authorship/src/lib.rs index ccf73cc93f197..acaf85db76336 100644 --- a/client/basic-authorship/src/lib.rs +++ b/client/basic-authorship/src/lib.rs @@ -45,6 +45,7 @@ //! client.clone(), //! txpool.clone(), //! None, +//! None, //! ); //! //! // From this factory, we create a `Proposer`. From cd8d77bd0e710a25df86711e708335952e818759 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 26 Feb 2021 15:32:11 +0100 Subject: [PATCH 65/92] Fix tests --- client/consensus/manual-seal/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/client/consensus/manual-seal/src/lib.rs b/client/consensus/manual-seal/src/lib.rs index 320f196c1052c..64de70939503c 100644 --- a/client/consensus/manual-seal/src/lib.rs +++ b/client/consensus/manual-seal/src/lib.rs @@ -300,6 +300,7 @@ mod tests { client.clone(), pool.clone(), None, + None, ); // this test checks that blocks are created as soon as transactions are imported into the pool. let (sender, receiver) = futures::channel::oneshot::channel(); @@ -371,6 +372,7 @@ mod tests { client.clone(), pool.clone(), None, + None, ); // this test checks that blocks are created as soon as an engine command is sent over the stream. let (mut sink, commands_stream) = futures::channel::mpsc::channel(1024); @@ -446,6 +448,7 @@ mod tests { client.clone(), pool.clone(), None, + None, ); // this test checks that blocks are created as soon as an engine command is sent over the stream. let (mut sink, commands_stream) = futures::channel::mpsc::channel(1024); From 08085362a1cafad971d8e6819d3c12366a28fd2d Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 26 Feb 2021 16:01:55 +0100 Subject: [PATCH 66/92] Fix more tests --- client/consensus/aura/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/client/consensus/aura/src/lib.rs b/client/consensus/aura/src/lib.rs index 91e31284f6802..af3a46a38215f 100644 --- a/client/consensus/aura/src/lib.rs +++ b/client/consensus/aura/src/lib.rs @@ -1003,6 +1003,7 @@ mod tests { inherent_data_providers, phantom: Default::default(), can_author_with: AlwaysCanAuthor, + telemetry: None, } }, PeersClient::Light(_, _) => unreachable!("No (yet) tests for light client + Aura"), @@ -1079,6 +1080,7 @@ mod tests { Some(BackoffAuthoringOnFinalizedHeadLagging::default()), keystore, sp_consensus::AlwaysCanAuthor, + None, ).expect("Starts aura")); } @@ -1138,6 +1140,7 @@ mod tests { sync_oracle: DummyOracle.clone(), force_authoring: false, backoff_authoring_blocks: Some(BackoffAuthoringOnFinalizedHeadLagging::default()), + telemetry: None, _key_type: PhantomData::, }; @@ -1185,6 +1188,7 @@ mod tests { sync_oracle: DummyOracle.clone(), force_authoring: false, backoff_authoring_blocks: Option::<()>::None, + telemetry: None, _key_type: PhantomData::, }; From 3b23031f7370ceeef6efb85579e46cdb1cc5b2b9 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 26 Feb 2021 16:17:40 +0100 Subject: [PATCH 67/92] Fixes --- bin/node/bench/src/construct.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/node/bench/src/construct.rs b/bin/node/bench/src/construct.rs index b64ffec641c22..8469ec62893b5 100644 --- a/bin/node/bench/src/construct.rs +++ b/bin/node/bench/src/construct.rs @@ -151,6 +151,7 @@ impl core::Benchmark for ConstructionBenchmark { context.client.clone(), self.transactions.clone().into(), None, + None, ); let inherent_data_providers = sp_inherents::InherentDataProviders::new(); inherent_data_providers From 4d02b5f9f52a36fcc55f8ed493e6384b8003116b Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 26 Feb 2021 16:37:17 +0100 Subject: [PATCH 68/92] Fix tests --- client/consensus/babe/src/tests.rs | 1 + client/finality-grandpa/src/tests.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/client/consensus/babe/src/tests.rs b/client/consensus/babe/src/tests.rs index 7f4b2d1c8b014..63c7e0c120377 100644 --- a/client/consensus/babe/src/tests.rs +++ b/client/consensus/babe/src/tests.rs @@ -432,6 +432,7 @@ fn run_one_test( babe_link: data.link.clone(), keystore, can_author_with: sp_consensus::AlwaysCanAuthor, + telemetry: None, }).expect("Starts babe")); } futures::executor::block_on(future::select( diff --git a/client/finality-grandpa/src/tests.rs b/client/finality-grandpa/src/tests.rs index 2cda1e52e92db..6824a8ed04273 100644 --- a/client/finality-grandpa/src/tests.rs +++ b/client/finality-grandpa/src/tests.rs @@ -120,6 +120,7 @@ impl TestNetFactory for GrandpaTestNet { client.clone(), &self.test_config, LongestChain::new(backend.clone()), + None, ).expect("Could not create block import for fresh peer."); let justification_import = Box::new(import.clone()); ( From 1b5df60760c95ebf522aa62d3ccbc6338c42bc96 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 3 Mar 2021 13:08:13 +0100 Subject: [PATCH 69/92] Revert TracingBounded channel --- primitives/utils/src/mpsc.rs | 195 +---------------------------------- 1 file changed, 4 insertions(+), 191 deletions(-) diff --git a/primitives/utils/src/mpsc.rs b/primitives/utils/src/mpsc.rs index 66f891cc83b6f..b033a5527d84a 100644 --- a/primitives/utils/src/mpsc.rs +++ b/primitives/utils/src/mpsc.rs @@ -20,24 +20,14 @@ #[cfg(not(feature = "metered"))] mod inner { // just aliased, non performance implications - use futures::channel::mpsc::{self, UnboundedReceiver, UnboundedSender, Receiver, Sender}; + use futures::channel::mpsc::{self, UnboundedReceiver, UnboundedSender}; pub type TracingUnboundedSender = UnboundedSender; pub type TracingUnboundedReceiver = UnboundedReceiver; - pub type TracingSender = Sender; - pub type TracingReceiver = Receiver; /// Alias `mpsc::unbounded` pub fn tracing_unbounded(_key: &'static str) ->(TracingUnboundedSender, TracingUnboundedReceiver) { mpsc::unbounded() } - - /// Alias `mpsc::channel` - pub fn tracing_channel( - _key: &'static str, - buffer: usize, - ) -> (TracingUnboundedSender, TracingUnboundedReceiver) { - mpsc::channel() - } } @@ -45,7 +35,7 @@ mod inner { mod inner { //tracing implementation use futures::channel::mpsc::{self, - UnboundedReceiver, UnboundedSender, Receiver, Sender, + UnboundedReceiver, UnboundedSender, TryRecvError, TrySendError, SendError }; use futures::{sink::Sink, task::{Poll, Context}, stream::{Stream, FusedStream}}; @@ -177,7 +167,7 @@ mod inner { Poll::Ready(msg) => { if msg.is_some() { UNBOUNDED_CHANNELS_COUNTER.with_label_values(&[s.0, "received"]).inc(); - } + } Poll::Ready(msg) } Poll::Pending => { @@ -256,183 +246,6 @@ mod inner { Poll::Ready(Ok(())) } } - - /// Wrapper Type around `Sender` that increases the global - /// measure when a message is added - #[derive(Debug)] - pub struct TracingSender(&'static str, Sender); - - // Strangely, deriving `Clone` requires that `T` is also `Clone`. - impl Clone for TracingSender { - fn clone(&self) -> Self { - Self(self.0, self.1.clone()) - } - } - - /// Wrapper Type around `Receiver` that decreases the global - /// measure when a message is polled - #[derive(Debug)] - pub struct TracingReceiver(&'static str, Receiver); - - /// Wrapper around `mpsc::unbounded` that tracks the in- and outflow via - /// `UNBOUNDED_CHANNELS_COUNTER` - pub fn tracing_channel( - key: &'static str, - buffer: usize, - ) -> (TracingSender, TracingReceiver) { - let (s, r) = mpsc::channel(buffer); - (TracingSender(key, s), TracingReceiver(key,r)) - } - - impl TracingSender { - /// Proxy function to mpsc::Sender - pub fn poll_ready(&mut self, ctx: &mut Context) -> Poll> { - self.1.poll_ready(ctx) - } - - /// Proxy function to mpsc::Sender - pub fn is_closed(&self) -> bool { - self.1.is_closed() - } - - /// Proxy function to mpsc::Sender - pub fn close_channel(&mut self) { - self.1.close_channel() - } - - /// Proxy function to mpsc::Sender - pub fn disconnect(&mut self) { - self.1.disconnect() - } - - /// Proxy function to mpsc::Sender - pub fn start_send(&mut self, msg: T) -> Result<(), SendError> { - self.1.start_send(msg) - } - - /// Proxy function to mpsc::Sender - pub fn try_send(&mut self, msg: T) -> Result<(), TrySendError> { - self.1.try_send(msg).map(|s|{ - UNBOUNDED_CHANNELS_COUNTER.with_label_values(&[self.0, &"send"]).inc(); - s - }) - } - - /// Proxy function to mpsc::Sender - pub fn same_receiver(&self, other: &Sender) -> bool { - self.1.same_receiver(other) - } - } - - impl TracingReceiver { - - fn consume(&mut self) { - // consume all items, make sure to reflect the updated count - let mut count = 0; - loop { - if self.1.is_terminated() { - break; - } - - match self.try_next() { - Ok(Some(..)) => count += 1, - _ => break - } - } - // and discount the messages - if count > 0 { - UNBOUNDED_CHANNELS_COUNTER.with_label_values(&[self.0, &"dropped"]).inc_by(count); - } - - } - - /// Proxy function to mpsc::Receiver - /// that consumes all messages first and updates the counter - pub fn close(&mut self) { - self.consume(); - self.1.close() - } - - /// Proxy function to mpsc::Receiver - /// that discounts the messages taken out - pub fn try_next(&mut self) -> Result, TryRecvError> { - self.1.try_next().map(|s| { - if s.is_some() { - UNBOUNDED_CHANNELS_COUNTER.with_label_values(&[self.0, &"received"]).inc(); - } - s - }) - } - } - - impl Drop for TracingReceiver { - fn drop(&mut self) { - self.consume(); - } - } - - impl Unpin for TracingReceiver {} - - impl Stream for TracingReceiver { - type Item = T; - - fn poll_next( - self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll> { - let s = self.get_mut(); - match Pin::new(&mut s.1).poll_next(cx) { - Poll::Ready(msg) => { - if msg.is_some() { - UNBOUNDED_CHANNELS_COUNTER.with_label_values(&[s.0, "received"]).inc(); - } - Poll::Ready(msg) - } - Poll::Pending => { - Poll::Pending - } - } - } - } - - impl FusedStream for TracingReceiver { - fn is_terminated(&self) -> bool { - self.1.is_terminated() - } - } - - impl Sink for TracingSender { - type Error = SendError; - - fn poll_ready( - mut self: Pin<&mut Self>, - cx: &mut Context<'_>, - ) -> Poll> { - TracingSender::poll_ready(&mut *self, cx) - } - - fn start_send( - mut self: Pin<&mut Self>, - msg: T, - ) -> Result<(), Self::Error> { - TracingSender::start_send(&mut *self, msg) - } - - fn poll_flush( - self: Pin<&mut Self>, - _: &mut Context<'_>, - ) -> Poll> { - Poll::Ready(Ok(())) - } - - fn poll_close( - mut self: Pin<&mut Self>, - _: &mut Context<'_>, - ) -> Poll> { - self.disconnect(); - Poll::Ready(Ok(())) - } - } } -pub use inner::*; +pub use inner::{tracing_unbounded, TracingUnboundedSender, TracingUnboundedReceiver}; From 9dae7e4ec818ea0382e6306545c71cd84423d893 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 3 Mar 2021 13:16:11 +0100 Subject: [PATCH 70/92] Replace channel with futures' mpsc channel --- Cargo.lock | 1 - client/telemetry/Cargo.toml | 1 - client/telemetry/src/node.rs | 7 ++++--- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e282d5f4fd2d7..c9f6a6706a5ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7753,7 +7753,6 @@ dependencies = [ "rand 0.7.3", "serde", "serde_json", - "sp-utils", "take_mut", "thiserror", "void", diff --git a/client/telemetry/Cargo.toml b/client/telemetry/Cargo.toml index 41dde1f73698e..5cacae400d2e6 100644 --- a/client/telemetry/Cargo.toml +++ b/client/telemetry/Cargo.toml @@ -26,7 +26,6 @@ serde = { version = "1.0.101", features = ["derive"] } take_mut = "0.2.2" void = "1.0.2" serde_json = "1.0.41" -sp-utils = { version = "3.0.0", path = "../../primitives/utils" } chrono = "0.4.19" global_counter = "0.2.1" thiserror = "1.0.21" diff --git a/client/telemetry/src/node.rs b/client/telemetry/src/node.rs index 0ecc9d6007e3b..2cd696894634f 100644 --- a/client/telemetry/src/node.rs +++ b/client/telemetry/src/node.rs @@ -18,18 +18,19 @@ use crate::TelemetryPayload; use futures::prelude::*; +use futures::channel::mpsc; use libp2p::core::transport::Transport; use libp2p::Multiaddr; use rand::Rng as _; use std::{fmt, mem, pin::Pin, task::Context, task::Poll, time::Duration}; use wasm_timer::Delay; -pub(crate) type ConnectionNotifierSender = sp_utils::mpsc::TracingSender<()>; -pub(crate) type ConnectionNotifierReceiver = sp_utils::mpsc::TracingReceiver<()>; +pub(crate) type ConnectionNotifierSender = mpsc::Sender<()>; +pub(crate) type ConnectionNotifierReceiver = mpsc::Receiver<()>; pub(crate) fn connection_notifier_channel() -> (ConnectionNotifierSender, ConnectionNotifierReceiver) { - sp_utils::mpsc::tracing_channel("mpsc_telemetry_on_connect", 0) + mpsc::channel(0) } /// Handler for a single telemetry node. From 4ad16438586331ae6f8d7575b84bbc5d16e0650c Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 3 Mar 2021 13:18:31 +0100 Subject: [PATCH 71/92] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/telemetry/src/node.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/telemetry/src/node.rs b/client/telemetry/src/node.rs index 2cd696894634f..edc9a006e75c6 100644 --- a/client/telemetry/src/node.rs +++ b/client/telemetry/src/node.rs @@ -248,7 +248,7 @@ where Ok(data) => { let _ = conn.sink.start_send_unpin(data); } - Err(err) => log::error!( + Err(err) => log::debug!( target: "telemetry", "Could not serialize payload: {}", err, From 31e08fa0a50fea30e5dd3c0c6c7d665853c35dae Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 3 Mar 2021 13:19:55 +0100 Subject: [PATCH 72/92] Apply suggestion --- client/telemetry/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 4e6bb4c2a8ec6..cc3a58c5394b6 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -316,7 +316,7 @@ impl TelemetryWorker { if let Some(node) = node_pool.get_mut(&addr) { let _ = node.send(message.clone()).await; } else { - log::error!( + log::debug!( target: "telemetry", "Received message for unknown node ({}). This is a bug. \ Message sent: {}", From 0650ca9738194455610d36d71c9501daaf5dbaad Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 3 Mar 2021 13:31:16 +0100 Subject: [PATCH 73/92] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- client/telemetry/src/lib.rs | 42 ++++++++++++++----------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index cc3a58c5394b6..9972d31b19def 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -119,8 +119,9 @@ pub struct TelemetryWorker { } impl TelemetryWorker { - /// Instantiate a new [`TelemetryWorker`] which can run in background. Only one is needed for - /// the process. + /// Instantiate a new [`TelemetryWorker`] which can run in background. + /// + /// Only one is needed per process. pub fn new(buffer_size: usize, transport: Option) -> Result { let transport = initialize_transport(transport)?; let (message_sender, message_receiver) = mpsc::channel(buffer_size); @@ -286,7 +287,7 @@ impl TelemetryWorker { let nodes = if let Some(nodes) = node_map.get(&id) { nodes } else { - // This is a normal error because the telemetry ID exists sooner than the telemetry is + // This is a normal error because the telemetry ID exists before the telemetry is // initialized. log::trace!( target: "telemetry", @@ -342,7 +343,7 @@ pub struct TelemetryWorkerHandle { } impl TelemetryWorkerHandle { - /// Instantiate a new [`Telemetry`] object which is normally stored in the Substrate's Client. + /// Instantiate a new [`Telemetry`] object. pub fn new_telemetry(&mut self, endpoints: TelemetryEndpoints) -> Telemetry { let addresses = endpoints.0.iter().map(|(addr, _)| addr.clone()).collect(); @@ -359,7 +360,7 @@ impl TelemetryWorkerHandle { } } -/// A telemetry instance that can be used to send telemetries. +/// A telemetry instance that can be used to send telemetry messages. #[derive(Debug)] pub struct Telemetry { message_sender: mpsc::Sender, @@ -381,31 +382,18 @@ impl Telemetry { /// The `connection_message` argument is a JSON object that is sent every time the connection /// (re-)establishes. pub fn start_telemetry(&mut self, connection_message: ConnectionMessage) -> Result<()> { - let Self { - message_sender: _, - register_sender, - id, - connection_notifier: _, - endpoints, - } = self; - - let endpoints = match endpoints.take() { + let endpoints = match self.endpoints.take() { Some(x) => x, None => return Err(Error::TelemetryAlreadyInitialized), }; - if register_sender + self.register_sender .unbounded_send(Register::Telemetry { - id: *id, + id: *self.id, endpoints, connection_message, }) - .is_err() - { - return Err(Error::TelemetryWorkerDropped); - } - - Ok(()) + .map_err(|_| Error::TelemetryWorkerDropped) } /// Make a new clonable handle to this [`Telemetry`]. This is used for reporting telemetries. @@ -418,7 +406,9 @@ impl Telemetry { } } -/// Handle to a [`Telemetry`]. Used to report telemetries. +/// Handle to a [`Telemetry`]. +/// +/// Used to report telemetry messages. #[derive(Debug, Clone)] pub struct TelemetryHandle { message_sender: Arc>>, @@ -427,13 +417,13 @@ pub struct TelemetryHandle { } impl TelemetryHandle { - /// Send telemetries. + /// Send telemetry messages. pub fn send_telemetry(&self, verbosity: VerbosityLevel, payload: TelemetryPayload) { match self.message_sender.lock().try_send((self.id, verbosity, payload)) { Ok(()) => {} Err(err) if err.is_full() => log::trace!( target: "telemetry", - "buffer overflow", + "Telemetry channel full.", ), Err(_) => unreachable!(), } @@ -516,7 +506,7 @@ macro_rules! telemetry { let verbosity: $crate::VerbosityLevel = $verbosity; match format_fields_to_json!($($t)*) { Err(err) => { - $crate::log::error!( + $crate::log::debug!( target: "telemetry", "Could not serialize value for telemetry: {}", err, From 13c62f31160e500595b254e67b3e290ca00cb92e Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 3 Mar 2021 13:32:19 +0100 Subject: [PATCH 74/92] WIP --- client/telemetry/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 9972d31b19def..59423be7f3542 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -389,7 +389,7 @@ impl Telemetry { self.register_sender .unbounded_send(Register::Telemetry { - id: *self.id, + id: self.id, endpoints, connection_message, }) From 051985ea5ab84e6fa0c26e98f86f261de14f1ddb Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 3 Mar 2021 13:33:42 +0100 Subject: [PATCH 75/92] Fix invalid unreachable!() --- client/telemetry/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 59423be7f3542..b05e6a2caac56 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -425,7 +425,10 @@ impl TelemetryHandle { target: "telemetry", "Telemetry channel full.", ), - Err(_) => unreachable!(), + Err(_) => log::trace!( + target: "telemetry", + "Telemetry channel closed.", + ), } } From 8d680216fb6e0ce057322ff564f7b0c64d5c0f5c Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 3 Mar 2021 13:43:18 +0100 Subject: [PATCH 76/92] Remove global_counter --- Cargo.lock | 11 -------- client/telemetry/Cargo.toml | 1 - client/telemetry/src/lib.rs | 55 ++++++++++++++++-------------------- client/telemetry/src/node.rs | 2 +- 4 files changed, 25 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c9f6a6706a5ab..f19c1c04b1fd1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2191,16 +2191,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" -[[package]] -name = "global_counter" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "037cd38cf90be505d4f74eef395d027308d74b5dda77e328ad53244b30566a36" -dependencies = [ - "once_cell", - "parking_lot 0.10.2", -] - [[package]] name = "globset" version = "0.4.6" @@ -7745,7 +7735,6 @@ version = "3.0.0" dependencies = [ "chrono", "futures 0.3.12", - "global_counter", "libp2p", "log", "parking_lot 0.11.1", diff --git a/client/telemetry/Cargo.toml b/client/telemetry/Cargo.toml index 5cacae400d2e6..ac64d34bea9fe 100644 --- a/client/telemetry/Cargo.toml +++ b/client/telemetry/Cargo.toml @@ -27,5 +27,4 @@ take_mut = "0.2.2" void = "1.0.2" serde_json = "1.0.41" chrono = "0.4.19" -global_counter = "0.2.1" thiserror = "1.0.21" diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index b05e6a2caac56..95e046158e09e 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -37,13 +37,12 @@ #![warn(missing_docs)] use futures::{channel::mpsc, prelude::*}; -use global_counter::primitive::exact::CounterU64; use libp2p::Multiaddr; use log::{error, warn}; -use serde::Serialize; use parking_lot::Mutex; +use serde::Serialize; use std::collections::HashMap; -use std::sync::Arc; +use std::sync::{atomic, Arc}; pub use libp2p::wasm_ext::ExtTransport; pub use log; @@ -114,7 +113,7 @@ pub struct TelemetryWorker { message_sender: mpsc::Sender, register_receiver: mpsc::UnboundedReceiver, register_sender: mpsc::UnboundedSender, - id_generator: Arc, + id_counter: Arc, transport: WsTrans, } @@ -132,7 +131,7 @@ impl TelemetryWorker { message_sender, register_receiver, register_sender, - id_generator: Arc::new(CounterU64::new(1)), + id_counter: Arc::new(atomic::AtomicU64::new(1)), transport, }) } @@ -144,40 +143,31 @@ impl TelemetryWorker { TelemetryWorkerHandle { message_sender: self.message_sender.clone(), register_sender: self.register_sender.clone(), - id_generator: self.id_generator.clone(), + id_counter: self.id_counter.clone(), } } /// Run the telemetry worker. /// /// This should be run in a background task. - pub async fn run(self) { - let Self { - mut message_receiver, - message_sender: _, - mut register_receiver, - register_sender: _, - id_generator: _, - transport, - } = self; - + pub async fn run(mut self) { let mut node_map: HashMap> = HashMap::new(); let mut node_pool: HashMap = HashMap::new(); let mut pending_connection_notifications: Vec<_> = Vec::new(); loop { futures::select! { - message = message_receiver.next() => Self::process_message( + message = self.message_receiver.next() => Self::process_message( message, &mut node_pool, &node_map, ).await, - init_payload = register_receiver.next() => Self::process_register( + init_payload = self.register_receiver.next() => Self::process_register( init_payload, &mut node_pool, &mut node_map, &mut pending_connection_notifications, - transport.clone(), + self.transport.clone(), ).await, } } @@ -238,16 +228,15 @@ impl TelemetryWorker { node.connection_messages.extend(connection_message.clone()); - pending_connection_notifications.retain( - |(addr_b, connection_message)| { - if *addr_b == addr { - node.telemetry_connection_notifier.push(connection_message.clone()); - false - } else { - true - } + pending_connection_notifications.retain(|(addr_b, connection_message)| { + if *addr_b == addr { + node.telemetry_connection_notifier + .push(connection_message.clone()); + false + } else { + true } - ); + }); } } Register::Notifier { @@ -339,7 +328,7 @@ impl TelemetryWorker { pub struct TelemetryWorkerHandle { message_sender: mpsc::Sender, register_sender: mpsc::UnboundedSender, - id_generator: Arc, + id_counter: Arc, } impl TelemetryWorkerHandle { @@ -350,7 +339,7 @@ impl TelemetryWorkerHandle { Telemetry { message_sender: self.message_sender.clone(), register_sender: self.register_sender.clone(), - id: self.id_generator.inc(), + id: self.id_counter.fetch_add(1, atomic::Ordering::Relaxed), connection_notifier: TelemetryConnectionNotifier { register_sender: self.register_sender.clone(), addresses, @@ -419,7 +408,11 @@ pub struct TelemetryHandle { impl TelemetryHandle { /// Send telemetry messages. pub fn send_telemetry(&self, verbosity: VerbosityLevel, payload: TelemetryPayload) { - match self.message_sender.lock().try_send((self.id, verbosity, payload)) { + match self + .message_sender + .lock() + .try_send((self.id, verbosity, payload)) + { Ok(()) => {} Err(err) if err.is_full() => log::trace!( target: "telemetry", diff --git a/client/telemetry/src/node.rs b/client/telemetry/src/node.rs index edc9a006e75c6..2d1a04b00a4cd 100644 --- a/client/telemetry/src/node.rs +++ b/client/telemetry/src/node.rs @@ -17,8 +17,8 @@ // along with this program. If not, see . use crate::TelemetryPayload; -use futures::prelude::*; use futures::channel::mpsc; +use futures::prelude::*; use libp2p::core::transport::Transport; use libp2p::Multiaddr; use rand::Rng as _; From 01959025818ca7b52e9fafc06de92aa39d098bba Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 3 Mar 2021 14:36:04 +0100 Subject: [PATCH 77/92] Do not create telemetry worker if not necessary --- bin/node-template/node/src/service.rs | 34 ++++++++++++++----- bin/node/cli/src/service.rs | 48 ++++++++++++++++++--------- 2 files changed, 57 insertions(+), 25 deletions(-) diff --git a/bin/node-template/node/src/service.rs b/bin/node-template/node/src/service.rs index 101cad58aac9b..3c1eb5e9272aa 100644 --- a/bin/node-template/node/src/service.rs +++ b/bin/node-template/node/src/service.rs @@ -46,19 +46,27 @@ pub fn new_partial(config: &Configuration) -> Result Result<_, sc_telemetry::Error> { + let worker = TelemetryWorker::new(16, None)?; + let telemetry = worker.handle().new_telemetry(endpoints); + Ok((worker, telemetry)) + }) + .transpose()?; let (client, backend, keystore_container, task_manager) = sc_service::new_full_parts::( &config, - telemetry.as_ref().map(|x| x.handle()), + telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()), )?; let client = Arc::new(client); - task_manager.spawn_handle().spawn("telemetry", telemetry_worker.run()); + let telemetry = telemetry + .map(|(worker, telemetry)| { + task_manager.spawn_handle().spawn("telemetry", worker.run()); + telemetry + }); let select_chain = sc_consensus::LongestChain::new(backend.clone()); @@ -278,18 +286,26 @@ pub fn new_full(mut config: Configuration) -> Result /// Builds a new service for a light client. pub fn new_light(mut config: Configuration) -> Result { - let telemetry_worker = TelemetryWorker::new(16, None)?; - let mut telemetry = config.telemetry_endpoints.clone() + let telemetry = config.telemetry_endpoints.clone() .filter(|x| !x.is_empty()) - .map(|endpoints| telemetry_worker.handle().new_telemetry(endpoints)); + .map(|endpoints| -> Result<_, sc_telemetry::Error> { + let worker = TelemetryWorker::new(16, None)?; + let telemetry = worker.handle().new_telemetry(endpoints); + Ok((worker, telemetry)) + }) + .transpose()?; let (client, backend, keystore_container, mut task_manager, on_demand) = sc_service::new_light_parts::( &config, - telemetry.as_ref().map(|x| x.handle()), + telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()), )?; - task_manager.spawn_handle().spawn("telemetry", telemetry_worker.run()); + let mut telemetry = telemetry + .map(|(worker, telemetry)| { + task_manager.spawn_handle().spawn("telemetry", worker.run()); + telemetry + }); config.network.extra_sets.push(sc_finality_grandpa::grandpa_peers_set_config()); diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index 96c089945aded..9d3e20b05c307 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -63,19 +63,27 @@ pub fn new_partial( Option, ) >, ServiceError> { - let telemetry_worker = TelemetryWorker::new(16, None)?; let telemetry = config.telemetry_endpoints.clone() .filter(|x| !x.is_empty()) - .map(|endpoints| telemetry_worker.handle().new_telemetry(endpoints)); + .map(|endpoints| -> Result<_, sc_telemetry::Error> { + let worker = TelemetryWorker::new(16, None)?; + let telemetry = worker.handle().new_telemetry(endpoints); + Ok((worker, telemetry)) + }) + .transpose()?; let (client, backend, keystore_container, task_manager) = sc_service::new_full_parts::( &config, - telemetry.as_ref().map(|x| x.handle()), + telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()), )?; let client = Arc::new(client); - task_manager.spawn_handle().spawn("telemetry", telemetry_worker.run()); + let telemetry = telemetry + .map(|(worker, telemetry)| { + task_manager.spawn_handle().spawn("telemetry", worker.run()); + telemetry + }); let select_chain = sc_consensus::LongestChain::new(backend.clone()); @@ -395,25 +403,33 @@ pub fn new_light_base( Arc::Hash>>, Arc>> ), ServiceError> { - #[cfg(feature = "browser")] - let transport = Some( - sc_telemetry::ExtTransport::new(libp2p_wasm_ext::ffi::websocket_transport()) - ); - #[cfg(not(feature = "browser"))] - let transport = None; - - let telemetry_worker = TelemetryWorker::new(16, transport)?; - let mut telemetry = config.telemetry_endpoints.clone() + let telemetry = config.telemetry_endpoints.clone() .filter(|x| !x.is_empty()) - .map(|endpoints| telemetry_worker.handle().new_telemetry(endpoints)); + .map(|endpoints| -> Result<_, sc_telemetry::Error> { + #[cfg(feature = "browser")] + let transport = Some( + sc_telemetry::ExtTransport::new(libp2p_wasm_ext::ffi::websocket_transport()) + ); + #[cfg(not(feature = "browser"))] + let transport = None; + + let worker = TelemetryWorker::new(16, transport)?; + let telemetry = worker.handle().new_telemetry(endpoints); + Ok((worker, telemetry)) + }) + .transpose()?; let (client, backend, keystore_container, mut task_manager, on_demand) = sc_service::new_light_parts::( &config, - telemetry.as_ref().map(|x| x.handle()), + telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()), )?; - task_manager.spawn_handle().spawn("telemetry", telemetry_worker.run()); + let mut telemetry = telemetry + .map(|(worker, telemetry)| { + task_manager.spawn_handle().spawn("telemetry", worker.run()); + telemetry + }); config.network.extra_sets.push(grandpa::grandpa_peers_set_config()); From 8e0dfd5c66a6c35cbbc75d918341d3c2c8dd72fc Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 13:58:34 +0100 Subject: [PATCH 78/92] Revert finality-grandpa --- client/finality-grandpa/src/authorities.rs | 73 ++--- client/finality-grandpa/src/aux_schema.rs | 2 +- .../src/communication/gossip.rs | 53 +--- .../finality-grandpa/src/communication/mod.rs | 187 +++++------- .../src/communication/tests.rs | 2 - client/finality-grandpa/src/environment.rs | 267 ++++++++---------- client/finality-grandpa/src/finality_proof.rs | 3 + client/finality-grandpa/src/import.rs | 21 +- client/finality-grandpa/src/lib.rs | 82 ++---- client/finality-grandpa/src/observer.rs | 15 +- client/finality-grandpa/src/tests.rs | 25 +- 11 files changed, 260 insertions(+), 470 deletions(-) diff --git a/client/finality-grandpa/src/authorities.rs b/client/finality-grandpa/src/authorities.rs index d077327f5d938..11d3d4ba691da 100644 --- a/client/finality-grandpa/src/authorities.rs +++ b/client/finality-grandpa/src/authorities.rs @@ -23,7 +23,7 @@ use parking_lot::RwLock; use finality_grandpa::voter_set::VoterSet; use parity_scale_codec::{Encode, Decode}; use log::debug; -use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, CONSENSUS_INFO}; use sp_finality_grandpa::{AuthorityId, AuthorityList}; use std::cmp::Ord; @@ -43,8 +43,8 @@ pub enum Error { #[display(fmt = "Multiple pending forced authority set changes are not allowed.")] MultiplePendingForcedAuthoritySetChanges, #[display( - fmt = "A pending forced authority set change could not be applied since it must be applied \ - after the pending standard change at #{}", + fmt = "A pending forced authority set change could not be applied since it must be applied after \ + the pending standard change at #{}", _0 )] ForcedAuthoritySetChangeDependencyUnsatisfied(N), @@ -278,13 +278,9 @@ where let hash = pending.canon_hash.clone(); let number = pending.canon_height.clone(); - debug!( - target: "afg", - "Inserting potential standard set change signaled at block {:?} (delayed by {:?} - blocks).", - (&number, &hash), - pending.delay, - ); + debug!(target: "afg", "Inserting potential standard set change signaled at block {:?} \ + (delayed by {:?} blocks).", + (&number, &hash), pending.delay); self.pending_standard_changes.import( hash, @@ -293,10 +289,8 @@ where is_descendent_of, )?; - debug!( - target: "afg", - "There are now {} alternatives for the next pending standard change (roots), and a - total of {} pending standard changes (across all forks).", + debug!(target: "afg", "There are now {} alternatives for the next pending standard change (roots), \ + and a total of {} pending standard changes (across all forks).", self.pending_standard_changes.roots().count(), self.pending_standard_changes.iter().count(), ); @@ -332,12 +326,9 @@ where )) .unwrap_or_else(|i| i); - debug!( - target: "afg", - "Inserting potential forced set change at block {:?} (delayed by {:?} blocks).", - (&pending.canon_height, &pending.canon_hash), - pending.delay, - ); + debug!(target: "afg", "Inserting potential forced set change at block {:?} \ + (delayed by {:?} blocks).", + (&pending.canon_height, &pending.canon_hash), pending.delay); self.pending_forced_changes.insert(idx, pending); @@ -418,7 +409,6 @@ where best_number: N, is_descendent_of: &F, initial_sync: bool, - telemetry: Option, ) -> Result, Error> where F: Fn(&H, &H) -> Result, @@ -471,7 +461,6 @@ where ); telemetry!( - telemetry; CONSENSUS_INFO; "afg.applying_forced_authority_set_change"; "block" => ?change.canon_height @@ -516,7 +505,6 @@ where finalized_number: N, is_descendent_of: &F, initial_sync: bool, - telemetry: Option<&mut TelemetryHandle>, ) -> Result, Error> where F: Fn(&H, &H) -> Result, @@ -556,10 +544,7 @@ where "👴 Applying authority set change scheduled at block #{:?}", change.canon_height, ); - telemetry!( - telemetry; - CONSENSUS_INFO; - "afg.applying_scheduled_authority_set_change"; + telemetry!(CONSENSUS_INFO; "afg.applying_scheduled_authority_set_change"; "block" => ?change.canon_height ); @@ -909,7 +894,6 @@ mod tests { _ => unreachable!(), }), false, - None, ).unwrap(); assert!(status.changed); @@ -929,7 +913,6 @@ mod tests { _ => unreachable!(), }), false, - None, ).unwrap(); assert!(status.changed); @@ -988,7 +971,7 @@ mod tests { // trying to finalize past `change_c` without finalizing `change_a` first assert!(matches!( - authorities.apply_standard_changes("hash_d", 40, &is_descendent_of, false, None), + authorities.apply_standard_changes("hash_d", 40, &is_descendent_of, false), Err(Error::ForkTree(fork_tree::Error::UnfinalizedAncestor)) )); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges::empty()); @@ -998,7 +981,6 @@ mod tests { 15, &is_descendent_of, false, - None, ).unwrap(); assert!(status.changed); @@ -1014,7 +996,6 @@ mod tests { 40, &is_descendent_of, false, - None, ).unwrap(); assert!(status.changed); @@ -1157,7 +1138,7 @@ mod tests { // too early and there's no forced changes to apply. assert!( authorities - .apply_forced_changes("hash_a10", 10, &static_is_descendent_of(true), false, None) + .apply_forced_changes("hash_a10", 10, &static_is_descendent_of(true), false) .unwrap() .is_none() ); @@ -1165,7 +1146,7 @@ mod tests { // too late. assert!( authorities - .apply_forced_changes("hash_a16", 16, &is_descendent_of_a, false, None) + .apply_forced_changes("hash_a16", 16, &is_descendent_of_a, false) .unwrap() .is_none() ); @@ -1173,7 +1154,7 @@ mod tests { // on time -- chooses the right change for this fork. assert_eq!( authorities - .apply_forced_changes("hash_a15", 15, &is_descendent_of_a, false, None) + .apply_forced_changes("hash_a15", 15, &is_descendent_of_a, false) .unwrap() .unwrap(), ( @@ -1221,7 +1202,7 @@ mod tests { // it should be enacted at the same block that signaled it assert!( authorities - .apply_forced_changes("hash_a", 5, &static_is_descendent_of(false), false, None) + .apply_forced_changes("hash_a", 5, &static_is_descendent_of(false), false) .unwrap() .is_some() ); @@ -1288,27 +1269,27 @@ mod tests { // the forced change cannot be applied since the pending changes it depends on // have not been applied yet. assert!(matches!( - authorities.apply_forced_changes("hash_d45", 45, &static_is_descendent_of(true), false, None), + authorities.apply_forced_changes("hash_d45", 45, &static_is_descendent_of(true), false), Err(Error::ForcedAuthoritySetChangeDependencyUnsatisfied(15)) )); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges::empty()); // we apply the first pending standard change at #15 authorities - .apply_standard_changes("hash_a15", 15, &static_is_descendent_of(true), false, None) + .apply_standard_changes("hash_a15", 15, &static_is_descendent_of(true), false) .unwrap(); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15)])); // but the forced change still depends on the next standard change assert!(matches!( - authorities.apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false, None), + authorities.apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false), Err(Error::ForcedAuthoritySetChangeDependencyUnsatisfied(20)) )); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15)])); // we apply the pending standard change at #20 authorities - .apply_standard_changes("hash_b", 20, &static_is_descendent_of(true), false, None) + .apply_standard_changes("hash_b", 20, &static_is_descendent_of(true), false) .unwrap(); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15), (1, 20)])); @@ -1317,7 +1298,7 @@ mod tests { // at #35. subsequent forced changes on the same branch must be kept assert_eq!( authorities - .apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false, None) + .apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false) .unwrap() .unwrap(), ( @@ -1414,7 +1395,7 @@ mod tests { // we apply the change at A0 which should prune it and the fork at B authorities - .apply_standard_changes("hash_a0", 5, &is_descendent_of, false, None) + .apply_standard_changes("hash_a0", 5, &is_descendent_of, false) .unwrap(); // the next change is now at A1 (#10) @@ -1602,14 +1583,14 @@ mod tests { // applying the standard change at A should not prune anything // other then the change that was applied authorities - .apply_standard_changes("A", 5, &is_descendent_of, false, None) + .apply_standard_changes("A", 5, &is_descendent_of, false) .unwrap(); assert_eq!(authorities.pending_changes().count(), 6); // same for B authorities - .apply_standard_changes("B", 10, &is_descendent_of, false, None) + .apply_standard_changes("B", 10, &is_descendent_of, false) .unwrap(); assert_eq!(authorities.pending_changes().count(), 5); @@ -1618,7 +1599,7 @@ mod tests { // finalizing C2 should clear all forced changes authorities - .apply_standard_changes("C2", 15, &is_descendent_of, false, None) + .apply_standard_changes("C2", 15, &is_descendent_of, false) .unwrap(); assert_eq!(authorities.pending_forced_changes.len(), 0); @@ -1626,7 +1607,7 @@ mod tests { // finalizing C0 should clear all forced changes but D let mut authorities = authorities2; authorities - .apply_standard_changes("C0", 15, &is_descendent_of, false, None) + .apply_standard_changes("C0", 15, &is_descendent_of, false) .unwrap(); assert_eq!(authorities.pending_forced_changes.len(), 1); diff --git a/client/finality-grandpa/src/aux_schema.rs b/client/finality-grandpa/src/aux_schema.rs index 43c45b9f10ae1..1ce3c7999f24c 100644 --- a/client/finality-grandpa/src/aux_schema.rs +++ b/client/finality-grandpa/src/aux_schema.rs @@ -137,7 +137,7 @@ struct V2AuthoritySet { } pub(crate) fn load_decode( - backend: &B, + backend: &B, key: &[u8] ) -> ClientResult> { match backend.get_aux(key)? { diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index a6c51f7eeee72..9f5582e5cea6b 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -90,7 +90,7 @@ use sc_network::{ObservedRole, PeerId, ReputationChange}; use parity_scale_codec::{Encode, Decode}; use sp_finality_grandpa::AuthorityId; -use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG}; +use sc_telemetry::{telemetry, CONSENSUS_DEBUG}; use log::{trace, debug}; use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; use prometheus_endpoint::{CounterVec, Opts, PrometheusError, register, Registry, U64}; @@ -744,7 +744,7 @@ impl Inner { fn note_set(&mut self, set_id: SetId, authorities: Vec) -> MaybeMessage { { let local_view = match self.local_view { - ref mut x @ None => x.get_or_insert(LocalView::new( + ref mut x @ None => x.get_or_insert(LocalView::new( set_id, Round(1), )), @@ -828,12 +828,7 @@ impl Inner { // ensure authority is part of the set. if !self.authorities.contains(&full.message.id) { debug!(target: "afg", "Message from unknown voter: {}", full.message.id); - telemetry!( - self.config.telemetry; - CONSENSUS_DEBUG; - "afg.bad_msg_signature"; - "signature" => ?full.message.id, - ); + telemetry!(CONSENSUS_DEBUG; "afg.bad_msg_signature"; "signature" => ?full.message.id); return Action::Discard(cost::UNKNOWN_VOTER); } @@ -845,12 +840,7 @@ impl Inner { full.set_id.0, ) { debug!(target: "afg", "Bad message signature {}", full.message.id); - telemetry!( - self.config.telemetry; - CONSENSUS_DEBUG; - "afg.bad_msg_signature"; - "signature" => ?full.message.id, - ); + telemetry!(CONSENSUS_DEBUG; "afg.bad_msg_signature"; "signature" => ?full.message.id); return Action::Discard(cost::BAD_SIGNATURE); } @@ -876,10 +866,7 @@ impl Inner { if full.message.precommits.len() != full.message.auth_data.len() || full.message.precommits.is_empty() { debug!(target: "afg", "Malformed compact commit"); - telemetry!( - self.config.telemetry; - CONSENSUS_DEBUG; - "afg.malformed_compact_commit"; + telemetry!(CONSENSUS_DEBUG; "afg.malformed_compact_commit"; "precommits_len" => ?full.message.precommits.len(), "auth_data_len" => ?full.message.auth_data.len(), "precommits_is_empty" => ?full.message.precommits.is_empty(), @@ -1290,7 +1277,6 @@ pub(super) struct GossipValidator { set_state: environment::SharedVoterSetState, report_sender: TracingUnboundedSender, metrics: Option, - telemetry: Option, } impl GossipValidator { @@ -1301,7 +1287,6 @@ impl GossipValidator { config: crate::Config, set_state: environment::SharedVoterSetState, prometheus_registry: Option<&Registry>, - telemetry: Option, ) -> (GossipValidator, TracingUnboundedReceiver) { let metrics = match prometheus_registry.map(Metrics::register) { Some(Ok(metrics)) => Some(metrics), @@ -1318,7 +1303,6 @@ impl GossipValidator { set_state, report_sender: tx, metrics, - telemetry, }; (val, rx) @@ -1427,12 +1411,7 @@ impl GossipValidator { Err(e) => { message_name = None; debug!(target: "afg", "Error decoding message: {}", e); - telemetry!( - self.telemetry; - CONSENSUS_DEBUG; - "afg.err_decoding_msg"; - "" => "", - ); + telemetry!(CONSENSUS_DEBUG; "afg.err_decoding_msg"; "" => ""); let len = std::cmp::min(i32::max_value() as usize, data.len()) as i32; Action::Discard(Misbehavior::UndecodablePacket(len).cost()) @@ -1651,7 +1630,6 @@ mod tests { name: None, is_authority: true, observer_enabled: true, - telemetry: None, } } @@ -1819,7 +1797,6 @@ mod tests { config(), voter_set_state(), None, - None, ); let set_id = 1; @@ -1856,7 +1833,6 @@ mod tests { config(), voter_set_state(), None, - None, ); let set_id = 1; let auth = AuthorityId::from_slice(&[1u8; 32]); @@ -1902,7 +1878,6 @@ mod tests { config(), voter_set_state(), None, - None, ); let set_id = 1; @@ -1972,7 +1947,6 @@ mod tests { config(), set_state.clone(), None, - None, ); let set_id = 1; @@ -2028,7 +2002,6 @@ mod tests { config(), set_state.clone(), None, - None, ); // the validator starts at set id 2 @@ -2109,7 +2082,6 @@ mod tests { config(), voter_set_state(), None, - None, ); // the validator starts at set id 1. @@ -2184,7 +2156,6 @@ mod tests { config, voter_set_state(), None, - None, ); // the validator starts at set id 1. @@ -2219,7 +2190,6 @@ mod tests { config(), voter_set_state(), None, - None, ); // the validator starts at set id 1. @@ -2280,7 +2250,6 @@ mod tests { config, voter_set_state(), None, - None, ); // the validator starts at set id 1. @@ -2320,7 +2289,6 @@ mod tests { config(), voter_set_state(), None, - None, ); // the validator starts at set id 1. @@ -2354,7 +2322,6 @@ mod tests { config, voter_set_state(), None, - None, ); // the validator start at set id 0 @@ -2434,7 +2401,6 @@ mod tests { config(), voter_set_state(), None, - None, ); // the validator start at set id 0 @@ -2475,7 +2441,6 @@ mod tests { config, voter_set_state(), None, - None, ); // the validator start at set id 0 @@ -2525,7 +2490,7 @@ mod tests { #[test] fn only_gossip_commits_to_peers_on_same_set() { - let (val, _) = GossipValidator::::new(config(), voter_set_state(), None, None); + let (val, _) = GossipValidator::::new(config(), voter_set_state(), None); // the validator start at set id 1 val.note_set(SetId(1), Vec::new(), |_, _| {}); @@ -2603,7 +2568,7 @@ mod tests { #[test] fn expire_commits_from_older_rounds() { - let (val, _) = GossipValidator::::new(config(), voter_set_state(), None, None); + let (val, _) = GossipValidator::::new(config(), voter_set_state(), None); let commit = |round, set_id, target_number| { let commit = finality_grandpa::CompactCommit { @@ -2654,7 +2619,7 @@ mod tests { #[test] fn allow_noting_different_authorities_for_same_set() { - let (val, _) = GossipValidator::::new(config(), voter_set_state(), None, None); + let (val, _) = GossipValidator::::new(config(), voter_set_state(), None); let a1 = vec![AuthorityId::from_slice(&[0; 32])]; val.note_set(SetId(1), a1.clone(), |_, _| {}); diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index 5953dbe3b6a9c..d502741465d23 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -42,7 +42,7 @@ use sc_network::{NetworkService, ReputationChange}; use sc_network_gossip::{GossipEngine, Network as GossipNetwork}; use parity_scale_codec::{Encode, Decode}; use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT, NumberFor}; -use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, CONSENSUS_DEBUG, CONSENSUS_INFO}; use crate::{ CatchUp, Commit, CommunicationIn, CommunicationOutH, @@ -192,8 +192,6 @@ pub(crate) struct NetworkBridge> { // just an `UnboundedReceiver`, one could also switch to a multi-producer-*multi*-consumer // channel implementation. gossip_validator_report_stream: Arc>>, - - telemetry: Option, } impl> Unpin for NetworkBridge {} @@ -208,13 +206,11 @@ impl> NetworkBridge { config: crate::Config, set_state: crate::environment::SharedVoterSetState, prometheus_registry: Option<&Registry>, - telemetry: Option, ) -> Self { let (validator, report_stream) = GossipValidator::new( config, set_state.clone(), prometheus_registry, - telemetry.clone(), ); let validator = Arc::new(validator); @@ -272,7 +268,6 @@ impl> NetworkBridge { neighbor_sender: neighbor_packet_sender, neighbor_packet_worker: Arc::new(Mutex::new(neighbor_packet_worker)), gossip_validator_report_stream: Arc::new(Mutex::new(report_stream)), - telemetry, } } @@ -325,7 +320,6 @@ impl> NetworkBridge { }); let topic = round_topic::(round.0, set_id.0); - let telemetry = self.telemetry.clone(); let incoming = self.gossip_engine.lock().messages_for(topic) .filter_map(move |notification| { let decoded = GossipMessage::::decode(&mut ¬ification.message[..]); @@ -345,30 +339,21 @@ impl> NetworkBridge { if voters.len().get() <= TELEMETRY_VOTERS_LIMIT { match &msg.message.message { PrimaryPropose(propose) => { - telemetry!( - telemetry; - CONSENSUS_INFO; - "afg.received_propose"; + telemetry!(CONSENSUS_INFO; "afg.received_propose"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?propose.target_number, "target_hash" => ?propose.target_hash, ); }, Prevote(prevote) => { - telemetry!( - telemetry; - CONSENSUS_INFO; - "afg.received_prevote"; + telemetry!(CONSENSUS_INFO; "afg.received_prevote"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?prevote.target_number, "target_hash" => ?prevote.target_hash, ); }, Precommit(precommit) => { - telemetry!( - telemetry; - CONSENSUS_INFO; - "afg.received_precommit"; + telemetry!(CONSENSUS_INFO; "afg.received_precommit"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?precommit.target_number, "target_hash" => ?precommit.target_hash, @@ -394,7 +379,6 @@ impl> NetworkBridge { network: self.gossip_engine.clone(), sender: tx, has_voted, - telemetry: self.telemetry.clone(), }; // Combine incoming votes from external GRANDPA nodes with outgoing @@ -428,7 +412,6 @@ impl> NetworkBridge { voters, self.validator.clone(), self.neighbor_sender.clone(), - self.telemetry.clone(), ); let outgoing = CommitsOut::::new( @@ -437,7 +420,6 @@ impl> NetworkBridge { is_voter, self.validator.clone(), self.neighbor_sender.clone(), - self.telemetry.clone(), ); let outgoing = outgoing.with(|out| { @@ -509,80 +491,72 @@ fn incoming_global( voters: Arc>, gossip_validator: Arc>, neighbor_sender: periodic::NeighborPacketSender, - telemetry: Option, ) -> impl Stream> { - let mut process_commit = { - let mut telemetry = telemetry.clone(); - move | - msg: FullCommitMessage, - mut notification: sc_network_gossip::TopicNotification, - gossip_engine: &Arc>>, - gossip_validator: &Arc>, - voters: &VoterSet, - | { - if voters.len().get() <= TELEMETRY_VOTERS_LIMIT { - let precommits_signed_by: Vec = - msg.message.auth_data.iter().map(move |(_, a)| { - format!("{}", a) - }).collect(); - - telemetry!( - telemetry; - CONSENSUS_INFO; - "afg.received_commit"; - "contains_precommits_signed_by" => ?precommits_signed_by, - "target_number" => ?msg.message.target_number.clone(), - "target_hash" => ?msg.message.target_hash.clone(), - ); - } - - if let Err(cost) = check_compact_commit::( - &msg.message, - voters, - msg.round, - msg.set_id, - telemetry.as_mut(), - ) { - if let Some(who) = notification.sender { - gossip_engine.lock().report(who, cost); - } + let process_commit = move | + msg: FullCommitMessage, + mut notification: sc_network_gossip::TopicNotification, + gossip_engine: &Arc>>, + gossip_validator: &Arc>, + voters: &VoterSet, + | { + if voters.len().get() <= TELEMETRY_VOTERS_LIMIT { + let precommits_signed_by: Vec = + msg.message.auth_data.iter().map(move |(_, a)| { + format!("{}", a) + }).collect(); + + telemetry!(CONSENSUS_INFO; "afg.received_commit"; + "contains_precommits_signed_by" => ?precommits_signed_by, + "target_number" => ?msg.message.target_number.clone(), + "target_hash" => ?msg.message.target_hash.clone(), + ); + } - return None; + if let Err(cost) = check_compact_commit::( + &msg.message, + voters, + msg.round, + msg.set_id, + ) { + if let Some(who) = notification.sender { + gossip_engine.lock().report(who, cost); } - let round = msg.round; - let set_id = msg.set_id; - let commit = msg.message; - let finalized_number = commit.target_number; - let gossip_validator = gossip_validator.clone(); - let gossip_engine = gossip_engine.clone(); - let neighbor_sender = neighbor_sender.clone(); - let cb = move |outcome| match outcome { - voter::CommitProcessingOutcome::Good(_) => { - // if it checks out, gossip it. not accounting for - // any discrepancy between the actual ghost and the claimed - // finalized number. - gossip_validator.note_commit_finalized( - round, - set_id, - finalized_number, - |to, neighbor| neighbor_sender.send(to, neighbor), - ); + return None; + } - gossip_engine.lock().gossip_message(topic, notification.message.clone(), false); - } - voter::CommitProcessingOutcome::Bad(_) => { - // report peer and do not gossip. - if let Some(who) = notification.sender.take() { - gossip_engine.lock().report(who, cost::INVALID_COMMIT); - } + let round = msg.round; + let set_id = msg.set_id; + let commit = msg.message; + let finalized_number = commit.target_number; + let gossip_validator = gossip_validator.clone(); + let gossip_engine = gossip_engine.clone(); + let neighbor_sender = neighbor_sender.clone(); + let cb = move |outcome| match outcome { + voter::CommitProcessingOutcome::Good(_) => { + // if it checks out, gossip it. not accounting for + // any discrepancy between the actual ghost and the claimed + // finalized number. + gossip_validator.note_commit_finalized( + round, + set_id, + finalized_number, + |to, neighbor| neighbor_sender.send(to, neighbor), + ); + + gossip_engine.lock().gossip_message(topic, notification.message.clone(), false); + } + voter::CommitProcessingOutcome::Bad(_) => { + // report peer and do not gossip. + if let Some(who) = notification.sender.take() { + gossip_engine.lock().report(who, cost::INVALID_COMMIT); } - }; + } + }; - let cb = voter::Callback::Work(Box::new(cb)); + let cb = voter::Callback::Work(Box::new(cb)); - Some(voter::CommunicationIn::Commit(round.0, commit, cb)) - } + Some(voter::CommunicationIn::Commit(round.0, commit, cb)) }; let process_catch_up = move | @@ -599,7 +573,6 @@ fn incoming_global( &msg.message, voters, msg.set_id, - telemetry.clone(), ) { if let Some(who) = notification.sender { gossip_engine.lock().report(who, cost); @@ -656,7 +629,6 @@ impl> Clone for NetworkBridge { neighbor_sender: self.neighbor_sender.clone(), neighbor_packet_worker: self.neighbor_packet_worker.clone(), gossip_validator_report_stream: self.gossip_validator_report_stream.clone(), - telemetry: self.telemetry.clone(), } } } @@ -683,7 +655,6 @@ pub(crate) struct OutgoingMessages { sender: mpsc::Sender>, network: Arc>>, has_voted: HasVoted, - telemetry: Option, } impl Unpin for OutgoingMessages {} @@ -746,9 +717,7 @@ impl Sink> for OutgoingMessages ); telemetry!( - self.telemetry; - CONSENSUS_DEBUG; - "afg.announcing_blocks_to_voted_peers"; + CONSENSUS_DEBUG; "afg.announcing_blocks_to_voted_peers"; "block" => ?target_hash, "round" => ?self.round, "set_id" => ?self.set_id, ); @@ -787,7 +756,6 @@ fn check_compact_commit( voters: &VoterSet, round: Round, set_id: SetId, - telemetry: Option<&mut TelemetryHandle>, ) -> Result<(), ReputationChange> { // 4f + 1 = equivocations from f voters. let f = voters.total_weight() - voters.threshold(); @@ -829,12 +797,7 @@ fn check_compact_commit( &mut buf, ) { debug!(target: "afg", "Bad commit message signature {}", id); - telemetry!( - telemetry; - CONSENSUS_DEBUG; - "afg.bad_commit_msg_signature"; - "id" => ?id, - ); + telemetry!(CONSENSUS_DEBUG; "afg.bad_commit_msg_signature"; "id" => ?id); let cost = Misbehavior::BadCommitMessage { signatures_checked: i as i32, blocks_loaded: 0, @@ -854,7 +817,6 @@ fn check_catch_up( msg: &CatchUp, voters: &VoterSet, set_id: SetId, - telemetry: Option, ) -> Result<(), ReputationChange> { // 4f + 1 = equivocations from f voters. let f = voters.total_weight() - voters.threshold(); @@ -905,7 +867,6 @@ fn check_catch_up( set_id: SetIdNumber, mut signatures_checked: usize, buf: &mut Vec, - telemetry: Option, ) -> Result where B: BlockT, I: Iterator, &'a AuthorityId, &'a AuthoritySignature)>, @@ -924,12 +885,7 @@ fn check_catch_up( buf, ) { debug!(target: "afg", "Bad catch up message signature {}", id); - telemetry!( - telemetry; - CONSENSUS_DEBUG; - "afg.bad_catch_up_msg_signature"; - "id" => ?id, - ); + telemetry!(CONSENSUS_DEBUG; "afg.bad_catch_up_msg_signature"; "id" => ?id); let cost = Misbehavior::BadCatchUpMessage { signatures_checked: signatures_checked as i32, @@ -953,7 +909,6 @@ fn check_catch_up( set_id.0, 0, &mut buf, - telemetry.clone(), )?; // check signatures on all contained precommits. @@ -965,7 +920,6 @@ fn check_catch_up( set_id.0, signatures_checked, &mut buf, - telemetry, )?; Ok(()) @@ -978,7 +932,6 @@ struct CommitsOut { is_voter: bool, gossip_validator: Arc>, neighbor_sender: periodic::NeighborPacketSender, - telemetry: Option, } impl CommitsOut { @@ -989,7 +942,6 @@ impl CommitsOut { is_voter: bool, gossip_validator: Arc>, neighbor_sender: periodic::NeighborPacketSender, - telemetry: Option, ) -> Self { CommitsOut { network, @@ -997,7 +949,6 @@ impl CommitsOut { is_voter, gossip_validator, neighbor_sender, - telemetry, } } } @@ -1017,12 +968,8 @@ impl Sink<(RoundNumber, Commit)> for CommitsOut { let (round, commit) = input; let round = Round(round); - telemetry!( - self.telemetry; - CONSENSUS_DEBUG; - "afg.commit_issued"; - "target_number" => ?commit.target_number, - "target_hash" => ?commit.target_hash, + telemetry!(CONSENSUS_DEBUG; "afg.commit_issued"; + "target_number" => ?commit.target_number, "target_hash" => ?commit.target_hash, ); let (precommits, auth_data) = commit.precommits.into_iter() .map(|signed| (signed.precommit, (signed.signature, signed.id))) diff --git a/client/finality-grandpa/src/communication/tests.rs b/client/finality-grandpa/src/communication/tests.rs index dc37a1615f415..4abea991cec37 100644 --- a/client/finality-grandpa/src/communication/tests.rs +++ b/client/finality-grandpa/src/communication/tests.rs @@ -139,7 +139,6 @@ fn config() -> crate::Config { name: None, is_authority: true, observer_enabled: true, - telemetry: None, } } @@ -190,7 +189,6 @@ pub(crate) fn make_test_network() -> ( config(), voter_set_state(), None, - None, ); ( diff --git a/client/finality-grandpa/src/environment.rs b/client/finality-grandpa/src/environment.rs index bdba8a970145a..7925a674c2983 100644 --- a/client/finality-grandpa/src/environment.rs +++ b/client/finality-grandpa/src/environment.rs @@ -39,7 +39,7 @@ use sp_runtime::generic::BlockId; use sp_runtime::traits::{ Block as BlockT, Header as HeaderT, NumberFor, Zero, }; -use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, CONSENSUS_DEBUG, CONSENSUS_INFO}; use crate::{ local_authority_id, CommandOrError, Commit, Config, Error, NewAuthoritySet, Precommit, Prevote, @@ -445,7 +445,6 @@ pub(crate) struct Environment, SC, pub(crate) voting_rule: VR, pub(crate) metrics: Option, pub(crate) justification_sender: Option>, - pub(crate) telemetry: Option, pub(crate) _phantom: PhantomData, } @@ -891,12 +890,8 @@ where None => return Ok(()), }; - let telemetry = self.telemetry.clone(); - let report_prevote_metrics = move |prevote: &Prevote| { - telemetry!( - telemetry; - CONSENSUS_DEBUG; - "afg.prevote_issued"; + let report_prevote_metrics = |prevote: &Prevote| { + telemetry!(CONSENSUS_DEBUG; "afg.prevote_issued"; "round" => round, "target_number" => ?prevote.target_number, "target_hash" => ?prevote.target_hash, @@ -954,12 +949,8 @@ where None => return Ok(()), }; - let telemetry = self.telemetry.clone(); - let report_precommit_metrics = move |precommit: &Precommit| { - telemetry!( - telemetry; - CONSENSUS_DEBUG; - "afg.precommit_issued"; + let report_precommit_metrics = |precommit: &Precommit| { + telemetry!(CONSENSUS_DEBUG; "afg.precommit_issued"; "round" => round, "target_number" => ?precommit.target_number, "target_hash" => ?precommit.target_hash, @@ -1155,7 +1146,6 @@ where (round, commit).into(), false, self.justification_sender.as_ref(), - self.telemetry.clone(), ) } @@ -1220,7 +1210,6 @@ pub(crate) fn finalize_block( justification_or_commit: JustificationOrCommit, initial_sync: bool, justification_sender: Option<&GrandpaJustificationSender>, - telemetry: Option, ) -> Result<(), CommandOrError>> where Block: BlockT, @@ -1250,154 +1239,138 @@ where // FIXME #1483: clone only when changed let old_authority_set = authority_set.clone(); - let update_res: Result<_, Error> = client.lock_import_and_run({ - let mut telemetry = telemetry.clone(); - let mut authority_set = authority_set.clone(); - let client = client.clone(); - move |import_op| { - let status = authority_set.apply_standard_changes( - hash, - number, - &is_descendent_of::(&*client, None), - initial_sync, - telemetry.as_mut(), - ).map_err(|e| Error::Safety(e.to_string()))?; - - // send a justification notification if a sender exists and in case of error log it. - fn notify_justification( - justification_sender: Option<&GrandpaJustificationSender>, - justification: impl FnOnce() -> Result, Error>, - ) { - if let Some(sender) = justification_sender { - if let Err(err) = sender.notify(justification) { - warn!( - target: "afg", - "Error creating justification for subscriber: {:?}", - err, - ); - } + let update_res: Result<_, Error> = client.lock_import_and_run(|import_op| { + let status = authority_set.apply_standard_changes( + hash, + number, + &is_descendent_of::(&*client, None), + initial_sync, + ).map_err(|e| Error::Safety(e.to_string()))?; + + // send a justification notification if a sender exists and in case of error log it. + fn notify_justification( + justification_sender: Option<&GrandpaJustificationSender>, + justification: impl FnOnce() -> Result, Error>, + ) { + if let Some(sender) = justification_sender { + if let Err(err) = sender.notify(justification) { + warn!(target: "afg", "Error creating justification for subscriber: {:?}", err); } } + } - // NOTE: this code assumes that honest voters will never vote past a - // transition block, thus we don't have to worry about the case where - // we have a transition with `effective_block = N`, but we finalize - // `N+1`. this assumption is required to make sure we store - // justifications for transition blocks which will be requested by - // syncing clients. - let justification = match justification_or_commit { - JustificationOrCommit::Justification(justification) => { - notify_justification(justification_sender, || Ok(justification.clone())); - Some(justification.encode()) - }, - JustificationOrCommit::Commit((round_number, commit)) => { - let mut justification_required = - // justification is always required when block that enacts new authorities - // set is finalized - status.new_set_block.is_some(); - - // justification is required every N blocks to be able to prove blocks - // finalization to remote nodes - if !justification_required { - if let Some(justification_period) = justification_period { - let last_finalized_number = client.info().finalized_number; - justification_required = - (!last_finalized_number.is_zero() || number - last_finalized_number == justification_period) && - (last_finalized_number / justification_period != number / justification_period); - } - } - - // NOTE: the code below is a bit more verbose because we - // really want to avoid creating a justification if it isn't - // needed (e.g. if there's no subscribers), and also to avoid - // creating it twice. depending on the vote tree for the round, - // creating a justification might require multiple fetches of - // headers from the database. - let justification = || GrandpaJustification::from_commit( - &client, - round_number, - commit, - ); - - if justification_required { - let justification = justification()?; - notify_justification(justification_sender, || Ok(justification.clone())); - - Some(justification.encode()) - } else { - notify_justification(justification_sender, justification); - - None + // NOTE: this code assumes that honest voters will never vote past a + // transition block, thus we don't have to worry about the case where + // we have a transition with `effective_block = N`, but we finalize + // `N+1`. this assumption is required to make sure we store + // justifications for transition blocks which will be requested by + // syncing clients. + let justification = match justification_or_commit { + JustificationOrCommit::Justification(justification) => { + notify_justification(justification_sender, || Ok(justification.clone())); + Some(justification.encode()) + }, + JustificationOrCommit::Commit((round_number, commit)) => { + let mut justification_required = + // justification is always required when block that enacts new authorities + // set is finalized + status.new_set_block.is_some(); + + // justification is required every N blocks to be able to prove blocks + // finalization to remote nodes + if !justification_required { + if let Some(justification_period) = justification_period { + let last_finalized_number = client.info().finalized_number; + justification_required = + (!last_finalized_number.is_zero() || number - last_finalized_number == justification_period) && + (last_finalized_number / justification_period != number / justification_period); } - }, - }; + } - debug!(target: "afg", "Finalizing blocks up to ({:?}, {})", number, hash); - - // ideally some handle to a synchronization oracle would be used - // to avoid unconditionally notifying. - client.apply_finality(import_op, BlockId::Hash(hash), justification, true).map_err(|e| { - warn!(target: "afg", "Error applying finality to block {:?}: {:?}", (hash, number), e); - e - })?; - telemetry!( - telemetry; - CONSENSUS_INFO; - "afg.finalized_blocks_up_to"; - "number" => ?number, "hash" => ?hash, - ); + // NOTE: the code below is a bit more verbose because we + // really want to avoid creating a justification if it isn't + // needed (e.g. if there's no subscribers), and also to avoid + // creating it twice. depending on the vote tree for the round, + // creating a justification might require multiple fetches of + // headers from the database. + let justification = || GrandpaJustification::from_commit( + &client, + round_number, + commit, + ); - let new_authorities = if let Some((canon_hash, canon_number)) = status.new_set_block { - // the authority set has changed. - let (new_id, set_ref) = authority_set.current(); + if justification_required { + let justification = justification()?; + notify_justification(justification_sender, || Ok(justification.clone())); - if set_ref.len() > 16 { - afg_log!(initial_sync, - "👴 Applying GRANDPA set change to new set with {} authorities", - set_ref.len(), - ); + Some(justification.encode()) } else { - afg_log!(initial_sync, - "👴 Applying GRANDPA set change to new set {:?}", - set_ref, - ); + notify_justification(justification_sender, justification); + + None } + }, + }; - telemetry!( - telemetry; - CONSENSUS_INFO; - "afg.generating_new_authority_set"; - "number" => ?canon_number, "hash" => ?canon_hash, - "authorities" => ?set_ref.to_vec(), - "set_id" => ?new_id, + debug!(target: "afg", "Finalizing blocks up to ({:?}, {})", number, hash); + + // ideally some handle to a synchronization oracle would be used + // to avoid unconditionally notifying. + client.apply_finality(import_op, BlockId::Hash(hash), justification, true).map_err(|e| { + warn!(target: "afg", "Error applying finality to block {:?}: {:?}", (hash, number), e); + e + })?; + telemetry!(CONSENSUS_INFO; "afg.finalized_blocks_up_to"; + "number" => ?number, "hash" => ?hash, + ); + + let new_authorities = if let Some((canon_hash, canon_number)) = status.new_set_block { + // the authority set has changed. + let (new_id, set_ref) = authority_set.current(); + + if set_ref.len() > 16 { + afg_log!(initial_sync, + "👴 Applying GRANDPA set change to new set with {} authorities", + set_ref.len(), ); - Some(NewAuthoritySet { - canon_hash, - canon_number, - set_id: new_id, - authorities: set_ref.to_vec(), - }) } else { - None - }; - - if status.changed { - let write_result = crate::aux_schema::update_authority_set::( - &authority_set, - new_authorities.as_ref(), - |insert| apply_aux(import_op, insert, &[]), + afg_log!(initial_sync, + "👴 Applying GRANDPA set change to new set {:?}", + set_ref, ); + } - if let Err(e) = write_result { - warn!(target: "afg", "Failed to write updated authority set to disk. Bailing."); - warn!(target: "afg", "Node is in a potentially inconsistent state."); + telemetry!(CONSENSUS_INFO; "afg.generating_new_authority_set"; + "number" => ?canon_number, "hash" => ?canon_hash, + "authorities" => ?set_ref.to_vec(), + "set_id" => ?new_id, + ); + Some(NewAuthoritySet { + canon_hash, + canon_number, + set_id: new_id, + authorities: set_ref.to_vec(), + }) + } else { + None + }; - return Err(e.into()); - } - } + if status.changed { + let write_result = crate::aux_schema::update_authority_set::( + &authority_set, + new_authorities.as_ref(), + |insert| apply_aux(import_op, insert, &[]), + ); + + if let Err(e) = write_result { + warn!(target: "afg", "Failed to write updated authority set to disk. Bailing."); + warn!(target: "afg", "Node is in a potentially inconsistent state."); - Ok(new_authorities.map(VoterCommand::ChangeAuthorities)) + return Err(e.into()); + } } + + Ok(new_authorities.map(VoterCommand::ChangeAuthorities)) }); match update_res { diff --git a/client/finality-grandpa/src/finality_proof.rs b/client/finality-grandpa/src/finality_proof.rs index b79b3190739d6..c88faa2498928 100644 --- a/client/finality-grandpa/src/finality_proof.rs +++ b/client/finality-grandpa/src/finality_proof.rs @@ -247,6 +247,9 @@ where .map_err(|_| ClientError::JustificationDecode)?; justification.verify(current_set_id, ¤t_authorities)?; + use sc_telemetry::{telemetry, CONSENSUS_INFO}; + telemetry!(CONSENSUS_INFO; "afg.finality_proof_ok"; + "finalized_header_hash" => ?proof.block); Ok(proof) } diff --git a/client/finality-grandpa/src/import.rs b/client/finality-grandpa/src/import.rs index 22d7b7fd5bcc8..2c86319dd54af 100644 --- a/client/finality-grandpa/src/import.rs +++ b/client/finality-grandpa/src/import.rs @@ -24,7 +24,6 @@ use parking_lot::RwLockWriteGuard; use sp_blockchain::{BlockStatus, well_known_cache_keys}; use sc_client_api::{backend::Backend, utils::is_descendent_of}; -use sc_telemetry::TelemetryHandle; use sp_utils::mpsc::TracingUnboundedSender; use sp_api::TransactionFor; @@ -63,7 +62,6 @@ pub struct GrandpaBlockImport { send_voter_commands: TracingUnboundedSender>>, authority_set_hard_forks: HashMap>>, justification_sender: GrandpaJustificationSender, - telemetry: Option, _phantom: PhantomData, } @@ -78,7 +76,6 @@ impl Clone for send_voter_commands: self.send_voter_commands.clone(), authority_set_hard_forks: self.authority_set_hard_forks.clone(), justification_sender: self.justification_sender.clone(), - telemetry: self.telemetry.clone(), _phantom: PhantomData, } } @@ -341,13 +338,7 @@ where let applied_changes = { let forced_change_set = guard .as_mut() - .apply_forced_changes( - hash, - number, - &is_descendent_of, - initial_sync, - self.telemetry.clone(), - ) + .apply_forced_changes(hash, number, &is_descendent_of, initial_sync) .map_err(|e| ConsensusError::ClientImport(e.to_string())) .map_err(ConsensusError::from)?; @@ -364,11 +355,8 @@ where let canon_hash = self.inner.header(BlockId::Number(canon_number)) .map_err(|e| ConsensusError::ClientImport(e.to_string()))? - .expect( - "the given block number is less or equal than the current best - finalized number; current best finalized number must exist in - chain; qed." - ) + .expect("the given block number is less or equal than the current best finalized number; \ + current best finalized number must exist in chain; qed.") .hash(); NewAuthoritySet { @@ -569,7 +557,6 @@ impl GrandpaBlockImport>>, authority_set_hard_forks: Vec<(SetId, PendingChange>)>, justification_sender: GrandpaJustificationSender, - telemetry: Option, ) -> GrandpaBlockImport { // check for and apply any forced authority set hard fork that applies // to the *current* authority set. @@ -613,7 +600,6 @@ impl GrandpaBlockImport, /// The keystore that manages the keys of this node. pub keystore: Option, - /// TelemetryHandle instance. - pub telemetry: Option, } impl Config { @@ -453,7 +451,6 @@ pub struct LinkHalf { voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: GrandpaJustificationSender, justification_stream: GrandpaJustificationStream, - telemetry: Option, } impl LinkHalf { @@ -504,7 +501,6 @@ pub fn block_import( client: Arc, genesis_authorities_provider: &dyn GenesisAuthoritySetProvider, select_chain: SC, - telemetry: Option, ) -> Result< ( GrandpaBlockImport, @@ -522,7 +518,6 @@ where genesis_authorities_provider, select_chain, Default::default(), - telemetry, ) } @@ -536,7 +531,6 @@ pub fn block_import_with_authority_set_hard_forks genesis_authorities_provider: &dyn GenesisAuthoritySetProvider, select_chain: SC, authority_set_hard_forks: Vec<(SetId, (Block::Hash, NumberFor), AuthorityList)>, - telemetry: Option, ) -> Result< ( GrandpaBlockImport, @@ -556,19 +550,13 @@ where &*client, genesis_hash, >::zero(), - { - let telemetry = telemetry.clone(); - move || { - let authorities = genesis_authorities_provider.get()?; - telemetry!( - telemetry; - CONSENSUS_DEBUG; - "afg.loading_authorities"; - "authorities_len" => ?authorities.len() - ); - Ok(authorities) - } - }, + || { + let authorities = genesis_authorities_provider.get()?; + telemetry!(CONSENSUS_DEBUG; "afg.loading_authorities"; + "authorities_len" => ?authorities.len() + ); + Ok(authorities) + } )?; let (voter_commands_tx, voter_commands_rx) = tracing_unbounded("mpsc_grandpa_voter_command"); @@ -602,7 +590,6 @@ where voter_commands_tx, authority_set_hard_forks, justification_sender.clone(), - telemetry.clone(), ), LinkHalf { client, @@ -611,7 +598,6 @@ where voter_commands_rx, justification_sender, justification_stream, - telemetry, }, )) } @@ -674,14 +660,14 @@ pub struct GrandpaParams { /// `sc_network` crate, it is assumed that the Grandpa notifications protocol has been passed /// to the configuration of the networking. See [`grandpa_peers_set_config`]. pub network: N, + /// If supplied, can be used to hook on telemetry connection established events. + pub telemetry_on_connect: Option>, /// A voting rule used to potentially restrict target votes. pub voting_rule: VR, /// The prometheus metrics registry. pub prometheus_registry: Option, /// The voter state is exposed at an RPC endpoint. pub shared_voter_state: SharedVoterState, - /// TelemetryHandle instance. - pub telemetry: Option, } /// Returns the configuration value to put in @@ -720,10 +706,10 @@ where mut config, link, network, + telemetry_on_connect, voting_rule, prometheus_registry, shared_voter_state, - telemetry, } = grandpa_params; // NOTE: we have recently removed `run_grandpa_observer` from the public @@ -739,7 +725,6 @@ where voter_commands_rx, justification_sender, justification_stream: _, - telemetry: _, } = link; let network = NetworkBridge::new( @@ -747,16 +732,11 @@ where config.clone(), persistent_data.set_state.clone(), prometheus_registry.as_ref(), - telemetry.clone(), ); let conf = config.clone(); - let telemetry_task = if let Some(telemetry_on_connect) = telemetry - .as_ref() - .map(|x| x.on_connect_stream()) - { + let telemetry_task = if let Some(telemetry_on_connect) = telemetry_on_connect { let authorities = persistent_data.authority_set.clone(); - let telemetry = telemetry.clone(); let events = telemetry_on_connect .for_each(move |_| { let current_authorities = authorities.current_authorities(); @@ -771,13 +751,10 @@ where let authorities = serde_json::to_string(&authorities).expect( "authorities is always at least an empty vector; \ - elements are always of type string", + elements are always of type string", ); - telemetry!( - telemetry; - CONSENSUS_INFO; - "afg.authority_set"; + telemetry!(CONSENSUS_INFO; "afg.authority_set"; "authority_id" => authority_id.to_string(), "authority_set_id" => ?set_id, "authorities" => authorities, @@ -801,7 +778,6 @@ where prometheus_registry, shared_voter_state, justification_sender, - telemetry, ); let voter_work = voter_work.map(|res| match res { @@ -840,7 +816,7 @@ struct VoterWork, SC, VR> { env: Arc>, voter_commands_rx: TracingUnboundedReceiver>>, network: NetworkBridge, - telemetry: Option, + /// Prometheus metrics. metrics: Option, } @@ -867,7 +843,6 @@ where prometheus_registry: Option, shared_voter_state: SharedVoterState, justification_sender: GrandpaJustificationSender, - telemetry: Option, ) -> Self { let metrics = match prometheus_registry.as_ref().map(Metrics::register) { Some(Ok(metrics)) => Some(metrics), @@ -891,7 +866,6 @@ where voter_set_state: persistent_data.set_state, metrics: metrics.as_ref().map(|m| m.environment.clone()), justification_sender: Some(justification_sender), - telemetry: telemetry.clone(), _phantom: PhantomData, }); @@ -903,7 +877,6 @@ where env, voter_commands_rx, network, - telemetry, metrics, }; work.rebuild_voter(); @@ -919,10 +892,7 @@ where let authority_id = local_authority_id(&self.env.voters, self.env.config.keystore.as_ref()) .unwrap_or_default(); - telemetry!( - self.telemetry; - CONSENSUS_DEBUG; - "afg.starting_new_voter"; + telemetry!(CONSENSUS_DEBUG; "afg.starting_new_voter"; "name" => ?self.env.config.name(), "set_id" => ?self.env.set_id, "authority_id" => authority_id.to_string(), @@ -941,10 +911,7 @@ where "authorities is always at least an empty vector; elements are always of type string", ); - telemetry!( - self.telemetry; - CONSENSUS_INFO; - "afg.authority_set"; + telemetry!(CONSENSUS_INFO; "afg.authority_set"; "number" => ?chain_info.finalized_number, "hash" => ?chain_info.finalized_hash, "authority_id" => authority_id.to_string(), @@ -1004,10 +971,7 @@ where let voters: Vec = new.authorities.iter().map(move |(a, _)| { format!("{}", a) }).collect(); - telemetry!( - self.telemetry; - CONSENSUS_INFO; - "afg.voter_command_change_authorities"; + telemetry!(CONSENSUS_INFO; "afg.voter_command_change_authorities"; "number" => ?new.canon_number, "hash" => ?new.canon_hash, "voters" => ?voters, @@ -1028,11 +992,10 @@ where })?; let voters = Arc::new(VoterSet::new(new.authorities.into_iter()) - .expect( - "new authorities come from pending change; \ - pending change comes from `AuthoritySet`; \ - `AuthoritySet` validates authorities is non-empty and weights are non-zero; \ - qed." + .expect("new authorities come from pending change; \ + pending change comes from `AuthoritySet`; \ + `AuthoritySet` validates authorities is non-empty and weights are non-zero; \ + qed." ) ); @@ -1048,7 +1011,6 @@ where voting_rule: self.env.voting_rule.clone(), metrics: self.env.metrics.clone(), justification_sender: self.env.justification_sender.clone(), - telemetry: self.telemetry.clone(), _phantom: PhantomData, }); diff --git a/client/finality-grandpa/src/observer.rs b/client/finality-grandpa/src/observer.rs index c0eab15e4f455..3054a9df61c56 100644 --- a/client/finality-grandpa/src/observer.rs +++ b/client/finality-grandpa/src/observer.rs @@ -29,7 +29,6 @@ use log::{debug, info, warn}; use sp_keystore::SyncCryptoStorePtr; use sp_consensus::SelectChain; use sc_client_api::backend::Backend; -use sc_telemetry::TelemetryHandle; use sp_utils::mpsc::TracingUnboundedReceiver; use sp_runtime::traits::{NumberFor, Block as BlockT}; use sp_blockchain::HeaderMetadata; @@ -68,7 +67,6 @@ fn grandpa_observer( last_finalized_number: NumberFor, commits: S, note_round: F, - telemetry: Option, ) -> impl Future>>> where NumberFor: BlockNumberOps, @@ -123,7 +121,6 @@ where (round, commit).into(), false, justification_sender.as_ref(), - telemetry.clone(), ) { Ok(_) => {}, Err(e) => return future::err(e), @@ -175,8 +172,7 @@ where persistent_data, voter_commands_rx, justification_sender, - justification_stream: _, - telemetry, + .. } = link; let network = NetworkBridge::new( @@ -184,17 +180,15 @@ where config.clone(), persistent_data.set_state.clone(), None, - telemetry.clone(), ); let observer_work = ObserverWork::new( - client.clone(), + client, network, persistent_data, config.keystore, voter_commands_rx, Some(justification_sender), - telemetry.clone(), ); let observer_work = observer_work @@ -216,7 +210,6 @@ struct ObserverWork> { keystore: Option, voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: Option>, - telemetry: Option, _phantom: PhantomData, } @@ -235,7 +228,6 @@ where keystore: Option, voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: Option>, - telemetry: Option, ) -> Self { let mut work = ObserverWork { @@ -248,7 +240,6 @@ where keystore: keystore.clone(), voter_commands_rx, justification_sender, - telemetry, _phantom: PhantomData, }; work.rebuild_observer(); @@ -298,7 +289,6 @@ where last_finalized_number, global_in, note_round, - self.telemetry.clone(), ); self.observer = Box::pin(observer); @@ -439,7 +429,6 @@ mod tests { None, voter_command_rx, None, - None, ); // Trigger a reputation change through the gossip validator. diff --git a/client/finality-grandpa/src/tests.rs b/client/finality-grandpa/src/tests.rs index 6824a8ed04273..921b49db61c25 100644 --- a/client/finality-grandpa/src/tests.rs +++ b/client/finality-grandpa/src/tests.rs @@ -120,7 +120,6 @@ impl TestNetFactory for GrandpaTestNet { client.clone(), &self.test_config, LongestChain::new(backend.clone()), - None, ).expect("Could not create block import for fresh peer."); let justification_import = Box::new(import.clone()); ( @@ -253,14 +252,13 @@ fn initialize_grandpa( name: Some(format!("peer#{}", peer_id)), is_authority: true, observer_enabled: true, - telemetry: None, }, link, network: net_service, + telemetry_on_connect: None, voting_rule: (), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), - telemetry: None, }; let voter = run_grandpa_voter(grandpa_params).expect("all in order with client and network"); @@ -397,14 +395,13 @@ fn finalize_3_voters_1_full_observer() { name: Some(format!("peer#{}", peer_id)), is_authority: true, observer_enabled: true, - telemetry: None, }, link: link, network: net_service, + telemetry_on_connect: None, voting_rule: (), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), - telemetry: None, }; run_grandpa_voter(grandpa_params).expect("all in order with client and network") @@ -491,14 +488,13 @@ fn transition_3_voters_twice_1_full_observer() { name: Some(format!("peer#{}", peer_id)), is_authority: true, observer_enabled: true, - telemetry: None, }, link, network: net_service, + telemetry_on_connect: None, voting_rule: (), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), - telemetry: None, }; voters.push(run_grandpa_voter(grandpa_params).expect("all in order with client and network")); @@ -925,7 +921,6 @@ fn voter_persists_its_votes() { name: Some(format!("peer#{}", 1)), is_authority: true, observer_enabled: true, - telemetry: None, }; let set_state = { @@ -944,7 +939,6 @@ fn voter_persists_its_votes() { config.clone(), set_state, None, - None, ) }; @@ -970,14 +964,13 @@ fn voter_persists_its_votes() { name: Some(format!("peer#{}", 0)), is_authority: true, observer_enabled: true, - telemetry: None, }, link, network: net_service, + telemetry_on_connect: None, voting_rule: VotingRulesBuilder::default().build(), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), - telemetry: None, }; run_grandpa_voter(grandpa_params).expect("all in order with client and network") @@ -1013,14 +1006,13 @@ fn voter_persists_its_votes() { name: Some(format!("peer#{}", 0)), is_authority: true, observer_enabled: true, - telemetry: None, }, link, network: net_service, + telemetry_on_connect: None, voting_rule: VotingRulesBuilder::default().build(), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), - telemetry: None, }; run_grandpa_voter(grandpa_params) @@ -1173,7 +1165,6 @@ fn finalize_3_voters_1_light_observer() { name: Some("observer".to_string()), is_authority: false, observer_enabled: true, - telemetry: None, }, net.peers[3].data.lock().take().expect("link initialized at startup; qed"), net.peers[3].network_service().clone(), @@ -1215,14 +1206,13 @@ fn voter_catches_up_to_latest_round_when_behind() { name: Some(format!("peer#{}", peer_id)), is_authority: true, observer_enabled: true, - telemetry: None, }, link, network: net.lock().peer(peer_id).network_service().clone(), + telemetry_on_connect: None, voting_rule: (), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), - telemetry: None, }; Box::pin(run_grandpa_voter(grandpa_params).expect("all in order with client and network")) @@ -1338,7 +1328,6 @@ where name: None, is_authority: true, observer_enabled: true, - telemetry: None, }; let network = NetworkBridge::new( @@ -1346,7 +1335,6 @@ where config.clone(), set_state.clone(), None, - None, ); Environment { @@ -1361,7 +1349,6 @@ where voting_rule, metrics: None, justification_sender: None, - telemetry: None, _phantom: PhantomData, } } From 560bb316300de54fd1b0469fb410455c46b95653 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 15:12:57 +0100 Subject: [PATCH 79/92] WIP --- client/finality-grandpa/src/authorities.rs | 4 ++-- .../finality-grandpa/src/communication/gossip.rs | 8 ++++---- client/finality-grandpa/src/communication/mod.rs | 16 ++++++++-------- client/finality-grandpa/src/environment.rs | 8 ++++---- client/finality-grandpa/src/finality_proof.rs | 3 --- client/finality-grandpa/src/lib.rs | 16 +++++++++++----- client/telemetry/src/lib.rs | 5 +++-- 7 files changed, 32 insertions(+), 28 deletions(-) diff --git a/client/finality-grandpa/src/authorities.rs b/client/finality-grandpa/src/authorities.rs index 11d3d4ba691da..e8bf4f148bb56 100644 --- a/client/finality-grandpa/src/authorities.rs +++ b/client/finality-grandpa/src/authorities.rs @@ -461,7 +461,7 @@ where ); telemetry!( - CONSENSUS_INFO; + None; CONSENSUS_INFO; "afg.applying_forced_authority_set_change"; "block" => ?change.canon_height ); @@ -544,7 +544,7 @@ where "👴 Applying authority set change scheduled at block #{:?}", change.canon_height, ); - telemetry!(CONSENSUS_INFO; "afg.applying_scheduled_authority_set_change"; + telemetry!(None; CONSENSUS_INFO; "afg.applying_scheduled_authority_set_change"; "block" => ?change.canon_height ); diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index 9f5582e5cea6b..117521e94d703 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -828,7 +828,7 @@ impl Inner { // ensure authority is part of the set. if !self.authorities.contains(&full.message.id) { debug!(target: "afg", "Message from unknown voter: {}", full.message.id); - telemetry!(CONSENSUS_DEBUG; "afg.bad_msg_signature"; "signature" => ?full.message.id); + telemetry!(None; CONSENSUS_DEBUG; "afg.bad_msg_signature"; "signature" => ?full.message.id); return Action::Discard(cost::UNKNOWN_VOTER); } @@ -840,7 +840,7 @@ impl Inner { full.set_id.0, ) { debug!(target: "afg", "Bad message signature {}", full.message.id); - telemetry!(CONSENSUS_DEBUG; "afg.bad_msg_signature"; "signature" => ?full.message.id); + telemetry!(None; CONSENSUS_DEBUG; "afg.bad_msg_signature"; "signature" => ?full.message.id); return Action::Discard(cost::BAD_SIGNATURE); } @@ -866,7 +866,7 @@ impl Inner { if full.message.precommits.len() != full.message.auth_data.len() || full.message.precommits.is_empty() { debug!(target: "afg", "Malformed compact commit"); - telemetry!(CONSENSUS_DEBUG; "afg.malformed_compact_commit"; + telemetry!(None; CONSENSUS_DEBUG; "afg.malformed_compact_commit"; "precommits_len" => ?full.message.precommits.len(), "auth_data_len" => ?full.message.auth_data.len(), "precommits_is_empty" => ?full.message.precommits.is_empty(), @@ -1411,7 +1411,7 @@ impl GossipValidator { Err(e) => { message_name = None; debug!(target: "afg", "Error decoding message: {}", e); - telemetry!(CONSENSUS_DEBUG; "afg.err_decoding_msg"; "" => ""); + telemetry!(None; CONSENSUS_DEBUG; "afg.err_decoding_msg"; "" => ""); let len = std::cmp::min(i32::max_value() as usize, data.len()) as i32; Action::Discard(Misbehavior::UndecodablePacket(len).cost()) diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index d502741465d23..d4bb28dcd3ca3 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -339,21 +339,21 @@ impl> NetworkBridge { if voters.len().get() <= TELEMETRY_VOTERS_LIMIT { match &msg.message.message { PrimaryPropose(propose) => { - telemetry!(CONSENSUS_INFO; "afg.received_propose"; + telemetry!(None; CONSENSUS_INFO; "afg.received_propose"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?propose.target_number, "target_hash" => ?propose.target_hash, ); }, Prevote(prevote) => { - telemetry!(CONSENSUS_INFO; "afg.received_prevote"; + telemetry!(None; CONSENSUS_INFO; "afg.received_prevote"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?prevote.target_number, "target_hash" => ?prevote.target_hash, ); }, Precommit(precommit) => { - telemetry!(CONSENSUS_INFO; "afg.received_precommit"; + telemetry!(None; CONSENSUS_INFO; "afg.received_precommit"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?precommit.target_number, "target_hash" => ?precommit.target_hash, @@ -505,7 +505,7 @@ fn incoming_global( format!("{}", a) }).collect(); - telemetry!(CONSENSUS_INFO; "afg.received_commit"; + telemetry!(None; CONSENSUS_INFO; "afg.received_commit"; "contains_precommits_signed_by" => ?precommits_signed_by, "target_number" => ?msg.message.target_number.clone(), "target_hash" => ?msg.message.target_hash.clone(), @@ -717,7 +717,7 @@ impl Sink> for OutgoingMessages ); telemetry!( - CONSENSUS_DEBUG; "afg.announcing_blocks_to_voted_peers"; + None; CONSENSUS_DEBUG; "afg.announcing_blocks_to_voted_peers"; "block" => ?target_hash, "round" => ?self.round, "set_id" => ?self.set_id, ); @@ -797,7 +797,7 @@ fn check_compact_commit( &mut buf, ) { debug!(target: "afg", "Bad commit message signature {}", id); - telemetry!(CONSENSUS_DEBUG; "afg.bad_commit_msg_signature"; "id" => ?id); + telemetry!(None; CONSENSUS_DEBUG; "afg.bad_commit_msg_signature"; "id" => ?id); let cost = Misbehavior::BadCommitMessage { signatures_checked: i as i32, blocks_loaded: 0, @@ -885,7 +885,7 @@ fn check_catch_up( buf, ) { debug!(target: "afg", "Bad catch up message signature {}", id); - telemetry!(CONSENSUS_DEBUG; "afg.bad_catch_up_msg_signature"; "id" => ?id); + telemetry!(None; CONSENSUS_DEBUG; "afg.bad_catch_up_msg_signature"; "id" => ?id); let cost = Misbehavior::BadCatchUpMessage { signatures_checked: signatures_checked as i32, @@ -968,7 +968,7 @@ impl Sink<(RoundNumber, Commit)> for CommitsOut { let (round, commit) = input; let round = Round(round); - telemetry!(CONSENSUS_DEBUG; "afg.commit_issued"; + telemetry!(None; CONSENSUS_DEBUG; "afg.commit_issued"; "target_number" => ?commit.target_number, "target_hash" => ?commit.target_hash, ); let (precommits, auth_data) = commit.precommits.into_iter() diff --git a/client/finality-grandpa/src/environment.rs b/client/finality-grandpa/src/environment.rs index 7925a674c2983..64cc05f70087e 100644 --- a/client/finality-grandpa/src/environment.rs +++ b/client/finality-grandpa/src/environment.rs @@ -891,7 +891,7 @@ where }; let report_prevote_metrics = |prevote: &Prevote| { - telemetry!(CONSENSUS_DEBUG; "afg.prevote_issued"; + telemetry!(None; CONSENSUS_DEBUG; "afg.prevote_issued"; "round" => round, "target_number" => ?prevote.target_number, "target_hash" => ?prevote.target_hash, @@ -950,7 +950,7 @@ where }; let report_precommit_metrics = |precommit: &Precommit| { - telemetry!(CONSENSUS_DEBUG; "afg.precommit_issued"; + telemetry!(None; CONSENSUS_DEBUG; "afg.precommit_issued"; "round" => round, "target_number" => ?precommit.target_number, "target_hash" => ?precommit.target_hash, @@ -1320,7 +1320,7 @@ where warn!(target: "afg", "Error applying finality to block {:?}: {:?}", (hash, number), e); e })?; - telemetry!(CONSENSUS_INFO; "afg.finalized_blocks_up_to"; + telemetry!(None; CONSENSUS_INFO; "afg.finalized_blocks_up_to"; "number" => ?number, "hash" => ?hash, ); @@ -1340,7 +1340,7 @@ where ); } - telemetry!(CONSENSUS_INFO; "afg.generating_new_authority_set"; + telemetry!(None; CONSENSUS_INFO; "afg.generating_new_authority_set"; "number" => ?canon_number, "hash" => ?canon_hash, "authorities" => ?set_ref.to_vec(), "set_id" => ?new_id, diff --git a/client/finality-grandpa/src/finality_proof.rs b/client/finality-grandpa/src/finality_proof.rs index c88faa2498928..b79b3190739d6 100644 --- a/client/finality-grandpa/src/finality_proof.rs +++ b/client/finality-grandpa/src/finality_proof.rs @@ -247,9 +247,6 @@ where .map_err(|_| ClientError::JustificationDecode)?; justification.verify(current_set_id, ¤t_authorities)?; - use sc_telemetry::{telemetry, CONSENSUS_INFO}; - telemetry!(CONSENSUS_INFO; "afg.finality_proof_ok"; - "finalized_header_hash" => ?proof.block); Ok(proof) } diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index 809d11ebae203..2ed7bafa87de8 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -552,7 +552,7 @@ where >::zero(), || { let authorities = genesis_authorities_provider.get()?; - telemetry!(CONSENSUS_DEBUG; "afg.loading_authorities"; + telemetry!(None; CONSENSUS_DEBUG; "afg.loading_authorities"; "authorities_len" => ?authorities.len() ); Ok(authorities) @@ -754,7 +754,7 @@ where elements are always of type string", ); - telemetry!(CONSENSUS_INFO; "afg.authority_set"; + telemetry!(None; CONSENSUS_INFO; "afg.authority_set"; "authority_id" => authority_id.to_string(), "authority_set_id" => ?set_id, "authorities" => authorities, @@ -892,7 +892,7 @@ where let authority_id = local_authority_id(&self.env.voters, self.env.config.keystore.as_ref()) .unwrap_or_default(); - telemetry!(CONSENSUS_DEBUG; "afg.starting_new_voter"; + telemetry!(None; CONSENSUS_DEBUG; "afg.starting_new_voter"; "name" => ?self.env.config.name(), "set_id" => ?self.env.set_id, "authority_id" => authority_id.to_string(), @@ -911,7 +911,10 @@ where "authorities is always at least an empty vector; elements are always of type string", ); - telemetry!(CONSENSUS_INFO; "afg.authority_set"; + telemetry!( + None; + CONSENSUS_INFO; + "afg.authority_set"; "number" => ?chain_info.finalized_number, "hash" => ?chain_info.finalized_hash, "authority_id" => authority_id.to_string(), @@ -971,7 +974,10 @@ where let voters: Vec = new.authorities.iter().map(move |(a, _)| { format!("{}", a) }).collect(); - telemetry!(CONSENSUS_INFO; "afg.voter_command_change_authorities"; + telemetry!( + None; + CONSENSUS_INFO; + "afg.voter_command_change_authorities"; "number" => ?new.canon_number, "hash" => ?new.canon_hash, "voters" => ?voters, diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 95e046158e09e..4e905b1e6d49f 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -487,7 +487,7 @@ enum Register { /// # let set_id = (43_u64, 44_u64); /// # let authorities = vec![45_u64]; /// telemetry!( -/// telemetry; // an `Option` +/// telemetry; // an `Option` /// CONSENSUS_INFO; /// "afg.authority_set"; /// "authority_id" => authority_id.to_string(), @@ -498,7 +498,8 @@ enum Register { #[macro_export(local_inner_macros)] macro_rules! telemetry { ( $telemetry:expr; $verbosity:expr; $msg:expr; $( $t:tt )* ) => {{ - if let Some(telemetry) = $telemetry.as_ref() { + let telemetry: Option<&$crate::TelemetryHandle> = $telemetry.as_ref(); + if let Some(telemetry) = telemetry { let verbosity: $crate::VerbosityLevel = $verbosity; match format_fields_to_json!($($t)*) { Err(err) => { From ddd2bf7c83ca1bd717d880a6bf07232af268d38f Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 15:21:14 +0100 Subject: [PATCH 80/92] WIP --- .../src/communication/gossip.rs | 1 + .../src/communication/tests.rs | 1 + client/finality-grandpa/src/lib.rs | 76 +++++++++++++------ client/finality-grandpa/src/tests.rs | 25 ++++-- 4 files changed, 75 insertions(+), 28 deletions(-) diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index 117521e94d703..0cefd5eb6e06a 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -1630,6 +1630,7 @@ mod tests { name: None, is_authority: true, observer_enabled: true, + telemetry: None, } } diff --git a/client/finality-grandpa/src/communication/tests.rs b/client/finality-grandpa/src/communication/tests.rs index 4abea991cec37..33e73e2f7a3b2 100644 --- a/client/finality-grandpa/src/communication/tests.rs +++ b/client/finality-grandpa/src/communication/tests.rs @@ -139,6 +139,7 @@ fn config() -> crate::Config { name: None, is_authority: true, observer_enabled: true, + telemetry: None, } } diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index 2ed7bafa87de8..70105158b3124 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -79,7 +79,7 @@ use sp_core::{ use sp_keystore::{SyncCryptoStorePtr, SyncCryptoStore}; use sp_application_crypto::AppKey; use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver}; -use sc_telemetry::{telemetry, CONSENSUS_INFO, CONSENSUS_DEBUG}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO, CONSENSUS_DEBUG}; use parking_lot::RwLock; use finality_grandpa::Error as GrandpaError; @@ -270,6 +270,8 @@ pub struct Config { pub name: Option, /// The keystore that manages the keys of this node. pub keystore: Option, + /// TelemetryHandle instance. + pub telemetry: Option, } impl Config { @@ -451,6 +453,7 @@ pub struct LinkHalf { voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: GrandpaJustificationSender, justification_stream: GrandpaJustificationStream, + telemetry: Option, } impl LinkHalf { @@ -501,6 +504,7 @@ pub fn block_import( client: Arc, genesis_authorities_provider: &dyn GenesisAuthoritySetProvider, select_chain: SC, + telemetry: Option, ) -> Result< ( GrandpaBlockImport, @@ -518,6 +522,7 @@ where genesis_authorities_provider, select_chain, Default::default(), + telemetry, ) } @@ -531,6 +536,7 @@ pub fn block_import_with_authority_set_hard_forks genesis_authorities_provider: &dyn GenesisAuthoritySetProvider, select_chain: SC, authority_set_hard_forks: Vec<(SetId, (Block::Hash, NumberFor), AuthorityList)>, + telemetry: Option, ) -> Result< ( GrandpaBlockImport, @@ -550,13 +556,19 @@ where &*client, genesis_hash, >::zero(), - || { - let authorities = genesis_authorities_provider.get()?; - telemetry!(None; CONSENSUS_DEBUG; "afg.loading_authorities"; - "authorities_len" => ?authorities.len() - ); - Ok(authorities) - } + { + let telemetry = telemetry.clone(); + move || { + let authorities = genesis_authorities_provider.get()?; + telemetry!( + telemetry; + CONSENSUS_DEBUG; + "afg.loading_authorities"; + "authorities_len" => ?authorities.len() + ); + Ok(authorities) + } + }, )?; let (voter_commands_tx, voter_commands_rx) = tracing_unbounded("mpsc_grandpa_voter_command"); @@ -590,6 +602,7 @@ where voter_commands_tx, authority_set_hard_forks, justification_sender.clone(), + //telemetry.clone(), ), LinkHalf { client, @@ -598,6 +611,7 @@ where voter_commands_rx, justification_sender, justification_stream, + telemetry, }, )) } @@ -660,14 +674,14 @@ pub struct GrandpaParams { /// `sc_network` crate, it is assumed that the Grandpa notifications protocol has been passed /// to the configuration of the networking. See [`grandpa_peers_set_config`]. pub network: N, - /// If supplied, can be used to hook on telemetry connection established events. - pub telemetry_on_connect: Option>, /// A voting rule used to potentially restrict target votes. pub voting_rule: VR, /// The prometheus metrics registry. pub prometheus_registry: Option, /// The voter state is exposed at an RPC endpoint. pub shared_voter_state: SharedVoterState, + /// TelemetryHandle instance. + pub telemetry: Option, } /// Returns the configuration value to put in @@ -706,10 +720,10 @@ where mut config, link, network, - telemetry_on_connect, voting_rule, prometheus_registry, shared_voter_state, + telemetry, } = grandpa_params; // NOTE: we have recently removed `run_grandpa_observer` from the public @@ -725,6 +739,7 @@ where voter_commands_rx, justification_sender, justification_stream: _, + telemetry: _, } = link; let network = NetworkBridge::new( @@ -732,11 +747,16 @@ where config.clone(), persistent_data.set_state.clone(), prometheus_registry.as_ref(), + //telemetry.clone(), ); let conf = config.clone(); - let telemetry_task = if let Some(telemetry_on_connect) = telemetry_on_connect { + let telemetry_task = if let Some(telemetry_on_connect) = telemetry + .as_ref() + .map(|x| x.on_connect_stream()) + { let authorities = persistent_data.authority_set.clone(); + let telemetry = telemetry.clone(); let events = telemetry_on_connect .for_each(move |_| { let current_authorities = authorities.current_authorities(); @@ -751,10 +771,13 @@ where let authorities = serde_json::to_string(&authorities).expect( "authorities is always at least an empty vector; \ - elements are always of type string", + elements are always of type string", ); - telemetry!(None; CONSENSUS_INFO; "afg.authority_set"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.authority_set"; "authority_id" => authority_id.to_string(), "authority_set_id" => ?set_id, "authorities" => authorities, @@ -778,6 +801,7 @@ where prometheus_registry, shared_voter_state, justification_sender, + telemetry, ); let voter_work = voter_work.map(|res| match res { @@ -816,7 +840,7 @@ struct VoterWork, SC, VR> { env: Arc>, voter_commands_rx: TracingUnboundedReceiver>>, network: NetworkBridge, - + telemetry: Option, /// Prometheus metrics. metrics: Option, } @@ -843,6 +867,7 @@ where prometheus_registry: Option, shared_voter_state: SharedVoterState, justification_sender: GrandpaJustificationSender, + telemetry: Option, ) -> Self { let metrics = match prometheus_registry.as_ref().map(Metrics::register) { Some(Ok(metrics)) => Some(metrics), @@ -866,6 +891,7 @@ where voter_set_state: persistent_data.set_state, metrics: metrics.as_ref().map(|m| m.environment.clone()), justification_sender: Some(justification_sender), + //telemetry: telemetry.clone(), _phantom: PhantomData, }); @@ -877,6 +903,7 @@ where env, voter_commands_rx, network, + telemetry, metrics, }; work.rebuild_voter(); @@ -892,7 +919,10 @@ where let authority_id = local_authority_id(&self.env.voters, self.env.config.keystore.as_ref()) .unwrap_or_default(); - telemetry!(None; CONSENSUS_DEBUG; "afg.starting_new_voter"; + telemetry!( + self.telemetry; + CONSENSUS_DEBUG; + "afg.starting_new_voter"; "name" => ?self.env.config.name(), "set_id" => ?self.env.set_id, "authority_id" => authority_id.to_string(), @@ -912,7 +942,7 @@ where ); telemetry!( - None; + self.telemetry; CONSENSUS_INFO; "afg.authority_set"; "number" => ?chain_info.finalized_number, @@ -975,7 +1005,7 @@ where format!("{}", a) }).collect(); telemetry!( - None; + self.telemetry; CONSENSUS_INFO; "afg.voter_command_change_authorities"; "number" => ?new.canon_number, @@ -998,10 +1028,11 @@ where })?; let voters = Arc::new(VoterSet::new(new.authorities.into_iter()) - .expect("new authorities come from pending change; \ - pending change comes from `AuthoritySet`; \ - `AuthoritySet` validates authorities is non-empty and weights are non-zero; \ - qed." + .expect( + "new authorities come from pending change; \ + pending change comes from `AuthoritySet`; \ + `AuthoritySet` validates authorities is non-empty and weights are non-zero; \ + qed." ) ); @@ -1017,6 +1048,7 @@ where voting_rule: self.env.voting_rule.clone(), metrics: self.env.metrics.clone(), justification_sender: self.env.justification_sender.clone(), + //telemetry: self.telemetry.clone(), _phantom: PhantomData, }); diff --git a/client/finality-grandpa/src/tests.rs b/client/finality-grandpa/src/tests.rs index 921b49db61c25..17992a0c48a67 100644 --- a/client/finality-grandpa/src/tests.rs +++ b/client/finality-grandpa/src/tests.rs @@ -120,6 +120,7 @@ impl TestNetFactory for GrandpaTestNet { client.clone(), &self.test_config, LongestChain::new(backend.clone()), + None, ).expect("Could not create block import for fresh peer."); let justification_import = Box::new(import.clone()); ( @@ -252,13 +253,14 @@ fn initialize_grandpa( name: Some(format!("peer#{}", peer_id)), is_authority: true, observer_enabled: true, + telemetry: None, }, link, network: net_service, - telemetry_on_connect: None, voting_rule: (), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), + telemetry: None, }; let voter = run_grandpa_voter(grandpa_params).expect("all in order with client and network"); @@ -395,13 +397,14 @@ fn finalize_3_voters_1_full_observer() { name: Some(format!("peer#{}", peer_id)), is_authority: true, observer_enabled: true, + telemetry: None, }, link: link, network: net_service, - telemetry_on_connect: None, voting_rule: (), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), + telemetry: None, }; run_grandpa_voter(grandpa_params).expect("all in order with client and network") @@ -488,13 +491,14 @@ fn transition_3_voters_twice_1_full_observer() { name: Some(format!("peer#{}", peer_id)), is_authority: true, observer_enabled: true, + telemetry: None, }, link, network: net_service, - telemetry_on_connect: None, voting_rule: (), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), + telemetry: None, }; voters.push(run_grandpa_voter(grandpa_params).expect("all in order with client and network")); @@ -921,6 +925,7 @@ fn voter_persists_its_votes() { name: Some(format!("peer#{}", 1)), is_authority: true, observer_enabled: true, + telemetry: None, }; let set_state = { @@ -939,6 +944,7 @@ fn voter_persists_its_votes() { config.clone(), set_state, None, + //None, ) }; @@ -964,13 +970,14 @@ fn voter_persists_its_votes() { name: Some(format!("peer#{}", 0)), is_authority: true, observer_enabled: true, + telemetry: None, }, link, network: net_service, - telemetry_on_connect: None, voting_rule: VotingRulesBuilder::default().build(), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), + telemetry: None, }; run_grandpa_voter(grandpa_params).expect("all in order with client and network") @@ -1006,13 +1013,14 @@ fn voter_persists_its_votes() { name: Some(format!("peer#{}", 0)), is_authority: true, observer_enabled: true, + telemetry: None, }, link, network: net_service, - telemetry_on_connect: None, voting_rule: VotingRulesBuilder::default().build(), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), + telemetry: None, }; run_grandpa_voter(grandpa_params) @@ -1165,6 +1173,7 @@ fn finalize_3_voters_1_light_observer() { name: Some("observer".to_string()), is_authority: false, observer_enabled: true, + telemetry: None, }, net.peers[3].data.lock().take().expect("link initialized at startup; qed"), net.peers[3].network_service().clone(), @@ -1206,13 +1215,14 @@ fn voter_catches_up_to_latest_round_when_behind() { name: Some(format!("peer#{}", peer_id)), is_authority: true, observer_enabled: true, + telemetry: None, }, link, network: net.lock().peer(peer_id).network_service().clone(), - telemetry_on_connect: None, voting_rule: (), prometheus_registry: None, shared_voter_state: SharedVoterState::empty(), + telemetry: None, }; Box::pin(run_grandpa_voter(grandpa_params).expect("all in order with client and network")) @@ -1328,6 +1338,7 @@ where name: None, is_authority: true, observer_enabled: true, + telemetry: None, }; let network = NetworkBridge::new( @@ -1335,6 +1346,7 @@ where config.clone(), set_state.clone(), None, + //None, ); Environment { @@ -1349,6 +1361,7 @@ where voting_rule, metrics: None, justification_sender: None, + //telemetry: None, _phantom: PhantomData, } } From a67f8f46750cba0520c40c80c6005646a4e4b99e Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 15:25:34 +0100 Subject: [PATCH 81/92] WIP --- client/finality-grandpa/src/observer.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/client/finality-grandpa/src/observer.rs b/client/finality-grandpa/src/observer.rs index 3054a9df61c56..ff1b6c359ff5a 100644 --- a/client/finality-grandpa/src/observer.rs +++ b/client/finality-grandpa/src/observer.rs @@ -29,6 +29,7 @@ use log::{debug, info, warn}; use sp_keystore::SyncCryptoStorePtr; use sp_consensus::SelectChain; use sc_client_api::backend::Backend; +use sc_telemetry::TelemetryHandle; use sp_utils::mpsc::TracingUnboundedReceiver; use sp_runtime::traits::{NumberFor, Block as BlockT}; use sp_blockchain::HeaderMetadata; @@ -67,6 +68,7 @@ fn grandpa_observer( last_finalized_number: NumberFor, commits: S, note_round: F, + telemetry: Option, ) -> impl Future>>> where NumberFor: BlockNumberOps, @@ -121,6 +123,7 @@ where (round, commit).into(), false, justification_sender.as_ref(), + //telemetry.clone(), ) { Ok(_) => {}, Err(e) => return future::err(e), @@ -172,7 +175,8 @@ where persistent_data, voter_commands_rx, justification_sender, - .. + justification_stream: _, + telemetry, } = link; let network = NetworkBridge::new( @@ -180,15 +184,17 @@ where config.clone(), persistent_data.set_state.clone(), None, + //telemetry.clone(), ); let observer_work = ObserverWork::new( - client, + client.clone(), network, persistent_data, config.keystore, voter_commands_rx, Some(justification_sender), + telemetry.clone(), ); let observer_work = observer_work @@ -210,6 +216,7 @@ struct ObserverWork> { keystore: Option, voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: Option>, + telemetry: Option, _phantom: PhantomData, } @@ -228,6 +235,7 @@ where keystore: Option, voter_commands_rx: TracingUnboundedReceiver>>, justification_sender: Option>, + telemetry: Option, ) -> Self { let mut work = ObserverWork { @@ -240,6 +248,7 @@ where keystore: keystore.clone(), voter_commands_rx, justification_sender, + telemetry, _phantom: PhantomData, }; work.rebuild_observer(); @@ -289,6 +298,7 @@ where last_finalized_number, global_in, note_round, + self.telemetry.clone(), ); self.observer = Box::pin(observer); @@ -429,6 +439,7 @@ mod tests { None, voter_command_rx, None, + None, ); // Trigger a reputation change through the gossip validator. From 9b6851ca6a9e6b4dc066c6baab0c4361fe5ef961 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 15:29:26 +0100 Subject: [PATCH 82/92] WIP --- client/finality-grandpa/src/import.rs | 21 ++++++++++++++++++--- client/finality-grandpa/src/lib.rs | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/client/finality-grandpa/src/import.rs b/client/finality-grandpa/src/import.rs index 2c86319dd54af..f88a8a3e46b4a 100644 --- a/client/finality-grandpa/src/import.rs +++ b/client/finality-grandpa/src/import.rs @@ -24,6 +24,7 @@ use parking_lot::RwLockWriteGuard; use sp_blockchain::{BlockStatus, well_known_cache_keys}; use sc_client_api::{backend::Backend, utils::is_descendent_of}; +use sc_telemetry::TelemetryHandle; use sp_utils::mpsc::TracingUnboundedSender; use sp_api::TransactionFor; @@ -62,6 +63,7 @@ pub struct GrandpaBlockImport { send_voter_commands: TracingUnboundedSender>>, authority_set_hard_forks: HashMap>>, justification_sender: GrandpaJustificationSender, + telemetry: Option, _phantom: PhantomData, } @@ -76,6 +78,7 @@ impl Clone for send_voter_commands: self.send_voter_commands.clone(), authority_set_hard_forks: self.authority_set_hard_forks.clone(), justification_sender: self.justification_sender.clone(), + telemetry: self.telemetry.clone(), _phantom: PhantomData, } } @@ -338,7 +341,13 @@ where let applied_changes = { let forced_change_set = guard .as_mut() - .apply_forced_changes(hash, number, &is_descendent_of, initial_sync) + .apply_forced_changes( + hash, + number, + &is_descendent_of, + initial_sync, + //self.telemetry.clone(), + ) .map_err(|e| ConsensusError::ClientImport(e.to_string())) .map_err(ConsensusError::from)?; @@ -355,8 +364,11 @@ where let canon_hash = self.inner.header(BlockId::Number(canon_number)) .map_err(|e| ConsensusError::ClientImport(e.to_string()))? - .expect("the given block number is less or equal than the current best finalized number; \ - current best finalized number must exist in chain; qed.") + .expect( + "the given block number is less or equal than the current best + finalized number; current best finalized number must exist in + chain; qed." + ) .hash(); NewAuthoritySet { @@ -557,6 +569,7 @@ impl GrandpaBlockImport>>, authority_set_hard_forks: Vec<(SetId, PendingChange>)>, justification_sender: GrandpaJustificationSender, + telemetry: Option, ) -> GrandpaBlockImport { // check for and apply any forced authority set hard fork that applies // to the *current* authority set. @@ -600,6 +613,7 @@ impl GrandpaBlockImport Date: Thu, 4 Mar 2021 15:37:17 +0100 Subject: [PATCH 83/92] WIP --- client/finality-grandpa/src/authorities.rs | 75 ++++++++++++++-------- client/finality-grandpa/src/aux_schema.rs | 2 +- client/finality-grandpa/src/environment.rs | 1 + client/finality-grandpa/src/import.rs | 2 +- 4 files changed, 50 insertions(+), 30 deletions(-) diff --git a/client/finality-grandpa/src/authorities.rs b/client/finality-grandpa/src/authorities.rs index e8bf4f148bb56..2edede186a324 100644 --- a/client/finality-grandpa/src/authorities.rs +++ b/client/finality-grandpa/src/authorities.rs @@ -23,7 +23,7 @@ use parking_lot::RwLock; use finality_grandpa::voter_set::VoterSet; use parity_scale_codec::{Encode, Decode}; use log::debug; -use sc_telemetry::{telemetry, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO}; use sp_finality_grandpa::{AuthorityId, AuthorityList}; use std::cmp::Ord; @@ -43,8 +43,8 @@ pub enum Error { #[display(fmt = "Multiple pending forced authority set changes are not allowed.")] MultiplePendingForcedAuthoritySetChanges, #[display( - fmt = "A pending forced authority set change could not be applied since it must be applied after \ - the pending standard change at #{}", + fmt = "A pending forced authority set change could not be applied since it must be applied \ + after the pending standard change at #{}", _0 )] ForcedAuthoritySetChangeDependencyUnsatisfied(N), @@ -278,9 +278,13 @@ where let hash = pending.canon_hash.clone(); let number = pending.canon_height.clone(); - debug!(target: "afg", "Inserting potential standard set change signaled at block {:?} \ - (delayed by {:?} blocks).", - (&number, &hash), pending.delay); + debug!( + target: "afg", + "Inserting potential standard set change signaled at block {:?} (delayed by {:?} + blocks).", + (&number, &hash), + pending.delay, + ); self.pending_standard_changes.import( hash, @@ -289,8 +293,10 @@ where is_descendent_of, )?; - debug!(target: "afg", "There are now {} alternatives for the next pending standard change (roots), \ - and a total of {} pending standard changes (across all forks).", + debug!( + target: "afg", + "There are now {} alternatives for the next pending standard change (roots), and a + total of {} pending standard changes (across all forks).", self.pending_standard_changes.roots().count(), self.pending_standard_changes.iter().count(), ); @@ -326,9 +332,12 @@ where )) .unwrap_or_else(|i| i); - debug!(target: "afg", "Inserting potential forced set change at block {:?} \ - (delayed by {:?} blocks).", - (&pending.canon_height, &pending.canon_hash), pending.delay); + debug!( + target: "afg", + "Inserting potential forced set change at block {:?} (delayed by {:?} blocks).", + (&pending.canon_height, &pending.canon_hash), + pending.delay, + ); self.pending_forced_changes.insert(idx, pending); @@ -409,6 +418,7 @@ where best_number: N, is_descendent_of: &F, initial_sync: bool, + telemetry: Option, ) -> Result, Error> where F: Fn(&H, &H) -> Result, @@ -461,7 +471,8 @@ where ); telemetry!( - None; CONSENSUS_INFO; + telemetry; + CONSENSUS_INFO; "afg.applying_forced_authority_set_change"; "block" => ?change.canon_height ); @@ -505,6 +516,7 @@ where finalized_number: N, is_descendent_of: &F, initial_sync: bool, + telemetry: Option, ) -> Result, Error> where F: Fn(&H, &H) -> Result, @@ -544,7 +556,10 @@ where "👴 Applying authority set change scheduled at block #{:?}", change.canon_height, ); - telemetry!(None; CONSENSUS_INFO; "afg.applying_scheduled_authority_set_change"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.applying_scheduled_authority_set_change"; "block" => ?change.canon_height ); @@ -894,6 +909,7 @@ mod tests { _ => unreachable!(), }), false, + None, ).unwrap(); assert!(status.changed); @@ -913,6 +929,7 @@ mod tests { _ => unreachable!(), }), false, + None, ).unwrap(); assert!(status.changed); @@ -971,7 +988,7 @@ mod tests { // trying to finalize past `change_c` without finalizing `change_a` first assert!(matches!( - authorities.apply_standard_changes("hash_d", 40, &is_descendent_of, false), + authorities.apply_standard_changes("hash_d", 40, &is_descendent_of, false, None), Err(Error::ForkTree(fork_tree::Error::UnfinalizedAncestor)) )); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges::empty()); @@ -981,6 +998,7 @@ mod tests { 15, &is_descendent_of, false, + None, ).unwrap(); assert!(status.changed); @@ -996,6 +1014,7 @@ mod tests { 40, &is_descendent_of, false, + None, ).unwrap(); assert!(status.changed); @@ -1138,7 +1157,7 @@ mod tests { // too early and there's no forced changes to apply. assert!( authorities - .apply_forced_changes("hash_a10", 10, &static_is_descendent_of(true), false) + .apply_forced_changes("hash_a10", 10, &static_is_descendent_of(true), false, None) .unwrap() .is_none() ); @@ -1146,7 +1165,7 @@ mod tests { // too late. assert!( authorities - .apply_forced_changes("hash_a16", 16, &is_descendent_of_a, false) + .apply_forced_changes("hash_a16", 16, &is_descendent_of_a, false, None) .unwrap() .is_none() ); @@ -1154,7 +1173,7 @@ mod tests { // on time -- chooses the right change for this fork. assert_eq!( authorities - .apply_forced_changes("hash_a15", 15, &is_descendent_of_a, false) + .apply_forced_changes("hash_a15", 15, &is_descendent_of_a, false, None) .unwrap() .unwrap(), ( @@ -1202,7 +1221,7 @@ mod tests { // it should be enacted at the same block that signaled it assert!( authorities - .apply_forced_changes("hash_a", 5, &static_is_descendent_of(false), false) + .apply_forced_changes("hash_a", 5, &static_is_descendent_of(false), false, None) .unwrap() .is_some() ); @@ -1269,27 +1288,27 @@ mod tests { // the forced change cannot be applied since the pending changes it depends on // have not been applied yet. assert!(matches!( - authorities.apply_forced_changes("hash_d45", 45, &static_is_descendent_of(true), false), + authorities.apply_forced_changes("hash_d45", 45, &static_is_descendent_of(true), false, None), Err(Error::ForcedAuthoritySetChangeDependencyUnsatisfied(15)) )); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges::empty()); // we apply the first pending standard change at #15 authorities - .apply_standard_changes("hash_a15", 15, &static_is_descendent_of(true), false) + .apply_standard_changes("hash_a15", 15, &static_is_descendent_of(true), false, None) .unwrap(); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15)])); // but the forced change still depends on the next standard change assert!(matches!( - authorities.apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false), + authorities.apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false, None), Err(Error::ForcedAuthoritySetChangeDependencyUnsatisfied(20)) )); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15)])); // we apply the pending standard change at #20 authorities - .apply_standard_changes("hash_b", 20, &static_is_descendent_of(true), false) + .apply_standard_changes("hash_b", 20, &static_is_descendent_of(true), false, None) .unwrap(); assert_eq!(authorities.authority_set_changes, AuthoritySetChanges(vec![(0, 15), (1, 20)])); @@ -1298,7 +1317,7 @@ mod tests { // at #35. subsequent forced changes on the same branch must be kept assert_eq!( authorities - .apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false) + .apply_forced_changes("hash_d", 45, &static_is_descendent_of(true), false, None) .unwrap() .unwrap(), ( @@ -1395,7 +1414,7 @@ mod tests { // we apply the change at A0 which should prune it and the fork at B authorities - .apply_standard_changes("hash_a0", 5, &is_descendent_of, false) + .apply_standard_changes("hash_a0", 5, &is_descendent_of, false, None) .unwrap(); // the next change is now at A1 (#10) @@ -1583,14 +1602,14 @@ mod tests { // applying the standard change at A should not prune anything // other then the change that was applied authorities - .apply_standard_changes("A", 5, &is_descendent_of, false) + .apply_standard_changes("A", 5, &is_descendent_of, false, None) .unwrap(); assert_eq!(authorities.pending_changes().count(), 6); // same for B authorities - .apply_standard_changes("B", 10, &is_descendent_of, false) + .apply_standard_changes("B", 10, &is_descendent_of, false, None) .unwrap(); assert_eq!(authorities.pending_changes().count(), 5); @@ -1599,7 +1618,7 @@ mod tests { // finalizing C2 should clear all forced changes authorities - .apply_standard_changes("C2", 15, &is_descendent_of, false) + .apply_standard_changes("C2", 15, &is_descendent_of, false, None) .unwrap(); assert_eq!(authorities.pending_forced_changes.len(), 0); @@ -1607,7 +1626,7 @@ mod tests { // finalizing C0 should clear all forced changes but D let mut authorities = authorities2; authorities - .apply_standard_changes("C0", 15, &is_descendent_of, false) + .apply_standard_changes("C0", 15, &is_descendent_of, false, None) .unwrap(); assert_eq!(authorities.pending_forced_changes.len(), 1); diff --git a/client/finality-grandpa/src/aux_schema.rs b/client/finality-grandpa/src/aux_schema.rs index 1ce3c7999f24c..43c45b9f10ae1 100644 --- a/client/finality-grandpa/src/aux_schema.rs +++ b/client/finality-grandpa/src/aux_schema.rs @@ -137,7 +137,7 @@ struct V2AuthoritySet { } pub(crate) fn load_decode( - backend: &B, + backend: &B, key: &[u8] ) -> ClientResult> { match backend.get_aux(key)? { diff --git a/client/finality-grandpa/src/environment.rs b/client/finality-grandpa/src/environment.rs index 64cc05f70087e..fbeaa89c6a882 100644 --- a/client/finality-grandpa/src/environment.rs +++ b/client/finality-grandpa/src/environment.rs @@ -1245,6 +1245,7 @@ where number, &is_descendent_of::(&*client, None), initial_sync, + None, ).map_err(|e| Error::Safety(e.to_string()))?; // send a justification notification if a sender exists and in case of error log it. diff --git a/client/finality-grandpa/src/import.rs b/client/finality-grandpa/src/import.rs index f88a8a3e46b4a..d15d084b45125 100644 --- a/client/finality-grandpa/src/import.rs +++ b/client/finality-grandpa/src/import.rs @@ -346,7 +346,7 @@ where number, &is_descendent_of, initial_sync, - //self.telemetry.clone(), + self.telemetry.clone(), ) .map_err(|e| ConsensusError::ClientImport(e.to_string())) .map_err(ConsensusError::from)?; From ae35cf7659ceae1a23a50889f627f6ed4aa3f52e Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 15:43:41 +0100 Subject: [PATCH 84/92] WIP --- .../finality-grandpa/src/communication/mod.rs | 187 +++++++++++------- .../src/communication/tests.rs | 1 + client/finality-grandpa/src/lib.rs | 2 +- client/finality-grandpa/src/observer.rs | 2 +- client/finality-grandpa/src/tests.rs | 4 +- 5 files changed, 125 insertions(+), 71 deletions(-) diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index d4bb28dcd3ca3..4c7f1d6ff9111 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -42,7 +42,7 @@ use sc_network::{NetworkService, ReputationChange}; use sc_network_gossip::{GossipEngine, Network as GossipNetwork}; use parity_scale_codec::{Encode, Decode}; use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT, NumberFor}; -use sc_telemetry::{telemetry, CONSENSUS_DEBUG, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG, CONSENSUS_INFO}; use crate::{ CatchUp, Commit, CommunicationIn, CommunicationOutH, @@ -192,6 +192,8 @@ pub(crate) struct NetworkBridge> { // just an `UnboundedReceiver`, one could also switch to a multi-producer-*multi*-consumer // channel implementation. gossip_validator_report_stream: Arc>>, + + telemetry: Option, } impl> Unpin for NetworkBridge {} @@ -206,11 +208,13 @@ impl> NetworkBridge { config: crate::Config, set_state: crate::environment::SharedVoterSetState, prometheus_registry: Option<&Registry>, + telemetry: Option, ) -> Self { let (validator, report_stream) = GossipValidator::new( config, set_state.clone(), prometheus_registry, + //telemetry.clone(), ); let validator = Arc::new(validator); @@ -268,6 +272,7 @@ impl> NetworkBridge { neighbor_sender: neighbor_packet_sender, neighbor_packet_worker: Arc::new(Mutex::new(neighbor_packet_worker)), gossip_validator_report_stream: Arc::new(Mutex::new(report_stream)), + telemetry, } } @@ -320,6 +325,7 @@ impl> NetworkBridge { }); let topic = round_topic::(round.0, set_id.0); + let telemetry = self.telemetry.clone(); let incoming = self.gossip_engine.lock().messages_for(topic) .filter_map(move |notification| { let decoded = GossipMessage::::decode(&mut ¬ification.message[..]); @@ -339,21 +345,30 @@ impl> NetworkBridge { if voters.len().get() <= TELEMETRY_VOTERS_LIMIT { match &msg.message.message { PrimaryPropose(propose) => { - telemetry!(None; CONSENSUS_INFO; "afg.received_propose"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.received_propose"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?propose.target_number, "target_hash" => ?propose.target_hash, ); }, Prevote(prevote) => { - telemetry!(None; CONSENSUS_INFO; "afg.received_prevote"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.received_prevote"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?prevote.target_number, "target_hash" => ?prevote.target_hash, ); }, Precommit(precommit) => { - telemetry!(None; CONSENSUS_INFO; "afg.received_precommit"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.received_precommit"; "voter" => ?format!("{}", msg.message.id), "target_number" => ?precommit.target_number, "target_hash" => ?precommit.target_hash, @@ -379,6 +394,7 @@ impl> NetworkBridge { network: self.gossip_engine.clone(), sender: tx, has_voted, + telemetry: self.telemetry.clone(), }; // Combine incoming votes from external GRANDPA nodes with outgoing @@ -412,6 +428,7 @@ impl> NetworkBridge { voters, self.validator.clone(), self.neighbor_sender.clone(), + self.telemetry.clone(), ); let outgoing = CommitsOut::::new( @@ -420,6 +437,7 @@ impl> NetworkBridge { is_voter, self.validator.clone(), self.neighbor_sender.clone(), + self.telemetry.clone(), ); let outgoing = outgoing.with(|out| { @@ -491,72 +509,80 @@ fn incoming_global( voters: Arc>, gossip_validator: Arc>, neighbor_sender: periodic::NeighborPacketSender, + telemetry: Option, ) -> impl Stream> { - let process_commit = move | - msg: FullCommitMessage, - mut notification: sc_network_gossip::TopicNotification, - gossip_engine: &Arc>>, - gossip_validator: &Arc>, - voters: &VoterSet, - | { - if voters.len().get() <= TELEMETRY_VOTERS_LIMIT { - let precommits_signed_by: Vec = - msg.message.auth_data.iter().map(move |(_, a)| { - format!("{}", a) - }).collect(); - - telemetry!(None; CONSENSUS_INFO; "afg.received_commit"; - "contains_precommits_signed_by" => ?precommits_signed_by, - "target_number" => ?msg.message.target_number.clone(), - "target_hash" => ?msg.message.target_hash.clone(), - ); - } - - if let Err(cost) = check_compact_commit::( - &msg.message, - voters, - msg.round, - msg.set_id, - ) { - if let Some(who) = notification.sender { - gossip_engine.lock().report(who, cost); + let mut process_commit = { + let mut telemetry = telemetry.clone(); + move | + msg: FullCommitMessage, + mut notification: sc_network_gossip::TopicNotification, + gossip_engine: &Arc>>, + gossip_validator: &Arc>, + voters: &VoterSet, + | { + if voters.len().get() <= TELEMETRY_VOTERS_LIMIT { + let precommits_signed_by: Vec = + msg.message.auth_data.iter().map(move |(_, a)| { + format!("{}", a) + }).collect(); + + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.received_commit"; + "contains_precommits_signed_by" => ?precommits_signed_by, + "target_number" => ?msg.message.target_number.clone(), + "target_hash" => ?msg.message.target_hash.clone(), + ); } - return None; - } - - let round = msg.round; - let set_id = msg.set_id; - let commit = msg.message; - let finalized_number = commit.target_number; - let gossip_validator = gossip_validator.clone(); - let gossip_engine = gossip_engine.clone(); - let neighbor_sender = neighbor_sender.clone(); - let cb = move |outcome| match outcome { - voter::CommitProcessingOutcome::Good(_) => { - // if it checks out, gossip it. not accounting for - // any discrepancy between the actual ghost and the claimed - // finalized number. - gossip_validator.note_commit_finalized( - round, - set_id, - finalized_number, - |to, neighbor| neighbor_sender.send(to, neighbor), - ); + if let Err(cost) = check_compact_commit::( + &msg.message, + voters, + msg.round, + msg.set_id, + telemetry.clone(), + ) { + if let Some(who) = notification.sender { + gossip_engine.lock().report(who, cost); + } - gossip_engine.lock().gossip_message(topic, notification.message.clone(), false); + return None; } - voter::CommitProcessingOutcome::Bad(_) => { - // report peer and do not gossip. - if let Some(who) = notification.sender.take() { - gossip_engine.lock().report(who, cost::INVALID_COMMIT); + + let round = msg.round; + let set_id = msg.set_id; + let commit = msg.message; + let finalized_number = commit.target_number; + let gossip_validator = gossip_validator.clone(); + let gossip_engine = gossip_engine.clone(); + let neighbor_sender = neighbor_sender.clone(); + let cb = move |outcome| match outcome { + voter::CommitProcessingOutcome::Good(_) => { + // if it checks out, gossip it. not accounting for + // any discrepancy between the actual ghost and the claimed + // finalized number. + gossip_validator.note_commit_finalized( + round, + set_id, + finalized_number, + |to, neighbor| neighbor_sender.send(to, neighbor), + ); + + gossip_engine.lock().gossip_message(topic, notification.message.clone(), false); } - } - }; + voter::CommitProcessingOutcome::Bad(_) => { + // report peer and do not gossip. + if let Some(who) = notification.sender.take() { + gossip_engine.lock().report(who, cost::INVALID_COMMIT); + } + } + }; - let cb = voter::Callback::Work(Box::new(cb)); + let cb = voter::Callback::Work(Box::new(cb)); - Some(voter::CommunicationIn::Commit(round.0, commit, cb)) + Some(voter::CommunicationIn::Commit(round.0, commit, cb)) + } }; let process_catch_up = move | @@ -573,6 +599,7 @@ fn incoming_global( &msg.message, voters, msg.set_id, + telemetry.clone(), ) { if let Some(who) = notification.sender { gossip_engine.lock().report(who, cost); @@ -629,6 +656,7 @@ impl> Clone for NetworkBridge { neighbor_sender: self.neighbor_sender.clone(), neighbor_packet_worker: self.neighbor_packet_worker.clone(), gossip_validator_report_stream: self.gossip_validator_report_stream.clone(), + telemetry: self.telemetry.clone(), } } } @@ -655,6 +683,7 @@ pub(crate) struct OutgoingMessages { sender: mpsc::Sender>, network: Arc>>, has_voted: HasVoted, + telemetry: Option, } impl Unpin for OutgoingMessages {} @@ -717,7 +746,9 @@ impl Sink> for OutgoingMessages ); telemetry!( - None; CONSENSUS_DEBUG; "afg.announcing_blocks_to_voted_peers"; + self.telemetry; + CONSENSUS_DEBUG; + "afg.announcing_blocks_to_voted_peers"; "block" => ?target_hash, "round" => ?self.round, "set_id" => ?self.set_id, ); @@ -756,6 +787,7 @@ fn check_compact_commit( voters: &VoterSet, round: Round, set_id: SetId, + telemetry: Option, ) -> Result<(), ReputationChange> { // 4f + 1 = equivocations from f voters. let f = voters.total_weight() - voters.threshold(); @@ -797,7 +829,12 @@ fn check_compact_commit( &mut buf, ) { debug!(target: "afg", "Bad commit message signature {}", id); - telemetry!(None; CONSENSUS_DEBUG; "afg.bad_commit_msg_signature"; "id" => ?id); + telemetry!( + telemetry; + CONSENSUS_DEBUG; + "afg.bad_commit_msg_signature"; + "id" => ?id, + ); let cost = Misbehavior::BadCommitMessage { signatures_checked: i as i32, blocks_loaded: 0, @@ -817,6 +854,7 @@ fn check_catch_up( msg: &CatchUp, voters: &VoterSet, set_id: SetId, + telemetry: Option, ) -> Result<(), ReputationChange> { // 4f + 1 = equivocations from f voters. let f = voters.total_weight() - voters.threshold(); @@ -867,6 +905,7 @@ fn check_catch_up( set_id: SetIdNumber, mut signatures_checked: usize, buf: &mut Vec, + telemetry: Option, ) -> Result where B: BlockT, I: Iterator, &'a AuthorityId, &'a AuthoritySignature)>, @@ -885,7 +924,12 @@ fn check_catch_up( buf, ) { debug!(target: "afg", "Bad catch up message signature {}", id); - telemetry!(None; CONSENSUS_DEBUG; "afg.bad_catch_up_msg_signature"; "id" => ?id); + telemetry!( + telemetry; + CONSENSUS_DEBUG; + "afg.bad_catch_up_msg_signature"; + "id" => ?id, + ); let cost = Misbehavior::BadCatchUpMessage { signatures_checked: signatures_checked as i32, @@ -909,6 +953,7 @@ fn check_catch_up( set_id.0, 0, &mut buf, + telemetry.clone(), )?; // check signatures on all contained precommits. @@ -920,6 +965,7 @@ fn check_catch_up( set_id.0, signatures_checked, &mut buf, + telemetry, )?; Ok(()) @@ -932,6 +978,7 @@ struct CommitsOut { is_voter: bool, gossip_validator: Arc>, neighbor_sender: periodic::NeighborPacketSender, + telemetry: Option, } impl CommitsOut { @@ -942,6 +989,7 @@ impl CommitsOut { is_voter: bool, gossip_validator: Arc>, neighbor_sender: periodic::NeighborPacketSender, + telemetry: Option, ) -> Self { CommitsOut { network, @@ -949,6 +997,7 @@ impl CommitsOut { is_voter, gossip_validator, neighbor_sender, + telemetry, } } } @@ -968,8 +1017,12 @@ impl Sink<(RoundNumber, Commit)> for CommitsOut { let (round, commit) = input; let round = Round(round); - telemetry!(None; CONSENSUS_DEBUG; "afg.commit_issued"; - "target_number" => ?commit.target_number, "target_hash" => ?commit.target_hash, + telemetry!( + self.telemetry; + CONSENSUS_DEBUG; + "afg.commit_issued"; + "target_number" => ?commit.target_number, + "target_hash" => ?commit.target_hash, ); let (precommits, auth_data) = commit.precommits.into_iter() .map(|signed| (signed.precommit, (signed.signature, signed.id))) diff --git a/client/finality-grandpa/src/communication/tests.rs b/client/finality-grandpa/src/communication/tests.rs index 33e73e2f7a3b2..dc37a1615f415 100644 --- a/client/finality-grandpa/src/communication/tests.rs +++ b/client/finality-grandpa/src/communication/tests.rs @@ -190,6 +190,7 @@ pub(crate) fn make_test_network() -> ( config(), voter_set_state(), None, + None, ); ( diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index c4e6adaabb2aa..c7084450340ea 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -747,7 +747,7 @@ where config.clone(), persistent_data.set_state.clone(), prometheus_registry.as_ref(), - //telemetry.clone(), + telemetry.clone(), ); let conf = config.clone(); diff --git a/client/finality-grandpa/src/observer.rs b/client/finality-grandpa/src/observer.rs index ff1b6c359ff5a..4707ffb9a3d07 100644 --- a/client/finality-grandpa/src/observer.rs +++ b/client/finality-grandpa/src/observer.rs @@ -184,7 +184,7 @@ where config.clone(), persistent_data.set_state.clone(), None, - //telemetry.clone(), + telemetry.clone(), ); let observer_work = ObserverWork::new( diff --git a/client/finality-grandpa/src/tests.rs b/client/finality-grandpa/src/tests.rs index 17992a0c48a67..42b5300d43e0d 100644 --- a/client/finality-grandpa/src/tests.rs +++ b/client/finality-grandpa/src/tests.rs @@ -944,7 +944,7 @@ fn voter_persists_its_votes() { config.clone(), set_state, None, - //None, + None, ) }; @@ -1346,7 +1346,7 @@ where config.clone(), set_state.clone(), None, - //None, + None, ); Environment { From b48c01e644d7c4910261e95a48f98a59a7005792 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 15:46:49 +0100 Subject: [PATCH 85/92] WIP --- .../src/communication/gossip.rs | 52 +++++++++++++++---- .../finality-grandpa/src/communication/mod.rs | 2 +- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/client/finality-grandpa/src/communication/gossip.rs b/client/finality-grandpa/src/communication/gossip.rs index 0cefd5eb6e06a..a6c51f7eeee72 100644 --- a/client/finality-grandpa/src/communication/gossip.rs +++ b/client/finality-grandpa/src/communication/gossip.rs @@ -90,7 +90,7 @@ use sc_network::{ObservedRole, PeerId, ReputationChange}; use parity_scale_codec::{Encode, Decode}; use sp_finality_grandpa::AuthorityId; -use sc_telemetry::{telemetry, CONSENSUS_DEBUG}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG}; use log::{trace, debug}; use sp_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}; use prometheus_endpoint::{CounterVec, Opts, PrometheusError, register, Registry, U64}; @@ -744,7 +744,7 @@ impl Inner { fn note_set(&mut self, set_id: SetId, authorities: Vec) -> MaybeMessage { { let local_view = match self.local_view { - ref mut x @ None => x.get_or_insert(LocalView::new( + ref mut x @ None => x.get_or_insert(LocalView::new( set_id, Round(1), )), @@ -828,7 +828,12 @@ impl Inner { // ensure authority is part of the set. if !self.authorities.contains(&full.message.id) { debug!(target: "afg", "Message from unknown voter: {}", full.message.id); - telemetry!(None; CONSENSUS_DEBUG; "afg.bad_msg_signature"; "signature" => ?full.message.id); + telemetry!( + self.config.telemetry; + CONSENSUS_DEBUG; + "afg.bad_msg_signature"; + "signature" => ?full.message.id, + ); return Action::Discard(cost::UNKNOWN_VOTER); } @@ -840,7 +845,12 @@ impl Inner { full.set_id.0, ) { debug!(target: "afg", "Bad message signature {}", full.message.id); - telemetry!(None; CONSENSUS_DEBUG; "afg.bad_msg_signature"; "signature" => ?full.message.id); + telemetry!( + self.config.telemetry; + CONSENSUS_DEBUG; + "afg.bad_msg_signature"; + "signature" => ?full.message.id, + ); return Action::Discard(cost::BAD_SIGNATURE); } @@ -866,7 +876,10 @@ impl Inner { if full.message.precommits.len() != full.message.auth_data.len() || full.message.precommits.is_empty() { debug!(target: "afg", "Malformed compact commit"); - telemetry!(None; CONSENSUS_DEBUG; "afg.malformed_compact_commit"; + telemetry!( + self.config.telemetry; + CONSENSUS_DEBUG; + "afg.malformed_compact_commit"; "precommits_len" => ?full.message.precommits.len(), "auth_data_len" => ?full.message.auth_data.len(), "precommits_is_empty" => ?full.message.precommits.is_empty(), @@ -1277,6 +1290,7 @@ pub(super) struct GossipValidator { set_state: environment::SharedVoterSetState, report_sender: TracingUnboundedSender, metrics: Option, + telemetry: Option, } impl GossipValidator { @@ -1287,6 +1301,7 @@ impl GossipValidator { config: crate::Config, set_state: environment::SharedVoterSetState, prometheus_registry: Option<&Registry>, + telemetry: Option, ) -> (GossipValidator, TracingUnboundedReceiver) { let metrics = match prometheus_registry.map(Metrics::register) { Some(Ok(metrics)) => Some(metrics), @@ -1303,6 +1318,7 @@ impl GossipValidator { set_state, report_sender: tx, metrics, + telemetry, }; (val, rx) @@ -1411,7 +1427,12 @@ impl GossipValidator { Err(e) => { message_name = None; debug!(target: "afg", "Error decoding message: {}", e); - telemetry!(None; CONSENSUS_DEBUG; "afg.err_decoding_msg"; "" => ""); + telemetry!( + self.telemetry; + CONSENSUS_DEBUG; + "afg.err_decoding_msg"; + "" => "", + ); let len = std::cmp::min(i32::max_value() as usize, data.len()) as i32; Action::Discard(Misbehavior::UndecodablePacket(len).cost()) @@ -1798,6 +1819,7 @@ mod tests { config(), voter_set_state(), None, + None, ); let set_id = 1; @@ -1834,6 +1856,7 @@ mod tests { config(), voter_set_state(), None, + None, ); let set_id = 1; let auth = AuthorityId::from_slice(&[1u8; 32]); @@ -1879,6 +1902,7 @@ mod tests { config(), voter_set_state(), None, + None, ); let set_id = 1; @@ -1948,6 +1972,7 @@ mod tests { config(), set_state.clone(), None, + None, ); let set_id = 1; @@ -2003,6 +2028,7 @@ mod tests { config(), set_state.clone(), None, + None, ); // the validator starts at set id 2 @@ -2083,6 +2109,7 @@ mod tests { config(), voter_set_state(), None, + None, ); // the validator starts at set id 1. @@ -2157,6 +2184,7 @@ mod tests { config, voter_set_state(), None, + None, ); // the validator starts at set id 1. @@ -2191,6 +2219,7 @@ mod tests { config(), voter_set_state(), None, + None, ); // the validator starts at set id 1. @@ -2251,6 +2280,7 @@ mod tests { config, voter_set_state(), None, + None, ); // the validator starts at set id 1. @@ -2290,6 +2320,7 @@ mod tests { config(), voter_set_state(), None, + None, ); // the validator starts at set id 1. @@ -2323,6 +2354,7 @@ mod tests { config, voter_set_state(), None, + None, ); // the validator start at set id 0 @@ -2402,6 +2434,7 @@ mod tests { config(), voter_set_state(), None, + None, ); // the validator start at set id 0 @@ -2442,6 +2475,7 @@ mod tests { config, voter_set_state(), None, + None, ); // the validator start at set id 0 @@ -2491,7 +2525,7 @@ mod tests { #[test] fn only_gossip_commits_to_peers_on_same_set() { - let (val, _) = GossipValidator::::new(config(), voter_set_state(), None); + let (val, _) = GossipValidator::::new(config(), voter_set_state(), None, None); // the validator start at set id 1 val.note_set(SetId(1), Vec::new(), |_, _| {}); @@ -2569,7 +2603,7 @@ mod tests { #[test] fn expire_commits_from_older_rounds() { - let (val, _) = GossipValidator::::new(config(), voter_set_state(), None); + let (val, _) = GossipValidator::::new(config(), voter_set_state(), None, None); let commit = |round, set_id, target_number| { let commit = finality_grandpa::CompactCommit { @@ -2620,7 +2654,7 @@ mod tests { #[test] fn allow_noting_different_authorities_for_same_set() { - let (val, _) = GossipValidator::::new(config(), voter_set_state(), None); + let (val, _) = GossipValidator::::new(config(), voter_set_state(), None, None); let a1 = vec![AuthorityId::from_slice(&[0; 32])]; val.note_set(SetId(1), a1.clone(), |_, _| {}); diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index 4c7f1d6ff9111..50b1affbfef35 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -214,7 +214,7 @@ impl> NetworkBridge { config, set_state.clone(), prometheus_registry, - //telemetry.clone(), + telemetry.clone(), ); let validator = Arc::new(validator); From d591bb41d29c0c9b6000f3530ebfb04a4426dc33 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 16:50:30 +0100 Subject: [PATCH 86/92] WIP --- client/finality-grandpa/src/environment.rs | 5 ++++- client/finality-grandpa/src/import.rs | 2 +- client/finality-grandpa/src/lib.rs | 4 ++-- client/finality-grandpa/src/observer.rs | 2 +- client/finality-grandpa/src/tests.rs | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/client/finality-grandpa/src/environment.rs b/client/finality-grandpa/src/environment.rs index fbeaa89c6a882..46e17c28583d9 100644 --- a/client/finality-grandpa/src/environment.rs +++ b/client/finality-grandpa/src/environment.rs @@ -39,7 +39,7 @@ use sp_runtime::generic::BlockId; use sp_runtime::traits::{ Block as BlockT, Header as HeaderT, NumberFor, Zero, }; -use sc_telemetry::{telemetry, CONSENSUS_DEBUG, CONSENSUS_INFO}; +use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG, CONSENSUS_INFO}; use crate::{ local_authority_id, CommandOrError, Commit, Config, Error, NewAuthoritySet, Precommit, Prevote, @@ -445,6 +445,7 @@ pub(crate) struct Environment, SC, pub(crate) voting_rule: VR, pub(crate) metrics: Option, pub(crate) justification_sender: Option>, + pub(crate) telemetry: Option, pub(crate) _phantom: PhantomData, } @@ -1146,6 +1147,7 @@ where (round, commit).into(), false, self.justification_sender.as_ref(), + self.telemetry.clone(), ) } @@ -1210,6 +1212,7 @@ pub(crate) fn finalize_block( justification_or_commit: JustificationOrCommit, initial_sync: bool, justification_sender: Option<&GrandpaJustificationSender>, + telemetry: Option, ) -> Result<(), CommandOrError>> where Block: BlockT, diff --git a/client/finality-grandpa/src/import.rs b/client/finality-grandpa/src/import.rs index d15d084b45125..22d7b7fd5bcc8 100644 --- a/client/finality-grandpa/src/import.rs +++ b/client/finality-grandpa/src/import.rs @@ -658,7 +658,7 @@ where justification.into(), initial_sync, Some(&self.justification_sender), - //self.telemetry.clone(), + self.telemetry.clone(), ); match result { diff --git a/client/finality-grandpa/src/lib.rs b/client/finality-grandpa/src/lib.rs index c7084450340ea..c6c2a39674b8c 100644 --- a/client/finality-grandpa/src/lib.rs +++ b/client/finality-grandpa/src/lib.rs @@ -891,7 +891,7 @@ where voter_set_state: persistent_data.set_state, metrics: metrics.as_ref().map(|m| m.environment.clone()), justification_sender: Some(justification_sender), - //telemetry: telemetry.clone(), + telemetry: telemetry.clone(), _phantom: PhantomData, }); @@ -1048,7 +1048,7 @@ where voting_rule: self.env.voting_rule.clone(), metrics: self.env.metrics.clone(), justification_sender: self.env.justification_sender.clone(), - //telemetry: self.telemetry.clone(), + telemetry: self.telemetry.clone(), _phantom: PhantomData, }); diff --git a/client/finality-grandpa/src/observer.rs b/client/finality-grandpa/src/observer.rs index 4707ffb9a3d07..c0eab15e4f455 100644 --- a/client/finality-grandpa/src/observer.rs +++ b/client/finality-grandpa/src/observer.rs @@ -123,7 +123,7 @@ where (round, commit).into(), false, justification_sender.as_ref(), - //telemetry.clone(), + telemetry.clone(), ) { Ok(_) => {}, Err(e) => return future::err(e), diff --git a/client/finality-grandpa/src/tests.rs b/client/finality-grandpa/src/tests.rs index 42b5300d43e0d..6824a8ed04273 100644 --- a/client/finality-grandpa/src/tests.rs +++ b/client/finality-grandpa/src/tests.rs @@ -1361,7 +1361,7 @@ where voting_rule, metrics: None, justification_sender: None, - //telemetry: None, + telemetry: None, _phantom: PhantomData, } } From 8228e0cb6199b560641afe56aa78a6bc0339b0da Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 16:56:28 +0100 Subject: [PATCH 87/92] WIP --- client/finality-grandpa/src/environment.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/client/finality-grandpa/src/environment.rs b/client/finality-grandpa/src/environment.rs index 46e17c28583d9..5bb525549b18c 100644 --- a/client/finality-grandpa/src/environment.rs +++ b/client/finality-grandpa/src/environment.rs @@ -892,7 +892,10 @@ where }; let report_prevote_metrics = |prevote: &Prevote| { - telemetry!(None; CONSENSUS_DEBUG; "afg.prevote_issued"; + telemetry!( + self.telemetry; + CONSENSUS_DEBUG; + "afg.prevote_issued"; "round" => round, "target_number" => ?prevote.target_number, "target_hash" => ?prevote.target_hash, @@ -951,7 +954,10 @@ where }; let report_precommit_metrics = |precommit: &Precommit| { - telemetry!(None; CONSENSUS_DEBUG; "afg.precommit_issued"; + telemetry!( + self.telemetry; + CONSENSUS_DEBUG; + "afg.precommit_issued"; "round" => round, "target_number" => ?precommit.target_number, "target_hash" => ?precommit.target_hash, @@ -1324,7 +1330,10 @@ where warn!(target: "afg", "Error applying finality to block {:?}: {:?}", (hash, number), e); e })?; - telemetry!(None; CONSENSUS_INFO; "afg.finalized_blocks_up_to"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.finalized_blocks_up_to"; "number" => ?number, "hash" => ?hash, ); @@ -1344,7 +1353,10 @@ where ); } - telemetry!(None; CONSENSUS_INFO; "afg.generating_new_authority_set"; + telemetry!( + telemetry; + CONSENSUS_INFO; + "afg.generating_new_authority_set"; "number" => ?canon_number, "hash" => ?canon_hash, "authorities" => ?set_ref.to_vec(), "set_id" => ?new_id, From f10ae74d4746514fc373c4baee9f4e7deac5d46a Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 17:02:59 +0100 Subject: [PATCH 88/92] WIP --- client/finality-grandpa/src/authorities.rs | 2 +- client/finality-grandpa/src/communication/mod.rs | 4 ++-- client/telemetry/src/lib.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/client/finality-grandpa/src/authorities.rs b/client/finality-grandpa/src/authorities.rs index 2edede186a324..1854a33d29f1f 100644 --- a/client/finality-grandpa/src/authorities.rs +++ b/client/finality-grandpa/src/authorities.rs @@ -516,7 +516,7 @@ where finalized_number: N, is_descendent_of: &F, initial_sync: bool, - telemetry: Option, + telemetry: Option<&TelemetryHandle>, ) -> Result, Error> where F: Fn(&H, &H) -> Result, diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index 50b1affbfef35..f8177d6ab08b9 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -511,8 +511,8 @@ fn incoming_global( neighbor_sender: periodic::NeighborPacketSender, telemetry: Option, ) -> impl Stream> { - let mut process_commit = { - let mut telemetry = telemetry.clone(); + let process_commit = { + let telemetry = telemetry.clone(); move | msg: FullCommitMessage, mut notification: sc_network_gossip::TopicNotification, diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 4e905b1e6d49f..65ec3e5c3673e 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -498,7 +498,7 @@ enum Register { #[macro_export(local_inner_macros)] macro_rules! telemetry { ( $telemetry:expr; $verbosity:expr; $msg:expr; $( $t:tt )* ) => {{ - let telemetry: Option<&$crate::TelemetryHandle> = $telemetry.as_ref(); + let telemetry = $telemetry.as_ref(); if let Some(telemetry) = telemetry { let verbosity: $crate::VerbosityLevel = $verbosity; match format_fields_to_json!($($t)*) { From 450cebe4e339973770be72bde9501bfdb2eccf93 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 17:04:03 +0100 Subject: [PATCH 89/92] WIP --- client/finality-grandpa/src/communication/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/finality-grandpa/src/communication/mod.rs b/client/finality-grandpa/src/communication/mod.rs index f8177d6ab08b9..0d287cc96e535 100644 --- a/client/finality-grandpa/src/communication/mod.rs +++ b/client/finality-grandpa/src/communication/mod.rs @@ -541,7 +541,7 @@ fn incoming_global( voters, msg.round, msg.set_id, - telemetry.clone(), + telemetry.as_ref(), ) { if let Some(who) = notification.sender { gossip_engine.lock().report(who, cost); @@ -787,7 +787,7 @@ fn check_compact_commit( voters: &VoterSet, round: Round, set_id: SetId, - telemetry: Option, + telemetry: Option<&TelemetryHandle>, ) -> Result<(), ReputationChange> { // 4f + 1 = equivocations from f voters. let f = voters.total_weight() - voters.threshold(); From 9fad6f8487a77cdfbfbbd5c52eee77d03506ecb4 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 17:05:20 +0100 Subject: [PATCH 90/92] WIP --- client/telemetry/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 65ec3e5c3673e..01b9e593043ce 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -498,8 +498,7 @@ enum Register { #[macro_export(local_inner_macros)] macro_rules! telemetry { ( $telemetry:expr; $verbosity:expr; $msg:expr; $( $t:tt )* ) => {{ - let telemetry = $telemetry.as_ref(); - if let Some(telemetry) = telemetry { + if let Some(telemetry) = $telemetry.as_ref() { let verbosity: $crate::VerbosityLevel = $verbosity; match format_fields_to_json!($($t)*) { Err(err) => { From a0d2206224fb92581713ac0fc645577bf1326ce2 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 4 Mar 2021 19:57:35 +0100 Subject: [PATCH 91/92] Fix doc test --- client/telemetry/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index 01b9e593043ce..b34cf3b168c48 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -486,6 +486,7 @@ enum Register { /// # let authority_id = 42_u64; /// # let set_id = (43_u64, 44_u64); /// # let authorities = vec![45_u64]; +/// # let telemetry: Option = None; /// telemetry!( /// telemetry; // an `Option` /// CONSENSUS_INFO; From e1881e00640a5339816fa0412a65128bea1e9077 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 5 Mar 2021 09:53:35 +0100 Subject: [PATCH 92/92] Add a method with_transport for when transport is needed --- bin/node-template/node/src/service.rs | 4 ++-- bin/node/cli/src/service.rs | 4 ++-- client/telemetry/src/lib.rs | 20 +++++++++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/bin/node-template/node/src/service.rs b/bin/node-template/node/src/service.rs index 3c1eb5e9272aa..669892ea875d8 100644 --- a/bin/node-template/node/src/service.rs +++ b/bin/node-template/node/src/service.rs @@ -49,7 +49,7 @@ pub fn new_partial(config: &Configuration) -> Result Result<_, sc_telemetry::Error> { - let worker = TelemetryWorker::new(16, None)?; + let worker = TelemetryWorker::new(16)?; let telemetry = worker.handle().new_telemetry(endpoints); Ok((worker, telemetry)) }) @@ -289,7 +289,7 @@ pub fn new_light(mut config: Configuration) -> Result let telemetry = config.telemetry_endpoints.clone() .filter(|x| !x.is_empty()) .map(|endpoints| -> Result<_, sc_telemetry::Error> { - let worker = TelemetryWorker::new(16, None)?; + let worker = TelemetryWorker::new(16)?; let telemetry = worker.handle().new_telemetry(endpoints); Ok((worker, telemetry)) }) diff --git a/bin/node/cli/src/service.rs b/bin/node/cli/src/service.rs index 9d3e20b05c307..0b2b500ac984c 100644 --- a/bin/node/cli/src/service.rs +++ b/bin/node/cli/src/service.rs @@ -66,7 +66,7 @@ pub fn new_partial( let telemetry = config.telemetry_endpoints.clone() .filter(|x| !x.is_empty()) .map(|endpoints| -> Result<_, sc_telemetry::Error> { - let worker = TelemetryWorker::new(16, None)?; + let worker = TelemetryWorker::new(16)?; let telemetry = worker.handle().new_telemetry(endpoints); Ok((worker, telemetry)) }) @@ -413,7 +413,7 @@ pub fn new_light_base( #[cfg(not(feature = "browser"))] let transport = None; - let worker = TelemetryWorker::new(16, transport)?; + let worker = TelemetryWorker::with_transport(16, transport)?; let telemetry = worker.handle().new_telemetry(endpoints); Ok((worker, telemetry)) }) diff --git a/client/telemetry/src/lib.rs b/client/telemetry/src/lib.rs index b34cf3b168c48..8d3b605db01a5 100644 --- a/client/telemetry/src/lib.rs +++ b/client/telemetry/src/lib.rs @@ -121,7 +121,25 @@ impl TelemetryWorker { /// Instantiate a new [`TelemetryWorker`] which can run in background. /// /// Only one is needed per process. - pub fn new(buffer_size: usize, transport: Option) -> Result { + pub fn new(buffer_size: usize) -> Result { + let transport = initialize_transport(None)?; + let (message_sender, message_receiver) = mpsc::channel(buffer_size); + let (register_sender, register_receiver) = mpsc::unbounded(); + + Ok(Self { + message_receiver, + message_sender, + register_receiver, + register_sender, + id_counter: Arc::new(atomic::AtomicU64::new(1)), + transport, + }) + } + + /// Instantiate a new [`TelemetryWorker`] which can run in background. + /// + /// Only one is needed per process. + pub fn with_transport(buffer_size: usize, transport: Option) -> Result { let transport = initialize_transport(transport)?; let (message_sender, message_receiver) = mpsc::channel(buffer_size); let (register_sender, register_receiver) = mpsc::unbounded();