The keeper's hearer binds <node>_nats_keeper on the kept streams of registration-changed, source-moved and findings-asked with its one handler; a stream it cannot bind is a journal line and the finding events-unheard through its held writer, and is bound again on the grants part's durables-composed and at a reconnect. Builds: design 47 H §836 5. (issue 1984 AC-836g)
148 lines
5.4 KiB
Go
148 lines
5.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
|
meshgrants "git.novox.be/novox/mesh-contracts/identity-and-access/go/seats/meshgrants"
|
|
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/sourcebranches"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
|
"git.novox.be/novox/mesh-sdk/go/views"
|
|
"nats-tools/internal/keeper"
|
|
)
|
|
|
|
// keptConsumed are the kept seat events the bus module's module consumes, each on its own stream `SEAT_<seat>_<event>`, where the
|
|
// grants part composes the keeper a durable named as its durable on EVENTS (design 47 H2b §836 5.).
|
|
var keptConsumed = []struct{ seat, event string }{
|
|
{catalog.Seat, catalog.EventRegistrationChanged},
|
|
{sourcebranches.Seat, sourcebranches.EventSourceMoved},
|
|
{meshconditions.Seat, meshconditions.EventFindingsAsked},
|
|
}
|
|
|
|
// keptHearing binds the keeper's durable on each kept stream it consumes and pulls it with the keeper's one handler. A stream
|
|
// it cannot bind is said (the journal, and unheard), and bound again when the grants part says it composed the module's durables
|
|
// and at the bus connection's reconnect, never on a timer.
|
|
type keptHearing struct {
|
|
js jetstream.JetStream
|
|
a process.Account
|
|
deliver func(jetstream.Msg)
|
|
unheard func([]process.Unheard)
|
|
logf func(string, ...any)
|
|
|
|
mu sync.Mutex // one pass at a time
|
|
bind map[string]bool
|
|
told bool
|
|
last []process.Unheard
|
|
}
|
|
|
|
// start makes the wake and the reconnect hook, then binds what it can.
|
|
func (k *keptHearing) start(ctx context.Context) {
|
|
k.bind = map[string]bool{}
|
|
nc := k.js.Conn()
|
|
sub, err := nc.Subscribe(meshgrants.SubjectDurablesComposed(k.a.Node, k.a.Module), func(*nats.Msg) { k.pass(ctx) })
|
|
if err != nil {
|
|
k.logf("the grants part's word of the module's durables is not heard (%v): a stream not bound is bound at the bus connection's reconnect or the next start", err)
|
|
} else {
|
|
go func() { <-ctx.Done(); _ = sub.Unsubscribe() }()
|
|
}
|
|
before := nc.Opts.ReconnectedCB
|
|
nc.SetReconnectHandler(func(c *nats.Conn) {
|
|
if before != nil {
|
|
before(c)
|
|
}
|
|
k.pass(ctx)
|
|
})
|
|
k.pass(ctx)
|
|
}
|
|
|
|
// pass binds every kept stream's durable not bound yet, and says the streams it could not.
|
|
func (k *keptHearing) pass(ctx context.Context) {
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
k.mu.Lock()
|
|
defer k.mu.Unlock()
|
|
var not []process.Unheard
|
|
for _, c := range keptConsumed {
|
|
stream := seatdef.KeptStreamPrefix + c.seat + "_" + c.event
|
|
if k.bind[stream] {
|
|
continue
|
|
}
|
|
pattern := seatdef.EventSubject(c.seat, c.event) + ".>"
|
|
if why := k.bindOne(ctx, stream); why != "" {
|
|
not = append(not, process.Unheard{Pattern: pattern, Stream: stream, Why: why})
|
|
}
|
|
}
|
|
if k.told && slices.Equal(not, k.last) {
|
|
return
|
|
}
|
|
k.told, k.last = true, not
|
|
for _, u := range not {
|
|
k.logf("events: the pattern %s is not heard on %s: %s", u.Pattern, u.Stream, u.Why)
|
|
}
|
|
if k.unheard != nil {
|
|
k.unheard(slices.Clone(not))
|
|
}
|
|
}
|
|
|
|
// bindOne binds the keeper's durable on stream; the why it could not, or "".
|
|
func (k *keptHearing) bindOne(ctx context.Context, stream string) string {
|
|
cons, err := k.js.Consumer(ctx, stream, k.a.QueueConsumerName())
|
|
switch {
|
|
case errors.Is(err, jetstream.ErrStreamNotFound):
|
|
return "no stream carries it"
|
|
case errors.Is(err, jetstream.ErrConsumerNotFound):
|
|
return "not composed"
|
|
case err != nil:
|
|
return fmt.Sprintf("refused: %v", err)
|
|
}
|
|
cc, err := process.HearEvents(cons, k.deliver, k.logf)
|
|
if err != nil {
|
|
return fmt.Sprintf("refused: %v", err)
|
|
}
|
|
k.bind[stream] = true
|
|
go func() { <-ctx.Done(); cc.Stop() }()
|
|
return ""
|
|
}
|
|
|
|
// unheardFinding raises the module's finding events-unheard through the held writer, naming each pattern not heard, and clears it
|
|
// on an empty list (design 47 H2b §836 4., 5.).
|
|
func unheardFinding(w interface {
|
|
Write(ctx context.Context, finding meshconditions.Finding) error
|
|
Clear(ctx context.Context, scope, id, kind, why string) error
|
|
}, a process.Account, logf func(string, ...any)) func([]process.Unheard) {
|
|
id := a.Module + "." + a.Node
|
|
return func(list []process.Unheard) {
|
|
ctx := context.Background()
|
|
if len(list) == 0 {
|
|
if err := w.Clear(ctx, "module", id, views.EventsUnheardKind, "the part hears every event it consumes"); err != nil {
|
|
logf("the finding %s cannot be cleared: %v", views.EventsUnheardKind, err)
|
|
}
|
|
return
|
|
}
|
|
var named []string
|
|
for _, u := range list {
|
|
named = append(named, fmt.Sprintf("%s on %s (%s)", u.Pattern, u.Stream, u.Why))
|
|
}
|
|
f := meshconditions.Finding{Scope: "module", ID: id, Kind: views.EventsUnheardKind,
|
|
Subject: meshconditions.Subject{Scope: "module", ID: keeper.Module},
|
|
Severity: meshconditions.FindingSeverityWarning, Headline: "The bus module does not hear all its events",
|
|
Summary: fmt.Sprintf("%s on %s does not hear %d event patterns: %v: what it consumes there it does not act on; a bind the bus refuses while its durable stands is made again by the node's service manager restarting the module's unit",
|
|
keeper.Module, a.Node, len(list), named),
|
|
Needs: "the grants part's durables composed, or the module's unit restarted", Source: "events", Observer: keeper.Module, At: time.Now().UTC()}
|
|
if err := w.Write(ctx, f); err != nil {
|
|
logf("the finding %s cannot be written: %v", views.EventsUnheardKind, err)
|
|
}
|
|
}
|
|
}
|