node-tools: the runner makes its twin from the membership's spec, removes it when withdrawn

The grants part no longer composes the tool runner's `_tools` twin: at a child's
first subscribe the runner creates or updates `<node>_<module>_tools` on EVENTS and
on every heard stream from the spec the membership's entry carries (filters, ack
wait, max deliver, max ack pending, deliver policy), and binds by name an entry an
older grants part wrote without one. When the grants part withdraws the module's
membership on this node, the runner removes the twins from the streams the last
entry named and makes none again until a membership is issued; it removes nothing
at its start. The heard entry is mesh-sdk's type, so the hearing keys its loops by
stream and consumer.

Builds: design 47 H2 H-A-472 (fifth, sixth, seventh, eighth and ninth addenda)
This commit is contained in:
2026-11-22 19:35:08 +01:00
parent 51a24ea801
commit ba82044cc0
3 changed files with 264 additions and 31 deletions
+75 -24
View File
@@ -3,6 +3,7 @@ package bus
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"sync"
@@ -11,6 +12,7 @@ import (
"github.com/nats-io/nats.go/jetstream"
"git.novox.be/novox/mesh-sdk/go/membership"
"git.novox.be/novox/mesh-sdk/go/process"
)
@@ -31,11 +33,54 @@ type StreamIssued struct {
Subject string `json:"subject"`
}
// HeardIssued is one module stream an assignment hears: the stream and the durable consumer on it the
// runtime binds. The consumer is the mesh's to make, to filter and to delete.
type HeardIssued struct {
Stream string `json:"stream"`
Consumer string `json:"consumer"`
// HeardIssued is one stream an assignment hears, EVENTS like any other: the stream, the module's durable consumer the
// runner's twin on it is named from, and, from a grants part that writes it, the twin's whole spec (mesh-sdk's
// membership.HeardIssued; design 47 H2, H-A-472 seventh and eighth addenda). An entry with a spec is created or updated
// from it by the runner at a child's first subscribe; one without, an older writer's, is bound by name as before.
type HeardIssued = membership.HeardIssued
// heardKey is one consumer a hearing binds: the stream and the runner's twin on it. The spec rides beside it, never in
// it, so a map is keyed by the consumer alone.
type heardKey struct{ Stream, Consumer string }
// twinConfig is the configuration of the runner's twin of a heard entry with a spec: its filters, ack wait, max deliver,
// max ack pending and deliver policy as the grants part composed them, under the twin's own name. Every filter goes in
// FilterSubjects, one alone too, so the create is on the name alone, the form the reader is granted.
func twinConfig(h HeardIssued) (jetstream.ConsumerConfig, error) {
cfg := jetstream.ConsumerConfig{
Durable: h.Consumer + ToolsSuffix,
AckPolicy: jetstream.AckExplicitPolicy,
AckWait: time.Duration(h.AckWaitSeconds) * time.Second,
MaxDeliver: h.MaxDeliver,
MaxAckPending: h.MaxAckPending,
FilterSubjects: append([]string(nil), h.Filters...),
}
switch h.DeliverPolicy {
case membership.DeliverFromNow:
cfg.DeliverPolicy = jetstream.DeliverNewPolicy
case membership.DeliverLastPerSubject:
cfg.DeliverPolicy = jetstream.DeliverLastPerSubjectPolicy
case membership.DeliverAll, "":
cfg.DeliverPolicy = jetstream.DeliverAllPolicy
default:
return cfg, fmt.Errorf("the membership names the deliver policy %q, which the runner does not know", h.DeliverPolicy)
}
return cfg, nil
}
// makeTwin creates or updates the runner's twin of a heard entry from its spec (H-A-472 fifth to seventh addenda): what
// the bus lacks is made as a part's process makes it, and one held is brought to the spec, its delivery position kept.
// An entry without a spec makes nothing: its consumer is the grants part's, bound by name.
func makeTwin(ctx context.Context, js jetstream.JetStream, h HeardIssued) error {
if !h.HasSpec() {
return nil
}
cfg, err := twinConfig(h)
if err != nil {
return err
}
_, err = js.CreateOrUpdateConsumer(ctx, h.Stream, cfg)
return err
}
// eventSubject is where a module's event is published: on the subject of the first of its membership's
@@ -94,15 +139,15 @@ type hearing struct {
mu sync.Mutex
stopped bool
bound map[HeardIssued]jetstream.ConsumeContext
bound map[heardKey]jetstream.ConsumeContext
// made is when the bus made each bound consumer: a consumer deleted and made again under its name has
// another time, and a loop bound to the old one is not reading the new one.
made map[HeardIssued]time.Time
said map[HeardIssued]bool
made map[heardKey]time.Time
said map[heardKey]bool
// why is the last error the bus gave a consumer's loop, said when the loop ends on it.
// Guarded by whyMu, not mu: the bus hands errors from its own goroutines, possibly while mu is held.
whyMu sync.Mutex
why map[HeardIssued]error
why map[heardKey]error
kick chan struct{}
done chan struct{}
unhear func()
@@ -115,16 +160,21 @@ type hearing struct {
func newHearing(c *Conn, js jetstream.JetStream, module string, deliver func(Envelope) error) *hearing {
return &hearing{c: c, js: js, module: module, deliver: deliver,
bound: map[HeardIssued]jetstream.ConsumeContext{}, made: map[HeardIssued]time.Time{}, said: map[HeardIssued]bool{}, why: map[HeardIssued]error{},
bound: map[heardKey]jetstream.ConsumeContext{}, made: map[heardKey]time.Time{}, said: map[heardKey]bool{}, why: map[heardKey]error{},
kick: make(chan struct{}, 1), done: make(chan struct{}),
rebind: RebindEvery, nakDelay: NakDelay, heartbeat: EventHeartbeat}
}
// bind reads one durable consumer by name, and on failure says what it was doing. Errors the bus gives
// the loop afterwards are handed to onErr, when given.
func (h *hearing) bind(stream, durable string, onErr func(error)) (jetstream.ConsumeContext, time.Time, string, error) {
// bind reads the runner's twin of one heard entry, made first from the entry's spec where it carries one (makeTwin), else
// bound by name, and on failure says what it was doing. Errors the bus gives the loop afterwards are handed to onErr, when
// given.
func (h *hearing) bind(heard HeardIssued, onErr func(error)) (jetstream.ConsumeContext, time.Time, string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
stream, durable := heard.Stream, heard.Consumer+ToolsSuffix
if err := makeTwin(ctx, h.js, heard); err != nil {
return nil, time.Time{}, "making", err
}
consumer, err := h.js.Consumer(ctx, stream, durable)
if err != nil {
return nil, time.Time{}, "binding", err
@@ -224,7 +274,7 @@ func (h *hearing) start() {
}
// forget drops the last error a consumer's loop was given.
func (h *hearing) forget(heard HeardIssued) {
func (h *hearing) forget(heard heardKey) {
h.whyMu.Lock()
delete(h.why, heard)
h.whyMu.Unlock()
@@ -260,7 +310,7 @@ func (h *hearing) stop() {
// replaced says whether the consumer a loop is bound to is gone from the bus or was made again since. A bus
// that cannot be asked right now says no: the loop is left as it is.
func (h *hearing) replaced(heard HeardIssued) bool {
func (h *hearing) replaced(heard heardKey) bool {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
consumer, err := h.js.Consumer(ctx, heard.Stream, heard.Consumer)
@@ -276,13 +326,14 @@ func (h *hearing) replaced(heard HeardIssued) bool {
// reconcile makes what is bound what the module's membership names now.
func (h *hearing) reconcile() {
want := map[HeardIssued]bool{}
if m := h.c.Membership(h.module); m != nil {
want := map[heardKey]HeardIssued{}
// A module whose membership was withdrawn hears nothing more here, and its twins are not made again (H-A-472 ninth addendum).
if m := h.c.Membership(h.module); m != nil && !h.c.Withdrawn(h.module) {
for _, heard := range m.Hears {
if heard.Stream != "" && heard.Consumer != "" {
// EVENTS is its caller's, bound beside the hearing (ConsumeAsBeatingWhen).
if heard.Stream != "" && heard.Consumer != "" && heard.Stream != EventsStream {
// The runner's own durable beside the program's (A-397).
heard.Consumer += ToolsSuffix
want[heard] = true
want[heardKey{heard.Stream, heard.Consumer + ToolsSuffix}] = heard
}
}
}
@@ -292,7 +343,7 @@ func (h *hearing) reconcile() {
return
}
for heard, reading := range h.bound {
if !want[heard] {
if _, still := want[heard]; !still {
reading.Stop()
delete(h.bound, heard)
delete(h.made, heard)
@@ -301,7 +352,7 @@ func (h *hearing) reconcile() {
}
}
for heard := range h.said {
if !want[heard] {
if _, still := want[heard]; !still {
delete(h.said, heard)
h.forget(heard)
}
@@ -340,7 +391,7 @@ func (h *hearing) reconcile() {
default:
}
}
missing := make([]HeardIssued, 0, len(want))
missing := make([]heardKey, 0, len(want))
for heard := range want {
if h.bound[heard] == nil {
missing = append(missing, heard)
@@ -350,7 +401,7 @@ func (h *hearing) reconcile() {
for _, heard := range missing {
heard := heard
h.forget(heard)
reading, created, doing, err := h.bind(heard.Stream, heard.Consumer, func(err error) {
reading, created, doing, err := h.bind(want[heard], func(err error) {
h.whyMu.Lock()
h.why[heard] = err
h.whyMu.Unlock()