E2: take a compose-candidate ask once by its id
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
This commit is contained in:
@@ -98,10 +98,87 @@ func (r *candidateRun) said(now time.Time) (waits string, on string) {
|
||||
return waits, on
|
||||
}
|
||||
|
||||
// ComposeCandidate is the node's answer to ask, within candidateWithin (or ctx's deadline, if sooner). Each answer is
|
||||
// said in one line, with what it waited on and for how long (issue 1126: ace, g14 and shanks answered nothing for a
|
||||
// minute while nothing said why). An answer not composed within the bound is unready, naming what the node waits on.
|
||||
func (s *Shadow) ComposeCandidate(ctx context.Context, ask nodeengine.ComposeCandidateInput) nodeengine.ComposeCandidateAnswer {
|
||||
// askRuns are the compose-candidate runs going now, by their ask's id (design 47 H2 §775, H-A-364 (b)): compose-candidate
|
||||
// is a read, so a repeat of an ask while its run goes joins that run and gets the answer it composes once, and a repeat
|
||||
// after the run ended is composed again. No answer is kept.
|
||||
type askRuns struct {
|
||||
mu sync.Mutex
|
||||
runs map[string]*askRun
|
||||
}
|
||||
|
||||
// askRun is one ask's run: done is closed once a holds its answer; joined counts the repeats that joined it, under the
|
||||
// runs' lock.
|
||||
type askRun struct {
|
||||
done chan struct{}
|
||||
a nodeengine.ComposeCandidateAnswer
|
||||
joined int
|
||||
}
|
||||
|
||||
// once answers the ask id by compose, run once for every repeat of id that comes while it runs; joined says this call
|
||||
// joined a run another started. A joiner whose ctx ends first answers ended's answer.
|
||||
func (r *askRuns) once(ctx context.Context, id string, compose func() nodeengine.ComposeCandidateAnswer, ended func() nodeengine.ComposeCandidateAnswer) (a nodeengine.ComposeCandidateAnswer, joined bool) {
|
||||
r.mu.Lock()
|
||||
if run, ok := r.runs[id]; ok {
|
||||
run.joined++
|
||||
r.mu.Unlock()
|
||||
select {
|
||||
case <-run.done:
|
||||
return run.a, true
|
||||
case <-ctx.Done():
|
||||
return ended(), true
|
||||
}
|
||||
}
|
||||
if r.runs == nil {
|
||||
r.runs = map[string]*askRun{}
|
||||
}
|
||||
run := &askRun{done: make(chan struct{})}
|
||||
r.runs[id] = run
|
||||
r.mu.Unlock()
|
||||
defer func() {
|
||||
r.mu.Lock()
|
||||
delete(r.runs, id)
|
||||
r.mu.Unlock()
|
||||
close(run.done)
|
||||
}()
|
||||
run.a = compose()
|
||||
return run.a, false
|
||||
}
|
||||
|
||||
// ComposeCandidate is the node's answer to ask, carried with the ask id id, within candidateWithin (or ctx's deadline, if
|
||||
// sooner). An ask with an id is taken once (design 47 H2 §775, H-A-364): kept in the state taken for compose-ask-bound, and
|
||||
// a repeat while its run goes joins that run (askRuns); an ask with none is composed as it comes. Each answer is said in one
|
||||
// line, with what it waited on and for how long (issue 1126: ace, g14 and shanks answered nothing for a minute while
|
||||
// nothing said why). An answer not composed within the bound is unready, naming what the node waits on.
|
||||
func (s *Shadow) ComposeCandidate(ctx context.Context, ask nodeengine.ComposeCandidateInput, id string) nodeengine.ComposeCandidateAnswer {
|
||||
if id == "" {
|
||||
return s.composeCandidateNow(ctx, ask, "")
|
||||
}
|
||||
s.mu.Lock()
|
||||
c := s.c
|
||||
s.mu.Unlock()
|
||||
a, joined := s.asked.once(ctx, id, func() nodeengine.ComposeCandidateAnswer {
|
||||
took := ""
|
||||
if c.env.Taken != nil {
|
||||
switch first, err := c.env.Taken(nodeengine.Seat, id, c.composeAskBound()); {
|
||||
case err != nil:
|
||||
took = fmt.Sprintf(", not kept in taken: %v", err)
|
||||
case !first:
|
||||
took = ", taken before and its run ended, so composed again"
|
||||
}
|
||||
}
|
||||
return s.composeCandidateNow(ctx, ask, " (ask "+id+took+")")
|
||||
}, func() nodeengine.ComposeCandidateAnswer {
|
||||
return nodeengine.ComposeCandidateAnswer{Node: c.env.Node, Candidate: ask.Candidate, Verdict: nec.Unready,
|
||||
Refused: "the run of ask " + id + " this repeat joined did not end within the ask's bound"}
|
||||
})
|
||||
if joined {
|
||||
c.env.Say(fmt.Sprintf("compose-candidate %s (ask %s again) joined its run and answered %s", ask.Candidate, id, a.Verdict))
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// composeCandidateNow composes ask, its journal line naming its ask in about.
|
||||
func (s *Shadow) composeCandidateNow(ctx context.Context, ask nodeengine.ComposeCandidateInput, about string) nodeengine.ComposeCandidateAnswer {
|
||||
s.mu.Lock()
|
||||
c := s.c
|
||||
s.mu.Unlock()
|
||||
@@ -125,7 +202,7 @@ func (s *Shadow) ComposeCandidate(ctx context.Context, ask nodeengine.ComposeCan
|
||||
if a.Refused != "" {
|
||||
words = ": " + a.Refused
|
||||
}
|
||||
c.env.Say(fmt.Sprintf("compose-candidate %s answered %s in %s (%s)%s", ask.Candidate, a.Verdict,
|
||||
c.env.Say(fmt.Sprintf("compose-candidate %s%s answered %s in %s (%s)%s", ask.Candidate, about, a.Verdict,
|
||||
(time.Duration(a.Took) * time.Millisecond).String(), waits, words))
|
||||
return a
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package selfcompose
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
@@ -84,12 +85,21 @@ func TestTheNodeComposesACandidateOnAsk(t *testing.T) {
|
||||
}
|
||||
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 == "" {
|
||||
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"},
|
||||
@@ -111,7 +121,7 @@ func TestTheNodeComposesACandidateOnAsk(t *testing.T) {
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
|
||||
a := s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: accepted})
|
||||
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)
|
||||
}
|
||||
@@ -120,9 +130,20 @@ func TestTheNodeComposesACandidateOnAsk(t *testing.T) {
|
||||
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})
|
||||
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" ||
|
||||
@@ -132,43 +153,83 @@ func TestTheNodeComposesACandidateOnAsk(t *testing.T) {
|
||||
// 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})
|
||||
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") {
|
||||
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 {
|
||||
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") {
|
||||
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") {
|
||||
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 ||
|
||||
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 {
|
||||
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
|
||||
@@ -269,7 +330,7 @@ func TestTheNodeComposesACandidateOnAsk(t *testing.T) {
|
||||
}
|
||||
var a nodeengine.ComposeCandidateAnswer
|
||||
for range tc.asks {
|
||||
a = s.ComposeCandidate(ctx, nodeengine.ComposeCandidateInput{Candidate: key})
|
||||
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)
|
||||
|
||||
@@ -153,6 +153,9 @@ type Env struct {
|
||||
// ComposeAskBound is the setting `compose-ask-bound`: how long a compose waits for allowed; DefaultComposeAskBound when 0
|
||||
// (design 08 control-verbs §1, H-A-314 (5)).
|
||||
ComposeAskBound time.Duration
|
||||
// Taken takes an ask of the seat seat once by its id (design 47 H2 §775, H-A-364 (a)): kept in the node-engine's state
|
||||
// `taken` under `<seat>.<ask>` for bound; false when it was taken before. Nil keeps nothing.
|
||||
Taken func(seat, ask string, bound time.Duration) (bool, error)
|
||||
// Registry asks the registry seats' verb versions (oci-artifact-registry, container-image-registry) in the engine's own
|
||||
// name: a want naming its version is resolved to its artifacts' digests through it (design 47 G A-430a).
|
||||
Registry meshcatalogclient.Call
|
||||
|
||||
@@ -68,6 +68,8 @@ type Shadow struct {
|
||||
entryMoved chan struct{}
|
||||
// ready says this run read every input once: the verb compose-candidate composes only then (ComposeCandidate).
|
||||
ready atomic.Bool
|
||||
// asked are the compose-candidate runs going now, by their ask's id (H-A-364 (b)).
|
||||
asked askRuns
|
||||
}
|
||||
|
||||
// answeredAt is what one proposal's answer was composed at: its keys' revisions, by node, and the changes of each input.
|
||||
|
||||
Reference in New Issue
Block a user