Skip to content

Commit 3c4551e

Browse files
authored
Merge pull request #6590 from IntersectMBO/mgalazyn/fix/grpc-config-reload
Fix node config reading in gRPC config reload on SIGHUP
2 parents 7e60b6c + 5dc9d9d commit 3c4551e

2 files changed

Lines changed: 76 additions & 20 deletions

File tree

  • cardano-node

cardano-node/src/Cardano/Node/Run.hs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ handleNodeWithTracers cmdPc nc p@(SomeConsensusProtocol blockType runP) = do
265265
then DisabledBlockForging
266266
else EnabledBlockForging))
267267

268-
handleSimpleNode blockType runP tracers nc networkMagic
268+
handleSimpleNode blockType runP tracers nc cmdPc networkMagic
269269
(\nk -> do
270270
setNodeKernel nodeKernelData nk
271271
traceWith (nodeStateTracer tracers) NodeKernelOnline)
@@ -305,13 +305,16 @@ handleSimpleNode
305305
-> Api.ProtocolInfoArgs blk
306306
-> Tracers RemoteAddress LocalAddress blk IO
307307
-> NodeConfiguration
308+
-> PartialNodeConfiguration
309+
-- ^ Original CLI configuration, used for SIGHUP config reload so CLI
310+
-- overrides are preserved when re-reading the YAML file.
308311
-> NetworkMagic
309312
-> (NodeKernel IO RemoteAddress LocalConnectionId blk -> IO ())
310313
-- ^ Called on the 'NodeKernel' after creating it, but before the network
311314
-- layer is initialised. This implies this function must not block,
312315
-- otherwise the node won't actually start.
313316
-> IO ()
314-
handleSimpleNode blockType runP tracers nc networkMagic onKernel = do
317+
handleSimpleNode blockType runP tracers nc cmdPc networkMagic onKernel = do
315318
logStartupWarnings
316319

317320
logDeprecatedLedgerDBOptions
@@ -439,7 +442,7 @@ handleSimpleNode blockType runP tracers nc networkMagic onKernel = do
439442
(readTVar ledgerPeerSnapshotPathVar)
440443
(readTVar useLedgerVar)
441444
(writeTVar ledgerPeerSnapshotVar)
442-
updateRpcConfiguration (startupTracer tracers) (ncConfigFile nc) rpcConfigVar
445+
updateRpcConfiguration (startupTracer tracers) cmdPc rpcConfigVar
443446
traceWith (startupTracer tracers) (BlockForgingUpdate NotEffective)
444447
)
445448
Nothing
@@ -484,7 +487,7 @@ handleSimpleNode blockType runP tracers nc networkMagic onKernel = do
484487
rnNodeKernelHook = \registry nodeKernel -> do
485488
-- reinstall `SIGHUP` handler
486489
installSigHUPHandler (startupTracer tracers) (Consensus.kesAgentTracer $ consensusTracers tracers)
487-
blockType nc networkMagic nodeKernel localRootsVar publicRootsVar useLedgerVar
490+
blockType nc cmdPc networkMagic nodeKernel localRootsVar publicRootsVar useLedgerVar
488491
useBootstrapVar ledgerPeerSnapshotPathVar ledgerPeerSnapshotVar
489492
rpcConfigVar
490493
rnNodeKernelHook nodeArgs registry nodeKernel
@@ -579,6 +582,7 @@ installSigHUPHandler :: Tracer IO (StartupTrace blk)
579582
-> Tracer IO KESAgentClientTrace
580583
-> Api.BlockType blk
581584
-> NodeConfiguration
585+
-> PartialNodeConfiguration -- ^ original CLI configuration
582586
-> NetworkMagic
583587
-> NodeKernel IO RemoteAddress (ConnectionId LocalAddress) blk
584588
-> StrictTVar IO [(HotValency, WarmValency, Map RelayAccessPoint (LocalRootConfig PeerTrustable))]
@@ -590,9 +594,9 @@ installSigHUPHandler :: Tracer IO (StartupTrace blk)
590594
-> StrictTVar IO RpcConfig
591595
-> IO ()
592596
#ifndef UNIX
593-
installSigHUPHandler _ _ _ _ _ _ _ _ _ _ _ _ _ = return ()
597+
installSigHUPHandler _ _ _ _ _ _ _ _ _ _ _ _ _ _ = return ()
594598
#else
595-
installSigHUPHandler startupTracer kesAgentTracer blockType nc networkMagic nodeKernel localRootsVar
599+
installSigHUPHandler startupTracer kesAgentTracer blockType nc cmdPc networkMagic nodeKernel localRootsVar
596600
publicRootsVar useLedgerVar useBootstrapPeersVar ledgerPeerSnapshotPathVar ledgerPeerSnapshotVar
597601
rpcConfigVar =
598602
void $ Signals.installHandler
@@ -609,7 +613,7 @@ installSigHUPHandler startupTracer kesAgentTracer blockType nc networkMagic node
609613
(readTVar ledgerPeerSnapshotPathVar)
610614
(readTVar useLedgerVar)
611615
(writeTVar ledgerPeerSnapshotVar)
612-
updateRpcConfiguration startupTracer (ncConfigFile nc) rpcConfigVar
616+
updateRpcConfiguration startupTracer cmdPc rpcConfigVar
613617
)
614618
Nothing
615619
#endif
@@ -786,21 +790,18 @@ rpcServerLoop startupTracer rpcTracer rpcConfigVar networkMagic = go
786790
atomically . modifyTVar rpcConfigVar $ \config -> config{isEnabled = Identity False}
787791

