Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Add a metric for original count of rep changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiEres committed Jun 7, 2023
1 parent 175b9f1 commit dda4119
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
13 changes: 11 additions & 2 deletions node/network/bridge/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ impl Metrics {
});
}

pub fn on_report_event(&self) {
pub fn on_report_event(&self, by: i32) {
if let Some(metrics) = self.0.as_ref() {
metrics.report_events.inc()
metrics.report_events.inc();
metrics.aggregated_report_events.inc_by(by);
}
}
}
Expand All @@ -117,6 +118,7 @@ pub(crate) struct MetricsInner {
disconnected_events: prometheus::CounterVec<prometheus::U64>,
desired_peer_count: prometheus::GaugeVec<prometheus::U64>,
report_events: prometheus::Counter<prometheus::U64>,
aggregated_report_events: prometheus::Counter<prometheus::U64>,

notifications_received: prometheus::CounterVec<prometheus::U64>,
notifications_sent: prometheus::CounterVec<prometheus::U64>,
Expand Down Expand Up @@ -177,6 +179,13 @@ impl metrics::Metrics for Metrics {
)?,
registry,
)?,
aggregated_report_events: prometheus::register(
prometheus::Counter::new(
"polkadot_parachain_network_aggregated_report_events_total",
"The amount of original reputation changes issued by subsystems",
)?,
registry,
)?,
notifications_received: prometheus::register(
prometheus::CounterVec::new(
prometheus::Opts::new(
Expand Down
14 changes: 4 additions & 10 deletions node/network/bridge/src/tx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,17 @@ where
gum::debug!(target: LOG_TARGET, ?peer, ?rep, action = "ReportPeer");
}

metrics.on_report_event();
metrics.on_report_event(1);
network_service.report_peer(peer, rep);
},
NetworkBridgeTxMessage::ReportPeer(ReportPeerMessage::Batch(batch)) => {
let reports: Vec<(PeerId, ReputationChange)> = batch
.iter()
.map(|(&peer, &score)| {
(peer, ReputationChange::new(score, "Aggregated reputation change"))
})
.collect();

for (peer, rep) in reports {
for (peer, (score, counter)) in batch {
let rep = ReputationChange::new(score, "Aggregated reputation change");
if !rep.value.is_positive() {
gum::debug!(target: LOG_TARGET, ?peer, ?rep, action = "ReportPeer");
}

metrics.on_report_event();
metrics.on_report_event(counter);
network_service.report_peer(peer, rep);
}
},
Expand Down
2 changes: 1 addition & 1 deletion node/subsystem-types/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ pub enum ReportPeerMessage {
/// Single peer report about malicious actions which should be sent right away
Single(PeerId, ReputationChange),
/// Delayed report for other actions.
Batch(HashMap<PeerId, i32>),
Batch(HashMap<PeerId, (i32, u64)>),
}

/// Messages received from other subsystems by the network bridge subsystem.
Expand Down
12 changes: 10 additions & 2 deletions node/subsystem-util/src/reputation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ use std::{collections::HashMap, time::Duration};
/// Default delay for sending reputation changes
pub const REPUTATION_CHANGE_INTERVAL: Duration = Duration::from_secs(30);

type BatchReputationChange = HashMap<PeerId, i32>;
type BatchReputationChange = HashMap<
PeerId,
(
i32, // Aggregated value
u64, // Counter
),
>;

/// Collects reputation changes and sends them in one batch to relieve network channels
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -113,5 +119,7 @@ pub fn add_reputation(
rep: UnifiedReputationChange,
) {
let cost = rep.cost_or_benefit();
acc.entry(peer_id).and_modify(|v| *v = v.saturating_add(cost)).or_insert(cost);
acc.entry(peer_id)
.and_modify(|v| *v = (v.0.saturating_add(cost), v.1 + 1))
.or_insert((cost, 1));
}

0 comments on commit dda4119

Please sign in to comment.