diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go index 181ed4907..364e48f03 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/main.go @@ -224,8 +224,6 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro return err } b.RecordBound = time.Duration(recordBound) * time.Second - // Before it waits for an unpinned package, the build reads what the Go package registry seat holds of it (H-A-194). - b.PublishedVersions = publishedVersionsVia(calls.Call) waitsBucket := process.AwaitBucket(ctx, js, conn, builds.StateBucket(cred.Module, build.WaitsState), "the toolchain waits are kept in memory only, and builder_waits shows none", logger.Printf) b.Waits = newWaitsKept(waitsBucket, func(ctx context.Context, event, id string, body any) error { return emitEvent(context.WithoutCancel(ctx), js, cred, event, id, body) diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go index 782c1c49d..c332ce3e1 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go @@ -189,6 +189,11 @@ func (rv *reservations) TakenOver(ctx context.Context, module, fingerprint strin return rv.r.TakenOver(ctx, module, fingerprint) } +// VersionAt is the version module's artifact is published at from the source revision of branch (H-A-547 (3), H-A-551). +func (rv *reservations) VersionAt(ctx context.Context, module, artifact, branch string, source int64) (string, bool, error) { + return rv.r.VersionAt(ctx, module, artifact, branch, source) +} + func (rv *reservations) Filled() bool { return rv.filled.Load() } func (rv *reservations) Reserve(ctx context.Context, module, series string, artifacts []string, src versions.Source) (versions.ReservedVersion, error) { diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/versions.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/versions.go index d57587403..0ac59ccad 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/versions.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/versions.go @@ -4,13 +4,11 @@ import ( "context" "encoding/json" "errors" - "fmt" "time" "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" - "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/gopackageregistry" "git.novox.be/novox/mesh-sdk/go/process" "mesh-delivery-builder/internal/versions" @@ -87,28 +85,6 @@ func (s awaitedState) KeysOf(ctx context.Context, filter string) ([]string, erro return out, nil } -// publishedVersionsVia asks the Go package registry seat's verb versions for one package path (design 47 H H-A-194): its -// versions, newest first. -func publishedVersionsVia(call toolCaller) func(context.Context, string) ([]string, error) { - return func(_ context.Context, path string) ([]string, error) { - raw, err := call("seat:"+gopackageregistry.Seat+"."+gopackageregistry.VerbVersions, gopackageregistry.VersionsInput{Name: path}) - if err != nil { - return nil, err - } - var a gopackageregistry.VersionsAnswer - if err := json.Unmarshal(raw, &a); err != nil { - return nil, fmt.Errorf("%s's versions answered what is no list of versions: %w", gopackageregistry.Seat, err) - } - var out []string - for _, v := range a.Versions { - if v.Version != "" { - out = append(out, v.Version) - } - } - return out, nil - } -} - // emitEvent says one of the builder's events, mesh.mod..event., with the SDK's envelope headers and id as // the bus's dedup id. func emitEvent(ctx context.Context, js jetstream.JetStream, cred process.Account, event, id string, body any) error { diff --git a/modules/mesh-delivery-builder/internal/build/numbers.go b/modules/mesh-delivery-builder/internal/build/numbers.go index 20f02d188..1a640aed9 100644 --- a/modules/mesh-delivery-builder/internal/build/numbers.go +++ b/modules/mesh-delivery-builder/internal/build/numbers.go @@ -127,6 +127,9 @@ type Reservations interface { // TakenOver is the reservation of module's series that took the series over from the build of fingerprint, whose number // it keeps withdrawn (design 47 H H-A-226), and whether one does (H-A-244). TakenOver(ctx context.Context, module, fingerprint string) (versions.ReservedVersion, bool, error) + // VersionAt is the version module's artifact is published at from the source revision of branch: its reservation of that + // origin with the artifact published, else its release recorded at that source revision (design 47 H2 H-A-547 (3), H-A-551). + VersionAt(ctx context.Context, module, artifact, branch string, source int64) (string, bool, error) } // reserves says whether a look at a trunk build is one that reserves: the build has a series of the form MAJOR.MINOR diff --git a/modules/mesh-delivery-builder/internal/build/reserve_test.go b/modules/mesh-delivery-builder/internal/build/reserve_test.go index bb63848d4..7b850c1e2 100644 --- a/modules/mesh-delivery-builder/internal/build/reserve_test.go +++ b/modules/mesh-delivery-builder/internal/build/reserve_test.go @@ -8,6 +8,7 @@ import ( "reflect" "slices" "strings" + "sync" "testing" "time" @@ -34,6 +35,25 @@ type fakeReservations struct { tookBy map[string]versions.ReservedVersion // by fingerprint, the reservation that took its series over (H-A-244) held map[string]versions.ReservedVersion // by fingerprint, the reservation ReservedFor answers as it stands (H-A-492) lost error // the record of the digests fails, as a store refusing it would + atMu sync.Mutex + at map[string]string // by ":@.", the version VersionAt answers (H-A-547 (3)) +} + +func (f *fakeReservations) VersionAt(_ context.Context, module, artifact, branch string, source int64) (string, bool, error) { + f.atMu.Lock() + defer f.atMu.Unlock() + v, ok := f.at[fmt.Sprintf("%s:%s@%s.%d", module, artifact, branch, source)] + return v, ok, nil +} + +// publishAt makes VersionAt answer version for module's artifact at the origin branch and source. +func (f *fakeReservations) publishAt(module, artifact, branch string, source int64, version string) { + f.atMu.Lock() + defer f.atMu.Unlock() + if f.at == nil { + f.at = map[string]string{} + } + f.at[fmt.Sprintf("%s:%s@%s.%d", module, artifact, branch, source)] = version } func (f *fakeReservations) TakenOver(_ context.Context, _, fp string) (versions.ReservedVersion, bool, error) { diff --git a/modules/mesh-delivery-builder/internal/build/revisionwait.go b/modules/mesh-delivery-builder/internal/build/revisionwait.go index 676d30c56..417f92c43 100644 --- a/modules/mesh-delivery-builder/internal/build/revisionwait.go +++ b/modules/mesh-delivery-builder/internal/build/revisionwait.go @@ -5,7 +5,6 @@ import ( "fmt" "maps" "slices" - "strconv" "strings" "time" @@ -190,14 +189,13 @@ func (b *Builder) awaitRevision(ctx context.Context, run meshbuilds.StepRun) (ou } } -// **A package with no pin waits for its publish** (design 47 H H-A-192, H-A-165, H-A-183, H-A-184, H-A-185): an entry -// keeps a mesh package the registry has not published within the series its module accepts with an empty version. Its -// build, a step ask's or a lane's, first reads the versions the registry seat holds of the path (its verb versions, -// H-A-194), then waits for the registry seat's published of that package path in that series (the -// series the module's manifest names in `packages`, else the default; catalogue.Manifest.AcceptedSeries), up to -// PackageWait (the setting package-wait-minutes), and pins the version that event carries; the builder never derives a -// pin (no release number of the module that holds the package). Past the bound the build fails, naming the package, and -// nothing is built. +// **A package with no pin is built against its publisher's version at the key's origin** (design 47 H2 H-A-521, H-A-547 (3), +// H-A-551; H-A-165): an entry names a mesh package with no version when a mover of the same take-in publishes it (DMC-1). Its +// build, a step ask's or a lane's, fills that version from the reservation of the module that publishes it (Members) at the +// source revision of the build key's own origin, once that reservation's artifact is published, and else from that module's +// release recorded at that source revision; never from the newest a registry seat said published. It waits for one, woken by +// each registry seat's published heard (PackagePublished), up to PackageWait (the setting package-wait-minutes). Past the bound +// the build fails, naming the package, and nothing is built. // DefaultPackageWait is the bound of a build's wait for a package's publish, its setting package-wait-minutes' default. const DefaultPackageWait = 10 * time.Minute @@ -209,44 +207,46 @@ func (b *Builder) packageWait() time.Duration { return DefaultPackageWait } -// PackagePublished takes the Go package registry seat's published of one package path in (its name and the version the -// event carries, H-A-192): the newest published in the version's series is kept, and a build waiting for the package -// wakes. A version that is no MAJOR.MINOR.PATCH (a group's tagged version) pins nothing. -func (b *Builder) PackagePublished(path, version string) { - v := strings.TrimPrefix(version, "v") - series, ok := seriesOfVersion(v) - if path == "" || !ok { +// PackagePublished is told each registry seat's published of a package (its name and the version the event carries): a build +// waiting for a package's version looks again at the reservations, which the published marked before it is told. +func (b *Builder) PackagePublished(name, version string) { + if name == "" { return } b.init() - b.mu.Lock() - in := b.publishedPins[path] - if in == nil { - in = map[string]string{} - b.publishedPins[path] = in - } - if old := in[series]; old == "" || compareVersions(v, old) > 0 { - in[series] = v - } - b.mu.Unlock() b.pictureMoved() } -// publishedPin is the pin the registry seat's published gave the package path in the series, as Go names it; "" for none. -func (b *Builder) publishedPin(path, series string) string { - b.mu.Lock() - v := b.publishedPins[path][series] - b.mu.Unlock() - if v == "" { - return "" +// versionAtOrigin is the version, as Go names it, the module that publishes the package holds at the source revision of the +// key's origin (H-A-547 (3), H-A-551), and words saying why there is none. +func (b *Builder) versionAtOrigin(ctx context.Context, key, pkg string) (version, none string) { + if b.Members == nil || b.Reserved == nil { + return "", "no module that publishes it can be read" } - return "v" + v + module, artifact, found, err := b.Members(ctx, pkg) + if err != nil || !found { + if err != nil { + return "", fmt.Sprintf("the module that publishes it cannot be read: %v", err) + } + return "", "no entry names the module that publishes it" + } + b.mu.Lock() + o := b.origin[key] + b.mu.Unlock() + v, found, err := b.Reserved.VersionAt(ctx, module, artifact, o.Branch, o.Source) + if err != nil { + return "", fmt.Sprintf("the version %s holds at source revision %d cannot be read: %v", module, o.Source, err) + } + if !found { + return "", fmt.Sprintf("%s publishes no version of it at source revision %d of %s", module, o.Source, o.Branch) + } + return "v" + strings.TrimPrefix(v, "v"), "" } -// awaitPublished fills each package of packages with no pin from the published of its path within the series the manifest -// with this digest accepts, waiting for one (H-A-192, H-A-165). It answers the packages with every pin, or an outcome and -// its words: failed past the bound naming the package, could not run when the manifest cannot be read or the builder stops. -func (b *Builder) awaitPublished(ctx context.Context, module, fp, digest string, packages map[string]string) (map[string]string, string, string) { +// awaitPublished fills each package of packages with no pin from the version its publisher holds at the key's origin +// (versionAtOrigin), waiting for one (H-A-521, H-A-165). It answers the packages with every pin, or an outcome and its words: +// failed past the bound naming the package, could not run when the builder stops. +func (b *Builder) awaitPublished(ctx context.Context, module, fp string, packages map[string]string) (map[string]string, string, string) { var unpinned []string for p, v := range packages { if v == "" { @@ -257,26 +257,9 @@ func (b *Builder) awaitPublished(ctx context.Context, module, fp, digest string, return packages, "", "" } slices.Sort(unpinned) - if b.Manifests == nil { - return nil, meshbuilds.StepEndedOutcomeCouldNotRun, fmt.Sprintf("the series %s accepts of %s cannot be read: no manifests", module, unpinned[0]) - } - man, err := b.Manifests.Manifest(ctx, digest) + key, err := builds.BuilderBuildKey(module, fp) if err != nil { - return nil, meshbuilds.StepEndedOutcomeCouldNotRun, fmt.Sprintf("the series %s accepts of %s cannot be read: its manifest %s: %v", module, unpinned[0], digest, err) - } - // What the registry seat holds is read once before the wait (H-A-194): the publishes heard are a cache of it. - for _, p := range unpinned { - if b.PublishedVersions == nil || b.publishedPin(p, man.AcceptedSeries(p)) != "" { - continue - } - vs, err := b.PublishedVersions(ctx, p) - if err != nil { - b.logf("%s at %s: the versions the registry seat holds of %s cannot be read, so the build waits for its publish: %v", module, fp, p, err) - continue - } - for _, v := range vs { - b.PackagePublished(p, v) - } + return nil, meshbuilds.StepEndedOutcomeCouldNotRun, err.Error() } bound := b.packageWait() wait, stop := context.WithTimeout(ctx, bound) // a context's bound, no timer (ADR 0437 §8) @@ -287,12 +270,12 @@ func (b *Builder) awaitPublished(ctx context.Context, module, fp, digest string, moved := b.moved b.mu.Unlock() pins := map[string]string{} - missing, missingSeries := "", "" + missing, why := "", "" for _, p := range unpinned { - series := man.AcceptedSeries(p) - pins[p] = b.publishedPin(p, series) - if pins[p] == "" && missing == "" { - missing, missingSeries = p, series + v, none := b.versionAtOrigin(wait, key, p) + pins[p] = v + if v == "" && missing == "" { + missing, why = p, none } } if missing == "" { @@ -303,58 +286,19 @@ func (b *Builder) awaitPublished(ctx context.Context, module, fp, digest string, } return out, "", "" } - notReleased := fmt.Sprintf("package %s has no released version in the series %s", missing, missingSeries) + notPublished := fmt.Sprintf("package %s has no published version: %s", missing, why) if !told { - b.logf("%s at %s: %s yet: the build waits for its publish, at most %s", module, fp, notReleased, bound) + b.logf("%s at %s: %s yet: the build waits for its publish, at most %s", module, fp, notPublished, bound) told = true } select { case <-moved: case <-wait.Done(): if ctx.Err() != nil { - return nil, meshbuilds.StepEndedOutcomeCouldNotRun, notReleased + ": the builder stopped before it was" + return nil, meshbuilds.StepEndedOutcomeCouldNotRun, notPublished + ": the builder stopped before it was" } - b.logf("%s at %s: %s within %s: the build fails, nothing is built", module, fp, notReleased, bound) - return nil, meshbuilds.StepEndedOutcomeFailed, fmt.Sprintf("%s within %s", notReleased, bound) + b.logf("%s at %s: %s within %s: the build fails, nothing is built", module, fp, notPublished, bound) + return nil, meshbuilds.StepEndedOutcomeFailed, fmt.Sprintf("%s within %s", notPublished, bound) } } } - -// seriesOfVersion is a MAJOR.MINOR.PATCH version's series, MAJOR.MINOR; false for any other form. -func seriesOfVersion(v string) (string, bool) { - if _, ok := versionParts(v); !ok { - return "", false - } - major, rest, _ := strings.Cut(v, ".") - minor, _, _ := strings.Cut(rest, ".") - return major + "." + minor, true -} - -// versionParts are a MAJOR.MINOR.PATCH version's numbers; false for any other form. -func versionParts(v string) ([3]int, bool) { - var out [3]int - parts := strings.Split(v, ".") - if len(parts) != 3 { - return out, false - } - for i, p := range parts { - n, err := strconv.Atoi(p) - if err != nil || n < 0 { - return out, false - } - out[i] = n - } - return out, true -} - -// compareVersions orders two MAJOR.MINOR.PATCH versions numerically. -func compareVersions(a, b string) int { - x, _ := versionParts(a) - y, _ := versionParts(b) - for i := range x { - if x[i] != y[i] { - return x[i] - y[i] - } - } - return 0 -} diff --git a/modules/mesh-delivery-builder/internal/build/run.go b/modules/mesh-delivery-builder/internal/build/run.go index 39ddc1ee2..6927d0f5d 100644 --- a/modules/mesh-delivery-builder/internal/build/run.go +++ b/modules/mesh-delivery-builder/internal/build/run.go @@ -121,15 +121,12 @@ type Builder struct { // when 0. RevisionWait time.Duration // PackageWait bounds a build's wait for the publish of a package its entry names with no pin, its setting - // package-wait-minutes (H-A-165); DefaultPackageWait when 0. Only a registry seat's published heard (PackagePublished) - // gives a pin (H-A-192). + // package-wait-minutes (H-A-165); DefaultPackageWait when 0. The version is its publisher's at the key's origin + // (H-A-547 (3), H-A-551). PackageWait time.Duration // RecordBound bounds the record of a reservation's digests after a build ends, its setting record-bound-seconds (design 47 // H2 H-A-492): the builder's own bound, never the build's context; DefaultRecordBound when 0. RecordBound time.Duration - // PublishedVersions asks the Go package registry seat's verb versions for one package path, its versions newest first - // (H-A-194): read once before a build waits for that package. Nil reads none. - PublishedVersions func(ctx context.Context, path string) ([]string, 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 @@ -174,9 +171,6 @@ 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{} - // publishedPins are the newest version a registry seat said published, by package path and series (PackagePublished): - // a package with no pin waits for one (H-A-192). - publishedPins map[string]map[string]string // supersededSaid are the build keys refused as superseded (H-A-489), so each is said once. supersededSaid sync.Map } @@ -209,7 +203,6 @@ 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.publishedPins = map[string]map[string]string{} }) } @@ -1212,9 +1205,9 @@ func (b *Builder) runLane(ctx context.Context, l *lane, repository string, m mes if language == sdkstep.AskGo && job.Fetch != nil && len(m.Packages) > 0 { // The versions of the mesh packages the fingerprint was computed with (novox/hq ADR 0573 §2): the build module writes // them into the workspace, whatever the repository's go.mod names. A package the entry names with no pin waits for - // its publish and is built with the version that publish carries; past the bound the lane fails naming it, and no - // ask is put (H-A-192, H-A-165). - pinned, outcome, said := b.awaitPublished(ctx, module, m.Fp2, m.Manifest, m.Packages) + // its publish and is built with the version its publisher holds at the key's origin; past the bound the lane fails + // naming it, and no ask is put (H-A-521, H-A-547 (3), H-A-165). + pinned, outcome, said := b.awaitPublished(ctx, module, m.Fp2, m.Packages) if outcome != "" { l.err = errors.New(said) return diff --git a/modules/mesh-delivery-builder/internal/build/run_test.go b/modules/mesh-delivery-builder/internal/build/run_test.go index 49f14622f..bd8cfd4e2 100644 --- a/modules/mesh-delivery-builder/internal/build/run_test.go +++ b/modules/mesh-delivery-builder/internal/build/run_test.go @@ -441,14 +441,12 @@ func TestAnOlderFingerprintAskedAfterANewerEntryIsRefused(t *testing.T) { func TestATrunkFingerprintIsOneAskOnTheDeliveryStream(t *testing.T) { r := newRig(t, goBundle) r.asks.Answer = nil // no node has pulled it yet - // Issue 1585 (ADR 0573 §2): the ask carries the module's pins; a package the entry keeps with no version (none - // published, design 47 H-A-161) is pinned at the version the registry seat's published carries (H-A-192), never - // asked without it. - r.b.PackagePublished("git.novox.be/novox/mesh-contracts/go/seats/x", "v0.1.7") + // Issue 1585 (ADR 0573 §2): the ask carries the module's pins (a package with none is filled at the key's origin, the + // step tests' H-A-547 (3)). sdk := buildstep.PackageVersions{"git.novox.be/novox/mesh-sdk/go": "v0.1.20", "git.novox.be/novox/mesh-contracts/go/seats/x": "v0.1.7"} data, err := meshcatalogclient.EncodeModule(meshcatalog.ModulesByModuleValue{Repository: "novox/mesh-catalog", Commit: "7a74cee83f88e6585a5511c3cc53bdf0f5303d20", Fp2: fp1, Manifest: mdigest('a'), Path: "modules/m", Toolchains: CatalogToolchains(agreedToolchains), - Packages: map[string]string{"git.novox.be/novox/mesh-sdk/go": "v0.1.20", "git.novox.be/novox/mesh-contracts/go/seats/x": ""}}) + Packages: map[string]string{"git.novox.be/novox/mesh-sdk/go": "v0.1.20", "git.novox.be/novox/mesh-contracts/go/seats/x": "v0.1.7"}}) if err != nil { t.Fatal(err) } diff --git a/modules/mesh-delivery-builder/internal/build/step.go b/modules/mesh-delivery-builder/internal/build/step.go index 25e57df5f..c46c51573 100644 --- a/modules/mesh-delivery-builder/internal/build/step.go +++ b/modules/mesh-delivery-builder/internal/build/step.go @@ -123,8 +123,9 @@ func (b *Builder) buildEntry(ctx context.Context, pipeline string, e meshbuilds. 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 publish, waited for (H-A-192, H-A-165). - pinned, outcome, said := b.awaitPublished(ctx, entry.Module, entry.Fp, meshcatalogclient.BareManifestDigest(entry.ManifestDigest), entry.Packages) + // A package the entry names with no pin is built against its publisher's version at the key's origin, waited for + // (H-A-521, H-A-547 (3), H-A-165). + pinned, outcome, said := b.awaitPublished(ctx, entry.Module, entry.Fp, entry.Packages) if outcome != "" { return outcome, said } diff --git a/modules/mesh-delivery-builder/internal/build/step_test.go b/modules/mesh-delivery-builder/internal/build/step_test.go index ba470fd87..645aac4dc 100644 --- a/modules/mesh-delivery-builder/internal/build/step_test.go +++ b/modules/mesh-delivery-builder/internal/build/step_test.go @@ -186,20 +186,26 @@ func TestABuildStepPassesAReservingBuildOnlyWithItsReservedVersion(t *testing.T) t.Run("H-A-155: past the bound the ask fails naming its revision, and nothing is built", aStepAskPastItsRevisionBoundFails) t.Run("H-A-205: an ask of a revision below the picture's newest is read by number and built", anOlderKeptRevisionIsReadByNumber) 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) - t.Run("H-A-192: a lane waits for an unpinned package's publish and fails past its bound", aLaneWaitsForAnUnpinnedPackage) - t.Run("H-A-194: what the registry seat holds pins the package before any wait", theSeatsVersionsPinBeforeTheWait) + t.Run("H-A-547 (3): a package with no pin waits for its publisher's version at the key's origin and is built with it", aPackageWithNoPinWaitsForItsRelease) + t.Run("H-A-165: past its bound a package with no version at the origin fails the step", aPackageWithNoReleaseFailsPastItsBound) + t.Run("H-A-547 (3): a lane waits for an unpinned package's version at its origin and fails past its bound", aLaneWaitsForAnUnpinnedPackage) } -// unpinnedRig is a rig whose step entry m names the package x with no version. Its manifest names no `packages`, so it -// accepts x in the default series 0.1. waiting is told when a build starts its wait for a package's publish. -func unpinnedRig(t *testing.T) (*rig, meshcatalog.CatalogEntry, <-chan struct{}) { +// unpinnedRig is a rig whose step entry m names the package x with no version, x published by the artifact x of the module xm, +// its key's origin source revision 41 of main. waiting is told when a build starts its wait for a package's publish. +func unpinnedRig(t *testing.T) (*rig, *fakeReservations, meshcatalog.CatalogEntry, <-chan struct{}) { var held atomic.Int64 - r, _, entry := revisionRig(t, &held, make(chan struct{}, 1)) + 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{unpinnedX: "", "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 "xm", "x", pkg == unpinnedX, nil + } + r.b.init() + r.b.mu.Lock() + r.b.origin[r.key] = Origin{Branch: "main", Source: 41, First: true} + r.b.mu.Unlock() waiting := make(chan struct{}, 1) r.b.Logf = func(format string, args ...any) { t.Logf(format, args...) @@ -210,7 +216,7 @@ func unpinnedRig(t *testing.T) (*rig, meshcatalog.CatalogEntry, <-chan struct{}) } } } - return r, entry, waiting + return r, res, entry, waiting } const unpinnedX = "git.novox.be/novox/mesh-contracts/go/seats/x" @@ -227,10 +233,10 @@ func awaitWaiting[T any](t *testing.T, waiting <-chan struct{}, ended <-chan T) } } -// H-A-192: the pin is the version the registry seat's published carries for the path in the accepted series; a publish in -// another series and the module's release number pin nothing. +// H-A-547 (3), H-A-551: the pin is the version the module publishing the package holds at the source revision of the key's +// origin; a version of another source revision and the newest a registry seat said published pin nothing. func aPackageWithNoPinWaitsForItsRelease(t *testing.T) { - r, _, waiting := unpinnedRig(t) + r, res, _, waiting := unpinnedRig(t) // Each entry's answer from a build is one journal line, built now or existing, beside the ask's answer (H-A-320 (1), H-A-333). var mu sync.Mutex var answers []string @@ -243,8 +249,8 @@ func aPackageWithNoPinWaitsForItsRelease(t *testing.T) { mu.Unlock() } } - r.b.PackagePublished(unpinnedX, "v0.2.9") // another series than the 0.1 the module accepts: no pin - r.b.PackagePublished(unpinnedX, "0.1.4-feat-x.2") // a group's tagged version: no pin + res.publishAt("xm", "x", "main", 40, "0.1.2") // another source revision than the key's origin: no pin + r.b.PackagePublished(unpinnedX, "v0.1.9") // the newest heard: no pin (H-A-547 (3)) ended := make(chan meshbuilds.StepEnded, 1) go func() { ended <- r.b.BuildStep(t.Context(), "a1", meshbuilds.StepRun{Pipeline: "main.9.m", Step: meshbuilds.StepRunStepBuild, Branch: "main", Source: 41, @@ -254,6 +260,7 @@ func aPackageWithNoPinWaitsForItsRelease(t *testing.T) { if _, ok := r.st.vals[r.key]; ok { t.Fatal("the build was made before the package was published") } + res.publishAt("xm", "x", "main", 41, "0.1.3") r.b.PackagePublished(unpinnedX, "v0.1.3") var end meshbuilds.StepEnded select { @@ -271,8 +278,8 @@ func aPackageWithNoPinWaitsForItsRelease(t *testing.T) { if !reflect.DeepEqual(end.Packages, map[string]string{unpinnedX: "v0.1.3", "git.novox.be/novox/mesh-sdk/go": "v0.1.20"}) { t.Errorf("the end reports the pins %v; want the ones it was built with", end.Packages) } - // A done key reports the pins it keeps it was built with, never the newer one a publish gives now (H-A-207). - r.b.PackagePublished(unpinnedX, "v0.1.5") + // A done key reports the pins it keeps it was built with, never a newer one published now (H-A-207). + res.publishAt("xm", "x", "main", 41, "0.1.5") again := r.b.BuildStep(t.Context(), "a1", meshbuilds.StepRun{Pipeline: "main.9.m", Step: meshbuilds.StepRunStepBuild, Branch: "main", Source: 41, Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}) if again.Outcome != meshbuilds.StepEndedOutcomePassed || again.Packages[unpinnedX] != "v0.1.3" { @@ -302,59 +309,27 @@ func aPackageWithNoPinWaitsForItsRelease(t *testing.T) { } func aPackageWithNoReleaseFailsPastItsBound(t *testing.T) { - r, _, _ := unpinnedRig(t) + r, res, _, _ := unpinnedRig(t) r.b.PackageWait = time.Nanosecond end := r.b.BuildStep(t.Context(), "a1", 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 "+unpinnedX+" has no released version in the series 0.1") { + !strings.Contains(end.Said, "package "+unpinnedX+" has no published version: xm publishes no version of it at source revision 41 of main") { t.Errorf("end = %+v, built %v; want failed naming the package, nothing built", end, built) } - // A publish heard before gives the pin at once. - r.b.PackagePublished(unpinnedX, "v0.1.2") + // A version its publisher holds at the origin gives the pin at once. + res.publishAt("xm", "x", "main", 41, "0.1.2") if end := r.b.BuildStep(t.Context(), "a1", 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 publish heard did not pin the package: %+v", end) + Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}); strings.Contains(end.Said, "no published version") { + t.Errorf("a version at the origin did not pin the package: %+v", end) } } -// H-A-194: the build reads the registry seat's versions of the path once before it waits: a version in the accepted series -// pins the package with no event heard; one only in another series leaves the build waiting for a publish. -func theSeatsVersionsPinBeforeTheWait(t *testing.T) { - r, _, _ := unpinnedRig(t) - r.b.PackageWait = time.Nanosecond // no wait: the seat's answer alone gives the pin - var read []string - r.b.PublishedVersions = func(_ context.Context, path string) ([]string, error) { - read = append(read, path) - return []string{"0.2.1", "0.1.9", "0.1.8"}, nil // newest first - } - end := r.b.BuildStep(t.Context(), "a1", meshbuilds.StepRun{Pipeline: "main.9.m", Step: meshbuilds.StepRunStepBuild, Branch: "main", Source: 41, - Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}) - r.b.Wait() - asks := r.asks.Asks() - if end.Outcome != meshbuilds.StepEndedOutcomePassed || len(asks) == 0 || asks[0].Packages[unpinnedX] != "v0.1.9" || !slices.Equal(read, []string{unpinnedX}) { - t.Errorf("end = %+v, asks %+v, read %v; want passed, built with x at v0.1.9 read once from the seat", end, asks, read) - } - r2, _, waiting := unpinnedRig(t) - r2.b.PublishedVersions = func(context.Context, string) ([]string, error) { return []string{"0.2.1"}, nil } - ended := make(chan meshbuilds.StepEnded, 1) - go func() { - ended <- r2.b.BuildStep(t.Context(), "a1", meshbuilds.StepRun{Pipeline: "main.9.m", Step: meshbuilds.StepRunStepBuild, Branch: "main", Source: 41, - Entries: []meshbuilds.BuildEntry{{Module: "m", Fingerprint: fp1}}}) - }() - awaitWaiting(t, waiting, ended) - r2.b.PackagePublished(unpinnedX, "0.1.10") - if end := <-ended; end.Outcome != meshbuilds.StepEndedOutcomePassed { - t.Errorf("end = %+v, want passed once x was published in its series", end) - } - r2.b.Wait() -} - -// H-A-192, H-A-165: the lane of an entry taken in waits the same way, never asking a build without the pin, and fails the +// H-A-547 (3), H-A-165: the lane of an entry taken in waits the same way, never asking a build without the pin, and fails the // key past the bound naming the package. func aLaneWaitsForAnUnpinnedPackage(t *testing.T) { - r, entry, waiting := unpinnedRig(t) + r, res, entry, waiting := unpinnedRig(t) done := make(chan struct{}) go func() { r.b.Entry(t.Context(), Origin{Branch: "main", Source: 41, First: true}, entry) @@ -365,6 +340,7 @@ func aLaneWaitsForAnUnpinnedPackage(t *testing.T) { if asks := r.asks.Asks(); len(asks) != 0 { t.Fatalf("a build was asked without the package's pin: %+v", asks) } + res.publishAt("xm", "x", "main", 41, "0.1.5") r.b.PackagePublished(unpinnedX, "0.1.5") select { case <-done: @@ -375,11 +351,11 @@ func aLaneWaitsForAnUnpinnedPackage(t *testing.T) { t.Errorf("asks %+v; want one asked with x at v0.1.5", asks) } // Past its bound the key fails naming the package, and nothing is asked. - r2, entry2, _ := unpinnedRig(t) + r2, _, entry2, _ := unpinnedRig(t) r2.b.PackageWait = time.Nanosecond r2.b.Entry(t.Context(), Origin{Branch: "main", Source: 41, First: true}, entry2) r2.b.Wait() - if asks := r2.asks.Asks(); len(asks) != 0 || !strings.Contains(string(r2.st.vals[r2.key]), "package "+unpinnedX+" has no released version") { + if asks := r2.asks.Asks(); len(asks) != 0 || !strings.Contains(string(r2.st.vals[r2.key]), "package "+unpinnedX+" has no published version") { t.Errorf("asks %+v, key %s; want none asked and the key failed naming the package", asks, r2.st.vals[r2.key]) } } diff --git a/modules/mesh-delivery-builder/internal/versions/reserve.go b/modules/mesh-delivery-builder/internal/versions/reserve.go index dd7a6f645..29fcbeb0b 100644 --- a/modules/mesh-delivery-builder/internal/versions/reserve.go +++ b/modules/mesh-delivery-builder/internal/versions/reserve.go @@ -427,6 +427,10 @@ type ReleasedBuild struct { // Withdrawn are the fingerprints whose numbers the released version took over, by the number withdrawn (H-A-226), so a // takeover still answers once the release deleted the series key (H-A-508); absent for a release that took none over. Withdrawn map[string]string `json:"withdrawn,omitempty"` + // Source is the source revision of the catalog revision the released build came from, as its reservation kept it (design 47 + // H2 H-A-551): an importer's build of that source revision is filled from it once the reservation is gone; 0 for a release + // kept before. + Source int64 `json:"source,omitempty"` } // ReleasedFor says whether the builder saw a version released for the build of fingerprint among the module's last releases. @@ -496,8 +500,49 @@ func (r *Reserver) PublishedOf(ctx context.Context, module, artifact, fingerprin return meshbuilds.PublishedAnswer{Published: false}, nil } +// VersionAt answers the version artifact of module is published at from the source revision of branch (design 47 H2 H-A-547 +// (3), H-A-551): the standing reservation of that branch and source revision whose artifact a registry seat said published, its +// number not withdrawn, first; else the module's release recorded at that source revision, the newest such. A reservation of +// that origin not yet published answers none, so the importer's build waits for the publish (H-A-521). +func (r *Reserver) VersionAt(ctx context.Context, module, artifact, branch string, source int64) (string, bool, error) { + if module == "" || artifact == "" || source <= 0 { + return "", false, nil + } + ref := module + ":" + artifact + keys, err := r.Store.KeysOf(ctx, NameFilter(module)) + if err != nil { + return "", false, fmt.Errorf("the reservations of %s cannot be listed: %w", module, err) + } + sort.Strings(keys) + for _, k := range keys { + cur, _, found, err := r.get(ctx, k) + if err != nil { + return "", false, err + } + if found && cur.Module == module && cur.Branch == branch && cur.Source == source && cur.Published[ref] && !hasKey(cur.Withdrawn, cur.Version) { + return cur.Version, true, nil + } + } + if r.Releases == nil { + return "", false, nil + } + raw, _, found, err := r.Releases.Get(ctx, ReleasedKey(module)) + if err != nil { + return "", false, fmt.Errorf("the releases of %s cannot be read: %w", module, err) + } + var rec ReleasedRecord + if found && json.Unmarshal(raw, &rec) == nil { + for i := len(rec.Kept) - 1; i >= 0; i-- { + if rec.Kept[i].Source == source { + return rec.Kept[i].Version, true, nil + } + } + } + return "", false, 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, digests, withdrawn map[string]string) error { +func (r *Reserver) keepRelease(ctx context.Context, module, fingerprint, version string, digests, withdrawn map[string]string, source int64) error { n := r.ReleasedKept if n < 1 { n = DefaultReleasedKept @@ -517,7 +562,7 @@ func (r *Reserver) keepRelease(ctx context.Context, module, fingerprint, version kept = append(kept, k) } } - kept = append(kept, ReleasedBuild{Fingerprint: fingerprint, Version: version, Digests: digests, Withdrawn: maps.Clone(withdrawn)}) + kept = append(kept, ReleasedBuild{Fingerprint: fingerprint, Version: version, Digests: digests, Withdrawn: maps.Clone(withdrawn), Source: source}) if len(kept) > n { kept = kept[len(kept)-n:] } @@ -567,7 +612,7 @@ func (r *Reserver) Released(ctx context.Context, module, version string) ([]stri continue } if cur.Version == version && cur.Fingerprint != "" && r.Releases != nil { - if err := r.keepRelease(ctx, module, cur.Fingerprint, version, cur.Artifacts, cur.Withdrawn); err != nil { + if err := r.keepRelease(ctx, module, cur.Fingerprint, version, cur.Artifacts, cur.Withdrawn, cur.Source); err != nil { return gone, fmt.Errorf("the release of %s %s cannot be recorded: %w", module, version, err) } } diff --git a/modules/mesh-delivery-builder/internal/versions/reserve_test.go b/modules/mesh-delivery-builder/internal/versions/reserve_test.go index 51a093ee0..165f2ccaa 100644 --- a/modules/mesh-delivery-builder/internal/versions/reserve_test.go +++ b/modules/mesh-delivery-builder/internal/versions/reserve_test.go @@ -217,6 +217,40 @@ func TestAReleaseIsRecordedByFingerprintBeforeItsReservationGoes(t *testing.T) { if all, _ := r.All(ctx); len(all) != 0 { t.Errorf("reservations left: %v", all) } + // Design 47 H2 H-A-547 (3), H-A-551: an importer's version at the source revision of its key's origin is the standing + // reservation of that origin once its artifact is published, then the release recorded at that source revision. + w, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: "src2:cc", Branch: "main", Revision: 41}) + if err != nil { + t.Fatal(err) + } + at := func(branch string, source int64) string { + v, found, err := r.VersionAt(ctx, "m", "a", branch, source) + if err != nil || found != (v != "") { + t.Fatalf("VersionAt %s.%d: %q %v %v", branch, source, v, found, err) + } + return v + } + if v := at("main", 41); v != "" { + t.Errorf("a reservation not yet published answers %q, want none", v) + } + if _, err := r.Built(ctx, "m", w.Version, "m:a", "sha256:cc", nil); err != nil { + t.Fatal(err) + } + if _, _, err := r.Published(ctx, "m/a", w.Version, "sha256:cc"); err != nil { + t.Fatal(err) + } + if v := at("main", 41); v != w.Version { + t.Errorf("the published reservation at main.41 answers %q, want %s", v, w.Version) + } + if v, v2 := at("main", 40), at("feat", 41); v != "" || v2 != "" { + t.Errorf("another source revision or branch answers %q %q, want none", v, v2) + } + if _, err := r.Released(ctx, "m", w.Version); err != nil { + t.Fatal(err) + } + if v := at("main", 41); v != w.Version { + t.Errorf("once released, main.41 answers %q, want %s from the release's record", v, w.Version) + } // One key per module holds the last releases, the oldest dropped (A-456b). for _, fp := range []string{"src2:b1", "src2:b2"} { n, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: fp})