Merge pull request 'node-engine: back up a data module before its apply only when its move changed; a needless backup's hold ends (design 47 H2 H-A-771, H-A-772; issue 1984)' (#260) from fix/47-H2-771-unchanged-no-backup into main

This commit was merged in pull request #260.
This commit is contained in:
2026-11-26 03:34:35 +00:00
2 changed files with 215 additions and 41 deletions
+52 -6
View File
@@ -274,6 +274,10 @@ type Agent struct {
// composition, read once at the start (loadHad): what runs of a module the agent has not taken yet, ahead of the kept
// declaration (issue 924).
had map[string]compNow
// appliedKept is, for each module, the composition the engine's applied store kept at the start (loadApplied), as the
// store has it, never replaced by a have: with applied, what the machine applied, which the judgement of a move before
// its backup compares to (design 47 H2 H-A-771, H-A-772 (1)).
appliedKept map[string]compNow
// keptData is, for each module of the kept declaration whose manifest could be read, whether it declares data
// (Env.HoldsData): the Data of what runs of it from the kept declaration.
keptData map[string]bool
@@ -362,7 +366,7 @@ func New(env Env) *Agent {
if env.Say == nil {
env.Say = func(string) {}
}
return &Agent{env: env, wants: map[string]wantNow{}, comps: map[string]compNow{}, running: map[string]nodeengine.HaveByNodeModuleValue{}, applied: map[string]compNow{}, had: map[string]compNow{}, keptData: map[string]bool{},
return &Agent{env: env, wants: map[string]wantNow{}, comps: map[string]compNow{}, running: map[string]nodeengine.HaveByNodeModuleValue{}, applied: map[string]compNow{}, had: map[string]compNow{}, appliedKept: map[string]compNow{}, keptData: map[string]bool{},
attempt: map[string]attempt{}, checked: map[string]bool{}, prior: map[string]bool{}, said: map[string]string{}, waited: map[string]string{}, notComposed: map[string]notComposedMark{},
pulls: map[string]*pullWait{}, settledAt: map[string]time.Time{}, settledSeq: map[string]int{},
retry: make(chan []string, 8), reconcile: make(chan []string, 64), failedAgain: make(chan string, 8), tries: map[string]int{}, again: make(chan againAsk, 64),
@@ -1338,6 +1342,7 @@ func (a *Agent) loadApplied(ctx context.Context) {
}
a.mu.Lock()
a.had[m] = compNow{Raw: kept[m], C: c, Dig: dig}
a.appliedKept[m] = a.had[m]
a.mu.Unlock()
switch {
case unread:
@@ -1791,10 +1796,17 @@ func (a *Agent) baseline(ctx context.Context, module string, w wantNow, comp com
a.mu.Unlock()
if h.Phase == string(nodeengineclient.PhaseHeld) && h.Detail == BackupInterrupted && w.Want.Backup != meshwants.WantsByWantNodeModuleValueBackupSkip {
// Held only because this node agent stopped while the backup ran (postgres on novox, 2026-10-31): no
// failure and nothing for the operator to answer. It is backed up again and applied (a.prior makes the
// apply back it up first); the want still asking a backup is all the answer needed.
// failure and nothing for the operator to answer; the want still asking a backup is all the answer needed.
// It is applied (a.prior makes the apply take it as a module that ran): backed up again first when the
// judge finds its move changed or the want says again, else with no backup, and the words say which
// (design 47 H2 H-A-771, H-A-772 (1), (3)).
words := fmt.Sprintf("%s's backup was interrupted by this node agent's stop: it is backed up again", module)
if w.Want.Backup != meshwants.WantsByWantNodeModuleValueBackupAgain && !a.moveChanged(module, comp) {
words = fmt.Sprintf("%s's backup was interrupted by this node agent's stop, and its move to %s is judged "+
"unchanged: it is applied with no backup", module, short(comp.Dig))
}
a.mu.Lock()
a.sayOnce("interrupted:"+module, fmt.Sprintf("%s's backup was interrupted by this node agent's stop: it is backed up again", module))
a.sayOnce("interrupted:"+module, words)
a.mu.Unlock()
return false
}
@@ -1828,6 +1840,18 @@ func (a *Agent) baseline(ctx context.Context, module string, w wantNow, comp com
h.Wait = nil
a.writeHave(ctx, h)
}
if h.Phase == string(nodeengineclient.PhaseHeld) && h.Wait == nil && !judgedAgain(h) && recoveryCause(h) == recoveryNone &&
!a.moveChanged(module, comp) {
// Held for a failed backup of a move the judge finds unchanged (lidarr on ace, 2026-11-26 03:50): a backup
// H-A-771 no longer asks, so it is applied with no backup and its hold ends, said here; its ask is the
// assigner's to take back, as a stale ask is, once its have runs (design 47 H2 H-A-772 (2)). Only a failed
// backup's hold, which carries no wait: a revert's hold carries one and is never ended here (H-A-772 (3)).
a.mu.Lock()
a.sayOnce("unheld:"+module+":"+comp.Dig, fmt.Sprintf("%s's hold for a failed backup (%s) ends at this start: its "+
"move to %s is judged unchanged, so it is applied with no backup", module, h.Detail, short(comp.Dig)))
a.mu.Unlock()
return false
}
if h.Phase == string(nodeengineclient.PhaseHeld) && !judgedAgain(h) {
// Held for a failed backup, and the operator's answer is still to come: the hold is kept, nothing is
// backed up again or applied, and the next revision of the want is the answer (ADR 0568 §4). Said, so
@@ -1927,8 +1951,12 @@ func (a *Agent) apply(ctx context.Context, module string, w wantNow, comp compNo
}
// A data module is backed up before a new composition of it moves (ADR 0429 §14), unless the want says to go on
// without one (ADR 0539 §16).
if comp.C.Data && ranBefore && a.env.Backup != nil && w.Want.Backup != meshwants.WantsByWantNodeModuleValueBackupSkip {
// without one (ADR 0539 §16), and only when the judge finds the move from what the machine applied changed: a move
// judged unchanged (its one difference env.MESH_FINGERPRINT) changes nothing of the module, so it applies with no
// backup, no hold and no ask (design 47 H2 H-A-771). The operator's answer again backs it up whatever the judgement
// says (H-A-772 (1)).
if comp.C.Data && ranBefore && a.env.Backup != nil && w.Want.Backup != meshwants.WantsByWantNodeModuleValueBackupSkip &&
(w.Want.Backup == meshwants.WantsByWantNodeModuleValueBackupAgain || a.moveChanged(module, comp)) {
// What runs, read before the have says backing-up: a module known only from its have (a.prior) has no running
// entry, and its held have kept naming nothing (postgres on novox, 2026-10-31), as the not-composed mark keeps it.
a.mu.Lock()
@@ -2093,6 +2121,7 @@ func (a *Agent) remove(ctx context.Context, module string, w wantNow) {
delete(a.attempt, module)
delete(a.pulls, module)
delete(a.had, module)
delete(a.appliedKept, module)
a.running[module] = nodeengine.HaveByNodeModuleValue{Phase: nodeengineclient.PhaseRemoved}
a.mu.Unlock()
if a.env.Applied != nil {
@@ -2225,6 +2254,23 @@ func (a *Agent) waitsForYes(ctx context.Context, module string, w wantNow, comp
return true
}
// moveChanged says the judge finds moving module from what the machine applied to comp changed: any judgement but
// unchanged (design 47 H2 H-A-771). What the machine applied is the composition this engine last applied it from, else the
// one its applied store kept at the start, never a have, which may name a held composition not yet applied (H-A-772 (1),
// issue 1585's shape); a module with neither cannot be judged, and is changed. Called without a.mu held.
func (a *Agent) moveChanged(module string, comp compNow) bool {
a.mu.Lock()
from, known := a.applied[module]
if !known || from.Err != nil {
from, known = a.appliedKept[module]
}
a.mu.Unlock()
if !known {
return true
}
return judge.JudgeMove(&from.C, comp.C).Judgement != judge.JudgedUnchanged
}
// buildAlone says a move of module from what runs to the build want changes the build alone (ADR 0601 §1): another build than
// the one it runs, and the build it runs composed with today's settings and secrets is the composition it runs. Anything it
// cannot tell is no: the move is then asked of a person.
+163 -35
View File
@@ -1032,26 +1032,72 @@ func testADataModuleIsBackedUpFirstAndHeldWhenItFails(t *testing.T) {
}
// The live shape (nats on novox, 2026-11-04, issue 1585, the second failure): a module the node composes itself, of
// policy record, its want Self with the yes for its composition, and its held have naming that very composition (what
// runs is what the want names). The operator's answer acted on at the start, and in a revision of the want that
// arrives while the module is held; the engine's read tool says the want's answer and the hold.
// policy record, its want Self with the yes for its composition, and its held have naming that very composition, while
// the machine applied an older one (its applied store keeps it). The operator's answer acted on at the start, and in a
// revision of the want that arrives while the module is held; the engine's read tool says the want's answer and the
// hold. Design 47 H2 H-A-772: the judgement before the backup is of what the machine applied, never of the have, so
// those rows keep their backup and their hold; a hold for a failed backup of a move judged unchanged (lidarr on ace,
// 2026-11-26: its held have naming what it applied, the want's composition differing in env.MESH_FINGERPRINT alone)
// ends at the start with no backup, and again on such a move backs up once.
nbox := func(env map[string]string) json.RawMessage {
c := map[string]any{"id": "nats.server", "type": "container", "module": "nats", "name": "nats",
"image": "nats@sha256:" + strings.Repeat("5", 64)}
if env != nil {
c["env"] = env
}
raw, _ := json.Marshal(c)
return raw
}
for _, row := range []struct {
name string
atStart string // the answer the want carries when the engine starts
later string // the answer a later revision carries; "" writes none
backups int
holdsTill bool // it is held, and said held, before the later revision
unchanged bool // what the machine applied differs from the want's composition in env.MESH_FINGERPRINT alone
// detail is the held have's words ("" a failed backup's) and wait whether it carries a wait of step compose with
// them, as an interrupted backup's and a revert's do; says is what the journal says, never what it must not.
detail string
wait bool
says, never string
// fails is the words of a backup that fails ("" one that is done): the module is held before the later answer.
fails string
}{
{"answered before the start", meshwants.WantsByWantNodeModuleValueBackupAgain, "", 1, false},
{"answered while held", "", meshwants.WantsByWantNodeModuleValueBackupAgain, 1, true},
{"skipped while held", "", meshwants.WantsByWantNodeModuleValueBackupSkip, 0, true},
{"answered before the start", meshwants.WantsByWantNodeModuleValueBackupAgain, "", 1, false, false, "", false, "", "", ""},
{"answered while held", "", meshwants.WantsByWantNodeModuleValueBackupAgain, 1, true, false, "", false, "", "", ""},
{"skipped while held", "", meshwants.WantsByWantNodeModuleValueBackupSkip, 0, true, false, "", false, "", "", ""},
{"held, its move unchanged", "", "", 0, false, true, "", false, "nats's hold for a failed backup", "", ""},
{"again on an unchanged move", meshwants.WantsByWantNodeModuleValueBackupAgain, "", 1, false, true, "", false, "", "", ""},
// H-A-772 (3): every line said names what happens; an interrupted backup of an unchanged move is applied with none.
{"interrupted, its move unchanged", "", "", 0, false, true, BackupInterrupted, true,
"judged unchanged: it is applied with no backup", "it is backed up again", ""},
// H-A-772 (3): the end is of a failed backup's hold only; a revert's hold carries a wait and waits for the answer.
{"a revert's hold, its move unchanged", "", meshwants.WantsByWantNodeModuleValueBackupSkip, 0, true, true,
"restore point missing: nats's cache has none of form 1 taken before " + fp2, true, "", "nats's hold for a failed backup", ""},
// H-A-772 (1), (3): the want's again backs up an unchanged move whose backup was interrupted, and says so.
{"interrupted, again on an unchanged move", meshwants.WantsByWantNodeModuleValueBackupAgain, "", 1, false, true, BackupInterrupted, true,
"it is backed up again", "applied with no backup", ""},
// H-A-772 (3): a hold after a failed again stays until the operator answers again, whatever the judgement.
{"a failed again on an unchanged move", meshwants.WantsByWantNodeModuleValueBackupAgain, meshwants.WantsByWantNodeModuleValueBackupSkip, 1, false, true,
"", false, "", "nats's hold for a failed backup", "restic: the repository is locked"},
} {
t.Run(row.name, func(t *testing.T) {
r := newBed(t)
r.env.SelfComposed, r.kept = true, nil
r.env.Policy = func(context.Context, string) string { return catalogue.PolicyRecord }
ck, craw, dig := composition(t, "nats", fp2, true, file("nats", "x", "2"))
ran, wanted := file("nats", "x", "1"), file("nats", "x", "2")
if row.unchanged {
ran, wanted = nbox(map[string]string{"MESH_FINGERPRINT": fp1}), nbox(nil)
}
ck, craw, dig := composition(t, "nats", fp2, true, wanted)
r.comps.before(ck, craw)
ack, araw, adig := composition(t, "nats", fp1, true, ran)
r.comps.before(ack, araw)
r.env.Applied = &memApplied{kept: map[string][]byte{"nats": araw}}
heldFP, heldDig := fp2, dig // issue 1585: the held have names the want's composition, not applied
if row.unchanged {
heldFP, heldDig = fp1, adig // as the engine writes a hold since A-453: what the machine applied
}
wk := meshwants.WantsByWantNodeModuleKey(node, "nats")
selfWant := func(backup string) []byte {
raw, err := bucketstest.Want(meshwants.WantsByWantNodeModuleValue{Fp: fp2, Self: true, Yes: dig, Backup: backup})
@@ -1061,10 +1107,31 @@ func testADataModuleIsBackedUpFirstAndHeldWhenItFails(t *testing.T) {
return raw
}
r.wants.before(wk, selfWant(row.atStart))
r.haves.stored = map[string]Have{"nats": {Module: "nats", FP: fp2, Composed: dig, Phase: string(nodeengineclient.PhaseHeld),
Detail: "the backup failed: sh: exit status 1"}}
held := Have{Module: "nats", FP: heldFP, Composed: heldDig, Phase: string(nodeengineclient.PhaseHeld),
Detail: "the backup failed: sh: exit status 1"}
if row.detail != "" {
held.Detail = row.detail
}
if row.wait {
held.Wait = &nodeengine.Wait{Owner: nodeengine.Seat, Step: nodeengine.WaitStepCompose, Reason: held.Detail, Words: held.Detail}
}
r.haves.stored = map[string]Have{"nats": held}
if row.fails != "" {
r.backup = func(string) Backup { return Backup{Words: row.fails} }
}
stopR := r.run()
defer stopR()
if row.fails != "" {
eventually(t, "the failed backup did not hold the module", func() bool {
h := r.haves.last("nats")
return h.Phase == string(nodeengineclient.PhaseHeld) && h.Detail == row.fails
})
r.idle()
if got := r.appliedList(); len(got) != 0 {
t.Fatalf("applied behind the hold of a failed again with no later answer: %v", got)
}
r.wants.put(wk, selfWant(row.later))
}
if row.holdsTill {
eventually(t, "the hold at the start is not said", func() bool {
r.mu.Lock()
@@ -1107,6 +1174,14 @@ func testADataModuleIsBackedUpFirstAndHeldWhenItFails(t *testing.T) {
if n != row.backups {
t.Errorf("%d backups for the answer, want %d", n, row.backups)
}
r.mu.Lock()
defer r.mu.Unlock()
if row.says != "" && !slices.ContainsFunc(r.said, func(s string) bool { return strings.Contains(s, row.says) }) {
t.Errorf("the journal does not say %q: %q", row.says, r.said)
}
if row.never != "" && slices.ContainsFunc(r.said, func(s string) bool { return strings.Contains(s, row.never) }) {
t.Errorf("the journal says %q: %q", row.never, r.said)
}
})
}
}
@@ -1129,35 +1204,88 @@ func cutAsAHaveIs(s string) string {
return s[:100] + " … " + s[len(s)-(300-100-len(" … ")):]
}
// A data module with a good backup is applied after it; one that never ran has nothing to back up.
// A data module with a good backup is applied after it; one that never ran has nothing to back up. Design 47 H2 H-A-771: a
// data module that ran before is backed up first only when the judge finds its move changed (any judgement but
// unchanged); a move whose one difference is env.MESH_FINGERPRINT is judged unchanged and applied with no backup ask.
func testABackupDoneIsFollowedByTheApplyAndANewModuleIsNotBackedUp(t *testing.T) {
t.Run("a move across a kept data's form change is taken as a restore point and restored at a revert", testAMoveAcrossAFormChange)
b := newBed(t)
stop := b.run()
defer stop()
fp := fpOf("c")
ck, craw, dig := composition(t, "db", fp, true, file("db", "x", "1"))
wk, wraw := wantFor(t, "db", fp, dig, "")
b.comps.put(ck, craw)
b.wants.put(wk, wraw)
eventually(t, "not applied", func() bool { return len(b.appliedList()) == 1 })
b.mu.Lock()
n := len(b.backups)
b.mu.Unlock()
if n != 0 {
t.Fatalf("a module that never ran was backed up (%d)", n)
fp, fp2 := fpOf("c"), fpOf("d")
box := func(image string, env map[string]string) json.RawMessage {
c := map[string]any{"id": "db.server", "type": "container", "module": "db", "name": "db",
"image": "db@sha256:" + strings.Repeat(image, 64), "volumes": []string{"/var/lib/db/data:/data"}}
if env != nil {
c["env"] = env
}
raw, _ := json.Marshal(c)
return raw
}
fp2 := fpOf("d")
ck2, craw2, dig2 := composition(t, "db", fp2, true, file("db", "x", "2"))
wk2, wraw2 := wantFor(t, "db", fp2, dig2, "")
b.comps.put(ck2, craw2)
b.wants.put(wk2, wraw2)
eventually(t, "second not applied", func() bool { return len(b.appliedList()) == 2 })
b.mu.Lock()
n = len(b.backups)
b.mu.Unlock()
if n != 1 {
t.Fatalf("a module that ran before was backed up %d times, want once", n)
dir := func(path string) json.RawMessage {
raw, _ := json.Marshal(map[string]any{"id": "db.data", "type": "directory", "module": "db", "path": path})
return raw
}
data := dir("/var/lib/db/data")
for _, row := range []struct {
name string
from, to []json.RawMessage
judged judge.Judgement
backedUp bool
}{
{"a file of it changes", []json.RawMessage{file("db", "x", "1")}, []json.RawMessage{file("db", "x", "2")},
judge.JudgedNeither, true},
{"its container recreates", []json.RawMessage{box("1", nil), data}, []json.RawMessage{box("2", nil), data},
judge.JudgedRecreates, true},
{"its data moves", []json.RawMessage{box("1", nil), data}, []json.RawMessage{box("1", nil), dir("/srv/db/data")},
judge.JudgedMovesData, true},
// H-A-771's live case (lidarr on ace, 2026-11-26 03:40): its container composed by an earlier node-engine with
// MESH_FINGERPRINT, the new composition with none (H-A-710 (1)).
{"only env.MESH_FINGERPRINT is taken out", []json.RawMessage{box("1", map[string]string{"MESH_FINGERPRINT": fp}), data},
[]json.RawMessage{box("1", nil), data}, judge.JudgedUnchanged, false},
{"only the value of env.MESH_FINGERPRINT differs", []json.RawMessage{box("1", map[string]string{"MESH_FINGERPRINT": fp}), data},
[]json.RawMessage{box("1", map[string]string{"MESH_FINGERPRINT": fp2}), data}, judge.JudgedUnchanged, false},
} {
t.Run(row.name, func(t *testing.T) {
if j := judge.JudgeMove(&compositions.Composed{Node: node, Module: "db", FP: fp, Data: true, Resources: row.from},
compositions.Composed{Node: node, Module: "db", FP: fp2, Data: true, Resources: row.to}); j.Judgement != row.judged {
t.Fatalf("the move is judged %s (%s), want %s", j.Judgement, j.Words, row.judged)
}
b := newBed(t)
stop := b.run()
defer stop()
ck, craw, dig := composition(t, "db", fp, true, row.from...)
wk, wraw := wantFor(t, "db", fp, dig, "")
b.comps.put(ck, craw)
b.wants.put(wk, wraw)
eventually(t, "not applied", func() bool { return len(b.appliedList()) == 1 })
b.mu.Lock()
n := len(b.backups)
b.mu.Unlock()
if n != 0 {
t.Fatalf("a module that never ran was backed up (%d)", n)
}
ck2, craw2, dig2 := composition(t, "db", fp2, true, row.to...)
wk2, wraw2 := wantFor(t, "db", fp2, dig2, "")
b.comps.put(ck2, craw2)
b.wants.put(wk2, wraw2)
eventually(t, "second not applied", func() bool {
h := b.haves.last("db")
return len(b.appliedList()) == 2 && h.Phase == string(nodeengineclient.PhaseRunning) && h.FP == fp2 && h.Composed == dig2
})
b.mu.Lock()
n = len(b.backups)
b.mu.Unlock()
if want := map[bool]int{true: 1, false: 0}[row.backedUp]; n != want {
t.Fatalf("a module that ran before, its move judged %s, was backed up %d times, want %d", row.judged, n, want)
}
if !row.backedUp {
b.haves.mu.Lock()
defer b.haves.mu.Unlock()
for _, h := range b.haves.log {
if h.Module == "db" && (h.Phase == string(nodeengineclient.PhaseBackingUp) || h.Phase == string(nodeengineclient.PhaseHeld)) {
t.Errorf("an unchanged move's have says %s: %+v", h.Phase, h)
}
}
}
})
}
}