diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/findings.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/findings.go index 8e0db2c85..ebee9999d 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/findings.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/findings.go @@ -19,10 +19,11 @@ import ( // A-29); its `findings-asked` subject is the seat's, from the generated contract (A-17). type heldWriter = views.HeldWriter[mcseat.Finding, mcseat.FindingCleared, mcseat.FindingsStated, mcseat.FindingsAskedEvent] -// findingWriter is the part of the held writer the builder's findings use. +// findingWriter is the part of the held writer the builder's findings use; Held answers the findings it holds (design 47 G A-38). type findingWriter interface { Write(ctx context.Context, f mcseat.Finding) error Clear(ctx context.Context, scope, id, kind, why string) error + Held() []mcseat.Finding } // findings writes the builder's findings through its held finding writer (design 47 G §689, A-29): a finding written before the @@ -113,6 +114,19 @@ func (f *findings) raise(ctx context.Context, fd finding) { } } +// stands says whether the writer holds the finding of kind about scope and id (design 47 G A-38): it is raised and not cleared. +func (f *findings) stands(kind, scope, id string) bool { + if f.w == nil { + return false + } + for _, x := range f.w.Held() { + if x.Kind == kind && x.Scope == scope && x.ID == keyID(id) { + return true + } + } + return false +} + // clear clears the finding of kind about scope and id; one not there is nothing to clear. func (f *findings) clear(ctx context.Context, kind, scope, id string) { if f.w == nil { diff --git a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/refused_test.go b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/refused_test.go index 1098c5150..c53d57d9b 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/refused_test.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/refused_test.go @@ -534,6 +534,17 @@ func (m *memFindings) Write(_ context.Context, f mcseat.Finding) error { return err } +// Held answers the findings kept, as the held writer answers those it holds. +func (m *memFindings) Held() []mcseat.Finding { + var out []mcseat.Finding + for _, v := range m.vals { + if f, err := decodeFinding(v); err == nil { + out = append(out, f) + } + } + return out +} + // Clear deletes the finding of scope, id and kind. func (m *memFindings) Clear(_ context.Context, scope, id, kind, _ string) error { delete(m.vals, views.FindingKey(scope, id, kind)) 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..16d5eff87 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go @@ -195,56 +195,104 @@ 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 the published kept on it taken in first, and then, on the +// reservation as that take-in left it, its publish's bound armed and its publish item asked (design 47 G A-457a; it stands until +// its release, A-456b). 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 } + if cur, standing = rv.takeKept(ctx, cur); !standing { + return nil + } rv.arm(ctx, cur) if rv.built != nil { if err := rv.built(ctx, module, version); err != nil { 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) - if err != nil { - rv.logf("the published kept on %s %s are not taken in: %v", module, 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) - // 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", - h.Name, h.Version, h.Digest, cur.Artifacts)) - } - for _, m := range marked { - rv.sayPublished(ctx, m, m.Reserved.Artifacts[m.Artifact]) - } 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). +// It answers the reservation as the take-in left it, and whether it stands built at its version, so its bound is judged on the +// artifacts the take-in marked (1984/AC5); one whose kept cannot be taken in is answered as it was given, its kept said not taken. +func (rv *reservations) takeKept(ctx context.Context, cur versions.ReservedVersion) (versions.ReservedVersion, bool) { + if len(cur.Kept) == 0 { + return cur, true + } + took, 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", cur.Module, cur.Version, err) + return cur, true + } + if !took.Standing { + rv.logf("the published kept on %s %s are not taken in: its reservation stands no more built at the version", cur.Module, cur.Version) + return cur, false + } + for _, h := range took.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, 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, took.Reserved, 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", + h.Name, h.Version, h.Digest, cur.Artifacts)) + } + for _, m := range took.Marked { + rv.sayPublished(ctx, m, m.Reserved.Artifacts[m.Artifact]) + } + return took.Reserved, true +} + +// 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 (lapsed), 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.lapsed(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 { rv.logf("the release bound of %s %s is not written, so it is raised unreleased now: %v", v.Module, v.Version, err) - rv.unreleased(ctx, v, fmt.Sprintf("its bound could not be written (%v)", err)) + rv.lapsed(ctx, v, fmt.Sprintf("its bound could not be written (%v)", err)) } } +// lapsed raises version-unreleased for a built reservation whose bound ended or could not be armed, only when the module's +// version-unreleased does not stand already (the held writer's Held, design 47 G A-38): a standing one keeps the words it was +// raised with, a holder's refusal of a publish again (ADR 0674 §3) or a digest no artifact carries (A-490j). Those two raise +// through unreleased, as they happen, and write over a standing one with their words. +func (rv *reservations) lapsed(ctx context.Context, v versions.ReservedVersion, why string) { + if rv.found.stands(versions.VersionUnreleased, meshconditionsclient.ScopeModule, v.Module) { + return + } + rv.unreleased(ctx, v, why) +} + // unreleased raises version-unreleased about the module, naming the version and the artifacts missing. func (rv *reservations) unreleased(ctx context.Context, v versions.ReservedVersion, why string) { missing := v.Missing() @@ -266,7 +314,9 @@ func (rv *reservations) unreleasedWords(ctx context.Context, v versions.Reserved } // ended is told each change of the state bounds: a reservation's bound whose time ended raises version-unreleased while -// the reservation stands built. +// the reservation stands built with an artifact unpublished (lapsed: a standing one keeps its words); one with every artifact +// published is not left unmarked (issue 1984 note 8, 1984/AC5) and waits for its version-released, as arm leaves it past the +// bound. func (rv *reservations) ended(ctx context.Context, e jetstream.KeyValueEntry) bool { key, ok := versions.ReservedBoundOf(e.Key()) if !ok { @@ -276,10 +326,10 @@ func (rv *reservations) ended(ctx context.Context, e jetstream.KeyValueEntry) bo return true } v, found, err := rv.r.At(ctx, key) - if err != nil || !found || v.BuiltAt.IsZero() { + if err != nil || !found || v.BuiltAt.IsZero() || len(v.Missing()) == 0 { return true } - rv.unreleased(ctx, v, fmt.Sprintf("nothing within %s of its build", rv.bound)) + rv.lapsed(ctx, v, fmt.Sprintf("nothing within %s of its build", rv.bound)) return true } @@ -348,7 +398,9 @@ 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): the published kept on it taken in and said, and then its bound armed +// on the reservation as that take-in left it. func (rv *reservations) releasedAtStart(ctx context.Context) { all, err := rv.r.All(ctx) if err != nil { @@ -374,6 +426,10 @@ 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. + if cur, standing := rv.takeKept(ctx, v); standing { + rv.arm(ctx, cur) + } continue } if err := rv.release(ctx, meshcatalog.VersionReleasedEvent{Module: v.Module, Version: v.Version}); err != nil { @@ -384,8 +440,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..456d21c62 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,251 @@ 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, and one whose kept published mark every artifact raises nothing. A version-unreleased standing with a holder's +// words keeps them past the bound and at the bound's end. +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: the published of the artifacts heard (a, b) + // 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, heard ...string) 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) + } + for _, a := range heard { + 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) != len(heard) { + 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), "a") + 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), "a") + 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), "a") + takeIn := func() { + if when == "a look" { + if err := rv.Built(ctx, module, v.Version, v.Artifacts, nil); err != nil { + t.Fatal(err) + } + } else { + rv.releasedAtStart(ctx) + } + } + takeIn() + 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) + } + // One standing is left as it was raised: the next take-in does not write it again over the holder's words (ADR 0674 §3). + rv.unreleasedWords(ctx, v, "the holder refused the publish again: no space left") + takeIn() + if got := unreleased(module); !strings.Contains(got, "no space left") || strings.Contains(got, "nothing within") { + t.Errorf("at %s again, version-unreleased reads %q, want the holder's words it was left with", when, got) + } + + // Note 8's own case: every artifact's published kept on it is taken in before its bound is judged, so nothing is + // unpublished and nothing is raised; each is said as package-published. + module = map[string]string{"a look": "keptlook", "the start": "keptstart"}[when] + v = recordedLost(module, now.Add(-versions.UnreleasedBound-time.Minute), "a", "b") + said = nil + takeIn() + if got := unreleased(module); got != "" { + t.Errorf("at %s, a reservation past its bound whose kept published mark every artifact raised %q, want no version-unreleased", when, got) + } + if !slices.Contains(said, module+":a@"+v.Version+" sha256:"+module+"a") || !slices.Contains(said, module+":b@"+v.Version+" sha256:"+module+"b") { + t.Errorf("at %s, said %v, want package-published of %s:a and %s:b", when, said, module, module) + } + if bounds.keys[versions.ReservedBoundKey(module, "0.1")] { + t.Errorf("at %s, the bound of a reservation past it was armed", when) + } + } + }) + t.Run("its bound ending", func(t *testing.T) { + v := recordedLost("ends", now.Add(-time.Minute), "a") + if err := rv.Built(ctx, "ends", v.Version, v.Artifacts, nil); err != nil { + t.Fatal(err) + } + boundKey := versions.ReservedBoundKey("ends", "0.1") + // The holder refused a publish again within the bound (ADR 0674 §3): the bound's end keeps those words. + rv.unreleasedWords(ctx, v, "the holder refused the publish again: no space left") + if !rv.ended(ctx, purged{key: boundKey}) { + t.Fatal("the reservation's bound was not taken in") + } + if got := unreleased("ends"); !strings.Contains(got, "no space left") || strings.Contains(got, "nothing within") { + t.Errorf("at the bound's end, version-unreleased reads %q, want the holder's words it was left with", got) + } + // With none standing, the bound's end raises it, naming the artifact unpublished. + _ = fs.Clear(ctx, meshconditionsclient.ScopeModule, "ends", versions.VersionUnreleased, "") + rv.ended(ctx, purged{key: boundKey}) + if got := unreleased("ends"); !strings.Contains(got, "ends:b unpublished") || !strings.Contains(got, "nothing within") { + t.Errorf("at the bound's end with none standing, version-unreleased reads %q, want nothing within naming ends:b", got) + } + // One whose every artifact is published is not left unmarked (1984/AC5): its bound's end raises nothing, and it waits for + // its version-released. + all := recordedLost("endsall", now.Add(-time.Minute), "a", "b") + if err := rv.Built(ctx, "endsall", all.Version, all.Artifacts, nil); err != nil { + t.Fatal(err) + } + if cur, _, _ := rv.r.Of(ctx, "endsall", all.Version); len(cur.Missing()) != 0 { + t.Fatalf("endsall: %v unpublished after the take-in, want every artifact published", cur.Missing()) + } + rv.ended(ctx, purged{key: versions.ReservedBoundKey("endsall", "0.1")}) + if got := unreleased("endsall"); got != "" { + t.Errorf("at the bound's end, a reservation whose every artifact is published raised %q, want no version-unreleased", got) + } + if cur, found, _ := rv.r.Of(ctx, "endsall", all.Version); !found || cur.BuiltAt.IsZero() { + t.Errorf("at the bound's end, the reservation of endsall %s stands no more, want it waiting for its version-released", all.Version) + } + }) +} + // 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 +806,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..a6a3a55e9 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 } } @@ -894,19 +911,32 @@ func (v ReservedVersion) keeps(name, digest string) bool { return false } +// KeptTaken is what Kept took in: the reservation as the take-in left it (as written, or as read when nothing was kept on it), +// whether it stands built at the version, the artifacts newly marked and the kept published whose digest matched no artifact. +type KeptTaken struct { + Reserved ReservedVersion + Standing bool + Marked []Marked + Unmatched []KeptPublished +} + // Kept takes the published kept on module's reservation of version in once its build recorded every digest (design 47 G // A-415): each kept published whose digest is an artifact's marks that artifact, as one heard after would, and every kept one -// leaves the reservation. It answers the artifacts newly marked and the kept published whose digest matched no artifact. -func (r *Reserver) Kept(ctx context.Context, module, version string) ([]Marked, []KeptPublished, error) { +// leaves the reservation. It answers the reservation as the take-in left it, so what follows the take-in is judged on the +// artifacts it marked (issue 1984 note 8, 1984/AC5). +func (r *Reserver) Kept(ctx context.Context, module, version string) (KeptTaken, error) { series, _, err := SplitVersion(version) if err != nil { - return nil, nil, err + return KeptTaken{}, err } key := ReservedKey(module, series) for { cur, rev, found, err := r.get(ctx, key) - if err != nil || !found || cur.Version != version || cur.BuiltAt.IsZero() || len(cur.Kept) == 0 { - return nil, nil, err + if err != nil || !found || cur.Version != version || cur.BuiltAt.IsZero() { + return KeptTaken{}, err + } + if len(cur.Kept) == 0 { + return KeptTaken{Reserved: cur, Standing: true}, nil } var arts []string var unmatched []KeptPublished @@ -932,13 +962,13 @@ func (r *Reserver) Kept(ctx context.Context, module, version string) ([]Marked, if _, err := r.Store.Update(ctx, key, raw, rev); errors.Is(err, ErrConflict) { continue } else if err != nil { - return nil, nil, fmt.Errorf("%s cannot be written: %w", key, err) + return KeptTaken{}, fmt.Errorf("%s cannot be written: %w", key, err) } - var marked []Marked + took := KeptTaken{Reserved: cur, Standing: true, Unmatched: unmatched} for _, a := range arts { - marked = append(marked, Marked{Reserved: cur, Artifact: a}) + took.Marked = append(took.Marked, Marked{Reserved: cur, Artifact: a}) } - return marked, unmatched, nil + return took, 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" {