Skip to content

Commit

Permalink
peer-metrics: makes it more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
coot committed Jul 17, 2023
1 parent 9107249 commit 3f92c57
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
1 change: 1 addition & 0 deletions ouroboros-network/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Limit concurrency used by dns resolution. We only resolve up to 8 dns names
concurrently for public / ledger peers and up to 2 for local root peers.
This will affect how quickly node connects to ledger peers when it starts.
* Improved memory footprint of peer metrics (#4620)

## 0.8.1.1

Expand Down
32 changes: 14 additions & 18 deletions ouroboros-network/src/Ouroboros/Network/PeerSelection/PeerMetric.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,31 @@ data PeerMetricsState p = PeerMetricsState {

-- | Header metrics.
--
headerMetrics :: SlotMetric p,
headerMetrics :: !(SlotMetric p),

-- | Fetch metrics.
--
fetchedMetrics :: SlotMetric (p, SizeInBytes),
fetchedMetrics :: !(SlotMetric (p, SizeInBytes)),

-- | Registry recording when a peer joined the board of 'PeerMetrics'. The
-- values are average header and fetched metrics.
--
peerRegistry :: PeerRegistry p,
peerRegistry :: !(PeerRegistry p),

-- | A registry which indicates when the last time a peer was seen.
--
-- If a peer hasn't been seen since the oldest recorded slot number, it will
-- be removed.
--
lastSeenRegistry :: LastSeenRegistry p,
lastSeenRegistry :: !(LastSeenRegistry p),

-- | Latest slot registered in the leader board.
--
lastSlotNo :: SlotNo,
lastSlotNo :: !SlotNo,

-- | Metrics configuration. Its kept here just for convenience.
--
metricsConfig :: PeerMetricsConfiguration
metricsConfig :: !PeerMetricsConfiguration
}


Expand Down Expand Up @@ -260,27 +260,23 @@ insertPeer :: forall p. Ord p
-> SlotNo -- ^ current slot
-> PeerMetricsState p
-> PeerMetricsState p
insertPeer p slotNo
insertPeer p !slotNo
peerMetricsState@PeerMetricsState { lastSeenRegistry, peerRegistry } =
if p `OrdPSQ.member` lastSeenRegistry
then peerMetricsState
else case OrdPSQ.alter f p peerRegistry of
(False, peerRegistry') -> peerMetricsState { peerRegistry = peerRegistry' }
(False, !peerRegistry') -> peerMetricsState { peerRegistry = peerRegistry' }
(True, _peerRegistry') -> peerMetricsState
where
f :: Maybe (SlotNo, AverageMetrics) -> (Bool, Maybe (SlotNo, AverageMetrics))
f a@Just {} = (True, a)
f Nothing = (False, Just ( slotNo
, AverageMetrics {
averageUpstreamyness = avg upstreamenessResults,
averageFetchynessBytes = avg fetchynessBytesResults,
averageFetchynessBlocks = avg fetchynessBlocksResults
}
))
f Nothing = (False, Just $! (slotNo, metrics))
where
upstreamenessResults = upstreamynessImpl peerMetricsState
fetchynessBytesResults = fetchynessBytesImpl peerMetricsState
fetchynessBlocksResults = fetchynessBlocksImpl peerMetricsState
!metrics = AverageMetrics {
averageUpstreamyness = avg (upstreamynessImpl peerMetricsState),
averageFetchynessBytes = avg (fetchynessBytesImpl peerMetricsState),
averageFetchynessBlocks = avg (fetchynessBlocksImpl peerMetricsState)
}

avg :: Map p Int -> Int
avg m | Map.null m = 0
Expand Down

0 comments on commit 3f92c57

Please sign in to comment.