Skip to content

Commit

Permalink
Update block forging configuration on SIGHUP
Browse files Browse the repository at this point in the history
  • Loading branch information
coot committed Jun 9, 2022
1 parent 32dff18 commit 3f667be
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
64 changes: 47 additions & 17 deletions cardano-node/src/Cardano/Node/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ runNode cmdPc = do
in getNetworkMagic $ Consensus.configBlock pInfoConfig

case p of
SomeConsensusProtocol _ runP ->
handleNodeWithTracers cmdPc nc p networkMagic runP
SomeConsensusProtocol blockType runP ->
handleNodeWithTracers cmdPc nc p networkMagic blockType runP

-- | Workaround to ensure that the main thread throws an async exception on
-- receiving a SIGTERM signal.
Expand Down Expand Up @@ -179,9 +179,10 @@ handleNodeWithTracers
-> NodeConfiguration
-> SomeConsensusProtocol
-> NetworkMagic
-> Protocol.BlockType blk
-> Protocol.ProtocolInfoArgs IO blk
-> IO ()
handleNodeWithTracers cmdPc nc p networkMagic runP = do
handleNodeWithTracers cmdPc nc p networkMagic blockType runP = do
-- This IORef contains node kernel structure which holds node kernel.
-- Used for ledger queries and peer connection status.
nodeKernelData <- mkNodeKernelData
Expand All @@ -200,7 +201,7 @@ handleNodeWithTracers cmdPc nc p networkMagic runP = do
networkMagic
nodeKernelData
p2pMode
handleSimpleNode runP p2pMode tracers nc
handleSimpleNode blockType runP p2pMode tracers nc
(\nk -> do
setNodeKernel nodeKernelData nk
traceWith (nodeStateTracer tracers) NodeKernelOnline)
Expand Down Expand Up @@ -241,7 +242,7 @@ handleNodeWithTracers cmdPc nc p networkMagic runP = do
$ \_peerLoggingThread ->
-- We ignore peer logging thread if it dies, but it will be killed
-- when 'handleSimpleNode' terminates.
handleSimpleNode runP p2pMode tracers nc
handleSimpleNode blockType runP p2pMode tracers nc
(\nk -> do
setNodeKernel nodeKernelData nk
traceWith (nodeStateTracer tracers) NodeKernelOnline)
Expand Down Expand Up @@ -297,7 +298,8 @@ handleSimpleNode
. ( RunNode blk
, Protocol.Protocol IO blk
)
=> Protocol.ProtocolInfoArgs IO blk
=> Protocol.BlockType blk
-> Protocol.ProtocolInfoArgs IO blk
-> NetworkP2PMode p2p
-> Tracers RemoteConnectionId LocalConnectionId blk p2p
-> NodeConfiguration
Expand All @@ -306,7 +308,7 @@ handleSimpleNode
-- layer is initialised. This implies this function must not block,
-- otherwise the node won't actually start.
-> IO ()
handleSimpleNode runP p2pMode tracers nc onKernel = do
handleSimpleNode blockType runP p2pMode tracers nc onKernel = do
logStartupWarnings

traceWith (startupTracer tracers)
Expand Down Expand Up @@ -402,15 +404,20 @@ handleSimpleNode runP p2pMode tracers nc onKernel = do
(localRootsVar :: StrictTVar IO [(Int, Map RelayAccessPoint PeerAdvertise)]) <- newTVarIO localRoots
publicRootsVar <- newTVarIO publicRoots
useLedgerVar <- newTVarIO (useLedgerAfterSlot nt)
#ifdef UNIX
_ <- Signals.installHandler
Signals.sigHUP
(updateTopologyConfiguration localRootsVar publicRootsVar useLedgerVar)
Nothing
#endif
void $
Node.run
nodeArgs
nodeArgs {
rnNodeKernelHook = \registry nodeKernel -> do
#ifdef UNIX
_ <- Signals.installHandler
Signals.sigHUP
(Signals.Catch $ do
updateBlockForging nodeKernel (ncProtocolConfig nc) (ncProtocolFiles nc)
updateTopologyConfiguration localRootsVar publicRootsVar useLedgerVar)
Nothing
#endif
rnNodeKernelHook nodeArgs registry nodeKernel
}
StdRunNodeArgs
{ srnBfcMaxConcurrencyBulkSync = unMaxConcurrencyBulkSync <$> ncMaxConcurrencyBulkSync nc
, srnBfcMaxConcurrencyDeadline = unMaxConcurrencyDeadline <$> ncMaxConcurrencyDeadline nc
Expand All @@ -432,6 +439,7 @@ handleSimpleNode runP p2pMode tracers nc onKernel = do
_ <- Signals.installHandler
Signals.sigHUP
(Signals.Catch $ do
traceWith (startupTracer tracers) BlockForgingUpdateUnsupported
traceWith (startupTracer tracers) NetworkConfigUpdateUnsupported
)
Nothing
Expand Down Expand Up @@ -502,12 +510,34 @@ handleSimpleNode runP p2pMode tracers nc onKernel = do
developmentNtcVersions)

