Watch
0
0
Fork
You've already forked hyperhive
0

start tuwunel after the container is up, under its own 30 min limit

This commit is contained in:
atlas 2026-11-28 02:48:25 +01:00 • committed by mara
commit ba0f9c6264
9 changed files with 220 additions and 3 deletions

View file

@ -52,6 +52,7 @@ jobs:
- swarm-archive
- swarm-builder-offline
- tuwunel-fallocate-probe
- container-deferred-start
- full-setup
- semi-spread
steps:

View file

@ -37,6 +37,14 @@ Same shape as [`gateway.md::hive-forge container shape`](../../4-implementation/
write-ahead logs pin far more disk than they hold there. On any other
filesystem tuwunel's default stays. A probe that can't decide says why in
the journal (`tuwunel-fallocate-probe:`) and leaves the default.
- tuwunel starts after the container is up, not during its boot. The
container's start job (`container@hive-matrix.service`, one minute) ends when
the guest's boot does, and a oneshot in the guest starts tuwunel without
waiting for it. tuwunel's own start, which replays the database's
write-ahead log from the beginning at every open, has its own limit of 30
minutes, and extends it while it reports progress. While it starts, the
gateway answers for the homeserver with an error, and the swarm's matrix
clients retry.
## Identity vs API listener: `serverName` vs `gatewayHost`

View file

@ -85,7 +85,7 @@ Tests that boot NixOS VMs are the `vmTests.<system>.<name>` flake output
builds them. It does build the `vm-test-typecheck-<name>` checks
(`nix/vm-test-typecheck.nix`): the type check and lint the test driver runs over
a test script, on the script text alone. They evaluate no node configuration
and build no VM. They cover all eight tests. `semi-spread` and `swarm-builder-offline`
and build no VM. They cover every test. `semi-spread` and `swarm-builder-offline`
read node config or an agent toplevel into their script, so each builds the script
with stand-in values (`passthru.typecheckScript`), from the same function as the real
one.

View file

@ -18,7 +18,17 @@
# a reason. It also adds the budgets of units ordered one after another by
# `after`/`before`, and fails when the longest such chain reaches the limit (a
# unit listed there adds 0 s) or the ordering has a cycle.
{ config, lib, ... }:
#
# A daemon whose own start can outlast that limit (a database open that replays
# a large log) is named in `startAfterReady`: it leaves the boot transaction, so
# the container is up without it, and a oneshot in the transaction starts it
# `--no-block`.
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.hyperhive.swarmContainer;
in
@ -35,6 +45,22 @@ in
'';
};
startAfterReady = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "tuwunel" ];
description = ''
Names of this container's services (without `.service`) that start
after the container's boot instead of in it. Each stops being wanted
by `multi-user.target`, and `swarm-container-deferred-start.service`,
which is, starts them with `systemctl start --no-block`. The host's
start job for the container then ends when the guest's boot does,
whatever the service's own start takes. The service keeps its own
`TimeoutStartSec`, and whatever depends on it has to cope with a
service that is still starting.
'';
};
writesOwnResolvConf = lib.mkOption {
type = lib.types.bool;
default = true;
@ -68,5 +94,26 @@ in
(lib.mkIf cfg.writesOwnResolvConf {
networking.resolvconf.enable = lib.mkForce false;
})
(lib.mkIf (cfg.startAfterReady != [ ]) {
systemd.services =
lib.genAttrs cfg.startAfterReady (_: {
wantedBy = lib.mkForce [ ];
})
// {
# Its own job ends at the enqueue, so the boot does not wait on the
# services it starts.
swarm-container-deferred-start = {
description = "start the services that outlast the container's start limit";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
TimeoutStartSec = 20;
ExecStart = "${pkgs.systemd}/bin/systemctl start --no-block ${
lib.concatMapStringsSep " " (n: "${n}.service") cfg.startAfterReady
}";
};
};
};
})
];
}

View file

