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 f386d5672..84a79675b 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go @@ -195,16 +195,13 @@ func (rv *reservations) Reserve(ctx context.Context, module, series string, arti return rv.r.Reserve(ctx, module, series, artifacts, src) } -// Built records the digests; once every artifact has one, the publish's bound starts (a key of the state bounds with its -// own time to live): its end with the reservation standing is version-unreleased. +// Built records the digests, in one compare-and-set of the reservation (issue 1984 note 8, 1984/AC4), and then takes the +// reservation's state in, whatever ended its build: this record or one before it whose end was lost, as a look hands a recorded +// reservation here with its digests (1984/AC5). A built reservation has its publish's bound armed, its publish item asked (design 47 +// G A-457a; it stands until its release, A-456b) and the published kept on it taken in. func (rv *reservations) Built(ctx context.Context, module, version string, digests map[string]string, deps map[string][]meshbuilds.Dependency) error { - for ref, d := range digests { - if _, err := rv.r.Built(ctx, module, version, ref, d, deps[ref]); err != nil { - return err - } - } - cur, found, err := rv.r.Of(ctx, module, version) - if err != nil || !found || cur.BuiltAt.IsZero() { + cur, standing, err := rv.r.Built(ctx, module, version, digests, deps) + if err != nil || !standing || cur.BuiltAt.IsZero() { return err } rv.arm(ctx, cur) @@ -213,13 +210,22 @@ func (rv *reservations) Built(ctx context.Context, module, version string, diges rv.logf("%v", err) } } - // A published heard before the digests were recorded marks its artifact now, said as one heard after (design 47 G A-415). - marked, unmatched, err := rv.r.Kept(ctx, module, version) + rv.takeKept(ctx, cur) + return nil +} + +// takeKept takes the published kept on a built reservation in (design 47 G A-415): each whose digest is an artifact's marks it and +// is said as package-published, as one heard after; one whose digest no artifact carries raises version-unreleased at once (A-490j). +func (rv *reservations) takeKept(ctx context.Context, cur versions.ReservedVersion) { + if len(cur.Kept) == 0 { + return + } + marked, unmatched, err := rv.r.Kept(ctx, cur.Module, cur.Version) if err != nil { - rv.logf("the published kept on %s %s are not taken in: %v", module, version, err) + rv.logf("the published kept on %s %s are not taken in: %v", cur.Module, cur.Version, err) } for _, h := range unmatched { - rv.logf("a published of %s@%s kept on %s %s marks no artifact: its digest %s is none of the built digests %v", h.Name, h.Version, module, version, h.Digest, cur.Artifacts) + rv.logf("a published of %s@%s kept on %s %s marks no artifact: its digest %s is none of the built digests %v", h.Name, h.Version, cur.Module, cur.Version, h.Digest, cur.Artifacts) // A build whose recorded digest differs from what the holder holds at its version fails loudly at once: the version is taken // and no artifact is marked for it, so it is never released (design 47 G A-490j). rv.unreleased(ctx, cur, fmt.Sprintf("the holder holds %s@%s under digest %s, which no artifact of the build carries (%v): the version is taken and is never released", @@ -228,15 +234,35 @@ func (rv *reservations) Built(ctx context.Context, module, version string, diges for _, m := range marked { rv.sayPublished(ctx, m, m.Reserved.Artifacts[m.Artifact]) } - return nil } +// now is the reservations' clock, the Reserver's, from which a build's end (BuiltAt) is written. +func (rv *reservations) now() time.Time { + if rv.r.Now != nil { + return rv.r.Now().UTC() + } + return time.Now().UTC() +} + +// arm arms a built reservation's publish's bound, a key of the state bounds with its own time to live, counted from its build's +// end (BuiltAt): its end with the reservation standing is version-unreleased. One armed already stands; at a look or the start +// after the bound passed, a reservation left with an artifact unpublished is version-unreleased at once and its bound is not armed +// again (issue 1984 note 8, 1984/AC5); one with every artifact published waits for its version-released. func (rv *reservations) arm(ctx context.Context, v versions.ReservedVersion) { + ends := v.BuiltAt.Add(rv.bound) + left := ends.Sub(rv.now()) + if left <= 0 { + if len(v.Missing()) > 0 { + rv.unreleased(ctx, v, fmt.Sprintf("nothing within %s of its build", rv.bound)) + } + return + } kv := rv.bounds() var err error if kv == nil { err = fmt.Errorf("%s is not on the bus yet", versions.BoundsState) - } else if _, err = kv.Create(ctx, versions.ReservedBoundKey(v.Module, v.Series), versions.EncodeBound("the release of "+v.Module+" "+v.Version, time.Now().Add(rv.bound)), jetstream.KeyTTL(rv.bound)); errors.Is(err, jetstream.ErrKeyExists) { + } else if _, err = kv.Create(ctx, versions.ReservedBoundKey(v.Module, v.Series), versions.EncodeBound("the release of "+v.Module+" "+v.Version, ends), + jetstream.KeyTTL(max(time.Second, left.Round(time.Second)))); errors.Is(err, jetstream.ErrKeyExists) { err = nil } if err != nil { @@ -348,7 +374,8 @@ func (rv *reservations) release(ctx context.Context, ev meshcatalog.VersionRelea // (design 47 H2 H-A-494). // A module whose versions cannot be read, and a release that is not taken, are said and left to its next version-released. // Before that, a standing reservation with no digests recorded whose build key is done has them recorded from the done key -// (design 47 H2 H-A-492), so it is held against the released versions too. +// (design 47 H2 H-A-492), so it is held against the released versions too. Every built reservation not released is taken in as +// its record's end would have (issue 1984 note 8, 1984/AC5): its bound armed, the published kept on it taken in and said. func (rv *reservations) releasedAtStart(ctx context.Context) { all, err := rv.r.All(ctx) if err != nil { @@ -374,6 +401,9 @@ func (rv *reservations) releasedAtStart(ctx context.Context) { read[v.Module] = released } if !slices.Contains(released, v.Version) { + // Taken in as its record's end would have (1984/AC5); its publish item is asked by AskAllDone, next. + rv.arm(ctx, v) + rv.takeKept(ctx, v) continue } if err := rv.release(ctx, meshcatalog.VersionReleasedEvent{Module: v.Module, Version: v.Version}); err != nil { @@ -384,8 +414,8 @@ func (rv *reservations) releasedAtStart(ctx context.Context) { } } -// recordedAtStart records the digests of every standing reservation that has none recorded and whose build key is done (design 47 -// H2 H-A-492): a record its build's end lost. It answers whether one was recorded; one that cannot be is said. +// recordedAtStart records the digests of every standing reservation that has not all recorded and whose build key is done (design +// 47 H2 H-A-492): a record its build's end lost, whole or in part. It answers whether one was recorded; one that cannot be is said. func (rv *reservations) recordedAtStart(ctx context.Context, all []versions.ReservedVersion) bool { if rv.recordAgain == nil { return false diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve_test.go index adde32fc0..571501507 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve_test.go @@ -64,14 +64,18 @@ func (m *memReserve) Delete(_ context.Context, key string) error { return nil } -// boundsKV keeps the keys created and deleted in the state bounds. +// boundsKV keeps the keys created and deleted in the state bounds, and with vals each created key's value. type boundsKV struct { jetstream.KeyValue keys map[string]bool + vals map[string][]byte } -func (b *boundsKV) Create(_ context.Context, key string, _ []byte, _ ...jetstream.KVCreateOpt) (uint64, error) { +func (b *boundsKV) Create(_ context.Context, key string, v []byte, _ ...jetstream.KVCreateOpt) (uint64, error) { b.keys[key] = true + if b.vals != nil { + b.vals[key] = v + } return 1, nil } func (b *boundsKV) Delete(_ context.Context, key string, _ ...jetstream.KVDeleteOpt) error { @@ -231,7 +235,7 @@ func TestAReleasedVersionWritesNoTag(t *testing.T) { var recordedAt []string rv.recordAgain = func(ctx context.Context, module, fp, when string) (string, error) { recordedAt = append(recordedAt, module+" "+fp+" "+when) - _, err := rv.r.Built(ctx, module, "0.1.0", "rec:bin", "sha256:bb", nil) // its done key's digest + _, _, err := rv.r.Built(ctx, module, "0.1.0", map[string]string{"rec:bin": "sha256:bb"}, nil) // its done key's digest return "0.1.0", err } catalog = []meshcatalog.VersionEntry{{Module: "rec", Version: "0.1.0"}} @@ -250,7 +254,7 @@ func TestAReleasedVersionWritesNoTag(t *testing.T) { if all, _ := rv.r.All(ctx); len(all) != 1 { t.Fatalf("at the start with no build done: reservations %v; want the reservation standing", all) } - if _, err := rv.r.Built(ctx, "pkg", "0.1.0", "pkg:lib", "sha256:aa", nil); err != nil { + if _, _, err := rv.r.Built(ctx, "pkg", "0.1.0", map[string]string{"pkg:lib": "sha256:aa"}, nil); err != nil { t.Fatal(err) } rv.releasedAtStart(ctx) // the catalog holds none released: it stands @@ -346,6 +350,185 @@ func TestAPublishedHeardBeforeTheDigestsIsSaidWhenTheBuildRecordsThem(t *testing } } +// TestABuildsDigestsAreRecordedInOneWrite: issue 1984 note 8, 1984/AC4 — recording a build's digests is one compare-and-set of +// its reservation and one reindex for all its artifacts (39 here, as mesh-delivery-contracts 0.1.108 had); a record tried again +// after one that timed out writes only what that one left (an artifact recorded already is skipped), and nothing when every +// digest stands recorded. +func TestABuildsDigestsAreRecordedInOneWrite(t *testing.T) { + ctx := context.Background() + store := &countedReserve{memReserve: &memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}}, writes: map[string]int{}} + none := func(string, any) (json.RawMessage, error) { return json.Marshal(meshcatalog.VersionsAnswer{}) } + rv := newReservations(store, none, func() jetstream.KeyValue { return &boundsKV{keys: map[string]bool{}} }, + &findings{module: builds.BuilderModule, w: &memFindings{vals: map[string][]byte{}}, logf: t.Logf, now: time.Now}, t.Logf) + var asked []string + rv.built = func(_ context.Context, m, v string) error { asked = append(asked, m+" "+v); return nil } + refsOf := func(module string) ([]string, map[string]string) { + var refs []string + digests := map[string]string{} + for i := range 39 { + ref := fmt.Sprintf("%s:a%02d", module, i) + refs, digests[ref] = append(refs, ref), fmt.Sprintf("sha256:%s%02d", module, i) + } + return refs, digests + } + built := func(module string) versions.ReservedVersion { + t.Helper() + v, found, err := rv.r.Of(ctx, module, "") + if err != nil || !found { + t.Fatalf("%s: no reservation (%v)", module, err) + } + return v + } + + refs, digests := refsOf("c") + res, err := rv.Reserve(ctx, "c", "0.1", refs, versions.Source{Fingerprint: "fpC"}) + if err != nil { + t.Fatal(err) + } + key := versions.ReservedKey("c", "0.1") + clear(store.writes) + store.indexReads = 0 + if err := rv.Built(ctx, "c", res.Version, digests, nil); err != nil { + t.Fatal(err) + } + // One reindex reads each index key the built reservation stands under (its version and its 39 digests) and the one it leaves + // (its version's unbuilt): 41 reads, never one reindex per artifact. + if w, r := store.writes[key], store.indexReads; w != 1 || r > 41 { + t.Errorf("39 digests recorded with %d writes of %s and %d reads of the index, want one write and one reindex (at most 41 reads) (1984/AC4)", w, key, r) + } + if v := built("c"); v.BuiltAt.IsZero() || len(v.Missing()) != 39 || !slices.Equal(asked, []string{"c " + res.Version}) { + t.Errorf("after the record: built at %v, %d unpublished, publish items asked %v; want the build ended, 39 awaited and its item asked once", v.BuiltAt, len(v.Missing()), asked) + } + + // A record that timed out left 30 of 39 digests written and the build not ended (the form the record of one artifact at a + // time wrote): the record tried again writes the reservation once, skipping the 30. + refs, digests = refsOf("d") + res, err = rv.Reserve(ctx, "d", "0.1", refs, versions.Source{Fingerprint: "fpD"}) + if err != nil { + t.Fatal(err) + } + key = versions.ReservedKey("d", "0.1") + left := res + left.Artifacts = map[string]string{} + for i, ref := range refs { + left.Artifacts[ref] = map[bool]string{true: digests[ref], false: ""}[i < 30] + } + store.vals[key], _ = json.Marshal(left) + clear(store.writes) + if err := rv.Built(ctx, "d", res.Version, digests, nil); err != nil { + t.Fatal(err) + } + if w := store.writes[key]; w != 1 || built("d").BuiltAt.IsZero() { + t.Errorf("the record tried again wrote %s %d times (built at %v), want once, the 30 recorded skipped (1984/AC4)", key, w, built("d").BuiltAt) + } + // Tried again once more, every digest recorded: nothing is written. + clear(store.writes) + if err := rv.Built(ctx, "d", res.Version, digests, nil); err != nil { + t.Fatal(err) + } + if w := store.writes[key]; w != 0 { + t.Errorf("a record of digests all recorded wrote %s %d times, want none (1984/AC4)", key, w) + } +} + +// TestABuiltReservationIsTakenInFromItsState: issue 1984 note 8, 1984/AC5 — what follows a build's record follows the +// reservation's state, not whether the record before ended: at a look (the builder's RecordAgain hands a recorded reservation to +// Built with its digests) and at the start, a built reservation with published kept on it takes them in, says package-published +// and arms its bound, counted from its build's end; one left unmarked past its bound raises version-unreleased, its bound not +// armed again. +func TestABuiltReservationIsTakenInFromItsState(t *testing.T) { + ctx := context.Background() + store := &memReserve{vals: map[string][]byte{}, revs: map[string]uint64{}} + none := func(string, any) (json.RawMessage, error) { return json.Marshal(meshcatalog.VersionsAnswer{}) } + fs := &memFindings{vals: map[string][]byte{}} + bounds := &boundsKV{keys: map[string]bool{}, vals: map[string][]byte{}} + rv := newReservations(store, none, func() jetstream.KeyValue { return bounds }, + &findings{module: builds.BuilderModule, w: fs, logf: t.Logf, now: time.Now}, t.Logf) + var said []string + rv.say = func(_ context.Context, p meshbuilds.PackagePublished) error { + said = append(said, p.Artifact+"@"+p.Version+" "+p.Digest) + return nil + } + var asked []string + rv.built = func(_ context.Context, m, v string) error { asked = append(asked, m+" "+v); return nil } + now := time.Now().UTC() + // recordedLost is module's reservation as a record whose end was lost leaves it: a published heard before the digests kept on + // it, every digest recorded and the build ended at builtAt, and nothing after (no bound, the kept published not taken in). + recordedLost := func(module string, builtAt time.Time) versions.ReservedVersion { + t.Helper() + res, err := rv.Reserve(ctx, module, "0.1", []string{module + ":a", module + ":b"}, versions.Source{Fingerprint: "fp" + module, Branch: "main", Revision: 41}) + if err != nil { + t.Fatal(err) + } + rv.published(ctx, versions.Outcome{Name: module + "-a", Version: res.Version, Digest: "sha256:" + module + "a"}, nil) + v, _, _ := rv.r.Of(ctx, module, res.Version) + if len(v.Kept) != 1 { + t.Fatalf("%s: kept %v, want the published heard before the digests kept", module, v.Kept) + } + v.Artifacts = map[string]string{module + ":a": "sha256:" + module + "a", module + ":b": "sha256:" + module + "b"} + v.BuiltAt = builtAt + key := versions.ReservedKey(module, "0.1") + store.vals[key], _ = json.Marshal(v) + return v + } + unreleased := func(module string) string { + return string(fs.vals[meshconditionsclient.Key(meshconditionsclient.ScopeModule, module, versions.VersionUnreleased)]) + } + boundEnds := func(module string) time.Time { + var b versions.Bound + _ = json.Unmarshal(bounds.vals[versions.ReservedBoundKey(module, "0.1")], &b) + return b.Ends + } + + t.Run("at a look", func(t *testing.T) { + v := recordedLost("look", now.Add(-5*time.Minute)) + said, asked = nil, nil + if err := rv.Built(ctx, "look", v.Version, v.Artifacts, nil); err != nil { + t.Fatal(err) + } + // Its publish item is asked as the build's end asks it: it stands until its release (design 47 G A-456b). + if !slices.Equal(said, []string{"look:a@" + v.Version + " sha256:looka"}) || !slices.Equal(asked, []string{"look " + v.Version}) { + t.Errorf("at a look: said %v, publish items asked %v; want look:a's package-published said and its item asked", said, asked) + } + if ends := boundEnds("look"); !ends.Equal(v.BuiltAt.Add(versions.UnreleasedBound)) { + t.Errorf("the bound armed at a look ends %v, want %v: counted from the build's end", ends, v.BuiltAt.Add(versions.UnreleasedBound)) + } + if cur, _, _ := rv.r.Of(ctx, "look", v.Version); len(cur.Kept) != 0 || !cur.Published["look:a"] { + t.Errorf("after the look: kept %v, published %v; want the kept published taken in", cur.Kept, cur.Published) + } + }) + t.Run("at the start", func(t *testing.T) { + v := recordedLost("start", now.Add(-time.Minute)) + said = nil + rv.releasedAtStart(ctx) + if !slices.Contains(said, "start:a@"+v.Version+" sha256:starta") || !bounds.keys[versions.ReservedBoundKey("start", "0.1")] { + t.Errorf("at the start: said %v, bounds %v; want start:a's package-published said and its bound armed", said, bounds.keys) + } + if unreleased("start") != "" { + t.Errorf("a built reservation within its bound raised %s", unreleased("start")) + } + }) + t.Run("past its bound", func(t *testing.T) { + for _, when := range []string{"a look", "the start"} { + module := map[string]string{"a look": "pastlook", "the start": "paststart"}[when] + v := recordedLost(module, now.Add(-versions.UnreleasedBound-time.Minute)) + if when == "a look" { + if err := rv.Built(ctx, module, v.Version, v.Artifacts, nil); err != nil { + t.Fatal(err) + } + } else { + rv.releasedAtStart(ctx) + } + if got := unreleased(module); !strings.Contains(got, module+":b unpublished") || !strings.Contains(got, v.Version) { + t.Errorf("at %s, a built reservation left unmarked past its bound raised %q, want version-unreleased naming %s:b of %s", when, got, module, v.Version) + } + if bounds.keys[versions.ReservedBoundKey(module, "0.1")] { + t.Errorf("at %s, the bound of a reservation past it was armed again", when) + } + } + }) +} + // TestABuildTakingOverAnOlderReservationDropsItsBoundAndSaysSo: design 47 G A-455 — a build that reserves while the series is // reserved by an older build never released drops the older one's bound and finding and says so in the builder's journal; and // H-A-225 — the older build is withdrawn, so nothing publishes under the dropped number, while a re-run of the newer keeps it. @@ -557,18 +740,34 @@ func TestARegistrysRecordEndsOrMovesAPublish(t *testing.T) { } } -// countedReserve counts the reads of reservation keys (two tokens). +// countedReserve counts the reads of reservation keys (two tokens) and of the index beside them, and with writes the writes of +// each key (issue 1984 note 8). type countedReserve struct { *memReserve - reads int + reads, indexReads int + writes map[string]int } func (c *countedReserve) Get(ctx context.Context, key string) ([]byte, uint64, bool, error) { if !strings.HasPrefix(key, "index.") { c.reads++ + } else { + c.indexReads++ } return c.memReserve.Get(ctx, key) } +func (c *countedReserve) Create(ctx context.Context, key string, v []byte) (uint64, error) { + if c.writes != nil { + c.writes[key]++ + } + return c.memReserve.Create(ctx, key, v) +} +func (c *countedReserve) Update(ctx context.Context, key string, v []byte, rev uint64) (uint64, error) { + if c.writes != nil { + c.writes[key]++ + } + return c.memReserve.Update(ctx, key, v, rev) +} // TestAPublishedThatMarksNothingIsSaidFromWhatItRead: design 47 H H-A-280 — a published whose digest no reservation holds, at a // version many reservations share, reads only the unbuilt ones there, each once; its journal line is made from those reads with diff --git a/modules/mesh-delivery-builder/internal/build/numbers.go b/modules/mesh-delivery-builder/internal/build/numbers.go index f19915d91..81ab181ab 100644 --- a/modules/mesh-delivery-builder/internal/build/numbers.go +++ b/modules/mesh-delivery-builder/internal/build/numbers.go @@ -61,9 +61,11 @@ func (b *Builder) recordContext(ctx context.Context) (context.Context, context.C return context.WithTimeout(context.WithoutCancel(ctx), bound) } -// RecordAgain records the digests of fp's done build under its standing reservation when its end did not record them (design 47 -// H2 H-A-492): at the builder's look of the key and at its start (when says which), said each time. It answers the version -// recorded, "" when nothing was to be recorded: the key is not done, no reservation stands for fp, or it is recorded already. +// RecordAgain records the digests of fp's done build under its standing reservation when its end did not record them all (design +// 47 H2 H-A-492): at the builder's look of the key and at its start (when says which), said each time. A reservation whose build +// ended recorded is handed to Built all the same, with the digests it holds, so its holder takes its state in (its bound, the +// published kept on it) whether or not the record before ended (issue 1984 note 8, 1984/AC5). It answers the version recorded, "" +// when nothing was to be recorded: the key is not done, no reservation stands for fp, or it is recorded already. func (b *Builder) RecordAgain(ctx context.Context, module, fp, when string) (string, error) { b.init() if b.Reserved == nil { @@ -77,7 +79,15 @@ func (b *Builder) RecordAgain(ctx context.Context, module, fp, when string) (str if err != nil { return "", fmt.Errorf("%s at %s: whether its done build's digests are recorded cannot be read %s: %w", module, fp, when, err) } - if !ok || !res.BuiltAt.IsZero() || len(res.Artifacts) == 0 || versions.NotStanding(res, true) != "" { + if !ok || len(res.Artifacts) == 0 || versions.NotStanding(res, true) != "" { + return "", nil + } + rec, stop := b.recordContext(ctx) + defer stop() + if !res.BuiltAt.IsZero() { + if err := b.Reserved.Built(rec, module, res.Version, res.Artifacts, nil); err != nil { + return "", fmt.Errorf("%s at %s: the recorded build of %s is not taken in %s: %w", module, fp, res.Version, when, err) + } return "", nil } refs := map[string]string{} @@ -89,8 +99,6 @@ func (b *Builder) RecordAgain(ctx context.Context, module, fp, when string) (str } refs[ref] = d } - rec, stop := b.recordContext(ctx) - defer stop() if err := b.Reserved.Built(rec, module, res.Version, refs, nil); err != nil { return "", fmt.Errorf("%s at %s: the digests of %s are not recorded %s either: %w", module, fp, res.Version, when, err) } diff --git a/modules/mesh-delivery-builder/internal/build/reserve_test.go b/modules/mesh-delivery-builder/internal/build/reserve_test.go index 87eaccaef..9492e6f48 100644 --- a/modules/mesh-delivery-builder/internal/build/reserve_test.go +++ b/modules/mesh-delivery-builder/internal/build/reserve_test.go @@ -244,12 +244,17 @@ func TestAVersionsDigestsAreRecordedUnderTheBuildersOwnBound(t *testing.T) { }) { t.Errorf("journal %q, want a line saying 0.1.5's digests were recorded at a look", journal) } - // Recorded once: a reservation whose build ended recorded is left. - res.built = nil - res.held[fp1] = versions.ReservedVersion{Module: "m", Series: "0.1", Version: "0.1.5", Fingerprint: fp1, Artifacts: map[string]string{"m:tool": digest}, BuiltAt: time.Unix(1, 0)} + // Issue 1984 note 8, 1984/AC5: a reservation whose build ended recorded is handed to Built at every look with the digests it + // holds, nothing recorded anew, so its holder takes its state in (the published kept on it, its bound) whether or not the + // record before ended; the journal says no record. + res.built, journal = nil, nil + res.held[fp1] = versions.ReservedVersion{Module: "m", Series: "0.1", Version: "0.1.5", Fingerprint: fp1, Artifacts: map[string]string{"m:tool": "sha256:recorded"}, BuiltAt: time.Unix(1, 0)} r.consider(t, "modules/m") - if res.built != nil { - t.Errorf("a recorded reservation was recorded again: %v", res.built) + if res.built["m:tool"] != "sha256:recorded" || len(res.built) != 2 { + t.Errorf("a recorded reservation at a look was given to Built as %v, want its recorded digest of m:tool under 0.1.5 (1984/AC5)", res.built) + } + if slices.ContainsFunc(journal, func(l string) bool { return strings.Contains(l, "were not recorded") }) { + t.Errorf("journal %q: a recorded reservation was said recorded again", journal) } }) } @@ -465,12 +470,8 @@ func (r *realReserve) ReservedFor(ctx context.Context, module, fp string) (versi return versions.ReservedVersion{}, false, err } func (r *realReserve) Built(ctx context.Context, module, version string, digests map[string]string, _ map[string][]meshbuilds.Dependency) error { - for ref, d := range digests { - if _, err := r.Reserver.Built(ctx, module, version, ref, d, nil); err != nil { - return err - } - } - return nil + _, _, err := r.Reserver.Built(ctx, module, version, digests, nil) + return err } type reserveMap struct{ *memStore } diff --git a/modules/mesh-delivery-builder/internal/versions/index_test.go b/modules/mesh-delivery-builder/internal/versions/index_test.go index b7f86ad50..5aa40fab2 100644 --- a/modules/mesh-delivery-builder/internal/versions/index_test.go +++ b/modules/mesh-delivery-builder/internal/versions/index_test.go @@ -52,7 +52,7 @@ func TestTheIndexFindsAPublishedReservationInOneRead(t *testing.T) { t.Fatal(err) } if i%2 == 0 { // half are built, each with its own digest - if _, err := r.Built(ctx, m, "0.1.0", m+":a", "sha256:"+m, nil); err != nil { + if _, _, err := r.Built(ctx, m, "0.1.0", map[string]string{m + ":a": "sha256:" + m}, nil); err != nil { t.Fatal(err) } } @@ -70,7 +70,7 @@ func TestTheIndexFindsAPublishedReservationInOneRead(t *testing.T) { if _, err := r.Reserve(ctx, m, "0.1", []string{m + ":a"}, Source{Fingerprint: "fp" + m}); err != nil { t.Fatal(err) } - if _, err := r.Built(ctx, m, "0.1.0", m+":a", "sha256:"+m, nil); err != nil { + if _, _, err := r.Built(ctx, m, "0.1.0", map[string]string{m + ":a": "sha256:" + m}, nil); err != nil { t.Fatal(err) } } diff --git a/modules/mesh-delivery-builder/internal/versions/republish_test.go b/modules/mesh-delivery-builder/internal/versions/republish_test.go index c5953b7a6..0f78961f4 100644 --- a/modules/mesh-delivery-builder/internal/versions/republish_test.go +++ b/modules/mesh-delivery-builder/internal/versions/republish_test.go @@ -102,10 +102,8 @@ func (b *republishBed) reserve(t *testing.T, module string) ReservedVersion { if err != nil { t.Fatal(err) } - for ref, d := range map[string]string{module + ":prog": "sha256:aa", module + ":lib": "sha256:bb"} { - if _, err := b.Reserved.Built(ctx, module, v.Version, ref, d, nil); err != nil { - t.Fatal(err) - } + if _, _, err := b.Reserved.Built(ctx, module, v.Version, map[string]string{module + ":prog": "sha256:aa", module + ":lib": "sha256:bb"}, nil); err != nil { + t.Fatal(err) } b.now = b.now.Add(UnreleasedBound + time.Minute) return v diff --git a/modules/mesh-delivery-builder/internal/versions/reserve.go b/modules/mesh-delivery-builder/internal/versions/reserve.go index c62df7d04..c5d88ec43 100644 --- a/modules/mesh-delivery-builder/internal/versions/reserve.go +++ b/modules/mesh-delivery-builder/internal/versions/reserve.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "maps" + "reflect" "slices" "sort" "strconv" @@ -354,39 +355,55 @@ func (r *Reserver) Withdraw(ctx context.Context, module, version string) (Reserv } } -// Built records artifact's digest under the reserved version of module's series; once every artifact has one the build is -// ended (BuiltAt). A reservation moved on to another version (a newer build took the next) is left alone and answers false. -func (r *Reserver) Built(ctx context.Context, module, version, artifact, digest string, deps []meshbuilds.Dependency) (bool, error) { +// Built records the digests of the artifacts built under the reserved version of module's series, by reference, with what each +// resolved and was built with, in one compare-and-set of the reservation and one reindex for all of them (issue 1984 note 8, +// 1984/AC4): an artifact recorded already with its digest (and the dependencies given, when any are) is skipped, so a record +// tried again after one that timed out writes only what that one left, and nothing when every digest stands recorded. Once every +// artifact has one the build is ended (BuiltAt). It answers the reservation as it stands after the record; a reservation moved on +// to another version (a newer build took the next) is left alone and answers false. +func (r *Reserver) Built(ctx context.Context, module, version string, digests map[string]string, deps map[string][]meshbuilds.Dependency) (ReservedVersion, bool, error) { series, _, err := SplitVersion(version) if err != nil { - return false, err + return ReservedVersion{}, false, err } key := ReservedKey(module, series) for { cur, rev, found, err := r.get(ctx, key) if err != nil || !found || cur.Version != version { - return false, err + return ReservedVersion{}, false, err } was := cur was.Artifacts = maps.Clone(cur.Artifacts) - cur.Artifacts[artifact] = digest - if len(deps) > 0 { - if cur.Dependencies == nil { - cur.Dependencies = map[string][]meshbuilds.Dependency{} + if cur.Artifacts == nil { + cur.Artifacts = map[string]string{} + } + changed := false + for ref, d := range digests { + if have, ok := cur.Artifacts[ref]; ok && have == d && (len(deps[ref]) == 0 || reflect.DeepEqual(cur.Dependencies[ref], deps[ref])) { + continue // recorded already (1984/AC4) + } + cur.Artifacts[ref], changed = d, true + if len(deps[ref]) > 0 { + if cur.Dependencies == nil { + cur.Dependencies = map[string][]meshbuilds.Dependency{} + } + cur.Dependencies[ref] = deps[ref] } - cur.Dependencies[artifact] = deps } if cur.BuiltAt.IsZero() && allSet(cur.Artifacts) { - cur.BuiltAt = r.now() + cur.BuiltAt, changed = r.now(), true + } + if !changed { + return cur, true, nil } raw, _ := json.Marshal(cur) if _, err := r.Store.Update(ctx, key, raw, rev); errors.Is(err, ErrConflict) { continue } else if err != nil { - return false, fmt.Errorf("%s cannot be written: %w", key, err) + return ReservedVersion{}, false, fmt.Errorf("%s cannot be written: %w", key, err) } r.said(r.reindex(ctx, key, was, cur)) - return true, nil + return cur, true, nil } } diff --git a/modules/mesh-delivery-builder/internal/versions/reserve_test.go b/modules/mesh-delivery-builder/internal/versions/reserve_test.go index d04dd022d..46616f5ed 100644 --- a/modules/mesh-delivery-builder/internal/versions/reserve_test.go +++ b/modules/mesh-delivery-builder/internal/versions/reserve_test.go @@ -73,12 +73,10 @@ func TestReservation(t *testing.T) { } // The artifacts' digests, then the bound. - for _, a := range arts { - if ok, err := r.Built(ctx, "m", "0.1.23", a, "sha256:"+a, nil); err != nil || !ok { - t.Fatalf("built %s: %v %v", a, ok, err) - } + if v, ok, err := r.Built(ctx, "m", "0.1.23", map[string]string{"m:a": "sha256:m:a", "m:b": "sha256:m:b"}, nil); err != nil || !ok || v.BuiltAt.IsZero() { + t.Fatalf("built %+v, %v: %v", v, ok, err) } - if ok, _ := r.Built(ctx, "m", "0.1.22", "m:a", "x", nil); ok { + if _, ok, _ := r.Built(ctx, "m", "0.1.22", map[string]string{"m:a": "x"}, nil); ok { t.Error("a digest was written under a version a newer build superseded") } published := func(_, _, a string) bool { return a == "m:a" } @@ -119,7 +117,7 @@ func TestReservationIsReadAndMarkedPublished(t *testing.T) { t.Fatalf("reserved %q (%v), want 1.4.3", v.Version, err) } deps := []meshbuilds.Dependency{{Kind: 1, Part: "mesh-sdk:go", Version: "0.1.14", Digest: "sha256:dd"}, {Kind: 3, Part: "go-toolchain:program", Version: "go1.27", Digest: "sha256:tt"}} - if _, err := r.Built(ctx, "m", "1.4.3", "m:a", "sha256:aa", deps); err != nil { + if _, _, err := r.Built(ctx, "m", "1.4.3", map[string]string{"m:a": "sha256:aa"}, map[string][]meshbuilds.Dependency{"m:a": deps}); err != nil { t.Fatal(err) } rd := Reader{ @@ -164,10 +162,8 @@ func TestReservationIsReadAndMarkedPublished(t *testing.T) { if err != nil || !reflect.DeepEqual(up.Upstream, []string{"u:server"}) { t.Fatalf("reserved %+v (%v), want u:server kept as upstream", up, err) } - for _, a := range []string{"u:server", "u:tools"} { - if _, err := r.Built(ctx, "u", up.Version, a, "sha256:"+a, nil); err != nil { - t.Fatal(err) - } + if _, _, err := r.Built(ctx, "u", up.Version, map[string]string{"u:server": "sha256:u:server", "u:tools": "sha256:u:tools"}, nil); err != nil { + t.Fatal(err) } if marked, _, err := r.Published(ctx, "u-tools", up.Version, "sha256:u:tools"); err != nil || len(marked) != 1 { t.Fatalf("the published of u:tools marked %v (%v)", marked, err) @@ -222,7 +218,7 @@ func TestAReleaseIsRecordedByFingerprintBeforeItsReservationGoes(t *testing.T) { 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 { + if _, _, err := r.Built(ctx, "m", w.Version, map[string]string{"m:a": "sha256:cc"}, nil); err != nil { t.Fatal(err) } if _, _, err := r.Published(ctx, "m/a", w.Version, "sha256:cc"); err != nil { @@ -278,7 +274,7 @@ func TestPublishedAnswersByTheBuildFingerprint(t *testing.T) { if err != nil { t.Fatal(err) } - if _, err := r.Built(ctx, "mesh-grants", v.Version, "mesh-grants:busgrants", "sha256:bb", nil); err != nil { + if _, _, err := r.Built(ctx, "mesh-grants", v.Version, map[string]string{"mesh-grants:busgrants": "sha256:bb"}, nil); err != nil { t.Fatal(err) } ask := func(artifact, fp string) meshbuilds.PublishedAnswer { @@ -468,7 +464,7 @@ func TestANumberARegistryHoldsIsNeverReserved(t *testing.T) { } // The build ends; the registry holds 0.1.39 at another digest meanwhile: the number is withdrawn, and the build again of the same // fingerprint reserves the next number no registry holds, with no digest yet (H-A-279). - if _, err := r.Built(ctx, "node-tools", "0.1.39", "node-tools:program", "sha256:7c9b10", nil); err != nil { + if _, _, err := r.Built(ctx, "node-tools", "0.1.39", map[string]string{"node-tools:program": "sha256:7c9b10"}, nil); err != nil { t.Fatal(err) } if w, ok, err := r.Withdraw(ctx, "node-tools", "0.1.39"); err != nil || !ok || w.Withdrawn["0.1.39"] != "fp1" {