diff --git a/modules/nats/cmd/nats-tools/removal.go b/modules/nats/cmd/nats-tools/removal.go index 69161828f..cf6113a78 100644 --- a/modules/nats/cmd/nats-tools/removal.go +++ b/modules/nats/cmd/nats-tools/removal.go @@ -357,7 +357,7 @@ func removalTools(bus Bus, said Said, newForm heldEntries, accepted acceptedModu if twinWithin != nil { within = twinWithin() } - twin, err := refuseAcceptedDurable(context.Background(), bus, accepted, arg(a, "stream"), arg(a, "consumer"), time.Now(), within) + twin, never, err := refuseAcceptedDurable(context.Background(), bus, accepted, arg(a, "stream"), arg(a, "consumer"), time.Now(), within) if err != nil { return nil, err } @@ -370,6 +370,9 @@ func removalTools(bus Bus, said Said, newForm heldEntries, accepted acceptedModu if twin != "" { what += fmt.Sprintf(", an orphan: its module's twin %s on that stream is current (design 47 H2, H-A-472)", twin) } + if never { + what += ", never delivered and no pull waiting, so no module reads it (design 47 H, H-A-852, H-A-868)" + } if record == nil { return nil, fmt.Errorf("%s WAS done (%s) but the hand-act log cannot be reached here", what, r.Why) } @@ -392,7 +395,7 @@ func removalTools(bus Bus, said Said, newForm heldEntries, accepted acceptedModu } 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), or an orphan on a module's kept-data stream. Refuses a durable named _ or beginning __ for a module of an accepted manifest, on any stream, unless it is an orphan: no pulls waiting on it while its twin (_ for __tools, and back) on the same stream is current, with pulls waiting and a delivery within the setting twin-current-hours (24 by default), both read at the call, and the hand act names that twin (design 47 H2, H-A-472); 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.", + 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 _ or beginning __ for a module of an accepted manifest, on any stream, unless it is an orphan: no pulls waiting on it and either it was never delivered (the consumer's delivered sequence 0), whatever its twin (design 47 H, H-A-852), or its twin (_ for __tools, and back) on the same stream is current, with pulls waiting and a delivery within the setting twin-current-hours (24 by default), both read at the call, and the hand act names that twin (design 47 H2, H-A-472); 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: removeConsumer}, {Name: "nats_stream_remove", @@ -615,18 +618,19 @@ func NarrowConsumer(ctx context.Context, bus Bus, accepted acceptedModules, cons // module's program's durable _ and its tool runner's __tools (design 47 G, A-340): a module // that moved into the tool runner left the first, one that runs its own program again left the second. A module's // only durable, any other suffix, a durable with pulls waiting, and a durable or twin that cannot be read or a twin -// that is not current, stay refused (H-A-127/128). within is the bound of "current", the setting twin-current-hours. -func refuseAcceptedDurable(ctx context.Context, bus Bus, accepted acceptedModules, stream, consumer string, now time.Time, within time.Duration) (string, error) { +// that is not current, stay refused (H-A-127/128), but a durable never delivered (the consumer's delivered sequence 0) with no pull +// waiting goes whatever its twin (H-A-852). within is the bound of "current", the setting twin-current-hours. +func refuseAcceptedDurable(ctx context.Context, bus Bus, accepted acceptedModules, stream, consumer string, now time.Time, within time.Duration) (string, bool, error) { if accepted == nil { - return "", nil + return "", false, nil } node, module, ok := strings.Cut(consumer, "_") if !ok || node == "" || module == "" { - return "", nil + return "", false, nil } held, err := accepted() 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) + return "", false, fmt.Errorf("whether %s is the durable of a module of an accepted manifest cannot be read (%v), so nothing was removed", consumer, err) } for m := range held { if module != m && !strings.HasPrefix(module, m+"_") { @@ -635,34 +639,40 @@ func refuseAcceptedDurable(ctx context.Context, bus Bus, accepted acceptedModule refused := fmt.Errorf("%s is %s's durable on %s, a module of an accepted manifest: it is not removed; nothing was removed", consumer, m, node) twin := twinOf(node, m, module) if twin == "" || bus == nil || !name.MatchString(stream) { - return "", refused + return "", false, refused } js, closeBus, err := bus(ctx) if err != nil { - return "", fmt.Errorf("%v (it and its twin %s on %s cannot be read: %v)", refused, twin, stream, err) + return "", false, fmt.Errorf("%v (it and its twin %s on %s cannot be read: %v)", refused, twin, stream, err) } defer closeBus() sctx, cancel := step(ctx) self, err := readConsumer(sctx, js, stream, consumer) cancel() if err != nil { - return "", fmt.Errorf("%v (it cannot be read on %s: %v)", refused, stream, err) + return "", false, fmt.Errorf("%v (it cannot be read on %s: %v)", refused, stream, err) } if self.NumWaiting > 0 { - return "", fmt.Errorf("%v (it is no orphan: %d pulls waiting on it)", refused, self.NumWaiting) + return "", false, fmt.Errorf("%v (it is no orphan: %d pulls waiting on it)", refused, self.NumWaiting) + } + if self.Delivered.Consumer == 0 { + // Design 47 H §837 1., H-A-852, H-A-860, H-A-868: a durable never delivered (the consumer's own count of + // deliveries, 0; never the stream sequence, which a durable made from the stream's end reads as its start) + // with no pull waiting is removed whatever its twin; there is no twin to name. + return "", true, nil } sctx, cancel = step(ctx) info, err := readConsumer(sctx, js, stream, twin) cancel() if err != nil { - return "", fmt.Errorf("%v (its twin %s on %s cannot be read: %v)", refused, twin, stream, err) + return "", false, fmt.Errorf("%v (its twin %s on %s cannot be read: %v)", refused, twin, stream, err) } if !current(info, now, within) { - return "", fmt.Errorf("%v (its twin %s on %s is not current: %d pulls waiting, last delivery %s, the bound %s)", refused, twin, stream, info.NumWaiting, lastDelivery(info), within) + return "", false, fmt.Errorf("%v (its twin %s on %s is not current: %d pulls waiting, last delivery %s, the bound %s)", refused, twin, stream, info.NumWaiting, lastDelivery(info), within) } - return twin, nil + return twin, false, nil } - return "", nil + return "", false, nil } // twinOf names the twin of a module's durable: __tools for _, and back; "" for any other. diff --git a/modules/nats/cmd/nats-tools/removal_test.go b/modules/nats/cmd/nats-tools/removal_test.go index e6301ac9a..1a424f600 100644 --- a/modules/nats/cmd/nats-tools/removal_test.go +++ b/modules/nats/cmd/nats-tools/removal_test.go @@ -240,9 +240,22 @@ accounts { MESH { jetstream: enabled, users: [ return js, nc.Close, err } accepted := func() (map[string]keeper.Planned, error) { - return map[string]keeper.Planned{"m": {Module: "m"}, "q": {Module: "q"}, "solo": {Module: "solo"}, "both": {Module: "both"}}, nil + return map[string]keeper.Planned{"m": {Module: "m"}, "q": {Module: "q"}, "solo": {Module: "solo"}, "both": {Module: "both"}, "held": {Module: "held"}}, nil } - for _, d := range []string{"novox_m", "novox_m_tools", "ace_q", "ace_q_tools", "ace_solo", "g14_both", "g14_both_tools"} { + if _, err := ajs.Publish(ctx, "mesh.events.one", []byte("{}")); err != nil { + t.Fatal(err) + } + // Made after a message, from the stream's end, as the grants part makes them (H-A-868): the stream sequence reads the + // stream's position before their start (1 for ace_q, 2 for ace_held), the consumer's own count of deliveries 0. + // ace_q is never delivered, no pull on it. ace_held is made below, after the second message, so its held fetch + // delivers nothing. + fromEnd := func(d string) { + if _, err := ajs.CreateConsumer(ctx, "EVENTS", jetstream.ConsumerConfig{Durable: d, AckPolicy: jetstream.AckExplicitPolicy, DeliverPolicy: jetstream.DeliverNewPolicy}); err != nil { + t.Fatal(err) + } + } + fromEnd("ace_q") + for _, d := range []string{"novox_m", "novox_m_tools", "ace_q_tools", "ace_solo", "g14_both", "g14_both_tools"} { if _, err := ajs.CreateConsumer(ctx, "EVENTS", jetstream.ConsumerConfig{Durable: d, AckPolicy: jetstream.AckExplicitPolicy}); err != nil { t.Fatal(err) } @@ -250,7 +263,8 @@ accounts { MESH { jetstream: enabled, users: [ if _, err := ajs.Publish(ctx, "mesh.events.one", []byte("{}")); err != nil { t.Fatal(err) } - for _, d := range []string{"novox_m_tools", "ace_q", "g14_both", "g14_both_tools"} { + fromEnd("ace_held") + for _, d := range []string{"novox_m", "novox_m_tools", "ace_q_tools", "ace_solo", "g14_both", "g14_both_tools"} { c, _ := ajs.Consumer(ctx, "EVENTS", d) if _, err := c.Next(jetstream.FetchMaxWait(2 * time.Second)); err != nil { t.Fatalf("a delivery to %s: %v", d, err) @@ -258,7 +272,7 @@ accounts { MESH { jetstream: enabled, users: [ } // novox_m_tools has a pull waiting (a fetch held open on the admin's connection); ace_q has none; both twins of // g14_both have one each (the third addendum: a durable with pulls waiting is no orphan, its twin current or not). - for _, d := range []string{"novox_m_tools", "g14_both", "g14_both_tools"} { + for _, d := range []string{"novox_m_tools", "g14_both", "g14_both_tools", "ace_held"} { live, _ := ajs.Consumer(ctx, "EVENTS", d) held, err := live.Consume(func(jetstream.Msg) {}, jetstream.PullExpiry(time.Minute)) if err != nil { @@ -278,6 +292,12 @@ accounts { MESH { jetstream: enabled, users: [ } } } + if ci, err := func() (*jetstream.ConsumerInfo, error) { + c, _ := ajs.Consumer(ctx, "EVENTS", "ace_held") + return c.Info(ctx) + }(); err != nil || ci.Delivered.Consumer != 0 || ci.NumWaiting != 1 { + t.Fatalf("ace_held must be never delivered with one pull held: %+v %v", ci, err) + } handActs = nil for _, tl := range removalTools(adminBus, say, nil, accepted, func(q meshhandactsclient.Request) error { handActs = append(handActs, q); return nil }, nil, nil) { if tl.Name == "nats_consumer_remove" { @@ -287,10 +307,12 @@ accounts { MESH { jetstream: enabled, users: [ for _, row := range []struct{ consumer, refused string }{ {"novox_m_tools", "it is no orphan: 1 pulls waiting on it"}, {"ace_q_tools", "its twin ace_q on EVENTS is not current"}, + {"ace_held", "it is no orphan: 1 pulls waiting on it"}, // consumer sequence 0 (asserted above), a pull held: the pull check comes before the never-delivered rule (1984/AC-837b) {"ace_solo", "a module of an accepted manifest"}, {"g14_both", "it is no orphan: 1 pulls waiting on it"}, {"g14_both_tools", "it is no orphan: 1 pulls waiting on it"}, {"novox_m", ""}, + {"ace_q", ""}, // 1984/AC-837b, H-A-852, H-A-868: never delivered (consumer sequence 0), no pull waiting, its twin not current: it goes } { _, err := removeConsumer(map[string]any{"stream": "EVENTS", "consumer": row.consumer, "why": "an orphaned durable"}) _, still := ajs.Consumer(ctx, "EVENTS", row.consumer) @@ -304,7 +326,7 @@ accounts { MESH { jetstream: enabled, users: [ t.Fatalf("%s: %v (still there: %v), want a refusal saying %q", row.consumer, err, still == nil, row.refused) } } - if len(handActs) != 1 || !strings.Contains(handActs[0].What, "novox_m of the stream EVENTS") || !strings.Contains(handActs[0].What, "twin novox_m_tools") { + if len(handActs) != 2 || !strings.Contains(handActs[1].What, "ace_q of the stream EVENTS") || !strings.Contains(handActs[1].What, "never delivered and no pull waiting") || !strings.Contains(handActs[0].What, "novox_m of the stream EVENTS") || !strings.Contains(handActs[0].What, "twin novox_m_tools") { t.Fatalf("the orphan's hand act: %+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 { @@ -353,7 +375,7 @@ accounts { MESH { jetstream: enabled, users: [ if k := keys(other); k != "g14.1" { t.Fatalf("another bucket changed: %s", k) } - if strings.Join(said, ",") != "consumer-removed,stream-removed,consumer-removed,consumer-removed,consumer-removed,stream-removed,bucket-keys-removed" { + if strings.Join(said, ",") != "consumer-removed,stream-removed,consumer-removed,consumer-removed,consumer-removed,consumer-removed,stream-removed,bucket-keys-removed" { t.Fatalf("the audit events said: %v", said) } } diff --git a/modules/nats/module.json b/modules/nats/module.json index a94ab7a54..96abb46c7 100644 --- a/modules/nats/module.json +++ b/modules/nats/module.json @@ -97,7 +97,7 @@ }, "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): nats_consumer_remove removes a module's durable with no pulls waiting only while its twin has pulls waiting and a delivery within these hours. The grants part's manifest names the same setting for its orphaned durables. The tool reads it as its configuration at each call.", + "description": "How many hours since a module's durable's twin last delivered it is current (design 47 H2, H-A-472): nats_consumer_remove removes a module's durable with no pulls waiting only while its twin has pulls waiting and a delivery within these hours, and removes one never delivered (the consumer's own delivered sequence 0) with no pulls waiting whatever its twin (H-A-852, H-A-868). The grants part's manifest names the same setting for its orphaned durables. The tool reads it as its configuration at each call.", "minimum": 1, "default": 24, "runtime": true