From ba0d2eeaccde9155948b95f3c162f02ab29ddf04 Mon Sep 17 00:00:00 2001 From: jochen Date: Sun, 15 Nov 2026 16:22:37 +0100 Subject: [PATCH] assigner: ask the operator only for the change kinds the setting ask-operator-for names Every proposal whose verdict passed waited on the operator's yes. Which are asked is now the assigner's setting ask-operator-for, a list of change kinds (place, take-off, pin, unpin, hold, unhold, decide, config, add-node, remove-node, and stops for a verdict whose answers say a node would stop a module), its default take-off, remove-node, stops. A proposal none of whose changes is listed is applied at once, said in the log and by assignments-changed, naming who proposed it and why. The key is declared in the manifest as the two others, filling a file the process reads at each pass; a change of it is said. Builds: design 47 G A-425, A-425a --- .../cmd/mesh-delivery-assigner/askfor.go | 117 ++++++++++++++++++ .../cmd/mesh-delivery-assigner/main.go | 3 +- .../cmd/mesh-delivery-assigner/pass.go | 3 + .../cmd/mesh-delivery-assigner/pass_test.go | 27 ++++ .../cmd/mesh-delivery-assigner/seat.go | 14 +++ .../internal/judge/judge.go | 14 +++ .../mesh-delivery-assigner/manifest_test.go | 6 +- modules/mesh-delivery-assigner/module.json | 17 ++- 8 files changed, 197 insertions(+), 4 deletions(-) create mode 100644 modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/askfor.go diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/askfor.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/askfor.go new file mode 100644 index 00000000..bcdb857e --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/askfor.go @@ -0,0 +1,117 @@ +package main + +import ( + "errors" + "io/fs" + "os" + "slices" + "strings" + + mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" + mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" + + "mesh-delivery-assigner/internal/judge" +) + +// Which proposals the assigner asks of the operator is its setting ask-operator-for (design 47 G A-425, A-425a): a list of change +// kinds, set at mesh level with mesh-config's config, its default take-off, remove-node and stops. A proposal none of whose changes +// is listed is applied at once when its verdict passes, said in the log and by assignments-changed; one with a listed kind is +// asked as before. The setting is read from the file the manifest declares with the content ${config:ask-operator-for}, which the +// environment names (A-112), at every pass, and a change of it is said. + +// KindStops is the change kind of a verdict whose answers say a node would stop running a module. +const KindStops = "stops" + +// defaultAskOperatorFor is the setting's default. +var defaultAskOperatorFor = []string{mmac.OpTakeOff, mmac.OpRemoveNode, KindStops} + +// askKinds are the kinds the setting may name. +var askKinds = []string{mmac.OpPlace, mmac.OpTakeOff, mmac.OpPin, mmac.OpUnpin, mmac.OpHold, mmac.OpUnhold, mmac.OpDecide, mmac.OpConfig, + mmac.OpAddNode, mmac.OpRemoveNode, KindStops} + +// parseAskOperatorFor reads the setting's value: kinds separated by commas or spaces; empty is the default. A kind it does not +// know is answered apart and named nowhere in the list. +func parseAskOperatorFor(raw string) (kinds, unknown []string) { + fields := strings.FieldsFunc(raw, func(r rune) bool { return r == ',' || r == ' ' || r == '\n' || r == '\t' }) + if len(fields) == 0 { + return slices.Clone(defaultAskOperatorFor), nil + } + for _, f := range fields { + f = strings.ToLower(strings.TrimSpace(f)) + switch { + case !slices.Contains(askKinds, f): + unknown = append(unknown, f) + case !slices.Contains(kinds, f): + kinds = append(kinds, f) + } + } + slices.Sort(kinds) + return kinds, unknown +} + +// readAskOperatorFor reads the setting's file: the default for no path or no file. +func readAskOperatorFor(path string) (kinds, unknown []string, err error) { + if path == "" { + k, u := parseAskOperatorFor("") + return k, u, nil + } + raw, err := os.ReadFile(path) + if errors.Is(err, fs.ErrNotExist) { + k, u := parseAskOperatorFor("") + return k, u, nil + } + if err != nil { + return nil, nil, err + } + k, u := parseAskOperatorFor(string(raw)) + return k, u, nil +} + +// askOperatorFor is the setting as this pass read it: the file read again each pass, a change said once, a file that cannot be +// read keeping what was read last (the default before any). +func (p *passer) askOperatorFor() []string { + kinds, unknown, err := readAskOperatorFor(p.askForFile) + if err != nil { + if p.askFor == nil { + p.askFor = slices.Clone(defaultAskOperatorFor) + } + p.logf("the setting ask-operator-for cannot be read from %s, so %s stands: %v", p.askForFile, strings.Join(p.askFor, ", "), err) + return p.askFor + } + if !slices.Equal(kinds, p.askFor) { + words := "the setting ask-operator-for is " + strings.Join(kinds, ", ") + ": a proposal with a change of another kind only is applied with no ask" + if len(unknown) > 0 { + words += "; kinds it names that are none are left out: " + strings.Join(unknown, ", ") + } + p.logf("%s", words) + p.askFor = kinds + } + return p.askFor +} + +// asksFor says whether a proposal of these change kinds is asked of the operator. +func (p *passer) asksFor(kinds []string) bool { + set := p.askOperatorFor() + for _, k := range kinds { + if slices.Contains(set, k) { + return true + } + } + return false +} + +// proposalKinds are the change kinds of a proposal whose verdict is v: each change's op, and stops when an answer says a node +// would stop running a module. +func proposalKinds(prop mma.Proposal, v mma.Verdict) []string { + var out []string + for _, c := range prop.Changes { + if c.Op != "" && !slices.Contains(out, c.Op) { + out = append(out, c.Op) + } + } + if len(judge.Stops(v)) > 0 { + out = append(out, KindStops) + } + slices.Sort(out) + return out +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go index ef9a14aa..94c57402 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/main.go @@ -587,7 +587,8 @@ func follow(ctx context.Context, js jetstream.JetStream, nc process.Subscriber, } } p := &passer{kvs: kvs, w: w, logf: logger.Printf, say: say, findings: findings, said: map[string]bool{}, answeredAt: map[string]uint64{}, - reads: reads, seats: gate} + reads: reads, seats: gate, askForFile: getenv("MESH_ASSIGNER_ASK_OPERATOR_FOR_FILE")} + p.askOperatorFor() // the setting ask-operator-for said once at the start (design 47 G A-425a) // The wait on an outside cause (outside.go, design 47 G ยง706): the keeper's three events say when to judge again, and the // seat mesh-wants' wait-held and wait-ended are said; registered before the hub starts. if bus != nil && kvs[buckets.Waits()] != nil { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go index 1069c05d..e5205a60 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go @@ -50,6 +50,9 @@ type passer struct { // pass says a condition once and a restart says it again (the router keeps one per key). say func(oc.Condition) error said map[string]bool + // askForFile is the file of the setting ask-operator-for, askFor the kinds read last (design 47 G A-425a). + askForFile string + askFor []string // skipped are the added modules whose absence from a candidate's added was said (skippedAdd), and the trunk entries whose // release is not heard (unheardRelease), once each. skipped map[string]bool diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass_test.go index 226f9fc8..86dfc7f4 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass_test.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass_test.go @@ -10,6 +10,8 @@ import ( "maps" "mesh-delivery-assigner/internal/buckets" "mesh-delivery-assigner/internal/puts" + "os" + "path/filepath" "slices" "strings" "testing" @@ -1327,6 +1329,31 @@ func TestAnApprovedAssignmentsCallsNoControllerVerb(t *testing.T) { if len(bus.calls) != 0 || !slices.Equal(said, []string{mma.EventAssignmentsChanged}) { t.Fatalf("calls = %d, said = %v", len(bus.calls), said) } + // Design 47 G A-425a: the setting ask-operator-for says which change kinds are asked; its default is take-off, remove-node and + // stops; a kind it does not know is left out; it is read from its file at each pass and a change of it is said. + if k, u := parseAskOperatorFor(""); !slices.Equal(k, []string{"take-off", "remove-node", "stops"}) || u != nil { + t.Errorf("the default = %v %v", k, u) + } + if k, u := parseAskOperatorFor("place, Stops,typo"); !slices.Equal(k, []string{"place", "stops"}) || !slices.Equal(u, []string{"typo"}) { + t.Errorf("parsed %v, unknown %v", k, u) + } + file := filepath.Join(t.TempDir(), "ask-operator-for") + var logs []string + p.logf = func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) } + p.askForFile = file + if p.asksFor([]string{"place"}) || !p.asksFor([]string{"place", "take-off"}) { + t.Error("with no file, a place alone is asked or a take-off is not") + } + if err := os.WriteFile(file, []byte("place"), 0o600); err != nil { + t.Fatal(err) + } + if !p.asksFor([]string{"place"}) || p.asksFor([]string{"take-off"}) || len(logs) != 2 || !strings.Contains(logs[1], "the setting ask-operator-for is place") { + t.Errorf("after the setting changed to place: logs %q", logs) + } + stopping := mma.Verdict{Answers: map[string]map[string]any{"anchor": {"removed": []any{"db"}}}} + if got := proposalKinds(mma.Proposal{Changes: []mma.ProposalChange{{Op: "place"}, {Op: "pin"}, {Op: "place"}}}, stopping); !slices.Equal(got, []string{"pin", "place", "stops"}) { + t.Errorf("proposal kinds = %v, want pin, place and stops", got) + } var conds []oc.Condition p.say = func(c oc.Condition) error { conds = append(conds, c); return nil } diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat.go index ce388be1..d406ece6 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/seat.go @@ -347,6 +347,14 @@ func (p *passer) stepProposal(ctx context.Context, prop mma.Proposal, rev uint64 prop.State, prop.Ask = mmac.ProposalAnswering, "" case mdcc.VerdictPass: if prop.State != mmac.ProposalAsked || again { + // A proposal none of whose changes the setting ask-operator-for names is applied at once, with no ask (A-425a). + if kinds := proposalKinds(prop, v); !p.asksFor(kinds) { + words := fmt.Sprintf("applied with no ask: its changes (%s) are none of the kinds the setting ask-operator-for names (%s); proposed by %s: %s", + strings.Join(kinds, ", "), strings.Join(p.askOperatorFor(), ", "), prop.By, prop.Why) + p.logf("the proposal %s is %s", prop.ID, words) + p.apply(ctx, prop, rev, mdc.Answer{Words: words}, current, revs) + return + } var ok bool if prop, ok = p.ask(ctx, prop, akey, judge.Brief(prop, v)); !ok { return @@ -500,6 +508,12 @@ func (p *passer) warranted(ctx context.Context, prop mma.Proposal, rev uint64, a return } answer, _ := p.w.answer(akey) + p.apply(ctx, prop, rev, answer, current, revs) +} + +// apply writes a proposal's laid candidates, its answer carried by assignments-changed: the operator's warranted answer, or the +// words of a proposal applied with no ask (design 47 G A-425a). +func (p *passer) apply(ctx context.Context, prop mma.Proposal, rev uint64, answer mdc.Answer, current map[string]mma.Assignments, revs map[string]uint64) { if moved(prop, revs) { p.layAgain(ctx, prop, rev, current, revs, "a key moved since it was approved, so its warrant is spent") return diff --git a/modules/mesh-delivery-assigner/internal/judge/judge.go b/modules/mesh-delivery-assigner/internal/judge/judge.go index 44b83b75..13510df0 100644 --- a/modules/mesh-delivery-assigner/internal/judge/judge.go +++ b/modules/mesh-delivery-assigner/internal/judge/judge.go @@ -11,6 +11,7 @@ import ( "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" mdcc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliverycheckerclient" "reflect" + "slices" "sort" "strconv" "strings" @@ -351,3 +352,16 @@ func answerOf(body map[string]any) nodeengine.CandidateAnswer { _ = json.Unmarshal(raw, &out) return out } + +// Stops are the modules a node's answer to the verdict says it would stop running, as `: ` (design 47 G A-425a: the +// change kind `stops`). +func Stops(v mma.Verdict) []string { + var out []string + for node, body := range v.Answers { + for _, m := range answerOf(body).Removed { + out = append(out, node+": "+m) + } + } + slices.Sort(out) + return out +} diff --git a/modules/mesh-delivery-assigner/manifest_test.go b/modules/mesh-delivery-assigner/manifest_test.go index 01bec1c4..b7985192 100644 --- a/modules/mesh-delivery-assigner/manifest_test.go +++ b/modules/mesh-delivery-assigner/manifest_test.go @@ -166,12 +166,14 @@ func TestManifest(t *testing.T) { } `json:"config"` } file, _ := os.ReadFile("module.json") - if err := json.Unmarshal(file, &raw); err != nil || raw.Config["outside-wait-minutes"].Default != float64(30) || raw.Config["outside-settle-seconds"].Default != float64(90) || len(raw.Config) != 2 { - t.Errorf("config = %+v (%v), want outside-wait-minutes of 30 and outside-settle-seconds of 90", raw.Config, err) + if err := json.Unmarshal(file, &raw); err != nil || raw.Config["outside-wait-minutes"].Default != float64(30) || raw.Config["outside-settle-seconds"].Default != float64(90) || + raw.Config["ask-operator-for"].Default != "take-off,remove-node,stops" || len(raw.Config) != 3 { + t.Errorf("config = %+v (%v), want outside-wait-minutes of 30, outside-settle-seconds of 90 and ask-operator-for of take-off,remove-node,stops (A-425a)", raw.Config, err) } for _, f := range []struct{ env, path, key string }{ {"MESH_ASSIGNER_OUTSIDE_WAIT_FILE", "${dir:state}/outside-wait-minutes", "outside-wait-minutes"}, {"MESH_ASSIGNER_OUTSIDE_SETTLE_FILE", "${dir:state}/outside-settle-seconds", "outside-settle-seconds"}, + {"MESH_ASSIGNER_ASK_OPERATOR_FOR_FILE", "${dir:state}/ask-operator-for", "ask-operator-for"}, } { if !strings.Contains(string(file), `"`+f.env+`": "`+f.path+`"`) || !strings.Contains(string(file), `"path": "`+f.path+`"`) || !strings.Contains(string(file), `"content": "${config:`+f.key+`}"`) { diff --git a/modules/mesh-delivery-assigner/module.json b/modules/mesh-delivery-assigner/module.json index 99c7955c..3388eb7d 100644 --- a/modules/mesh-delivery-assigner/module.json +++ b/modules/mesh-delivery-assigner/module.json @@ -174,6 +174,12 @@ "maximum": 3600, "trusted": false, "default": 90 + }, + "ask-operator-for": { + "type": "string", + "description": "the change kinds of a proposal the assigner asks the operator before it writes it, separated by commas: place, take-off, pin, unpin, hold, unhold, decide, config, add-node, remove-node, and stops for a verdict whose answers say a node would stop running a module (design 47 G A-425, A-425a). A proposal none of whose changes is of a kind named is applied at once when its verdict passes, said in the log and by assignments-changed. Empty is the default.", + "trusted": false, + "default": "take-off,remove-node,stops" } }, "objects": [ @@ -215,6 +221,14 @@ "mode": "0600", "content": "${config:outside-settle-seconds}" }, + { + "id": "ask-operator-for", + "type": "file", + "path": "${dir:state}/ask-operator-for", + "owner": "mesh-delivery-assigner", + "mode": "0600", + "content": "${config:ask-operator-for}" + }, { "id": "assigner", "type": "process", @@ -226,7 +240,8 @@ "env": { "MESH_BROKER_FILE": "${dir:state}/broker", "MESH_ASSIGNER_OUTSIDE_WAIT_FILE": "${dir:state}/outside-wait-minutes", - "MESH_ASSIGNER_OUTSIDE_SETTLE_FILE": "${dir:state}/outside-settle-seconds" + "MESH_ASSIGNER_OUTSIDE_SETTLE_FILE": "${dir:state}/outside-settle-seconds", + "MESH_ASSIGNER_ASK_OPERATOR_FOR_FILE": "${dir:state}/ask-operator-for" }, "user": "mesh-delivery-assigner", "health": {