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 authored and facundominguez committed Aug 6, 2024
1 parent 8a70896 commit f27e7c6
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 35 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 @@ -69,6 +70,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 @@ -91,6 +93,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 @@ -121,6 +124,7 @@ nullTracers = Tracers
, consensusErrorTracer = nullTracer
, gsmTracer = nullTracer
, gddTracer = nullTracer
, csjTracer = nullTracer
}

showTracers :: ( Show blk
Expand Down Expand Up @@ -154,6 +158,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 @@ -391,6 +391,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 @@ -85,6 +85,7 @@ startBlockFetchLogic registry tracer chainDb fetchClientRegistry csHandlesCol =

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 @@ -165,6 +165,7 @@ module Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping (
, JumpInstruction (..)
, JumpResult (..)
, Jumping (..)
, TraceEvent (..)
, getDynamo
, makeContext
, mkJumping
Expand All @@ -176,7 +177,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 @@ -773,38 +775,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 @@ -905,3 +911,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 @@ -65,6 +65,7 @@ module Ouroboros.Consensus.Storage.ChainDB.Impl.Types (
) where

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 @@ -281,6 +281,7 @@ runBlockFetchTest BlockFetchClientTestSetup{..} = withRegistry \registry -> do
-> BlockFetchConsensusInterface PeerId (Header TestBlock) TestBlock m
mkTestBlockFetchConsensusInterface getCandidates chainDbView =
(BlockFetchClientInterface.mkBlockFetchConsensusInterface @m @PeerId
nullTracer
(TestBlockConfig numCoreNodes)
chainDbView
(error "ChainSyncClientHandleCollection not provided to mkBlockFetchConsensusInterface")
Expand Down

0 comments on commit f27e7c6

Please sign in to comment.