From 08ec25e367edb68e1628b90e7785bae0242a1d1b Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 21 Nov 2026 02:32:01 +0100 Subject: [PATCH] Design 47 H1: a kept old-section entry keeps only the objects the keeper made; a kept state stays in the cap sum MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review round 1. keepOld took the old plan's objects whole, so a state's `-objects` store the grants part made beside a bucket the keeper made alone would have been compared, grown and, on a recreate, deleted by the keeper: it now keeps only the old plan's objects the made record already holds. CapSum skipped every kept entry as counted in the old sections' caps, which count objects, logs and streams but never a state: it now skips only a kept objects, log or stream entry, so a kept state's bucket stays in the sum. At the delivery the plan digest changes once for every module with old sections (most modules, since state counts): each plans again once, quietly, as its entries are compared with what the bus holds and nothing is created or said where they agree. Builds: design 47 H H-A-229, H-A-230, H-A-231; G §547 --- modules/nats/internal/keeper/keeper.go | 17 ++++++++-- modules/nats/internal/keeper/keeper_test.go | 36 +++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/modules/nats/internal/keeper/keeper.go b/modules/nats/internal/keeper/keeper.go index 279a8b5b9..1cf484be7 100644 --- a/modules/nats/internal/keeper/keeper.go +++ b/modules/nats/internal/keeper/keeper.go @@ -8,6 +8,7 @@ import ( "encoding/json" "errors" "fmt" + "slices" "sort" "strings" "sync" @@ -265,7 +266,7 @@ type Limits struct { // CapSum is the sum, in bytes, of the caps of every object the entries made hold, but for the entry of skipModule and // skipLocal (design 47 G §547): what the bus's limits are read against. An entry never made, or refused, holds none, and -// one an old section keeps is counted once, in the old sections' caps (design 47 H H-A-229). +// one an old objects, log or stream section keeps is counted once, in the old sections' caps (design 47 H H-A-229). func CapSum(held map[string]Asked, skipModule, skipLocal string) int64 { var sum int64 for module, a := range held { @@ -273,7 +274,9 @@ func CapSum(held map[string]Asked, skipModule, skipLocal string) int64 { if (module == skipModule && local == skipLocal) || !(m.Created || m.Orphaned) { continue } - if _, kept := oldKept(a.Old, local, m); kept { + // A kept entry of an objects, log or stream section is in the old sections' caps already; a state's is not + // (design 47 G §547, H H-A-229). + if p, kept := oldKept(a.Old, local, m); kept && p.Storage != catalogue.StorageKeyValue { continue } for _, o := range m.Plan.Objects { @@ -688,7 +691,15 @@ func (k *Keeper) keepOld(held *Asked, local string) bool { m.Orphaned = false k.clear(conditionOrphaned, held.Module, local) } - m.Plan.Objects = p.Objects + // Only the objects the keeper made are its own: one the old plan names beside them (a state's `-objects` the + // grants part made) is never compared, grown or rebuilt by the keeper (H-A-231). + var objects []Object + for _, o := range p.Objects { + if slices.ContainsFunc(m.Plan.Objects, func(made Object) bool { return made.Stream() == o.Stream() }) { + objects = append(objects, o) + } + } + m.Plan.Objects = objects k.ensureMade(held, &m, false) held.Made[local] = m return true diff --git a/modules/nats/internal/keeper/keeper_test.go b/modules/nats/internal/keeper/keeper_test.go index 068447d3d..05b3b0ac4 100644 --- a/modules/nats/internal/keeper/keeper_test.go +++ b/modules/nats/internal/keeper/keeper_test.go @@ -1160,6 +1160,42 @@ func TestTheKeeperMakesWhatAVerdictAsksForAndSaysIt(t *testing.T) { t.Errorf("request %+v, said %v, held %+v: rebuilt at the declared cap", got, out.events, bus.held["OBJ_m_incoming"]) } }}, + // Design 47 H H-A-231, G §547: a state the keeper made as its bucket alone keeps only that bucket; the store beside it + // the grants part made is neither compared, grown nor rebuilt by the keeper; a kept state's cap stays in the sum, a + // kept objects section's is counted in the old sections' caps alone. + {"a kept state's store made by the grants part is left alone, its bucket's cap summed", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) { + store := k.Store.(*memStore) + incoming := entry("incoming", catalogue.StorageObjectStore, catalogue.TemplateObjects, map[string]string{"max-mib": "8"}) + k.Heard(Accepted{Module: "m", Revision: 1, BusState: []catalogue.BusStateEntry{incoming}}) + old := OldPlansOf(catalogue.Manifest{Module: "m", State: []catalogue.StateDeclaration{{Name: "findings"}}, + Objects: []catalogue.ObjectsDeclaration{{Name: "incoming", MaxMiB: 8}}}) + bucket := old[0].Objects[0] + bus.held[bucket.Stream()] = bucket.Settings + bus.held["OBJ_m_findings-objects"] = Settings{MaxBytes: 8 * MiB, Discard: DiscardNew} + held := k.Held()["m"] + held.Made["findings"] = Made{Plan: Plan{Module: "m", Local: "findings", Storage: catalogue.StorageKeyValue, Template: catalogue.TemplateState, + Objects: []Object{bucket}}, Created: true, Orphaned: true} + k.write(held) + k.Heard(Accepted{Module: "m", Revision: 2, Old: old}) + k.CheckAll() + if m := k.Held()["m"].Made["findings"]; m.Orphaned || len(m.Plan.Objects) != 1 || len(bus.updated) != 0 || m.Drift != "" { + t.Errorf("findings %+v, updated %v: kept, its bucket alone, the grants part's store not grown", m, bus.updated) + } + if sum := CapSum(k.Held(), "", ""); sum != 64*MiB { + t.Errorf("the sum is %d MiB, want 64: the kept state's bucket, not the kept objects section's store", sum/MiB) + } + binds, _ := occ.ActDigest(occ.Act{"verb": "recreate", "module": "m", "local": "findings"}) + ask := oc.Ask{ID: "bus-8", Who: occ.Operator, Expires: occ.FormatTime(k.now().Add(time.Hour)), Options: []oc.Option{{ID: "go", Label: "Rebuild", Level: occ.Destroy, Binds: binds}}} + w := &oc.Warrant{Ask: "bus-8", Asker: Module, Outcome: occ.OutcomeChosen, By: &oc.Person{Who: occ.Operator}, + Option: "go", Label: "Rebuild", Level: occ.Destroy, At: occ.FormatTime(k.now()), AskDigest: occ.Digest(ask)} + k.Warrant = func(string) (*oc.Ask, *oc.Warrant, error) { return &ask, w, nil } + raw, _ := json.Marshal(Request{ID: "r8", Verb: VerbRecreate, Module: "m", Local: "findings", Ask: "bus-8"}) + _, _ = store.Put(StateRequests, "r8", raw) + k.ActOnRequests() + if fmt.Sprint(bus.deleted) != "[KV_m_findings]" || bus.held["OBJ_m_findings-objects"].MaxBytes != 8*MiB { + t.Errorf("deleted %v, store held %+v: the recreate rebuilds the bucket alone", bus.deleted, bus.held["OBJ_m_findings-objects"]) + } + }}, {"a recreate rebuilds a drifted entry after a snapshot", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) { store := k.Store.(*memStore) k.Heard(Accepted{Module: "m", Revision: 1, BusState: []catalogue.BusStateEntry{log}})