From b2c9e60ac371d814e8e6e53b4d6056060247619d Mon Sep 17 00:00:00 2001 From: jochen Date: Sun, 22 Nov 2026 14:34:06 +0100 Subject: [PATCH] Grants: the bound of a current twin is the setting twin-current-hours Review round 1: the day after which a twin is no longer current was a constant here and again in the bus module. It is now the setting twin-current-hours of the manifest (24 by default), written into a file the part reads at its start, the field name the bus module's manifest names too, so the operator sets it once per module alike; the durable-orphaned condition names the bound. hasOwnProcess no longer sits under ownServesOf's doc comment. Builds: design 47 H2 H-A-472 (2) and its third addendum (2); review round 1 findings (2), (3) --- modules/mesh-grants/cmd/mesh-grants/main.go | 5 ++ modules/mesh-grants/grants_test.go | 26 ++++++++ .../mesh-grants/internal/grants/consumers.go | 60 ++++++++++++++++--- modules/mesh-grants/internal/grants/part.go | 3 + .../mesh-grants/internal/grants/records.go | 4 +- modules/mesh-grants/module.json | 18 +++++- 6 files changed, 104 insertions(+), 12 deletions(-) diff --git a/modules/mesh-grants/cmd/mesh-grants/main.go b/modules/mesh-grants/cmd/mesh-grants/main.go index f9d392ea2..844761bee 100644 --- a/modules/mesh-grants/cmd/mesh-grants/main.go +++ b/modules/mesh-grants/cmd/mesh-grants/main.go @@ -188,6 +188,11 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro return err } f.Part.RevisionsPerBranch, f.Part.Followed = perBranch, branches.Followed + // The setting twin-current-hours, read at the start: the bound of a current twin for the orphaned durables (design 47 H2, + // H-A-472 third addendum). + if f.Part.TwinCurrentHours, err = grants.TwinCurrentHoursSetting(strings.TrimSpace(getenv(grants.EnvTwinCurrentHours))); err != nil { + return err + } f.Watch = fv f.Part.ReadingRunning(nodeengine.StreamRunning) // The catalog-revision event names each branch's newest revision, so grants-for asks the seat mesh-catalog's revision only diff --git a/modules/mesh-grants/grants_test.go b/modules/mesh-grants/grants_test.go index bc036ef84..cb7731f88 100644 --- a/modules/mesh-grants/grants_test.go +++ b/modules/mesh-grants/grants_test.go @@ -4254,6 +4254,32 @@ func TestGrants(t *testing.T) { if res = p.Compose(); len(res.Consumers.Behind) != 1 || len(orphaned) != o+2 || orphaned[o+1].State != occ.StateCleared { t.Fatalf("a durable whose twin delivered two days ago: %+v, orphaned said %+v", res.Consumers, orphaned) } + // The third addendum (2): the bound is the setting twin-current-hours; at 72 hours, a twin that delivered two days + // ago is current, so the durable is an orphan again; the setting's file gives its hours, its default when missing. + p.TwinCurrentHours = 72 + if res = p.Compose(); len(res.Consumers.Behind) != 0 || len(orphaned) != o+3 || orphaned[o+2].State != occ.StateRaised { + t.Fatalf("with twin-current-hours 72, a durable whose twin delivered two days ago: %+v, orphaned said %+v", res.Consumers, orphaned) + } + p.TwinCurrentHours = 0 + if res = p.Compose(); len(res.Consumers.Behind) != 1 || len(orphaned) != o+4 || orphaned[o+3].State != occ.StateCleared { + t.Fatalf("back at the default bound: %+v, orphaned said %+v", res.Consumers, orphaned) + } + for _, row := range []struct { + text string + hours int + fails bool + }{{"72\n", 72, false}, {"", grants.DefaultTwinCurrentHours, false}, {"0", 0, true}, {"x", 0, true}} { + path := filepath.Join(t.TempDir(), "twin-current-hours") + if err := os.WriteFile(path, []byte(row.text), 0o600); err != nil { + t.Fatal(err) + } + if n, err := grants.TwinCurrentHoursSetting(path); n != row.hours || (err != nil) != row.fails { + t.Errorf("twin-current-hours %q: %d, %v", row.text, n, err) + } + } + if n, err := grants.TwinCurrentHoursSetting(filepath.Join(t.TempDir(), "none")); n != 24 || err != nil { + t.Errorf("with no file twin-current-hours is %d, %v; want 24", n, err) + } made["pending"] = grants.BehindBound p.Compose() // H-A-472 second addendum: a module with an account of its own but no process resource with no bus of its own has diff --git a/modules/mesh-grants/internal/grants/consumers.go b/modules/mesh-grants/internal/grants/consumers.go index 88fc21371..2a3f60169 100644 --- a/modules/mesh-grants/internal/grants/consumers.go +++ b/modules/mesh-grants/internal/grants/consumers.go @@ -4,8 +4,11 @@ import ( "encoding/json" "errors" "fmt" + "io/fs" + "os" "slices" "sort" + "strconv" "strings" "time" @@ -66,8 +69,45 @@ type heldConsumer struct { LastDelivered string `json:"last_delivered"` } -// orphanedWithin is how recent a twin's last delivery is for it to be current: within the day (design 47 H2, H-A-472). -const orphanedWithin = 24 * time.Hour +// DefaultTwinCurrentHours is the setting twin-current-hours's default (design 47 H2, H-A-472 third addendum): how many +// hours since a twin's last delivery it is current, within the day; the bus module's manifest names the same setting for +// nats_consumer_remove, so "current" and "orphaned" share one bound. +const DefaultTwinCurrentHours = 24 + +// EnvTwinCurrentHours names the file the manifest writes the setting twin-current-hours into. +const EnvTwinCurrentHours = "MESH_GRANTS_TWIN_CURRENT_HOURS_FILE" + +// TwinCurrentHoursSetting reads the setting twin-current-hours from its file, read at the start: a missing or empty file +// is its default, one that holds no positive whole number an error naming the file. +func TwinCurrentHoursSetting(path string) (int, error) { + if path == "" { + return DefaultTwinCurrentHours, nil + } + raw, err := os.ReadFile(path) + if errors.Is(err, fs.ErrNotExist) { + return DefaultTwinCurrentHours, nil + } + if err != nil { + return 0, err + } + v := strings.TrimSpace(string(raw)) + if v == "" { + return DefaultTwinCurrentHours, nil + } + n, err := strconv.Atoi(v) + if err != nil || n < 1 { + return 0, fmt.Errorf("%s holds %q, which is not a positive whole number", path, v) + } + return n, nil +} + +// twinCurrentWithin is the part's bound of a current twin: its setting twin-current-hours, else the default. +func (p *Part) twinCurrentWithin() time.Duration { + if p.TwinCurrentHours > 0 { + return time.Duration(p.TwinCurrentHours) * time.Hour + } + return DefaultTwinCurrentHours * time.Hour +} // durableTwin names the twin of a module's durable: its program's _ and its tool runner's // __tools (design 47 G, A-340), each the other's. @@ -79,9 +119,10 @@ func durableTwin(name string) string { } // orphaned says whether a held durable is an orphan (design 47 H2, H-A-472 (2)): no pulls waiting on it, while its twin -// on the same stream is current, with pulls waiting and a delivery within the day. A module that moved into the tool -// runner left its program's durable so, and one that runs its own program again its tool runner's. -func orphaned(h heldConsumer, held map[string]heldConsumer, now time.Time) bool { +// on the same stream is current, with pulls waiting and a delivery within the bound (the setting twin-current-hours, +// the third addendum). A module that moved into the tool runner left its program's durable so, and one that runs its own +// program again its tool runner's. +func orphaned(h heldConsumer, held map[string]heldConsumer, now time.Time, within time.Duration) bool { if h.WaitingPulls > 0 { return false } @@ -90,7 +131,7 @@ func orphaned(h heldConsumer, held map[string]heldConsumer, now time.Time) bool return false } last, err := time.Parse(time.RFC3339Nano, t.LastDelivered) - return err == nil && now.Sub(last) < orphanedWithin + return err == nil && now.Sub(last) < within } // heldConsumers reads the consumers the bus holds on stream, by name. @@ -177,7 +218,7 @@ func (p *Part) consumers(res *Result, recs buscompose.Records, match string) { // alarm says a real backlog only. switch { case !p.behindSeen[key]: - case orphaned(h, held, p.now()): + case orphaned(h, held, p.now(), p.twinCurrentWithin()): orphans = append(orphans, key) default: st.Behind = append(st.Behind, fmt.Sprintf("%s: %d pending", key, h.Pending)) @@ -411,8 +452,9 @@ func (p *Part) sayOrphaned(orphans []string) { c.Headline = "A listener's old consumer is left over" needsYou(&c, "their removal, with the bus module's nats_consumer_remove", "These durables are more than the bound behind "+ "their stream's head with no pulls waiting, while their module's twin on the same stream (_ and "+ - "__tools) has pulls waiting and delivered within the day: a module that moved into or out of the tool "+ - "runner left them, and they are not said as messages piling up: "+why) + "__tools) has pulls waiting and delivered within "+p.twinCurrentWithin().String()+" (the setting "+ + "twin-current-hours): a module that moved into or out of the tool runner left them, and they are not said as "+ + "messages piling up: "+why) c.Resolved = "no orphaned durable is left" } if p.Say(c) == nil { diff --git a/modules/mesh-grants/internal/grants/part.go b/modules/mesh-grants/internal/grants/part.go index 99350c347..30c3ae625 100644 --- a/modules/mesh-grants/internal/grants/part.go +++ b/modules/mesh-grants/internal/grants/part.go @@ -197,6 +197,9 @@ type Part struct { // RevisionsPerBranch is the setting grants-for-revisions-per-branch, read at the start (grantsfor.go, design 47 H H-A-195); // 0 is its default. RevisionsPerBranch int + // TwinCurrentHours is the setting twin-current-hours, read at the start (consumers.go, design 47 H2, H-A-472 third + // addendum): the bound of a current twin, by which a durable is orphaned; 0 is its default. + TwinCurrentHours int // Followed is the watched branches the part follows, as named (the branch follower's): grants-for keeps the revisions // of these alone, and frees a branch's once it leaves them; nil in the tools, where every branch is kept. Followed func() []string diff --git a/modules/mesh-grants/internal/grants/records.go b/modules/mesh-grants/internal/grants/records.go index bba9b316f..39a52c43e 100644 --- a/modules/mesh-grants/internal/grants/records.go +++ b/modules/mesh-grants/internal/grants/records.go @@ -652,8 +652,6 @@ func queueOwnersOf(m catalogue.Manifest) []buscompose.QueueOwner { return out } -// ownServesOf is the seat verbs of the module's processes that declare no bus, which run on the module's own account (design -// 47 G A-115, A-127); a process with a bus carries its own serves (processesOf). // hasOwnProcess says the manifest names a process resource with no `bus` of its own: one that runs on the module's own // account and reads its consumer _ (H-A-472 second addendum). func hasOwnProcess(m catalogue.Manifest) bool { @@ -669,6 +667,8 @@ func hasOwnProcess(m catalogue.Manifest) bool { return false } +// ownServesOf is the seat verbs of the module's processes that declare no bus, which run on the module's own account (design +// 47 G A-115, A-127); a process with a bus carries its own serves (processesOf). func ownServesOf(m catalogue.Manifest) []string { buses := m.ProcessBuses() var out []string diff --git a/modules/mesh-grants/module.json b/modules/mesh-grants/module.json index 835b6bf32..7513fdd69 100644 --- a/modules/mesh-grants/module.json +++ b/modules/mesh-grants/module.json @@ -73,6 +73,13 @@ "maximum": 32, "trusted": false, "default": 4 + }, + "twin-current-hours": { + "type": "number", + "description": "how many hours since a module's durable's twin last delivered it is current (design 47 H2 H-A-472): the consumer-behind check leaves out a durable with no pulls waiting whose twin has pulls waiting and a delivery within these hours, and names it in durable-orphaned. The bus module's manifest names the same setting for nats_consumer_remove.", + "minimum": 1, + "trusted": false, + "default": 24 } }, "emits": [ @@ -175,6 +182,14 @@ "mode": "0600", "content": "${config:grants-for-revisions-per-branch}" }, + { + "id": "twin-current-hours", + "type": "file", + "path": "${dir:state}/twin-current-hours", + "owner": "mesh-grants", + "mode": "0600", + "content": "${config:twin-current-hours}" + }, { "id": "grants", "type": "process", @@ -185,7 +200,8 @@ ], "env": { "MESH_BROKER_FILE": "${dir:state}/broker", - "MESH_GRANTS_REVISIONS_PER_BRANCH_FILE": "${dir:state}/grants-for-revisions-per-branch" + "MESH_GRANTS_REVISIONS_PER_BRANCH_FILE": "${dir:state}/grants-for-revisions-per-branch", + "MESH_GRANTS_TWIN_CURRENT_HOURS_FILE": "${dir:state}/twin-current-hours" }, "user": "mesh-grants", "health": {