From 269d3df35188b5a33f1d46539a6b44b710da868e Mon Sep 17 00:00:00 2001 From: Giovanni Petrantoni <7008900+sinkingsugar@users.noreply.github.com> Date: Thu, 4 Mar 2021 06:11:21 +0900 Subject: [PATCH] Consolidate metrics collection for mesh (#540) * Consolidate metrics collection for mesh * more fixes * wrapping up * Update libp2p/protocols/pubsub/gossipsub/behavior.nim Co-authored-by: Jacek Sieka * Update libp2p/protocols/pubsub/gossipsub/behavior.nim Co-authored-by: Jacek Sieka * Update libp2p/protocols/pubsub/gossipsub/behavior.nim Co-authored-by: Jacek Sieka Co-authored-by: Jacek Sieka --- .../protocols/pubsub/gossipsub/behavior.nim | 37 +++++++++---------- libp2p/protocols/pubsub/gossipsub/types.nim | 6 +-- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/libp2p/protocols/pubsub/gossipsub/behavior.nim b/libp2p/protocols/pubsub/gossipsub/behavior.nim index 580d834..1a173b1 100644 --- a/libp2p/protocols/pubsub/gossipsub/behavior.nim +++ b/libp2p/protocols/pubsub/gossipsub/behavior.nim @@ -19,10 +19,10 @@ declareGauge(libp2p_gossipsub_cache_window_size, "the number of messages in the declareGauge(libp2p_gossipsub_peers_per_topic_mesh, "gossipsub peers per topic in mesh", labels = ["topic"]) declareGauge(libp2p_gossipsub_peers_per_topic_fanout, "gossipsub peers per topic in fanout", labels = ["topic"]) declareGauge(libp2p_gossipsub_peers_per_topic_gossipsub, "gossipsub peers per topic in gossipsub", labels = ["topic"]) -declareGauge(libp2p_gossipsub_under_dlow_topics, "number of topics below dlow") declareGauge(libp2p_gossipsub_under_dout_topics, "number of topics below dout") -declareGauge(libp2p_gossipsub_under_dhigh_above_dlow_topics, "number of topics below dhigh but above dlow") -declareGauge(libp2p_gossipsub_no_peers_topics, "number of topics without peers available") +declareGauge(libp2p_gossipsub_no_peers_topics, "number of topics in mesh with no peers") +declareGauge(libp2p_gossipsub_low_peers_topics, "number of topics in mesh with at least one but below dlow peers") +declareGauge(libp2p_gossipsub_healthy_peers_topics, "number of topics in mesh with at least dlow peers (but below dhigh)") declareCounter(libp2p_gossipsub_above_dhigh_condition, "number of above dhigh pruning branches ran", labels = ["topic"]) proc grafted*(g: GossipSub, p: PubSubPeer, topic: string) = @@ -250,10 +250,10 @@ proc handleIWant*(g: GossipSub, return proc commitMetrics(metrics: var MeshMetrics) = - libp2p_gossipsub_under_dlow_topics.set(metrics.underDlowTopics) + libp2p_gossipsub_low_peers_topics.set(metrics.lowPeersTopics) libp2p_gossipsub_no_peers_topics.set(metrics.noPeersTopics) libp2p_gossipsub_under_dout_topics.set(metrics.underDoutTopics) - libp2p_gossipsub_under_dhigh_above_dlow_topics.set(metrics.underDhighAboveDlowTopics) + libp2p_gossipsub_healthy_peers_topics.set(metrics.healthyPeersTopics) libp2p_gossipsub_peers_per_topic_gossipsub.set(metrics.otherPeersPerTopicGossipsub, labelValues = ["other"]) libp2p_gossipsub_peers_per_topic_fanout.set(metrics.otherPeersPerTopicFanout, labelValues = ["other"]) libp2p_gossipsub_peers_per_topic_mesh.set(metrics.otherPeersPerTopicMesh, labelValues = ["other"]) @@ -273,9 +273,6 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) npeers = g.mesh.peers(topic) if npeers < g.parameters.dLow: - if not isNil(metrics): - inc metrics[].underDlowTopics - trace "replenishing mesh", peers = npeers # replenish the mesh if we're below Dlo var candidates = toSeq( @@ -309,16 +306,7 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) g.fanout.removePeer(topic, peer) grafts &= peer - if not isNil(metrics) and g.mesh.peers(topic) == 0: - inc metrics[].noPeersTopics - else: - var meshPeers = toSeq(g.mesh.getOrDefault(topic, initHashSet[PubSubPeer]())) - meshPeers.keepIf do (x: PubSubPeer) -> bool: x.outbound - if meshPeers.len < g.parameters.dOut: - if not isNil(metrics): - inc metrics[].underDoutTopics - trace "replenishing mesh outbound quota", peers = g.mesh.peers(topic) var candidates = toSeq( @@ -409,8 +397,6 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) trace "pruning peer on rebalance", peer, score = peer.score g.pruned(peer, topic) g.mesh.removePeer(topic, peer) - elif npeers > g.parameters.dLow and not isNil(metrics): - inc metrics[].underDhighAboveDlowTopics # opportunistic grafting, by spec mesh should not be empty... if g.mesh.peers(topic) > 1: @@ -445,6 +431,19 @@ proc rebalanceMesh*(g: GossipSub, topic: string, metrics: ptr MeshMetrics = nil) trace "opportunistic grafting", peer if not isNil(metrics): + npeers = g.mesh.peers(topic) + if npeers == 0: + inc metrics[].noPeersTopics + elif npeers < g.parameters.dLow: + inc metrics[].lowPeersTopics + else: + inc metrics[].healthyPeersTopics + + var meshPeers = toSeq(g.mesh.getOrDefault(topic, initHashSet[PubSubPeer]())) + meshPeers.keepIf do (x: PubSubPeer) -> bool: x.outbound + if meshPeers.len < g.parameters.dOut: + inc metrics[].underDoutTopics + if g.knownTopics.contains(topic): libp2p_gossipsub_peers_per_topic_gossipsub .set(g.gossipsub.peers(topic).int64, labelValues = [topic]) diff --git a/libp2p/protocols/pubsub/gossipsub/types.nim b/libp2p/protocols/pubsub/gossipsub/types.nim index c9fced1..bd3a67b 100644 --- a/libp2p/protocols/pubsub/gossipsub/types.nim +++ b/libp2p/protocols/pubsub/gossipsub/types.nim @@ -157,7 +157,7 @@ type otherPeersPerTopicMesh*: int64 otherPeersPerTopicFanout*: int64 otherPeersPerTopicGossipsub*: int64 - underDlowTopics*: int64 + noPeersTopics*: int64 # npeers == 0 + lowPeersTopics*: int64 # npeers < dlow + healthyPeersTopics*: int64 # npeers >= dlow underDoutTopics*: int64 - underDhighAboveDlowTopics*: int64 - noPeersTopics*: int64