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
+71 -7
View File
@@ -191,9 +191,12 @@ type Conn struct {
cred Credential
mu sync.Mutex
issued map[string]*Membership // module → membership; present with nil = followed, none issued
onNew []*func(Membership)
subs []*nats.Subscription
Logf func(format string, args ...any)
// withdrawn are the modules whose membership the grants part deleted: no longer assigned on this node (design 47 H2,
// H-A-472 ninth addendum); their twins are removed and not made again, until a membership is issued again.
withdrawn map[string]bool
onNew []*func(Membership)
subs []*nats.Subscription
Logf func(format string, args ...any)
// answering is every subject this runtime answers on, so a subscription the bus refused can be
// asked for again (novox/hq issue 222).
answering map[string]*answered
@@ -411,6 +414,7 @@ var (
func (c *Conn) issue(module string, m Membership) {
c.mu.Lock()
c.issued[module] = &m
delete(c.withdrawn, module)
handlers := make([]func(Membership), 0, len(c.onNew))
for _, h := range c.onNew {
handlers = append(handlers, *h)
@@ -494,7 +498,9 @@ func (c *Conn) watchGrants(module string, say bool, patience, timeout time.Durat
}
apply := func(e jetstream.KeyValueEntry) {
if e.Operation() != jetstream.KeyValuePut {
return // a membership withdrawn is the stream's, or the module's unassignment's, to say
// A membership withdrawn: the module is no longer assigned on this node, so its twins go (H-A-472 ninth addendum).
c.withdraw(js, module)
return
}
// Read through mesh-sdk's one codec of this state, the grants part's writer's (issue 774): it takes the bare
// membership and a wrapper the part wrote before of one published or unchanged, and refuses a held-back one's
@@ -541,6 +547,60 @@ func (c *Conn) watchGrants(module string, say bool, patience, timeout time.Durat
return true
}
// withdraw removes the runner's twin `<consumer>_tools` from each stream the module's last membership named, as the grants
// part withdrew the membership (design 47 H2, H-A-472 ninth addendum): the module left this node. The membership is kept as
// it was, and the module marked withdrawn, so no hearing makes a twin again. A module with no membership issued names no
// stream, so a delete read at the start removes nothing (the runner removes nothing at its start). Each removal is said; a
// twin the bus does not hold is no failure.
func (c *Conn) withdraw(js jetstream.JetStream, module string) {
c.mu.Lock()
m := c.issued[module]
if c.withdrawn == nil {
c.withdrawn = map[string]bool{}
}
c.withdrawn[module] = true
c.mu.Unlock()
if m == nil {
return
}
for _, h := range m.Hears {
if h.Stream == "" || h.Consumer == "" {
continue
}
twin := h.Consumer + ToolsSuffix
ctx, cancel := context.WithTimeout(context.Background(), StateTimeout)
err := js.DeleteConsumer(ctx, h.Stream, twin)
cancel()
switch {
case err == nil:
c.Logf("[mesh-tools] %s's membership on %s was withdrawn; its consumer %s on %s is removed", module, c.node, twin, h.Stream)
case errors.Is(err, jetstream.ErrConsumerNotFound):
default:
c.Logf("[mesh-tools] %s's membership on %s was withdrawn, and its consumer %s on %s cannot be removed: %v", module, c.node, twin, h.Stream, err)
}
}
}
// Withdrawn says whether the grants part withdrew the module's membership here, and none was issued since.
func (c *Conn) Withdrawn(module string) bool {
c.mu.Lock()
defer c.mu.Unlock()
return c.withdrawn[module]
}
// heardOn is the entry of the module's membership for a stream, or, where it names none, the entry an older membership
// implies: the module's consumer by name, bound and never made.
func (c *Conn) heardOn(module, stream string) HeardIssued {
if m := c.Membership(module); m != nil {
for _, h := range m.Hears {
if h.Stream == stream && h.Consumer != "" {
return h
}
}
}
return HeardIssued{Stream: stream, Consumer: ConsumerOf(c.node, module)}
}
// Membership is what the mesh issued a module here, or nil when nothing has been issued.
func (c *Conn) Membership(module string) *Membership {
c.mu.Lock()
@@ -1205,9 +1265,13 @@ func (c *Conn) ConsumeAsBeatingWhen(module string, deliver func(Envelope) error,
missed.Store(false) // an event arrived: the consumer is heard, whatever was said before
return deliver(env)
})
// Bound by name, never created and never matched against a subject: the consumer and its
// filters are the mesh's (design 29 §3), exactly as the module's own runtime bound it.
events, _, doing, err := h.bind(EventsStream, durable, func(err error) {
// Made first from the membership's spec where its EVENTS entry carries one (H-A-472 fifth to eighth addenda), else bound
// by name: then the consumer and its filters are the mesh's (design 29 §3), exactly as the module's own runtime bound it.
heard := c.heardOn(module, EventsStream)
if c.Withdrawn(module) {
heard.Filters = nil
}
events, _, doing, err := h.bind(heard, func(err error) {
if errors.Is(err, jetstream.ErrNoHeartbeat) {
say("the bus's heartbeat to the consumer " + durable + " stopped arriving")
}