mesh-delivery-builder: record a build's digests in one write and take a built reservation in from its state (issue 1984)

mesh-delivery-contracts 0.1.108's 39 digests were recorded one artifact at a
time, each a rewrite and a reindex (about 850 index reads), and timed out three
times under the one-minute record bound after its build was ended: its bound was
never armed, the published kept on it never taken in, and RecordAgain skipped it
since its build stood ended, so nothing said it (main.495's tier 2 held).

- versions.Reserver.Built takes the whole digest map: one compare-and-set of the
  reservation and one reindex; an artifact recorded already is skipped, so a
  record tried again writes only what the one before left, and nothing when all
  stand (1984/AC4). It answers the reservation as written, so the holder no
  longer reads every reservation after the record.
- What follows a record follows the reservation's state: RecordAgain hands a
  recorded reservation to Built at every look, and the start takes in every
  built one not released: its bound is armed, counted from its build's end, and
  the published kept on it are taken in and said as package-published; one left
  with an artifact unpublished past its bound raises version-unreleased at once
  and is not armed again (1984/AC5).

Builds: issue 1984 note 8 (1984/AC4, AC5), design 47 G §369, A-415, A-456b, A-457a, H-A-492
This commit is contained in:
2026-11-23 18:22:14 +01:00
parent 8bbe956102
commit d0a04b77f0
8 changed files with 322 additions and 73 deletions
@@ -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
@@ -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