diff --git a/go/process/event_hub.go b/go/process/event_hub.go index 8027d757..d0eb3840 100644 --- a/go/process/event_hub.go +++ b/go/process/event_hub.go @@ -47,9 +47,16 @@ type EventHub struct { // eventsSubjects are EVENTS' own, read once; unheardNow the patterns not heard as of the last pass; allTold says // OnAllHeard was told for the present state. eventsSubjects []string - unheardNow []unheard + unheardNow []Unheard allTold bool - passMu sync.Mutex + // late are the patterns refused after Start (they stay unheard); lastTold the list the hooks were last told, and toldOnce says + // a pass told them (Start's, whatever the list). + late []Unheard + lastTold []Unheard + toldOnce bool + passed bool + unheardTo []func([]Unheard) + passMu sync.Mutex // unhandled are told each subject no handler took (OnUnhandled). unhandled []func(subject string) // overruns are told the handlings past event-overrun (OnOverrun). @@ -107,15 +114,24 @@ func (h *EventHub) handleByKey(pattern string, handle func(jetstream.Msg), keyOf return fmt.Errorf("an event handler needs a subject pattern and a handler") } h.mu.Lock() - defer h.mu.Unlock() - if h.started { - return fmt.Errorf("the events %q are registered after Start: register every handler, then Start once (design 47 H2b §836 3.)", pattern) - } for _, r := range h.routes { if subjectsOverlap(r.pattern, pattern) { + h.mu.Unlock() return fmt.Errorf("the events %q overlap %q, already handled: a message has one taker", pattern, r.pattern) } } + if h.started { + // An event no registered pattern covers (its first adopt) is not heard; one an earlier handler takes is. + err := fmt.Errorf("the events %q are registered after Start: register every handler, then Start once (design 47 H2b §836 3.)", pattern) + late := Unheard{pattern, "", "registered after Start"} + if !slices.Contains(h.late, late) { + h.late = append(h.late, late) + } + h.mu.Unlock() + h.announce() + return err + } + defer h.mu.Unlock() h.routes = append(h.routes, hubRoute{pattern, handle, keyOf}) return nil } @@ -158,6 +174,15 @@ func (h *EventHub) OnAllHeard(f func()) { h.mu.Unlock() } +// OnUnheard registers f, told the patterns the hub does not hear, each with its stream and why, whenever that list changes, Start +// included, and an empty list once every pattern is heard, as OnOverrun is told (design 47 H2b §836 4.): the process reader raises +// the finding `events-unheard` from it. A pattern refused after Start is in the list. Register before Start. +func (h *EventHub) OnUnheard(f func(unheard []Unheard)) { + h.mu.Lock() + h.unheardTo = append(h.unheardTo, f) + h.mu.Unlock() +} + // Patterns are the registered subject patterns, in order. func (h *EventHub) Patterns() []string { h.mu.RLock() @@ -275,8 +300,9 @@ func (b natsHubBus) reconnected(f func()) { }) } -// unheard is a registered pattern the hub does not hear: the stream that should carry it ("" when none does) and why. -type unheard struct{ pattern, stream, why string } +// Unheard is a registered pattern the hub does not hear: the stream that should carry it ("" when none does) and why (design 47 +// H2b §836 4.): "no stream carries it", "not composed", "refused: " or "registered after Start". +type Unheard struct{ Pattern, Stream, Why string } func (h *EventHub) start(ctx context.Context, bus hubBus, cred Account) error { h.mu.Lock() @@ -370,7 +396,7 @@ func (h *EventHub) pass(ctx context.Context, bus hubBus, cred Account) { h.bindOne(ctx, bus, cred, stream) } } - var not []unheard + var not []Unheard h.mu.RLock() for i, p := range patterns { if len(carriers[i]) == 0 { @@ -378,22 +404,23 @@ func (h *EventHub) pass(ctx context.Context, bus hubBus, cred Account) { if nerr != nil { why = fmt.Sprintf("the streams are not listed (%v)", nerr) } - not = append(not, unheard{p, "", why}) + not = append(not, Unheard{p, "", why}) } for _, stream := range carriers[i] { if _, bound := h.heard[stream]; !bound { - not = append(not, unheard{p, stream, h.why[stream]}) + not = append(not, Unheard{p, stream, h.why[stream]}) } } } h.mu.RUnlock() h.mu.Lock() - h.unheardNow = not + h.unheardNow, h.passed = not, true all := len(not) == 0 tell := all && !h.allTold h.allTold = all told := h.allHeard h.mu.Unlock() + h.announce() if tell { for _, f := range told { f() @@ -401,6 +428,36 @@ func (h *EventHub) pass(ctx context.Context, bus hubBus, cred Account) { } } +// announce says the patterns not heard (the journal, one line each) and tells each OnUnheard hook, when the list differs from the one +// told last or none was told yet (Start's pass tells even an empty list: a restarted program clears what an earlier process raised). +// Nothing is told before a pass has made the list. +func (h *EventHub) announce() { + h.mu.Lock() + if !h.passed { + h.mu.Unlock() + return + } + list := append(slices.Clone(h.unheardNow), h.late...) + if h.toldOnce && slices.Equal(list, h.lastTold) { + h.mu.Unlock() + return + } + h.toldOnce, h.lastTold = true, list + hooks := slices.Clone(h.unheardTo) + h.mu.Unlock() + for _, u := range list { + switch { + case u.Stream == "": + h.logf("events: the pattern %s is not heard: %s", u.Pattern, u.Why) + default: + h.logf("events: the pattern %s is not heard on %s: %s", u.Pattern, u.Stream, u.Why) + } + } + for _, f := range hooks { + f(slices.Clone(list)) + } +} + // eventsTake says whether EVENTS carries pattern: some subject of its own overlaps it (all of them, when they are not read). func (h *EventHub) eventsTake(subjects []string, pattern string) bool { if subjects == nil { @@ -491,6 +548,7 @@ func (h *EventHub) Stop() { h.stopWake = nil } h.started, h.allTold, h.unheardNow = false, false, nil + h.passed, h.toldOnce, h.lastTold, h.late = false, false, nil, nil for _, f := range h.feeds { f.end() } diff --git a/go/process/event_hub_test.go b/go/process/event_hub_test.go index d6a3cc20..bc47bd11 100644 --- a/go/process/event_hub_test.go +++ b/go/process/event_hub_test.go @@ -484,8 +484,11 @@ func (b *fakeHubBus) reconnected(f func()) { b.reconn = f } func subTheHubStartBindsEveryPatternAndSaysWhenAllAreHeard(t *testing.T) { kept := "SEAT_node-engine_running" cred := Account{Node: "ace", Module: "teller"} + var unheardTold [][]Unheard // what OnUnheard was told, in order (1984/AC-836e) newHub := func(bus *fakeHubBus) (h *EventHub, allHeard *int, created *[]time.Time) { h, allHeard, created = NewEventHub(t.Logf), new(int), new([]time.Time) + unheardTold = nil + h.OnUnheard(func(list []Unheard) { unheardTold = append(unheardTold, list) }) h.OnAllHeard(func() { *allHeard++ }) h.OnBound(func(at time.Time) { *created = append(*created, at) }) if err := h.Handle("mesh.seat.node-engine.event.running.>", func(jetstream.Msg) {}); err != nil { @@ -517,6 +520,9 @@ func subTheHubStartBindsEveryPatternAndSaysWhenAllAreHeard(t *testing.T) { } bus.onWake() bus.reconn() + if len(unheardTold) != 2 || len(unheardTold[0]) != 0 || len(unheardTold[1]) != 1 { + t.Errorf("a start that binds everything tells an empty list once, a later wake and reconnect nothing, and the refused late pattern: %v", unheardTold) + } if *allHeard != 1 || len(bus.binds) != 2 { t.Errorf("a bound stream was told or bound again: %d, %v", *allHeard, bus.binds) } @@ -526,20 +532,32 @@ func subTheHubStartBindsEveryPatternAndSaysWhenAllAreHeard(t *testing.T) { h, allHeard, _ := newHub(bus) h.start(context.Background(), bus, cred) defer h.Stop() - if *allHeard != 0 || len(h.unheardNow) != 1 || h.unheardNow[0].stream != kept || !strings.Contains(h.unheardNow[0].why, "not composed") { + if *allHeard != 0 || len(h.unheardNow) != 1 || h.unheardNow[0].Stream != kept || !strings.Contains(h.unheardNow[0].Why, "not composed") { t.Fatalf("all heard %d, unheard %+v", *allHeard, h.unheardNow) } bus.composed[kept] = errors.New("the bus refuses") bus.onWake() - if *allHeard != 0 || !strings.Contains(h.unheardNow[0].why, "refused") { + if *allHeard != 0 || !strings.Contains(h.unheardNow[0].Why, "refused") { t.Fatalf("a refused bind: all heard %d, unheard %+v", *allHeard, h.unheardNow) } + bus.onWake() // the same reason again: nothing new to tell bus.composed[kept] = nil bus.reconn() bus.onWake() if *allHeard != 1 || len(h.unheardNow) != 0 || len(bus.binds) != 2 { t.Errorf("all heard %d, unheard %+v, binds %v", *allHeard, h.unheardNow, bus.binds) } + if len(unheardTold) != 3 || len(unheardTold[2]) != 0 || len(unheardTold[0]) != 1 || len(unheardTold[1]) != 1 || + unheardTold[0][0] == unheardTold[1][0] { + t.Errorf("OnUnheard was told %v: the list at Start, its change, then empty once heard", unheardTold) + } + // 1984/AC-836s: a pattern registered after Start is refused and is among those not heard + if err := h.Handle("mesh.mod.p.event.late", func(jetstream.Msg) {}); err == nil { + t.Error("a handler after Start was accepted") + } + if n := len(unheardTold); n != 4 || len(unheardTold[3]) != 1 || unheardTold[3][0] != (Unheard{"mesh.mod.p.event.late", "", "registered after Start"}) { + t.Errorf("OnUnheard was told %v", unheardTold) + } }) t.Run("no stream carries a pattern", func(t *testing.T) { bus := base(map[string]error{EventsStream: nil, kept: nil}) @@ -547,7 +565,7 @@ func subTheHubStartBindsEveryPatternAndSaysWhenAllAreHeard(t *testing.T) { h, allHeard, _ := newHub(bus) h.start(context.Background(), bus, cred) defer h.Stop() - if *allHeard != 0 || len(h.unheardNow) != 1 || h.unheardNow[0].pattern != "mesh.mod.p.event.x" || h.unheardNow[0].why != "no stream carries it" { + if *allHeard != 0 || len(h.unheardNow) != 1 || h.unheardNow[0].Pattern != "mesh.mod.p.event.x" || h.unheardNow[0].Why != "no stream carries it" { t.Errorf("all heard %d, unheard %+v", *allHeard, h.unheardNow) } }) diff --git a/go/views/findings.go b/go/views/findings.go index 5ca4aa38..d1e64887 100644 --- a/go/views/findings.go +++ b/go/views/findings.go @@ -30,6 +30,9 @@ const ( // EventOverrunKind is a process's finding of the handlings of events that run past the setting event-overrun, one per module, // its summary naming each (design 47 H2 H-A-683 (3), H-A-697). EventOverrunKind = "event-overrun" + // EventsUnheardKind is a process's finding of the patterns its event hub does not hear, one per module on its node, its summary + // naming each with its stream and why (design 47 H2b §836 4.). + EventsUnheardKind = "events-unheard" ) // viewFinding is what a views part says is wrong with a state it owns or follows, in plain fields: kind and id name it, summary @@ -153,3 +156,24 @@ func overrunFinding(module string, past []process.Handling, at time.Time) viewFi summary := fmt.Sprintf("%s has %d event handlings past their bound: %s: the events behind them wait", module, len(past), strings.Join(named, "; ")) return viewFinding{kind: EventOverrunKind, id: module, summary: summary, headline: fmt.Sprintf("%s handles an event very slowly", module), at: at.UTC()} } + +// unheardFinding is the module's one finding of the patterns its hub does not hear (design 47 H2b §836 4.): its id `.`, +// its summary each pattern with its stream and why, at most five and then how many more, and the way through: the node's service +// manager restarts the module's unit. +func unheardFinding(module, node string, list []process.Unheard, at time.Time) viewFinding { + var named []string + for i, u := range list { + if i == overrunMost { + named = append(named, fmt.Sprintf("and %d more", len(list)-overrunMost)) + break + } + if u.Stream == "" { + named = append(named, fmt.Sprintf("%s (%s)", u.Pattern, u.Why)) + continue + } + named = append(named, fmt.Sprintf("%s on %s (%s)", u.Pattern, u.Stream, u.Why)) + } + summary := fmt.Sprintf("%s on %s does not hear %d event patterns: %s: 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", + module, node, len(list), strings.Join(named, "; ")) + return viewFinding{kind: EventsUnheardKind, id: module + "." + node, summary: summary, headline: fmt.Sprintf("%s does not hear all its events", module), at: at.UTC()} +} diff --git a/go/views/process_reader.go b/go/views/process_reader.go index 41319f30..b523b465 100644 --- a/go/views/process_reader.go +++ b/go/views/process_reader.go @@ -456,6 +456,28 @@ func (r *ProcessReader) overrun(past []process.Handling) { } } +// unheardPatterns is the event hub's word of the patterns it does not hear: the module's finding `events-unheard`, raised naming +// each, cleared on an empty list (design 47 H2b §836 4.). With no findings writer the hub's journal line stands alone (H-A-840). +func (r *ProcessReader) unheardPatterns(list []process.Unheard) { + if r.Findings == nil { + return + } + w := r.Findings() + if w == nil { + return + } + module, node := w.ModuleNode() + if len(list) == 0 { + if err := (viewFinding{kind: EventsUnheardKind, id: module + "." + node}).drop(w); err != nil { + r.logf("views: the finding %s of %s is not cleared: %v", EventsUnheardKind, r.Module, err) + } + return + } + if err := unheardFinding(module, node, list, r.now()).put(w); err != nil { + r.logf("views: the finding %s of %s is not written: %v", EventsUnheardKind, r.Module, err) + } +} + // clearUnhandled clears the module's findings `event-unhandled` and `event-overrun` at the process's start: a new build may // handle the event (H-A-169), and no handling of the new process runs yet (H-A-697 (1)). func (r *ProcessReader) clearUnhandled() { @@ -932,6 +954,7 @@ func (r *ProcessReader) Start(ctx context.Context, js jetstream.JetStream, cred if wire { hub.OnUnhandled(r.unhandled) hub.OnOverrun(r.overrun) + hub.OnUnheard(r.unheardPatterns) } } r.mu.Lock() diff --git a/go/views/process_reader_integration_test.go b/go/views/process_reader_integration_test.go index f368af0e..80816d82 100644 --- a/go/views/process_reader_integration_test.go +++ b/go/views/process_reader_integration_test.go @@ -311,8 +311,8 @@ func subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable(t *testing.T) { default: t.Fatal("the unhandled event after the second start wrote no finding") } - // A hub started before the bus holds its durable says so, and binds at a later start: a module is not lost for the - // order in which the bus makes its consumer. + // A hub started before the bus holds its durable says so, and binds when the grants part says it composed the module's + // durables: a module is not lost for the order in which the bus makes its consumer (design 47 H2b §836 2.). late := process.Account{Node: "n1", Module: "late"} lateHub := process.NewEventHub(t.Logf) heard := make(chan string, 1) @@ -320,15 +320,22 @@ func subTheViewsReaderAndTwoHandlersShareTheModulesOneDurable(t *testing.T) { t.Fatal(err) } defer lateHub.Stop() - if err := lateHub.Start(ctx, js, late); err == nil { - t.Fatal("a hub with no durable on the bus started") + allHeard := make(chan struct{}, 1) + lateHub.OnAllHeard(func() { allHeard <- struct{}{} }) + if err := lateHub.Start(ctx, js, late); err != nil { + t.Fatalf("a hub with no durable on the bus failed its start: %v", err) } if _, err := js.CreateOrUpdateConsumer(ctx, process.EventsStream, jetstream.ConsumerConfig{Durable: late.EventConsumerName(), AckPolicy: jetstream.AckExplicitPolicy, FilterSubjects: []string{"mesh.mod.l.event.>"}}); err != nil { t.Fatal(err) } - if err := lateHub.Start(ctx, js, late); err != nil { - t.Fatalf("the hub did not bind at its second start: %v", err) + if err := nc.Publish("mesh.seat.mesh-grants.event.durables-composed.n1.late", []byte("{}")); err != nil { + t.Fatal(err) + } + select { + case <-allHeard: + case <-time.After(10 * time.Second): + t.Fatal("the grants part said the durables are composed and the hub did not bind") } if _, err := js.Publish(ctx, "mesh.mod.l.event.x", []byte("{}")); err != nil { t.Fatal(err) @@ -399,6 +406,10 @@ func subAHandlingPastEventOverrunRaisesItsFindingAndTheNextStartClearsIt(t *test for range 2 { // the start clears event-unhandled and event-overrun <-found.cleared } + // design 47 H2b §836 4.: the hub's Start binds every pattern and tells an empty list, so events-unheard an earlier process raised is cleared + if got := <-found.cleared; got != "events-unheard reader=2en1" { + t.Fatalf("the hub's start cleared %s", got) + } if _, err := js.Publish(ctx, "mesh.mod.p.event.slow", []byte("{}")); err != nil { t.Fatal(err) } diff --git a/go/views/views_test.go b/go/views/views_test.go index 3f63e1ee..dc5747ff 100644 --- a/go/views/views_test.go +++ b/go/views/views_test.go @@ -1584,4 +1584,23 @@ func TestTheOverrunFindingNamesTheHandlingsOldestFirst(t *testing.T) { if eight := overrunFinding("builder", past(8), at); !strings.Contains(eight.summary, "and 3 more") || strings.Contains(eight.summary, "e5 ") { t.Errorf("eight handlings: %s", eight.summary) } + // Design 47 H2b §836 4. (1984/AC-836e, AC-836r): the finding events-unheard, id `.`, names each pattern with its stream and + // why, is cleared by an empty list, and a reader with no findings writer says nothing (the hub's journal line stands alone). + list := []process.Unheard{{Pattern: "mesh.seat.s.event.e.>", Stream: "SEAT_s_e", Why: "not composed"}, {Pattern: "mesh.mod.p.event.x", Why: "no stream carries it"}} + if f := unheardFinding("builder", "n1", list, at); f.kind != EventsUnheardKind || f.id != "builder.n1" || + !strings.Contains(f.summary, "mesh.seat.s.event.e.> on SEAT_s_e (not composed)") || !strings.Contains(f.summary, "mesh.mod.p.event.x (no stream carries it)") { + t.Errorf("the finding: %+v", f) + } + fm := &findingsMap{} + pr := &ProcessReader{Module: "m", Findings: func() WireWriter { return fm }, Now: func() time.Time { return at }} + pr.unheardPatterns(list) + if _, ok := fm.m["events-unheard/m.n1"]; !ok || len(fm.m) != 1 { + t.Errorf("the finding was not raised: %v", fm.m) + } + pr.unheardPatterns(nil) + if len(fm.m) != 0 || fm.deletes != 1 { + t.Errorf("an empty list did not clear it: %v", fm.m) + } + quiet := &ProcessReader{Module: "m", Now: func() time.Time { return at }} + quiet.unheardPatterns(list) // no findings writer: nothing written, nothing panics } diff --git a/go/views/writer.go b/go/views/writer.go index bd942359..348b1bb4 100644 --- a/go/views/writer.go +++ b/go/views/writer.go @@ -74,7 +74,7 @@ func requireFields[T any](what string, fields ...string) error { } // ViewKinds are the finding kinds a views reader writes through the part's writer (design 47 G §639). -var ViewKinds = []string{ViewWaitsKind, UnansweredKind, UnsaidKind, TooLargeKind, EntryNotFoundKind, EventUnhandledKind, EventOverrunKind} +var ViewKinds = []string{ViewWaitsKind, UnansweredKind, UnsaidKind, TooLargeKind, EntryNotFoundKind, EventUnhandledKind, EventOverrunKind, EventsUnheardKind} var ( sessionForm = regexp.MustCompile(`^[a-z0-9][a-z0-9._-]*@[a-z0-9][a-z0-9-]*$`) diff --git a/go/views/writer_test.go b/go/views/writer_test.go index 24b944da..016c02e0 100644 --- a/go/views/writer_test.go +++ b/go/views/writer_test.go @@ -452,7 +452,7 @@ func subWriterKindsAndReaderFindings(t *testing.T) { t.Fatal(err) } // H-A-697 (2): a writer given its own kinds writes event-unhandled and event-overrun too, and states them. - for _, kind := range []string{views.EventUnhandledKind, views.EventOverrunKind} { + for _, kind := range []string{views.EventUnhandledKind, views.EventOverrunKind, views.EventsUnheardKind} { if err := views.PutViewFinding(r.w, kind, "m", "sum", "head"); err != nil { t.Errorf("a writer given its own kinds refuses %s: %v", kind, err) } @@ -479,7 +479,7 @@ func subWriterKindsAndReaderFindings(t *testing.T) { } } if !slices.Contains(stated.Kinds, "k") || !slices.Contains(stated.Kinds, views.ViewWaitsKind) || - !slices.Contains(stated.Kinds, views.EventUnhandledKind) || !slices.Contains(stated.Kinds, views.EventOverrunKind) || len(stated.Findings) != 4 { + !slices.Contains(stated.Kinds, views.EventUnhandledKind) || !slices.Contains(stated.Kinds, views.EventOverrunKind) || !slices.Contains(stated.Kinds, views.EventsUnheardKind) || len(stated.Findings) != 5 { t.Errorf("the statement: kinds %v, %d findings", stated.Kinds, len(stated.Findings)) } if err := views.DropViewFinding(r.w, views.ViewWaitsKind, "m.o.s"); err != nil {