788792
#ifdef UNIX
789-
-- | Reload RPC configuration from the configuration file
790-
updateRpcConfiguration :: Tracer IO (StartupTrace blk) -- ^ tracer tracing the configuration reload
791-
-> ConfigYamlFilePath -- ^ node configuration file, to reload configuration from
793+
-- | Reload RPC configuration by re-reading the YAML config file and merging
794+
-- with CLI overrides, exactly like startup does via 'buildNodeConfiguration'.
795+
updateRpcConfiguration :: Tracer IO (StartupTrace blk) -- ^ tracer for configuration reload events
796+
-> PartialNodeConfiguration -- ^ original CLI configuration, merged with re-read YAML on reload
792797
-> StrictTVar IO RpcConfig -- ^ TVar storing RPC configuration
793798
-> IO ()
794-
updateRpcConfiguration tracer configFilePath rpcConfigVar = do
795-
result <- fmap (join . first Exception.displayException)
796-
. try @Exception.SomeException
797-
. fmap makeNodeConfiguration
798-
. parseNodeConfigurationFP
799-
$ Just configFilePath
799+
updateRpcConfiguration tracer cmdPc rpcConfigVar = do
800+
result <- try @Exception.SomeException $ buildNodeConfiguration cmdPc
800801
case result of
801802
Left err ->
802803
-- reload failure, we don't do anything this time
803-
traceWith tracer (RpcConfigUpdateError $ pack err)
804+
traceWith tracer (RpcConfigUpdateError $ pack (Exception.displayException err))
804805
Right NodeConfiguration{ncRpcConfig=newConfig} ->
805806
join . atomically $ do
806807
oldConfig <- readTVar rpcConfigVar

cardano-node/test/Test/Cardano/Node/POM.hs

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module Test.Cardano.Node.POM
88
) where
99

1010

11+
import Cardano.Api (File (..))
12+
1113
import Cardano.Crypto.ProtocolMagic (RequiresNetworkMagic (..))
1214
import Cardano.Network.ConsensusMode (ConsensusMode (..))
1315
import Cardano.Network.Diffusion.Configuration (defaultNumberOfBigLedgerPeers)
@@ -18,7 +20,7 @@ import Cardano.Node.Configuration.POM
1820
import Cardano.Node.Configuration.Socket
1921
import Cardano.Node.Handlers.Shutdown
2022
import Cardano.Node.Types
21-
import Cardano.Rpc.Server.Config (makeRpcConfig)
23+
import Cardano.Rpc.Server.Config (RpcConfigF (..), makeRpcConfig)
2224
import Ouroboros.Consensus.Node (NodeDatabasePaths (..))
2325
import Ouroboros.Consensus.Node.Genesis (disableGenesisConfig)
2426
import Ouroboros.Consensus.Storage.LedgerDB.Args
@@ -29,12 +31,14 @@ import Ouroboros.Network.PeerSelection.PeerSharing (PeerSharing (..))
2931
import Ouroboros.Network.TxSubmission.Inbound.V2.Types
3032

3133
import Data.Bifunctor (first)
34+
import Data.Functor.Identity (Identity (..))
3235
import Data.Monoid (Last (..))
3336
import Data.String
3437
import Data.Text (Text)
3538

