When a registry seat says published, the builder says the seat mesh-builds' package-published once per artifact the reservation newly marks: the artifact reference, version and digest published, the watched branch and the source revision kept on the reservation from the entry the build came from (A-57), and the dependencies of kind 1 (resolved) and 3 (toolchain) the step result named (§466, §673), their parts the artifact references the entry's edges name. The reservation keeps the dependencies from the build's end until the publication. Builds: design 47 G §52, §334, §349, §371, §466, §673, A-57 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
259 lines
10 KiB
Go
259 lines
10 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"git.novox.be/novox/mesh-delivery/contracts/go/seats/meshbuilds"
|
|
"strings"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
|
"git.novox.be/novox/mesh-delivery/contracts/go/seats/git"
|
|
"git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
|
|
|
"mesh-delivery-builder/internal/build"
|
|
"mesh-delivery-builder/internal/versions"
|
|
)
|
|
|
|
// The reservations (design 47 G §369): the builder's state reserved, the catalog's versions it numbers from, and the
|
|
// catalog's version-released and versions-filled it hears.
|
|
|
|
// awaitedReserved is the state reserved as versions.ReserveStore: bound once the bus holds it.
|
|
type awaitedReserved struct{ awaitedVersions }
|
|
|
|
func (s awaitedReserved) Delete(ctx context.Context, key string) error {
|
|
kv, err := s.kv()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := kv.Delete(ctx, key); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// reservations is the builder's reserving: the Reserver, the fill the catalog said, the bounds of a built reservation's
|
|
// publish and the findings.
|
|
type reservations struct {
|
|
r *versions.Reserver
|
|
filled atomic.Bool
|
|
bounds func() jetstream.KeyValue
|
|
found *findings
|
|
bound time.Duration
|
|
wake func(context.Context)
|
|
logf func(string, ...any)
|
|
// call asks a seat's verb: the git seat's tag (design 47 G §370).
|
|
call toolCaller
|
|
// say says package-published on the seat mesh-builds; nil says none.
|
|
say func(context.Context, meshbuilds.PackagePublished) error
|
|
}
|
|
|
|
// newReservations makes the reserving over store; the released versions of a module are asked of the seat mesh-catalog's
|
|
// verb versions through call (design 47 G §348), the only source numbering reads them from.
|
|
func newReservations(store versions.ReserveStore, call toolCaller, bounds func() jetstream.KeyValue, found *findings, wake func(context.Context), logf func(string, ...any)) *reservations {
|
|
rv := &reservations{call: call, bounds: bounds, found: found, bound: versions.UnreleasedBound, wake: wake, logf: logf}
|
|
rv.r = &versions.Reserver{Store: store, Filled: rv.filled.Load, ReleasedOf: func(_ context.Context, module string) ([]string, error) {
|
|
raw, err := call("seat:"+meshcatalog.Seat+".versions", meshcatalog.VersionsInput{Module: module})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var a meshcatalog.VersionsAnswer
|
|
if err := json.Unmarshal(raw, &a); err != nil {
|
|
return nil, fmt.Errorf("the catalog's versions of %s answered what is no list of versions: %w", module, err)
|
|
}
|
|
var out []string
|
|
for _, v := range a.Versions {
|
|
out = append(out, v.Version)
|
|
}
|
|
return out, nil
|
|
}}
|
|
return rv
|
|
}
|
|
|
|
var _ build.Reservations = (*reservations)(nil)
|
|
|
|
func (rv *reservations) Filled() bool { return rv.filled.Load() }
|
|
|
|
func (rv *reservations) Reserve(ctx context.Context, module, series string, artifacts []string, src versions.Source) (versions.ReservedVersion, error) {
|
|
return rv.r.Reserve(ctx, module, series, artifacts, src)
|
|
}
|
|
|
|
// Built records the digests; once every artifact has one, the publish's bound starts (a key of the state bounds with its
|
|
// own time to live): its end with the reservation standing is version-unreleased.
|
|
func (rv *reservations) Built(ctx context.Context, module, version string, digests map[string]string, deps map[string][]meshbuilds.Dependency) error {
|
|
for ref, d := range digests {
|
|
if _, err := rv.r.Built(ctx, module, version, ref, d, deps[ref]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
cur, found, err := rv.r.Of(ctx, module, version)
|
|
if err != nil || !found || cur.BuiltAt.IsZero() {
|
|
return err
|
|
}
|
|
rv.arm(ctx, cur)
|
|
return nil
|
|
}
|
|
|
|
func (rv *reservations) arm(ctx context.Context, v versions.ReservedVersion) {
|
|
kv := rv.bounds()
|
|
var err error
|
|
if kv == nil {
|
|
err = fmt.Errorf("%s is not on the bus yet", versions.BoundsBucket)
|
|
} else if _, err = kv.Create(ctx, versions.ReservedBoundKey(v.Module, v.Series), versions.EncodeBound("the release of "+v.Module+" "+v.Version, time.Now().Add(rv.bound)), jetstream.KeyTTL(rv.bound)); errors.Is(err, jetstream.ErrKeyExists) {
|
|
err = nil
|
|
}
|
|
if err != nil {
|
|
rv.logf("the release bound of %s %s is not written, so it is raised unreleased now: %v", v.Module, v.Version, err)
|
|
rv.unreleased(ctx, v, fmt.Sprintf("its bound could not be written (%v)", err))
|
|
}
|
|
}
|
|
|
|
// unreleased raises version-unreleased about the module, naming the version and the artifacts missing.
|
|
func (rv *reservations) unreleased(ctx context.Context, v versions.ReservedVersion, why string) {
|
|
missing := v.Missing()
|
|
what := "an artifact is unpublished"
|
|
if len(missing) > 0 {
|
|
what = strings.Join(missing, ", ") + " unpublished"
|
|
}
|
|
rv.found.raise(ctx, finding{kind: versions.VersionUnreleased, scope: meshconditionsclient.ScopeModule, id: v.Module,
|
|
summary: fmt.Sprintf("%s %s is built and not released: %s (%s)", v.Module, v.Version, what, why),
|
|
headline: "A built version is not released",
|
|
explanation: "The builder reserved " + v.Module + " " + v.Version + " and its build ended, and the catalog has not said version-released: " + what + ".",
|
|
resolved: "The catalog says version-released for " + v.Module + " " + v.Version + ".",
|
|
needs: "builder_reserved shows the reservation; builder_publish_again publishes the missing artifacts under it; builder_reserved_drop drops it."})
|
|
}
|
|
|
|
// ended is told each change of the state bounds: a reservation's bound whose time ended raises version-unreleased while
|
|
// the reservation stands built.
|
|
func (rv *reservations) ended(ctx context.Context, e jetstream.KeyValueEntry) bool {
|
|
key, ok := versions.ReservedBoundOf(e.Key())
|
|
if !ok {
|
|
return false
|
|
}
|
|
if e.Operation() != jetstream.KeyValuePurge {
|
|
return true
|
|
}
|
|
v, found, err := rv.r.At(ctx, key)
|
|
if err != nil || !found || v.BuiltAt.IsZero() {
|
|
return true
|
|
}
|
|
rv.unreleased(ctx, v, fmt.Sprintf("nothing within %s of its build", rv.bound))
|
|
return true
|
|
}
|
|
|
|
// dropBound ends a reservation's bound.
|
|
func (rv *reservations) dropBound(ctx context.Context, key string) {
|
|
if kv := rv.bounds(); kv != nil {
|
|
if err := kv.Delete(ctx, versions.ReservedBoundFor(key)); err != nil && !errors.Is(err, jetstream.ErrKeyNotFound) {
|
|
rv.logf("the release bound of %s is not ended: %v", key, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// heard takes the catalog's version-released and versions-filled in, and a registry seat's published (published): a
|
|
// message that is none of them is left to the others. A state that cannot take one leaves it for the bus to give again.
|
|
func (rv *reservations) heard(ctx context.Context, m jetstream.Msg) bool {
|
|
switch m.Subject() {
|
|
case meshcatalog.SubjectVersionsFilled:
|
|
rv.filled.Store(true)
|
|
rv.logf("the catalog said versions-filled: versions are reserved from now on")
|
|
if rv.wake != nil {
|
|
rv.wake(ctx)
|
|
}
|
|
case meshcatalog.SubjectVersionReleased:
|
|
var ev meshcatalog.VersionReleasedEvent
|
|
if err := json.Unmarshal(m.Data(), &ev); err != nil || ev.Module == "" || ev.Version == "" {
|
|
rv.logf("%s holds what is no version-released, left: %v", m.Subject(), err)
|
|
break
|
|
}
|
|
if err := rv.tagged(ctx, ev); err != nil {
|
|
rv.logf("%v; heard again", err)
|
|
if nakErr := m.Nak(); nakErr != nil {
|
|
rv.logf("leaving %s for the bus to give again: %v", m.Subject(), nakErr)
|
|
}
|
|
return true
|
|
}
|
|
gone, err := rv.r.Released(ctx, ev.Module, ev.Version)
|
|
if err != nil {
|
|
rv.logf("%v; heard again", err)
|
|
if nakErr := m.Nak(); nakErr != nil {
|
|
rv.logf("leaving %s for the bus to give again: %v", m.Subject(), nakErr)
|
|
}
|
|
return true
|
|
}
|
|
for _, k := range gone {
|
|
rv.dropBound(ctx, k)
|
|
}
|
|
rv.found.clear(ctx, versions.VersionUnreleased, meshconditionsclient.ScopeModule, ev.Module)
|
|
default:
|
|
return false
|
|
}
|
|
if err := m.Ack(); err != nil {
|
|
rv.logf("acknowledging %s: %v", m.Subject(), err)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// published marks the artifacts of the reservation a registry seat's published names (its version and digest).
|
|
func (rv *reservations) published(ctx context.Context, o versions.Outcome) {
|
|
marked, err := rv.r.Published(ctx, o.Version, o.Digest)
|
|
if err != nil {
|
|
rv.logf("a published of %s@%s is not recorded in the reservations: %v", o.Name, o.Version, err)
|
|
}
|
|
// Each artifact the holder's published newly marked is said as the seat mesh-builds' package-published (design 47 G §52, §334).
|
|
for _, m := range marked {
|
|
if rv.say == nil {
|
|
continue
|
|
}
|
|
if err := rv.say(ctx, m.PackagePublished(o.Digest, time.Now())); err != nil {
|
|
rv.logf("the package-published of %s at %s is not said: %v", m.Artifact, m.Reserved.Version, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// dropped is builder_reserved_drop: the reservation is deleted and its bound ended.
|
|
func (rv *reservations) dropped(ctx context.Context, module, series, by, why string, at time.Time) (string, error) {
|
|
ok, err := rv.r.Drop(ctx, module, series)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !ok {
|
|
return "", fmt.Errorf("%s holds no reservation of %s", module, series)
|
|
}
|
|
rv.dropBound(ctx, versions.ReservedKey(module, series))
|
|
rv.found.clear(ctx, versions.VersionUnreleased, meshconditionsclient.ScopeModule, module)
|
|
return fmt.Sprintf("the reservation of %s %s is dropped (%s, %s)", module, series, by, why), nil
|
|
}
|
|
|
|
// tagged writes the git tag of a released version (design 47 G §370): on the catalog's version-released, through the seat
|
|
// git's verb tag, at the commit the reservation of that version was made from. A version with no reservation (a superseded
|
|
// build's version is never released, and the key of a version released by another builder is gone) or one that names no
|
|
// tag (a module with no Go package) is tagged nowhere. A tag the forge could not write is an error, the message heard again.
|
|
func (rv *reservations) tagged(ctx context.Context, ev meshcatalog.VersionReleasedEvent) error {
|
|
res, found, err := rv.r.Of(ctx, ev.Module, ev.Version)
|
|
if err != nil {
|
|
return fmt.Errorf("the reservation of %s %s cannot be read to tag it: %w", ev.Module, ev.Version, err)
|
|
}
|
|
if !found || res.Tag == "" || res.Commit == "" {
|
|
return nil
|
|
}
|
|
raw, err := rv.call("seat:"+git.Seat+".tag", git.TagInput{Repository: res.Repository, Commit: res.Commit, Tag: res.Tag})
|
|
if err != nil {
|
|
return fmt.Errorf("the tag %s of %s is not written: %w", res.Tag, ev.Module, err)
|
|
}
|
|
var a git.TagAnswer
|
|
if err := json.Unmarshal(raw, &a); err != nil {
|
|
return fmt.Errorf("the git seat answered what is no tag answer for %s: %w", res.Tag, err)
|
|
}
|
|
if a.Error != "" || !a.Performed {
|
|
return fmt.Errorf("the tag %s of %s is not written: %s (%s)", res.Tag, ev.Module, a.Error, a.ErrorKind)
|
|
}
|
|
rv.logf("%s is tagged %s at %s", ev.Module, res.Tag, res.Commit)
|
|
return nil
|
|
}
|