350 lines
16 KiB
Go
350 lines
16 KiB
Go
package build
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"maps"
|
|
"slices"
|
|
"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/meshbuilds"
|
|
)
|
|
|
|
// **A build waits for its revision; a build with no version is never made** (design 47 H H-A-155, H-A-156; H-A-141): a step
|
|
// ask names its branch and source revision, and the builder reserves the version before it builds. When the builder's picture
|
|
// of that branch does not hold that source revision yet, or holds it but has not taken in the entry of the asked fingerprint
|
|
// from it, the ask waits for the picture to take it in, woken by each entry the picture takes in (take, EntryGone) and each
|
|
// revision its view holds (PictureHeld, H-A-162), never a
|
|
// timer or a poll, up to RevisionWait (the setting revision-wait-minutes); past it the ask fails, naming the revision and the
|
|
// branch, and nothing is built. A step ask with no source (a run on request) does not wait.
|
|
//
|
|
// **The picture holds every kept revision, not only the newest** (design 47 H H-A-205, H-A-147): an ask of a source revision
|
|
// below the newest the picture holds, whose entries the picture no longer names, asks the catalog seat's verb revision for
|
|
// its catalog revision by number, once, and is built when that kept revision is of its source and names every entry it
|
|
// asks; it never waits, since the picture only moves on. Only a revision above the newest is waited for.
|
|
//
|
|
// **An ask naming a revision is held by its number** (design 47 H H-A-222): when the picture does not name the ask's
|
|
// entries, the ask's catalog revision is read by number, and the ask is built when the catalog keeps it and it names every
|
|
// fingerprint asked, whatever the source the picture holds says (a re-pin keeps a later revision of the same source, H-A-211,
|
|
// H-A-213, so the source alone cannot say it). The source is compared only for an ask naming no revision; an ask whose
|
|
// revision the catalog does not answer and whose source is not below the picture's waits as before.
|
|
|
|
// DefaultRevisionWait is the bound of a step ask's wait for its revision, its setting revision-wait-minutes' default (H-A-156).
|
|
const DefaultRevisionWait = 2 * time.Minute
|
|
|
|
func (b *Builder) revisionWait() time.Duration {
|
|
if b.RevisionWait > 0 {
|
|
return b.RevisionWait
|
|
}
|
|
return DefaultRevisionWait
|
|
}
|
|
|
|
// PictureHeld is told each revision a branch's view takes in whole, whether or not an entry changed (H-A-162): every step
|
|
// ask waiting for its revision looks again.
|
|
func (b *Builder) PictureHeld() {
|
|
b.init()
|
|
b.pictureMoved()
|
|
}
|
|
|
|
// pictureMoved wakes every step ask waiting for the picture: an entry was taken in or went, or a revision was held.
|
|
func (b *Builder) pictureMoved() {
|
|
b.mu.Lock()
|
|
moved := b.moved
|
|
b.moved = make(chan struct{})
|
|
b.mu.Unlock()
|
|
close(moved)
|
|
}
|
|
|
|
// holdsRevision says whether the builder's picture of branch holds the source revision and has taken in every entry of the
|
|
// ask from that branch.
|
|
func (b *Builder) holdsRevision(branch string, source int64, entries []meshbuilds.BuildEntry) bool {
|
|
held, ok := b.Picture(branch)
|
|
if !ok || held < source {
|
|
return false
|
|
}
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
for _, e := range entries {
|
|
key, err := builds.BuilderBuildKey(e.Module, e.Fingerprint)
|
|
if err != nil {
|
|
return true // the build key is refused where the entry is built, with why
|
|
}
|
|
if b.origin[key].Branch != branch && b.heard[branch+"\x00"+e.Module].Fp2 != e.Fingerprint {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// keptRevisionHolds answers no outcome when the catalog keeps the ask's revision (its catalog revision by number) and that
|
|
// revision names every entry of the ask (H-A-205, H-A-222); superseded when it names a newer fingerprint of an entry (H-A-508
|
|
// second addendum); else failed or could not run, with its words.
|
|
func (b *Builder) keptRevisionHolds(ctx context.Context, run meshbuilds.StepRun, held int64) (outcome, said string) {
|
|
below := fmt.Sprintf("revision %d of %s is not named by the builder's picture, which holds source revision %d", run.Source, run.Branch, held)
|
|
if run.Source < held {
|
|
below = fmt.Sprintf("revision %d of %s is below the %d the builder's picture holds", run.Source, run.Branch, held)
|
|
}
|
|
if b.KeptRevision == nil || run.Revision <= 0 {
|
|
return meshbuilds.StepEndedOutcomeFailed, below + ", and the ask names no catalog revision to read it by"
|
|
}
|
|
source, fps, err := b.KeptRevision(ctx, run.Branch, run.Revision)
|
|
if err != nil {
|
|
b.logf("%s: %s, and the catalog's revision %d cannot be read: %v", run.Pipeline, below, run.Revision, err)
|
|
return meshbuilds.StepEndedOutcomeCouldNotRun, fmt.Sprintf("%s, and the catalog's revision %d cannot be read: %v", below, run.Revision, err)
|
|
}
|
|
// An ask names the revision and the source of one revision (H-A-218, H-A-219): a kept revision of another source is
|
|
// not the ask's.
|
|
if source != run.Source {
|
|
b.logf("%s: %s, and the catalog's revision %d is of source revision %d, the ask names %d: the step fails, nothing is built", run.Pipeline, below, run.Revision, source, run.Source)
|
|
return meshbuilds.StepEndedOutcomeFailed, fmt.Sprintf("%s, and the catalog's revision %d is of source revision %d, the ask names %d", below, run.Revision, source, run.Source)
|
|
}
|
|
// A step whose fingerprint the catalog's revision no longer names but names a newer one of its module, found below the
|
|
// picture or by the revision's number, ends superseded with the superseding fingerprint in its why, never failed (design
|
|
// 47 H2 H-A-508 second addendum: one outcome for one fact, found by either check). Failed stays for any other mismatch.
|
|
var superseded []string
|
|
for _, e := range run.Entries {
|
|
if fps[e.Module] == e.Fingerprint {
|
|
continue
|
|
}
|
|
if fps[e.Module] != "" {
|
|
superseded = append(superseded, stepSuperseded(e.Module, e.Fingerprint, fps[e.Module]))
|
|
continue
|
|
}
|
|
b.logf("%s: %s, and the catalog's revision %d names %s at %q, not %s: the step fails, nothing is built", run.Pipeline, below, run.Revision, e.Module, fps[e.Module], e.Fingerprint)
|
|
return meshbuilds.StepEndedOutcomeFailed, fmt.Sprintf("%s, and the catalog's revision %d names %s at %q, not %s", below, run.Revision, e.Module, fps[e.Module], e.Fingerprint)
|
|
}
|
|
if len(superseded) > 0 {
|
|
said := strings.Join(superseded, "; ")
|
|
b.logf("%s: %s, and the catalog's revision %d names newer fingerprints: %s; nothing is built", run.Pipeline, below, run.Revision, said)
|
|
return meshbuilds.StepEndedOutcomeSuperseded, said
|
|
}
|
|
// Each asked fingerprint is recorded as the branch's, so its build reserves its version (H-A-155) as one the picture
|
|
// took in does; what the picture heard stays as it is, so the newer fingerprint's build is not superseded. Once the picture
|
|
// heard a newer entry of the module on the first branch, H-A-489 overrides H-A-205: the older fingerprint is refused
|
|
// superseded (buildEntry), never built or reserved.
|
|
b.mu.Lock()
|
|
for _, e := range run.Entries {
|
|
key, err := builds.BuilderBuildKey(e.Module, e.Fingerprint)
|
|
if err != nil || b.origin[key].Branch != "" {
|
|
continue
|
|
}
|
|
first := b.FirstBranch == nil || b.FirstBranch(run.Branch, e.Module)
|
|
b.trunkFP[key] = true
|
|
b.origin[key] = Origin{Branch: run.Branch, Source: run.Source, Revision: run.Revision, First: first}
|
|
}
|
|
b.mu.Unlock()
|
|
b.logf("%s: %s; the catalog keeps revision %d with its entries: the build goes on", run.Pipeline, below, run.Revision)
|
|
return "", ""
|
|
}
|
|
|
|
// awaitRevision returns no outcome once the builder's picture of the ask's branch holds its source revision; past the bound the
|
|
// step's outcome failed, at the builder's stop could not run, each with its words.
|
|
func (b *Builder) awaitRevision(ctx context.Context, run meshbuilds.StepRun) (outcome, said string) {
|
|
if b.Picture == nil || run.Source <= 0 || run.Branch == "" {
|
|
return "", ""
|
|
}
|
|
if held, ok := b.Picture(run.Branch); !b.holdsRevision(run.Branch, run.Source, run.Entries) {
|
|
below := ok && run.Source < held
|
|
if (run.Revision > 0 && b.KeptRevision != nil) || below {
|
|
// By its number (H-A-222), whatever the source the picture holds; for an ask naming none, only below the
|
|
// picture (H-A-205). A kept revision of another source or naming other fingerprints is a mismatch the wait
|
|
// cannot mend: it fails at once, or ends superseded at once when it names a newer fingerprint of the module (H-A-508
|
|
// second addendum); only a revision the catalog does not answer is waited for.
|
|
outcome, said := b.keptRevisionHolds(ctx, run, held)
|
|
if outcome == "" || below || outcome == meshbuilds.StepEndedOutcomeFailed || outcome == meshbuilds.StepEndedOutcomeSuperseded {
|
|
return outcome, said
|
|
}
|
|
}
|
|
}
|
|
bound := b.revisionWait()
|
|
notHeld := fmt.Sprintf("revision %d of %s not in the builder's picture", run.Source, run.Branch)
|
|
wait, stop := context.WithTimeout(ctx, bound) // a context's bound, no timer (ADR 0437 §8)
|
|
defer stop()
|
|
told := false
|
|
var keys []string
|
|
// Each entry of the ask is a wait said on the builder's stream waits (design 47 H2 H-A-577 (1)): `wait-held` while the picture
|
|
// does not hold the revision, `wait-ended` with how it ended: ran, failed at the bound, released by builder_release.
|
|
endWaits := func(how string) {
|
|
for _, e := range run.Entries {
|
|
if b.Waits != nil {
|
|
b.Waits.EndRevision(context.WithoutCancel(ctx), e.Module, e.Fingerprint, how)
|
|
}
|
|
}
|
|
b.mu.Lock()
|
|
for _, k := range keys {
|
|
delete(b.revisionWaiting, k)
|
|
delete(b.revisionReleased, k)
|
|
}
|
|
b.mu.Unlock()
|
|
}
|
|
for {
|
|
b.mu.Lock()
|
|
moved := b.moved
|
|
released := false
|
|
for _, k := range keys {
|
|
released = released || b.revisionReleased[k]
|
|
}
|
|
b.mu.Unlock()
|
|
if b.holdsRevision(run.Branch, run.Source, run.Entries) {
|
|
if told {
|
|
b.logf("%s: revision %d of %s is in the builder's picture: the build goes on", run.Pipeline, run.Source, run.Branch)
|
|
endWaits(WaitRan)
|
|
}
|
|
return "", ""
|
|
}
|
|
if released {
|
|
endWaits(WaitReleased)
|
|
b.logf("%s: %s: the wait was released by hand, the step fails, nothing is built", run.Pipeline, notHeld)
|
|
return meshbuilds.StepEndedOutcomeFailed, notHeld + ": the wait was released by hand, and a build with no version is never made"
|
|
}
|
|
if !told {
|
|
b.logf("%s: %s yet: the ask waits for it, at most %s", run.Pipeline, notHeld, bound)
|
|
told = true
|
|
since := b.now()
|
|
for _, e := range run.Entries {
|
|
if run.Revision <= 0 {
|
|
break // an ask naming no revision has no `revision:<R>` to say
|
|
}
|
|
k, err := builds.BuilderBuildKey(e.Module, e.Fingerprint)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
keys = append(keys, k)
|
|
b.mu.Lock()
|
|
b.revisionWaiting[k] = true
|
|
b.mu.Unlock()
|
|
if b.Waits != nil {
|
|
b.Waits.Hold(ctx, ToolchainWait{Module: e.Module, Fingerprint: e.Fingerprint, Since: since, Kind: WaitKindRevision, Revision: run.Revision, Bound: bound.String()})
|
|
}
|
|
}
|
|
}
|
|
select {
|
|
case <-moved:
|
|
case <-wait.Done():
|
|
if ctx.Err() != nil {
|
|
endWaits(WaitFailed)
|
|
return meshbuilds.StepEndedOutcomeCouldNotRun, notHeld + ": the builder stopped before it was"
|
|
}
|
|
endWaits(WaitFailed)
|
|
b.logf("%s: %s within %s: the step fails, nothing is built", run.Pipeline, notHeld, bound)
|
|
return meshbuilds.StepEndedOutcomeFailed, fmt.Sprintf("%s within %s", notHeld, bound)
|
|
}
|
|
}
|
|
}
|
|
|
|
// **A package with no pin is built against its publisher's version at the key's origin** (design 47 H2 H-A-521, H-A-547 (3),
|
|
// H-A-551; H-A-165): an entry names a mesh package with no version when a mover of the same take-in publishes it (DMC-1). Its
|
|
// build, a step ask's or a lane's, fills that version from the reservation of the module that publishes it (Members) at the
|
|
// source revision of the build key's own origin, once that reservation's artifact is published, and else from that module's
|
|
// release recorded at that source revision; never from the newest a registry seat said published. It waits for one, woken by
|
|
// each registry seat's published heard (PackagePublished), up to PackageWait (the setting package-wait-minutes). Past the bound
|
|
// the build fails, naming the package, and nothing is built.
|
|
|
|
// DefaultPackageWait is the bound of a build's wait for a package's publish, its setting package-wait-minutes' default.
|
|
const DefaultPackageWait = 10 * time.Minute
|
|
|
|
func (b *Builder) packageWait() time.Duration {
|
|
if b.PackageWait > 0 {
|
|
return b.PackageWait
|
|
}
|
|
return DefaultPackageWait
|
|
}
|
|
|
|
// PackagePublished is told each registry seat's published of a package (its name and the version the event carries): a build
|
|
// waiting for a package's version looks again at the reservations, which the published marked before it is told.
|
|
func (b *Builder) PackagePublished(name, version string) {
|
|
if name == "" {
|
|
return
|
|
}
|
|
b.init()
|
|
b.pictureMoved()
|
|
}
|
|
|
|
// versionAtOrigin is the version, as Go names it, the module that publishes the package holds at the source revision of the
|
|
// key's origin (H-A-547 (3), H-A-551), and words saying why there is none.
|
|
func (b *Builder) versionAtOrigin(ctx context.Context, key, pkg string) (version, none string) {
|
|
if b.Members == nil || b.Reserved == nil {
|
|
return "", "no module that publishes it can be read"
|
|
}
|
|
module, artifact, found, err := b.Members(ctx, pkg)
|
|
if err != nil || !found {
|
|
if err != nil {
|
|
return "", fmt.Sprintf("the module that publishes it cannot be read: %v", err)
|
|
}
|
|
return "", "no entry names the module that publishes it"
|
|
}
|
|
b.mu.Lock()
|
|
o := b.origin[key]
|
|
b.mu.Unlock()
|
|
v, found, err := b.Reserved.VersionAt(ctx, module, artifact, o.Branch, o.Source)
|
|
if err != nil {
|
|
return "", fmt.Sprintf("the version %s holds at source revision %d cannot be read: %v", module, o.Source, err)
|
|
}
|
|
if !found {
|
|
return "", fmt.Sprintf("%s publishes no version of it at source revision %d of %s", module, o.Source, o.Branch)
|
|
}
|
|
return "v" + strings.TrimPrefix(v, "v"), ""
|
|
}
|
|
|
|
// awaitPublished fills each package of packages with no pin from the version its publisher holds at the key's origin
|
|
// (versionAtOrigin), waiting for one (H-A-521, H-A-165). It answers the packages with every pin, or an outcome and its words:
|
|
// failed past the bound naming the package, could not run when the builder stops.
|
|
func (b *Builder) awaitPublished(ctx context.Context, module, fp string, packages map[string]string) (map[string]string, string, string) {
|
|
var unpinned []string
|
|
for p, v := range packages {
|
|
if v == "" {
|
|
unpinned = append(unpinned, p)
|
|
}
|
|
}
|
|
if len(unpinned) == 0 {
|
|
return packages, "", ""
|
|
}
|
|
slices.Sort(unpinned)
|
|
key, err := builds.BuilderBuildKey(module, fp)
|
|
if err != nil {
|
|
return nil, meshbuilds.StepEndedOutcomeCouldNotRun, err.Error()
|
|
}
|
|
bound := b.packageWait()
|
|
wait, stop := context.WithTimeout(ctx, bound) // a context's bound, no timer (ADR 0437 §8)
|
|
defer stop()
|
|
told := false
|
|
for {
|
|
b.mu.Lock()
|
|
moved := b.moved
|
|
b.mu.Unlock()
|
|
pins := map[string]string{}
|
|
missing, why := "", ""
|
|
for _, p := range unpinned {
|
|
v, none := b.versionAtOrigin(wait, key, p)
|
|
pins[p] = v
|
|
if v == "" && missing == "" {
|
|
missing, why = p, none
|
|
}
|
|
}
|
|
if missing == "" {
|
|
out := maps.Clone(packages)
|
|
maps.Copy(out, pins)
|
|
if told {
|
|
b.logf("%s at %s: every package it names is published: built with %v", module, fp, pins)
|
|
}
|
|
return out, "", ""
|
|
}
|
|
notPublished := fmt.Sprintf("package %s has no published version: %s", missing, why)
|
|
if !told {
|
|
b.logf("%s at %s: %s yet: the build waits for its publish, at most %s", module, fp, notPublished, bound)
|
|
told = true
|
|
}
|
|
select {
|
|
case <-moved:
|
|
case <-wait.Done():
|
|
if ctx.Err() != nil {
|
|
return nil, meshbuilds.StepEndedOutcomeCouldNotRun, notPublished + ": the builder stopped before it was"
|
|
}
|
|
b.logf("%s at %s: %s within %s: the build fails, nothing is built", module, fp, notPublished, bound)
|
|
return nil, meshbuilds.StepEndedOutcomeFailed, fmt.Sprintf("%s within %s", notPublished, bound)
|
|
}
|
|
}
|
|
}
|