36-
import Hedgehog (Property, discover, withTests, (===))
39+
import Hedgehog (Property, discover, (===))
3740
import qualified Hedgehog
41+
import qualified Hedgehog.Extras as H
3842
import Hedgehog.Internal.Property (evalEither, failWith)
3943

4044

@@ -45,7 +49,7 @@ import Hedgehog.Internal.Property (evalEither, failWith)
4549

4650
prop_sanityCheck_POM :: Property
4751
prop_sanityCheck_POM =
48-
withTests 1 . Hedgehog.property $ do
52+
H.propertyOnce $ do
4953
let combinedPartials = defaultPartialNodeConfiguration
5054
<> testPartialYamlConfig
5155
<> testPartialCliConfig
@@ -290,6 +294,57 @@ eExpectedConfig = do
290294
, ncTxSubmissionInitDelay = defaultTxSubmissionInitDelay
291295
}
292296

297+
-- A socket config with a node socket path, needed for RPC-enabled tests
298+
-- because makeRpcConfig validates that a node socket exists when RPC is enabled.
299+
testSocketConfigWithPath :: Last SocketConfig
300+
testSocketConfigWithPath =
301+
Last . Just $ SocketConfig mempty mempty mempty (Last . Just $ File "node.socket")
302+
303+
-- | CLI --grpc-enable + YAML silent -> RPC stays enabled.
304+
-- Documents the config precedence that issue #6589 depends on: CLI flags
305+
-- must survive a YAML-only re-read.
306+
prop_rpcReload_cliEnabledYamlSilent :: Property
307+
prop_rpcReload_cliEnabledYamlSilent =
308+
H.propertyOnce $ do
309+
let cliConfig = testPartialCliConfig
310+
{ pncRpcConfig = RpcConfig (Last (Just True)) mempty mempty
311+
, pncSocketConfig = testSocketConfigWithPath
312+
}
313+
merged = defaultPartialNodeConfiguration <> testPartialYamlConfig <> cliConfig
314+
NodeConfiguration{ncRpcConfig = RpcConfig{isEnabled}} <- evalEither $ makeNodeConfiguration merged
315+
isEnabled === Identity True
316+
317+
-- | CLI silent + YAML EnableRpc -> RPC enabled.
318+
prop_rpcReload_cliSilentYamlEnabled :: Property
319+
prop_rpcReload_cliSilentYamlEnabled =
320+
H.propertyOnce $ do
321+
let yamlConfig = testPartialYamlConfig{pncRpcConfig = RpcConfig (Last (Just True)) mempty mempty}
322+
cliConfig = testPartialCliConfig{pncSocketConfig = testSocketConfigWithPath}
323+
merged = defaultPartialNodeConfiguration <> yamlConfig <> cliConfig
324+
NodeConfiguration{ncRpcConfig = RpcConfig{isEnabled}} <- evalEither $ makeNodeConfiguration merged
325+
isEnabled === Identity True
326+
327+
-- | Both CLI and YAML silent -> RPC disabled (default).
328+
prop_rpcReload_bothSilent :: Property
329+
prop_rpcReload_bothSilent =
330+
H.propertyOnce $ do
331+
let merged = defaultPartialNodeConfiguration <> testPartialYamlConfig <> testPartialCliConfig
332+
NodeConfiguration{ncRpcConfig = RpcConfig{isEnabled}} <- evalEither $ makeNodeConfiguration merged
333+
isEnabled === Identity False
334+
335+
-- | CLI --grpc-enable wins over YAML EnableRpc: false.
336+
prop_rpcReload_cliOverridesYaml :: Property
337+
prop_rpcReload_cliOverridesYaml =
338+
H.propertyOnce $ do
339+
let yamlConfig = testPartialYamlConfig{pncRpcConfig = RpcConfig (Last (Just False)) mempty mempty}
340+
cliConfig = testPartialCliConfig
341+
{ pncRpcConfig = RpcConfig (Last (Just True)) mempty mempty
342+
, pncSocketConfig = testSocketConfigWithPath
343+
}
344+
merged = defaultPartialNodeConfiguration <> yamlConfig <> cliConfig
345+
NodeConfiguration{ncRpcConfig = RpcConfig{isEnabled}} <- evalEither $ makeNodeConfiguration merged
346+
isEnabled === Identity True
347+
293348
-- -----------------------------------------------------------------------------
294349

295350
tests :: IO Bool

0 commit comments

Comments
 (0)