Files
mesh-delivery/modules/mesh-delivery-builder/internal/versions/tools.go
T
jschoubben 6210384d3a B1b: the builder's numberer and its state versions go; builder_versions answers from the reservations
The numbering from before G goes (design 47 H §756, G §372): the builder keeps
no state versions, marks no numbered entry on a registry seat's word and arms
no numbered version's publish bound; the publishes' findings and the
reservations still hear the same events (DQ-11), and a reserved version's bound
is the reservation's (DQ-10). builder_versions keeps answering, from the
reservations alone (DMC-5): what the seat mesh-builds' reserved answers for a
name and, with a version and a digest, whether that version is reserved or
released with that digest.

Builds: design 47 H §756, G §372, G §381, H2 H-A-532 (DMC-5, DQ-10, DQ-11), spec 29, 41, 48
2026-11-23 11:53:05 +01:00

171 lines
6.9 KiB
Go

package versions
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
)
// The builder's tools of its versions (design 51 *Events of the builder*, *Tools of the builder*).
const (
VersionsTool = "builder_versions"
PublishAgainTool = "builder_publish_again"
OldFormTool = "builder_old_form"
// OldFormClosedEvent is the builder's own family `version`, kept seven days,
// apart from its builds' ends, so a waiter on a build's end does not wake on them.
OldFormClosedEvent = "version.old-form-closed"
)
// Reader reads the state as the tools bundle may: every key, and one; and, where All is set, the state's values in one page,
// one request for many (design 47 H H-A-290 (5), H-A-292).
type Reader struct {
Keys func() ([]string, error)
Get func(key string) (json.RawMessage, bool, error)
// All is one page of the state's values; nil, or an error satisfying errors.Is(err, errors.ErrUnsupported), reads key by key.
All func() (StatePage, error)
}
// StatePage is one page of a state (H-A-292, H-A-293): its values, and, when the state holds more than the page, More with the
// state's size as its info says it (Keys, Bytes; InfoWords when the info cannot be read). No second page is asked.
type StatePage struct {
Values map[string]json.RawMessage
More bool
Keys, Bytes uint64
InfoWords string
}
// StateCapped is what an answer says of a state it read only the first page of (H-A-293): how many keys it read, and the
// state's size from its info.
type StateCapped struct {
State string `json:"state"`
KeysRead int `json:"keys-read"`
StateKeys uint64 `json:"state-keys"`
StateBytes uint64 `json:"state-bytes"`
InfoWords string `json:"info,omitempty"`
}
// values is the state's values: one page where the reader can (capped says what a page past the state's end left out), else
// key by key.
func (r Reader) values(state string) (map[string]json.RawMessage, *StateCapped, error) {
if r.All != nil {
pg, err := r.All()
if !errors.Is(err, errors.ErrUnsupported) {
if err != nil || !pg.More {
return pg.Values, nil, err
}
return pg.Values, &StateCapped{State: state, KeysRead: len(pg.Values), StateKeys: pg.Keys, StateBytes: pg.Bytes, InfoWords: pg.InfoWords}, nil
}
}
keys, err := r.Keys()
if err != nil {
return nil, nil, err
}
out := make(map[string]json.RawMessage, len(keys))
for _, k := range keys {
raw, ok, err := r.Get(k)
if err != nil || !ok {
continue
}
out[k] = raw
}
return out, nil, nil
}
// VersionsAnswer is what builder_versions answers, from the reservations alone (design 47 H §756, G §372, §381): what the
// seat mesh-builds' verb reserved answers for the name, and, asked for a version and a digest, whether the reservation holds
// that version with that digest.
type VersionsAnswer struct {
Name string `json:"name"`
// Asked, Recorded and Why answer a holder's question for one version and digest (ADR 0605 §4).
Asked string `json:"asked,omitempty"`
Recorded *bool `json:"recorded,omitempty"`
Why string `json:"why,omitempty"`
// Reserved is what the seat mesh-builds' verb reserved answers for the name (and version): the reserved or released version
// with each artifact's digest, or its error (design 47 G §572, §582).
Reserved *meshbuilds.ReservedAnswer `json:"reserved,omitempty"`
}
// AnswerVersions is builder_versions: what the reservation answers for a name, read through reserve (the seat mesh-builds'
// verb reserved); with a version and a digest, recorded says whether that version is the one reserved or released and an
// artifact of it has that digest or none recorded yet, the question a registry seat's holder asks before it stores
// (ADR 0605 §4, design 47 G §381, A-490j).
func AnswerVersions(name, version, digest string, reserve func(name, version string) (meshbuilds.ReservedAnswer, error)) (VersionsAnswer, error) {
name, version, digest = strings.TrimSpace(name), strings.TrimSpace(version), strings.TrimSpace(digest)
if name == "" {
return VersionsAnswer{}, errors.New("name is required: the package, image, bundle or archive name")
}
if reserve == nil {
return VersionsAnswer{}, errors.New("the seat mesh-builds' reserved cannot be asked")
}
ra, err := reserve(name, version)
if err != nil {
return VersionsAnswer{}, fmt.Errorf("the seat mesh-builds' reserved did not answer for %s: %w", name, err)
}
out := VersionsAnswer{Name: name, Reserved: &ra}
if version == "" {
return out, nil
}
ok, why := recordedIn(ra, name, version, digest)
out.Asked, out.Recorded, out.Why = version, &ok, why
return out, nil
}
// recordedIn is whether the reservation answer ra holds name@version with digest, and why not.
func recordedIn(ra meshbuilds.ReservedAnswer, name, version, digest string) (bool, string) {
switch {
case ra.Error != "":
return false, ra.Error
case ra.Version != version:
return false, fmt.Sprintf("%s has no reservation of version %s of %s (it holds %s)", ra.Module, version, name, ra.Version)
}
var held []string
for _, a := range ra.Artifacts {
if a.Digest == digest || a.Digest == "" {
return true, ""
}
held = append(held, a.Digest)
}
if len(held) == 0 {
return false, fmt.Sprintf("%s holds %s@%s with no artifact, so no digest takes %s", ra.Module, name, version, digest)
}
return false, fmt.Sprintf("%s holds %s@%s for digest %s, not %s", ra.Module, name, version, strings.Join(held, ", "), digest)
}
// VersionsDescription is builder_versions' description.
const VersionsDescription = "A name's reservation, from the reservations alone (the numbering from before G is gone, design 47 H §756): " +
"what the seat mesh-builds' verb reserved answers for the package, image, bundle or archive name, the reserved or released version " +
"with each artifact's digest. With version, and digest, it also answers recorded: whether that version is the one reserved or " +
"released and an artifact of it has that digest (or none recorded yet), and why not: the question a registry seat's holder asks " +
"before it stores a publish (ADR 0605 §4). Read-only."
// readStore is a Store over a Reader that refuses every write: the tools bundle only reads the builder's states.
type readStore struct{ r Reader }
var errReadOnly = errors.New("the builder's states are written by the builder alone")
func (s readStore) Get(_ context.Context, key string) ([]byte, uint64, bool, error) {
v, ok, err := s.r.Get(key)
return v, 0, ok, err
}
func (readStore) Create(context.Context, string, []byte) (uint64, error) { return 0, errReadOnly }
func (readStore) Update(context.Context, string, []byte, uint64) (uint64, error) {
return 0, errReadOnly
}
func (s readStore) KeysOf(_ context.Context, filter string) ([]string, error) {
all, err := s.r.Keys()
if err != nil {
return nil, err
}
var out []string
for _, k := range all {
if FilterMatches(filter, k) {
out = append(out, k)
}
}
return out, nil
}