Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/versions_fill_edges.go
T
jschoubben 8d40d90950 Fill the catalog's versions from every number the builder gave, numbered or published (design 47 G §372, A-350)
The builder numbers fresh versions without publishing them, so a fill from published alone would number again
below what the builder already said, and one version would name two contents. The fill and its kind 1 and 3 edges
take numbered and published alike; the version-form check and the claimed cut stay. A version the builder numbered
without a digest is filled with none.

Builds: design 47 G §372, A-350, A-351

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-13 13:06:53 +01:00

160 lines
5.9 KiB
Go

package index
import (
"cmp"
"context"
"encoding/json"
"slices"
"strings"
"time"
"git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
"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-sdk/go/keys"
)
// The edge entries of the first fill (design 47 G §377, A-78 c): each module's newest version gets its entries once, each marked
// filled. Kinds 1 and 3 come from the dependency version published latest before the version's build was claimed, read with
// the times of builder_versions, and its part's digest; kinds 2, 4 and 6 from the version every machine placing the dependency
// runs, present or away, the machines from the assigner's placement (A-62) and each node's last running from the node-engine seat's
// stream read whole (§556); kind 5 as done for every acting part's running build.
// PlacementAsker asks the seat mesh-module-assigner's verb placement for a module (a read, A-62).
type PlacementAsker func(ctx context.Context, module string) ([]meshmoduleassigner.ModulePlacement, error)
// RunningReader reads the node-engine seat's stream running whole, every subject's last message (§556): by node, then module.
type RunningReader func(ctx context.Context) (map[string]map[string]nodeengine.Running, error)
// filledEdges are the edge entries of the entry's newest version, which was filled as built; claimed is when its build was claimed.
func (ix *Indexer) filledEdges(ctx context.Context, entry meshcatalog.CatalogEntry, claimed time.Time, ask VersionsAsker, trunk map[string]meshcatalog.CatalogEntry,
built map[string]meshcatalog.VersionEntry, running map[string]map[string]nodeengine.Running, answers map[string]BuilderVersionsAnswer) []meshcatalog.VersionEdge {
var out []meshcatalog.VersionEdge
seen := func(e meshcatalog.VersionEdge) bool {
return slices.ContainsFunc(out, func(x meshcatalog.VersionEdge) bool { return x.Kind == e.Kind && x.Part == e.Part && x.Word == e.Word })
}
for _, g := range entry.Edges {
part := cmp.Or(g.Part, g.From)
module := strings.SplitN(part, ":", 2)[0]
if part == "" || module == entry.Module {
continue
}
e := meshcatalog.VersionEdge{Kind: g.Kind, Part: part, Word: g.Word, Filled: true}
switch g.Kind {
case 1, 3:
dep, ok := trunk[module]
if !ok {
continue
}
var name string
for _, a := range dep.Artifacts {
if a.Ref == part {
name = versionNameOf(module, a)
}
}
if name == "" {
continue
}
ans, ok := answers[name]
if !ok {
a, err := ask(ctx, module, name)
if err != nil {
continue
}
ans, answers[name] = a, a
}
var best *BuilderVersion
for i, v := range ans.Versions {
if _, ok := versionParts(v.Version); !ok || !givenANumber(v.State) || v.At.After(claimed) {
continue
}
if best == nil || v.At.After(best.At) {
best = &ans.Versions[i]
}
}
if best == nil {
continue
}
e.Version, e.Digest, e.Stale = best.Version, best.Digest, true
case 2, 4, 6:
e.Version = notYetDelivered
if v, ok := ix.runningVersion(ctx, module, trunk, built, running); ok {
e.Version = v.Version
for _, a := range v.Artifacts {
if a.Artifact == part {
e.Digest = a.Digest
}
}
}
case 5:
e.Fact = "acted for the running build " + entry.Fp
default:
continue
}
if !seen(e) {
out = append(out, e)
}
}
return out
}
// runningVersion is the version of a module that every machine placing it runs, present or away, when each runs the newest
// build; false where no machine places it, a placed machine runs none or another build, or the placement is not answered.
func (ix *Indexer) runningVersion(ctx context.Context, module string, trunk map[string]meshcatalog.CatalogEntry, built map[string]meshcatalog.VersionEntry,
running map[string]map[string]nodeengine.Running) (meshcatalog.VersionEntry, bool) {
v, ok := built[module]
e, known := trunk[module]
if !ok || !known || e.Fp == "" || ix.Placement == nil {
return v, false
}
placements, err := ix.Placement(ctx, module)
if err != nil {
ix.logf("versions fill: the placement of %s is not answered: %v", module, err)
return v, false
}
nodes := 0
for _, p := range placements {
for _, n := range p.Nodes {
nodes++
r, ok := running[n][module]
if !ok || r.Stopped || r.Fingerprint != e.Fp {
return v, false
}
}
}
return v, nodes > 0
}
// keepFilledFacts keeps what the first fill derived of a version as heard: for each entry of kinds 2, 4 and 6 read as delivered,
// the dependency's version as its newest delivered one (A-68), and for each kind-5 edge the act for the running build (A-78 a).
func (ix *Indexer) keepFilledFacts(ctx context.Context, v meshcatalog.VersionEntry, entry meshcatalog.CatalogEntry, built map[string]meshcatalog.VersionEntry, at time.Time) {
for _, e := range v.Edges {
switch e.Kind {
case 2, 4, 6:
dep := built[strings.SplitN(e.Part, ":", 2)[0]]
if e.Version == notYetDelivered || dep.Version != e.Version {
continue
}
parts := map[string]string{}
for _, a := range dep.Artifacts {
parts[a.Artifact] = a.Digest
}
d := meshwants.VersionDeliveredEvent{V: 1, Module: dep.Module, Version: dep.Version, Parts: parts, At: &at}
if err := ix.keepNewest(ctx, DeliveredPrefix+keys.EscapeName(dep.Module), d, func([]byte) bool { return false }); err != nil {
ix.logf("versions fill: the delivered version of %s is not kept: %v", dep.Module, err)
}
case 5:
data, _ := json.Marshal(Acted{V: 1, Part: e.Part, Build: entry.Fp, How: "filled", At: at})
if err := write(ctx, ix.Heard, ActedKey(e.Part, v.Module), func(_ []byte, found bool) ([]byte, bool, error) {
if found {
return nil, false, nil
}
return data, false, nil
}); err != nil {
ix.logf("versions fill: the act for %s is not kept: %v", v.Module, err)
}
}
}
}