The node-engine never overwrites a rank wait with the composer's mark; the composer's watch ends with its run (H-A-480) #252

Merged
mesh-tools merged 3 commits from fix/rank-wait-kept into main 2026-11-22 15:22:37 +00:00
8 changed files with 498 additions and 65 deletions
+22 -5
View File
@@ -104,12 +104,23 @@ func (h sessionHaves) ReadErr(ctx context.Context, module string) (agent.Have, b
}
return agent.Have{Module: module, Composed: got.Composed, Sealed: got.SealedAt}, true, nil
}
got, ok, err := h.session.ReadHaveErr(ctx, module)
got, _, ok, err := h.ReadAt(ctx, module)
return got, ok, err
}
// ReadAt is ReadErr with the revision of the module's key (agent.HavesAt).
func (h sessionHaves) ReadAt(ctx context.Context, module string) (agent.Have, uint64, bool, error) {
got, revision, ok, err := h.session.ReadHaveAt(ctx, module)
if !ok || err != nil {
return agent.Have{}, false, err
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}, true, nil
Sealed: got.SealedAt, Wait: got.Wait, Run: got.Run, Pipeline: got.Pipeline}, revision, true, nil
}
// WriteAt writes a module's have only while its key is at the revision read (agent.HavesAt, H-A-480).
func (h sessionHaves) WriteAt(ctx context.Context, x agent.Have, revision uint64) (bool, error) {
return h.session.NoteModuleAt(ctx, moduleFact(x), revision)
}
func (h sessionHaves) Write(ctx context.Context, x agent.Have) {
@@ -120,13 +131,18 @@ func (h sessionHaves) Write(ctx context.Context, x agent.Have) {
}
return
}
h.session.NoteModule(ctx, moduleFact(x))
}
// moduleFact is a module's have as the session writes it.
func moduleFact(x agent.Have) link.ModuleFact {
phase := x.Phase
if phase == string(nodeengineclient.PhaseRunning) {
phase = ""
}
h.session.NoteModule(ctx, link.ModuleFact{Module: x.Module, FP: x.FP, Composed: x.Composed, Phase: phase,
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}
}
func (h sessionHaves) Removed(ctx context.Context, module string) {
@@ -780,6 +796,7 @@ func nodeAgent(ctx context.Context, opts options, signer ed25519.PublicKey, trus
err = ag.Run(run)
stopRun()
cancel()
composer.Wait() // its watch ends with the agent's stop, and says nothing after it (H-A-480)
current.set(nil)
composerNow.Store(nil)
ownFindingsNow.Store(nil)
+156 -32
View File
@@ -581,6 +581,42 @@ type HavesReadErr interface {
ReadErr(ctx context.Context, module string) (Have, bool, error)
}
// HavesAt is what a Haves may answer besides Write: a module's have with the revision of its key (zero when the bucket
// holds none), and a write of it that lands only while the key is still at that revision, false when it moved (design 47
// H2 H-A-480: the composer's mark is a compare-and-set on the have's revision).
type HavesAt interface {
ReadAt(ctx context.Context, module string) (Have, uint64, bool, error)
WriteAt(ctx context.Context, h Have, revision uint64) (bool, error)
}
// readHaveAt is readHave with the revision of the have's key, for a write over it (writeHaveAt). A Haves that keeps no
// revisions answers zero, and its writes land whatever moved.
func (a *Agent) readHaveAt(ctx context.Context, module string) (h Have, revision uint64, found, unread bool) {
at, tells := a.env.Haves.(HavesAt)
if !tells {
h, found, unread = a.readHave(ctx, module)
return h, 0, found, unread
}
for try := 0; ; try++ {
h, revision, found, err := at.ReadAt(ctx, module)
if err == nil {
return h, revision, found, false
}
if try == 2 || ctx.Err() != nil {
return Have{}, 0, false, true
}
select {
case <-ctx.Done():
return Have{}, 0, false, true
case <-time.After(time.Duration(1<<try) * readAgain):
}
}
}
// markTries is how many times a mark or its clear is written over a have that moved since it was read, each after
// reading it again (writeHaveAt).
const markTries = 3
// readHave reads a module's have: found, absent, or unread. A read that fails is tried again after a short wait while
// the bus does not answer (a reload pauses it for a few seconds, issue 1126), and is unread when it still fails. A
// Haves that cannot tell an absent key from a failed read says absent.
@@ -672,21 +708,18 @@ func (a *Agent) NotComposedEach(ctx context.Context, why string, held map[string
back.Phase = string(r.Phase)
}
}
if back.Composed == "" {
continue // nothing said what ran: nothing to say back
if back.Module == "" {
// Nothing said what ran, and its have held no key before the mark: the apply the composing pass starts
// replaces the mark in the same settle (H-A-480).
continue
}
a.writeHave(ctx, back)
a.clearMark(ctx, module, back)
continue
}
if !marked {
if ran {
mark.prior = Have{Module: module, FP: r.Fp, Composed: r.Composed, Phase: string(r.Phase)}
if mark.prior.Phase == "" {
mark.prior.Phase = string(nodeengineclient.PhaseRunning)
}
} else if h, found, unread := a.readHave(ctx, module); found {
mark.prior = h // what its last have said runs: this agent has run nothing of it since its start
} else if unread {
var revision uint64
if !marked && !ran {
h, rev, found, unread := a.readHaveAt(ctx, module)
if unread {
if ctx.Err() != nil {
return
}
@@ -697,24 +730,29 @@ func (a *Agent) NotComposedEach(ctx context.Context, why string, held map[string
a.mu.Unlock()
continue
}
if found {
mark.prior = h // what its last have said runs: this agent has run nothing of it since its start
}
revision = rev
} else {
if !marked {
mark.prior = Have{Module: module, FP: r.Fp, Composed: r.Composed, Phase: string(r.Phase)}
if mark.prior.Phase == "" {
mark.prior.Phase = string(nodeengineclient.PhaseRunning)
}
}
_, rev, _, unread := a.readHaveAt(ctx, module)
if unread {
if ctx.Err() != nil {
return
}
a.mu.Lock()
a.sayOnce("unmarked:"+module, fmt.Sprintf("%s's have cannot be read, so it is not marked: the node cannot compose it: %s", module, why))
a.mu.Unlock()
continue
}
revision = rev
}
// A have that already says another cause (an apply that failed, a backup held, an apply under way) keeps it: a
// composer's mark over it would hide that cause, and baseline would take the mark for what runs (review of the
// switch, round 1).
if p := mark.prior.Phase; !marked && p != "" && p != string(nodeengineclient.PhaseRunning) && !composerMark(mark.prior) {
a.mu.Lock()
a.sayOnce("unmarked-cause:"+module, fmt.Sprintf("%s is not marked: its have says %s (%s), and the node cannot compose it: %s",
module, p, mark.prior.Detail, why))
a.mu.Unlock()
continue
}
if ctx.Err() != nil {
return
}
mark.why = why
a.mu.Lock()
a.notComposed[module] = mark
a.mu.Unlock()
phase, detail := string(nodeengineclient.PhaseFailed), NotComposedDetail+why
var wait *nodeengine.Wait
if heldForEntry(why) {
@@ -724,8 +762,71 @@ func (a *Agent) NotComposedEach(ctx context.Context, why string, held map[string
// The hold says what it waits on, as every hold but a failed backup does (design 47 G A-453, section 541).
wait = a.composeWait(strings.TrimSuffix(heldReason(why), ":"), why)
}
a.writeHave(ctx, Have{Module: module, FP: mark.prior.FP, Composed: mark.prior.Composed, Sealed: mark.prior.Sealed,
Phase: phase, Detail: detail, Wait: wait})
written, lost := false, 0
for try := 0; try < markTries && !written; try++ {
if ctx.Err() != nil {
return
}
// A module waiting for its rank is never marked: its have says that wait (design 47 H §771, H-A-480).
a.mu.Lock()
_, placing := a.placing[module]
a.mu.Unlock()
if placing {
break
}
// A have that already says another cause (an apply that failed, a backup held, an apply under way, a wait
// for its rank) keeps it: a composer's mark over it would hide that cause, and baseline would take the mark
// for what runs (review of the switch, round 1).
if p := mark.prior.Phase; !marked && p != "" && p != string(nodeengineclient.PhaseRunning) && !composerMark(mark.prior) {
a.mu.Lock()
a.sayOnce("unmarked-cause:"+module, fmt.Sprintf("%s is not marked: its have says %s (%s), and the node cannot compose it: %s",
module, p, mark.prior.Detail, why))
a.mu.Unlock()
break
}
if written = a.writeHaveAt(ctx, Have{Module: module, FP: mark.prior.FP, Composed: mark.prior.Composed, Sealed: mark.prior.Sealed,
Phase: phase, Detail: detail, Wait: wait}, revision); written {
break
}
// The have moved since it was read: read it again, and judge the mark against what it says now.
lost++
h, rev, found, unread := a.readHaveAt(ctx, module)
if unread {
break
}
revision = rev
if found && !composerMark(h) {
mark.prior, marked = h, false // what another write said since: judged again as a have before any mark
}
}
if !written {
if lost == markTries {
// Every write lost its race to another: not marked, and said (P-failure-loud); the next refusal marks it.
a.mu.Lock()
a.sayOnce("unmarked-moved:"+module, fmt.Sprintf("%s is not marked: its have moved under each of %d writes, and the node cannot compose it: %s",
module, markTries, why))
a.mu.Unlock()
}
continue
}
mark.why = why
a.mu.Lock()
a.notComposed[module] = mark
a.mu.Unlock()
}
}
// clearMark gives a module's have back as it was before the composer marked it (back), by a compare-and-set while the
// have still says the mark: a have another cause wrote since is kept (H-A-480).
func (a *Agent) clearMark(ctx context.Context, module string, back Have) {
for try := 0; try < markTries && ctx.Err() == nil; try++ {
h, revision, found, unread := a.readHaveAt(ctx, module)
if unread || !found || !composerMark(h) {
return
}
if a.writeHaveAt(ctx, back, revision) {
return
}
}
}
@@ -744,6 +845,12 @@ func (a *Agent) composeWait(reason, words string) *nodeengine.Wait {
// said runs, its fingerprint, digest and sealed values, rather than a have naming nothing: what runs is not lost, and
// the boxes the composer reuses after a restart stay (review of L2, rounds 2 and 3).
func (a *Agent) writeHave(ctx context.Context, h Have) {
a.env.Haves.Write(ctx, a.filledHave(ctx, h))
}
// filledHave is h as writeHave writes it: with what the composer keeps of it, and what its last have said where h names
// none.
func (a *Agent) filledHave(ctx context.Context, h Have) Have {
if h.Module != NodeModule {
a.mu.Lock()
h.Kept = a.kept[h.Module]
@@ -769,7 +876,24 @@ func (a *Agent) writeHave(ctx context.Context, h Have) {
if a.env.SelfComposed && h.Sealed == nil && h.Composed != "" {
h.Sealed = a.sealedOf(ctx, h.Module, h.FP, h.Composed)
}
a.env.Haves.Write(ctx, h)
return h
}
// writeHaveAt is writeHave as a compare-and-set on the revision of the have's key the caller read (readHaveAt): false when
// the key moved since, written nothing. A Haves that keeps no revisions writes whatever moved.
func (a *Agent) writeHaveAt(ctx context.Context, h Have, revision uint64) bool {
at, tells := a.env.Haves.(HavesAt)
if !tells {
a.writeHave(ctx, h)
return true
}
ok, err := at.WriteAt(ctx, a.filledHave(ctx, h), revision)
if err != nil {
a.mu.Lock()
a.sayOnce("unwritten:"+h.Module, fmt.Sprintf("%s's have is not written: its key cannot be read: %v", h.Module, err))
a.mu.Unlock()
}
return ok && err == nil
}
// KeptNow takes in what the composer's last pass keeps of each module as it runs because its manifest does not yet say it
+151 -3
View File
@@ -71,6 +71,46 @@ type haves struct {
removed []string
// unreadable is each module whose have the bus cannot read now (ReadErr).
unreadable map[string]bool
// revs is each key's revision, moved by every write (HavesAt); afterRead is, by module, what writes between the next read
// of its have and the write after it, once (H-A-480).
revs map[string]uint64
afterRead map[string]func()
}
// read is a module's have and its revision, then what afterRead writes after it, once.
func (h *haves) read(module string) (Have, uint64, bool) {
h.mu.Lock()
x, ok := h.stored[module]
rev := h.revs[module]
then := h.afterRead[module]
delete(h.afterRead, module)
h.mu.Unlock()
if then != nil {
then()
}
return x, rev, ok
}
func (h *haves) ReadAt(_ context.Context, module string) (Have, uint64, bool, error) {
h.mu.Lock()
bad := h.unreadable[module]
h.mu.Unlock()
if bad {
return Have{}, 0, false, fmt.Errorf("nats: no responders available for request")
}
x, rev, ok := h.read(module)
return x, rev, ok, nil
}
func (h *haves) WriteAt(ctx context.Context, x Have, revision uint64) (bool, error) {
h.mu.Lock()
if h.revs[x.Module] != revision {
h.mu.Unlock()
return false, nil
}
h.mu.Unlock()
h.Write(ctx, x)
return true, nil
}
func (h *haves) ReadErr(ctx context.Context, module string) (Have, bool, error) {
@@ -85,9 +125,7 @@ func (h *haves) ReadErr(ctx context.Context, module string) (Have, bool, error)
}
func (h *haves) Read(_ context.Context, module string) (Have, bool) {
h.mu.Lock()
defer h.mu.Unlock()
x, ok := h.stored[module]
x, _, ok := h.read(module)
return x, ok
}
func (h *haves) Write(_ context.Context, x Have) {
@@ -96,6 +134,10 @@ func (h *haves) Write(_ context.Context, x Have) {
if h.stored == nil {
h.stored = map[string]Have{}
}
if h.revs == nil {
h.revs = map[string]uint64{}
}
h.revs[x.Module]++
h.stored[x.Module] = x
h.log = append(h.log, x)
}
@@ -2293,4 +2335,110 @@ func TestAgent(t *testing.T) {
t.Run("ACompositionWhoseResourcesWereRemovedIsAppliedAgain", testACompositionWhoseResourcesWereRemovedIsAppliedAgain)
t.Run("WhatRunsBeforeTheAgentTakesAModuleNamesItsFilledPaths", testWhatRunsBeforeTheAgentTakesAModuleNamesItsFilledPaths)
t.Run("TheNodesOwnCompositionIsAppliedWhenItDiffersAndARecordMoveWaitsForAYes", testTheNodesOwnCompositionIsAppliedWhenItDiffersAndARecordMoveWaitsForAYes)
t.Run("AComposersMarkKeepsWhatWasWrittenSinceItsRead", testAComposersMarkKeepsWhatWasWrittenSinceItsRead)
}
// The composer's mark is a compare-and-set on the have's revision (design 47 H2 H-A-480): a wait for its rank or another
// cause written between its read of the have and its write is kept, and a mark on a module that never ran is cleared at the
// next pass that composes it. Seen 2026-11-22 under load: second's wait of step placing (rev 3) overwritten by the mark of
// the bootstrapped seat list (rev 4), never written again.
func testAComposersMarkKeepsWhatWasWrittenSinceItsRead(t *testing.T) {
const why = "the node holds only its bootstrapped seat list, and has not read the seat mesh-catalog"
failed := Have{Module: "second", Phase: string(nodeengineclient.PhaseFailed), Detail: "the apply failed: no space left"}
for _, row := range []struct {
name string
// before writes the module's have before the composer reads it; between writes it between its read and its write.
before, between func(ctx context.Context, b *bed)
check func(h Have) bool
}{
{"a module that runs and waits for its rank", func(ctx context.Context, b *bed) {
b.ag.running["second"] = nodeengine.HaveByNodeModuleValue{Fp: fpOf("a"), Composed: "sha256:" + strings.Repeat("1", 64), Phase: nodeengineclient.PhaseRunning}
b.ag.placingHave(ctx, "second", []string{"first"})
}, nil,
func(h Have) bool {
return h.Phase == string(nodeengineclient.PhaseHeld) && h.Wait != nil && h.Wait.Step == nodeengine.WaitStepPlacing && h.Wait.On == "first"
}},
{"a wait for its rank", nil, func(ctx context.Context, b *bed) { b.ag.placingHave(ctx, "second", []string{"first"}) },
func(h Have) bool {
return h.Phase == string(nodeengineclient.PhaseHeld) && h.Wait != nil && h.Wait.Step == nodeengine.WaitStepPlacing && h.Wait.On == "first"
}},
{"another cause", nil, func(ctx context.Context, b *bed) { b.haves.Write(ctx, failed) },
func(h Have) bool { return h.Phase == failed.Phase && h.Detail == failed.Detail }},
} {
t.Run(row.name, func(t *testing.T) {
b := newBed(t)
b.ag = New(b.env)
ctx := context.Background()
b.ag.wants["second"] = wantNow{Want: meshwants.WantsByWantNodeModuleValue{Fp: fpOf("b"), Pipeline: "main.7", Rank: 2}, Present: true}
if row.before != nil {
row.before(ctx, b)
}
if row.between != nil {
b.haves.afterRead = map[string]func(){"second": func() { row.between(ctx, b) }}
}
b.ag.NotComposedEach(ctx, "", map[string]string{"second": why})
if h := b.haves.last("second"); !row.check(h) {
t.Fatalf("what was written between the composer's read and its write was overwritten: %+v", h)
}
})
}
t.Run("a mark on a module that never ran is cleared", func(t *testing.T) {
b := newBed(t)
b.ag = New(b.env)
ctx := context.Background()
b.ag.wants["third"] = wantNow{Want: meshwants.WantsByWantNodeModuleValue{Fp: fpOf("c")}, Present: true}
b.haves.Write(ctx, Have{Module: "third", FP: fpOf("c"), Phase: string(nodeengineclient.PhaseRunning)})
b.ag.NotComposedEach(ctx, "", map[string]string{"third": why})
if h := b.haves.last("third"); !composerMark(h) {
t.Fatalf("third is not marked: %+v", h)
}
b.ag.NotComposedEach(ctx, "", nil)
if h := b.haves.last("third"); composerMark(h) || h.Phase != string(nodeengineclient.PhaseRunning) || h.FP != fpOf("c") {
t.Fatalf("the mark of a module that never ran stays once it composes: %+v", h)
}
})
t.Run("a cause written between the mark and its clear is kept", func(t *testing.T) {
b := newBed(t)
b.ag = New(b.env)
ctx := context.Background()
b.ag.wants["third"] = wantNow{Want: meshwants.WantsByWantNodeModuleValue{Fp: fpOf("c")}, Present: true}
b.haves.Write(ctx, Have{Module: "third", FP: fpOf("c"), Phase: string(nodeengineclient.PhaseRunning)})
b.ag.NotComposedEach(ctx, "", map[string]string{"third": why})
cause := Have{Module: "third", FP: fpOf("c"), Phase: string(nodeengineclient.PhaseFailed), Detail: "the apply failed: no space left"}
b.haves.Write(ctx, cause)
b.ag.NotComposedEach(ctx, "", nil)
if h := b.haves.last("third"); h.Phase != cause.Phase || h.Detail != cause.Detail {
t.Fatalf("the clear of a mark wrote over a cause written since: %+v", h)
}
})
t.Run("a mark that loses every race is said", func(t *testing.T) {
b := newBed(t)
b.ag = New(b.env)
ctx := context.Background()
b.ag.wants["fourth"] = wantNow{Want: meshwants.WantsByWantNodeModuleValue{Fp: fpOf("d")}, Present: true}
running := Have{Module: "fourth", FP: fpOf("d"), Phase: string(nodeengineclient.PhaseRunning)}
b.haves.Write(ctx, running)
// Every read is followed by a write of what runs: the key moves under each mark.
var again func()
again = func() {
b.haves.Write(ctx, running)
b.haves.mu.Lock()
b.haves.afterRead["fourth"] = again
b.haves.mu.Unlock()
}
b.haves.afterRead = map[string]func(){"fourth": again}
b.ag.NotComposedEach(ctx, "", map[string]string{"fourth": why})
b.haves.mu.Lock()
b.haves.afterRead = nil
b.haves.mu.Unlock()
said := false
b.mu.Lock()
for _, l := range b.said {
said = said || strings.Contains(l, "fourth is not marked") && strings.Contains(l, "moved")
}
b.mu.Unlock()
if !said {
t.Fatalf("a mark dropped after every write lost its race was not said: %v", b.said)
}
})
}
+87 -19
View File
@@ -75,6 +75,9 @@ type haveState struct {
// unjudged is each module whose composition moved (an apply began, or the fact names another build) and that no look
// has judged since it stopped moving: its health is not yet judged, never the last look's of what ran before.
unjudged map[string]bool
// unwritten is each module whose fact was taken in and is not yet written in its key: a write over a revision
// (NoteModuleAt) loses to it, as the key is about to move (design 47 H2 H-A-480).
unwritten map[string]bool
}
// inMotion says a module's phase is one of a change still under way: what a look finds of it then judges nothing of the
@@ -442,6 +445,7 @@ func (q *Session) writeHaves(ctx context.Context, bus Bus) {
s.written = map[string]string{}
}
s.written[module] = f.Build
delete(s.unwritten, module)
if say, _ := q.haveSaid(); say != nil {
said = append(said, func() { say(module, h) })
}
@@ -714,17 +718,7 @@ func (q *Session) NoteModule(ctx context.Context, f ModuleFact) {
q.init()
s := &q.haves
s.mu.Lock()
if s.modules == nil {
s.modules = map[string]ModuleFact{}
}
// A change under way, or a fact naming another composition than the one looked at, leaves the module's health not
// yet judged until a look after it: the last look judged what ran before (2026-10-25, the canary of the controller on
// novox held on a have saying applying and failed two seconds before its launch said healthy).
s.moved(f)
s.modules[f.Module] = f
delete(s.removed, f.Module)
// Out of motion and not judged: a look is owed now, not at the loop's next statement (review of fix 15, B1).
owe := !inMotion(f.Phase) && s.unjudged[f.Module]
owe := s.take(f)
s.mu.Unlock()
if owe {
q.oweHealth()
@@ -735,6 +729,72 @@ func (q *Session) NoteModule(ctx context.Context, f ModuleFact) {
q.writeHaves(ctx, bus)
}
// take takes in one module's fact to be written, and says whether a look is owed for it now. Under s.mu.
func (s *haveState) take(f ModuleFact) bool {
if s.modules == nil {
s.modules = map[string]ModuleFact{}
}
if s.unwritten == nil {
s.unwritten = map[string]bool{}
}
// A change under way, or a fact naming another composition than the one looked at, leaves the module's health not
// yet judged until a look after it: the last look judged what ran before (2026-10-25, the canary of the controller on
// novox held on a have saying applying and failed two seconds before its launch said healthy).
s.moved(f)
s.modules[f.Module] = f
s.unwritten[f.Module] = true
delete(s.removed, f.Module)
// Out of motion and not judged: a look is owed now, not at the loop's next statement (review of fix 15, B1).
return !inMotion(f.Phase) && s.unjudged[f.Module]
}
// NoteModuleAt is NoteModule as a compare-and-set on the revision of the module's key the caller read (zero: no key):
// the fact is taken in and written only while the key is still at that revision and no other fact of the module waits
// to be written; otherwise nothing is taken in, and it answers false, for the caller to read again (design 47 H2
// H-A-480: the composer's mark never hides a wait written after its read). An error is the key that could not be read.
func (q *Session) NoteModuleAt(ctx context.Context, f ModuleFact, revision uint64) (bool, error) {
q.init()
q.mu.Lock()
bus := q.bus
q.mu.Unlock()
hb, ok := bus.(HaveBus)
if !ok || bus == nil {
return false, ErrNotLinkedHaves
}
key, err := nodeengineclient.HaveKey(q.Membership.Node, f.Module)
if err != nil {
return false, err
}
s := &q.haves
s.mu.Lock()
if s.unwritten[f.Module] {
s.mu.Unlock()
return false, nil
}
read, cancel := context.WithTimeout(ctx, q.timeout())
defer cancel()
bucket, err := hb.HaveBind(read)
if err != nil {
s.mu.Unlock()
return false, err
}
_, now, err := bucket.Get(read, key)
if errors.Is(err, ErrHaveAbsent) {
now, err = 0, nil
}
if err != nil || now != revision {
s.mu.Unlock()
return false, err
}
owe := s.take(f)
s.mu.Unlock()
if owe {
q.oweHealth()
}
q.writeHaves(ctx, bus)
return true, nil
}
// NoteUserList takes what the bus node's engine wrote of the bus's user list, and writes it in the bus module's have
// where it differs from the bucket's (novox/hq ADR 0547 §8). A module this engine has said no have of yet carries it
// from its first have on.
@@ -767,6 +827,7 @@ func (q *Session) NoteRemoved(ctx context.Context, module, build string) {
}
s.removed[module] = build
delete(s.modules, module)
delete(s.unwritten, module)
delete(s.written, module)
delete(s.unjudged, module)
s.mu.Unlock()
@@ -892,36 +953,43 @@ var ErrNotLinkedHaves = errors.New("no link to the bus that binds the have bucke
// ReadHaveErr is ReadHave telling a key absent (false, nil) from one that could not be read (false, the error): a bus
// that does not answer for a moment is not a module that never ran (issue 1126).
func (q *Session) ReadHaveErr(ctx context.Context, module string) (nodeengine.HaveByNodeModuleValue, bool, error) {
h, _, ok, err := q.ReadHaveAt(ctx, module)
return h, ok, err
}
// ReadHaveAt is ReadHaveErr with the revision of the key it read (zero when the bucket holds none), for a write over it
// (NoteModuleAt).
func (q *Session) ReadHaveAt(ctx context.Context, module string) (nodeengine.HaveByNodeModuleValue, uint64, bool, error) {
q.init()
q.mu.Lock()
bus := q.bus
q.mu.Unlock()
hb, ok := bus.(HaveBus)
if !ok || bus == nil {
return nodeengine.HaveByNodeModuleValue{}, false, ErrNotLinkedHaves
return nodeengine.HaveByNodeModuleValue{}, 0, false, ErrNotLinkedHaves
}
key, err := nodeengineclient.HaveKey(q.Membership.Node, module)
if err != nil {
return nodeengine.HaveByNodeModuleValue{}, false, err
return nodeengine.HaveByNodeModuleValue{}, 0, false, err
}
read, cancel := context.WithTimeout(ctx, q.timeout())
defer cancel()
bucket, err := hb.HaveBind(read)
if err != nil {
return nodeengine.HaveByNodeModuleValue{}, false, err
return nodeengine.HaveByNodeModuleValue{}, 0, false, err
}
value, _, err := bucket.Get(read, key)
value, revision, err := bucket.Get(read, key)
if errors.Is(err, ErrHaveAbsent) {
return nodeengine.HaveByNodeModuleValue{}, false, nil
return nodeengine.HaveByNodeModuleValue{}, 0, false, nil
}
if err != nil {
return nodeengine.HaveByNodeModuleValue{}, false, err
return nodeengine.HaveByNodeModuleValue{}, 0, false, err
}
h, err := nodeengineclient.DecodeHave(value)
if err != nil {
return nodeengine.HaveByNodeModuleValue{}, false, nil // a have no engine can read names nothing it runs
return nodeengine.HaveByNodeModuleValue{}, revision, false, nil // a have no engine can read names nothing it runs
}
return h, true, nil
return h, revision, true, nil
}
// nothingJudged is the health of a module a look judged no resource of: ok when it was looked at and the module applied
+37
View File
@@ -1082,4 +1082,41 @@ func TestHave(t *testing.T) {
t.Run("AHealthIsNotCarriedOverToAnotherComposition", testAHealthIsNotCarriedOverToAnotherComposition)
t.Run("ADeclarationReportMovesAHealthAndARemovalForgetsIt", testADeclarationReportMovesAHealthAndARemovalForgetsIt)
t.Run("AUserListReachesTheHaveOfAModuleNoApplyHereSays", testAUserListReachesTheHaveOfAModuleNoApplyHereSays)
t.Run("AWriteOverARevisionLosesToAKeyThatMovedOrAFactNotYetWritten", testAWriteOverARevisionLosesToAKeyThatMovedOrAFactNotYetWritten)
}
// H-A-480: the composer's mark is written over the revision of the have it read; a key that moved since, or a fact of the
// module taken in and not yet written, wins, and nothing of the mark is taken in.
func testAWriteOverARevisionLosesToAKeyThatMovedOrAFactNotYetWritten(t *testing.T) {
l := newHaveLink()
q, _ := aHaveQueue(l)
ctx := context.Background()
mark := ModuleFact{Module: "a", Phase: nodeengineclient.PhaseFailed, Failed: true, Detail: "the node cannot compose it"}
if ok, err := q.NoteModuleAt(ctx, mark, 0); !ok || err != nil || l.have(t, "n1.a").Detail != mark.Detail {
t.Fatalf("a mark over no key: %v %v, %+v", ok, err, l.have(t, "n1.a"))
}
wait := ModuleFact{Module: "a", Phase: nodeengineclient.PhaseHeld, Detail: "a waits for b of its pipeline"}
q.NoteModule(ctx, wait)
_, rev, _, err := q.ReadHaveAt(ctx, "a")
if err != nil || rev != 2 {
t.Fatalf("the revision read: %d %v", rev, err)
}
if ok, err := q.NoteModuleAt(ctx, mark, 1); ok || err != nil || l.have(t, "n1.a").Detail != wait.Detail {
t.Fatalf("a mark over a revision the key moved from: %v %v, %+v", ok, err, l.have(t, "n1.a"))
}
// A fact taken in whose write failed is still to be written: a mark over the revision read loses to it.
l.hmu.Lock()
l.bindFails = true
l.hmu.Unlock()
q.NoteModule(ctx, ModuleFact{Module: "a", Phase: nodeengineclient.PhaseHeld, Detail: "a waits for c of its pipeline"})
l.hmu.Lock()
l.bindFails = false
l.hmu.Unlock()
if ok, err := q.NoteModuleAt(ctx, mark, 2); ok || err != nil {
t.Fatalf("a mark over a fact not yet written: %v %v", ok, err)
}
q.NoteModule(ctx, ModuleFact{Module: "a", Phase: nodeengineclient.PhaseHeld, Detail: "a waits for c of its pipeline"})
if got := l.have(t, "n1.a").Detail; got != "a waits for c of its pipeline" {
t.Fatalf("the fact the mark lost to was not written: %q", got)
}
}
@@ -79,3 +79,25 @@ func composeAnsweredWhileAPassRuns(t *testing.T) {
t.Fatal("the queued pass was not made when the running one ended")
}
}
// H-A-480: the composer's watch ends with the agent's stop: once its ctx ended, Wait returns only after its loop ended,
// so nothing it says comes after the run (the integration tests at -count=15 logged after their test completed).
func theWatchEndsWithItsRun(t *testing.T) {
b := newFakeBus()
ctx, cancel := context.WithCancel(context.Background())
c := New(b.env())
out, err := c.Watch(ctx, "n1.>")
if err != nil {
t.Fatal(err)
}
cancel()
c.Wait()
select {
case _, ok := <-out:
for ok {
_, ok = <-out
}
default:
t.Fatal("the composer's watch went on after Wait returned")
}
}
@@ -91,6 +91,7 @@ func (f *fakeComposed) keep(t *testing.T, half catalogHalf, module, fp string) {
// by a want, and deleted otherwise.
func TestTheNodePublishesItsCompositions(t *testing.T) {
t.Run("the verb compose is answered while a pass runs", composeAnsweredWhileAPassRuns)
t.Run("the watch ends with its run", theWatchEndsWithItsRun)
b := newFakeBus()
fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64)
ofp := meshcatalogclient.SourcePrefix2 + strings.Repeat("c", 64)
+22 -6
View File
@@ -197,6 +197,8 @@ type Composer struct {
AlsoEntriesAskAgain func(ctx context.Context)
env Env
// watchers counts the goroutines Watch started that end with its ctx (its loop and its hearers), for Wait (H-A-480).
watchers sync.WaitGroup
mu sync.Mutex
wants map[string]meshwants.WantsByWantNodeModuleValue // by module, the node's own under agent.NodeModule
assignments map[string]meshmoduleassigner.Assignments
@@ -582,12 +584,16 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er
}(i, ch)
}
if c.env.Hear != nil {
go c.hearCatalogRevisions(ctx, func() {
select {
case merged <- tagged{in: -1, ok: true}:
case <-ctx.Done():
}
})
c.watchers.Add(1)
go func() {
defer c.watchers.Done()
c.hearCatalogRevisions(ctx, func() {
select {
case merged <- tagged{in: -1, ok: true}:
case <-ctx.Done():
}
})
}()
heard, stop, err := c.env.Hear(VaultSecretChanged)
if err != nil {
return nil, fmt.Errorf("the node composes again when a secret changes, and cannot hear the vault say so: %w", err)
@@ -604,7 +610,9 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er
heard <-chan []byte
stop func()
}{{heard, stop}, {changed, stopChanged}} {
c.watchers.Add(1)
go func() {
defer c.watchers.Done()
defer h.stop()
for {
select {
@@ -625,7 +633,9 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er
}
}
if c.env.SeatListChanged != nil {
c.watchers.Add(1)
go func() {
defer c.watchers.Done()
for {
select {
case <-ctx.Done():
@@ -645,7 +655,9 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er
}
out := make(chan agent.Update, 64)
c.watching.Store(true)
c.watchers.Add(1)
go func() {
defer c.watchers.Done()
defer close(out)
defer c.watching.Store(false)
caught := make([]bool, len(ins))
@@ -761,6 +773,10 @@ func (c *Composer) Watch(ctx context.Context, _ string) (<-chan agent.Update, er
return out, nil
}
// Wait waits until the loop and the hearers Watch started have ended, each once its ctx ended: the node agent's stop waits for it,
// so the composer says nothing once its run is over (design 47 H2 H-A-480).
func (c *Composer) Wait() { c.watchers.Wait() }
// hearCatalogRevisions hears the seat mesh-catalog's event catalog-revision: each one asks again the entries whose ask failed (H-A-110), clears the backoffs of the trunk builds the
// catalog gave none of and wakes a pass (A-487a). A wake-up only, the backoff bounds the asks without it: a subscription refused
// is said once with its subject and error (principle 3), the composer goes on composing, and the subscription is tried again on