Files
mesh-delivery/modules/mesh-grants/internal/buscompose/kept.go
T
jschoubben 3f25e659ba grants: a seat's holder hears its shared worker created and deleted
The builder binds the seat's worker when the bus says it was created and again after a deletion, but no holder was
granted those advisories, so under scoped grants it would never hear them. Each holder of a seat with an accept not
pulled by kind is now granted the subscribe of CREATED and DELETED for that seat's worker. The worker the grants part
creates is SeatQueue's as returned, every shared setting copied, and a test holds it equal to SeatQueue's.

Builds: design 47 H2 H-A-468 (1), addendum, second addendum
2026-11-22 11:37:03 +01:00

170 lines
7.5 KiB
Go

package buscompose
import (
"maps"
"slices"
"sort"
"strings"
"time"
"github.com/nats-io/nats.go/jetstream"
sdkbc "git.novox.be/novox/mesh-sdk/go/buscompose"
"git.novox.be/novox/mesh-sdk/go/seatdef"
)
// **A seat's event kept in a stream of its own** (novox/hq design 47, *What each step says*; the seat definition's `keep`):
// the node-engine's `running.<node>.<module>` last per subject with no age limit, the gate's and the checker's `owed` for a
// week within 64 MiB. The event's subject lies under `mesh.seat.*.event.>`, which the shared events stream already takes, and
// the bus refuses two streams taking one subject: so the kept stream takes no subject of its own but sources the shared
// stream's messages of that subject, and keeps them by its own limits (the shared stream's seven days never end a
// `running`). The part makes the stream, and each hearing module's durable on it from the module's `consumes` (the bus name
// is seatdef.KeptStream's, the one name for every part that hears it).
// KeptEvent is a seat's event with a stream of its own.
type KeptEvent struct {
Seat string
Event string
// Stream is its name on the bus (seatdef.KeptStream).
Stream string
// Filter is the subject that carries every instance of the event, a part of its subject `*`.
Filter string
// Last says the stream keeps the last message of each subject; MaxAgeDays (0 for no age limit) and MaxMiB bound it
// as the event's keep says, its defaults 90 days and 16 MiB (design 47 G §396, §331).
Last bool
MaxAgeDays int
MaxMiB int
Why string
}
// KeptEventsOf is every event the seat definitions keep in a stream of its own, sorted by stream name. Pure.
func KeptEventsOf(defs map[string]seatdef.Definition) []KeptEvent {
var out []KeptEvent
for _, d := range defs {
for _, e := range d.Events {
if e.Keep == nil {
continue
}
out = append(out, KeptEvent{Seat: d.Seat, Event: e.Name, Stream: seatdef.KeptStream(d.Seat, e),
Filter: seatdef.EventSubject(d.Seat, e.Local()), Last: e.KeptLastPerSubject(), MaxAgeDays: e.KeptAgeDays(), MaxMiB: e.KeptMiB(),
Why: "the seat " + d.Seat + "'s event " + e.Name + ", kept apart from the shared events stream (design 47): " + keptHow(e)})
}
}
sort.Slice(out, func(i, j int) bool { return out[i].Stream < out[j].Stream })
return out
}
func keptHow(e seatdef.Message) string {
if e.KeptLastPerSubject() {
return "the last message of each subject, no age limit"
}
return "every message within its age and size"
}
// LastPerSubject says a hearing consumer starts from the last message of each subject, never from now.
func (k KeptEvent) LastPerSubject() bool { return k.Last }
// StreamConfig is the kept stream as it is created: sourced from the shared events stream by the event's subject, with
// its own limits as the event's keep says (design 47 G §396): its age (none for 0), its cap, and one message a subject
// when it keeps the last of each. A stream at its cap drops its oldest message (§331).
func (k KeptEvent) StreamConfig() jetstream.StreamConfig {
cfg := jetstream.StreamConfig{
Name: k.Stream,
Description: k.Why,
Storage: jetstream.FileStorage,
Replicas: 1,
Sources: []*jetstream.StreamSource{{Name: EventsStream, FilterSubject: k.Filter}},
MaxAge: time.Duration(k.MaxAgeDays) * 24 * time.Hour,
MaxBytes: int64(k.MaxMiB) << 20,
}
if k.Last {
cfg.MaxMsgsPerSubject = 1
}
return cfg
}
// KeptHeard is the part of a module's `consumes` (`<seat>.<event>`) that names a kept event, as the Heard it makes: the
// module's durable on the kept stream, filtered to the subject it consumes, from the last message of each subject for an
// event so kept. ok is false for a consume that names none.
func KeptHeard(kept []KeptEvent, consume string) (h Heard, ok bool) {
seat, rest, named := strings.Cut(consume, ".")
if !named {
return Heard{}, false
}
for _, k := range kept {
local := strings.TrimPrefix(k.Filter, seatdef.EventSubject(k.Seat, ""))
if k.Seat != seat || (rest != k.Event && rest != local) {
continue
}
return Heard{Stream: k.Stream, Emitter: k.Seat, Filters: []string{k.Filter}, LastPerSubject: k.LastPerSubject()}, true
}
return Heard{}, false
}
// SplitKept divides a module's consumes into the ones the shared events stream carries and the kept events' hearing, merged
// by stream, filters sorted without repeats.
func SplitKept(kept []KeptEvent, consumes []string) (rest []string, hears []Heard) {
byStream := map[string]*Heard{}
var order []string
for _, c := range consumes {
h, ok := KeptHeard(kept, c)
if !ok {
rest = append(rest, c)
continue
}
if byStream[h.Stream] == nil {
byStream[h.Stream] = &Heard{Stream: h.Stream, Emitter: h.Emitter, LastPerSubject: h.LastPerSubject}
order = append(order, h.Stream)
}
byStream[h.Stream].Filters = append(byStream[h.Stream].Filters, h.Filters...)
}
for _, s := range order {
h := byStream[s]
slices.Sort(h.Filters)
h.Filters = slices.Compact(h.Filters)
hears = append(hears, *h)
}
return rest, hears
}
// AskStreamsOf is the work-queue stream of each seat of the seat list that accepts work, as mesh-sdk's buscompose.SeatQueue
// derives it from the seat's accepted kinds' settings (design 47 H2 §777; H-A-362): `SEAT_<SEAT>` on
// `mesh.seat.<seat>.accept.>`, its max-age, max-msgs and duplicates window, so an ask is held until one holder takes it, and
// a JetStream publish of an ask is answered by the bus. Before G the controller made these; a seat whose queues came with G
// (mesh-builds, mesh-wants) had none, so every ask to it timed out unanswered (lead-47, 2026-11-15). A seat whose
// settings SeatQueue refuses is named in refused and gets no stream. Sorted by stream.
//
// workers are the one shared worker SeatQueue names for each such seat that is not pulled by kind (design 47 H2 H-A-468):
// the grants part creates it where the bus lacks it, as it creates the seat's stream, for its holders bind it and never
// make it; a seat pulled by kind keeps its per-queue workers (SeatTrafficOf). Sorted by stream.
func AskStreamsOf(defs map[string]seatdef.Definition) (out []jetstream.StreamConfig, workers []Consumer, refused []string) {
for _, name := range slices.Sorted(maps.Keys(defs)) {
d := defs[name]
if len(d.Accepts) == 0 || !safeSubject.MatchString(d.Seat) {
continue
}
st, w, ok, err := sdkbc.SeatQueue(d.Seat, d.Accepts)
if err != nil {
refused = append(refused, err.Error())
continue
}
if !ok {
continue
}
out = append(out, jetstream.StreamConfig{Name: st.Name, Description: st.Why, Subjects: st.Subjects,
Retention: jetstream.WorkQueuePolicy, Storage: jetstream.FileStorage, Replicas: 1,
MaxAge: time.Duration(st.MaxAge) * time.Second, MaxMsgs: int64(st.MaxMsgs),
Duplicates: time.Duration(st.DuplicatesSeconds) * time.Second})
if !d.PulledByKind {
// SeatQueue's worker as it returns it, every setting the two types share (its in-progress is the holder's beat,
// no setting of the bus's): the grants part makes no derivation of its own (H-A-468 second addendum).
workers = append(workers, Consumer{Name: w.Name, Stream: w.Stream, Filters: w.Filters, Queue: w.Queue, Push: w.Push,
TakesOverFrom: w.TakesOverFrom, DeliverSubject: w.DeliverSubject, AckWaitSeconds: w.AckWaitSeconds,
MaxDeliver: w.MaxDeliver, MaxAckPending: w.MaxAckPending, FromNow: w.FromNow, Why: w.Why, Resettable: w.Resettable})
}
}
slices.SortFunc(out, func(a, b jetstream.StreamConfig) int { return strings.Compare(a.Name, b.Name) })
slices.SortFunc(workers, func(a, b Consumer) int { return strings.Compare(a.Stream, b.Stream) })
return out, workers, refused
}