diff --git a/modules/nats/cmd/nats-tools/broker.go b/modules/nats/cmd/nats-tools/broker.go index 86421c683..c691fc721 100644 --- a/modules/nats/cmd/nats-tools/broker.go +++ b/modules/nats/cmd/nats-tools/broker.go @@ -96,7 +96,7 @@ func streamsHeld(jsz []byte) (map[string]busStream, error) { // heldOf is one entry as the seat's list, show and orphans answer it (mesh-contracts meshbroker.Held): its bus name, its // template, its options or configuration as the manifest asked them, the settings the bus holds of each of its objects // by stream, and its fill summed over them against their caps. -func heldOf(module, local string, m keeper.Made, held map[string]busStream) meshbroker.Held { +func heldOf(module, local string, m keeper.Made, held map[string]busStream, objects bool) meshbroker.Held { h := meshbroker.Held{BusName: keeper.BusName(module, local), Module: module, LocalName: local, StorageType: m.Plan.Storage, Template: m.Plan.Template, Options: anyOf(m.Plan.Entry.Options), Configuration: anyOf(m.Plan.Entry.Configuration)} settings := map[string]any{} @@ -110,6 +110,11 @@ func heldOf(module, local string, m keeper.Made, held map[string]busStream) mesh fill.Bytes += int64(s.State.Bytes) fill.Count += int64(s.State.Messages) fill.Cap += s.Config.MaxBytes + if objects { + // show's fill keeps the entry's sum and gains one item per object (design 47 H2 H-A-634 (9), H-A-687 (3)). + fill.Objects = append(fill.Objects, meshbroker.ObjectFill{Object: o.Stream(), Part: m.Plan.PartOf(o), Count: int64(s.State.Messages), + Bytes: int64(s.State.Bytes), Cap: s.Config.MaxBytes}) + } } if len(settings) > 0 { h.Settings, h.Fill = settings, &fill @@ -189,7 +194,7 @@ func brokerTools(read heldEntries, status func() (keeper.Status, error), jsz fun for _, module := range sortedNames(entries) { for _, local := range sortedNames(entries[module].Made) { if m := entries[module].Made[local]; keep(m) { - out = append(out, heldOf(module, local, m, held)) + out = append(out, heldOf(module, local, m, held, false)) } } } @@ -227,7 +232,7 @@ func brokerTools(read heldEntries, status func() (keeper.Status, error), jsz fun if !ok { return nil, fmt.Errorf("the bus module made no entry %q of %q", local, module) } - return heldOf(module, local, m, held), nil + return heldOf(module, local, m, held, true), nil }}, {Name: "mesh-broker.check", Description: "What the bus holds against what was asked, entry by entry: each object missing, or differing from its manifest's settings, with both. A difference that only grows is applied by the bus module itself; any other is a drift, raised to the operator.", diff --git a/modules/nats/cmd/nats-tools/broker_test.go b/modules/nats/cmd/nats-tools/broker_test.go index 348b7b4eb..21bb3636b 100644 --- a/modules/nats/cmd/nats-tools/broker_test.go +++ b/modules/nats/cmd/nats-tools/broker_test.go @@ -67,6 +67,9 @@ func TestTheSeatsReadingVerbsAnswerFromTheKeeperAndTheBus(t *testing.T) { {"mesh-broker.list", nil, []string{`"caps":{"account-limit-mib":2048,"server-limit-mib":4096,"sum-mib":1376`}, ""}, {"mesh-broker.show", map[string]any{"module": "m", "local-name": "log"}, []string{`"bus-name":"m_log"`, `"template":"log"`, `"m_log":{"max-bytes":33554432`}, ""}, + // show's fill keeps the entry's sum and gains one item per object (design 47 H2 H-A-634 (9), H-A-687 (3)). + {"mesh-broker.show", map[string]any{"module": "m", "local-name": "drafts"}, []string{`"fill":{"bytes":1048576,"cap":83886080,"count":3,"objects":[{"bytes":1048576,"cap":67108864,"count":3,"object":"KV_m_drafts"}`, + `{"bytes":0,"cap":16777216,"count":0,"object":"OBJ_m_drafts-objects"}]}`}, ""}, {"mesh-broker.show", map[string]any{"module": "m", "local-name": "later"}, []string{`"local-name":"later"`}, ""}, {"mesh-broker.show", map[string]any{"module": "m", "local-name": "none"}, nil, `made no entry "none" of "m"`}, {"mesh-broker.check", nil, []string{`{"differences":[{`, `"local-name":"log"`, `"asked":{"m_log":{"max-bytes":16777216`, diff --git a/modules/nats/internal/keeper/fill.go b/modules/nats/internal/keeper/fill.go index 87a9de1e7..75a9e0173 100644 --- a/modules/nats/internal/keeper/fill.go +++ b/modules/nats/internal/keeper/fill.go @@ -38,6 +38,7 @@ func (k *Keeper) FillPass() { continue } var most int64 = -1 + var judged *EventFill for _, o := range m.Plan.Objects { if !o.Settings.Refuses() || o.Settings.MaxBytes <= 0 { continue @@ -48,6 +49,7 @@ func (k *Keeper) FillPass() { } if p := int64(f.Bytes) * 100 / o.Settings.MaxBytes; p > most { most = p + judged = &EventFill{Count: f.Messages, Bytes: f.Bytes, Cap: o.Settings.MaxBytes, Object: o.Stream(), Part: m.Plan.PartOf(o)} } } if most < 0 { @@ -58,13 +60,13 @@ func (k *Keeper) FillPass() { switch { case !m.Full && most >= raise: m.Full = true - k.emit(EventFull, m.Plan, held.Revision, fmt.Sprintf("%d%% of its cap, past the raise mark %d%%", most, raise), nil, nil) + k.emitFill(EventFull, m.Plan, held.Revision, fmt.Sprintf("%s holds %d%% of its cap, past the raise mark %d%%", nameOf(judged), most, raise), nil, nil, judged) k.raise(conditionFull, module, local, occ.SeverityWarning, "Kept data is filling up", "a larger cap in the module's manifest, or data removed", - fmt.Sprintf("%s of %s holds %d%% of its cap and refuses new data when full; it is cleared below %d%%", local, module, most, clear)) + fmt.Sprintf("%s of %s of %s holds %d%% of its cap and refuses new data when full; it is cleared below %d%%", nameOf(judged), local, module, most, clear)) case m.Full && most < clear: m.Full = false - k.emit(EventFullCleared, m.Plan, held.Revision, fmt.Sprintf("%d%% of its cap, below the clear mark %d%%", most, clear), nil, nil) + k.emitFill(EventFullCleared, m.Plan, held.Revision, fmt.Sprintf("%s holds %d%% of its cap, below the clear mark %d%%", nameOf(judged), most, clear), nil, nil, judged) k.clear(conditionFull, module, local) } if m.Fill != was.Fill || m.Full != was.Full { @@ -77,3 +79,15 @@ func (k *Keeper) FillPass() { } k.setStatus(func(s *Status) { s.Pass = k.now() }) } + +// nameOf names the object a fill is of in words: its part and its bus name for a ledger, its bus name alone for an entry of +// one object (design 47 H2 H-A-634 (9): the state `KV_mesh-delivery-gate_records_state`). +func nameOf(f *EventFill) string { + if f == nil { + return "it" + } + if f.Part == "" { + return "`" + f.Object + "`" + } + return "the " + f.Part + " `" + f.Object + "`" +} diff --git a/modules/nats/internal/keeper/keeper.go b/modules/nats/internal/keeper/keeper.go index 97e6a4b69..031d6c6f8 100644 --- a/modules/nats/internal/keeper/keeper.go +++ b/modules/nats/internal/keeper/keeper.go @@ -76,11 +76,15 @@ type Event struct { Revision uint64 `json:"revision"` } -// EventFill is an entry's fill as an event says it: its messages, its bytes and its cap. +// EventFill is the fill an event says: the object it is of, its messages, its bytes and its cap; Object is its name on the +// bus and Part the place it has in the entry's binding (history, state, `index ` or snapshot of a ledger), absent for an +// entry of one object (design 47 H2 H-A-634 (9), H-A-687 (3)). type EventFill struct { - Count uint64 `json:"count"` - Bytes uint64 `json:"bytes"` - Cap int64 `json:"cap"` + Count uint64 `json:"count"` + Bytes uint64 `json:"bytes"` + Cap int64 `json:"cap"` + Object string `json:"object,omitempty"` + Part string `json:"part,omitempty"` } // Accepted is one module's entry as the catalog half's watched branches give it (design 47 G §344, §433, §532): the @@ -1040,10 +1044,16 @@ func (k *Keeper) SayUnsaid() { // emit says one event; one the bus did not take whole is kept in the keeper's status and said again, on the subjects // left, at the next fill pass. func (k *Keeper) emit(event string, p Plan, revision uint64, why string, asked, held map[string]Settings) { + k.emitFill(event, p, revision, why, asked, held, nil) +} + +// emitFill is emit with the fill of the object judged, which full and full-cleared carry (design 47 H2 H-A-634 (9)). +func (k *Keeper) emitFill(event string, p Plan, revision uint64, why string, asked, held map[string]Settings, fill *EventFill) { if k.Emit == nil { return } s := k.saying(event, p, revision, why, asked, held) + s.Body.Fill = fill if err := k.Emit(&s); err != nil { s.Error = err.Error() k.setStatus(func(st *Status) { st.Unsaid = append(st.Unsaid, s) }) @@ -1074,7 +1084,7 @@ func (k *Keeper) sayNow(event string, p Plan, revision uint64, why string, asked } func (k *Keeper) saying(event string, p Plan, revision uint64, why string, asked, held map[string]Settings) Saying { - body := Event{V: 1, Module: p.Module, Local: p.Local, Storage: p.Storage, Template: p.Template, Options: p.Entry.Options, + body := Event{V: 2, Module: p.Module, Local: p.Local, Storage: p.Storage, Template: p.Template, Options: p.Entry.Options, Configuration: p.Entry.Configuration, BusName: BusName(p.Module, p.Local), Binding: p.Binding, Asked: asked, Held: held, Why: why, Revision: revision} if event == EventDropped { diff --git a/modules/nats/internal/keeper/keeper_test.go b/modules/nats/internal/keeper/keeper_test.go index 3454d66fe..7bbb09720 100644 --- a/modules/nats/internal/keeper/keeper_test.go +++ b/modules/nats/internal/keeper/keeper_test.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "os" + "slices" "sort" "strings" "sync" @@ -772,6 +773,54 @@ func TestTheKeeperMakesWhatAVerdictAsksForAndSaysIt(t *testing.T) { t.Errorf("drift %q, said %v: a snapshot store that drops its oldest drifts", d, out.events) } }}, + // Design 47 H2 H-A-634 (9), H-A-636, H-A-687 (3): full and full-cleared carry the fill of the object judged, naming it and its + // part; a ledger whose state holds 100 MiB under its cap of 1 GiB says none, at 75% its state is named, and a snapshot store + // at 75% is named with the part snapshot. + {"a ledger's full names the object it judged and its part", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) { + ledger := entry("records", catalogue.StorageLedger, catalogue.TemplateLedger, map[string]string{"max-mib": "1024"}) + k.Heard(Accepted{Module: "m", Revision: 1, BusState: []catalogue.BusStateEntry{ledger}}) + out.reset() + bus.fill["KV_m_records_state"] = 100 * uint64(MiB) + bus.fill["m_records"] = 200 * uint64(MiB) + k.FillPass() + if len(out.events) != 0 { + t.Fatalf("said %v: a state at 100 MiB of 1 GiB and a history at 20%% are not full (H-A-636)", out.events) + } + bus.fill["KV_m_records_state"] = 768 * uint64(MiB) + k.FillPass() + if fmt.Sprint(out.events) != "[full m records]" || out.bodies[0].V != 2 { + t.Fatalf("said %v", out.events) + } + if f := out.bodies[0].Fill; f == nil || f.Object != "KV_m_records_state" || f.Part != "state" || f.Bytes != 768*uint64(MiB) || f.Cap != 1024*MiB || + !strings.Contains(out.bodies[0].Why, "the state `KV_m_records_state` holds 75%") { + t.Fatalf("full carries %+v and says %q: the state, at 75%% of 1 GiB", f, out.bodies[0].Why) + } + if !slices.ContainsFunc(out.conds, func(c string) bool { return strings.HasPrefix(c, "raised") }) { + t.Fatalf("conditions %v", out.conds) + } + bus.fill["KV_m_records_state"] = 0 + bus.fill["m_records_snapshot"] = 768 * uint64(MiB) + k.FillPass() + k.FillPass() + if len(out.events) != 1 { + t.Fatalf("said %v: the entry stays full while any object holds past the clear mark", out.events) + } + bus.fill["m_records_snapshot"] = 10 * uint64(MiB) + k.FillPass() + if cleared := out.bodies[len(out.bodies)-1]; fmt.Sprint(out.events) != "[full m records full-cleared m records]" || cleared.Fill == nil || cleared.Fill.Part != "history" { + t.Fatalf("said %v", out.events) + } + }}, + {"a ledger's snapshot store at 75% says full with the part snapshot", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) { + ledger := entry("records", catalogue.StorageLedger, catalogue.TemplateLedger, map[string]string{"max-mib": "1024"}) + k.Heard(Accepted{Module: "m", Revision: 1, BusState: []catalogue.BusStateEntry{ledger}}) + out.reset() + bus.fill["m_records_snapshot"] = 768 * uint64(MiB) + k.FillPass() + if f := out.bodies[0].Fill; fmt.Sprint(out.events) != "[full m records]" || f == nil || f.Object != "m_records_snapshot" || f.Part != "snapshot" { + t.Fatalf("said %v: full naming the snapshot store %+v", out.events, f) + } + }}, {"a plan whose caps pass the account's storage limit is refused, naming the sum and the limit, and the caps made stay (§547)", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) { // drafts: 64 MiB state + 16 MiB objects = 80 MiB; log: 1024 MiB. bus.limits = Limits{AccountBytes: 100 * MiB} diff --git a/modules/nats/internal/keeper/plan.go b/modules/nats/internal/keeper/plan.go index 1c794c953..63574870e 100644 --- a/modules/nats/internal/keeper/plan.go +++ b/modules/nats/internal/keeper/plan.go @@ -129,6 +129,29 @@ type Plan struct { Entry catalogue.BusStateEntry `json:"entry"` } +// PartOf is the place an object has in its entry's binding, for a ledger: `history`, `state`, `index ` or `snapshot`; +// empty for an entry of one object (design 47 H2 H-A-634 (9), H-A-687 (3)). +func (p Plan) PartOf(o Object) string { + if p.Storage != catalogue.StorageLedger { + return "" + } + b := p.Binding + switch { + case o.Name == b.History && b.History != "": + return "history" + case o.Name == b.State && b.State != "": + return "state" + case b.History != "" && o.Name == b.History+"_snapshot": + return "snapshot" + } + for name, bucket := range b.Indexes { + if o.Name == bucket { + return "index " + name + } + } + return "" +} + // BusName is `_`, the name the mesh gives an entry on the bus (design 25 § 12.1). func BusName(module, local string) string { return module + Separator + local }