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 25fe3fc02..46f804285 100644 --- a/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go +++ b/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go @@ -197,43 +197,54 @@ func (rv *reservations) Reserve(ctx context.Context, module, series string, arti // 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. +// 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 { 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) } } - 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) { +// 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 + return cur, true } - marked, unmatched, err := rv.r.Kept(ctx, cur.Module, cur.Version) + 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 } - for _, h := range unmatched { + 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, 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", + 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 marked { + 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. @@ -246,15 +257,14 @@ func (rv *reservations) now() time.Time { // 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, unless the module's -// version-unreleased stands already, which keeps the words it was raised with (ADR 0674 §3), and its bound is not armed again -// (issue 1984 note 8, 1984/AC5); one with every artifact published waits for its version-released. +// 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.found.stands(versions.VersionUnreleased, meshconditionsclient.ScopeModule, v.Module) { - rv.unreleased(ctx, v, fmt.Sprintf("nothing within %s of its build", rv.bound)) + if len(v.Missing()) > 0 { + rv.lapsed(ctx, v, fmt.Sprintf("nothing within %s of its build", rv.bound)) } return } @@ -268,10 +278,21 @@ func (rv *reservations) arm(ctx context.Context, v versions.ReservedVersion) { } 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() @@ -293,7 +314,7 @@ 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 (lapsed: a standing one keeps its words). func (rv *reservations) ended(ctx context.Context, e jetstream.KeyValueEntry) bool { key, ok := versions.ReservedBoundOf(e.Key()) if !ok { @@ -306,7 +327,7 @@ func (rv *reservations) ended(ctx context.Context, e jetstream.KeyValueEntry) bo if err != nil || !found || v.BuiltAt.IsZero() { 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 } @@ -376,7 +397,8 @@ func (rv *reservations) release(ctx context.Context, ev meshcatalog.VersionRelea // 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. 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. +// 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 { @@ -403,8 +425,9 @@ func (rv *reservations) releasedAtStart(ctx context.Context) { } 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) + 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 { 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 51271eb68..23162f468 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 @@ -435,7 +435,8 @@ func TestABuildsDigestsAreRecordedInOneWrite(t *testing.T) { // 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. +// 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{}} @@ -452,17 +453,20 @@ func TestABuiltReservationIsTakenInFromItsState(t *testing.T) { 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 { + // 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) } - rv.published(ctx, versions.Outcome{Name: module + "-a", Version: res.Version, Digest: "sha256:" + module + "a"}, nil) + 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) != 1 { + 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"} @@ -481,7 +485,7 @@ func TestABuiltReservationIsTakenInFromItsState(t *testing.T) { } t.Run("at a look", func(t *testing.T) { - v := recordedLost("look", now.Add(-5*time.Minute)) + 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) @@ -498,7 +502,7 @@ func TestABuiltReservationIsTakenInFromItsState(t *testing.T) { } }) t.Run("at the start", func(t *testing.T) { - v := recordedLost("start", now.Add(-time.Minute)) + 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")] { @@ -511,7 +515,7 @@ func TestABuiltReservationIsTakenInFromItsState(t *testing.T) { 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)) + 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 { @@ -534,6 +538,43 @@ func TestABuiltReservationIsTakenInFromItsState(t *testing.T) { 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) } }) } diff --git a/modules/mesh-delivery-builder/internal/versions/reserve.go b/modules/mesh-delivery-builder/internal/versions/reserve.go index c5d88ec43..a6a3a55e9 100644 --- a/modules/mesh-delivery-builder/internal/versions/reserve.go +++ b/modules/mesh-delivery-builder/internal/versions/reserve.go @@ -911,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 @@ -949,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 } }