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

418 lines
19 KiB
Go

package build
import (
"context"
"errors"
"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()
}
// pinOf is the version of package pkg for the build of module at fp, which holds origin, and what it waited for when there is none
// (design 47 H2 H-A-707 (3), (4)). First the entry the picture holds for the module on the branch of origin, when it is of fp and
// names a version for the package: a newer entry of the key's fingerprint has the pin its earlier entry lacked, and an entry of
// another branch is never read. Else the version of the publisher's build in the build's revision: the module that publishes the
// package (Members), its fingerprint in the catalog revision origin names (the picture while it holds that revision, else the
// revision read by number, once), and the version the seat mesh-builds' published rule answers (Reserver.PublishedOf, H-A-328).
// publisher is `<module>@<fingerprint>` of that build, empty where no module publishes the package.
func (b *Builder) pinOf(ctx context.Context, module, fp, pkg string, origin Origin) (version, publisher, none string) {
hk := origin.Branch + "\x00" + module
b.mu.Lock()
h, heard := b.heard[hk]
at := b.heardAt[hk]
b.mu.Unlock()
if heard && h.Fp2 == fp && h.Packages[pkg] != "" {
if _, told := b.pinTold.LoadOrStore(module+"@"+fp+" "+pkg, true); !told {
b.logf("%s at %s: %s takes %s, the pin of the entry of revision %d, heard while the build from revision %d waited", module, fp, pkg, h.Packages[pkg], at.Revision, origin.Revision)
}
return h.Packages[pkg], "", ""
}
if b.Members == nil || b.Reserved == nil {
return "", "", "no module that publishes it can be read"
}
pubModule, artifact, found, err := b.Members(ctx, pkg)
if err != nil {
return "", "", fmt.Sprintf("the module that publishes it cannot be read: %v", err)
}
if !found {
return "", "", fmt.Sprintf("no module of revision %d publishes it", origin.Revision)
}
pubFP, err := b.fingerprintAt(ctx, origin, pubModule)
if err != nil {
return "", pubModule + "@", fmt.Sprintf("%s's fingerprint in revision %d cannot be read: %v", pubModule, origin.Revision, err)
}
publisher = pubModule + "@" + pubFP
if pubFP == "" {
return "", publisher, fmt.Sprintf("%s has no entry in revision %d of %s", pubModule, origin.Revision, origin.Branch)
}
a, err := b.Reserved.PublishedOf(ctx, pubModule, pubModule+":"+artifact, pubFP) // the reservation keeps an artifact by its ref
if err != nil {
return "", publisher, fmt.Sprintf("the version %s holds at %s cannot be read: %v", pubModule, pubFP, err)
}
if !a.Published {
return "", publisher, fmt.Sprintf("%s at %s (revision %d) is not published", pubModule, pubFP, origin.Revision)
}
return "v" + strings.TrimPrefix(a.Version, "v"), publisher, ""
}
// fingerprintAt is module's fingerprint in the catalog revision origin names: in the picture of the branch while the picture holds
// that revision (or the origin names none), else in the revision read by number through the seat mesh-catalog, kept once read.
func (b *Builder) fingerprintAt(ctx context.Context, origin Origin, module string) (string, error) {
hk := origin.Branch + "\x00" + module
b.mu.Lock()
h, heard := b.heard[hk]
at := b.heardAt[hk]
b.mu.Unlock()
if heard && (origin.Revision <= 0 || at.Revision == origin.Revision) {
return h.Fp2, nil
}
if b.KeptRevision == nil || origin.Revision <= 0 {
return "", errors.New("the picture does not hold the revision and none can be read by its number")
}
rk := fmt.Sprintf("%s\x00%d", origin.Branch, origin.Revision)
b.mu.Lock()
fps, ok := b.keptFPs[rk]
b.mu.Unlock()
if !ok {
_, read, err := b.KeptRevision(ctx, origin.Branch, origin.Revision)
if err != nil {
return "", err
}
fps = read
b.mu.Lock()
if b.keptFPs == nil {
b.keptFPs = map[string]map[string]string{}
}
b.keptFPs[rk] = fps
b.mu.Unlock()
}
return fps[module], nil
}
// awaitPublished fills each package of packages with no pin (pinOf), waiting for one (H-A-521, H-A-165). The wait is kept in the
// state waits in the one wait form, held under the build's key with the package's path, the publisher, the catalog revision and the
// time it ends, ended published or failed (H-A-707 (5)). It answers the packages with every pin, or an outcome and its words: failed
// past the bound naming the publisher and its fingerprint, could not run when the builder stops.
func (b *Builder) awaitPublished(ctx context.Context, module, fp string, packages map[string]string, origin Origin) (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)
if _, err := builds.BuilderBuildKey(module, fp); 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
since := b.now()
endWait := func(how string) {
if told && b.Waits != nil {
b.Waits.End(ctx, module, fp, how)
}
}
for {
b.mu.Lock()
moved := b.moved
b.mu.Unlock()
pins := map[string]string{}
missing, why, publisher := "", "", ""
for _, p := range unpinned {
v, pub, none := b.pinOf(wait, module, fp, p, origin)
pins[p] = v
if v == "" && missing == "" {
missing, why, publisher = p, none, pub
}
}
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)
endWait(WaitPublished)
}
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
}
if b.Waits != nil {
b.Waits.Hold(ctx, ToolchainWait{Module: module, Fingerprint: fp, Since: since, Package: missing, Publisher: publisher,
Revision: origin.Revision, Bound: bound.String(), Until: since.Add(bound)})
}
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)
endWait(WaitFailed)
return nil, meshbuilds.StepEndedOutcomeFailed, fmt.Sprintf("%s within %s", notPublished, bound)
}
}
}