Skip to content

Commit

Permalink
feat: misc. updates for discovery network analysis (#2930)
Browse files Browse the repository at this point in the history
added metrics, a way to start without RLN and a new avg latency algorithm
  • Loading branch information
SionoiS authored Aug 7, 2024
1 parent 0f11ee1 commit 4340eb7
Show file tree
Hide file tree
Showing 3 changed files with 827 additions and 16 deletions.
46 changes: 33 additions & 13 deletions apps/networkmonitor/networkmonitor.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{.push raises: [].}

import
std/[tables, strutils, times, sequtils, random],
std/[net, tables, strutils, times, sequtils, random],
results,
stew/shims/net,
chronicles,
chronicles/topics_registry,
chronos,
Expand Down Expand Up @@ -40,7 +39,7 @@ logScope:
const ReconnectTime = 60
const MaxConnectionRetries = 5
const ResetRetriesAfter = 1200
const AvgPingWindow = 10.0
const PingSmoothing = 0.3
const MaxConnectedPeers = 150

const git_version* {.strdefine.} = "n/a"
Expand All @@ -55,6 +54,23 @@ proc setDiscoveredPeersCapabilities(routingTableNodes: seq[waku_enr.Record]) =
int64(nOfNodesWithCapability), labelValues = [$capability]
)

proc setDiscoveredPeersCluster(routingTableNodes: seq[Node]) =
var clusters: CountTable[uint16]

for node in routingTableNodes:
let typedRec = node.record.toTyped().valueOr:
clusters.inc(0)
continue

let relayShard = typedRec.relaySharding().valueOr:
clusters.inc(0)
continue

clusters.inc(relayShard.clusterId)

for (key, value) in clusters.pairs:
networkmonitor_peer_cluster_as_per_enr.set(int64(value), labelValues = [$key])

proc analyzePeer(
customPeerInfo: CustomPeerInfoRef,
peerInfo: RemotePeerInfo,
Expand Down Expand Up @@ -87,16 +103,17 @@ proc analyzePeer(
info "successfully pinged peer", peer = peerInfo, duration = pingDelay.millis
networkmonitor_peer_ping.observe(pingDelay.millis)

if customPeerInfo.avgPingDuration == 0.millis:
customPeerInfo.avgPingDuration = pingDelay
# We are using a smoothed moving average
customPeerInfo.avgPingDuration =
if customPeerInfo.avgPingDuration.millis == 0:
pingDelay
else:
let newAvg =
(float64(pingDelay.millis) * PingSmoothing) +
float64(customPeerInfo.avgPingDuration.millis) * (1.0 - PingSmoothing)

int64(newAvg).millis

# TODO: check why the calculation ends up losing precision
customPeerInfo.avgPingDuration = int64(
(
float64(customPeerInfo.avgPingDuration.millis) * (AvgPingWindow - 1.0) +
float64(pingDelay.millis)
) / AvgPingWindow
).millis
customPeerInfo.lastPingDuration = pingDelay

return ok(customPeerInfo.peerId)
Expand Down Expand Up @@ -306,6 +323,9 @@ proc crawlNetwork(
# populate metrics related to capabilities as advertised by the ENR (see waku field)
setDiscoveredPeersCapabilities(discoveredNodes)

# populate cluster metrics as advertised by the ENR
setDiscoveredPeersCluster(flatNodes)

# tries to connect to all newly discovered nodes
# and populates metrics related to peers we could connect
# note random discovered nodes can be already known
Expand Down Expand Up @@ -589,7 +609,7 @@ when isMainModule:
waitFor node.mountRelay()
waitFor node.mountLibp2pPing()

if conf.rlnRelayEthContractAddress != "":
if conf.rlnRelay and conf.rlnRelayEthContractAddress != "":
let rlnConf = WakuRlnConfig(
rlnRelayDynamic: conf.rlnRelayDynamic,
rlnRelayCredIndex: some(uint(0)),
Expand Down
8 changes: 5 additions & 3 deletions apps/networkmonitor/networkmonitor_metrics.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{.push raises: [].}

import
std/[json, tables, sequtils],
std/[net, json, tables, sequtils],
chronicles,
chronicles/topics_registry,
chronos,
Expand All @@ -10,8 +10,7 @@ import
metrics/chronos_httpserver,
presto/route,
presto/server,
results,
stew/shims/net
results

logScope:
topics = "networkmonitor_metrics"
Expand All @@ -26,6 +25,9 @@ declarePublicGauge networkmonitor_peer_type_as_per_enr,
"Number of peers supporting each capability according to the ENR",
labels = ["capability"]

declarePublicGauge networkmonitor_peer_cluster_as_per_enr,
"Number of peers on each cluster according to the ENR", labels = ["cluster"]

declarePublicGauge networkmonitor_peer_type_as_per_protocol,
"Number of peers supporting each protocol, after a successful connection) ",
labels = ["protocols"]
Expand Down
Loading

0 comments on commit 4340eb7

Please sign in to comment.