Files
mesh-delivery/modules/mesh-delivery-builder/internal/build/numbers.go
T

184 lines
8.7 KiB
Go

package build
import (
"context"
"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)
// MarkRegistryHeld reads the registries' record of v's artifacts at its version and writes on the reservation the held digest of each
// artifact a registry holds there for the build's fingerprint, marking it published, before a build module is asked for the
// fingerprint again (design 47 H2 H-A-588 (4)). A read that does not answer is said and the build goes on.
MarkRegistryHeld(ctx context.Context, v versions.ReservedVersion)
// 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
}