From 3ae5fbdf1a3037b065bb868953f6de2b80a1dbd5 Mon Sep 17 00:00:00 2001 From: jochen Date: Mon, 23 Nov 2026 00:28:26 +0100 Subject: [PATCH] Assigner: drop a retired module's assignment key, by hand and at the pass A module key of a retired module, written before the value carried a version, stayed in the assignments state for ever and stopped every reader of the whole state. The tool assigner_assignment_drop {module, node, why} deletes one such key, recorded as a hand act first; the pass's clean state drops each itself, naming it in the journal. Either drops only a key no manifest of the trunk names whose node key does not name it; nothing goes while the trunk is empty. Builds: design 47 H2 H-A-509 (and its second addendum) --- .../assignmentdrop_tool.go | 116 ++++++++++++++ .../assignmentdrop_tool_test.go | 146 ++++++++++++++++++ .../cmd/mesh-delivery-assigner/pass.go | 42 +++++ .../cmd/mesh-delivery-assigner/tools.go | 1 + modules/mesh-delivery-assigner/module.json | 1 + 5 files changed, 306 insertions(+) create mode 100644 modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/assignmentdrop_tool.go create mode 100644 modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/assignmentdrop_tool_test.go diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/assignmentdrop_tool.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/assignmentdrop_tool.go new file mode 100644 index 000000000..3444bd7cf --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/assignmentdrop_tool.go @@ -0,0 +1,116 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "slices" + + mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" + mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" + "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" + stdio "git.novox.be/novox/mesh-sdk/go" +) + +// assignmentDropTool drops one assignment key of a module no manifest names any more by hand (design 47 H2 H-A-509): a +// module key `.` of a retired module, as the clean state of the pass drops it, the act written in the +// hand-act log first. +const assignmentDropTool = "assigner_assignment_drop" + +// assignmentLeft is why a module key `.` is not dropped, "" when it may be (H-A-509 and its second +// addendum): only when no manifest the trunk holds names the module and the node key does not name it, since the node key +// is the commit of a write and a hand act never leaves one node's keys disagreeing; nothing while the trunk is empty. +func assignmentLeft(node, module string, trunk map[string]bool, nodeRaw []byte, nodeFound bool) string { + switch { + case len(trunk) == 0: + return "the trunk holds no manifest yet, so no module is known to be retired" + case trunk[module]: + return "a manifest the trunk holds names " + module + case !nodeFound: + return "" + } + n, err := mmac.DecodeNode(nodeRaw) + if err != nil { + return fmt.Sprintf("the node key of %s does not read, so whether it names %s is not known: %v", node, module, err) + } + if slices.ContainsFunc(n.Modules, func(r mma.AssignmentsByNodeNodeValueModulesItem) bool { return r.Module == module }) { + return "the node key of " + node + " still names " + module + ", so it stays placed there" + } + return "" +} + +// dropAssignment deletes the module key of module on node, the act first recorded through the seat mesh-hand-acts; trunk +// answers the modules the trunk holds. +func dropAssignment(st seatStore, ask func(string, any) (json.RawMessage, error), trunk func() (map[string]bool, error), node, module, why, by string) (any, error) { + switch { + case node == "" || module == "": + return nil, errors.New("name the node and the module whose assignment key is dropped") + case why == "": + return nil, errors.New("an assignment key dropped by hand says why") + case by == "" || by == stdio.Unproven: + return nil, errors.New("an assignment key dropped by hand names who drops it, and the runtime proved no caller") + } + key, err := mmac.ModuleKey(node, module) + if err != nil { + return nil, err + } + nodeKey, err := mmac.NodeKey(node) + if err != nil { + return nil, err + } + raw, _, found, err := st.get(mma.StateAssignments, key) + if err != nil { + return nil, err + } + if !found { + return nil, fmt.Errorf("the assigner holds no assignment key %s", key) + } + modules, err := trunk() + if err != nil { + return nil, fmt.Errorf("the trunk's modules cannot be read, so %s stays: %w", key, err) + } + nodeRaw, _, nodeFound, err := st.get(mma.StateAssignments, nodeKey) + if err != nil { + return nil, err + } + if left := assignmentLeft(node, module, modules, nodeRaw, nodeFound); left != "" { + return nil, fmt.Errorf("the assignment key %s stays: %s", key, left) + } + taken, err := meshhandactsclient.Recorder{Ask: ask}.Record(meshhandactsclient.Request{What: assignmentDropTool + " " + key, Why: why + " (" + by + ")", Cause: "assignment-drop"}) + if err != nil { + return nil, fmt.Errorf("the drop of %s is not recorded, so the key stays: %w", key, err) + } + if err := st.del(mma.StateAssignments, key); err != nil { + return nil, fmt.Errorf("the assignment key %s is not dropped: %w", key, err) + } + return map[string]any{"key": key, "dropped": json.RawMessage(raw), "by": by, "hand-act": taken.Log}, nil +} + +// trunkModules is the modules the trunk holds, asked of the indexer's first view of modules (ADR 0596, ADR 0621). +func trunkModules(ask func(string, any) (json.RawMessage, error)) func() (map[string]bool, error) { + return func() (map[string]bool, error) { + mods, err := indexerView(ask, indexerModules) + if err != nil { + return nil, err + } + out := make(map[string]bool, len(mods)) + for name := range mods { + out[name] = true + } + return out, nil + } +} + +// assignmentDropTools is the tool over the runtime's states. +func assignmentDropTools(st seatStore, ask func(string, any) (json.RawMessage, error), trunk func() (map[string]bool, error)) []stdio.Tool { + return []stdio.Tool{{Name: assignmentDropTool, Description: "Drop one assignment key by hand (design 47 H2 H-A-509): the module " + + "key . of the seat mesh-module-assigner's assignments, for a module no manifest the trunk holds names any more " + + "(a retired module's key, which may predate the value's version), as the clean state of the pass drops it. Refused while a " + + "manifest of the trunk names the module, while the node key .node still names it, or with no key there. Recorded as a " + + "hand act first.", + Input: schema(map[string]string{"node": "the node's name", "module": "the module's name", "why": "why the key is dropped"}, + []string{"node", "module", "why"}), + Run: func(a map[string]any) (any, error) { + return dropAssignment(st, ask, trunk, arg(a, "node"), arg(a, "module"), arg(a, "why"), stdio.CallerOf(a)) + }}} +} diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/assignmentdrop_tool_test.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/assignmentdrop_tool_test.go new file mode 100644 index 000000000..5705af273 --- /dev/null +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/assignmentdrop_tool_test.go @@ -0,0 +1,146 @@ +package main + +import ( + "context" + "encoding/json" + "errors" + "strings" + "testing" + "time" + + mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" + mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" + "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog" + + plc "mesh-delivery-assigner/internal/assignment" +) + +// H-A-509 and its second addendum: a module key of a module no manifest of the trunk names, that the node key does not name, +// is dropped, by hand through assigner_assignment_drop (recorded as a hand act first) and at the pass's clean state (named in +// the journal); a key a manifest of the trunk still names, or the node key still names, stays; nothing goes while the trunk +// is empty. +func TestARetiredModulesAssignmentKeyIsDropped(t *testing.T) { + // novox places web; the key of the retired checker predates the value's version; the key of the retired old is still + // named by the node key. + values, err := mmac.AssignmentsValues(mma.Assignments{V: mmac.Version, Node: "novox", Modules: []mma.PlacedModule{{Module: "web"}, {Module: "old"}}}) + if err != nil { + t.Fatal(err) + } + values["novox.checker"] = []byte(`{"node":"novox"}`) + checker, web, old := "novox.checker", "novox.web", "novox.old" + trunkOf := func(names ...string) map[string]bool { + out := map[string]bool{} + for _, n := range names { + out[n] = true + } + return out + } + copyOf := func() map[string][]byte { + out := map[string][]byte{} + for k, v := range values { + out[k] = v + } + return out + } + + t.Run("by hand", func(t *testing.T) { + for _, c := range []struct { + name, module string + trunk map[string]bool + refuse error + dropped bool + }{ + {"a retired module's key", "checker", trunkOf("web"), nil, true}, + {"a manifest of the trunk names it", "web", trunkOf("web"), nil, false}, + {"the node key still names it", "old", trunkOf("web"), nil, false}, + {"the trunk is empty", "checker", trunkOf(), nil, false}, + {"no key there", "gone", trunkOf("web"), nil, false}, + {"not recorded", "checker", trunkOf("web"), errors.New("no responders"), false}, + } { + st := memStore{mma.StateAssignments: &memKV{values: copyOf()}} + var asked []string + ask := func(verb string, body any) (json.RawMessage, error) { + raw, _ := json.Marshal(body) + asked = append(asked, verb+" "+string(raw)) + return json.RawMessage(`{}`), c.refuse + } + trunk := func() (map[string]bool, error) { return c.trunk, nil } + out, err := dropAssignment(st, ask, trunk, "novox", c.module, "retired", "person.jochen") + key := "novox." + c.module + _, there := st[mma.StateAssignments].values[key] + switch { + case c.dropped && (err != nil || there || len(asked) != 1 || !strings.Contains(asked[0], assignmentDropTool+" "+key)): + t.Errorf("%s: %v, %v; the key left %v, asked %v", c.name, out, err, there, asked) + case !c.dropped && err == nil: + t.Errorf("%s: %v, want it refused", c.name, out) + case len(st[mma.StateAssignments].values) != len(values)-btoi(c.dropped): + t.Errorf("%s: %d keys left, want %d", c.name, len(st[mma.StateAssignments].values), len(values)-btoi(c.dropped)) + } + } + if _, err := dropAssignment(memStore{mma.StateAssignments: &memKV{values: copyOf()}}, nil, func() (map[string]bool, error) { return trunkOf("web"), nil }, + "novox", "checker", "", "person.jochen"); err == nil { + t.Error("a drop with no why is not refused") + } + }) + + t.Run("at the pass", func(t *testing.T) { + for _, c := range []struct { + name string + trunk []string + gone []string + }{ + {"a retired module's key goes, one the node key names stays", []string{"web"}, []string{checker}}, + {"the trunk is empty", nil, nil}, + } { + kv := &memKV{values: copyOf()} + p := newPasser(kvSet{plc.AssignmentsBucket(): kv}, time.Now()) + var said []string + p.logf = func(f string, a ...any) { said = append(said, f) } + for k, v := range kv.values { + p.w.put(plc.AssignmentsBucket(), k, v) + } + trunk := map[string]meshcatalog.ModulesByModuleValue{} + for _, n := range c.trunk { + trunk[n] = meshcatalog.ModulesByModuleValue{} + } + for range 2 { + p.retiredAssignments(context.Background(), trunk) + } + for _, key := range []string{checker, web, old, "novox.node"} { + _, there := kv.values[key] + if want := !contains(c.gone, key); there != want { + t.Errorf("%s: %s left = %v, want %v", c.name, key, there, want) + } + } + if dropped, left := countSaid(said, "is dropped"), countSaid(said, "is left"); len(c.gone) > 0 && (dropped != 1 || left != 1) { + t.Errorf("%s: said dropped %d and left %d times, want once each: %v", c.name, dropped, left, said) + } + } + }) +} + +func btoi(b bool) int { + if b { + return 1 + } + return 0 +} + +func contains(list []string, s string) bool { + for _, x := range list { + if x == s { + return true + } + } + return false +} + +func countSaid(said []string, words string) int { + n := 0 + for _, s := range said { + if strings.Contains(s, words) { + n++ + } + } + return n +} 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 6dc77a0fb..1b6b586b8 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/pass.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "git.novox.be/novox/mesh-contracts/go/seats/nodeengine" + "maps" "mesh-delivery-assigner/internal/buckets" "reflect" "slices" @@ -14,6 +15,7 @@ import ( "strings" "time" + mmac "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient" "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient" mma "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks" @@ -97,6 +99,9 @@ type passer struct { // saidWaits is the last said of the seat's wait for its states (sayWaits). saidWaits string intended map[string]map[string]map[string]bool + // assignmentLeftSaid is, by assignment key, the why last said that the clean state leaves a retired module's key (H-A-509), + // so each is said once per why. + assignmentLeftSaid map[string]string // laidHere are the proposals whose candidates this process wrote (design 47 G A-317), by id. laidHere map[string]bool // reads is the assigner's state engine-reads, what each node's engine said it reads (enginewords.go, design 47 H-A-104); @@ -222,6 +227,7 @@ func (p *passer) wants(ctx context.Context) { puts := p.putsByModule() builds := placementBuilds(p.w.assignments()) p.staleAsks(ctx, trunk) + p.retiredAssignments(ctx, trunk) names := map[string]bool{} for name := range trunk { names[name] = true @@ -528,6 +534,42 @@ func (p *passer) staleAsks(ctx context.Context, trunk map[string]meshcatalog.Mod } } +// retiredAssignments is the clean state of the assignments at the pass (design 47 H2 H-A-509 and its second addendum): each +// module key `.` of a module no manifest the trunk holds names, that the node key does not name, is deleted and +// named in the journal, whether its value reads or not; one the node key still names is left, said once per why. Nothing goes +// while the trunk is empty, or with no assignments bucket open. +func (p *passer) retiredAssignments(ctx context.Context, trunk map[string]meshcatalog.ModulesByModuleValue) { + if len(trunk) == 0 || p.kvs[plc.AssignmentsBucket()] == nil { + return + } + names := make(map[string]bool, len(trunk)) + for name := range trunk { + names[name] = true + } + held := p.w.read(plc.AssignmentsBucket()) + for _, key := range slices.Sorted(maps.Keys(held)) { + node, module, kind, err := mmac.ParseKey(key) + if err != nil || kind != mmac.ModuleKind || names[module] { + continue + } + nodeKey, _ := mmac.NodeKey(node) + nodeRaw, nodeFound := held[nodeKey] + if left := assignmentLeft(node, module, names, nodeRaw, nodeFound); left != "" { + if p.assignmentLeftSaid[key] != left { + if p.assignmentLeftSaid == nil { + p.assignmentLeftSaid = map[string]string{} + } + p.assignmentLeftSaid[key] = left + p.logf("the assignment key %s is of a module no manifest of the trunk names, and is left: %s", key, left) + } + continue + } + delete(p.assignmentLeftSaid, key) + p.logf("the assignment key %s is of a module no manifest of the trunk names, and is dropped", key) + p.remove(ctx, plc.AssignmentsBucket(), key) + } +} + // remove deletes one key of the assigner's own bucket when it holds it; a delete that fails is said and made again at the next // pass. func (p *passer) remove(ctx context.Context, bucket, key string) { diff --git a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go index eb704a538..2b8bdd808 100644 --- a/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go +++ b/modules/mesh-delivery-assigner/cmd/mesh-delivery-assigner/tools.go @@ -417,6 +417,7 @@ func serveTools() error { tools = append(tools, whyTools(runtimeStore{})...) tools = append(tools, askAgainTools(runtimeStore{}, stdio.Ask, time.Now)...) tools = append(tools, wantDropTools(runtimeStore{}, stdio.Ask)...) + tools = append(tools, assignmentDropTools(runtimeStore{}, stdio.Ask, trunkModules(stdio.Ask))...) // a retired module's assignment key (H-A-509) tools = append(tools, asksTools(stdio.QueueWaiting, stdio.State(process.TakenState), stdio.Ask)...) // the seat's work queue (§777) tools = append(tools, keepNowTools(stdio.Ask)...) tools = append(tools, validateBeforeTools(runtimeStore{})...) // what a validate step did not count (§814) // the keeping by hand (§813 (2)) diff --git a/modules/mesh-delivery-assigner/module.json b/modules/mesh-delivery-assigner/module.json index 1c5ce91f9..1bd9bbdfe 100644 --- a/modules/mesh-delivery-assigner/module.json +++ b/modules/mesh-delivery-assigner/module.json @@ -134,6 +134,7 @@ "assigner_written", "assigner_why", "assigner_want_drop", + "assigner_assignment_drop", "assigner_asks", "assigner_ask_drop", "assigner_keep_now",