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

178 lines
7.3 KiB
Go

package build
import (
"context"
"encoding/json"
"errors"
"fmt"
"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"
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
)
// **The builder's toolchain wait** (design 47 G §672, §267, §276): the builder holds an entry while its fingerprint names a
// toolchain version of its language other than the one the agreed toolchain of that language names.
const (
// WaitsTool is builder_waits, which reads the waits; ReleaseTool is builder_release, which builds the entry with the
// running build module (§276), a hand act written in the hand-act log.
WaitsTool = "builder_waits"
ReleaseTool = "builder_release"
// WaitsState is the builder's old state section of the waits, one key <module>.<fingerprint>.
WaitsState = "waits"
// ModeRelease is the Mode of a reasks value of builder_release.
ModeRelease = "release"
// WaitRan ends a wait when a toolchain-agreed names the version; WaitReleased when builder_release ended it.
WaitRan = "ran"
// WaitPublished ends the wait of a key queued behind a package, when the key is claimed (the package is published).
WaitPublished = "published"
WaitReleased = "released"
// WaitFailed ends a revision wait at its bound (design 47 H2 H-A-577 (1)).
WaitFailed = "failed"
// WaitReplaced ends a wait a build's next wait took the key from (design 47 H2 H-A-723 (1)).
WaitReplaced = "replaced"
// WaitKindRevision is the Kind of a revision wait.
WaitKindRevision = "revision"
)
// ToolchainWait is one wait: the entry's module and fingerprint, and the toolchain it waits for.
type ToolchainWait struct {
Module string `json:"module"`
Fingerprint string `json:"fingerprint"`
Language string `json:"language"`
Version string `json:"version"`
Since time.Time `json:"since"`
// Package is the package version a key queued behind it waits for, `<name>@<version>` (ADR 0603 §8; design 47 G A-192): the wait is the
// package's, not a toolchain's, and Language and Version are empty.
Package string `json:"package,omitempty"`
// Kind is "revision" on a step ask's wait for its revision; Revision is then the module graph revision the ask names (design
// 47 H2 H-A-577 (1)); Bound is that wait's own bound, said as `bound` (the toolchain wait's is the builder's).
Kind string `json:"kind,omitempty"`
Revision int64 `json:"revision,omitempty"`
Bound string `json:"bound,omitempty"`
// Publisher (`<module>@<fingerprint>`) and Until are the pin wait's: a build waiting for a blank pin's version holds Package, the
// package's path alone, Publisher the build it waits for, Revision the catalog revision the build holds and Until its start plus
// `package-wait-minutes` (design 47 H2 H-A-707 (5), DQ-53, H-A-717).
Publisher string `json:"publisher,omitempty"`
Until time.Time `json:"until,omitzero"`
}
// SameWait says w and o are one wait: every field but when it began and when it ends, so a Hold of the wait that stands changes
// nothing (design 47 H2 H-A-723 (1)).
func (w ToolchainWait) SameWait(o ToolchainWait) bool {
w.Since, w.Until, o.Since, o.Until = time.Time{}, time.Time{}, time.Time{}, time.Time{}
return w == o
}
// IsRevision says whether the wait is a step ask's wait for its revision, kept under `<module>.<fingerprint>.revision`.
func (w ToolchainWait) IsRevision() bool { return w.Kind == WaitKindRevision }
// On is what the wait waits for, as the wait's `on` says it.
func (w ToolchainWait) On() string {
if w.Package != "" {
return "package:" + w.Package
}
if w.IsRevision() {
return fmt.Sprintf("revision:%d", w.Revision)
}
return "toolchain:" + w.Language + "@" + w.Version
}
// Waits keeps the builder's waits: Hold writes one in the state waits and says wait-held, once per wait; End deletes it and
// says wait-ended with how, and answers whether one was held.
type Waits interface {
Hold(ctx context.Context, w ToolchainWait)
End(ctx context.Context, module, fp, how string) bool
// EndRevision ends the revision wait of module's fingerprint, saying how (ran, failed or released), and answers whether
// one was held (design 47 H2 H-A-577 (1)).
EndRevision(ctx context.Context, module, fp, how string) bool
}
// toolchainWait is the toolchain wait of a module's fingerprint, or false when no language of its plan names a version
// other than the agreed one (a language with no agreed toolchain is not a wait of §672).
func (b *Builder) toolchainWait(module string, m meshcatalog.ModulesByModuleValue, plan Plan) (ToolchainWait, bool) {
for _, bm := range plan.Modules() {
if !plan.NeedsToolchain(bm) {
continue
}
language, _ := b.Seats.LanguageOf(bm)
ag := sdkstep.ToolchainAgreement(b.Toolchains.Entries(language))
if want, ok := m.Toolchains[language]; ag.Wait == "" && ok && want.Version != ag.Toolchain.Version {
return ToolchainWait{Module: module, Fingerprint: m.Fp2, Language: language, Version: want.Version, Since: b.now()}, true
}
}
return ToolchainWait{}, false
}
// Release builds the held entry of module at fp with the running build module, now, and ends its wait released (design 47
// G §276, §672). It answers why nothing was done, or empty.
func (b *Builder) Release(ctx context.Context, module, fp, why, by string) string {
b.init()
key, err := builds.BuilderBuildKey(module, fp)
if err != nil {
return err.Error()
}
if b.Waits == nil {
return "this builder keeps no waits"
}
b.mu.Lock()
m, known := b.known[module]
b.mu.Unlock()
if !known || m.Fp2 != fp || m.Manifest == "" {
return fmt.Sprintf("module %s does not have the fingerprint %s now: the builder builds the module's current fingerprint", module, fp)
}
b.mu.Lock()
if b.released == nil {
b.released = map[string]bool{}
}
b.released[key] = true
b.mu.Unlock()
if !b.Waits.End(ctx, module, fp, WaitReleased) {
b.mu.Lock()
delete(b.released, key)
revision := b.revisionWaiting[key]
b.mu.Unlock()
// A step ask's wait for its revision ends as its bound does (design 47 H2 H-A-577 (1)): the ask fails, since a build
// with no version is never made (H-A-155).
if revision {
b.mu.Lock()
b.revisionReleased[key] = true
b.mu.Unlock()
b.pictureMoved()
b.logf("%s: the revision wait is released by %s (%s): the ask fails, nothing is built", key, by, why)
return ""
}
return fmt.Sprintf("%s holds no toolchain wait: %s builds a failed key again", key, BuildAgainTool)
}
b.logf("%s: released by %s (%s): built with the running build module", key, by, why)
b.startOnce(ctx, key, module, m)
return ""
}
func (b *Builder) isReleased(key string) bool {
b.mu.Lock()
defer b.mu.Unlock()
return b.released[key]
}
func (b *Builder) unrelease(key string) {
b.mu.Lock()
delete(b.released, key)
b.mu.Unlock()
}
// NewRelease is the value builder_release writes for a module's fingerprint: its key and the value.
func NewRelease(module, fp, why, by string, at time.Time) (string, []byte, error) {
key, err := builds.BuilderBuildKey(module, fp)
if err != nil {
return "", nil, err
}
if strings.TrimSpace(why) == "" {
return "", nil, errors.New("say why the entry is released (why)")
}
data, err := json.Marshal(ReAsk{V: 1, Why: strings.TrimSpace(why), By: by, At: at.UTC(), Mode: ModeRelease, Module: module, FP: fp})
return key, data, err
}