Skip to content

Commit 4e91897

Browse files
Niolsneilmayhew
authored andcommitted
Add explicit tracing events for CSJ
1 parent e1e6f56 commit 4e91897

File tree

7 files changed

+56
-38
lines changed

7 files changed

+56
-38
lines changed

ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/Node/Tracers.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Ouroboros.Consensus.MiniProtocol.BlockFetch.Server
3232
(TraceBlockFetchServerEvent)
3333
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client
3434
(TraceChainSyncClientEvent)
35+
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping as CSJumping
3536
import Ouroboros.Consensus.MiniProtocol.ChainSync.Server
3637
(TraceChainSyncServerEvent)
3738
import Ouroboros.Consensus.MiniProtocol.LocalTxSubmission.Server
@@ -71,6 +72,7 @@ data Tracers' remotePeer localPeer blk f = Tracers
7172
, consensusErrorTracer :: f SomeException
7273
, gsmTracer :: f (TraceGsmEvent (Tip blk))
7374
, gddTracer :: f (TraceGDDEvent remotePeer blk)
75+
, csjTracer :: f (CSJumping.TraceEvent remotePeer)
7476
}
7577

7678
instance (forall a. Semigroup (f a))
@@ -94,6 +96,7 @@ instance (forall a. Semigroup (f a))
9496
, consensusErrorTracer = f consensusErrorTracer
9597
, gsmTracer = f gsmTracer
9698
, gddTracer = f gddTracer
99+
, csjTracer = f csjTracer
97100
}
98101
where
99102
f :: forall a. Semigroup a
@@ -125,6 +128,7 @@ nullTracers = Tracers
125128
, consensusErrorTracer = nullTracer
126129
, gsmTracer = nullTracer
127130
, gddTracer = nullTracer
131+
, csjTracer = nullTracer
128132
}
129133

130134
showTracers :: ( Show blk
@@ -159,6 +163,7 @@ showTracers tr = Tracers
159163
, consensusErrorTracer = showTracing tr
160164
, gsmTracer = showTracing tr
161165
, gddTracer = showTracing tr
166+
, csjTracer = showTracing tr
162167
}
163168

