diff --git a/cmd/mesh-host/nodeagent.go b/cmd/mesh-host/nodeagent.go index fde0506a..5c10a5b0 100644 --- a/cmd/mesh-host/nodeagent.go +++ b/cmd/mesh-host/nodeagent.go @@ -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) diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 159b5c69..cf1c7f67 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -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<") + 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") + } +} diff --git a/internal/selfcompose/composedstate_test.go b/internal/selfcompose/composedstate_test.go index 5de5c96f..104216c0 100644 --- a/internal/selfcompose/composedstate_test.go +++ b/internal/selfcompose/composedstate_test.go @@ -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) diff --git a/internal/selfcompose/selfcompose.go b/internal/selfcompose/selfcompose.go index cf265bfd..4c7d33ba 100644 --- a/internal/selfcompose/selfcompose.go +++ b/internal/selfcompose/selfcompose.go @@ -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