@ -969,6 +969,11 @@ in
services.hyperhive.swarmContainer = {
inherit privateNetwork;
writesOwnResolvConf = false;
# The container's start limit is one minute and tuwunel's start
# replays the write-ahead log, which RocksDB redoes from the start
# at every open: a start killed before it ends makes no progress.
# tuwunel starts after the container is up, under its own limit.
startAfterReady = [ "tuwunel" ];
};
# Swarm-internal trust reaches tuwunel at RUNTIME, not via
@ -1102,6 +1107,13 @@ in
# Upstream's `ProtectSystem = "strict"` leaves `/run` read-only.
systemd.services.tuwunel.serviceConfig.ReadWritePaths = [ socketDir ];
# tuwunel asks systemd for 30 s more every 15 s while it opens the
# database (`EXTEND_TIMEOUT_USEC`), which moves the deadline past
# this value for as long as it reports; this is the limit when it does
# not. 30 min is ten times the 3 min of CPU a replay has already used
# without ending.
systemd.services.tuwunel.serviceConfig.TimeoutStartSec = "30min";
# The probe's file lives in upstream's `RuntimeDirectory`.
systemd.services.tuwunel.serviceConfig.ExecStartPre =
(fallocateProbe.serviceConfig "/var/lib/${stateName}").ExecStartPre;

View file

@ -267,7 +267,6 @@ let
"grafana.service"
"nats.service"
"opentelemetry-collector.service"
"tuwunel.service"
"victorialogs.service"
"victoriametrics.service"
];
@ -986,6 +985,35 @@ let
&& c.timeoutStartSec == "1min"
&& startLimitOf "hive-matrix" == 60;
}
{
# tuwunel's start (a database open that replays the write-ahead log from
# the beginning every time) can outlast the container's one-minute limit,
# and a start killed on it makes no progress. So tuwunel is out of the
# guest's boot: not wanted by `multi-user.target`, started `--no-block` by
# a oneshot that is, with a limit of its own. The sync and the render stay
# in the boot wait, the presence arms of the walk that finds tuwunel
# absent from it.
name = "the homeserver starts after the container is up, under a limit of its own, and the container's boot does not wait on it";
ok =
let
host = serviceContainersWithCi;
g = host.containers.hive-matrix.config;
unit = g.systemd.services.tuwunel;
starter = g.systemd.services.swarm-container-deferred-start;
booted = bootWait.bootSet g.systemd;
in
g.services.hyperhive.swarmContainer.startAfterReady == [ "tuwunel" ]
&& unit.wantedBy == [ ]
&& bootWait.spanSeconds unit.serviceConfig.TimeoutStartSec == 1800
&& starter.wantedBy == [ "multi-user.target" ]
&& starter.serviceConfig.Type == "oneshot"
&& starter.serviceConfig.TimeoutStartSec == 20
&& lib.hasSuffix "/bin/systemctl start --no-block tuwunel.service" starter.serviceConfig.ExecStart
&& !(lib.elem "tuwunel.service" booted)
&& lib.elem "swarm-container-deferred-start.service" booted
&& lib.elem "swarm-matrix-appservice-sync.service" booted
&& lib.elem "swarm-matrix-appservice-render.service" booted;
}
{
# On the bridge by DHCP, with all three listeners on sockets: nothing
# the bridge can dial. Every vhost location that forwards goes through

View file

@ -136,6 +136,7 @@ let
semi-spread
swarm-builder-offline
tuwunel-fallocate-probe
container-deferred-start
;
};
in

View file

@ -16,5 +16,6 @@
swarm-builder-offline = import ./vm-tests/swarm-builder-offline.nix { inherit pkgs self; };
full-setup = import ./vm-tests/full-setup.nix { inherit pkgs self; };
tuwunel-fallocate-probe = import ./vm-tests/tuwunel-fallocate-probe.nix { inherit pkgs; };
container-deferred-start = import ./vm-tests/container-deferred-start.nix { inherit pkgs; };
semi-spread = import ./vm-tests/semi-spread.nix { inherit pkgs self; };
}

View file

