Skip to content

Commit c7bce17

Browse files
authored
Merge pull request IntersectMBO#5537 from input-output-hk/svc-topo-opt
imp: nixos node svc topo moved to /etc to allow svc SIGHUP
2 parents 978b82a + 0893454 commit c7bce17

File tree

5 files changed

+62
-27
lines changed

5 files changed

+62
-27
lines changed

nix/nixos/cardano-node-service.nix

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@ let
1111
suffixDir = base: i: "${base}${optionalString (i != 0) "-${toString i}"}";
1212
nullOrStr = types.nullOr types.str;
1313
funcToOr = t: types.either t (types.functionTo t);
14+
15+
newTopology = i: {
16+
localRoots = map (g: {
17+
accessPoints = map (e: builtins.removeAttrs e ["valency"]) g.accessPoints;
18+
advertise = g.advertise or false;
19+
valency = g.valency or (length g.accessPoints);
20+
}) (cfg.producers ++ (cfg.instanceProducers i));
21+
publicRoots = map (g: {
22+
accessPoints = map (e: builtins.removeAttrs e ["valency"]) g.accessPoints;
23+
advertise = g.advertise or false;
24+
}) (cfg.publicProducers ++ (cfg.instancePublicProducers i));
25+
} // optionalAttrs (cfg.usePeersFromLedgerAfterSlot != null) {
26+
useLedgerAfterSlot = cfg.usePeersFromLedgerAfterSlot;
27+
};
28+
29+
oldTopology = i: {
30+
Producers = concatMap (g: map (a: {
31+
addr = a.address;
32+
inherit (a) port;
33+
valency = a.valency or 1;
34+
}) g.accessPoints) (
35+
cfg.producers ++ (cfg.instanceProducers i) ++ cfg.publicProducers ++ (cfg.instancePublicProducers i)
36+
);
37+
};
38+
39+
selectTopology = i:
40+
if cfg.topology != null
41+
then cfg.topology
42+
else toFile "topology.yaml" (toJSON (if (cfg.useNewTopology) then newTopology i else oldTopology i));
43+
44+
topology = i:
45+
if cfg.useSystemdReload
46+
then "/etc/cardano-node/topology-${toString i}.yaml"
47+
else selectTopology i;
48+
1449
mkScript = cfg:
1550
let baseConfig =
1651
recursiveUpdate
@@ -49,32 +84,6 @@ let
4984
instanceConfig = recursiveUpdate (baseInstanceConfig i) (cfg.extraNodeInstanceConfig i);
5085
nodeConfigFile = if (cfg.nodeConfigFile != null) then cfg.nodeConfigFile
5186
else toFile "config-${toString cfg.nodeId}-${toString i}.json" (toJSON instanceConfig);
52-
newTopology = {
53-
localRoots = map (g: {
54-
accessPoints = map (e: builtins.removeAttrs e ["valency"]) g.accessPoints;
55-
advertise = g.advertise or false;
56-
valency = g.valency or (length g.accessPoints);
57-
}) (cfg.producers ++ (cfg.instanceProducers i));
58-
publicRoots = map (g: {
59-
accessPoints = map (e: builtins.removeAttrs e ["valency"]) g.accessPoints;
60-
advertise = g.advertise or false;
61-
}) (cfg.publicProducers ++ (cfg.instancePublicProducers i));
62-
} // optionalAttrs (cfg.usePeersFromLedgerAfterSlot != null) {
63-
useLedgerAfterSlot = cfg.usePeersFromLedgerAfterSlot;
64-
};
65-
oldTopology = {
66-
Producers = concatMap (g: map (a: {
67-
addr = a.address;
68-
inherit (a) port;
69-
valency = a.valency or 1;
70-
}) g.accessPoints) (
71-
cfg.producers ++ (cfg.instanceProducers i) ++ cfg.publicProducers ++ (cfg.instancePublicProducers i)
72-
);
73-
};
74-
topology = if cfg.topology != null then cfg.topology else toFile "topology.yaml" (toJSON (
75-
if (cfg.useNewTopology) then newTopology
76-
else oldTopology
77-
));
7887
consensusParams = {
7988
RealPBFT = [
8089
"${lib.optionalString (cfg.signingKey != null)
@@ -108,7 +117,7 @@ let
108117
"${cfg.executable} run"
109118
"--config ${nodeConfigFile}"
110119
"--database-path ${instanceDbPath}"
111-
"--topology ${topology}"
120+
"--topology ${topology i}"
112121
] ++ lib.optionals (!cfg.systemdSocketActivation) ([
113122
"--host-addr ${cfg.hostAddr}"
114123
"--port ${if (cfg.shareIpv4port || cfg.shareIpv6port) then toString cfg.port else toString (cfg.port + i)}"
@@ -143,6 +152,7 @@ in {
143152
(the blockchain protocols running cardano).
144153
'';
145154
};
155+
146156
instances = mkOption {
147157
type = types.int;
148158
default = 1;
@@ -511,6 +521,21 @@ in {
511521
'';
512522
};
513523

524+
useSystemdReload = mkOption {
525+
type = types.bool;
526+
default = false;
527+
description = ''
528+
If set, systemd will reload cardano-node service units instead of restarting them
529+
if only the topology file has changed and p2p is in use.
530+
531+
Cardano-node topology files will be stored in /etc as:
532+
/etc/cardano-node/topology-''${toString i}.yaml
533+
534+
Enabling this option will also allow direct topology edits for tests when a full
535+
service re-deployment is not desired.
536+
'';
537+
};
538+
514539
nodeConfig = mkOption {
515540
type = types.attrs // {
516541
merge = loc: foldl' (res: def: recursiveUpdate res def.value) {};
@@ -642,6 +667,10 @@ in {
642667
isSystemUser = true;
643668
};
644669

670+
environment.etc = mkIf cfg.useSystemdReload (foldl'
671+
(acc: i: recursiveUpdate acc {"cardano-node/topology-${toString i}.yaml".source = selectTopology i;}) {}
672+
(range 0 (cfg.instances - 1)));
673+
645674
## TODO: use http://hackage.haskell.org/package/systemd for:
646675
## 1. only declaring success after we perform meaningful init (local state recovery)
647676
## 2. heartbeat & watchdog functionality
@@ -655,10 +684,12 @@ in {
655684
wants = [ "network-online.target" ];
656685
wantedBy = [ "multi-user.target" ];
657686
partOf = mkIf (cfg.instances > 1) ["cardano-node.service"];
687+
reloadTriggers = mkIf (cfg.useSystemdReload && cfg.useNewTopology) [ (selectTopology i) ];
658688
script = mkScript cfg i;
659689
serviceConfig = {
660690
User = "cardano-node";
661691
Group = "cardano-node";
692+
ExecReload = mkIf (cfg.useSystemdReload && cfg.useNewTopology) "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
662693
Restart = "always";
663694
RuntimeDirectory = lib.mkIf (!cfg.systemdSocketActivation)
664695
(lib.removePrefix runDirBase (runtimeDir i));

nix/svclib.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ let
240240
systemd.services = mkOption {};
241241
assertions = [];
242242
users = mkOption {};
243+
environment = mkOption {};
243244
};
244245
in pkgs.writeScript "run-${svcName}" ''
245246
#!${pkgs.runtimeShell}

nix/workbench/service/generator.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ let
100100
systemd.sockets = mkOption {};
101101
users = mkOption {};
102102
assertions = mkOption {};
103+
environment = mkOption {};
103104
};
104105
eval = let
105106
extra = {

nix/workbench/service/nodes.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ let
164164
systemd.sockets = mkOption {};
165165
users = mkOption {};
166166
assertions = mkOption {};
167+
environment = mkOption {};
167168
};
168169
eval = let
169170
extra = {

nix/workbench/service/tracer.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ let
4141
systemd.sockets = mkOption {};
4242
users = mkOption {};
4343
assertions = mkOption {};
44+
environment = mkOption {};
4445
};
4546
eval =
4647
let

0 commit comments

Comments
 (0)