Skip to content

Commit

Permalink
Add explicit tracing events for CSJ
Browse files Browse the repository at this point in the history
  • Loading branch information
Niols committed Jul 15, 2024
1 parent 310d3a9 commit 0c8ef37
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Ouroboros.Consensus.MiniProtocol.BlockFetch.Server
(TraceBlockFetchServerEvent)
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client
(InvalidBlockReason, TraceChainSyncClientEvent)
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping as CSJumping
import Ouroboros.Consensus.MiniProtocol.ChainSync.Server
(TraceChainSyncServerEvent)
import Ouroboros.Consensus.MiniProtocol.LocalTxSubmission.Server
Expand Down Expand Up @@ -67,6 +68,7 @@ data Tracers' remotePeer localPeer blk f = Tracers
, consensusErrorTracer :: f SomeException
, gsmTracer :: f (TraceGsmEvent (Tip blk))
, gddTracer :: f (TraceGDDEvent remotePeer blk)
, csjTracer :: f (CSJumping.TraceEvent remotePeer)
}

instance (forall a. Semigroup (f a))
Expand All @@ -89,6 +91,7 @@ instance (forall a. Semigroup (f a))
, consensusErrorTracer = f consensusErrorTracer
, gsmTracer = f gsmTracer
, gddTracer = f gddTracer
, csjTracer = f csjTracer
}
where
f :: forall a. Semigroup a
Expand Down Expand Up @@ -119,6 +122,7 @@ nullTracers = Tracers
, consensusErrorTracer = nullTracer
, gsmTracer = nullTracer
, gddTracer = nullTracer
, csjTracer = nullTracer
}

showTracers :: ( Show blk
Expand Down Expand Up @@ -152,6 +156,7 @@ showTracers tr = Tracers
, consensusErrorTracer = showTracing tr
, gsmTracer = showTracing tr
, gddTracer = showTracing tr
, csjTracer = showTracing tr
}

{-------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ initInternalState NodeKernelArgs { tracers, chainDB, registry, cfg
(GSM.gsmStateToLedgerJudgement <$> readTVar varGsmState)
blockFetchInterface :: BlockFetchConsensusInterface (ConnectionId addrNTN) (Header blk) blk m
blockFetchInterface = BlockFetchClientInterface.mkBlockFetchConsensusInterface
(csjTracer tracers)
(configBlock cfg)
(BlockFetchClientInterface.defaultChainDbView chainDB)
varChainSyncHandles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ startBlockFetchLogic enableChainSelStarvation registry tracer chainDb fetchClien

blockFetchConsensusInterface =
BlockFetchClientInterface.mkBlockFetchConsensusInterface
nullTracer -- FIXME
(TestBlockConfig $ NumCoreNodes 0) -- Only needed when minting blocks
(BlockFetchClientInterface.defaultChainDbView chainDb)
csHandlesCol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Ouroboros.Consensus.MiniProtocol.BlockFetch.ClientInterface (
) where

import Control.Monad
import Control.Tracer (Tracer)
import Data.Map.Strict (Map)
import Data.Time.Clock (UTCTime)
import GHC.Stack (HasCallStack)
Expand All @@ -29,7 +30,7 @@ import Ouroboros.Consensus.Ledger.Extended
import Ouroboros.Consensus.Ledger.SupportsProtocol
(LedgerSupportsProtocol)
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client as CSClient
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping as Jumping
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping as CSJumping
import Ouroboros.Consensus.Storage.ChainDB.API (ChainDB)
import qualified Ouroboros.Consensus.Storage.ChainDB.API as ChainDB
import Ouroboros.Consensus.Storage.ChainDB.API.Types.InvalidBlockPunishment
Expand Down Expand Up @@ -179,7 +180,8 @@ mkBlockFetchConsensusInterface ::
, Ord peer
, LedgerSupportsProtocol blk
)
=> BlockConfig blk
=> Tracer m (CSJumping.TraceEvent peer)
-> BlockConfig blk
-> ChainDbView m blk
-> CSClient.ChainSyncClientHandleCollection peer m blk
-> (Header blk -> SizeInBytes)
Expand All @@ -189,7 +191,7 @@ mkBlockFetchConsensusInterface ::
-- ^ See 'readFetchMode'.
-> BlockFetchConsensusInterface peer (Header blk) blk m
mkBlockFetchConsensusInterface
bcfg chainDB csHandlesCol blockFetchSize slotForgeTime readFetchMode =
csjTracer bcfg chainDB csHandlesCol blockFetchSize slotForgeTime readFetchMode =
BlockFetchConsensusInterface {..}
where
getCandidates :: STM m (Map peer (AnchoredFragment (Header blk)))
Expand Down Expand Up @@ -355,4 +357,4 @@ mkBlockFetchConsensusInterface
readChainSelStarvation = getChainSelStarvation chainDB

demoteCSJDynamo :: peer -> m ()
demoteCSJDynamo = void . atomically . Jumping.rotateDynamo csHandlesCol
demoteCSJDynamo = CSJumping.rotateDynamo csjTracer csHandlesCol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}

