Skip to content

Commit

Permalink
Merge pull request #2167 from RolandSherwin/metrics_shunned_close
Browse files Browse the repository at this point in the history
feat(metrics):  add close group shunned metrics
  • Loading branch information
RolandSherwin authored Oct 6, 2024
2 parents d3ed939 + f050a0e commit c717713
Show file tree
Hide file tree
Showing 4 changed files with 461 additions and 50 deletions.
11 changes: 11 additions & 0 deletions sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,8 @@ impl NetworkBuilder {
local: self.local,
is_client,
is_behind_home_network: self.is_behind_home_network,
#[cfg(feature = "open-metrics")]
close_group: Vec::with_capacity(CLOSE_GROUP_SIZE),
peers_in_rt: 0,
bootstrap,
relay_manager,
Expand Down Expand Up @@ -715,6 +717,8 @@ pub struct SwarmDriver {
pub(crate) local: bool,
pub(crate) is_client: bool,
pub(crate) is_behind_home_network: bool,
#[cfg(feature = "open-metrics")]
pub(crate) close_group: Vec<PeerId>,
pub(crate) peers_in_rt: usize,
pub(crate) bootstrap: ContinuousBootstrap,
pub(crate) external_address_manager: ExternalAddressManager,
Expand Down Expand Up @@ -991,6 +995,13 @@ impl SwarmDriver {
metrics_recorder.record_from_marker(marker)
}
}
#[cfg(feature = "open-metrics")]
/// Updates metrics that rely on our current close group.
pub(crate) fn record_change_in_close_group(&self, new_close_group: Vec<PeerId>) {
if let Some(metrics_recorder) = self.metrics_recorder.as_ref() {
metrics_recorder.record_change_in_close_group(new_close_group);
}
}

/// Listen on the provided address. Also records it within RelayManager
pub(crate) fn listen_on(&mut self, addr: Multiaddr) -> Result<()> {
Expand Down
36 changes: 34 additions & 2 deletions sn_networking/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ use libp2p::{

use sn_protocol::{
messages::{Query, Request, Response},
NetworkAddress, PrettyPrintRecordKey,
NetworkAddress, PrettyPrintRecordKey, CLOSE_GROUP_SIZE,
};
use sn_transfers::PaymentQuote;
use std::{
collections::BTreeSet,
collections::{BTreeSet, HashSet},
fmt::{Debug, Formatter},
};
use tokio::sync::oneshot;
Expand Down Expand Up @@ -216,6 +216,28 @@ impl Debug for NetworkEvent {
}

impl SwarmDriver {
/// Check for changes in our close group
#[cfg(feature = "open-metrics")]
pub(crate) fn check_for_change_in_our_close_group(&mut self) {
// this includes self
let closest_k_peers = self.get_closest_k_value_local_peers();

let new_closest_peers: Vec<_> =
closest_k_peers.into_iter().take(CLOSE_GROUP_SIZE).collect();

let old = self.close_group.iter().cloned().collect::<HashSet<_>>();
let new_members: Vec<_> = new_closest_peers
.iter()
.filter(|p| !old.contains(p))
.collect();
if !new_members.is_empty() {
debug!("The close group has been updated. The new members are {new_members:?}");
debug!("New close group: {new_closest_peers:?}");
self.close_group = new_closest_peers.clone();
self.record_change_in_close_group(new_closest_peers);
}
}

/// Update state on addition of a peer to the routing table.
pub(crate) fn update_on_peer_addition(&mut self, added_peer: PeerId) {
self.peers_in_rt = self.peers_in_rt.saturating_add(1);
Expand All @@ -226,6 +248,11 @@ impl SwarmDriver {
self.log_kbuckets(&added_peer);
self.send_event(NetworkEvent::PeerAdded(added_peer, self.peers_in_rt));

#[cfg(feature = "open-metrics")]
if self.metrics_recorder.is_some() {
self.check_for_change_in_our_close_group();
}

#[cfg(feature = "open-metrics")]
if let Some(metrics_recorder) = &self.metrics_recorder {
metrics_recorder
Expand All @@ -244,6 +271,11 @@ impl SwarmDriver {
self.log_kbuckets(&removed_peer);
self.send_event(NetworkEvent::PeerRemoved(removed_peer, self.peers_in_rt));

#[cfg(feature = "open-metrics")]
if self.metrics_recorder.is_some() {
self.check_for_change_in_our_close_group();
}

#[cfg(feature = "open-metrics")]
if let Some(metrics_recorder) = &self.metrics_recorder {
metrics_recorder
Expand Down
Loading

0 comments on commit c717713

Please sign in to comment.