#ifdef UNIX
updateBlockForging :: NodeKernel IO RemoteConnectionId LocalConnectionId blk
-> NodeProtocolConfiguration
-> ProtocolFilepaths -> IO ()
updateBlockForging nodeKernel ncProtocolConfig ncProtocolFiles = do
eitherSomeProtocol <- runExceptT $ mkConsensusProtocol
ncProtocolConfig
(Just ncProtocolFiles)
case eitherSomeProtocol of
Left err ->
traceWith (startupTracer tracers) (BlockForgingUpdateError err)
Right (SomeConsensusProtocol blockType' runP') ->
case Protocol.reflBlockType blockType blockType' of
Just Refl -> do
-- TODO: check if runP' has changed
traceWith (startupTracer tracers) BlockForgingUpdate
Protocol.blockForging runP' >>= setBlockForging nodeKernel
Nothing ->
traceWith (startupTracer tracers)
$ BlockForgingBlockTypeMismatch
(Protocol.SomeBlockType blockType)
(Protocol.SomeBlockType blockType')
return ()

updateTopologyConfiguration :: StrictTVar IO [(Int, Map RelayAccessPoint PeerAdvertise)]
-> StrictTVar IO [RelayAccessPoint]
-> StrictTVar IO UseLedgerAfter
-> Signals.Handler
updateTopologyConfiguration localRootsVar publicRootsVar useLedgerVar =
Signals.Catch $ do
-> IO ()
updateTopologyConfiguration localRootsVar publicRootsVar useLedgerVar = do
traceWith (startupTracer tracers) NetworkConfigUpdate
result <- try $ readTopologyFileOrError nc
case result of
Expand Down
19 changes: 18 additions & 1 deletion cardano-node/src/Cardano/Node/Startup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ import Ouroboros.Network.PeerSelection.Types (PeerAdvertise)
import Ouroboros.Network.Subscription.Dns (DnsSubscriptionTarget (..))
import Ouroboros.Network.Subscription.Ip (IPSubscriptionTarget (..))

import Cardano.Api.Protocol.Types (BlockType (..), protocolInfo)
import Cardano.Api.Protocol.Types (BlockType (..), SomeBlockType,
protocolInfo)
import Cardano.Logging
import Cardano.Node.Configuration.Socket
import Cardano.Node.Protocol (ProtocolInstantiationError)
import Cardano.Node.Protocol.Types (Protocol (..), SomeConsensusProtocol (..))

import Cardano.Git.Rev (gitRev)
Expand Down Expand Up @@ -75,6 +77,21 @@ data StartupTrace blk =

| StartupDBValidation

-- | Log that the block forging is being updated
| BlockForgingUpdate

-- | Protocol instantiation error when updating block forging
| BlockForgingUpdateError ProtocolInstantiationError

-- | Mismatched block type
| BlockForgingBlockTypeMismatch
SomeBlockType -- ^ expected
SomeBlockType -- ^ provided

-- | Re-configuration of block forging is not supported.
--
| BlockForgingUpdateUnsupported

-- | Log that the network configuration is being updated.
--
| NetworkConfigUpdate
Expand Down

0 comments on commit 3f667be

Please sign in to comment.