From 0f41b405ba65da5b62f30e9169ff084e4be82d1e Mon Sep 17 00:00:00 2001 From: jochen Date: Fri, 20 Nov 2026 17:35:37 +0100 Subject: [PATCH] Builder: a step ask naming a package with no pin waits for its release (H-A-165) An entry keeps a mesh package with no released version unpinned (H-A-161). Its step ask now waits, in the wait of H-A-155, for the release of the module holding the package, heard or kept in the builder's record of releases, and builds with that release as the pin; past the bound the step fails naming the package and builds nothing. Builds: design 47 H H-A-165, H-A-155, H-A-161 --- .../internal/build/revisionwait.go | 108 ++++++++++++++++++ .../internal/build/run.go | 8 ++ .../internal/build/step.go | 4 + .../internal/build/step_test.go | 76 ++++++++++++ .../internal/versions/reserve.go | 17 +++ 5 files changed, 213 insertions(+) diff --git a/modules/mesh-delivery-builder/internal/build/revisionwait.go b/modules/mesh-delivery-builder/internal/build/revisionwait.go index 32c15fcf..6739c40b 100644 --- a/modules/mesh-delivery-builder/internal/build/revisionwait.go +++ b/modules/mesh-delivery-builder/internal/build/revisionwait.go @@ -3,10 +3,13 @@ package build import ( "context" "fmt" + "maps" + "slices" "time" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" ) // **A build waits for its revision; a build with no version is never made** (design 47 H H-A-155, H-A-156; H-A-141): a step @@ -100,3 +103,108 @@ func (b *Builder) awaitRevision(ctx context.Context, run meshbuilds.StepRun) (ou } } } + +// **A package with no pin waits for its release** (design 47 H H-A-165, H-A-161): an entry keeps a mesh package none of +// whose versions is released with an empty version. Its step ask waits, as for its revision, for the release of the module +// that holds the package (the catalog's version-released, VersionReleased), up to PackageWait (the setting +// package-wait-minutes), then builds with that release as the package's pin (v); past the bound the step fails, +// naming the package, and nothing is built. + +// DefaultPackageWait is the bound of a step ask's wait for a package's release, its setting package-wait-minutes' default. +const DefaultPackageWait = 10 * time.Minute + +func (b *Builder) packageWait() time.Duration { + if b.PackageWait > 0 { + return b.PackageWait + } + return DefaultPackageWait +} + +// VersionReleased takes the catalog's version-released of module in: a step ask waiting for a package of it wakes. +func (b *Builder) VersionReleased(module, version string) { + b.init() + b.mu.Lock() + b.releases[module] = version + b.mu.Unlock() + b.pictureMoved() +} + +// releaseOf is the pin a package is built against: the release of the module holding it, as Go names it; "" for none yet. +func (b *Builder) releaseOf(ctx context.Context, pkg string) string { + if b.Members == nil { + return "" + } + module, _, found, err := b.Members(ctx, pkg) + if err != nil || !found { + return "" + } + b.mu.Lock() + v := b.releases[module] + b.mu.Unlock() + if v == "" && b.NewestReleased != nil { + if kept, ok, err := b.NewestReleased(ctx, module); err == nil && ok { + v = kept + } + } + if v == "" { + return "" + } + return "v" + v +} + +// awaitPins fills each package entry names with no pin from its release, waiting for one; no outcome once every package has +// a pin, failed past the bound, could not run at the builder's stop. +func (b *Builder) awaitPins(ctx context.Context, entry *meshcatalog.CatalogEntry) (outcome, said string) { + var unpinned []string + for p, v := range entry.Packages { + if v == "" { + unpinned = append(unpinned, p) + } + } + if len(unpinned) == 0 { + return "", "" + } + slices.Sort(unpinned) + bound := b.packageWait() + wait, stop := context.WithTimeout(ctx, bound) // a context's bound, no timer (ADR 0437 §8) + defer stop() + pins := map[string]string{} + told := false + for { + b.mu.Lock() + moved := b.moved + b.mu.Unlock() + missing := "" + for _, p := range unpinned { + if pins[p] == "" { + pins[p] = b.releaseOf(ctx, p) + } + if pins[p] == "" && missing == "" { + missing = p + } + } + if missing == "" { + packages := maps.Clone(entry.Packages) + maps.Copy(packages, pins) + entry.Packages = packages + if told { + b.logf("%s at %s: every package it names has a released version: built with %v", entry.Module, entry.Fp, pins) + } + return "", "" + } + notReleased := fmt.Sprintf("package %s has no released version", missing) + if !told { + b.logf("%s at %s: %s yet: the ask waits for its release, at most %s", entry.Module, entry.Fp, notReleased, bound) + told = true + } + select { + case <-moved: + case <-wait.Done(): + if ctx.Err() != nil { + return meshbuilds.StepEndedOutcomeCouldNotRun, notReleased + ": the builder stopped before it was" + } + b.logf("%s at %s: %s within %s: the step fails, nothing is built", entry.Module, entry.Fp, notReleased, bound) + return meshbuilds.StepEndedOutcomeFailed, fmt.Sprintf("%s within %s", notReleased, bound) + } + } +} diff --git a/modules/mesh-delivery-builder/internal/build/run.go b/modules/mesh-delivery-builder/internal/build/run.go index 4b3906fa..7a329c8c 100644 --- a/modules/mesh-delivery-builder/internal/build/run.go +++ b/modules/mesh-delivery-builder/internal/build/run.go @@ -113,6 +113,11 @@ type Builder struct { // RevisionWait bounds a step ask's wait for its revision, its setting revision-wait-minutes (H-A-156); DefaultRevisionWait // when 0. RevisionWait time.Duration + // PackageWait bounds a step ask's wait for the release of a package its entry names with no pin, its setting + // package-wait-minutes (H-A-165); DefaultPackageWait when 0. NewestReleased reads the newest version the builder's + // record of releases keeps for a module; nil reads none, and only a release heard (VersionReleased) gives a pin. + PackageWait time.Duration + NewestReleased func(ctx context.Context, module string) (version string, found bool, err error) // Forms answers what each node's engine states it reads (ADR 0618 §2, ADR 0624 §1): present, the nodes whose engine // answered when last asked, and counted, every node the assignments names, present or away, a node with no statement // stating nothing (the baseline's first forms). A done key names each artifact in the forms these keep, the oldest in @@ -157,6 +162,8 @@ type Builder struct { builds map[string]context.CancelCauseFunc // moved is closed and made again at each entry the picture takes in or drops: the step asks waiting for a revision wake. moved chan struct{} + // releases are the versions heard released, by module (VersionReleased): a package with no pin waits for one. + releases map[string]string } type planned struct { @@ -187,6 +194,7 @@ func (b *Builder) init() { b.hands, b.events = map[string]*handWatch{}, map[string]bool{} b.builds = map[string]context.CancelCauseFunc{} b.moved = make(chan struct{}) + b.releases = map[string]string{} }) } diff --git a/modules/mesh-delivery-builder/internal/build/step.go b/modules/mesh-delivery-builder/internal/build/step.go index eb40ed3a..af0c9f9d 100644 --- a/modules/mesh-delivery-builder/internal/build/step.go +++ b/modules/mesh-delivery-builder/internal/build/step.go @@ -63,6 +63,10 @@ func (b *Builder) buildEntry(ctx context.Context, e meshbuilds.BuildEntry) (outc if entry.Module != e.Module || (e.Commit != "" && entry.Commit != e.Commit) { return meshbuilds.StepEndedOutcomeCouldNotRun, fmt.Sprintf("the entry of %s at %s is of %s at %s", e.Module, e.Fingerprint, entry.Module, entry.Commit) } + // A package the entry names with no pin is built against its release, waited for (H-A-165). + if outcome, said := b.awaitPins(ctx, &entry); outcome != "" { + return outcome, said + } key, err := builds.BuilderBuildKey(entry.Module, entry.Fp) if err != nil { return meshbuilds.StepEndedOutcomeCouldNotRun, err.Error() diff --git a/modules/mesh-delivery-builder/internal/build/step_test.go b/modules/mesh-delivery-builder/internal/build/step_test.go index 920a4f74..91854a2a 100644 --- a/modules/mesh-delivery-builder/internal/build/step_test.go +++ b/modules/mesh-delivery-builder/internal/build/step_test.go @@ -3,7 +3,9 @@ package build import ( "context" "errors" + "reflect" "strings" + "sync" "sync/atomic" "testing" "time" @@ -11,6 +13,7 @@ import ( "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep" "git.novox.be/novox/mesh-sdk/go/catalogue" "mesh-delivery-builder/internal/versions" @@ -116,6 +119,79 @@ func TestABuildStepPassesAReservingBuildOnlyWithItsReservedVersion(t *testing.T) t.Run("H-A-155: an ask waits for the picture to take its revision in, then builds with its version reserved", aStepAskWaitsForItsRevision) t.Run("H-A-155: past the bound the ask fails naming its revision, and nothing is built", aStepAskPastItsRevisionBoundFails) t.Run("H-A-162: a revision held that changes no entry wakes the ask", aRevisionHeldWakesTheAsk) + t.Run("H-A-165: a package with no pin waits for its release and is built with it", aPackageWithNoPinWaitsForItsRelease) + t.Run("H-A-165: past its bound a package with no release fails the step", aPackageWithNoReleaseFailsPastItsBound) +} + +// unpinnedRig is a rig whose step entry m names the package x with no version, built by the module contracts. +func unpinnedRig(t *testing.T) (*rig, *fakeReservations) { + var held atomic.Int64 + r, res, entry := revisionRig(t, &held, make(chan struct{}, 1)) + r.b.Picture = nil // no revision to wait for here + entry.Packages = map[string]string{"git.novox.be/novox/mesh-contracts/go/seats/x": "", "git.novox.be/novox/mesh-sdk/go": "v0.1.20"} + r.b.StepEntry = func(context.Context, string) (meshcatalog.CatalogEntry, error) { return entry, nil } + r.b.Members = func(_ context.Context, pkg string) (string, string, bool, error) { + return "contracts", "contracts:x", pkg == "git.novox.be/novox/mesh-contracts/go/seats/x", nil + } + return r, res +} + +func aPackageWithNoPinWaitsForItsRelease(t *testing.T) { + r, _ := unpinnedRig(t) + var once sync.Once + looked := make(chan struct{}) + r.b.NewestReleased = func(context.Context, string) (string, bool, error) { // the record holds none yet + once.Do(func() { close(looked) }) + return "", false, nil + } + ended := make(chan meshbuilds.StepEnded, 1) + go func() { + ended <- r.b.BuildStep(t.Context(), meshbuilds.StepRun{Pipeline: "main.9.m", Step: meshbuilds.StepRunStepBuild, Branch: "main", Source: 41, + Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}) + }() + select { + case <-looked: + case end := <-ended: + t.Fatalf("the ask ended before the package was released: %+v, asks %+v", end, r.asks.Asks()) + case <-time.After(5 * time.Second): + t.Fatal("the ask never looked for the package's release") + } + if _, ok := r.st.vals[r.key]; ok { + t.Fatal("the build was made before the package had a release") + } + r.b.VersionReleased("contracts", "0.1.3") + var end meshbuilds.StepEnded + select { + case end = <-ended: + case <-time.After(5 * time.Second): + t.Fatal("the release did not wake the ask") + } + r.b.Wait() + asks := r.asks.Asks() + if end.Outcome != meshbuilds.StepEndedOutcomePassed || len(asks) == 0 || + !reflect.DeepEqual(asks[0].Packages, buildstep.PackageVersions{"git.novox.be/novox/mesh-contracts/go/seats/x": "v0.1.3", "git.novox.be/novox/mesh-sdk/go": "v0.1.20"}) { + t.Errorf("end = %+v, asks %+v; want passed, built with x at v0.1.3", end, asks) + } +} + +func aPackageWithNoReleaseFailsPastItsBound(t *testing.T) { + r, _ := unpinnedRig(t) + r.b.PackageWait = time.Nanosecond + end := r.b.BuildStep(t.Context(), meshbuilds.StepRun{Pipeline: "main.9.m", Step: meshbuilds.StepRunStepBuild, Branch: "main", Source: 41, + Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}) + r.b.Wait() + if _, built := r.st.vals[r.key]; end.Outcome != meshbuilds.StepEndedOutcomeFailed || built || + !strings.Contains(end.Said, "package git.novox.be/novox/mesh-contracts/go/seats/x has no released version") { + t.Errorf("end = %+v, built %v; want failed naming the package, nothing built", end, built) + } + // A release the builder's own record keeps gives the pin at once. + r.b.NewestReleased = func(_ context.Context, module string) (string, bool, error) { + return "0.1.2", module == "contracts", nil + } + if end := r.b.BuildStep(t.Context(), meshbuilds.StepRun{Pipeline: "main.9.m", Step: meshbuilds.StepRunStepBuild, Branch: "main", Source: 41, + Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}); strings.Contains(end.Said, "no released version") { + t.Errorf("a release the record keeps did not pin the package: %+v", end) + } } func aRevisionHeldWakesTheAsk(t *testing.T) { diff --git a/modules/mesh-delivery-builder/internal/versions/reserve.go b/modules/mesh-delivery-builder/internal/versions/reserve.go index 0bd46450..0527980e 100644 --- a/modules/mesh-delivery-builder/internal/versions/reserve.go +++ b/modules/mesh-delivery-builder/internal/versions/reserve.go @@ -295,6 +295,23 @@ func (r *Reserver) ReleasedFor(ctx context.Context, module, fingerprint string) return false, nil } +// NewestReleased is the newest version the builder's own record of releases keeps for module (design 47 H H-A-165): a +// package with no pin is built against it; false when the record holds none. +func (r *Reserver) NewestReleased(ctx context.Context, module string) (string, bool, error) { + if r.Releases == nil { + return "", false, nil + } + raw, _, found, err := r.Releases.Get(ctx, ReleasedKey(module)) + if err != nil || !found { + return "", false, err + } + var rec ReleasedRecord + if json.Unmarshal(raw, &rec) != nil || len(rec.Kept) == 0 { + return "", false, nil + } + return rec.Kept[len(rec.Kept)-1].Version, true, nil +} + // keepRelease adds a release to the module's record, dropping the oldest past ReleasedKept. func (r *Reserver) keepRelease(ctx context.Context, module, fingerprint, version string) error { n := r.ReleasedKept