diff --git a/cmd/mesh-host/nodeagent.go b/cmd/mesh-host/nodeagent.go index 7c77f6b2..f5a4c0a1 100644 --- a/cmd/mesh-host/nodeagent.go +++ b/cmd/mesh-host/nodeagent.go @@ -109,7 +109,7 @@ func (h sessionHaves) ReadErr(ctx context.Context, module string) (agent.Have, b return agent.Have{}, 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}, true, nil + Sealed: got.SealedAt, Wait: got.Wait, Run: got.Run}, true, nil } func (h sessionHaves) Write(ctx context.Context, x agent.Have) { @@ -126,7 +126,7 @@ func (h sessionHaves) Write(ctx context.Context, x agent.Have) { } h.session.NoteModule(ctx, 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}) + BuildAlone: x.BuildAlone, Kept: x.Kept, Sealed: x.Sealed, PullWait: x.PullWait, Wait: x.Wait, Run: x.Run}) } func (h sessionHaves) Removed(ctx context.Context, module string) { diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 754478f7..e4beb02a 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -78,6 +78,10 @@ type Have struct { PullWait *nodeengine.PullWait // 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). + Run string } // Haves is the node's have bucket as the agent uses it. @@ -250,9 +254,6 @@ type Agent struct { // applied is the composition each module was last applied from: a revision a want named from the key's history is // not the key's latest value, and what it holds is still held out of the others' applies. applied map[string]compNow - // appliedRun is, of each module applied from a want that carried run, that run: the composition run's pipeline sequence - // in decimal (design 47 H2 §813 (1), H-A-355). A want whose run is not newer is not applied again. - appliedRun map[string]string // had is, for each module whose have names a composition the bucket keeps (its latest value or a revision), that // 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). @@ -345,7 +346,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{}, appliedRun: map[string]string{}, 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{}, 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), @@ -737,11 +738,17 @@ func (a *Agent) writeHave(ctx context.Context, h Have) { h.Kept = a.kept[h.Module] a.mu.Unlock() } - if h.Composed == "" && h.Module != NodeModule { - if last, ok := a.env.Haves.Read(ctx, h.Module); ok && last.Composed != "" { - h.FP, h.Composed = last.FP, last.Composed - if h.Sealed == nil { - h.Sealed = last.Sealed + if (h.Composed == "" || h.Run == "") && 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 + if h.Sealed == nil { + h.Sealed = last.Sealed + } + } + // The run the module was applied from stays in its have until an apply from another run (H-A-384 (2)). + if h.Run == "" { + h.Run = last.Run } } } @@ -1515,6 +1522,22 @@ 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. + 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 { + appliedRun = h.Run + } + } a.mu.Lock() r, runs := a.running[module] // A module whose last apply failed runs no composition whole, whatever it ran before (ranFailed). @@ -1548,7 +1571,7 @@ 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 := a.appliedRun[module]; !runIsNewer(w.Want.Run, was) { + 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)) @@ -1841,9 +1864,6 @@ func (a *Agent) apply(ctx context.Context, module string, w wantNow, comp compNo a.running[module] = nodeengine.HaveByNodeModuleValue{Fp: fp, Composed: dig, Phase: nodeengineclient.PhaseRunning} a.applied[module] = comp run := w.Want.Run - if run != "" { - a.appliedRun[module] = run - } a.mu.Unlock() if run != "" { a.env.Say(fmt.Sprintf("applied %s from run %s", what, run)) @@ -1852,7 +1872,7 @@ 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}) + a.writeHave(ctx, Have{Module: module, FP: fp, Composed: dig, Phase: string(nodeengineclient.PhaseRunning), Detail: res.Detail, Run: run}) a.catchUp(ctx, module, catchUps) } diff --git a/internal/agent/agent_test.go b/internal/agent/agent_test.go index 0e7c6ff5..8c06ef1b 100644 --- a/internal/agent/agent_test.go +++ b/internal/agent/agent_test.go @@ -368,10 +368,30 @@ func TestAWantAndItsCompositionApplyTheModuleAndTheHaveNamesBoth(t *testing.T) { t.Errorf("a want of run %s after run 7: applied %d times, want %d", row.run, got, row.applied) } } + // 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) + 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) + } 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"} { + 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"} { 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 be776119..63d28d87 100644 --- a/internal/link/have.go +++ b/internal/link/have.go @@ -413,6 +413,8 @@ 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). + h.Run, h.Form = f.Run, 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 dc27c29a..94231488 100644 --- a/internal/link/have_test.go +++ b/internal/link/have_test.go @@ -256,11 +256,12 @@ func TestAnAppliedModuleIsWrittenRunningAndAFailedOneFailed(t *testing.T) { } } -// 1132/AC5: a have equal to the bucket's value is not written; a different one is written over the last revision. +// 1132/AC5: a have equal to the bucket's value is not written; a different one is written over the last revision. Each +// is written at the seat's form (H-A-388). func TestAHaveIsWrittenOnlyWhenItDiffers(t *testing.T) { l := newHaveLink() - l.put(t, "n1.a", nodeengine.HaveByNodeModuleValue{V: sdkkeys.Version, Build: "b-1", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - l.put(t, "n1.b", nodeengine.HaveByNodeModuleValue{V: sdkkeys.Version, Build: "b-0", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) + l.put(t, "n1.a", nodeengine.HaveByNodeModuleValue{V: sdkkeys.Version, Form: nodeengine.Form, Build: "b-1", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) + l.put(t, "n1.b", nodeengine.HaveByNodeModuleValue{V: sdkkeys.Version, Form: nodeengine.Form, Build: "b-0", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) q, _ := aHaveQueue(l) q.SayFacts(context.Background(), appliedModules()) if l.kv["n1.a"].rev != 1 { @@ -711,8 +712,8 @@ func TestARefusedRemovalIsWrittenByTheNextPass(t *testing.T) { // before: one of them the new declaration lacks is written removed, one that is not in the bucket is not created. func TestARestartedNodeEngineWritesTheRemovalFromTheReportsBefore(t *testing.T) { l := newHaveLink() - l.put(t, "n1.a", nodeengine.HaveByNodeModuleValue{V: sdkkeys.Version, Build: "b-1", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) - l.put(t, "n1.b", nodeengine.HaveByNodeModuleValue{V: sdkkeys.Version, Build: "b-2", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) + l.put(t, "n1.a", nodeengine.HaveByNodeModuleValue{V: sdkkeys.Version, Form: nodeengine.Form, Build: "b-1", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) + l.put(t, "n1.b", nodeengine.HaveByNodeModuleValue{V: sdkkeys.Version, Form: nodeengine.Form, Build: "b-2", Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}) q, _ := aHaveQueue(l) // fresh: nothing written by this process r := onlyA() r.Before = []string{"a", "b", "c"} diff --git a/internal/link/messages.go b/internal/link/messages.go index e84f31e6..9c2490a9 100644 --- a/internal/link/messages.go +++ b/internal/link/messages.go @@ -649,4 +649,6 @@ type ModuleFact struct { PullWait *nodeengine.PullWait // Wait is the hold or wait the module has, with the ask-at and act of a recovery wait (nodeengine.HaveByNodeModuleValue.Wait, design 47 G §560). Wait *nodeengine.Wait + // Run is the composition run the module was applied from (nodeengine.HaveByNodeModuleValue.Run, design 47 H2 §813 (1), H-A-388). + Run string }