Implement drain control via an external marker file. The daemon polls
$XDG_CONFIG_HOME/archie/.drain_request.json (the repo's config-home
convention) for a drain request, validates it against the current
instantiation epoch (kernel boot id + PID 1 start time) and, only when
the marker is live, cancels the boot context to trigger the same
graceful shutdown an operator SIGTERM does.
A marker written against a prior instantiation (reboot or PID 1/container
restart) is stale and ignored, so a leftover request never drains a
daemon that came up after it was written. An unreadable marker or epoch
fails closed to no drain. The daemon never invents a new shutdown path.
domain/drain owns the pure decision (Epoch, Marker, Decide);
infrastructure/drain reads the marker file and /proc boot facts;
app/archied wires a poller that cancels the root context on a valid
request.
Path note: the epic named ~/.archie/.drain_request.json, but archied
resolves every per-user path under ${XDG_CONFIG_HOME:-~/.config}/archie
(see AGENTS.md 'Config path'), so the marker lives there too rather than
at a path this daemon never uses.
Refs bead archie-core-1786637496817-266-06b64a72 (drain control: marker
file and instantiation epoch), part of the operations epic
archie-core-1786637496703-262-1e49aef1.
Tests: table-driven Decide (epoch mismatch -> stale, matching -> valid,
nil -> none, unreadable -> stale), marker round-trip from operator JSON,
and monitor (valid -> shutdown, stale/none/error -> no shutdown).
99 lines
2.8 KiB
Go
99 lines
2.8 KiB
Go
package drain
|
|
|
|
import "testing"
|
|
|
|
func TestDecideTable(t *testing.T) {
|
|
bootA := Epoch{BootID: "boot-a", PID1Start: 100}
|
|
bootB := Epoch{BootID: "boot-b", PID1Start: 100}
|
|
|
|
tests := []struct {
|
|
name string
|
|
current Epoch
|
|
marker *Marker
|
|
want Decision
|
|
}{
|
|
{
|
|
name: "no marker reports none",
|
|
current: bootA,
|
|
marker: nil,
|
|
want: DecisionNone,
|
|
},
|
|
{
|
|
name: "marker matching current epoch is valid",
|
|
current: bootA,
|
|
marker: &Marker{InstantiationEpoch: bootA},
|
|
want: DecisionValid,
|
|
},
|
|
{
|
|
name: "marker written against a prior boot is stale",
|
|
current: bootA,
|
|
marker: &Marker{InstantiationEpoch: bootB},
|
|
want: DecisionStale,
|
|
},
|
|
{
|
|
name: "marker written against a prior pid1 start within same boot is stale",
|
|
current: bootA,
|
|
marker: &Marker{InstantiationEpoch: Epoch{BootID: "boot-a", PID1Start: 99}},
|
|
want: DecisionStale,
|
|
},
|
|
{
|
|
name: "marker still valid when operator fields present",
|
|
current: bootA,
|
|
marker: &Marker{InstantiationEpoch: bootA, Reason: "maintenance", RequestedAt: "2026-09-05T00:00:00Z"},
|
|
want: DecisionValid,
|
|
},
|
|
{
|
|
name: "empty current epoch fails closed to stale even on matching marker",
|
|
current: Epoch{},
|
|
marker: &Marker{InstantiationEpoch: bootA},
|
|
want: DecisionStale,
|
|
},
|
|
{
|
|
name: "zero pid1 start in current epoch fails closed to stale",
|
|
current: Epoch{BootID: "boot-a", PID1Start: 0},
|
|
marker: &Marker{InstantiationEpoch: Epoch{BootID: "boot-a", PID1Start: 0}},
|
|
want: DecisionStale,
|
|
},
|
|
{
|
|
name: "empty boot id in current epoch fails closed to stale",
|
|
current: Epoch{BootID: "", PID1Start: 100},
|
|
marker: &Marker{InstantiationEpoch: Epoch{BootID: "", PID1Start: 100}},
|
|
want: DecisionStale,
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if got := Decide(test.current, test.marker); got != test.want {
|
|
t.Fatalf("Decide() = %v, want %v", got, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEpochEqual(t *testing.T) {
|
|
if !(Epoch{BootID: "b", PID1Start: 1}).Equal(Epoch{BootID: "b", PID1Start: 1}) {
|
|
t.Fatal("equal epochs reported unequal")
|
|
}
|
|
if (Epoch{BootID: "b", PID1Start: 1}).Equal(Epoch{BootID: "b", PID1Start: 2}) {
|
|
t.Fatal("epochs differing only in pid1 start reported equal")
|
|
}
|
|
if (Epoch{BootID: "a", PID1Start: 1}).Equal(Epoch{BootID: "b", PID1Start: 1}) {
|
|
t.Fatal("epochs differing only in boot id reported equal")
|
|
}
|
|
}
|
|
|
|
func TestEpochEmpty(t *testing.T) {
|
|
if !(Epoch{}).Empty() {
|
|
t.Fatal("zero epoch should be empty")
|
|
}
|
|
if !(Epoch{BootID: "a", PID1Start: 0}).Empty() {
|
|
t.Fatal("zero pid1 start should be empty")
|
|
}
|
|
if !(Epoch{BootID: "", PID1Start: 5}).Empty() {
|
|
t.Fatal("empty boot id should be empty")
|
|
}
|
|
if (Epoch{BootID: "a", PID1Start: 5}).Empty() {
|
|
t.Fatal("well-formed epoch should not be empty")
|
|
}
|
|
}
|