@ -0,0 +1,119 @@
# `vmTests.<system>.container-deferred-start`: a service container whose daemon
# takes longer to start than the container's one-minute start limit, with and
# without the daemon named in `services.hyperhive.swarmContainer.startAfterReady`
# (../container-modules/swarm-container.nix). The daemon stands in for tuwunel:
# a `Type=notify` unit wanted by `multi-user.target` whose start, a pause of 90 s
# before it reports ready, is longer than the limit.
#
# What it pins:
# - `old`, the daemon in the boot as nixpkgs ships it: the container's start
# job fails with `start operation timed out`, which is what pr1ma's
# homeserver does at every attempt (the control);
# - `new`, the daemon named in `startAfterReady`: the container's start job
# ends inside the limit with the daemon still `activating`, the daemon is
# `active` once its pause is over, and the container's unit was never
# restarted;
# - a unit that depends on the daemon and retries (the shape of the homeserver's
# clients) fails while the daemon starts and succeeds once it is ready.
#
# The module-eval suite pins the same wiring on the real homeserver.
{ pkgs }:
let
# What each container's daemon waits before it reports ready: past the
# container's 60 s limit with the guest's own boot added.
pause = 90;
daemon = pkgs.writeShellScript "slow-daemon" ''
${pkgs.coreutils}/bin/sleep ${toString pause}
${pkgs.systemd}/bin/systemd-notify --ready
exec ${pkgs.coreutils}/bin/sleep infinity
'';
guest =
{ deferred }:
{
imports = [ ../container-modules/swarm-container.nix ];
system.stateVersion = "26.05";
services.hyperhive.swarmContainer = {
privateNetwork = false;
startAfterReady = pkgs.lib.optional deferred "slow";
};
systemd.services.slow = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "notify";
NotifyAccess = "all";
ExecStart = "${daemon}";
TimeoutStartSec = "30min";
};
};
# Retries until the daemon is up, the way the homeserver's clients do.
systemd.services.client = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
TimeoutStartSec = 20;
ExecStart = "${pkgs.systemd}/bin/systemctl is-active --quiet slow.service";
Restart = "on-failure";
RestartSec = 5;
};
};
};
in
pkgs.testers.runNixOSTest {
name = "container-deferred-start";
# Without this the driver allows an hour, the runner's whole job limit.
globalTimeout = 900;
nodes.host =
{ ... }:
{
containers.old = {
autoStart = false;
config = guest { deferred = false; };
};
containers.new = {
autoStart = false;
config = guest { deferred = true; };
};
};
testScript = ''
host.start()
host.wait_for_unit("multi-user.target")
def inside(name: str, cmd: str) -> str:
return host.succeed(f"nixos-container run {name} -- {cmd}").strip()
with subtest("control: a daemon in the boot that outlasts the limit fails the container's start"):
host.fail("systemctl start container@old.service")
out = host.succeed("journalctl -u container@old.service --no-pager -o cat")
assert "start operation timed out" in out, out
host.succeed("systemctl stop container@old.service; systemctl reset-failed container@old.service")
with subtest("a daemon named in startAfterReady: the container's start ends inside the limit"):
host.succeed("systemctl start container@new.service")
out = host.succeed("journalctl -u container@new.service --no-pager -o cat")
assert "start operation timed out" not in out, out
host.succeed("systemctl is-active container@new.service")
state = inside("new", "systemctl show -p ActiveState --value slow.service")
assert state == "activating", state
with subtest("the dependant fails while the daemon starts"):
assert inside("new", "systemctl show -p ActiveState --value client.service") != "active"
with subtest("the daemon finishes its start, and the dependant recovers"):
host.wait_until_succeeds("nixos-container run new -- systemctl is-active --quiet slow.service", timeout=300)
host.wait_until_succeeds("nixos-container run new -- systemctl is-active --quiet client.service", timeout=60)
out = host.succeed("journalctl -u container@new.service --no-pager -o cat")
assert "start operation timed out" not in out, out
host.succeed("systemctl is-active container@new.service")
restarts = host.succeed("systemctl show -p NRestarts --value container@new.service").strip()
assert restarts == "0", restarts
host.succeed("systemctl stop container@new.service")
'';
}