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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user