diff --git a/modules/nats/cmd/nats-tools/main.go b/modules/nats/cmd/nats-tools/main.go index 9b43093f5..c2c000df5 100644 --- a/modules/nats/cmd/nats-tools/main.go +++ b/modules/nats/cmd/nats-tools/main.go @@ -191,7 +191,7 @@ func allTools() []stdio.Tool { defer cancel() return monitor(ctx, heldJsz) } - out := append(tools(monitor, ReadUsers, UsersWritten), removalTools(LiveBus(), SaidByTheRuntime, HeldInState)...) + out := append(tools(monitor, ReadUsers, UsersWritten), removalTools(LiveBus(), SaidByTheRuntime, HeldInState, RecordedBySeat)...) out = append(out, expiryTools(LiveBus(), SaidByTheRuntime, RecordedBySeat)...) out = append(out, brokerTools(HeldInState, StatusInState, jsz, StreamHistory(LiveBus()))...) out = append(out, keeperHealthTool(StatusInState, HeldInState, jsz)) diff --git a/modules/nats/cmd/nats-tools/removal.go b/modules/nats/cmd/nats-tools/removal.go index d6a9e0ae5..81597d4d0 100644 --- a/modules/nats/cmd/nats-tools/removal.go +++ b/modules/nats/cmd/nats-tools/removal.go @@ -20,6 +20,7 @@ import ( "strings" "time" + "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" stdio "git.novox.be/novox/mesh-sdk/go" "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" @@ -338,7 +339,30 @@ func RemoveStream(ctx context.Context, bus Bus, said Said, a map[string]any) (an // recreate, so a stream, a bucket or a consumer of a stream the keeper made (its state asked, read through newForm) is // refused, naming nats_drop, before the bus is reached; the old sections' entries are removed as before. A newForm // that cannot be read refuses too: what cannot be told apart is not removed. -func removalTools(bus Bus, said Said, newForm heldEntries) []stdio.Tool { +// +// **A consumer is judged by its name, not by its stream** (design 47 H, H-A-126, H-A-127): the mesh names a module's +// durable _, so a durable of that form for a module of an accepted manifest (the keeper's accepted set) is +// refused, on any stream; every other durable is an orphan and goes, `why` its warrant, at once, and its removal is +// recorded as a hand act through mesh-hand-acts, as nats_bucket_expiry's change is. +func removalTools(bus Bus, said Said, newForm heldEntries, record Recorded) []stdio.Tool { + removeConsumer := func(a map[string]any) (any, error) { + if err := refuseAcceptedDurable(newForm, arg(a, "consumer")); err != nil { + return nil, err + } + out, err := RemoveConsumer(context.Background(), bus, said, a) + if err != nil { + return nil, err + } + r := out.(ConsumerRemoved) + what := fmt.Sprintf("removed the consumer %s of the stream %s", r.Consumer, r.Stream) + if record == nil { + return nil, fmt.Errorf("%s WAS done (%s) but the hand-act log cannot be reached here", what, r.Why) + } + if err := record(meshhandactsclient.Request{What: what, Why: r.Why, Cause: "consumer-remove"}); err != nil { + return nil, fmt.Errorf("%s WAS done (%s) but the hand act could not be recorded: %w", what, r.Why, err) + } + return r, nil + } run := func(f func(context.Context, Bus, Said, map[string]any) (any, error)) func(map[string]any) (any, error) { return func(a map[string]any) (any, error) { stream := arg(a, "stream") @@ -353,9 +377,9 @@ func removalTools(bus Bus, said Said, newForm heldEntries) []stdio.Tool { } return []stdio.Tool{ {Name: "nats_consumer_remove", - Description: "Removes (deletes) one named JetStream consumer of one stream, e.g. a consumer the controller left, such as `controller` of DEAD_LETTER_NOTICES or `controller-merges` of EVENTS (novox/hq ADR 0590 §6, recorded as a hand act under issue 1126). Needs `why`, kept in the audit trail; refuses an unknown stream or consumer; reads the consumer first and answers what it was (filters, pending, unacknowledged); the stream and its other consumers are not touched. The bus refuses any consumer the module's credential is not granted.", + Description: "Removes (deletes) one named JetStream consumer of one stream, e.g. a consumer the controller left, such as `controller` of DEAD_LETTER_NOTICES or `controller-merges` of EVENTS (novox/hq ADR 0590 §6), or an orphan on a module's kept-data stream. Refuses a durable named _ for a module of an accepted manifest, on any stream; any other durable goes. Needs `why`, its warrant, kept in the audit trail and recorded as a hand act through mesh-hand-acts (design 47 H, H-A-127); refuses an unknown stream or consumer; reads the consumer first and answers what it was (filters, pending, unacknowledged); the stream and its other consumers are not touched. The bus refuses any consumer the module's credential is not granted.", Input: map[string]any{"stream": str("the stream, e.g. DEAD_LETTER_NOTICES"), "consumer": str("the consumer's name, e.g. controller"), "why": str("why it is removed, for the audit trail (required)")}, - Run: run(RemoveConsumer)}, + Run: removeConsumer}, {Name: "nats_stream_remove", Description: "Removes (deletes) one named JetStream stream whole, e.g. a stream the controller left: CONTROL, ASSIGNMENTS, one of its buckets as KV_ or a node's declaration files as OBJ_declaration-files- (novox/hq ADR 0590 §6; a bucket's whole removal is this tool's, never nats_bucket_remove's). Needs `why`, kept in the audit trail; reads the stream first and answers its messages, bytes and consumers; refuses a stream that still has a consumer; a stream already gone is answered as such and is no error, so asking again is safe. `dry_run` only reads. The bus refuses any stream the module's credential is not granted.", Input: map[string]any{"stream": str("the stream, e.g. ASSIGNMENTS"), "why": str("why it is removed, for the audit trail (required)"), "dry_run": map[string]any{"type": "boolean", "description": "read the stream, remove nothing"}}, @@ -373,6 +397,26 @@ func removalTools(bus Bus, said Said, newForm heldEntries) []stdio.Tool { } } +// refuseAcceptedDurable refuses a durable named _ for a module of the keeper's accepted set (design 47 H, +// H-A-127); a set that cannot be read refuses too: what cannot be told apart is not removed. +func refuseAcceptedDurable(newForm heldEntries, consumer string) error { + if newForm == nil { + return nil + } + node, module, ok := strings.Cut(consumer, "_") + if !ok || node == "" || module == "" { + return nil + } + held, err := newForm() + if err != nil { + return fmt.Errorf("whether %s is the durable of a module of an accepted manifest cannot be read (%v), so nothing was removed", consumer, err) + } + if _, ok := held[module]; ok { + return fmt.Errorf("%s is %s's durable on %s, a module of an accepted manifest: it is not removed; nothing was removed", consumer, module, node) + } + return nil +} + // refuseNewForm refuses a stream the keeper made for a module's bus state, naming the tool that removes it. func refuseNewForm(newForm heldEntries, stream string) error { if newForm == nil || stream == "" { diff --git a/modules/nats/cmd/nats-tools/removal_test.go b/modules/nats/cmd/nats-tools/removal_test.go index 680fee08c..3e5d47433 100644 --- a/modules/nats/cmd/nats-tools/removal_test.go +++ b/modules/nats/cmd/nats-tools/removal_test.go @@ -5,6 +5,7 @@ package main import ( "context" "fmt" + "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" "os" "path/filepath" "sort" @@ -191,6 +192,14 @@ accounts { MESH { jetstream: enabled, users: [ } // Issue 1126: the controller's leftovers were push consumers too, which js.Consumer refuses ("not a pull consumer"): // a pull and a push consumer are read, removed and confirmed alike. + // Through the tool, `why` its warrant: each removal is recorded as a hand act (design 47 H, H-A-127). + var handActs []meshhandactsclient.Request + var removeConsumer func(map[string]any) (any, error) + for _, tl := range removalTools(bus, say, nil, func(q meshhandactsclient.Request) error { handActs = append(handActs, q); return nil }) { + if tl.Name == "nats_consumer_remove" { + removeConsumer = tl.Run + } + } for _, row := range []struct{ name, kind, deliver string }{ {"controller-merges", "pull", ""}, {"controller", "push", "deliver.controller"}, @@ -200,7 +209,7 @@ accounts { MESH { jetstream: enabled, users: [ t.Fatal(err) } } - got, err := RemoveConsumer(ctx, bus, say, map[string]any{"stream": "EVENTS", "consumer": row.name, "why": "the controller ended"}) + got, err := removeConsumer(map[string]any{"stream": "EVENTS", "consumer": row.name, "why": "the controller ended"}) if err != nil { t.Fatalf("the controller's %s consumer %s of EVENTS: %v", row.kind, row.name, err) } @@ -214,6 +223,9 @@ accounts { MESH { jetstream: enabled, users: [ if _, err := ajs.Consumer(ctx, "EVENTS", "mesh-grants"); err != nil { t.Fatalf("the other consumer of EVENTS went: %v", err) } + if len(handActs) != 2 || handActs[0].Why != "the controller ended" || !strings.Contains(handActs[1].What, "controller of the stream EVENTS") { + t.Fatalf("the hand acts: %+v", handActs) + } if r, err := RemoveStream(ctx, bus, say, map[string]any{"stream": "KV_mesh-controller_rollouts", "why": "the controller ended"}); err != nil || !r.(StreamRemoved).Removed || r.(StreamRemoved).Messages != 1 { t.Fatalf("the controller's bucket: %+v %v", r, err) } diff --git a/modules/nats/cmd/nats-tools/tools_test.go b/modules/nats/cmd/nats-tools/tools_test.go index ce2b827d1..31880ebec 100644 --- a/modules/nats/cmd/nats-tools/tools_test.go +++ b/modules/nats/cmd/nats-tools/tools_test.go @@ -255,10 +255,12 @@ func TestTheRemovalToolsRefuseBeforeTheyReachTheBus(t *testing.T) { // CAT-N9: an entry of the new form goes only by drop or recreate; an old section's goes as before. {"nats_stream_remove", map[string]any{"stream": "m_log", "why": "x"}, "removed only by nats_drop"}, {"nats_bucket_remove", map[string]any{"bucket": "m_drafts", "node": "g14", "why": "x"}, "m's kept data \"drafts\""}, - {"nats_consumer_remove", map[string]any{"stream": "m_log", "consumer": "c", "why": "x"}, "removed only by nats_drop"}, + // Design 47 H, H-A-127: a consumer is judged by its name: an accepted module's durable is refused on any stream. + {"nats_consumer_remove", map[string]any{"stream": "EVENTS", "consumer": "novox_m", "why": "x"}, "a module of an accepted manifest"}, + {"nats_consumer_remove", map[string]any{"stream": "m_log", "consumer": "ace_m", "why": "x"}, "a module of an accepted manifest"}, } { var run func(map[string]any) (any, error) - for _, tl := range removalTools(bus, said, newForm) { + for _, tl := range removalTools(bus, said, newForm, nil) { if tl.Name == c.tool { run = tl.Run } @@ -270,6 +272,18 @@ func TestTheRemovalToolsRefuseBeforeTheyReachTheBus(t *testing.T) { t.Errorf("%s %v: %v, want a refusal saying %q", c.tool, c.args, err, c.want) } } + // An orphan on a module's kept-data stream is not refused by its stream (H-A-126): the call reaches the bus. + unreachable := func(context.Context) (jetstream.JetStream, func(), error) { + return nil, nil, errors.New("the bus is not reached here") + } + for _, tl := range removalTools(unreachable, said, newForm, nil) { + if tl.Name != "nats_consumer_remove" { + continue + } + if _, err := tl.Run(map[string]any{"stream": "m_log", "consumer": "c", "why": "an orphan"}); err == nil || !strings.Contains(err.Error(), "not reached") { + t.Errorf("an orphan on m_log: %v, want the call to reach the bus", err) + } + } // The read tools stay read tools: none of the first ten takes a `why`, and the list is these ten. for _, tl := range tools(nil, nil, nil) { if _, ok := tl.Input["why"]; ok || strings.Contains(tl.Name, "remove") {