164169
{-------------------------------------------------------------------------------

ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/NodeKernel.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ initInternalState NodeKernelArgs { tracers, chainDB, registry, cfg
402402
(GSM.gsmStateToLedgerJudgement <$> readTVar varGsmState)
403403
blockFetchInterface :: BlockFetchConsensusInterface (ConnectionId addrNTN) (Header blk) blk m
404404
blockFetchInterface = BlockFetchClientInterface.mkBlockFetchConsensusInterface
405+
(csjTracer tracers)
405406
(configBlock cfg)
406407
(BlockFetchClientInterface.defaultChainDbView chainDB)
407408
varChainSyncHandles

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/PeerSimulator/BlockFetch.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ startBlockFetchLogic registry tracer chainDb fetchClientRegistry csHandlesCol =
8383

8484
blockFetchConsensusInterface =
8585
BlockFetchClientInterface.mkBlockFetchConsensusInterface
86+
nullTracer -- FIXME
8687
(TestBlockConfig $ NumCoreNodes 0) -- Only needed when minting blocks
8788
(BlockFetchClientInterface.defaultChainDbView chainDb)
8889
csHandlesCol

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/MiniProtocol/BlockFetch/ClientInterface.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Ouroboros.Consensus.MiniProtocol.BlockFetch.ClientInterface (
1414
) where
1515

1616
import Control.Monad
17+
import Control.Tracer (Tracer)
1718
import Data.Map.Strict (Map)
1819
import Data.Time.Clock (UTCTime)
1920
import GHC.Stack (HasCallStack)
@@ -29,7 +30,7 @@ import Ouroboros.Consensus.Ledger.Extended
2930
import Ouroboros.Consensus.Ledger.SupportsProtocol
3031
(LedgerSupportsProtocol)
3132
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client as CSClient
32-
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping as Jumping
33+
import qualified Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping as CSJumping
3334
import Ouroboros.Consensus.Storage.ChainDB.API (ChainDB)
3435
import qualified Ouroboros.Consensus.Storage.ChainDB.API as ChainDB
3536
import Ouroboros.Consensus.Storage.ChainDB.API.Types.InvalidBlockPunishment
@@ -179,7 +180,8 @@ mkBlockFetchConsensusInterface ::
179180
, Ord peer
180181
, LedgerSupportsProtocol blk
181182
)
182-
=> BlockConfig blk
183+
=> Tracer m (CSJumping.TraceEvent peer)
184+
-> BlockConfig blk
183185
-> ChainDbView m blk
184186
-> CSClient.ChainSyncClientHandleCollection peer m blk
185187
-> (Header blk -> SizeInBytes)
@@ -190,7 +192,7 @@ mkBlockFetchConsensusInterface ::
190192
-> DiffusionPipeliningSupport
191193
-> BlockFetchConsensusInterface peer (Header blk) blk m
192194
mkBlockFetchConsensusInterface
193-
bcfg chainDB csHandlesCol blockFetchSize slotForgeTime readFetchMode pipelining =
195+
csjTracer bcfg chainDB csHandlesCol blockFetchSize slotForgeTime readFetchMode pipelining =
194196
BlockFetchConsensusInterface {..}
195197
where
196198
getCandidates :: STM m (Map peer (AnchoredFragment (Header blk)))
@@ -343,5 +345,5 @@ mkBlockFetchConsensusInterface
343345

344346
readChainSelStarvation = getChainSelStarvation chainDB
345347

346-
demoteCSJDynamo :: peer -> m ()
347-
demoteCSJDynamo = void . atomically . Jumping.rotateDynamo csHandlesCol
348+
demoteChainSyncJumpingDynamo :: peer -> m ()
349+
demoteChainSyncJumpingDynamo = CSJumping.rotateDynamo csjTracer csHandlesCol

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/MiniProtocol/ChainSync/Client/Jumping.hs

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ module Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping (
165165
, JumpInstruction (..)
166166
, JumpResult (..)
167167
, Jumping (..)
168+
, TraceEvent (..)
168169
, getDynamo
169170
, makeContext
170171
, mkJumping
@@ -176,7 +177,8 @@ module Ouroboros.Consensus.MiniProtocol.ChainSync.Client.Jumping (
176177

177178
import Cardano.Slotting.Slot (SlotNo (..), WithOrigin (..))
178179
import Control.Monad (forM, forM_, void, when)
179-
import Data.Foldable (toList)
180+
import Control.Tracer (Tracer, traceWith)
181+
import Data.Foldable (toList, traverse_)
180182
import Data.List (sortOn)
181183
import qualified Data.Map as Map
182184
import Data.Maybe (catMaybes, fromMaybe)
@@ -766,45 +768,46 @@ unregisterClient context = do
766768
--
767769
-- It does nothing if there is no other engaged peer to elect or if the given
768770
-- peer is not the dynamo.
769-
--
770-
-- Yields the new dynamo, if there is one.
771771
rotateDynamo ::
772772
( Ord peer,
773773
LedgerSupportsProtocol blk,
774774
MonadSTM m
775775
) =>
776+
Tracer m (TraceEvent peer) ->
776777
ChainSyncClientHandleCollection peer m blk ->
777778
peer ->
778-
STM m (Maybe (peer, ChainSyncClientHandle m blk))
779-
rotateDynamo handlesCol peer = do
780-
handles <- cschcMap handlesCol
781-
case handles Map.!? peer of
782-
Nothing ->
783-
-- Do not re-elect a dynamo if the peer has been disconnected.
784-
getDynamo handlesCol
785-
Just oldDynHandle ->
786-
readTVar (cschJumping oldDynHandle) >>= \case
787-
Dynamo{} -> do
788-
cschcRotateHandle handlesCol peer
789-
peerStates <- cschcSeq handlesCol
790-
mEngaged <- findNonDisengaged peerStates
791-
case mEngaged of
792-
Nothing ->
793-
-- There are no engaged peers. This case cannot happen, as the
794-
-- dynamo is always engaged.
795-
error "rotateDynamo: no engaged peer found"
796-
Just (newDynamoId, newDynHandle)
797-
| newDynamoId == peer ->
798-
-- The old dynamo is the only engaged peer left.
799-
pure $ Just (newDynamoId, newDynHandle)
800-
| otherwise -> do
801-
newJumper Nothing (Happy FreshJumper Nothing)
802-
>>= writeTVar (cschJumping oldDynHandle)
803-
promoteToDynamo peerStates newDynamoId newDynHandle
804-
pure $ Just (newDynamoId, newDynHandle)
805-
_ ->
806-
-- Do not re-elect a dynamo if the peer is not the dynamo.
807-
getDynamo handlesCol
779+
m ()
780+
rotateDynamo tracer handlesCol peer = do
781+
traceEvent <- atomically $ do
782+
handles <- cschcMap handlesCol
783+
case handles Map.!? peer of
784+
Nothing ->
785+
-- Do not re-elect a dynamo if the peer has been disconnected.
786+
pure Nothing
787+
Just oldDynHandle ->
788+
readTVar (cschJumping oldDynHandle) >>= \case
789+
Dynamo{} -> do
790+
cschcRotateHandle handlesCol peer
791+
peerStates <- cschcSeq handlesCol
792+
mEngaged <- findNonDisengaged peerStates
793+
case mEngaged of
794+
Nothing ->
795+
-- There are no engaged peers. This case cannot happen, as the
796+
-- dynamo is always engaged.
797+
error "rotateDynamo: no engaged peer found"
798+
Just (newDynamoId, newDynHandle)
799+
| newDynamoId == peer ->
800+
-- The old dynamo is the only engaged peer left.
801+
pure Nothing
802+
| otherwise -> do
803+
newJumper Nothing (Happy FreshJumper Nothing)
804+
>>= writeTVar (cschJumping oldDynHandle)
805+
promoteToDynamo peerStates newDynamoId newDynHandle
806+
pure $ Just $ RotatedDynamo peer newDynamoId
807+
_ ->
808+
-- Do not re-elect a dynamo if the peer is not the dynamo.
809+
pure Nothing
810+
traverse_ (traceWith tracer) traceEvent
808811

809812
-- | Choose an unspecified new non-idling dynamo and demote all other peers to
810813
-- jumpers.
@@ -905,3 +908,7 @@ electNewObjector context = do
905908
pure $ Just (badPoint, (initState, goodJumpInfo, handle))
906909
_ ->
907910
pure Nothing
911+
912+
data TraceEvent peer
913+
= RotatedDynamo peer peer
914+
deriving (Show)

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/Impl/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ module Ouroboros.Consensus.Storage.ChainDB.Impl.Types (
6464
) where
6565

6666
import Cardano.Prelude (whenM)
67+
import Control.Monad (when)
6768
import Control.ResourceRegistry
6869
import Control.Tracer
6970
import Data.Foldable (traverse_)

ouroboros-consensus/test/consensus-test/Test/Consensus/MiniProtocol/BlockFetch/Client.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ runBlockFetchTest BlockFetchClientTestSetup{..} = withRegistry \registry -> do
279279
-> BlockFetchConsensusInterface PeerId (Header TestBlock) TestBlock m
280280
mkTestBlockFetchConsensusInterface getCandidates chainDbView =
281281
(BlockFetchClientInterface.mkBlockFetchConsensusInterface @m @PeerId
282+
nullTracer
282283
(TestBlockConfig numCoreNodes)
283284
chainDbView
284285
(error "ChainSyncClientHandleCollection not provided to mkBlockFetchConsensusInterface")

0 commit comments

Comments
 (0)