A composition step ended at a node-engine's first "no responders": the engine between two lives gave no input. The holder of mesh-wants now asks compose-candidate through mesh-sdk's kept chunked call, under the ask id <step ask>.<node> the engine takes once, called again at the kept-call pace through transport faults until compose-ask-bound (a runtime setting, 10s). Builds: design 47 H2 §775; H-A-405, H-A-364
189 lines
7.1 KiB
Go
189 lines
7.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"time"
|
|
|
|
nec "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient"
|
|
ne "git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
|
meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
)
|
|
|
|
// The inputs of a composition run (design 47 G §354, §718, A-243, A-252): one request per candidate and machine, carrying that
|
|
// machine's input, asked of the machine's node-engine by the seat's verb compose-candidate when the run starts.
|
|
|
|
// composeCandidateKey is the seat verb's call key, asked of one node.
|
|
const composeCandidateKey = "seat:" + ne.Seat + "." + ne.VerbComposeCandidate
|
|
|
|
// candidateKeyOf is the key the step's pipeline names its candidate by (A-226): a run on request's `run-<id>`, or for a branch
|
|
// pipeline the branch's catalog graph revision `<branch>.<revision>`, the branch escaped as a key part as the pipeline key does.
|
|
func candidateKeyOf(run meshwants.StepRun) (string, error) {
|
|
switch {
|
|
case run.Run != "":
|
|
return nec.RunKey(run.Run)
|
|
case run.Branch == "" || run.Revision <= 0:
|
|
return "", errors.New("the step names neither a run on request nor a branch's revision, so no candidate is named")
|
|
}
|
|
return nec.RevisionKey(run.Branch, run.Revision) // built by the seat's client, never by hand (A-226b)
|
|
}
|
|
|
|
// machinesFor are the machines of a step (A-252): the nodes whose placement of the module names the step's branch (a placement
|
|
// naming none is the trunk's, trunk the first watched branch); a branch that is not watched takes the nodes placing the module on the trunk. In name order.
|
|
func machinesFor(placed map[string]map[string]string, module, branch, trunk string, watched bool) []string {
|
|
if branch == "" || !watched {
|
|
branch = trunk
|
|
}
|
|
var out []string
|
|
for node, b := range placed[module] {
|
|
if b == "" {
|
|
b = trunk
|
|
}
|
|
if b == branch {
|
|
out = append(out, node)
|
|
}
|
|
}
|
|
slices.Sort(out)
|
|
return out
|
|
}
|
|
|
|
// machinesOf are the machines of a composition step in the world's assignments.
|
|
func (w *world) machinesOf(module, branch string) []string {
|
|
return machinesFor(w.placedBranches(), module, branch, w.trunkName(), w.holdsBranch(branch))
|
|
}
|
|
|
|
// nodeInput is one machine's input: its bytes, or why the node gave none, or that it is away.
|
|
type nodeInput struct {
|
|
Node string
|
|
Body []byte
|
|
// Why is set when the node gave no input: its words, or the call's error.
|
|
Why string
|
|
// Away is a machine the watchman said away: it is not asked (§64).
|
|
Away bool
|
|
}
|
|
|
|
// compositionInputs asks the machines of a step for their inputs.
|
|
type compositionInputs struct {
|
|
machines func(module, branch string) []string
|
|
// callFile asks a seat verb marked chunks as a kept call (process.Conn.CallFileKept; design 47 H2 §775, H-A-405), addressed to
|
|
// one node as `seat:<seat>.<verb>@<node>`, carrying the ask's id and stopping at its bound, and answers the verb's answer with
|
|
// the whole file received added as its member `file`, base64 (design 47 G A-237b, A-237c, A-249). The call token is the
|
|
// body's `call`.
|
|
callFile func(ask process.KeptAsk, key string, body any) (json.RawMessage, error)
|
|
// bound is compose-ask-bound: how long a machine's compose-candidate is asked again through a transport fault (§775).
|
|
bound time.Duration
|
|
}
|
|
|
|
// composeAskBoundSetting is the holder's bound on its compose-candidate asks (design 47 H2 §775): past it the machine gave no
|
|
// input, naming every call.
|
|
const (
|
|
composeAskBoundSetting = "compose-ask-bound"
|
|
defaultComposeAskBound = 10 * time.Second
|
|
)
|
|
|
|
// composeAskBound reads the setting compose-ask-bound (§781).
|
|
func composeAskBound() (time.Duration, error) {
|
|
d, err := stdio.SettingDuration(composeAskBoundSetting, defaultComposeAskBound)
|
|
if err != nil {
|
|
return 0, errors.New("the assigner's " + err.Error())
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
// composeAskID is the id of the compose-candidate ask of the step asked under stepAsk to node (H-A-405 (2)): one per node per
|
|
// step, so the engines' mesh-wide state taken keeps each apart, and a repeat to the same node carries the same id.
|
|
func composeAskID(stepAsk, node string) string { return stepAsk + "." + node }
|
|
|
|
// of asks the machine node for its input with the candidate's key (§718), as a kept call under the id of stepAsk, the step
|
|
// that starts the machine's composition run: asked once per candidate and machine, again only through a transport fault until
|
|
// compose-ask-bound (§775). A node that is silent past the bound, unready or answers no input has Why set.
|
|
func (c *compositionInputs) of(ctx context.Context, node, candidate, stepAsk string) nodeInput {
|
|
bound := c.bound
|
|
if bound <= 0 {
|
|
bound = defaultComposeAskBound
|
|
}
|
|
in := nodeInput{Node: node}
|
|
type reply struct {
|
|
raw json.RawMessage
|
|
err error
|
|
}
|
|
got := make(chan reply, 1)
|
|
go func() {
|
|
// The input is any size, so the verb sends it in chunks on the call token chosen here, and the file is the input (A-237b, A-237c).
|
|
var bits [16]byte
|
|
if _, err := rand.Read(bits[:]); err != nil {
|
|
got <- reply{nil, err}
|
|
return
|
|
}
|
|
raw, err := c.callFile(process.KeptAsk{ID: composeAskID(stepAsk, node), Bound: bound}, composeCandidateKey+"@"+node,
|
|
ne.ComposeCandidateInput{Candidate: candidate, Input: true, Call: hex.EncodeToString(bits[:])})
|
|
got <- reply{raw, err}
|
|
}()
|
|
var raw json.RawMessage
|
|
select {
|
|
case <-ctx.Done():
|
|
in.Why = "its step's wait ended: " + context.Cause(ctx).Error()
|
|
return in
|
|
case r := <-got:
|
|
if r.err != nil {
|
|
in.Why = r.err.Error()
|
|
return in
|
|
}
|
|
raw = r.raw
|
|
}
|
|
var a ne.ComposeCandidateAnswer
|
|
// its numbers as written (json.Number), so the input kept is the engine's own bytes' values
|
|
dec := json.NewDecoder(bytes.NewReader(raw))
|
|
dec.UseNumber()
|
|
if err := dec.Decode(&a); err != nil {
|
|
in.Why = "its answer is not a compose-candidate answer: " + err.Error()
|
|
return in
|
|
}
|
|
switch {
|
|
case a.Verdict == nec.Unready:
|
|
in.Why = "unready: " + a.Refused
|
|
return in
|
|
case a.Chunks == 0 && len(a.Input) == 0:
|
|
in.Why = "the engine answered no composition input"
|
|
if a.Refused != "" {
|
|
in.Why += ": " + a.Refused
|
|
}
|
|
return in
|
|
}
|
|
var body []byte
|
|
if a.Chunks > 0 {
|
|
// The composition input is the file the chunks carried, the engine's own bytes (numbers as written).
|
|
var whole struct {
|
|
File string `json:"file"`
|
|
}
|
|
if err := json.Unmarshal(raw, &whole); err != nil {
|
|
in.Why = "its answer is not a compose-candidate answer: " + err.Error()
|
|
return in
|
|
}
|
|
file, err := base64.StdEncoding.DecodeString(whole.File)
|
|
if err != nil || len(file) == 0 {
|
|
in.Why = fmt.Sprintf("the engine named %d chunks of composition input and the file received is empty or not base64: %v", a.Chunks, err)
|
|
return in
|
|
}
|
|
body = file
|
|
} else {
|
|
// An answer with the input inline (an engine before A-237b, read until the checker is removed, A-237c, G-6 §321): the object, as it was.
|
|
var err error
|
|
if body, err = json.Marshal(a.Input); err != nil {
|
|
in.Why = "its composition input cannot be encoded again: " + err.Error()
|
|
return in
|
|
}
|
|
}
|
|
in.Body = body
|
|
return in
|
|
}
|