Expand Down Expand Up @@ -167,6 +166,7 @@ module Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping (
, JumpInstruction (..)
, JumpResult (..)
, Jumping (..)
, TraceEvent (..)
, getDynamo
, makeContext
, mkJumping
Expand All @@ -178,7 +178,8 @@ module Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping (

import Cardano.Slotting.Slot (SlotNo (..), WithOrigin (..))
import Control.Monad (forM, forM_, void, when)
import Data.Foldable (toList)
import Control.Tracer (Tracer, traceWith)
import Data.Foldable (toList, traverse_)
import Data.List (sortOn)
import qualified Data.Map as Map
import Data.Maybe (catMaybes, fromMaybe)
Expand Down Expand Up @@ -775,38 +776,42 @@ rotateDynamo ::
LedgerSupportsProtocol blk,
MonadSTM m
) =>
Tracer m (TraceEvent peer) ->
ChainSyncClientHandleCollection peer m blk ->
peer ->
STM m (Maybe (peer, ChainSyncClientHandle m blk))
rotateDynamo handlesCol peer = do
handles <- cschcMap handlesCol
case handles Map.!? peer of
Nothing ->
-- Do not re-elect a dynamo if the peer has been disconnected.
getDynamo handlesCol
Just oldDynHandle ->
readTVar (cschJumping oldDynHandle) >>= \case
Dynamo{} -> do
cschcRotateHandle handlesCol peer
peerStates <- cschcSeq handlesCol
mEngaged <- findNonDisengaged peerStates
case mEngaged of
Nothing ->
-- There are no engaged peers. This case cannot happen, as the
-- dynamo is always engaged.
error "rotateDynamo: no engaged peer found"
Just (newDynamoId, newDynHandle)
| newDynamoId == peer ->
-- The old dynamo is the only engaged peer left.
pure $ Just (newDynamoId, newDynHandle)
| otherwise -> do
newJumper Nothing (Happy FreshJumper Nothing)
>>= writeTVar (cschJumping oldDynHandle)
promoteToDynamo peerStates newDynamoId newDynHandle
pure $ Just (newDynamoId, newDynHandle)
_ ->
-- Do not re-elect a dynamo if the peer is not the dynamo.
getDynamo handlesCol
m ()
-- STM m (Maybe (peer, ChainSyncClientHandle m blk))
rotateDynamo tracer handlesCol peer = do
traceEvent <- atomically $ do
handles <- cschcMap handlesCol
case handles Map.!? peer of
Nothing ->
-- Do not re-elect a dynamo if the peer has been disconnected.
pure Nothing
Just oldDynHandle ->
readTVar (cschJumping oldDynHandle) >>= \case
Dynamo{} -> do
cschcRotateHandle handlesCol peer
peerStates <- cschcSeq handlesCol
mEngaged <- findNonDisengaged peerStates
case mEngaged of
Nothing ->
-- There are no engaged peers. This case cannot happen, as the
-- dynamo is always engaged.
error "rotateDynamo: no engaged peer found"
Just (newDynamoId, newDynHandle)
| newDynamoId == peer ->
-- The old dynamo is the only engaged peer left.
pure Nothing
| otherwise -> do
newJumper Nothing (Happy FreshJumper Nothing)
>>= writeTVar (cschJumping oldDynHandle)
promoteToDynamo peerStates newDynamoId newDynHandle
pure $ Just $ RotatedDynamo peer newDynamoId
_ ->
-- Do not re-elect a dynamo if the peer is not the dynamo.
pure Nothing
traverse_ (traceWith tracer) traceEvent

-- | Choose an unspecified new non-idling dynamo and demote all other peers to
-- jumpers.
Expand Down Expand Up @@ -907,3 +912,7 @@ electNewObjector context = do
pure $ Just (badPoint, (initState, goodJumpInfo, handle))
_ ->
pure Nothing

data TraceEvent peer
= RotatedDynamo peer peer
deriving (Show)
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ module Ouroboros.Consensus.Storage.ChainDB.Impl.Types (
, TraceValidationEvent (..)
) where

import Control.Monad (when)
import Cardano.Prelude (whenM)
import Control.Monad (when)
import Control.Tracer
import Data.Foldable (traverse_)
import Data.Map.Strict (Map)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ runBlockFetchTest BlockFetchClientTestSetup{..} = withRegistry \registry -> do
-> BlockFetchConsensusInterface PeerId (Header TestBlock) TestBlock m
mkTestBlockFetchConsensusInterface getCandidates chainDbView =
BlockFetchClientInterface.mkBlockFetchConsensusInterface
nullTracer
(TestBlockConfig numCoreNodes)
chainDbView
getCandidates
Expand Down

0 comments on commit 0c8ef37

Please sign in to comment.