Files
mesh-delivery/modules/mesh-delivery-builder/internal/build/numbers.go
T
jschoubben d0a04b77f0 mesh-delivery-builder: record a build's digests in one write and take a built reservation in from its state (issue 1984)
mesh-delivery-contracts 0.1.108's 39 digests were recorded one artifact at a
time, each a rewrite and a reindex (about 850 index reads), and timed out three
times under the one-minute record bound after its build was ended: its bound was
never armed, the published kept on it never taken in, and RecordAgain skipped it
since its build stood ended, so nothing said it (main.495's tier 2 held).

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

Builds: issue 1984 note 8 (1984/AC4, AC5), design 47 G §369, A-415, A-456b, A-457a, H-A-492
2026-11-23 18:22:14 +01:00

245 lines
11 KiB
Go

package build
import (
"context"
"errors"
"fmt"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
"git.novox.be/novox/mesh-sdk/go/catalogue"
"strings"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"mesh-delivery-builder/internal/versions"
)
// doneDigests are the digests of a trunk build's key by artifact when the key is done, nil otherwise or when it cannot be
// read: a look at a done build numbers what it left unnumbered with them (versions.BuildClaim.Done).
func (b *Builder) doneDigests(ctx context.Context, module, fp string) map[string]string {
key, err := builds.BuilderBuildKey(module, fp)
if err != nil || b.Claimer.Store == nil {
return nil
}
raw, _, found, err := b.Claimer.Store.Get(ctx, key)
if err != nil {
b.logf("%s: the build key cannot be read, so it is taken as not done: %v", key, err)
return nil
}
if !found {
return nil
}
cur, err := builds.DecodeBuilderBuild(raw)
if err != nil {
b.logf("%s: the build key holds what is no build, so it is taken as not done: %v", key, err)
return nil
}
if cur.State != builds.BuildDone {
return nil
}
out := map[string]string{}
for _, a := range cur.Artifacts {
if a.Digest != "" {
out[a.Name] = a.Digest
}
}
return out
}
// DefaultRecordBound is the bound of the record of a reservation's digests, its setting record-bound-seconds' default (design 47
// H2 H-A-492).
const DefaultRecordBound = time.Minute
// recordContext is the context a reservation's digests are recorded under: ctx's values with no cancel of its own, bounded by
// RecordBound (design 47 H2 H-A-492), so a takeover or an end cancelling the build cannot cancel the record.
func (b *Builder) recordContext(ctx context.Context) (context.Context, context.CancelFunc) {
bound := b.RecordBound
if bound <= 0 {
bound = DefaultRecordBound
}
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 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 {
return "", nil
}
digests := b.doneDigests(ctx, module, fp)
if digests == nil {
return "", nil
}
res, ok, err := b.Reserved.ReservedFor(ctx, module, fp)
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 || 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{}
for ref := range res.Artifacts {
_, artifact, _ := strings.Cut(ref, ":")
d, have := digests[artifact]
if !have {
return "", fmt.Errorf("%s at %s: the digests of %s are not recorded, and the done key holds no digest of %s to record %s", module, fp, res.Version, ref, when)
}
refs[ref] = d
}
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)
}
b.logf("%s at %s: the digests of %s were not recorded at its build's end: recorded %s from the done key", module, fp, res.Version, when)
return res.Version, nil
}
// PackageNamed looks at a module again when the catalog half's entry of it names a package's name in its ecosystem
// (design 47 G §348): a done build is looked at and nothing is built again.
func (b *Builder) PackageNamed(ctx context.Context, module string) {
b.init()
b.mu.Lock()
m, ok := b.known[module]
b.mu.Unlock()
if ok {
b.consider(ctx, module, m)
}
}
// Reservations reserves the version of a trunk build at its start and records each artifact's digest (design 47 G §369).
// Nil reserves nothing.
type Reservations interface {
// Reserve reserves module's next version in series for the artifacts (their references, <module>:<artifact>).
Reserve(ctx context.Context, module, series string, artifacts []string, src versions.Source) (versions.ReservedVersion, error)
// Built records the digests of the artifacts built under the reserved version, by reference, and what each resolved and was
// built with (design 47 G §466, §673).
Built(ctx context.Context, module, version string, digests map[string]string, deps map[string][]meshbuilds.Dependency) error
// ReservedFor is the reservation made for the build of fingerprint, and whether there is one.
ReservedFor(ctx context.Context, module, fingerprint string) (versions.ReservedVersion, bool, error)
// ReleasedFor says whether a version was released for the build of fingerprint, read from the builder's own record (A-456a).
ReleasedFor(ctx context.Context, module, fingerprint string) (bool, error)
// TakenOver is the reservation of module's series that took the series over from the build of fingerprint, whose number
// it keeps withdrawn (design 47 H H-A-226), and whether one does (H-A-244).
TakenOver(ctx context.Context, module, fingerprint string) (versions.ReservedVersion, bool, error)
// VersionAt is the version module's artifact is published at from the source revision of branch: its reservation of that
// origin with the artifact published, else its release recorded at that source revision (design 47 H2 H-A-547 (3), H-A-551).
VersionAt(ctx context.Context, module, artifact, branch string, source int64) (string, bool, error)
}
// reserves says whether a look at a trunk build is one that reserves: the build has a series of the form MAJOR.MINOR
// (design 47 G §368) and artifacts, and its key is not done already.
func (b *Builder) reserves(ctx context.Context, module string, m meshcatalog.ModulesByModuleValue, plan Plan) bool {
if b.Reserved == nil || len(plan.Tasks) == 0 {
return false
}
if strings.Count(plan.Series, ".") != 1 {
// G §505: such an entry (a manifest from before G, "1") reserves nothing and is built as a build without a version.
b.logf("%s: the catalog entry's version %q is not MAJOR.MINOR: no version is reserved, and the build is made and delivered without one", module, plan.Series)
return false
}
return b.doneDigests(ctx, module, m.Fp2) == nil
}
// reservesVersion says whether a plan's build reserves a version: it has artifacts and a MAJOR.MINOR series (§368).
func (b *Builder) reservesVersion(plan Plan) bool {
return len(plan.Tasks) > 0 && strings.Count(plan.Series, ".") == 1
}
// reserve reserves the version of the build that starts; a reservation that cannot be made is said and the build goes on
// unreserved: the caller fails the key, which a look takes again (A-490).
func (b *Builder) reserve(ctx context.Context, module string, m meshcatalog.ModulesByModuleValue, plan Plan, o Origin, rebuild bool) (versions.ReservedVersion, bool) {
refs := make([]string, 0, len(plan.Tasks))
for _, t := range plan.Tasks {
refs = append(refs, module+":"+t.Artifact)
}
src := versions.Source{Repository: m.Repository, Commit: m.Commit, Fingerprint: m.Fp2, Branch: o.Branch, Revision: o.Source, CatalogRevision: o.Revision, Rebuild: rebuild}
for _, t := range plan.Tasks {
if t.Kind == catalogue.ArtifactUpstream {
src.Upstream = append(src.Upstream, module+":"+t.Artifact) // copied by its pin, never awaited (H-A-486)
}
}
res, err := b.Reserved.Reserve(ctx, module, plan.Series, refs, src)
if err != nil {
b.logf("%s: no version is reserved for this build: %v", module, err)
return versions.ReservedVersion{}, false
}
b.logf("%s: version %s is reserved for this build", module, res.Version)
return res, true
}
// ReserveDone is builder_reserve_done (design 47 G A-455): a done trunk build of the first watched branch that reserved no
// version (its key is done, so a look never reserves for it, and a done key cannot be built again) reserves the next version of
// its series now, with the digests the done key holds, as a build that started would, and its build ends with every digest
// recorded. It answers the reserved version; the caller queues its publish (versions.Republisher). A fingerprint whose key is not
// done, whose entry no watched branch carries now, that is no first branch's, or that holds a reservation already is refused, saying which.
func (b *Builder) ReserveDone(ctx context.Context, module, fp string) (versions.ReservedVersion, error) {
b.init()
if b.Reserved == nil {
return versions.ReservedVersion{}, errors.New("this builder reserves no versions")
}
key, err := builds.BuilderBuildKey(module, fp)
if err != nil {
return versions.ReservedVersion{}, err
}
b.mu.Lock()
var m meshcatalog.ModulesByModuleValue
found := false
for hk, v := range b.heard {
if strings.HasSuffix(hk, "\x00"+module) && v.Fp2 == fp {
m, found = v, true
}
}
trunk, first := b.trunkFP[key], b.origin[key].First
b.mu.Unlock()
switch {
case !found:
return versions.ReservedVersion{}, fmt.Errorf("no watched branch carries an entry of %s at %s now, so there is nothing to reserve it from", module, fp)
case !trunk || !first:
return versions.ReservedVersion{}, fmt.Errorf("%s at %s is not the first watched branch's build: only that one reserves a version (§371)", module, fp)
}
digests := b.doneDigests(ctx, module, fp)
if digests == nil {
return versions.ReservedVersion{}, fmt.Errorf("the build key of %s at %s is not done", module, fp)
}
plan, planErr, _ := b.planOf(ctx, m.Manifest)
switch {
case planErr != nil:
return versions.ReservedVersion{}, fmt.Errorf("the plan of %s at %s cannot be read: %w", module, fp, planErr)
case len(plan.Tasks) == 0 || strings.Count(plan.Series, ".") != 1:
return versions.ReservedVersion{}, fmt.Errorf("%s at %s builds nothing or has no MAJOR.MINOR series (%q): it reserves no version", module, fp, plan.Series)
}
if cur, ok, err := b.Reserved.ReservedFor(ctx, module, fp); err != nil {
return versions.ReservedVersion{}, err
} else if ok {
return versions.ReservedVersion{}, fmt.Errorf("%s at %s holds the reservation of %s already", module, fp, cur.Version)
}
res, ok := b.reserve(ctx, module, m, plan, b.originOf(key), false)
if !ok {
return versions.ReservedVersion{}, fmt.Errorf("no version could be reserved for %s at %s (the builder's journal says why)", module, fp)
}
refs := map[string]string{}
for _, t := range plan.Tasks {
d, have := digests[t.Artifact]
if !have {
return res, fmt.Errorf("%s is reserved for %s at %s, but the done key holds no digest of %s", res.Version, module, fp, t.Artifact)
}
refs[module+":"+t.Artifact] = d
}
if err := b.Reserved.Built(ctx, module, res.Version, refs, nil); err != nil {
return res, fmt.Errorf("%s is reserved for %s at %s, but its digests are not recorded: %w", res.Version, module, fp, err)
}
return res, nil
}