N1 fix r2: one pattern per consume, the keeper's views reader raises events-unheard from the hub and kept.go goes, no backoff for a refusal
Review r2 part 4 items 1 to 3 (H-A-876, H-A-877). Builds: design 47 H2b §836 5.
This commit is contained in:
@@ -183,7 +183,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
|
||||
seats.Follow(seatlist.ProcessBus(conn.Call, hub), nil, logger.Printf)
|
||||
hub.OnAllHeard(seats.EventsLive)
|
||||
if err := hub.Start(ctx, js, account); err != nil {
|
||||
logger.Printf("the consumer %s is not bound yet (%v): bound once the bus makes it, the seat list read then", account.EventConsumerName(), err)
|
||||
logger.Printf("the event hub is not started (%v): a stream it cannot bind is said by events-unheard and bound when its durable %s is composed, the seat list read then", err, account.EventConsumerName())
|
||||
}
|
||||
defer hub.Stop()
|
||||
stopAnswers, err := conn.AnswerSeat(recovery.Seat, seatlist.Definition(seats.Reader, recovery.Seat), e.Handlers())
|
||||
|
||||
@@ -239,7 +239,11 @@ func runKeeper(ctx context.Context, getenv func(string) string, logOut io.Writer
|
||||
default:
|
||||
}
|
||||
}
|
||||
follow, err := followCatalog(ctx, conn, writer, wakeMoved, logger.Printf)
|
||||
hub, err := newHub(account, logger.Printf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
follow, err := followCatalog(ctx, js, account, hub, conn, writer, wakeMoved, logger.Printf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -251,7 +255,7 @@ func runKeeper(ctx context.Context, getenv func(string) string, logOut io.Writer
|
||||
}
|
||||
defer stopAnswers()
|
||||
logger.Printf("started on the account %s: following the catalog half of every watched branch through the seat %s", account.User, catalog.Seat)
|
||||
return Keep(ctx, k, hearer(js, account, follow.take, follow.bound, unheardFinding(writer, account, logger.Printf), logger.Printf), requestsOf(conn), moved, logger.Printf)
|
||||
return Keep(ctx, k, hearer(js, account, hub, follow.take, logger.Printf), requestsOf(conn), moved, logger.Printf)
|
||||
}
|
||||
|
||||
// catalogFollow is the keeper's reading of the catalog half and the seat list (design 47 G §532, §614): the reader, the
|
||||
@@ -265,9 +269,9 @@ type catalogFollow struct {
|
||||
}
|
||||
|
||||
// followCatalog wires the branch follower and the seat list, and asks their first views; moved is told of each change.
|
||||
func followCatalog(ctx context.Context, conn *process.Conn, writer views.WireWriter, moved func(), logf func(string, ...any)) (*catalogFollow, error) {
|
||||
func followCatalog(ctx context.Context, js jetstream.JetStream, account process.Account, hub *process.EventHub, conn *process.Conn, writer views.WireWriter, moved func(), logf func(string, ...any)) (*catalogFollow, error) {
|
||||
f := &catalogFollow{ctx: ctx, routes: &eventRoutes{by: map[string]func(jetstream.Msg){}}}
|
||||
f.reader = &views.ProcessReader{Module: keeper.Module, Call: conn.Call, Logf: logf, Findings: func() views.WireWriter { return writer }}
|
||||
f.reader = &views.ProcessReader{Module: keeper.Module, Call: conn.Call, Logf: logf, Hub: hub, Findings: func() views.WireWriter { return writer }}
|
||||
branches, err := meshcatalogclient.FollowBranchesProcess(f.reader, conn.Call)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("the catalog half cannot be followed: %w", err)
|
||||
@@ -286,7 +290,9 @@ func followCatalog(ctx context.Context, conn *process.Conn, writer views.WireWri
|
||||
if err != nil {
|
||||
logf("the seat list cannot be followed (%v): a module's sent store waits for it", err)
|
||||
}
|
||||
f.reader.Ask(ctx, nil)
|
||||
// The reader registers its views events and its findings on the hub, raises `events-unheard` from it, and tells each view the
|
||||
// bus made the keeper's consumer, so a first view taken before is asked again; the hub is started by the hearer.
|
||||
f.reader.Start(ctx, js, account, nil)
|
||||
return f, nil
|
||||
}
|
||||
|
||||
@@ -346,13 +352,6 @@ func (f *catalogFollow) take(m jetstream.Msg) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// bound tells each view the bus made the keeper's consumer then, so a first view taken before is asked again.
|
||||
func (f *catalogFollow) bound(created time.Time) {
|
||||
for _, fl := range f.reader.Followers() {
|
||||
fl.ConsumerMade(f.ctx, created)
|
||||
}
|
||||
}
|
||||
|
||||
// eventRoutes are the seat list's handlers of its events by subject, on the keeper's one durable consumer.
|
||||
type eventRoutes struct {
|
||||
mu sync.Mutex
|
||||
@@ -375,12 +374,19 @@ func (r *eventRoutes) get(subject string) func(jetstream.Msg) {
|
||||
return r.by[subject]
|
||||
}
|
||||
|
||||
// hearer hears the events the keeper consumes through mesh-sdk's event hub, which binds the process's own durable
|
||||
// `<node>_nats_keeper` on EVENTS and on every kept seat event's stream that carries one of its patterns, binds the ones it
|
||||
// could not when the grants part says it composed the module's durables and at a reconnect, and says each it cannot (design 47
|
||||
// H2b §836 5., H-A-876): a message of the catalog half or the seat list goes to take, which acknowledges it; every other to
|
||||
// handle, acknowledged once applied. bound is told when the bus made a durable; unheard, the patterns not heard.
|
||||
func hearer(js jetstream.JetStream, a process.Account, take func(jetstream.Msg) bool, bound func(time.Time), unheard func([]process.Unheard), logf func(string, ...any)) func(context.Context, func(subject string, body []byte) error) error {
|
||||
// newHub is the keeper's event hub: mesh-sdk's, binding the process's own durable `<node>_nats_keeper` on EVENTS and on every
|
||||
// kept seat event's stream that carries one of its patterns (design 47 H2b §836 5., H-A-876). Its views reader is given it
|
||||
// (ProcessReader.Hub), which raises `events-unheard` from it.
|
||||
func newHub(a process.Account, logf func(string, ...any)) (*process.EventHub, error) {
|
||||
hub := process.NewEventHub(logf)
|
||||
return hub, hub.ForProcess(a.ProcessID())
|
||||
}
|
||||
|
||||
// hearer registers the keeper's one handler on each event its module consumes and starts the hub, which binds each durable it can,
|
||||
// says each it cannot, and binds it when the grants part says it composed the module's durables and at a reconnect: a message of
|
||||
// the catalog half or the seat list goes to take, which acknowledges it; every other to handle, acknowledged once applied. The
|
||||
// error is the hub's refusal to start, never a stream not heard (that is `events-unheard`).
|
||||
func hearer(js jetstream.JetStream, a process.Account, hub *process.EventHub, take func(jetstream.Msg) bool, logf func(string, ...any)) func(context.Context, func(subject string, body []byte) error) error {
|
||||
return func(ctx context.Context, handle func(string, []byte) error) error {
|
||||
deliver := func(m jetstream.Msg) {
|
||||
if take != nil && take(m) {
|
||||
@@ -393,16 +399,6 @@ func hearer(js jetstream.JetStream, a process.Account, take func(jetstream.Msg)
|
||||
}
|
||||
_ = m.Ack()
|
||||
}
|
||||
hub := process.NewEventHub(logf)
|
||||
if err := hub.ForProcess(a.ProcessID()); err != nil {
|
||||
return err
|
||||
}
|
||||
if bound != nil {
|
||||
hub.OnBound(bound)
|
||||
}
|
||||
if unheard != nil {
|
||||
hub.OnUnheard(unheard)
|
||||
}
|
||||
for _, pattern := range consumedPatterns {
|
||||
if err := hub.Handle(pattern, deliver); err != nil {
|
||||
return err
|
||||
@@ -416,12 +412,16 @@ func hearer(js jetstream.JetStream, a process.Account, take func(jetstream.Msg)
|
||||
}
|
||||
}
|
||||
|
||||
// consumedPatterns are the subjects of the events the keeper consumes (its module's `consumes` and the operator channel's decided
|
||||
// for its own asks); which of them a kept stream carries is the hub's to find, never listed here.
|
||||
// consumedPatterns are the subjects of the events the keeper's module consumes (its manifest's `consumes`, one pattern each, from
|
||||
// the contracts' subjects) and the operator channel's decided for its own asks; which of them a kept stream carries is the hub's
|
||||
// to find, never listed here.
|
||||
var consumedPatterns = []string{
|
||||
"mesh.seat." + catalog.Seat + ".event.>",
|
||||
catalog.SubjectCatalogRevisionAll[:len(catalog.SubjectCatalogRevisionAll)-1] + ">",
|
||||
catalog.SubjectRegistrationChanged,
|
||||
catalog.SubjectSeatDefinitionChanged,
|
||||
catalog.SubjectSeatDefinitionRemoved,
|
||||
seatdef.EventSubject(sourcebranches.Seat, sourcebranches.EventSourceMoved) + ".>",
|
||||
seatdef.EventSubject(meshconditions.Seat, meshconditions.EventFindingsAsked) + ".>",
|
||||
meshconditions.SubjectFindingsAsked,
|
||||
DecidedSubject,
|
||||
}
|
||||
|
||||
@@ -461,33 +461,16 @@ func Keep(ctx context.Context, k *keeper.Keeper, hear func(context.Context, func
|
||||
}
|
||||
}
|
||||
asked := make(chan struct{}, 1)
|
||||
for backoff := 10 * time.Second; ; backoff = min(backoff*2, 5*time.Minute) {
|
||||
err := hear(ctx, func(subject string, body []byte) error {
|
||||
if subject == DecidedSubject {
|
||||
// The operator answered one of the bus module's asks: the waiting requests are read again now.
|
||||
wake(asked)()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
logf("the keeper's consumer cannot be heard yet (%v): asked again in %s", err, backoff)
|
||||
// The fill pass and the plans run on while the consumer is bound again (review round 2, running F1).
|
||||
bound := time.After(backoff)
|
||||
binding:
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-tick.C:
|
||||
k.FillPass()
|
||||
case <-moved:
|
||||
plan()
|
||||
case <-bound:
|
||||
break binding
|
||||
}
|
||||
// The hub's refusal to start (a pattern refused, a start twice) is the program's fault and the same at every try, so it ends the
|
||||
// keeper; a stream not heard is no error here: the hub says it (`events-unheard`) and binds it when its durable is composed.
|
||||
if err := hear(ctx, func(subject string, body []byte) error {
|
||||
if subject == DecidedSubject {
|
||||
// The operator answered one of the bus module's asks: the waiting requests are read again now.
|
||||
wake(asked)()
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return fmt.Errorf("the event hub is not started: %w", err)
|
||||
}
|
||||
if requests != nil {
|
||||
if err := requests(ctx, wake(asked)); err != nil {
|
||||
|
||||
@@ -233,7 +233,7 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
|
||||
return nil
|
||||
}
|
||||
go func() {
|
||||
_ = Keep(kctx, k, hearer(js, account, nil, nil, nil, t.Logf), watch, moved, t.Logf)
|
||||
_ = Keep(kctx, k, hearer(js, account, testHub(t, account), nil, t.Logf), watch, moved, t.Logf)
|
||||
close(done)
|
||||
}()
|
||||
return func() { stop(); <-done }
|
||||
@@ -310,24 +310,12 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
|
||||
stopDown := run(down)
|
||||
eventually(t, "a fill pass with no first view", func() bool { return !down.Status().Pass.IsZero() && down.Status().FirstView.Answered.IsZero() })
|
||||
stopDown()
|
||||
// And while its consumer is bound again on its backoff (review round 2, running F1).
|
||||
was := FillEvery
|
||||
FillEvery = 20 * time.Millisecond
|
||||
defer func() { FillEvery = was }()
|
||||
// A hub that refuses to start ends the keeper with that error, after its first fill pass; no backoff retries a refusal.
|
||||
deaf := newKeeper()
|
||||
kctx, stopDeaf := context.WithCancel(ctx)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
_ = Keep(kctx, deaf, func(context.Context, func(string, []byte) error) error { return errors.New("no consumer yet") }, nil, nil, t.Logf)
|
||||
close(done)
|
||||
}()
|
||||
// A first view listing no module is none yet and never marked answered (A-367): the first fill pass is waited
|
||||
// for instead, then taken as the first (design 47 H, H-A-268).
|
||||
eventually(t, "the first fill pass", func() bool { return !deaf.Status().Pass.IsZero() })
|
||||
first := deaf.Status().Pass
|
||||
eventually(t, "a fill pass while the consumer is bound again", func() bool { return deaf.Status().Pass.After(first) })
|
||||
stopDeaf()
|
||||
<-done
|
||||
err := Keep(ctx, deaf, func(context.Context, func(string, []byte) error) error { return errors.New("no consumer yet") }, nil, nil, t.Logf)
|
||||
if err == nil || !strings.Contains(err.Error(), "the event hub is not started") || deaf.Status().Pass.IsZero() {
|
||||
t.Fatalf("the keeper ended with %v, last fill pass %v", err, deaf.Status().Pass)
|
||||
}
|
||||
})
|
||||
k := newKeeper()
|
||||
stop := run(k)
|
||||
@@ -852,14 +840,23 @@ func TestTheBusModuleRunsWhole(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const findingKey = "module.nats.anchor.events-unheard"
|
||||
const findingKey = "module.nats=2eanchor.events-unheard" // the id `nats.anchor`, escaped as the views reader writes it
|
||||
var cmu sync.Mutex
|
||||
var told [][]process.Unheard
|
||||
var got []string
|
||||
kctx, kstop := context.WithCancel(ctx)
|
||||
defer kstop()
|
||||
raise := unheardFinding(writer, account, t.Logf)
|
||||
hear := hearer(js, account, nil, nil, func(l []process.Unheard) { cmu.Lock(); told = append(told, l); cmu.Unlock(); raise(l) }, t.Logf)
|
||||
// A kept stream the keeper consumes none of (a version-released) gets it no durable and is no finding.
|
||||
if _, err := js.CreateStream(ctx, jetstream.StreamConfig{Name: "SEAT_mesh-catalog_version-released",
|
||||
Sources: []*jetstream.StreamSource{{Name: "EVENTS", FilterSubject: "mesh.seat.mesh-catalog.event.version-released.>"}}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The keeper's views reader is given the hub and raises `events-unheard` from it, through the writer.
|
||||
hub := testHub(t, account)
|
||||
hub.OnUnheard(func(l []process.Unheard) { cmu.Lock(); told = append(told, l); cmu.Unlock() })
|
||||
reader := &views.ProcessReader{Module: keeper.Module, Logf: t.Logf, Hub: hub, Findings: func() views.WireWriter { return writer }}
|
||||
reader.Start(kctx, js, account, nil)
|
||||
hear := hearer(js, account, hub, nil, t.Logf)
|
||||
if err := hear(kctx, func(subject string, _ []byte) error { cmu.Lock(); got = append(got, subject); cmu.Unlock(); return nil }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -898,3 +895,13 @@ func entry(name, storage, template string, options map[string]string) catalogue.
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
// testHub is the keeper's hub as the process makes it.
|
||||
func testHub(t *testing.T, account process.Account) *process.EventHub {
|
||||
t.Helper()
|
||||
hub, err := newHub(account, t.Logf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return hub
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
||||
"git.novox.be/novox/mesh-sdk/go/process"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
"nats-tools/internal/keeper"
|
||||
)
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user