Skip to content

Commit

Permalink
Churn BootstrapPeers
Browse files Browse the repository at this point in the history
  • Loading branch information
bolt12 committed Feb 21, 2024
1 parent 58a4f3b commit 05ab38b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 39 deletions.
5 changes: 5 additions & 0 deletions ouroboros-network/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
* Fix `targetPeers` monitoring action to use the correct set of local peers
when in sensitive mode.

* Forget non-established bootstrap peers when transitioning from
`TooOld` state to `YoungEnough`

* Implemented Churn for bootstrap peers

## 0.11.0.0 -- 2023-01-22

### Breaking changes
Expand Down
1 change: 1 addition & 0 deletions ouroboros-network/src/Ouroboros/Network/Diffusion/P2P.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,7 @@ runM Interfaces
daBlockFetchMode
daPeerSelectionTargets
peerSelectionTargetsVar
daReadUseBootstrapPeers

--
-- Two functions only used in InitiatorAndResponder mode
Expand Down
94 changes: 62 additions & 32 deletions ouroboros-network/src/Ouroboros/Network/PeerSelection/Churn.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import System.Random

import Ouroboros.Network.BlockFetch (FetchMode (..))
import Ouroboros.Network.Diffusion.Policies (closeConnectionTimeout)
import Ouroboros.Network.PeerSelection.Bootstrap (UseBootstrapPeers (..))
import Ouroboros.Network.PeerSelection.Governor.Types hiding (targets)
import Ouroboros.Network.PeerSelection.PeerMetric

Expand Down Expand Up @@ -45,18 +46,23 @@ peerChurnGovernor :: forall m peeraddr.
-> STM m FetchMode
-> PeerSelectionTargets
-> StrictTVar m PeerSelectionTargets
-> STM m UseBootstrapPeers
-> m Void
peerChurnGovernor tracer deadlineChurnInterval bulkChurnInterval psOverallTimeout
_metrics churnModeVar inRng getFetchMode base peerSelectionVar = do
_metrics churnModeVar inRng getFetchMode base peerSelectionVar
getUseBootstrapPeers = do
-- Wait a while so that not only the closest peers have had the time
-- to become warm.
startTs0 <- getMonotonicTime
-- TODO: revisit the policy once we have local root peers in the governor.
-- The intention is to give local root peers give head start and avoid
-- giving advantage to hostile and quick root peers.
threadDelay 3
mode <- atomically updateChurnMode
atomically $ increaseActivePeers mode
(mode, ubp) <- atomically ((,) <$> updateChurnMode
<*> getUseBootstrapPeers)
atomically $ do
increaseActivePeers mode
increaseEstablishedPeers mode ubp
endTs0 <- getMonotonicTime
fuzzyDelay inRng (endTs0 `diffTime` startTs0) >>= go

Expand All @@ -73,7 +79,7 @@ peerChurnGovernor tracer deadlineChurnInterval bulkChurnInterval psOverallTimeou

-- TODO: #3396 revisit the policy for genesis
increaseActivePeers :: ChurnMode -> STM m ()
increaseActivePeers mode = do
increaseActivePeers mode =
modifyTVar peerSelectionVar (\targets -> targets {
targetNumberOfActivePeers =
case mode of
Expand All @@ -84,7 +90,7 @@ peerChurnGovernor tracer deadlineChurnInterval bulkChurnInterval psOverallTimeou
})

decreaseActivePeers :: ChurnMode -> STM m ()
decreaseActivePeers mode = do
decreaseActivePeers mode =
modifyTVar peerSelectionVar (\targets -> targets {
targetNumberOfActivePeers =
case mode of
Expand All @@ -94,8 +100,30 @@ peerChurnGovernor tracer deadlineChurnInterval bulkChurnInterval psOverallTimeou
min 1 (targetNumberOfActivePeers base - 1)
})

increaseEstablishedPeers :: ChurnMode -> UseBootstrapPeers -> STM m ()
increaseEstablishedPeers mode ubp =
modifyTVar peerSelectionVar (\targets -> targets {
targetNumberOfEstablishedPeers =
case (mode, ubp) of
(ChurnModeBulkSync, UseBootstrapPeers _) ->
min (targetNumberOfActivePeers targets + 1)
(targetNumberOfEstablishedPeers base)
_ -> targetNumberOfEstablishedPeers base
})

decreaseEstablished :: ChurnMode -> UseBootstrapPeers -> STM m ()
decreaseEstablished mode ubp =
modifyTVar peerSelectionVar (\targets -> targets {
targetNumberOfEstablishedPeers =
case (mode, ubp) of
(ChurnModeBulkSync, UseBootstrapPeers _) ->
min (targetNumberOfActivePeers targets) (targetNumberOfEstablishedPeers base - 1)
_ -> decrease (targetNumberOfEstablishedPeers base - targetNumberOfActivePeers base)
+ targetNumberOfActivePeers base
})

increaseActiveBigLedgerPeers :: ChurnMode -> STM m ()
increaseActiveBigLedgerPeers mode = do
increaseActiveBigLedgerPeers mode =
modifyTVar peerSelectionVar (\targets -> targets {
-- TODO: when chain-skipping will be implemented and chain-sync client
-- will take into account big ledger peers, we don't need pattern
Expand All @@ -110,7 +138,7 @@ peerChurnGovernor tracer deadlineChurnInterval bulkChurnInterval psOverallTimeou
})

