The holder of mesh-wants asks each node-engine compose-candidate as a kept call, so the same ask may come again on a transport fault. The engine reads the ask id from the request header, keeps it in its own state taken for compose-ask-bound (process.StateOver over the link it serves its seat on), and a repeat while the ask's run goes joins that run and gets the answer composed once; a repeat after the run ended is composed again, since the verb is a read and no answer is kept. The manifest declares taken with its 8 MiB cap. Builds: design 47 H2 §775, §820; H-A-351, H-A-364, H-A-383
351 lines
18 KiB
Go
351 lines
18 KiB
Go
package selfcompose
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"runtime"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient"
|
|
"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/buckets"
|
|
"github.com/novox/mesh-host/internal/buckets/bucketstest"
|
|
|
|
nec "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient"
|
|
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
|
|
|
"github.com/novox/mesh-host/internal/agent"
|
|
"github.com/novox/mesh-host/internal/compose"
|
|
)
|
|
|
|
// novox/hq ADR 0593 §7: the verb compose-candidate composes a check's candidate on this node in shadow. A candidate that
|
|
// changes a placed module composes it at the candidate's build, changed; one whose manifest the indexer refused is
|
|
// answered refused, naming the module and the judge's words, and the verdict is refused; a candidate the indexer does not
|
|
// hold is refused in words. Nothing is applied, nothing is written, no assignment answer is handed, and the answer comes
|
|
// within the bound; a shadow not ready and an ended bound answer unready at once, which the checker takes as no answer.
|
|
func TestTheNodeComposesACandidateOnAsk(t *testing.T) {
|
|
b := newFakeBus()
|
|
fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64)
|
|
fpNew := meshcatalogclient.SourcePrefix2 + strings.Repeat("b", 64)
|
|
digest := meshcatalogclient.ManifestDigest([]byte(lemurs))
|
|
lemurs2 := strings.Replace(lemurs, `"version":"1"`, `"version":"2"`, 1)
|
|
digest2 := meshcatalogclient.ManifestDigest([]byte(lemurs2))
|
|
for d, body := range map[string]string{digest: lemurs, digest2: lemurs2} {
|
|
man, _ := meshcatalogclient.EncodeManifest(meshcatalog.ManifestsByDigestValue{Module: "lemurs", Manifest: body})
|
|
key, _ := meshcatalogclient.ManifestsKey(d)
|
|
b.kv[buckets.Manifests+"/"+key] = man
|
|
}
|
|
// The indexer keys a candidate by its group alone (buckets.CandidateKey, the branch name escaped, a slash
|
|
// allowed), its heads hash inside the value; the checker asks by the composition key, `<group>.<heads hash>`.
|
|
candidateOf := func(group, manifest string) string {
|
|
head := meshdeliveryclient.Head{Repository: "novox/x", Number: 7, Head: "c0ffee" + manifest[len(manifest)-6:]}
|
|
heads, _ := meshdeliveryclient.HeadsHash([]meshdeliveryclient.Head{head})
|
|
pulls := []meshcatalog.CandidatesByGroupValuePullsItem{bucketstest.Pull(head, []string{"lemurs"},
|
|
map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{"lemurs": {Fp2: fpNew, Manifest: manifest}})}
|
|
raw, err := bucketstest.GroupCandidate(meshcatalog.CandidatesByGroupValue{Group: group, Heads: heads, Pulls: pulls,
|
|
Fpset: meshcatalogclient.FingerprintSet(pulls)})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ckey, _ := buckets.CandidateKey(group)
|
|
b.kv[buckets.Candidates+"/"+ckey] = raw
|
|
key, _ := nec.CompositionKey(group, heads)
|
|
return key
|
|
}
|
|
accepted := candidateOf("fix/940-no-retire-on-partial-read", digest2)
|
|
|
|
env := b.env()
|
|
state := &fakeComposed{kv: map[string]ComposedEntry{}}
|
|
env.ComposedState = state
|
|
var applied atomic.Int64
|
|
env.Refused = func(string, map[string]string) { applied.Add(1) }
|
|
// What the node's want names is read from the catalog half's entry (design 47 G §421, A-308): the shadow watches no modules.
|
|
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: digest}
|
|
// A-226b: the revision a branch pipeline names is read through the verb revision: revision 362 of main holds lemurs at its
|
|
// new build and manifest; no other revision is held.
|
|
env.Entry = EntryCall(func(_ context.Context, _, verb string, args any) (json.RawMessage, error) {
|
|
if verb == meshcatalog.VerbRevision {
|
|
in, _ := args.(meshcatalog.RevisionInput)
|
|
if in.Revision != 362 || in.Branch != "main" {
|
|
return json.Marshal(meshcatalog.RevisionAnswer{NoneYet: true})
|
|
}
|
|
moved := entry
|
|
moved.Fp, moved.ManifestDigest = fpNew, "sha256:"+digest2 // the entry carries the prefixed form
|
|
return json.Marshal(meshcatalog.RevisionAnswer{Revision: &meshcatalog.CatalogRevision{Branch: "main", Revision: 362}, Entries: []meshcatalog.CatalogEntry{moved}})
|
|
}
|
|
return json.Marshal(meshcatalog.EntryAnswer{Entry: entry})
|
|
})
|
|
// Design 47 H2 §775, H-A-364: an ask with an id is taken once in the state taken, for compose-ask-bound.
|
|
var takes []string
|
|
taken := map[string]bool{}
|
|
env.Taken = func(seat, ask string, bound time.Duration) (bool, error) {
|
|
takes = append(takes, seat+"."+ask+" "+bound.String())
|
|
first := !taken[ask]
|
|
taken[ask] = true
|
|
return first, nil
|
|
}
|
|
var handed atomic.Int64
|
|
s := NewShadow(env, func(context.Context, map[string]nodeengine.CandidateAnswer) { handed.Add(1) })
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go func() { _ = s.Run(ctx) }()
|
|
if a := s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: accepted}, ""); a.Verdict != nec.Unready || a.Refused == "" {
|
|
t.Errorf("a shadow not ready answered %+v", a)
|
|
}
|
|
place := assignedValues(t, meshmoduleassigner.Assignments{Node: "n1", Decided: meshmoduleassigner.NodeDecided{Hub: true, Address: "10.77.0.1"},
|
|
Modules: []meshmoduleassigner.PlacedModule{{Module: "lemurs"}}})
|
|
have, _ := nec.EncodeHaveNode(nodeengine.HaveByNodeNodeValue{Record: &nodeengine.NodeRecord{Node: "n1", TunnelKey: "k", SealingKey: "sk", Capabilities: []string{"systemd"}}})
|
|
want, _ := bucketstest.Want(meshwants.WantsByWantNodeModuleValue{Fp: fp, Self: true})
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
watching := func() int { b.mu.Lock(); defer b.mu.Unlock(); return len(b.watches) }
|
|
for watching() < 6 && time.Now().Before(deadline) {
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
b.putAssigned(t, place)
|
|
b.put(t, buckets.Have, "n1.node", have)
|
|
b.put(t, buckets.Wants, "want.n1.lemurs", want)
|
|
for _, bucket := range []string{buckets.ProposalCandidates, buckets.Assignments, buckets.Have, buckets.Wants, ResolvedBucket} {
|
|
b.send(bucket, agent.Update{Caught: true})
|
|
}
|
|
for !s.ready.Load() && time.Now().Before(deadline) {
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
|
|
a := s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: accepted}, "")
|
|
if _, err := nec.EncodeAnswer(a); err != nil {
|
|
t.Fatalf("the answer does not fit the seat's shape: %v (%+v)", err, a)
|
|
}
|
|
if a.Verdict != nec.Composed || len(a.Modules) != 1 || a.Modules[0].Module != "lemurs" ||
|
|
a.Modules[0].State != nec.ModuleComposed || a.Modules[0].Fp != fpNew || !a.Modules[0].Changed ||
|
|
a.Own.Module != nec.NodeWord || a.Took > nec.AnswerBound.Milliseconds() {
|
|
t.Errorf("an accepted candidate answered %+v", a)
|
|
}
|
|
// Design 47 H2 §775, H-A-364 (b): an ask with an id is taken once; given again after its run ended it is composed again,
|
|
// to the same answer, and nothing of the answer is kept.
|
|
for range 2 {
|
|
if a := s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: accepted}, "ask-1"); a.Verdict != nec.Composed ||
|
|
len(a.Modules) != 1 || a.Modules[0].Fp != fpNew {
|
|
t.Errorf("an ask with an id answered %+v", a)
|
|
}
|
|
}
|
|
if strings.Join(takes, ",") != "node-engine.ask-1 10s,node-engine.ask-1 10s" {
|
|
t.Errorf("the asks with an id were taken as %v; want node-engine.ask-1 for compose-ask-bound, each time", takes)
|
|
}
|
|
// Issue 774: the input it sends the sandbox says how it composed the node (the builds), so the sandbox composes it
|
|
// through the same path.
|
|
sent := s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: accepted, Input: true}, "")
|
|
var in compose.Input
|
|
raw, _ := json.Marshal(sent.Input)
|
|
if err := json.Unmarshal(raw, &in); err != nil || in.ComposedHow == nil || in.ComposedHow.Node != "n1" ||
|
|
in.ComposedHow.Builds["lemurs"] != fpNew {
|
|
t.Errorf("the input sent for the sandbox does not say how the node composes: %+v, %v", in.ComposedHow, err)
|
|
}
|
|
// A-226b: a branch pipeline's revision composes as the candidate, each module at the revision's build; one the catalog does
|
|
// not hold, and a run on request's key, are refused in words.
|
|
revKey, _ := nec.RevisionKey("main", 362)
|
|
rv := s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: revKey}, "")
|
|
if rv.Verdict != nec.Composed || len(rv.Modules) != 1 || rv.Modules[0].Fp != fpNew || !rv.Modules[0].Changed {
|
|
t.Errorf("a catalog revision answered %+v", rv)
|
|
}
|
|
noRev, _ := nec.RevisionKey("main", 999)
|
|
if m := s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: noRev}, ""); m.Verdict != nec.Refused || !strings.Contains(m.Refused, "holds no revision 999") {
|
|
t.Errorf("a revision the catalog does not hold answered %+v", m)
|
|
}
|
|
// A-226c: a run's key reads the indexer's candidates.run-<id> by its group, with no heads compared; one it holds none of is refused.
|
|
candidateOf("run-r1", digest2)
|
|
runKey, _ := nec.RunKey("r1")
|
|
if m := s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: runKey}, ""); m.Verdict != nec.Composed || len(m.Modules) != 1 || m.Modules[0].Fp != fpNew || !m.Modules[0].Changed {
|
|
t.Errorf("a run's key answered %+v", m)
|
|
}
|
|
noRun, _ := nec.RunKey("r2")
|
|
if m := s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: noRun}, ""); m.Verdict != nec.Refused || !strings.Contains(m.Refused, "holds no candidate") {
|
|
t.Errorf("a run the indexer holds none of answered %+v", m)
|
|
}
|
|
missing, _ := nec.CompositionKey("novox/x#8", "0123456789ab")
|
|
if m := s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: missing}, ""); !strings.Contains(m.Refused, "holds no candidate") {
|
|
t.Errorf("a candidate the indexer does not hold answered %+v", m)
|
|
}
|
|
// The group's candidate moved to other heads since the ask: the node composes not the one asked, and answers
|
|
// unready (no answer), naming the heads it holds.
|
|
moved, _ := nec.CompositionKey("fix/940-no-retire-on-partial-read", "0123456789ab")
|
|
if m := s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: moved}, ""); m.Verdict != nec.Unready ||
|
|
!strings.Contains(m.Refused, "heads") {
|
|
t.Errorf("a candidate at other heads answered %+v", m)
|
|
}
|
|
ended, end := context.WithCancel(ctx)
|
|
end()
|
|
if e := s.ComposeCandidate(ended, nodeengine.ComposeCandidateInput{Candidate: accepted}, ""); e.Verdict != nec.Unready {
|
|
t.Errorf("an ended bound answered %+v", e)
|
|
}
|
|
if len(state.written()) != 0 || applied.Load() != 0 || handed.Load() != 0 {
|
|
t.Errorf("composing candidates wrote %v, marked %d have(s), handed %d answer(s); want nothing", state.written(), applied.Load(), handed.Load())
|
|
}
|
|
// H-A-364 (b): a repeat of an ask while its run goes joins that run and gets the answer it composes once; one whose
|
|
// bound ends first answers unready; a repeat after the run ended composes again.
|
|
t.Run("a repeat of an ask joins the run it started", func(t *testing.T) {
|
|
var runs askRuns
|
|
var composed atomic.Int64
|
|
release, started := make(chan struct{}), make(chan struct{})
|
|
compose := func() nodeengine.ComposeCandidateAnswer {
|
|
if composed.Add(1) == 1 {
|
|
close(started)
|
|
<-release
|
|
}
|
|
return nodeengine.ComposeCandidateAnswer{Verdict: nec.Composed}
|
|
}
|
|
ended := func() nodeengine.ComposeCandidateAnswer { return nodeengine.ComposeCandidateAnswer{Verdict: nec.Unready} }
|
|
first := make(chan bool, 1)
|
|
go func() { _, joined := runs.once(ctx, "a", compose, ended); first <- joined }()
|
|
<-started
|
|
gone, cut := context.WithCancel(ctx)
|
|
cut()
|
|
if a, joined := runs.once(gone, "a", compose, ended); !joined || a.Verdict != nec.Unready {
|
|
t.Errorf("a repeat whose bound ended answered %+v (joined %v)", a, joined)
|
|
}
|
|
again := make(chan nodeengine.ComposeCandidateAnswer, 1)
|
|
go func() { a, _ := runs.once(ctx, "a", compose, ended); again <- a }()
|
|
for joined := 0; joined < 2; runtime.Gosched() { // the cut repeat, then this one
|
|
runs.mu.Lock()
|
|
joined = runs.runs["a"].joined
|
|
runs.mu.Unlock()
|
|
}
|
|
close(release)
|
|
if joined := <-first; joined {
|
|
t.Error("the first ask joined a run")
|
|
}
|
|
if a := <-again; a.Verdict != nec.Composed || composed.Load() != 1 {
|
|
t.Errorf("a repeat while the run went answered %+v after %d composition(s); want the one", a, composed.Load())
|
|
}
|
|
if _, joined := runs.once(ctx, "a", compose, ended); joined || composed.Load() != 2 {
|
|
t.Errorf("a repeat after the run ended joined %v, %d composition(s); want composed again", joined, composed.Load())
|
|
}
|
|
})
|
|
// Issue 1126, 2026-11-03: off the control-node an ask of compose-candidate was not answered within a minute, and said
|
|
// nothing of why. Each of its two compositions read every manifest in the mesh one by one, on copies of the shadow
|
|
// that kept nothing, so every ask read them all twice again. Each manifest is now read once, before either
|
|
// composition, into the shadow, and a later ask reads none; an ask whose read does not answer is answered unready at
|
|
// its bound, naming what the node waits on; and every answer is said in one line with what it waited on.
|
|
t.Run("each manifest is read once, and the answer comes within its bound", func(t *testing.T) {
|
|
fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64)
|
|
fpNew := meshcatalogclient.SourcePrefix2 + strings.Repeat("b", 64)
|
|
lemurs2 := strings.Replace(lemurs, `"version":"1"`, `"version":"2"`, 1)
|
|
other := strings.NewReplacer(`"module":"lemurs"`, `"module":"other"`, "lemurs.service", "other.service").Replace(lemurs)
|
|
digest, digest2, digestOther := meshcatalogclient.ManifestDigest([]byte(lemurs)), meshcatalogclient.ManifestDigest([]byte(lemurs2)),
|
|
meshcatalogclient.ManifestDigest([]byte(other))
|
|
otherKey, _ := meshcatalogclient.ManifestsKey(digestOther)
|
|
|
|
// shadow is a ready shadow of n1 over b: lemurs placed on it, other placed nowhere (its manifest read for the mesh's
|
|
// catalogue alone), and a candidate laying lemurs at a new build; its clock moves a second at each reading.
|
|
shadow := func(b *fakeBus) (*Shadow, string) {
|
|
for d, body := range map[string]string{digest: lemurs, digest2: lemurs2, digestOther: other} {
|
|
man, _ := meshcatalogclient.EncodeManifest(meshcatalog.ManifestsByDigestValue{Module: "x", Manifest: body})
|
|
key, _ := meshcatalogclient.ManifestsKey(d)
|
|
b.kv[buckets.Manifests+"/"+key] = man
|
|
}
|
|
head := meshdeliveryclient.Head{Repository: "novox/x", Number: 7, Head: "c0ffee"}
|
|
heads, _ := meshdeliveryclient.HeadsHash([]meshdeliveryclient.Head{head})
|
|
pulls := []meshcatalog.CandidatesByGroupValuePullsItem{bucketstest.Pull(head, []string{"lemurs"},
|
|
map[string]meshcatalog.CandidatesByGroupValuePullsItemFpsValue{"lemurs": {Fp2: fpNew, Manifest: digest2}})}
|
|
raw, _ := bucketstest.GroupCandidate(meshcatalog.CandidatesByGroupValue{Group: "fix/1126", Heads: heads, Pulls: pulls,
|
|
Fpset: meshcatalogclient.FingerprintSet(pulls)})
|
|
ckey, _ := buckets.CandidateKey("fix/1126")
|
|
b.kv[buckets.Candidates+"/"+ckey] = raw
|
|
env := b.env()
|
|
var tick atomic.Int64
|
|
env.Now = func() time.Time { return time.Unix(0, 0).Add(time.Duration(tick.Add(1)) * time.Second) }
|
|
s := NewShadow(env, nil)
|
|
s.c.assignments["n1"] = meshmoduleassigner.Assignments{Node: "n1", Decided: meshmoduleassigner.NodeDecided{Hub: true, Address: "10.77.0.1"},
|
|
Modules: []meshmoduleassigner.PlacedModule{{Module: "lemurs"}}}
|
|
s.c.records["n1"] = nodeengine.NodeRecord{Node: "n1", TunnelKey: "k", SealingKey: "sk", Capabilities: []string{"systemd"}}
|
|
s.c.modules["lemurs"] = meshcatalog.ModulesByModuleValue{Fp2: fp, Manifest: digest, Repository: "novox/x", Commit: "c"}
|
|
s.c.modules["other"] = meshcatalog.ModulesByModuleValue{Fp2: fp, Manifest: digestOther, Repository: "novox/x", Commit: "c"}
|
|
s.ready.Store(true)
|
|
key, _ := nec.CompositionKey("fix/1126", heads)
|
|
return s, key
|
|
}
|
|
readsOf := func(b *fakeBus, key string) (n int) {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
for _, r := range b.reads {
|
|
if r == [2]string{buckets.Manifests, key} {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
saidLast := func(b *fakeBus) string {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
if len(b.said) == 0 {
|
|
return ""
|
|
}
|
|
return b.said[len(b.said)-1]
|
|
}
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
// stuck: the read of other's manifest does not answer until the ask's bound ends.
|
|
stuck bool
|
|
asks int
|
|
verdict string
|
|
reads int // of other's manifest, over every ask
|
|
words string // in the answer's words
|
|
said string // in the line said of the last answer
|
|
}{
|
|
{name: "one ask reads each manifest once", asks: 1, verdict: nec.Composed, reads: 1,
|
|
said: "compose-candidate fix/1126."},
|
|
{name: "a later ask reads none again", asks: 2, verdict: nec.Composed, reads: 1,
|
|
said: "read(s) waited on"},
|
|
{name: "a read that does not answer is named at the bound", stuck: true, asks: 1, verdict: nec.Unready,
|
|
words: "waits on", said: "answered unready"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
b := newFakeBus()
|
|
s, key := shadow(b)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
if tc.stuck {
|
|
get := s.c.env.Get
|
|
s.c.env.Get = func(rctx context.Context, bucket, k string) ([]byte, error) {
|
|
if bucket == buckets.Manifests && k == otherKey {
|
|
b.mu.Lock()
|
|
b.reads = append(b.reads, [2]string{bucket, k})
|
|
b.mu.Unlock()
|
|
cancel() // the ask's bound ends while the read waits
|
|
<-rctx.Done()
|
|
return nil, rctx.Err()
|
|
}
|
|
return get(rctx, bucket, k)
|
|
}
|
|
}
|
|
var a nodeengine.ComposeCandidateAnswer
|
|
for range tc.asks {
|
|
a = s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: key}, "")
|
|
}
|
|
if a.Verdict != tc.verdict || !strings.Contains(a.Refused, tc.words) {
|
|
t.Errorf("answered %s %q; want %s with %q", a.Verdict, a.Refused, tc.verdict, tc.words)
|
|
}
|
|
if tc.stuck && !strings.Contains(a.Refused, "manifest") {
|
|
t.Errorf("answered %q; want the manifest it waits on named", a.Refused)
|
|
}
|
|
if got := readsOf(b, otherKey); tc.reads > 0 && got != tc.reads {
|
|
t.Errorf("other's manifest was read %d time(s); want %d", got, tc.reads)
|
|
}
|
|
if line := saidLast(b); !strings.Contains(line, tc.said) {
|
|
t.Errorf("said %q; want a line with %q", line, tc.said)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|