Files
mesh-delivery/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/reserve.go
T

697 lines
34 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
"slices"
"sort"
"strings"
"time"
"github.com/nats-io/nats.go/jetstream"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-sdk/go/process"
"mesh-delivery-builder/internal/build"
"mesh-delivery-builder/internal/versions"
)
// The reservations (design 47 G §369): the builder's state reserved, the catalog's versions it numbers from, and the
// catalog's version-released it hears.
// awaitedReserved is the state reserved as versions.ReserveStore: bound once the bus holds it.
type awaitedReserved struct{ awaitedState }
func (s awaitedReserved) Delete(ctx context.Context, key string) error {
kv, err := s.kv()
if err != nil {
return err
}
if err := kv.Delete(ctx, key); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) {
return err
}
return nil
}
// reservations is the builder's reserving: the Reserver, the fill the catalog said, the bounds of a built reservation's
// publish and the findings.
type reservations struct {
r *versions.Reserver
// built asks a done build's publish (design 47 G A-457a), released ends its item; nil are none.
built func(ctx context.Context, module, version string) error
released func(ctx context.Context, module, version string)
// ended ends the publish item of a build withdrawn, superseded or dropped, its held items terminated; heard acknowledges the
// held items of a version a published newly marked (design 47 H2 §793). Nil are none.
itemEnded, itemHeard func(ctx context.Context, module, version string)
bounds func() jetstream.KeyValue
found *findings
bound time.Duration
logf func(string, ...any)
// withdraw ends the running build of a module's fingerprint, superseded, with why (H-A-225); nil ends none.
withdraw func(module, fingerprint, why string)
// recordAgain records the digests of a done build of module at fp its end did not record, saying so (design 47 H2 H-A-492);
// nil records none.
recordAgain func(ctx context.Context, module, fp, when string) (string, error)
// rebuild builds a done key of module at fp again (the build-again path, A-490; H-A-279); nil builds none.
rebuild func(ctx context.Context, module, fp, why string) error
// looking says a look of the builder holds the build key of module at fp now (builder_reserved_drop refuses it); nil says none.
looking func(module, fp string) bool
// judge reads the registries' record of a reservation's artifacts at its version (the republisher's Held); nil reads none.
judge func(ctx context.Context, v versions.ReservedVersion) (versions.Judged, error)
// say says package-published on the seat mesh-builds; nil says none.
say func(context.Context, meshbuilds.PackagePublished) error
// names is each package's name as its series key names it (for Go, its module path), and sayReserved says version-reserved on
// the seat mesh-builds (design 47 H2 §805); nil says none.
names versions.PackageNames
sayReserved func(context.Context, meshbuilds.VersionReservedEvent) error
}
// reservedSaid says version-reserved of each package a reservation newly numbers (design 47 H2 §805, H-A-420 (2)): its module,
// artifact, the package's path, the version, the fingerprint and the catalog revision it was reserved at, as the indexer names
// a revision, `<branch>.<n>`. A reservation that names no catalog revision is said in the journal, not on the seat: the indexer
// re-pins at a revision, and there is none to name.
func (rv *reservations) reservedSaid(ctx context.Context, v versions.ReservedVersion) {
if rv.sayReserved == nil || rv.names == nil {
return
}
refs := make([]string, 0, len(v.Artifacts))
for ref := range v.Artifacts {
refs = append(refs, ref)
}
slices.Sort(refs)
for _, ref := range refs {
_, artifact, _ := strings.Cut(ref, ":")
path, known, err := rv.names(ctx, v.Module, artifact)
if err != nil {
rv.logf("version-reserved of %s at %s is not said: its package's name cannot be read: %v", ref, v.Version, err)
continue
}
if !known {
continue // no package: nothing imports it by a version
}
if v.Branch == "" || v.Revision == 0 {
rv.logf("version-reserved of %s at %s is not said: the reservation names no catalog revision to re-pin at", ref, v.Version)
continue
}
ev := meshbuilds.VersionReservedEvent{Module: v.Module, Artifact: ref, Path: path, Version: v.Version, Fingerprint: v.Fingerprint,
Revision: fmt.Sprintf("%s.%d", v.Branch, v.Revision)}
if err := rv.sayReserved(ctx, ev); err != nil {
rv.logf("the version-reserved of %s at %s is not said: %v", ref, v.Version, err)
} else {
rv.logf("version-reserved said: %s (%s) at %s, revision %s", ref, path, v.Version, ev.Revision)
}
}
}
// sayReservedOn says version-reserved on the seat mesh-builds' events as cred, under an event id of the module, the version and
// the artifact, so a reservation said again is stored once (design 47 H2 §805, §821).
func sayReservedOn(js jetstream.JetStream, cred process.Account) func(context.Context, meshbuilds.VersionReservedEvent) error {
return func(ctx context.Context, ev meshbuilds.VersionReservedEvent) error {
return emitSeatEvent(context.WithoutCancel(ctx), js, cred, meshbuilds.SubjectVersionReserved, "version-reserved-"+ev.Module+"-"+ev.Version+"-"+ev.Artifact, ev)
}
}
// newReservations makes the reserving over store; the released versions of a module are asked of the seat mesh-catalog's
// verb versions through call (design 47 G §348), the only source numbering reads them from.
func newReservations(store versions.ReserveStore, call toolCaller, bounds func() jetstream.KeyValue, found *findings, logf func(string, ...any)) *reservations {
rv := &reservations{bounds: bounds, found: found, bound: versions.UnreleasedBound, logf: logf}
rv.r = &versions.Reserver{Store: store, ReleasedOf: func(_ context.Context, module string) ([]string, error) {
raw, err := call("seat:"+meshcatalog.Seat+".versions", meshcatalog.VersionsInput{Module: module})
if err != nil {
return nil, err
}
var a meshcatalog.VersionsAnswer
if err := json.Unmarshal(raw, &a); err != nil {
return nil, fmt.Errorf("the catalog's versions of %s answered what is no list of versions: %w", module, err)
}
var out []string
for _, v := range a.Versions {
out = append(out, v.Version)
}
return out, nil
}}
rv.r.Took, rv.r.Logf, rv.r.Reserved = rv.took, logf, rv.reservedSaid
return rv
}
// reindex writes the reservations' index again from the reservations, once at the builder's start (H-A-269 (1), H-A-271), and
// says so.
func (rv *reservations) reindex(ctx context.Context) {
n, err := rv.r.Reindex(ctx)
if err != nil {
rv.logf("the reservations' index is written again from %d reservations, not whole: %v", n, err)
return
}
rv.logf("the reservations' index is written again from %d reservations", n)
}
// took is a build's takeover of the reservation of an older build never released (design 47 G A-455): the older version is
// dropped, its bound ended and its finding cleared (the newer build's reservation has its own), and the builder's log says so.
func (rv *reservations) took(ctx context.Context, older, newer versions.ReservedVersion) {
if older.Fingerprint == newer.Fingerprint {
rv.logf("%s: the build of %s reserves again: %s takes the place of %s", newer.Module, newer.Fingerprint, newer.Version, older.Version)
} else {
rv.logf("%s: the reservation of %s (build of %s, never released) is dropped: the build of %s takes the series over and reserves %s", newer.Module, older.Version, older.Fingerprint, newer.Fingerprint, newer.Version)
}
if rv.itemEnded != nil {
rv.itemEnded(ctx, older.Module, older.Version) // its publish item goes with its reservation (A-456b), its items terminated (§793)
}
rv.dropBound(ctx, versions.ReservedKey(older.Module, older.Series))
rv.found.clear(ctx, versions.VersionUnreleased, meshconditionsclient.ScopeModule, older.Module)
if older.Fingerprint != newer.Fingerprint && rv.withdraw != nil {
// The older build is ended superseded, as the picture's supersession ends one (H-A-225, ADR 0664 §4): nothing publishes
// under the dropped number.
rv.withdraw(older.Module, older.Fingerprint, "its reservation "+older.Version+" was taken over by the build of "+newer.Fingerprint+", which reserves "+newer.Version)
}
}
var _ build.Reservations = (*reservations)(nil)
// ReservedFor is the reservation made for the build of fingerprint.
func (rv *reservations) ReservedFor(ctx context.Context, module, fingerprint string) (versions.ReservedVersion, bool, error) {
all, err := rv.r.All(ctx)
if err != nil {
return versions.ReservedVersion{}, false, err
}
for _, v := range all {
if v.Module == module && v.Fingerprint == fingerprint {
return v, true, nil
}
}
return versions.ReservedVersion{}, false, nil
}
// TakenOver is the reservation that took module's series over from the build of fingerprint (H-A-244).
func (rv *reservations) TakenOver(ctx context.Context, module, fingerprint string) (versions.ReservedVersion, bool, error) {
return rv.r.TakenOver(ctx, module, fingerprint)
}
// PublishedOf answers the seat mesh-builds' published rule for the build's pins (H-A-328, design 47 H2 H-A-707 (4)).
func (rv *reservations) PublishedOf(ctx context.Context, module, artifact, fingerprint string) (meshbuilds.PublishedAnswer, error) {
return rv.r.PublishedOf(ctx, module, artifact, fingerprint)
}
func (rv *reservations) Reserve(ctx context.Context, module, series string, artifacts []string, src versions.Source) (versions.ReservedVersion, error) {
return rv.r.Reserve(ctx, module, series, artifacts, src)
}
// 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 {
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)
}
}
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, 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.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()
what := "an artifact is unpublished"
if len(missing) > 0 {
what = strings.Join(missing, ", ") + " unpublished"
}
rv.found.raise(ctx, finding{kind: versions.VersionUnreleased, scope: meshconditionsclient.ScopeModule, id: v.Module,
summary: fmt.Sprintf("%s %s is built and not released: %s (%s)", v.Module, v.Version, what, why),
headline: "A built version is not released",
explanation: "The builder reserved " + v.Module + " " + v.Version + " and its build ended, and the catalog has not said version-released: " + what + ".",
resolved: "The catalog says version-released for " + v.Module + " " + v.Version + ".",
needs: "builder_reserved shows the reservation; builder_publish_again publishes the missing artifacts under it; builder_reserved_drop drops it."})
}
// unreleasedWords raises version-unreleased about a version with the words of a publish again that ended badly.
func (rv *reservations) unreleasedWords(ctx context.Context, v versions.ReservedVersion, why string) {
rv.unreleased(ctx, v, why)
}
// ended is told each change of the state bounds: a reservation's bound whose time ended raises version-unreleased while
// 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 {
return false
}
if e.Operation() != jetstream.KeyValuePurge {
return true
}
v, found, err := rv.r.At(ctx, key)
if err != nil || !found || v.BuiltAt.IsZero() || len(v.Missing()) == 0 {
return true
}
rv.lapsed(ctx, v, fmt.Sprintf("nothing within %s of its build", rv.bound))
return true
}
// dropBound ends a reservation's bound.
func (rv *reservations) dropBound(ctx context.Context, key string) {
if kv := rv.bounds(); kv != nil {
if err := kv.Delete(ctx, versions.ReservedBoundFor(key)); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) {
rv.logf("the release bound of %s is not ended: %v", key, err)
}
}
}
// heard takes the catalog's version-released in, and a registry seat's published (published): a
// message that is none of them is left to the others. A state that cannot take one leaves it for the bus to give again.
func (rv *reservations) heard(ctx context.Context, m jetstream.Msg) bool {
switch m.Subject() {
case meshcatalog.SubjectVersionReleased:
var ev meshcatalog.VersionReleasedEvent
if err := json.Unmarshal(m.Data(), &ev); err != nil || ev.Module == "" || ev.Version == "" {
rv.logf("%s holds what is no version-released, left: %v", m.Subject(), err)
break
}
if err := rv.release(ctx, ev); err != nil {
rv.logf("%v; heard again", err)
if nakErr := m.Nak(); nakErr != nil {
rv.logf("leaving %s for the bus to give again: %v", m.Subject(), nakErr)
}
givenUp(m, "the version-released of "+ev.Module+" "+ev.Version, err, rv.logf)
return true
}
default:
return false
}
if err := m.Ack(); err != nil {
rv.logf("acknowledging %s: %v", m.Subject(), err)
}
return true
}
// release takes a released version in as the catalog's version-released says it (design 47 G §369): its reservation released,
// its bound and publish item ended and its findings cleared. No git tag is written (design 47 H2 H-A-494).
func (rv *reservations) release(ctx context.Context, ev meshcatalog.VersionReleasedEvent) error {
// A version no reservation holds is acknowledged at once from its one key, never by listing (H-A-514 addendum).
if held, err := rv.r.Holds(ctx, ev.Module, ev.Version); err == nil && !held {
return nil
}
gone, err := rv.r.Released(ctx, ev.Module, ev.Version)
if err != nil {
return err
}
for _, k := range gone {
rv.dropBound(ctx, k)
}
if rv.released != nil {
rv.released(ctx, ev.Module, ev.Version)
}
rv.found.clear(ctx, versions.VersionUnreleased, meshconditionsclient.ScopeModule, ev.Module)
rv.found.clear(ctx, versions.VersionHeldElsewhere, meshconditionsclient.ScopeModule, ev.Module)
rv.found.clear(ctx, versions.VersionTaggedElsewhere, meshconditionsclient.ScopeModule, ev.Module) // one raised before H-A-494
return nil
}
// releasedAtStart takes in, at the builder's start, every built, unreleased reservation whose version the catalog's verb versions
// holds released, as its version-released would (design 47 H2 H-A-484 addendum): a version-released the bus gave up on, or one
// said while the builder was away, is taken all the same, its reservation released and its publish item ended; no tag is written
// (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. 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 {
rv.logf("the reservations are not held against the catalog's released versions at the start: %v", err)
return
}
if rv.recordedAtStart(ctx, all) {
if all, err = rv.r.All(ctx); err != nil {
rv.logf("the reservations are not held against the catalog's released versions at the start: %v", err)
return
}
}
read := map[string][]string{}
for _, v := range builtOf(all) {
if versions.NotStanding(v, true) != "" {
continue
}
released, ok := read[v.Module]
if !ok {
if released, err = rv.r.ReleasedOf(ctx, v.Module); err != nil {
rv.logf("%s: the catalog's released versions cannot be read at the start, so its built reservations wait for its next version-released: %v", v.Module, err)
}
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 {
rv.logf("%s %s is released in the catalog and not taken in at the start: %v", v.Module, v.Version, err)
continue
}
rv.logf("%s %s is released in the catalog: taken in at the start, as its version-released", v.Module, v.Version)
}
}
// 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
}
recorded := false
for _, v := range all {
if !v.BuiltAt.IsZero() || v.Fingerprint == "" || versions.NotStanding(v, true) != "" {
continue
}
version, err := rv.recordAgain(ctx, v.Module, v.Fingerprint, "at the builder's start")
if err != nil {
rv.logf("%v", err)
}
recorded = recorded || version != ""
}
return recorded
}
// published marks the artifacts of the reservation a registry seat's published names (its version and digest). inProgress is told
// before a pass that may read many reservations, so the handler says in progress to the bus (design 47 H H-A-280); nil is told
// nothing.
func (rv *reservations) published(ctx context.Context, o versions.Outcome, inProgress func()) {
out, err := rv.r.PublishedRead(ctx, o.Name, o.Version, o.Digest, inProgress)
if err != nil {
rv.logf("a published of %s@%s is not recorded in the reservations: %v", o.Name, o.Version, err)
}
if len(out.Kept) > 0 {
rv.logf("the published of %s@%s (digest %s, holder %s) is kept on %s until the build records its digests", o.Name, o.Version, o.Digest, o.Holder, strings.Join(out.Kept, ", "))
} else if len(out.Marked) == 0 {
rv.logf("package-published not said: the published of %s@%s (digest %s, holder %s) marks no artifact of a reservation: %s", o.Name, o.Version, o.Digest, o.Holder, rv.noMark(ctx, o, out.Read))
}
// Each artifact the holder's published newly marked is said as the seat mesh-builds' package-published (design 47 G §52, §334).
for _, m := range out.Marked {
rv.sayPublished(ctx, m, o.Digest)
}
if rv.itemHeard != nil { // the publish items a published newly marked are acknowledged (§793)
seen := map[string]bool{}
for _, m := range out.Marked {
if k := m.Reserved.Module + " " + m.Reserved.Version; !seen[k] {
seen[k] = true
rv.itemHeard(ctx, m.Reserved.Module, m.Reserved.Version)
}
}
}
}
// sayPublished says the seat mesh-builds' package-published of an artifact a published marked.
func (rv *reservations) sayPublished(ctx context.Context, m versions.Marked, digest string) {
if rv.say == nil {
rv.logf("package-published of %s at %s not said: no seat to say it on", m.Artifact, m.Reserved.Version)
return
}
if err := rv.say(ctx, m.PackagePublished(digest, time.Now())); err != nil {
rv.logf("the package-published of %s at %s is not said: %v", m.Artifact, m.Reserved.Version, err)
} else {
rv.logf("package-published said: %s at %s, digest %s", m.Artifact, m.Reserved.Version, digest)
}
}
// dropped is builder_reserved_drop (design 47 H2 H-A-592): it names its version, and the reservation of the series is deleted whole
// only when it stands at that version and its fingerprint is not building; the publish item and the version-unreleased go with it,
// the bound ends, and the answer names what was forgotten.
func (rv *reservations) dropped(ctx context.Context, module, series, version, by, why string, at time.Time) (string, error) {
cur, held, err := rv.r.At(ctx, versions.ReservedKey(module, series))
if err != nil {
return "", err
}
if !held {
return "", fmt.Errorf("%s holds no reservation of %s", module, series)
}
if cur.Version != version {
return "", fmt.Errorf("%s's reservation of %s stands at %s, not %s: a reservation a newer build took is never dropped", module, series, cur.Version, version)
}
if rv.looking != nil && rv.looking(module, cur.Fingerprint) {
return "", fmt.Errorf("the build of %s at %s, which holds %s, is building: drop it when that build ends or its ask is dropped (builder_stuck reads it)", module, cur.Fingerprint, version)
}
atRegistries := "the registries' record was not read"
if rv.judge != nil {
if j, jerr := rv.judge(ctx, cur); jerr == nil {
var names []string
for _, h := range append(append([]versions.HeldArtifact{}, j.Held...), j.Elsewhere...) {
names = append(names, h.Seat+" "+h.Name)
}
atRegistries = "the registries keep " + strings.Join(names, ", ") + " at it, a gap the next reserve skips"
if len(names) == 0 {
atRegistries = "no registry holds an artifact at it"
}
}
}
ok, err := rv.r.Drop(ctx, module, series)
if err != nil {
return "", err
}
if !ok {
return "", fmt.Errorf("%s holds no reservation of %s", module, series)
}
if rv.itemEnded != nil {
rv.itemEnded(ctx, module, cur.Version) // its publish item goes with its reservation (A-456b), its items terminated (§793)
}
rv.dropBound(ctx, versions.ReservedKey(module, series))
rv.found.clear(ctx, versions.VersionUnreleased, meshconditionsclient.ScopeModule, module)
withdrawn := make([]string, 0, len(cur.Withdrawn))
for v := range cur.Withdrawn {
withdrawn = append(withdrawn, v)
}
sort.Strings(withdrawn)
return fmt.Sprintf("the reservation of %s %s is dropped (%s, %s): forgot version %s of fingerprint %s, %d recorded digests, withdrawn numbers %v and its publish item and version-unreleased; %s",
module, series, by, why, cur.Version, cur.Fingerprint, len(cur.Artifacts), withdrawn, atRegistries), nil
}
// noMark says why a published marked nothing, from the reservations the published read (design 47 H H-A-280: no second read):
// what each holds of digests, and how many stand at that version by the index by version (its one key, no reservation read).
func (rv *reservations) noMark(ctx context.Context, o versions.Outcome, read []versions.ReadReservation) string {
version := strings.TrimPrefix(o.Version, "v")
if version == "" || o.Digest == "" {
return "it names no version or no digest (a by-digest publish)"
}
var out []string
for _, r := range read {
v := r.Reserved
if v.Version != version {
continue
}
out = append(out, fmt.Sprintf("%s holds %d built digests %v, published %v", v.Module, len(v.Artifacts), v.Artifacts, v.Published))
}
standing := "the number standing at it is not read"
if keys, err := rv.r.IndexedAt(ctx, version); err == nil {
standing = fmt.Sprintf("%d reservations stand at it, the built ones not read", len(keys))
}
if len(out) == 0 {
return "no reservation read holds version " + version + " unbuilt or with that digest (" + standing + ")"
}
return strings.Join(out, "; ") + " (" + standing + "; a published heard before the build recorded its digests is kept until it does)"
}
// ReleasedFor says whether a version was released for the build of fingerprint (design 47 G A-456a).
func (rv *reservations) ReleasedFor(ctx context.Context, module, fingerprint string) (bool, error) {
return rv.r.ReleasedFor(ctx, module, fingerprint)
}
// MarkRegistryHeld reads the registries' record of v's artifacts and writes the held digest of each artifact a registry holds at v's version
// for the build's fingerprint on the reservation, marked published, and says package-published of each once (design 47 H2 H-A-588
// (4)); what the registries do not hold loses its recorded digest (H-A-664). A read that does not answer is said and nothing is
// written, so the build goes on.
func (rv *reservations) MarkRegistryHeld(ctx context.Context, v versions.ReservedVersion) {
if rv.judge == nil {
return
}
j, err := rv.judge(ctx, v)
if err != nil {
rv.logf("%s %s: the registries' record of its artifacts is not read before the build module is asked again, so none is marked held: %v", v.Module, v.Version, err)
return
}
marked, _, err := rv.r.MarkHeld(ctx, v.Module, v.Version, j.Held)
if err != nil {
rv.logf("%s %s: what the registries hold is not written on the reservation: %v", v.Module, v.Version, err)
return
}
for _, m := range marked {
var held versions.HeldArtifact
for _, h := range j.Held {
if h.Ref == m.Artifact {
held = h
}
}
rv.logf("%s %s: %s holds %s %s at %s for this build: marked published from its record before the build is asked again", v.Module, v.Version, held.Seat, held.Name, held.Version, held.Digest)
rv.sayPublished(ctx, m, held.Digest)
}
}
// markHeld marks the artifacts the registries' record holds at their version with the built digest published, as their published
// heard would, and says package-published of each (design 47 H H-A-274): a published heard and lost never leaves the publish
// asked for ever. The version's release follows from the catalog as for any published.
func (rv *reservations) markHeld(ctx context.Context, v versions.ReservedVersion, held []versions.HeldArtifact) {
for _, h := range held {
if v.Published[h.Ref] {
continue // marked already: its package-published was said (design 47 H2 H-A-588 (4): once)
}
rv.logf("%s %s: %s holds %s %s with the built digest %s: marked published from its record", v.Module, v.Version, h.Seat, h.Name, h.Version, h.Digest)
rv.published(ctx, versions.Outcome{Name: h.Name, Version: h.Version, Digest: h.Digest, Holder: h.Seat}, nil)
}
}
// heldElsewhere raises version-held-elsewhere about module (design 47 H H-A-275): numbers a registry holds were skipped or moved
// from, said with why.
func (rv *reservations) heldElsewhere(ctx context.Context, module, why string) {
rv.logf("%s: %s", module, why)
rv.found.raise(ctx, finding{kind: versions.VersionHeldElsewhere, scope: meshconditionsclient.ScopeModule, id: module,
summary: module + ": " + why,
headline: "A number a registry already holds is not used",
explanation: "A registry seat holds a version of " + module + " the builder did not publish there: " + why + ".",
resolved: "The catalog says version-released for " + module + ".",
needs: "builder_reserved shows the reservation and its withdrawn numbers; the registry seat's versions shows what it holds."})
}
// movedElsewhere takes a done build whose number a registry holds at another digest off that number (design 47 H H-A-275,
// H-A-279): every artifact carries its version from its build, so no digest survives a move. The number is withdrawn as a gap
// (H-A-226), the finding raised naming both digests, and the build of the same fingerprint asked again (the build-again path of a
// done key whose version is not released, A-490): its reserve takes the next number no registry holds, it records fresh digests
// and its own publish runs; the takeover of the withdrawn reservation ends its publish item and bound.
func (rv *reservations) movedElsewhere(ctx context.Context, v versions.ReservedVersion, j versions.Judged) error {
if rv.rebuild == nil {
return errors.New("no build again is wired")
}
if _, ok, err := rv.r.Withdraw(ctx, v.Module, v.Version); err != nil || !ok {
if err == nil {
err = fmt.Errorf("no reservation of %s stands at %s", v.Module, v.Version)
}
return err
}
why := elsewhereWords(v, j.Elsewhere)
rv.heldElsewhere(ctx, v.Module, why)
return rv.rebuild(ctx, v.Module, v.Fingerprint, why)
}
// elsewhereWords says what withdrew v's number: each artifact a registry holds there for another build, with the seat, both digests and
// the fingerprint the registry keeps for it when one is kept (design 47 H2 H-A-590 (1), H-A-279).
func elsewhereWords(v versions.ReservedVersion, elsewhere []versions.HeldArtifact) string {
var at []string
for _, h := range elsewhere {
w := fmt.Sprintf("%s holds %s %s at %s, built %s", h.Seat, h.Name, h.Version, h.Digest, v.Artifacts[h.Ref])
if h.Fingerprint != "" {
w += ", for fingerprint " + h.Fingerprint
}
at = append(at, w)
}
return fmt.Sprintf("%s is withdrawn and the build of %s is built again at the next free number: %s", v.Version, v.Fingerprint, strings.Join(at, "; "))
}
// unstoredAgain builds a key again whose reservation's first artifact a holder refused as not stored (design 47 H2 H-A-662): the
// build under its standing version completes what the registries lack, as the person's builder_build_again would. A key the
// build again refuses (not a first branch's trunk build, or released) is said and left to the person's tool.
func (rv *reservations) unstoredAgain(ctx context.Context, v versions.ReservedVersion, ref, words string) {
if rv.rebuild == nil {
return
}
why := fmt.Sprintf("%s refused by its holder as not stored (%s): the build of %s is built again under %s", ref, words, v.Fingerprint, v.Version)
if err := rv.rebuild(ctx, v.Module, v.Fingerprint, why); err != nil {
rv.logf("%s %s: the build again is not started: %v; builder_build_again builds it", v.Module, v.Version, err)
}
}
// movedFrom is told by the reserver a standing number it withdrew at a build again (design 47 H2 H-A-590 (1)): the finding is raised;
// the reserve itself takes the next number no registry holds.
func (rv *reservations) movedFrom(ctx context.Context, v versions.ReservedVersion, elsewhere []versions.HeldArtifact) {
rv.heldElsewhere(ctx, v.Module, elsewhereWords(v, elsewhere))
}