H2 owner-pass-with-units-stopped: the owner pass at the engine's start starts again exactly what its stop found running

StopModule answers only what it found running (a container found stopped is not in it); StartModule starts that answer, services before timers, then containers.

Builds: design 47 H2 H-A-752 (amends H-A-735 (1)), H-A-641
This commit is contained in:
2026-11-25 18:52:01 +01:00
parent d4fb8ef371
commit 6d866665c2
5 changed files with 64 additions and 28 deletions
+3
View File
@@ -25,6 +25,9 @@ func TestTheNodeAgentsStopStopsAModuleAndRemovesNothing(t *testing.T) {
var asked []string
opts := options{state: path, runner: func(_ context.Context, name string, args ...string) (string, error) {
asked = append(asked, name+" "+strings.Join(args, " "))
if len(args) > 1 && args[0] == "container" && args[1] == "inspect" {
return "true\t\n", nil // found running: a container found stopped is not stopped (H-A-752)
}
return "", nil
}}
if res := stopComposition(context.Background(), opts, "app"); res.Failed {
+26 -5
View File
@@ -277,7 +277,17 @@ func TestTheOwnerPassAtTheEnginesStartStopsAndStartsTheUnits(t *testing.T) {
}
d := parse(t, fmt.Sprintf(`{"declaration":1,"resources":[
{"id":"app.state","type":"directory","path":%q,"mode":"0700","owner":"%d:%d"}]}`, dir, me+4242, myGroup+4242))
m := unitsMachine(map[string]*fakeUnit{"app.service": {active: "active", enabled: "enabled"}})
// H-A-752: only what the pass found running is stopped and started again, services before timers, then
// containers; a run-once step's service, a scheduled process's service off its timer, a service and a container
// found stopped stay stopped.
m := unitsMachine(map[string]*fakeUnit{
"app.service": {active: "active", enabled: "enabled"}, "app.timer": {active: "active", enabled: "enabled"},
"once.service": {active: "inactive", enabled: "disabled"},
"sched.timer": {active: "active", enabled: "enabled"},
"sched.service": {active: "inactive", enabled: "disabled"},
"idle.service": {active: "inactive", enabled: "enabled"},
})
m.containers = map[string]*fakeContainer{"app-box": {id: "a", running: true}, "app-off": {id: "b"}}
var events []string
run := func(ctx context.Context, name string, args ...string) (string, error) {
out, err := m.run(ctx, name, args...)
@@ -287,6 +297,9 @@ func TestTheOwnerPassAtTheEnginesStartStopsAndStartsTheUnits(t *testing.T) {
events = append(events, args[0]+" "+args[len(args)-1])
}
}
if name == "docker" && (args[0] == "stop" || args[0] == "start") {
events = append(events, "docker "+args[0]+" "+args[len(args)-1])
}
return out, err
}
wasContent, wasOwn, wasF := lchownContent, ownDirectory, fchownDirectory
@@ -301,12 +314,20 @@ func TestTheOwnerPassAtTheEnginesStartStopsAndStartsTheUnits(t *testing.T) {
ownDirectory = func(string, string) error { events = append(events, "own directory"); return nil }
fchownDirectory = func(int, int, int) error { events = append(events, "own directory"); return nil }
known := store.State{}
known.Record(store.Applied{ID: "app.run", Type: "service", Target: "app.service"})
known.Record(store.Applied{ID: "app.state", Type: "directory", Target: dir})
for _, r := range []store.Applied{
{ID: "app.run", Type: "service", Target: "app.service"}, {ID: "app.tick", Type: "service", Target: "app.timer"},
{ID: "app.once", Type: "service", Target: "once.service"}, {ID: "app.sched", Type: "process", Target: "sched"},
{ID: "app.idle", Type: "service", Target: "idle.service"},
{ID: "app.box", Type: "container", Target: "app-box"}, {ID: "app.off", Type: "container", Target: "app-off"},
{ID: "app.state", Type: "directory", Target: dir},
} {
known.Record(r)
}
ConvergeDirectories(context.Background(), archHost(t), d, known, run)
want := "stop app.service,give webhook.secret,own directory,start app.service"
stops := "stop app.timer,stop sched.timer,stop app.service,docker stop app-box"
want := stops + ",give webhook.secret,own directory,start app.service,start app.timer,start sched.timer,docker start app-box"
if fails {
want = "stop app.service"
want = stops
}
if got := strings.Join(events, ","); got != want {
t.Fatalf("events %q, want %q", got, want)
+6 -6
View File
@@ -24,7 +24,7 @@ func ConvergeDirectories(ctx context.Context, sys system.System, d *declaration.
placed := placedPaths(d.Resources)
// H-A-641: the owner pass runs here with the module's units stopped, as in an apply, and with no apply after it they are
// started again once every directory of the module is through; one a pass failed on leaves them stopped (H-A-701).
stopped, failed := map[string]bool{}, map[string]bool{}
stopped, failed := map[string][]string{}, map[string]bool{}
for _, r := range d.Resources {
dir, ok := r.(*declaration.Directory)
if !ok {
@@ -43,13 +43,13 @@ func ConvergeDirectories(ctx context.Context, sys system.System, d *declaration.
continue
}
module, ours := d.ModuleOfID(dir.ID)
if ours && !stopped[module] && ownerPassRuns(dir, rec, true) {
stopped[module] = true
if _, done := stopped[module]; ours && !done && ownerPassRuns(dir, rec, true) {
stopped[module] = nil
var err error
if sys == nil {
err = errors.New("this host is built for no system it knows")
} else {
_, err = StopModule(ctx, sys, known, module, run)
stopped[module], err = StopModule(ctx, sys, known, module, run)
}
if err != nil {
failed[module] = true
@@ -71,11 +71,11 @@ func ConvergeDirectories(ctx context.Context, sys system.System, d *declaration.
lines = append(lines, fmt.Sprintf("%s (%s) was %s, and is set right at this start", dir.Path, dir.ID, drift))
}
}
for module := range stopped {
for module, names := range stopped {
if failed[module] || sys == nil {
continue
}
if _, err := StartModule(ctx, sys, known, module, run); err != nil {
if _, err := StartModule(ctx, sys, known, module, names, run); err != nil {
lines = append(lines, fmt.Sprintf("the units of %s stopped for its owner pass could not be started again: %v", module, err))
}
}
+23 -14
View File
@@ -43,33 +43,38 @@ func unitsOf(known store.State, module string) (timers, services, containers []s
return timers, services, containers
}
// StartModule starts again what StopModule stopped: the module's services and timers found stopped, services first, and its
// containers found stopped (design 47 H2, H-A-641: a pass at the engine's start, with no apply after it, starts the units
// it stopped). It answers what it started, and the first failure.
func StartModule(ctx context.Context, sys system.System, known store.State, module string, run Runner) ([]string, error) {
// StartModule starts again exactly what StopModule found running and stopped, which is what it is given as stopped: the
// module's services, then its timers, then its containers, and nothing else (design 47 H2, H-A-752: a pass at the engine's
// start, with no apply after it, starts again the units it stopped, never a run-once step's service, a scheduled
// process's service off its timer or a unit found stopped before). It answers what it started, and the first failure.
func StartModule(ctx context.Context, sys system.System, known store.State, module string, stopped []string, run Runner) ([]string, error) {
timers, services, containers := unitsOf(known, module)
was := map[string]bool{}
for _, name := range stopped {
was[name] = true
}
var started []string
for _, r := range append(services, timers...) {
manager := managerFor(r.Scope, r.User, run)
if state, err := sys.ServiceState(ctx, manager, r.Target); err != nil || state != "stopped" {
if !was[r.Target] {
continue
}
manager := managerFor(r.Scope, r.User, run)
if err := sys.SetServiceState(ctx, manager, r.Target, "running"); err != nil {
return started, fmt.Errorf("starting %s of %s: %w", r.Target, module, err)
}
started = append(started, r.Target)
}
if len(containers) == 0 {
return started, nil
}
cri, err := containerRuntime(ctx, run)
if err != nil {
return started, err
}
var cri string
for _, r := range containers {
if state, err := containerState(ctx, r.Target, run); err != nil || state.Running {
if !was[r.Target] {
continue
}
if cri == "" {
var err error
if cri, err = containerRuntime(ctx, run); err != nil {
return started, err
}
}
if _, err := run(ctx, cri, "start", r.Target); err != nil {
return started, fmt.Errorf("starting the container %s of %s: %w", r.Target, module, err)
}
@@ -111,6 +116,10 @@ func StopModule(ctx context.Context, sys system.System, known store.State, modul
}
sort.Slice(containers, func(i, j int) bool { return containers[i].Target < containers[j].Target })
for _, r := range containers {
// A container found stopped is not in the answer, so it is not started again (H-A-752).
if state, err := containerState(ctx, r.Target, run); err != nil || !state.Running {
continue
}
if _, err := run(ctx, cri, "stop", r.Target); err != nil {
return stopped, fmt.Errorf("stopping the container %s of %s: %w", r.Target, module, err)
}
+6 -3
View File
@@ -19,11 +19,14 @@ func TestStopModuleStopsAModulesUnitsAndContainersAndRemovesNothing(t *testing.T
"app-worker.service": {active: "active", enabled: "enabled"},
"app-rt.service": {active: "active", enabled: "enabled"},
})
// A container found stopped is not stopped and not in the answer, so it is not started again (H-A-752).
m.containers = map[string]*fakeContainer{"app-box": {id: "a", running: true}, "app-idle": {id: "b"}}
state := store.State{}
for _, r := range []store.Applied{
{ID: "app.run", Type: "service", Target: "app.service"},
{ID: "app.tick", Type: "service", Target: "app.timer"},
{ID: "app.c", Type: "container", Target: "app-box"},
{ID: "app.i", Type: "container", Target: "app-idle"},
{ID: "app.worker", Type: "process", Target: "app-worker"},
{ID: "app.runtime", Type: "process", Target: "app-rt"},
{ID: "other.run", Type: "service", Target: "other.service"},
@@ -42,10 +45,10 @@ func TestStopModuleStopsAModulesUnitsAndContainersAndRemovesNothing(t *testing.T
m.units["app-worker.service"].active != "inactive" || m.units["app-rt.service"].active != "active" {
t.Fatalf("units are %+v", m.units)
}
if !m.did("docker stop app-box") || m.did("docker stop other-box") || m.did("docker rm") {
if !m.did("docker stop app-box") || m.did("docker stop app-idle") || m.did("docker stop other-box") || m.did("docker rm") {
t.Fatalf("the machine was asked %v", m.asked)
}
if len(state.Resources) != 7 {
t.Fatalf("a record was removed: %d left of 7", len(state.Resources))
if len(state.Resources) != 8 {
t.Fatalf("a record was removed: %d left of 8", len(state.Resources))
}
}