decreaseActiveBigLedgerPeers :: ChurnMode -> STM m ()
decreaseActiveBigLedgerPeers mode = do
decreaseActiveBigLedgerPeers mode =
modifyTVar peerSelectionVar (\targets -> targets {
targetNumberOfActiveBigLedgerPeers =
case mode of
Expand All @@ -125,11 +153,13 @@ peerChurnGovernor tracer deadlineChurnInterval bulkChurnInterval psOverallTimeou
go !rng = do
startTs <- getMonotonicTime

churnMode <- atomically updateChurnMode
(churnMode, ubp) <- atomically ((,) <$> updateChurnMode
<*> getUseBootstrapPeers)
traceWith tracer $ TraceChurnMode churnMode

-- Purge the worst active peer(s).
atomically $ decreaseActivePeers churnMode
atomically $ do
-- Purge the worst active peer(s).
decreaseActivePeers churnMode

-- Short delay, we may have no active peers right now
threadDelay 1
Expand All @@ -151,25 +181,24 @@ peerChurnGovernor tracer deadlineChurnInterval bulkChurnInterval psOverallTimeou
threadDelay 1

-- Forget the worst performing non-active peers.
atomically $ modifyTVar peerSelectionVar (\targets -> targets {
targetNumberOfRootPeers =
decrease (targetNumberOfRootPeers base - targetNumberOfEstablishedPeers base)
+ targetNumberOfEstablishedPeers base
, targetNumberOfKnownPeers =
decrease (targetNumberOfKnownPeers base - targetNumberOfEstablishedPeers base)
+ targetNumberOfEstablishedPeers base
, targetNumberOfEstablishedPeers =
decrease (targetNumberOfEstablishedPeers base - targetNumberOfActivePeers base)
+ targetNumberOfActivePeers base
, targetNumberOfKnownBigLedgerPeers =
decrease (targetNumberOfKnownBigLedgerPeers base -
targetNumberOfEstablishedBigLedgerPeers base)
+ targetNumberOfEstablishedBigLedgerPeers base
, targetNumberOfEstablishedBigLedgerPeers =
decrease (targetNumberOfEstablishedBigLedgerPeers base -
targetNumberOfActiveBigLedgerPeers base)
+ targetNumberOfActiveBigLedgerPeers base
})
atomically $ do
decreaseEstablished churnMode ubp
modifyTVar peerSelectionVar (\targets -> targets {
targetNumberOfRootPeers =
decrease (targetNumberOfRootPeers base - targetNumberOfEstablishedPeers base)
+ targetNumberOfEstablishedPeers base
, targetNumberOfKnownPeers =
decrease (targetNumberOfKnownPeers base - targetNumberOfEstablishedPeers base)
+ targetNumberOfEstablishedPeers base
, targetNumberOfKnownBigLedgerPeers =
decrease (targetNumberOfKnownBigLedgerPeers base -
targetNumberOfEstablishedBigLedgerPeers base)
+ targetNumberOfEstablishedBigLedgerPeers base
, targetNumberOfEstablishedBigLedgerPeers =
decrease (targetNumberOfEstablishedBigLedgerPeers base -
targetNumberOfActiveBigLedgerPeers base)
+ targetNumberOfActiveBigLedgerPeers base
})

-- Give the governor time to properly demote them.
threadDelay $ 1 + closeConnectionTimeout
Expand All @@ -185,9 +214,10 @@ peerChurnGovernor tracer deadlineChurnInterval bulkChurnInterval psOverallTimeou
threadDelay $ 1 + psOverallTimeout

-- Pick new non-active peers
atomically $ modifyTVar peerSelectionVar (\targets -> targets {
targetNumberOfEstablishedPeers = targetNumberOfEstablishedPeers base
, targetNumberOfEstablishedBigLedgerPeers = targetNumberOfEstablishedBigLedgerPeers base
atomically $ do
increaseEstablishedPeers churnMode ubp
modifyTVar peerSelectionVar (\targets -> targets {
targetNumberOfEstablishedBigLedgerPeers = targetNumberOfEstablishedBigLedgerPeers base
})
endTs <- getMonotonicTime

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,9 @@ localRoots actions@PeerSelectionActions{ readLocalRootPeers
monitorBootstrapPeersFlag :: ( MonadSTM m
, Ord peeraddr
)
=> PeerSelectionActions peeraddr peerconn m
-> PeerSelectionState peeraddr peerconn
-> Guarded (STM m) (TimedDecision m peeraddr peerconn)
=> PeerSelectionActions peeraddr peerconn m
-> PeerSelectionState peeraddr peerconn
-> Guarded (STM m) (TimedDecision m peeraddr peerconn)
monitorBootstrapPeersFlag PeerSelectionActions { readUseBootstrapPeers }
st@PeerSelectionState { bootstrapPeersFlag
, knownPeers
Expand All @@ -573,9 +573,9 @@ monitorBootstrapPeersFlag PeerSelectionActions { readUseBootstrapPeers }
check (ubp /= bootstrapPeersFlag)
let nonEstablishedBootstrapPeers =
PublicRootPeers.getBootstrapPeers publicRootPeers
`Set.difference`
Set.\\
EstablishedPeers.toSet establishedPeers
`Set.difference`
Set.\\
(inProgressPromoteCold <> inProgressPromoteWarm)
return $ \_now ->
Decision {
Expand Down Expand Up @@ -654,9 +654,9 @@ monitorLedgerStateJudgement PeerSelectionActions{ readLedgerStateJudgement }
YoungEnough -> do
let nonEstablishedBootstrapPeers =
PublicRootPeers.getBootstrapPeers publicRootPeers
`Set.difference`
Set.\\
EstablishedPeers.toSet establishedPeers
`Set.difference`
Set.\\
(inProgressPromoteCold <> inProgressPromoteWarm)
return (\_ -> st
{ ledgerStateJudgement = lsj
Expand Down

0 comments on commit 05ab38b

Please sign in to comment.