Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sishan/Revoke metric for tracking the number of peers. #1802

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/hotshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ sha3 = "^0.10"
snafu = { workspace = true }
surf-disco = { workspace = true }
time = { workspace = true }
dyn-clone = { git = "https://github.com/dtolnay/dyn-clone", tag = "1.0.14" }

tracing = { workspace = true }
typenum = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/hotshot/src/traits/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ pub mod libp2p_network;
pub mod memory_network;
pub mod web_server_libp2p_fallback;
pub mod web_server_network;

use custom_debug::Debug;
use hotshot_types::traits::metrics::{Counter, Gauge, Metrics};
pub use hotshot_types::traits::network::{
ChannelSendSnafu, CouldNotDeliverSnafu, FailedToDeserializeSnafu, FailedToSerializeSnafu,
NetworkError, NetworkReliability, NoSuchNodeSnafu, ShutDownSnafu,
};

use hotshot_types::traits::metrics::{Counter, Gauge, Metrics};

/// Contains the metrics that we're interested in from the networking interfaces
#[derive(Clone, Debug)]
pub struct NetworkingMetrics {
#[allow(dead_code)]
/// A [`Gauge`] which tracks how many peers are connected
Expand Down
12 changes: 9 additions & 3 deletions crates/hotshot/src/traits/networking/libp2p_network.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Libp2p based/production networking implementation
//! This module provides a libp2p based networking implementation where each node in the
//! network forms a tcp or udp connection to a subset of other nodes in the network

use super::NetworkingMetrics;
use crate::NodeImplementation;
use async_compatibility_layer::{
Expand Down Expand Up @@ -78,6 +77,7 @@ impl<M: NetworkMsg, K: SignatureKey + 'static> Debug for Libp2pNetwork<M, K> {
pub type PeerInfoVec = Arc<RwLock<Vec<(Option<PeerId>, Multiaddr)>>>;

/// The underlying state of the libp2p network
#[derive(Debug)]
struct Libp2pNetworkInner<M: NetworkMsg, K: SignatureKey + 'static> {
/// this node's public key
pk: K,
Expand Down Expand Up @@ -320,7 +320,7 @@ impl<M: NetworkMsg, K: SignatureKey + 'static> Libp2pNetwork<M, K> {
let (node_lookup_send, node_lookup_recv) = unbounded();
let (cache_gc_shutdown_send, cache_gc_shutdown_recv) = unbounded::<()>();

let result = Libp2pNetwork {
let mut result = Libp2pNetwork {
inner: Arc::new(Libp2pNetworkInner {
handle: network_handle,
broadcast_recv,
Expand Down Expand Up @@ -404,13 +404,14 @@ impl<M: NetworkMsg, K: SignatureKey + 'static> Libp2pNetwork<M, K> {
}

/// Initiates connection to the outside world
fn spawn_connect(&self, id: usize) {
fn spawn_connect(&mut self, id: usize) {
let pk = self.inner.pk.clone();
let bootstrap_ref = self.inner.bootstrap_addrs.clone();
let num_bootstrap = self.inner.bootstrap_addrs_len;
let handle = self.inner.handle.clone();
let is_bootstrapped = self.inner.is_bootstrapped.clone();
let node_type = self.inner.handle.config().node_type;
let metrics_connected_peers = self.inner.clone();
async_spawn({
let is_ready = self.inner.is_ready.clone();
async move {
Expand Down Expand Up @@ -439,6 +440,11 @@ impl<M: NetworkMsg, K: SignatureKey + 'static> Libp2pNetwork<M, K> {
.await
.unwrap();

let connected_num = handle.num_connected().await?;
metrics_connected_peers
.metrics
.connected_peers
.set(connected_num);
while !is_bootstrapped.load(Ordering::Relaxed) {
async_sleep(Duration::from_secs(1)).await;
}
Expand Down
1 change: 1 addition & 0 deletions crates/hotshot/src/traits/networking/memory_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ enum Combo<T> {
}

/// Internal state for a `MemoryNetwork` instance
#[derive(Debug)]
struct MemoryNetworkInner<M: NetworkMsg, K: SignatureKey> {
/// Input for broadcast messages
broadcast_input: RwLock<Option<Sender<Vec<u8>>>>,
Expand Down
1 change: 1 addition & 0 deletions crates/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ time = { workspace = true }
tracing = { workspace = true }
ethereum-types = { workspace = true }
typenum = { workspace = true }
dyn-clone = { git = "https://github.com/dtolnay/dyn-clone", tag = "1.0.14" }

[dev-dependencies]
serde_json = "1.0.107"
Expand Down
15 changes: 10 additions & 5 deletions crates/types/src/traits/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! - [`Histogram`]: stores multiple float values based for a graph (example usage: CPU %)
//! - [`Label`]: Stores the last string (example usage: current version, network online/offline)

use dyn_clone::DynClone;
use std::fmt::Debug;

/// The metrics type.
Expand Down Expand Up @@ -78,12 +79,12 @@ impl Label for NoMetrics {
}

/// An ever-incrementing counter
pub trait Counter: Send + Sync + Debug {
pub trait Counter: Send + Sync + Debug + DynClone {
/// Add a value to the counter
fn add(&self, amount: usize);
}
/// A gauge that stores the latest value.
pub trait Gauge: Send + Sync + Debug {
pub trait Gauge: Send + Sync + Debug + DynClone {
/// Set the gauge value
fn set(&self, amount: usize);

Expand All @@ -92,16 +93,20 @@ pub trait Gauge: Send + Sync + Debug {
}

/// A histogram which will record a series of points.
pub trait Histogram: Send + Sync + Debug {
pub trait Histogram: Send + Sync + Debug + DynClone {
/// Add a point to this histogram.
fn add_point(&self, point: f64);
}

/// A label that stores the last string value.
pub trait Label: Send + Sync {
pub trait Label: Send + Sync + DynClone {
/// Set the label value
fn set(&self, value: String);
}
dyn_clone::clone_trait_object!(Gauge);
dyn_clone::clone_trait_object!(Counter);
dyn_clone::clone_trait_object!(Histogram);
dyn_clone::clone_trait_object!(Label);

#[cfg(test)]
mod test {
Expand All @@ -111,7 +116,7 @@ mod test {
sync::{Arc, Mutex},
};

#[derive(Debug)]
#[derive(Debug, Clone)]
struct TestMetrics {
prefix: String,
values: Arc<Mutex<Inner>>,
Expand Down