Files
mesh-host/internal/selfcompose/entries_test.go
T
jschoubben 43c812e98e selfcompose: a module placed on another node and not heard running is laid from its placed or trunk build
Being heard running is no condition: such a module counts as one whose
running build has no entry (wantedBuilds, askTrunkBuilds, layModules,
lacksEntry), so the store holder is found from the build its placement names or
its trunk build.

Builds: design 47 G-22 A-461d, A-461b
2026-11-17 16:06:01 +01:00

400 lines
17 KiB
Go

package selfcompose
import (
"context"
"encoding/json"
"errors"
"fmt"
"runtime"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient"
"git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
nodeengine "git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"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"
"github.com/novox/mesh-host/internal/agent"
"github.com/novox/mesh-host/internal/buckets"
"github.com/novox/mesh-host/internal/buckets/bucketstest"
"github.com/novox/mesh-host/internal/compositions"
)
// Design 47 G §421, §683, §698 (the lead's answers A-47, A-50): the node-engine reads the entry its want names by the
// fingerprint, through its own calls of the seat mesh-catalog's verb entry, and reads neither the indexer's states nor the
// builder's; a want whose entry the catalog half holds none of is held, its have saying entry-missing and naming the
// fingerprint; the entries and wants it composes from are kept in its composition state, written before the composition that
// uses them and read at its start before anything is asked; a want removed takes its kept want and entry away.
func composeFromEntries(t *testing.T) {
b := newFakeBus()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64)
ghost := meshcatalogclient.SourcePrefix2 + strings.Repeat("b", 64)
var manifest map[string]any
if err := json.Unmarshal([]byte(lemurs), &manifest); err != nil {
t.Fatal(err)
}
entry := meshcatalog.CatalogEntry{Module: "lemurs", Fp: fp, Manifest: manifest, Repository: "novox/x", Commit: "c",
ManifestDigest: meshcatalogclient.ManifestDigest([]byte(lemurs))}
var mu sync.Mutex
var asked []string
ask := func(_ context.Context, seat, verb string, args any) (json.RawMessage, error) {
in := args.(meshcatalog.EntryInput)
mu.Lock()
asked = append(asked, in.Fingerprint)
mu.Unlock()
if seat != "mesh-catalog" || verb != "entry" {
t.Errorf("asked %s.%s", seat, verb)
}
if in.Fingerprint != fp {
return nil, errors.New("no entry of fingerprint " + in.Fingerprint + " is kept")
}
return json.Marshal(meshcatalog.EntryAnswer{Entry: entry})
}
state := &fakeComposed{kv: map[string]ComposedEntry{}}
var held map[string]string
env := b.env()
env.Entry, env.ComposedState = EntryCall(ask), state
refused := make(chan struct{}, 1) // fed at each refusal the composer says, so a wait follows what it says, not a pause
env.Refused = func(_ string, h map[string]string) {
mu.Lock()
held = h
mu.Unlock()
select {
case refused <- struct{}{}:
default:
}
}
told := map[string]bool{}
env.EntryNotFound = func(fp string, notFound bool) { mu.Lock(); told[fp] = notFound; mu.Unlock() }
c := New(env)
out, err := c.Watch(ctx, "n1.>")
if err != nil {
t.Fatal(err)
}
place := assignedValues(t, meshmoduleassigner.Assignments{Node: "n1", Decided: meshmoduleassigner.NodeDecided{Hub: true, Address: "10.77.0.1"},
Modules: []meshmoduleassigner.PlacedModule{{Module: "lemurs"}, {Module: "ghost"}}})
have, _ := nodeengineclient.EncodeHaveNode(nodeengine.HaveByNodeNodeValue{Record: &nodeengine.NodeRecord{Node: "n1", TunnelKey: "k", SealingKey: "sk", Capabilities: []string{"systemd"}}})
nodeWant, _ := bucketstest.NodeWant(meshwants.WantsByWantNodeNodeValue{Self: true})
b.putAssigned(t, place)
b.put(t, buckets.Have, "n1.node", have)
b.put(t, buckets.Wants, "want.n1.node", nodeWant)
for module, w := range map[string]meshwants.WantsByWantNodeModuleValue{"lemurs": {Fp: fp, Self: true}, "ghost": {Fp: ghost, Self: true}} {
raw, _ := bucketstest.Want(w)
b.put(t, buckets.Wants, "want.n1."+module, raw)
}
for _, bucket := range []string{buckets.Wants, buckets.Assignments, buckets.Have, ResolvedBucket} {
b.send(bucket, agent.Update{Caught: true})
}
lemursKey, _ := compositions.Key("n1", "lemurs", fp)
handed := map[string]bool{}
for !handed[lemursKey] {
handed[next(t, out).Key] = true
}
// Neither the indexer's states nor the builder's are watched or read.
b.mu.Lock()
for _, r := range b.reads {
if r[0] == buckets.Modules || r[0] == buckets.Manifests || r[0] == buckets.Verdicts || strings.HasSuffix(r[0], "_builds") {
t.Errorf("the node-engine read %s of %s", r[1], r[0])
}
}
b.mu.Unlock()
// A want whose entry the half holds none of is held, saying entry-missing and the fingerprint; the rest is composed.
// The ghost's entry is asked first and answered not found after: wait until the refusal says entry-missing.
var why string
for deadline := time.After(10 * time.Second); !strings.HasPrefix(why, agent.EntryMissing); {
mu.Lock()
why = held["ghost"]
mu.Unlock()
if strings.HasPrefix(why, agent.EntryMissing) {
break
}
select {
case <-refused:
case <-deadline:
t.Fatalf("the ghost's reason is still %q after 10 s, want %s", why, agent.EntryMissing)
}
}
if !strings.HasPrefix(why, agent.EntryMissing) || !strings.Contains(why, ghost) || held["lemurs"] != "" {
t.Errorf("held %q, want ghost held with %s naming %s", held, agent.EntryMissing, ghost)
}
// And the finding entry-not-found is told for the fingerprint the half holds none of, and for no other (design 47 G §614, A-117).
mu.Lock()
nf, nfOther := told[ghost], told[fp]
mu.Unlock()
if !nf || nfOther {
t.Errorf("entry-not-found told %v for the ghost and %v for the entry held", nf, nfOther)
}
// An entry asked again by hand is asked again in the shadow's cache too (lead answer A-319): the shadow holds the ghost as not
// found, the half then holds it, and the node's own composer asked again takes it in on the shadow as well.
var late atomic.Bool
shadowEnv := b.env()
shadowEnv.Entry = EntryCall(func(ctx context.Context, seat, verb string, args any) (json.RawMessage, error) {
if in := args.(meshcatalog.EntryInput); in.Fingerprint == ghost && late.Load() {
return json.Marshal(meshcatalog.EntryAnswer{Entry: meshcatalog.CatalogEntry{Module: "ghost", Fp: ghost}})
}
return ask(ctx, seat, verb, args)
})
shadow := NewShadow(shadowEnv, nil)
shadow.c.entries.Need(ctx, ghost)
for deadline := time.Now().Add(10 * time.Second); shadow.c.entries.Rows() == nil || shadow.c.entries.Rows()[0].State != meshcatalogclient.EntryNotFound; {
if time.Now().After(deadline) {
t.Fatal("the shadow did not hold the ghost as not found")
}
runtime.Gosched()
}
late.Store(true)
c.AlsoEntryAgain = shadow.EntryAgain
if err := c.EntryAgain(ctx, ghost); err != nil {
t.Fatal(err)
}
if _, state := shadow.c.entries.Need(ctx, ghost); state != meshcatalogclient.EntryAnswered {
t.Errorf("the shadow holds the ghost's entry as %s after entry-again, want answered", state)
}
// Each entry and want it composes from is kept in the composition state before the composition that uses them.
state.mu.Lock()
_, hasEntry := state.kv[entryKey(fp)]
_, hasWant := state.kv["want.lemurs"]
state.mu.Unlock()
if !hasEntry || !hasWant {
t.Errorf("the composition state holds entry %v and want %v of lemurs: %v", hasEntry, hasWant, state.written())
}
// The seat's first-views lists them.
_, entries, wants := c.FirstViews()
if len(entries) < 2 || len(wants) != 2 {
t.Errorf("first-views lists %v and %v", entries, wants)
}
// A restart reads what is kept before it asks anything: lemurs's entry is not asked again.
mu.Lock()
asked = nil
mu.Unlock()
env2 := b.env()
env2.Entry, env2.ComposedState = EntryCall(ask), state
// The node's own record, stated by the node-engine of itself in memory (A-120).
env2.NodeRecord = func() (nodeengine.NodeRecord, bool) {
return nodeengine.NodeRecord{Node: "n1", TunnelKey: "k", SealingKey: "sk", Capabilities: []string{"systemd"}}, true
}
c2 := New(env2)
out2, err := c2.Watch(ctx, "n1.>")
if err != nil {
t.Fatal(err)
}
// And composes the module it kept a want and an entry of before any input is read, nothing of the others (design 47 G §683, A-117).
if u := next(t, out2); u.Key != lemursKey || u.Deleted {
t.Errorf("the early pass handed %q, want the composition %s of lemurs", u.Key, lemursKey)
}
c2.mu.Lock()
kept, keptWant := c2.keptEnt[fp], c2.wants["lemurs"].Fp
c2.mu.Unlock()
if kept.entry.Module != "lemurs" || keptWant != fp {
t.Errorf("a restart read entry %q and want %q from the state", kept.entry.Module, keptWant)
}
// A want removed takes its kept want and entry away.
raw, _ := bucketstest.Want(meshwants.WantsByWantNodeModuleValue{Fp: fp, Removed: true})
b.put(t, buckets.Wants, "want.n1.lemurs", raw)
for range 20 {
state.mu.Lock()
_, hasWant = state.kv["want.lemurs"]
_, hasEntry = state.kv[entryKey(fp)]
state.mu.Unlock()
if !hasWant && !hasEntry {
break
}
next(t, out)
}
if hasWant || hasEntry {
t.Errorf("a removed want left its kept want %v and entry %v", hasWant, hasEntry)
}
}
// catalogHalf is the catalog half's entries a test composes from, by fingerprint, answered through the seat mesh-catalog's verbs
// entry and module (design 47 G §421, A-312): the node-engine reads no indexer.
type catalogHalf map[string]meshcatalog.CatalogEntry
// add holds the entry of module at fp, made of the manifest body.
func (h catalogHalf) add(t *testing.T, module, fp, body string) {
t.Helper()
var m map[string]any
if err := json.Unmarshal([]byte(body), &m); err != nil {
t.Fatal(err)
}
h[fp] = meshcatalog.CatalogEntry{Module: module, Fp: fp, Manifest: m, Repository: "novox/" + module, Commit: "c", ManifestDigest: meshcatalogclient.ManifestDigest([]byte(body))}
}
func (h catalogHalf) call() meshcatalogclient.Call {
return EntryCall(func(_ context.Context, _, verb string, args any) (json.RawMessage, error) {
switch in := args.(type) {
case meshcatalog.ModuleInput:
for _, e := range h {
if e.Module == in.Module {
return json.Marshal(meshcatalog.ModuleAnswer{Module: meshcatalog.ModuleAnswerModule{Module: e.Module, Repository: e.Repository, Commit: e.Commit, Fp: e.Fp}})
}
}
return nil, fmt.Errorf("the catalog holds no module %s", in.Module)
case meshcatalog.EntryInput:
if e, ok := h[in.Fingerprint]; ok {
return json.Marshal(meshcatalog.EntryAnswer{Entry: e})
}
return nil, errors.New("no entry of fingerprint " + in.Fingerprint + " is kept")
}
return nil, fmt.Errorf("asked %s of %T", verb, args)
})
}
// holdEntry makes the composer hold the entry of module at fp, as its call of the verb entry would have answered it (§421), for the
// tests that set the rest of its picture directly.
func (c *Composer) holdEntry(t *testing.T, module, fp, body string) {
t.Helper()
half := catalogHalf{}
half.add(t, module, fp, body)
c.mu.Lock()
defer c.mu.Unlock()
if c.keptEnt == nil {
c.keptEnt = map[string]keptEntry{}
}
c.keptEnt[fp] = keptEntry{entry: half[fp]}
}
// A-405, A-407: a module the node runs whose wanted build has no entry stands, for resolution alone, as the build its want says
// is being built, at the want's fingerprint, so its want stays held; one not running here, or whose want names no newer build the
// half holds, stands as its name alone.
func runningModuleWithoutEntryStandsAsTheNewestBuild(t *testing.T) {
old := meshcatalogclient.SourcePrefix2 + strings.Repeat("1", 64)
newest := meshcatalogclient.SourcePrefix2 + strings.Repeat("2", 64)
body := `{"module":"postgres","version":"2"}`
for _, tc := range []struct {
name string
runs bool
newest string
holds bool
laid bool
wantFp string
}{
{"runs, newest entry held", true, newest, true, true, old},
{"not running here", false, newest, true, false, old},
{"names no newer build", true, "", false, false, old},
{"newest entry not held", true, newest, false, false, old},
} {
t.Run(tc.name, func(t *testing.T) {
env := newFakeBus().env()
env.Node = "n1"
c := New(env)
c.mu.Lock()
c.wants["postgres"] = meshwants.WantsByWantNodeModuleValue{Fp: old, Newest: tc.newest}
c.assignments["n1"] = meshmoduleassigner.Assignments{Node: "n1", Modules: []meshmoduleassigner.PlacedModule{{Module: "postgres"}}}
if tc.runs {
c.heard = map[string]map[string]heardRun{"n1": {"postgres": {fingerprint: old}}}
}
c.mu.Unlock()
if tc.holds {
c.holdEntry(t, "postgres", newest, body)
}
if got := c.wantedBuilds()[newest]; tc.newest != "" && got {
t.Errorf("the newest build is named by a want, want only asked")
}
c.layModules()
c.mu.Lock()
m, ok := c.modules["postgres"]
raw := c.manifests[m.Manifest]
c.mu.Unlock()
if !ok || m.Fp2 != tc.wantFp {
t.Fatalf("modules[postgres] = %+v (%v), want fingerprint %s", m, ok, tc.wantFp)
}
if got := strings.Contains(string(raw), `"version":"2"`); got != tc.laid {
t.Errorf("manifest of the newest build laid = %v, want %v (%s)", got, tc.laid, raw)
}
})
}
}
// The node judges a move's data forms from the entry it kept of the build it runs, though the catalog half dropped that entry and no
// running event names it; and an answered entry has the composition handed again, which the agent waits on (novox, mesh-hand-acts).
func TestRunningBuildEntryKeptAndHandedAgain(t *testing.T) {
old := meshcatalogclient.SourcePrefix2 + strings.Repeat("1", 64)
next := meshcatalogclient.SourcePrefix2 + strings.Repeat("2", 64)
env := newFakeBus().env()
env.Node = "n1"
env.Running = func() []compositions.Composed {
return []compositions.Composed{{Node: "n1", Module: "hand", FP: old}}
}
c := New(env)
c.mu.Lock()
c.wants["hand"] = meshwants.WantsByWantNodeModuleValue{Fp: next}
c.mu.Unlock()
if named, ok := c.wantedBuilds()[old]; !ok || named {
t.Errorf("the running build %s is asked of no want yet kept: %v, %v", old, named, ok)
}
key, _ := compositions.Key("n1", "hand", next)
c.mu.Lock()
c.handed[key] = "d"
c.mu.Unlock()
c.entryChanged(meshcatalogclient.EntryRow{Fingerprint: old, State: meshcatalogclient.EntryAnswered,
Entry: &meshcatalog.CatalogEntry{Module: "hand", Fp: old}})
c.mu.Lock()
_, still := c.handed[key]
c.mu.Unlock()
if still {
t.Error("an entry of the running build was answered, and the composition was not handed again")
}
// A-461, A-461a, A-461b: another node's module whose running build has no entry is laid from the entry of the build the assignment
// places for it on that node, or, where the placement names none, of its trunk build; with neither entry it stays as its name
// alone, and this node's own module is never laid from a placement.
placedFp := meshcatalogclient.SourcePrefix2 + strings.Repeat("3", 64)
for _, tc := range []struct {
name string
node string
build, trunk string
holds, laid bool
notHeard bool // A-461d: nothing of what the node runs is heard
}{
{"placed build has an entry", "n2", placedFp, "", true, true, false},
{"neither build has an entry", "n2", placedFp, "", false, false, false},
{"placement names no build, trunk build has an entry", "n2", "", placedFp, true, true, false},
{"placement names no build, trunk names none", "n2", "", "", false, false, false},
{"this node's own module is not laid from a placement", "n1", placedFp, "", true, false, false},
{"placed, not heard, placement names no build, trunk build has an entry (A-461d)", "n2", "", placedFp, true, true, true},
{"placed, not heard, placed build has an entry (A-461d)", "n2", placedFp, "", true, true, true},
{"placed, not heard, trunk names none (A-461d)", "n2", "", "", false, false, true},
} {
t.Run(tc.name, func(t *testing.T) {
env := newFakeBus().env()
env.Node = "n1"
env.Entry = EntryCall(func(_ context.Context, _, verb string, _ any) (json.RawMessage, error) {
if verb == meshcatalog.VerbModule {
return json.Marshal(meshcatalog.ModuleAnswer{Module: meshcatalog.ModuleAnswerModule{Fp: tc.trunk}})
}
return nil, errors.New("not held")
})
c := New(env)
c.mu.Lock()
c.assignments[tc.node] = meshmoduleassigner.Assignments{Node: tc.node, Modules: []meshmoduleassigner.PlacedModule{{Module: "distribution", Build: tc.build}}}
c.heard = map[string]map[string]heardRun{tc.node: {"distribution": {fingerprint: old}}}
if tc.notHeard {
c.heard = map[string]map[string]heardRun{}
}
c.mu.Unlock()
if tc.holds {
c.holdEntry(t, "distribution", placedFp, `{"module":"distribution","version":"2"}`)
}
c.askTrunkBuilds()
if _, asked := c.wantedBuilds()[placedFp]; asked != (tc.node == "n2" && (tc.build != "" || tc.trunk != "")) {
t.Errorf("the placed build asked = %v", asked)
}
c.layModules()
c.mu.Lock()
m, ok := c.modules["distribution"]
raw := c.manifests[m.Manifest]
c.mu.Unlock()
if got := ok && strings.Contains(string(raw), `"version":"2"`); got != tc.laid {
t.Errorf("laid from the placed build = %v, want %v (%+v %s)", got, tc.laid, m, raw)
}
})
}
}