Files
mesh-host/internal/selfcompose/versions.go
T
jschoubben 132b767043 H-A-150: forget only on the store's own answer, and on the apply's fetch too
A fetch nothing answered for (a bus or store that did not answer, a gateway
saying the store is away) proves nothing about the digest, so it is tried
again as before and the module is not held for it; only an answer of the
store's (a 404, bytes not of the digest) forgets the resolution. The apply's
fetch of a bundle meets the same wrong digest as the take-in's, so it tells
the composer too (apply.WithFetchFailed, set in the node agent's apply).

Builds: design 47 H H-A-150, H-A-146, G A-430a
2026-11-20 16:35:42 +01:00

266 lines
10 KiB
Go

package selfcompose
import (
"encoding/json"
"fmt"
"maps"
"slices"
"sort"
"strings"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry"
"git.novox.be/novox/mesh-sdk/go/buscalls"
"git.novox.be/novox/mesh-sdk/go/catalogue"
)
// **A want names its version; the node-engine resolves the digests** (design 47 G A-430, A-430a, the lead's answer A-430b).
// A want of a released build names the module's version and no parts: each artifact of the build's entry is looked up at that
// version through the verb `versions` of the registry seat of its kind (oci-artifact-registry for a bundle or an archive,
// container-image-registry for an image), under the name the builder numbered it by: `<module>/<artifact>`, or the entry's
// package name for a package. An upstream copy is not numbered (it is mirrored by its pinned digest): its digest is the one its
// manifest's `from` pins. A want that names parts (a build no version is known of) is read by its parts as before. A lookup that
// fails is said in the journal, naming the module, the version and the seat, and the module is held whole: never composed partly.
// resolveAgain is how long a failed lookup stands before it is asked again; a pass is asked then.
const resolveAgain = time.Minute
// versionsVerb is the verb a lookup asks.
const versionsVerb = "versions"
// resolvedKey names one resolution: a module's build at a version.
type resolvedKey struct{ module, fp, version string }
// versionParts is a module's build at a version as resolved: its digests by artifact reference, or why not, and when that was found.
// forgotten marks a resolution whose fetch failed (H-A-146): it stands as a failed one, and is asked again at the module's next want,
// at the next compose by hand, or after resolveAgain.
type versionParts struct {
parts map[string]string
failed string
at time.Time
forgotten bool
}
// registryVersions is the answer of either seat's verb `versions`: the two seats answer the same shape.
type registryVersions struct {
Versions []struct {
Digest string `json:"digest"`
Tag string `json:"tag,omitempty"`
Version string `json:"version,omitempty"`
} `json:"versions"`
}
// seatOfKind is the registry seat an artifact of kind is numbered in, and whether one is (design 51 *The seats*).
func seatOfKind(kind string) (string, bool) {
switch kind {
case catalogue.ArtifactImage:
return containerimageregistry.Seat, true
case catalogue.ArtifactBundle, catalogue.ArtifactArchive:
return ociartifactregistry.Seat, true
}
return "", false
}
// registryName is the name an artifact of module is numbered under: `<module>/<artifact>`, or a package's name in its
// ecosystem as the entry gives it.
func registryName(module string, a meshcatalog.Artifact) string {
_, artifact, _ := strings.Cut(a.Ref, ":")
if a.Kind == catalogue.ArtifactPackage && a.Package != nil && a.Package.Name != "" {
return a.Package.Name
}
return module + "/" + artifact
}
// partsOf is the digest of each artifact of e for want w, by artifact reference, and the artifacts it names no digest for ("" when
// each has one). A want names its version alone (design 47 H §750); its digests are resolved through the registry seats.
func (c *Composer) partsOf(module string, e meshcatalog.CatalogEntry, w meshwants.WantsByWantNodeModuleValue) (map[string]string, string) {
var parts map[string]string
if w.Version != "" && len(e.Artifacts) > 0 {
var why string
if parts, why = c.resolve(module, e, w); why != "" {
return nil, why
}
}
return parts, partsMissing(e, parts)
}
// partsMissing names the artifacts of e whose digest parts does not give; "" when each has one.
func partsMissing(e meshcatalog.CatalogEntry, parts map[string]string) string {
var out []string
for _, a := range e.Artifacts {
if parts[a.Ref] == "" {
out = append(out, a.Ref)
}
}
sort.Strings(out)
return strings.Join(out, ", ")
}
// resolve is the digests of module's build at w's version, from what is resolved already or asked of the seats now. why names
// what failed; a failure stands resolveAgain, then is asked again.
func (c *Composer) resolve(module string, e meshcatalog.CatalogEntry, w meshwants.WantsByWantNodeModuleValue) (map[string]string, string) {
key := resolvedKey{module, w.Fp, w.Version}
now := c.now()
c.mu.Lock()
r, ok := c.resolved[key]
c.mu.Unlock()
if ok && (r.failed == "" || now.Sub(r.at) < resolveAgain) {
return r.parts, r.failed
}
parts, failed := c.lookUp(module, e, w)
c.mu.Lock()
if c.resolved == nil {
c.resolved = map[resolvedKey]versionParts{}
}
// Only the resolutions of the builds a want names now are kept.
for k := range c.resolved {
if cur, wanted := c.wants[k.module]; k.module != module && (!wanted || cur.Fp != k.fp || cur.Version != k.version) {
delete(c.resolved, k)
}
}
c.resolved[key] = versionParts{parts: parts, failed: failed, at: now}
c.mu.Unlock()
if failed != "" {
time.AfterFunc(resolveAgain, c.askRehand)
return nil, failed
}
return parts, ""
}
// forgetFetched forgets every resolution that names digest, as its fetch failed for why (design 47 H, the lead's answer H-A-146; A-430a:
// the bytes are fetched by the digest the versions answer gives): the store's answer may have been wrong, and a resolution kept for
// the process's life would never see it fixed. Each is said in the journal; the module is held until the seat's verb versions is
// asked again, at its next want, at the next compose by hand, or after resolveAgain. A resolution whose fetch succeeded is kept.
func (c *Composer) forgetFetched(digest, why string) {
if digest == "" {
return
}
now := c.now()
var lines []string
c.mu.Lock()
for k, r := range c.resolved {
if r.failed != "" || !slices.Contains(slices.Collect(maps.Values(r.parts)), digest) {
continue
}
line := fmt.Sprintf("resolution of %s %s forgotten: %s", k.module, k.version, why)
c.resolved[k] = versionParts{failed: line + "; the seats' verb versions is asked again at its next want, at the next compose or in " + resolveAgain.String(),
at: now, forgotten: true}
lines = append(lines, line)
}
c.mu.Unlock()
sort.Strings(lines)
for _, line := range lines {
c.env.Say(line)
}
if len(lines) > 0 {
time.AfterFunc(resolveAgain, c.askRehand)
}
}
// ForgetFetched is forgetFetched for the apply's fetch of a bundle the store answered and that failed (H-A-150, apply.FetchFailed).
func (c *Composer) ForgetFetched(digest, why string) { c.forgetFetched(digest, why) }
// dropForgottenLocked ends the standing of every forgotten resolution (H-A-146), of module alone or, for "", of all: the next
// pass asks the seats' verb versions again. Under mu.
func (c *Composer) dropForgottenLocked(module string) {
for k, r := range c.resolved {
if r.forgotten && (module == "" || k.module == module) {
delete(c.resolved, k)
}
}
}
// lookUp asks each artifact's digest at w's version of the seat of its kind. Every failure is said, naming the module, the version
// and the seat; any failure resolves nothing of the build.
func (c *Composer) lookUp(module string, e meshcatalog.CatalogEntry, w meshwants.WantsByWantNodeModuleValue) (map[string]string, string) {
parts := map[string]string{}
var failed []string
fail := func(seat, ref, why string) {
line := fmt.Sprintf("%s at version %s: the seat %s's verb versions resolves no digest of %s: %s, so the node holds %s and composes nothing of it",
module, w.Version, seat, ref, why, module)
c.sayOnceEach(line)
failed = append(failed, fmt.Sprintf("%s (seat %s: %s)", ref, seat, why))
}
var manifest *catalogue.Manifest
for _, a := range e.Artifacts {
_, artifact, _ := strings.Cut(a.Ref, ":")
if a.Kind == catalogue.ArtifactUpstream {
if manifest == nil {
_, raw, err := entryManifest(e)
m, derr := catalogue.Decode(raw)
if err != nil || derr != nil {
fail("(none)", a.Ref, "the entry's manifest cannot be read for the digest its upstream copy pins")
continue
}
manifest = &m
}
if d := pinnedDigest(*manifest, artifact); d != "" {
parts[a.Ref] = d
} else {
fail("(none)", a.Ref, "the manifest pins no digest of the upstream copy")
}
continue
}
seat, numbered := seatOfKind(a.Kind)
if !numbered {
fail("(none)", a.Ref, "no registry seat the node-engine reads numbers an artifact of kind "+a.Kind)
continue
}
digest, why := c.versionDigest(seat, registryName(module, a), w)
if why != "" {
fail(seat, a.Ref, why)
continue
}
parts[a.Ref] = digest
}
if len(failed) > 0 {
sort.Strings(failed)
return nil, "no digest is resolved at version " + w.Version + " for " + strings.Join(failed, ", ")
}
return parts, ""
}
// versionDigest is the digest the seat's verb versions answers for name at w's version (its version or its tag), the newest
// first: a version names one build (A-430a), so the version alone is matched.
func (c *Composer) versionDigest(seat, name string, w meshwants.WantsByWantNodeModuleValue) (string, string) {
if c.env.Registry == nil {
return "", "this node-engine reads no registry seat"
}
raw, err := c.env.Registry("seat:"+seat+"."+versionsVerb, map[string]string{"name": name})
if err != nil {
return "", fmt.Sprintf("the call for %s failed: %v", name, err)
}
answer, said, ok := buscalls.Unwrap(raw)
if !ok {
return "", fmt.Sprintf("the seat refused %s: %s", name, said)
}
var v registryVersions
if err := json.Unmarshal(answer, &v); err != nil {
return "", fmt.Sprintf("its answer for %s cannot be read: %v", name, err)
}
for _, a := range v.Versions {
if (a.Version == w.Version || a.Tag == w.Version) && a.Digest != "" {
return a.Digest, ""
}
}
return "", fmt.Sprintf("it holds no version %s of %s", w.Version, name)
}
// pinnedDigest is the digest the manifest's upstream artifact pins in its from (`<image>@sha256:<hex>`); "" when none.
func pinnedDigest(m catalogue.Manifest, artifact string) string {
if m.Build == nil {
return ""
}
for _, a := range m.Build.Artifacts {
if a.Name == artifact {
if _, d, ok := strings.Cut(a.From, "@"); ok && strings.HasPrefix(d, "sha256:") {
return d
}
}
}
return ""
}