diff --git a/cmd/mesh-host/nodeagent.go b/cmd/mesh-host/nodeagent.go index 5c10a5b0..13e96beb 100644 --- a/cmd/mesh-host/nodeagent.go +++ b/cmd/mesh-host/nodeagent.go @@ -90,8 +90,15 @@ func (h sessionHaves) Read(ctx context.Context, module string) (agent.Have, bool if !ok { return agent.Have{}, false } + return haveOf(module, got), true +} + +// haveOf is a module's have value as the agent reads it: every read gives the run, the pipeline and the want revision it +// was applied from, so a have written over it (held, failed, waiting) keeps them (H-A-504 fourth addendum). +func haveOf(module string, got nodeengine.HaveByNodeModuleValue) agent.Have { return agent.Have{Module: module, FP: got.Fp, Composed: got.Composed, Phase: string(got.Phase), Detail: got.Detail, - Sealed: got.SealedAt, Wait: got.Wait}, true + Sealed: got.SealedAt, Wait: got.Wait, Run: got.Run, Pipeline: got.Pipeline, + WantRevision: uint64(max(got.WantRevision, 0))} } // ReadErr is Read telling a have absent from one the bus could not read (agent.HavesReadErr). @@ -114,8 +121,7 @@ func (h sessionHaves) ReadAt(ctx context.Context, module string) (agent.Have, ui if !ok || err != nil { return agent.Have{}, revision, false, err } - return agent.Have{Module: module, FP: got.Fp, Composed: got.Composed, Phase: string(got.Phase), Detail: got.Detail, - Sealed: got.SealedAt, Wait: got.Wait, Run: got.Run, Pipeline: got.Pipeline}, revision, true, nil + return haveOf(module, got), revision, true, nil } // WriteAt writes a module's have only while its key is at the revision read (agent.HavesAt, H-A-480). @@ -142,7 +148,8 @@ func moduleFact(x agent.Have) link.ModuleFact { } return link.ModuleFact{Module: x.Module, FP: x.FP, Composed: x.Composed, Phase: phase, Failed: phase == string(nodeengineclient.PhaseFailed), Detail: shorten(x.Detail), Judgement: x.Judgement, Waits: x.Waits, - BuildAlone: x.BuildAlone, Kept: x.Kept, Sealed: x.Sealed, PullWait: x.PullWait, Wait: x.Wait, Run: x.Run, Pipeline: x.Pipeline} + BuildAlone: x.BuildAlone, Kept: x.Kept, Sealed: x.Sealed, PullWait: x.PullWait, Wait: x.Wait, Run: x.Run, Pipeline: x.Pipeline, + WantRevision: x.WantRevision} } func (h sessionHaves) Removed(ctx context.Context, module string) { diff --git a/cmd/mesh-host/recovery_integration_test.go b/cmd/mesh-host/recovery_integration_test.go index 4c8d2cdb..4267c4d1 100644 --- a/cmd/mesh-host/recovery_integration_test.go +++ b/cmd/mesh-host/recovery_integration_test.go @@ -21,6 +21,7 @@ import ( "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" + "github.com/novox/mesh-host/internal/agent" "github.com/novox/mesh-host/internal/buckets" "github.com/novox/mesh-host/internal/link" "github.com/novox/mesh-host/internal/nodegrant" @@ -134,6 +135,17 @@ func TestNatsTheRecoveryStepAsksHearsBoundsAndCatchesUpOverTheBus(t *testing.T) say("old", meshrecovery.RestoredEventOutcomeFailed) // said before anyone listens: the last message of the subject session := b.startSession() + // H-A-504 fourth addendum: a have written through the agent's haves and read back gives the run, the pipeline and the + // want revision it was applied from, by every read, so a have written over it keeps them. + hv := sessionHaves{session: session} + hv.Write(ctx, agent.Have{Module: "web", FP: "src2:" + strings.Repeat("a", 64), Composed: "sha256:" + strings.Repeat("b", 64), + Phase: "running", Run: "12", Pipeline: "main.12", WantRevision: 301924}) + within(t, 5*time.Second, "the have read back through the agent's haves lacks its run, pipeline or want revision", func() bool { + got, ok := hv.Read(ctx, "web") + at, _, okAt, err := hv.ReadAt(ctx, "web") + return ok && okAt && err == nil && got.Run == "12" && got.Pipeline == "main.12" && got.WantRevision == 301924 && + at.Run == got.Run && at.Pipeline == got.Pipeline && at.WantRevision == got.WantRevision + }) r := recoveryEnv(session, nil) // Design 47 H2 §775, H-A-364: over the same session, an ask of the seat is taken once in the state taken: the first time, // and never again within its bound. diff --git a/internal/agent/agent.go b/internal/agent/agent.go index cf1c7f67..440f69db 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -23,7 +23,6 @@ import ( "github.com/novox/mesh-host/internal/compositions" "slices" "sort" - "strconv" "strings" "sync" "sync/atomic" @@ -79,13 +78,18 @@ type Have struct { // Wait is the hold or wait the have says, in the recovery wait with its ask-at and act (design 47 G §560). Wait *nodeengine.Wait // Run is the composition run the module was applied from, its pipeline sequence in decimal (design 47 H2 §813 (1), - // H-A-384 (2), H-A-388; nodeengine.HaveByNodeModuleValue.Run): kept in the have, so a restarted engine applies no - // older run again. A write that names none keeps the one the module's last have said (writeHave). + // H-A-384 (2), H-A-388; nodeengine.HaveByNodeModuleValue.Run): information for the why and the tools, never a gate + // (H-A-504 third addendum). A write that names none keeps the one the module's last have said (writeHave). Run string // Pipeline is the pipeline of the want the module was applied from, its key ., kept beside Run - // (H-A-429 (1), H-A-458 (1); nodeengine.HaveByNodeModuleValue.Pipeline): a want's run is compared with Run only when - // the want's pipeline is of the same branch (pipelineBranch). Kept and replaced together with Run. + // (H-A-429 (1), H-A-458 (1); nodeengine.HaveByNodeModuleValue.Pipeline): information, like Run (H-A-504 third + // addendum). Kept and replaced together with Run. Pipeline string + // WantRevision is the bus revision of the want key the module was applied from (design 47 H2 §813 (1), H-A-504 third + // and fourth addenda; nodeengine.HaveByNodeModuleValue.WantRevision): the engine applies a want whose key revision is + // newer than it, whatever the want's run. Zero (a have written before it) counts as no revision applied. A write that + // names none keeps the one the module's last have said, as Run (writeHave). + WantRevision uint64 } // Haves is the node's have bucket as the agent uses it. @@ -856,7 +860,7 @@ func (a *Agent) filledHave(ctx context.Context, h Have) Have { h.Kept = a.kept[h.Module] a.mu.Unlock() } - if (h.Composed == "" || h.Run == "") && h.Module != NodeModule { + if (h.Composed == "" || h.Run == "" || h.WantRevision == 0) && h.Module != NodeModule { if last, ok := a.env.Haves.Read(ctx, h.Module); ok { if h.Composed == "" && last.Composed != "" { h.FP, h.Composed = last.FP, last.Composed @@ -869,6 +873,10 @@ func (a *Agent) filledHave(ctx context.Context, h Have) Have { if h.Run == "" { h.Run, h.Pipeline = last.Run, last.Pipeline } + // So does the want revision it was applied from (H-A-504 fourth addendum). + if h.WantRevision == 0 { + h.WantRevision = last.WantRevision + } } } // The node's own composition too: its core boxes are sealed again by the vault at a restart, and kept in its key the @@ -1658,24 +1666,20 @@ func (a *Agent) settleOne(ctx context.Context, module string) { } else { a.mu.Unlock() } - // Design 47 H2 §813 (1), H-A-384 (2): the run the module was applied from is its have's, never this engine's memory, so - // a restarted engine applies no older run again. Read only for a want that carries one, and compared only within the - // want's branch: a want of another branch is a placement the assigner made on purpose and replaces whatever stands - // (H-A-429 (1), H-A-458 (1)). A pipeline's key is ., so every pipeline of a branch has its own key and - // the branch is the key without its last part. - appliedRun := "" - if w.Want.Run != "" { - h, found, unread := a.readHave(ctx, module) - if unread { - a.mu.Lock() - a.sayOnce("run-unread:"+module+":"+w.Want.Run, fmt.Sprintf("the want of %s carries run %s, and its have, which says "+ - "the run it was applied from, cannot be read: nothing is applied until it reads", module, w.Want.Run)) - a.mu.Unlock() - return - } - if found && pipelineBranch(h.Pipeline) == pipelineBranch(w.Want.Pipeline) { - appliedRun = h.Run - } + // Design 47 H2 §813 (1), H-A-504 third and fourth addenda: the order of wants is the bus's own, the revision of the want + // key; the revision the module was applied from is its have's, never this engine's memory, so a restarted engine applies + // no older write again. The want's run is information, never a gate. + h, found, unread := a.readHave(ctx, module) + if unread { + a.mu.Lock() + a.sayOnce("revision-unread:"+module+":"+fmt.Sprint(w.Rev), fmt.Sprintf("the want of %s at revision %d is not applied: "+ + "its have, which says the revision it was applied from, cannot be read; nothing is applied until it reads", module, w.Rev)) + a.mu.Unlock() + return + } + var appliedRev uint64 + if found { + appliedRev = h.WantRevision } a.mu.Lock() r, runs := a.running[module] @@ -1710,13 +1714,19 @@ func (a *Agent) settleOne(ctx context.Context, module string) { a.mu.Unlock() return // failed already for exactly these keys: retried by the next change of either } - if was := appliedRun; !runIsNewer(w.Want.Run, was) { - // Design 47 H2 §813 (1), H-A-355: one writer per composition key, and the engine applies again only a newer write. - a.sayOnce("run:"+module+":"+w.Want.Run, fmt.Sprintf("the want of %s carries run %s, and the engine applied it from run %s, "+ - "which is not older: nothing is applied", module, w.Want.Run, was)) + if revisionIsOlder(w.Rev, appliedRev) { + // Design 47 H2 §813 (1), H-A-504 third and fifth addenda: one writer per composition key, and the engine never applies + // an older write, an older revision of the want key. The same revision is the same write: it goes on to the checks + // above, so an apply again (Reconcile, a transient retry) and a recomposition of the same want still apply. + a.sayOnce("revision:"+module+":"+fmt.Sprint(w.Rev), fmt.Sprintf("the want of %s at revision %d is not applied: the "+ + "engine applied it from revision %d, which is newer", module, w.Rev, appliedRev)) a.mu.Unlock() return } + if appliedRev != 0 && w.Rev > appliedRev { + a.sayOnce("revision-newer:"+module+":"+fmt.Sprint(w.Rev), fmt.Sprintf("the want of %s at revision %d is applied over "+ + "the applied %d", module, w.Rev, appliedRev)) + } if module == NodeModule { // Not while a module's composition cannot be read or is refused: what it carries may be what left the node's own, // and the node's own apply would remove it, held out by nothing (issue 1945, the review's gap 2). @@ -2004,7 +2014,7 @@ func (a *Agent) apply(ctx context.Context, module string, w wantNow, comp compNo delete(a.notComposed, module) // the apply says what it runs now a.running[module] = nodeengine.HaveByNodeModuleValue{Fp: fp, Composed: dig, Phase: nodeengineclient.PhaseRunning} a.applied[module] = comp - run, pipeline := w.Want.Run, w.Want.Pipeline + run, pipeline, wantRev := w.Want.Run, w.Want.Pipeline, w.Rev a.mu.Unlock() if run != "" { a.env.Say(fmt.Sprintf("applied %s from run %s", what, run)) @@ -2013,7 +2023,8 @@ func (a *Agent) apply(ctx context.Context, module string, w wantNow, comp compNo if a.env.Settled != nil { a.env.Settled(module) } - a.writeHave(ctx, Have{Module: module, FP: fp, Composed: dig, Phase: string(nodeengineclient.PhaseRunning), Detail: res.Detail, Run: run, Pipeline: pipeline}) + a.writeHave(ctx, Have{Module: module, FP: fp, Composed: dig, Phase: string(nodeengineclient.PhaseRunning), Detail: res.Detail, Run: run, Pipeline: pipeline, + WantRevision: wantRev}) a.catchUp(ctx, module, catchUps) } @@ -2024,29 +2035,12 @@ func (a *Agent) takenAtStart(module string, runs compositions.Composed) { } } -// runIsNewer says whether a want's run orders after the run a module was applied from (design 47 H2 §813 (1)), both of one -// pipeline (settleOne compares no runs of two, H-A-458 (1)): each is -// the composition run's pipeline sequence in decimal. A want or an apply without one is not ordered by run, nor is a run -// that is no decimal number. -// pipelineBranch is the branch of a pipeline's key . (design 47 H1, the runner's key; H-A-458 (1)): the -// key without its last part. A key with no source part is its own branch. -func pipelineBranch(key string) string { - if i := strings.LastIndexByte(key, '.'); i >= 0 { - return key[:i] - } - return key -} - -func runIsNewer(want, applied string) bool { - if want == "" || applied == "" { - return true - } - w, err1 := strconv.ParseUint(want, 10, 64) - p, err2 := strconv.ParseUint(applied, 10, 64) - if err1 != nil || err2 != nil { - return true - } - return w > p +// revisionIsOlder says whether a want's key revision orders before the one a module was applied from (design 47 H2 §813 +// (1), H-A-504 third, fourth and fifth addenda): the bus orders the writes of one key, and the same revision is the same +// write, not an older one. Nothing applied yet (zero: no have, or a have written before it kept the revision) orders no +// want before it. +func revisionIsOlder(want, applied uint64) bool { + return applied != 0 && want < applied } // ranFailed marks what runs of a module as failed once an apply of it failed (novox/hq issue 1966, 2026-11-08): a failed diff --git a/internal/agent/agent_test.go b/internal/agent/agent_test.go index bf493f76..d892c2f7 100644 --- a/internal/agent/agent_test.go +++ b/internal/agent/agent_test.go @@ -396,16 +396,43 @@ func testAWantAndItsCompositionApplyTheModuleAndTheHaveNamesBoth(t *testing.T) { if h := b.haves.last("web"); h.Phase != string(nodeengineclient.PhaseRunning) || h.Wait != nil || h.FP != fpn { t.Fatalf("the have is %s %q wait %+v, want running at %s with no wait (§750)", h.Phase, h.Detail, h.Wait, fpn) } - // Design 47 H2 §813 (1), H-A-355: the engine applies a composition again only when its run is newer than the one it - // applied, naming the run in its journal; a want of an older run is said and not applied. + // Design 47 H2 §813 (1), H-A-504 third and fourth addenda: the engine applies a want whose key revision is newer than + // the one it applied, whatever its run, and keeps that revision in the have, never in memory: a want of a newer + // revision and a lower run is applied; a have written before it kept the revision counts as nothing applied; a have an + // earlier life of the engine wrote at a later revision holds a want of an older one back, said once. The same revision + // is the same write and is applied again by Reconcile (H-A-504 fifth addendum; + // testReconcileAppliesARunningModuleAgainAndLeavesTheHeldAndTheUnwantedAlone). + wantsRev := func() uint64 { + b.wants.mu.Lock() + defer b.wants.mu.Unlock() + return b.wants.rev + } for _, row := range []struct { - fp, run string + name, fp, run string + // haveRev is what the have says was applied before the want is written: kept is what the last apply wrote, and + // a number is that many revisions past the want's own (negative: before it). + haveRev string + ahead int applied int - }{{"6", "5", 0}, {"6", "7", 0}, {"6", "8", 1}} { + }{ + {"a newer revision with a lower run", "6", "5", "kept", 0, 1}, + {"a newer revision with a higher run", "7", "8", "kept", 0, 1}, + {"a first apply over a have without a revision", "9", "3", "set", -1, 1}, + {"an older revision than an earlier life applied", "8", "10", "set", 20, 0}, + } { before := len(b.appliedList()) fpr := fpOf(row.fp) ckr, crawr, digr := composition(t, "web", fpr, false, archive("a", d2)) b.comps.put(ckr, crawr) + next := wantsRev() + 1 + if row.haveRev == "set" { + h := b.haves.last("web") + h.WantRevision = 0 + if row.ahead > 0 { + h.WantRevision = next + uint64(row.ahead) + } + b.haves.Write(context.Background(), h) + } wkr, _ := wantFor(t, "web", fpr, digr, "") rawr, err := bucketstest.Want(meshwants.WantsByWantNodeModuleValue{Fp: fpr, Run: row.run}) if err != nil { @@ -414,55 +441,38 @@ func testAWantAndItsCompositionApplyTheModuleAndTheHaveNamesBoth(t *testing.T) { b.wants.put(wkr, rawr) b.idle() if got := len(b.appliedList()) - before; got != row.applied { - t.Errorf("a want of run %s after run 7: applied %d times, want %d", row.run, got, row.applied) + t.Errorf("%s (want revision %d, run %s): applied %d times, want %d", row.name, next, row.run, got, row.applied) + } + if h := b.haves.last("web"); row.applied == 1 && (h.WantRevision != next || h.Run != row.run) { + t.Errorf("%s: the have names want revision %d and run %q, want %d and %q", row.name, h.WantRevision, h.Run, next, row.run) } } - // H-A-384 (2), H-A-388: the run is kept in the have, never in memory: the have names run 8, and a have an earlier life of - // the engine wrote at run 20 holds a want of run 10 back, though this one applied run 8 last. - if h := b.haves.last("web"); h.Run != "8" { - t.Errorf("the have names run %q after run 8 was applied", h.Run) - } - h := b.haves.last("web") - h.Run = "20" - b.haves.Write(context.Background(), h) - before = len(b.appliedList()) - fpr := fpOf("9") - ckr, crawr, digr := composition(t, "web", fpr, false, archive("a", d1)) - b.comps.put(ckr, crawr) - wkr, _ := wantFor(t, "web", fpr, digr, "") - rawr, _ := bucketstest.Want(meshwants.WantsByWantNodeModuleValue{Fp: fpr, Run: "10"}) - b.wants.put(wkr, rawr) + // H-A-504 fourth addendum: the want revision is written with every have, so a failed apply after one keeps the revision + // the module was applied from, and the next want is still ordered against it. + hf := b.haves.last("web") + hf.WantRevision = wantsRev() // the older row's earlier life ends: what was applied is at the latest write + b.haves.Write(context.Background(), hf) + applied := hf.WantRevision + b.mu.Lock() + b.failWith = "systemctl start web.service: exit status 1" + b.mu.Unlock() + fpf := fpOf("c") + ckf, crawf, digf := composition(t, "web", fpf, false, archive("a", d1)) + b.comps.put(ckf, crawf) + wkf, _ := wantFor(t, "web", fpf, digf, "") + rawf, _ := bucketstest.Want(meshwants.WantsByWantNodeModuleValue{Fp: fpf, Run: "11"}) + b.wants.put(wkf, rawf) b.idle() - if got := len(b.appliedList()) - before; got != 0 { - t.Errorf("a want of run 10 after a have of run 20: applied %d times, want none", got) - } - // H-A-429 (1), H-A-458 (1): a want's run is compared with the have's only within one branch, a pipeline's key being - // .: a lower run of another branch replaces what stands, and the have keeps its pipeline beside its - // run; a lower run of an older pipeline of the same branch is refused. - for _, row := range []struct { - fp, run, pipeline string - applied int - }{{"7", "10", "feat.10", 1}, {"8", "9", "feat.9", 0}, {"b", "5", "main.5", 1}} { - before := len(b.appliedList()) - fpr := fpOf(row.fp) - ckr, crawr, digr := composition(t, "web", fpr, false, archive("a", d2)) - b.comps.put(ckr, crawr) - wkr, _ := wantFor(t, "web", fpr, digr, "") - rawr, _ := bucketstest.Want(meshwants.WantsByWantNodeModuleValue{Fp: fpr, Run: row.run, Pipeline: row.pipeline}) - b.wants.put(wkr, rawr) - b.idle() - if got := len(b.appliedList()) - before; got != row.applied { - t.Errorf("a want of run %s of pipeline %s: applied %d times, want %d", row.run, row.pipeline, got, row.applied) - } - } - if h := b.haves.last("web"); h.Run != "5" || h.Pipeline != "main.5" { - t.Errorf("the have names run %q of pipeline %q, want run 5 of main.5", h.Run, h.Pipeline) + if h := b.haves.last("web"); h.Phase != string(nodeengineclient.PhaseFailed) || h.WantRevision != applied || h.Run != "3" { + t.Errorf("the failed have after an apply: phase %s, want revision %d, run %q; want failed, %d and \"3\"", h.Phase, h.WantRevision, h.Run, applied) } b.mu.Lock() + b.failWith = "" + b.mu.Unlock() + b.mu.Lock() said := strings.Join(b.said, "\n") b.mu.Unlock() - for _, want := range []string{"from run 7", "from run 8", "carries run 5, and the engine applied it from run 7", - "carries run 10, and the engine applied it from run 20", "carries run 9, and the engine applied it from run 10"} { + for _, want := range []string{"from run 5", "from run 8", "is applied over the applied", "the engine applied it from revision"} { if !strings.Contains(said, want) { t.Errorf("the journal does not say %q:\n%s", want, said) } diff --git a/internal/link/have.go b/internal/link/have.go index 9e6bf5e1..a933214b 100644 --- a/internal/link/have.go +++ b/internal/link/have.go @@ -416,8 +416,9 @@ func (q *Session) writeHaves(ctx context.Context, bus Bus) { h.PullWait = f.PullWait } h.Wait = f.Wait - // The seat's form 2 carries the run the module was applied from (H-A-388), form 4 the pipeline beside it (H-A-458 (1)). - h.Run, h.Pipeline, h.Form = f.Run, f.Pipeline, nodeengine.Form + // The seat's form 2 carries the run the module was applied from (H-A-388), form 4 the pipeline beside it (H-A-458 (1)), + // form 5 the want revision it was applied from (H-A-504 fourth addendum). + h.Run, h.Pipeline, h.WantRevision, h.Form = f.Run, f.Pipeline, int64(f.WantRevision), nodeengine.Form h.Health = s.healthOf(module, f) if h.Detail == "" { h.Detail = s.wordsOf(module, h.Health) diff --git a/internal/link/have_test.go b/internal/link/have_test.go index dfd5704d..ea8a0c2b 100644 --- a/internal/link/have_test.go +++ b/internal/link/have_test.go @@ -771,11 +771,12 @@ func testAnAppliedModulesHaveNamesTheFingerprintAndTheDigestAndFollowsItsHealth( q, _ := aHaveQueue(l) fp := "src2:" + strings.Repeat("a", 64) dig := "sha256:" + strings.Repeat("b", 64) - // H-A-458 (1): the run it was applied from is kept with the pipeline of its want. - q.NoteModule(context.Background(), ModuleFact{Module: "web", FP: fp, Composed: dig, Run: "12", Pipeline: "main.12"}) + // H-A-458 (1): the run it was applied from is kept with the pipeline of its want; H-A-504 fourth addendum: and the want + // revision it was applied from, at the seat's form. + q.NoteModule(context.Background(), ModuleFact{Module: "web", FP: fp, Composed: dig, Run: "12", Pipeline: "main.12", WantRevision: 301924}) h := l.have(t, "n1.web") if h.Fp != fp || h.Composed != dig || h.Phase != nodeengineclient.PhaseRunning || h.Health != nodeengineclient.HealthUnknown || - h.Run != "12" || h.Pipeline != "main.12" { + h.Run != "12" || h.Pipeline != "main.12" || h.WantRevision != 301924 || h.Form != nodeengine.Form { t.Fatalf("the have after the apply: %+v", h) } // Design 47, *What each step says*: running is said once, when the build is healthy and its manifest is known, on diff --git a/internal/link/messages.go b/internal/link/messages.go index 5f21990e..c5207597 100644 --- a/internal/link/messages.go +++ b/internal/link/messages.go @@ -654,4 +654,7 @@ type ModuleFact struct { // Pipeline is the pipeline of the want it was applied from, kept beside Run (nodeengine.HaveByNodeModuleValue.Pipeline, // H-A-458 (1)). Pipeline string + // WantRevision is the bus revision of the want key it was applied from (nodeengine.HaveByNodeModuleValue.WantRevision, + // H-A-504 fourth addendum). + WantRevision uint64 }