diff --git a/cmd/mesh-host/nodeagent.go b/cmd/mesh-host/nodeagent.go index fde0506a..c3b8e793 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) { diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 159b5c69..41fc650e 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<