Skip to content

Commit

Permalink
network:bridge: fix peer_count metric (paritytech#3711)
Browse files Browse the repository at this point in the history
The metric records the current protocol_version of the validator that
just connected with the peer_map.len(), which contains all peers that
connected, that has the effect the metric will be wrong since it won't
tell us how many peers we have connected per version because it will
always record the total number of peers

Fix this by counting by version inside peer_map, additionally because
that might be a bit heavier than len(), publish it only on-active
leaves.

---------

Signed-off-by: Alexandru Gheorghe <alexandru.gheorghe@parity.io>
  • Loading branch information
alexggh authored and dharjeezy committed Apr 9, 2024
1 parent 28b0988 commit 1990d20
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
24 changes: 24 additions & 0 deletions polkadot/node/network/bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,30 @@ struct SharedInner {
collation_peers: HashMap<PeerId, PeerData>,
}

// Counts the number of peers that are connectioned using `version`
fn count_peers_by_version(peers: &HashMap<PeerId, PeerData>) -> HashMap<ProtocolVersion, usize> {
let mut by_version_count = HashMap::new();
for peer in peers.values() {
*(by_version_count.entry(peer.version).or_default()) += 1;
}
by_version_count
}

// Notes the peer count
fn note_peers_count(metrics: &Metrics, shared: &Shared) {
let guard = shared.0.lock();
let validation_stats = count_peers_by_version(&guard.validation_peers);
let collation_stats = count_peers_by_version(&guard.collation_peers);

for (version, count) in validation_stats {
metrics.note_peer_count(PeerSet::Validation, version, count)
}

for (version, count) in collation_stats {
metrics.note_peer_count(PeerSet::Collation, version, count)
}
}

pub(crate) enum Mode {
Syncing(Box<dyn SyncOracle + Send>),
Active,
Expand Down
6 changes: 1 addition & 5 deletions polkadot/node/network/bridge/src/rx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ async fn handle_validation_message<AD>(
}

metrics.on_peer_connected(peer_set, version);
metrics.note_peer_count(peer_set, version, peer_map.len());

shared.local_view.clone().unwrap_or(View::default())
};
Expand Down Expand Up @@ -320,8 +319,6 @@ async fn handle_validation_message<AD>(
let w = peer_map.remove(&peer).is_some();

metrics.on_peer_disconnected(peer_set, version);
metrics.note_peer_count(peer_set, version, peer_map.len());

w
};

Expand Down Expand Up @@ -524,7 +521,6 @@ async fn handle_collation_message<AD>(
}

metrics.on_peer_connected(peer_set, version);
metrics.note_peer_count(peer_set, version, peer_map.len());

shared.local_view.clone().unwrap_or(View::default())
};
Expand Down Expand Up @@ -575,7 +571,6 @@ async fn handle_collation_message<AD>(
let w = peer_map.remove(&peer).is_some();

metrics.on_peer_disconnected(peer_set, version);
metrics.note_peer_count(peer_set, version, peer_map.len());

w
};
Expand Down Expand Up @@ -832,6 +827,7 @@ where
&metrics,
&notification_sinks,
);
note_peers_count(&metrics, &shared);
}
}
},
Expand Down

0 comments on commit 1990d20

Please sign in to comment.