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

network:bridge: fix peer_count metric #3711

Merged
merged 5 commits into from
Apr 1, 2024
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
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
Loading