views: count the events no handler takes as the module's finding
An event a module consumes that no handler takes was said in the journal alone, so a part that never acted on it (the runner's placing at 17:34) was seen only by reading the log. The event hub now tells a hook of each such subject; the process reader, where it holds a findings writer, counts them in one finding event-unhandled per module (the count since the start and the last subject) and clears it at its start, since a new build may handle the event. Without a findings writer the journal line stands alone. Builds: design 47 H H-A-166, H-A-169
This commit is contained in:
@@ -26,6 +26,8 @@ type EventHub struct {
|
|||||||
routes []hubRoute
|
routes []hubRoute
|
||||||
cons jetstream.ConsumeContext
|
cons jetstream.ConsumeContext
|
||||||
bound []func(created time.Time)
|
bound []func(created time.Time)
|
||||||
|
// unhandled are told each subject no handler took (OnUnhandled).
|
||||||
|
unhandled []func(subject string)
|
||||||
// kept are the module's durables on the kept seat events' streams it handles (StartKept).
|
// kept are the module's durables on the kept seat events' streams it handles (StartKept).
|
||||||
kept []jetstream.ConsumeContext
|
kept []jetstream.ConsumeContext
|
||||||
}
|
}
|
||||||
@@ -72,6 +74,15 @@ func (h *EventHub) OnBound(f func(created time.Time)) {
|
|||||||
h.mu.Unlock()
|
h.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnUnhandled registers f, told the subject of each message no handler takes, after the hub said it in the log and
|
||||||
|
// acknowledged it: the module's process reader counts them in its finding `event-unhandled` (design 47 H, H-A-166,
|
||||||
|
// H-A-169).
|
||||||
|
func (h *EventHub) OnUnhandled(f func(subject string)) {
|
||||||
|
h.mu.Lock()
|
||||||
|
h.unhandled = append(h.unhandled, f)
|
||||||
|
h.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
// Patterns are the registered subject patterns, in order.
|
// Patterns are the registered subject patterns, in order.
|
||||||
func (h *EventHub) Patterns() []string {
|
func (h *EventHub) Patterns() []string {
|
||||||
h.mu.RLock()
|
h.mu.RLock()
|
||||||
@@ -235,10 +246,14 @@ func (h *EventHub) hear(m jetstream.Msg) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
told := h.unhandled
|
||||||
h.mu.RUnlock()
|
h.mu.RUnlock()
|
||||||
if take == nil {
|
if take == nil {
|
||||||
h.logf("events: %s is taken by no handler of this module, acknowledged", m.Subject())
|
h.logf("events: %s is taken by no handler of this module, acknowledged", m.Subject())
|
||||||
_ = m.Ack()
|
_ = m.Ack()
|
||||||
|
for _, f := range told {
|
||||||
|
f(m.Subject())
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
take(m)
|
take(m)
|
||||||
|
|||||||
@@ -37,6 +37,15 @@ func TestTheHubMatchesSubjectsAndRefusesOverlappingPatterns(t *testing.T) {
|
|||||||
if err := h.Handle("mesh.mod.q.event.>", nop); err != nil {
|
if err := h.Handle("mesh.mod.q.event.>", nop); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
// H-A-169: a message no handler takes is acknowledged and told to each OnUnhandled hook, with its subject; one taken is not.
|
||||||
|
var told []string
|
||||||
|
h.OnUnhandled(func(subject string) { told = append(told, subject) })
|
||||||
|
taken, lost := &bindMsg{subject: "mesh.mod.q.event.x"}, &bindMsg{subject: "mesh.seat.s.event.nobody"}
|
||||||
|
h.hear(taken)
|
||||||
|
h.hear(lost)
|
||||||
|
if !lost.acked || len(told) != 1 || told[0] != "mesh.seat.s.event.nobody" {
|
||||||
|
t.Errorf("the unhandled message: acked %v, told %v", lost.acked, told)
|
||||||
|
}
|
||||||
// W1.12: a seat's event kept in a stream of its own is heard on one bus name and one durable, the maker's and the
|
// W1.12: a seat's event kept in a stream of its own is heard on one bus name and one durable, the maker's and the
|
||||||
// hearer's alike; an event kept last per subject is delivered from the last of each subject. The name is the
|
// hearer's alike; an event kept last per subject is delivered from the last of each subject. The name is the
|
||||||
// contract's Stream<E> as it stands (seatdef.KeptStream), never made again here: a dotted or unprefixed name is
|
// contract's Stream<E> as it stands (seatdef.KeptStream), never made again here: a dotted or unprefixed name is
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ const (
|
|||||||
// EntryNotFoundKind is a part's finding of an entry by fingerprint the seat mesh-catalog's `entry` answered it does not hold
|
// EntryNotFoundKind is a part's finding of an entry by fingerprint the seat mesh-catalog's `entry` answered it does not hold
|
||||||
// (design 47 G §614, A-15, A-103): scope the part, id the fingerprint.
|
// (design 47 G §614, A-15, A-103): scope the part, id the fingerprint.
|
||||||
EntryNotFoundKind = "entry-not-found"
|
EntryNotFoundKind = "entry-not-found"
|
||||||
|
// EventUnhandledKind is a process's finding of the events its durable consumer delivered that no handler of the module
|
||||||
|
// took, one per module, counted since the process started (design 47 H, H-A-166, H-A-169).
|
||||||
|
EventUnhandledKind = "event-unhandled"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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
|
// 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
|
||||||
@@ -120,3 +123,11 @@ func WriteEntryNotFound(w WireWriter, fingerprint string, since, at time.Time) e
|
|||||||
func ClearEntryNotFound(w WireWriter, fingerprint string) error {
|
func ClearEntryNotFound(w WireWriter, fingerprint string) error {
|
||||||
return viewFinding{kind: EntryNotFoundKind, id: fingerprint}.drop(w)
|
return viewFinding{kind: EntryNotFoundKind, id: fingerprint}.drop(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// unhandledFinding is the module's one finding of the events no handler took since its process started (H-A-169): its id the
|
||||||
|
// module, its summary the count and the last subject.
|
||||||
|
func unhandledFinding(module string, count int, last string, since, at time.Time) viewFinding {
|
||||||
|
summary := fmt.Sprintf("%s took %d events by no handler since its process started at %s, the last on %s: what the module consumes it does not act on",
|
||||||
|
module, count, since.UTC().Format(time.RFC3339), last)
|
||||||
|
return viewFinding{kind: EventUnhandledKind, id: module, summary: summary, headline: fmt.Sprintf("%s hears events it does not handle", module), at: at.UTC()}
|
||||||
|
}
|
||||||
|
|||||||
@@ -73,6 +73,10 @@ type ProcessReader struct {
|
|||||||
asked bool
|
asked bool
|
||||||
begun map[string]context.CancelFunc
|
begun map[string]context.CancelFunc
|
||||||
status *process.AwaitedBucket
|
status *process.AwaitedBucket
|
||||||
|
// unheard counts the events no handler of the module took since Start, the last of them on lastUnheard (H-A-169).
|
||||||
|
unheard int
|
||||||
|
lastUnheard string
|
||||||
|
startedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// adoptedHear is one adopted follower's handler of its seat event, by its picture's bucket.
|
// adoptedHear is one adopted follower's handler of its seat event, by its picture's bucket.
|
||||||
@@ -395,6 +399,50 @@ func (r *ProcessReader) Hear(ctx context.Context) func(jetstream.Msg) {
|
|||||||
}
|
}
|
||||||
r.logf("views: %s is no views event of an owner followed, acknowledged", m.Subject())
|
r.logf("views: %s is no views event of an owner followed, acknowledged", m.Subject())
|
||||||
_ = m.Ack()
|
_ = m.Ack()
|
||||||
|
r.unhandled(m.Subject())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// now is the reader's clock.
|
||||||
|
func (r *ProcessReader) now() time.Time {
|
||||||
|
if r.Now != nil {
|
||||||
|
return r.Now()
|
||||||
|
}
|
||||||
|
return time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
// unhandled counts an event no handler of the module took, said in the log already, in the module's finding
|
||||||
|
// `event-unhandled` (design 47 H, H-A-166, H-A-169); with no findings writer the log line stands alone.
|
||||||
|
func (r *ProcessReader) unhandled(subject string) {
|
||||||
|
if r.Findings == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r.mu.Lock()
|
||||||
|
r.unheard++
|
||||||
|
r.lastUnheard = subject
|
||||||
|
count, since := r.unheard, r.startedAt
|
||||||
|
r.mu.Unlock()
|
||||||
|
w := r.Findings()
|
||||||
|
if w == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := unhandledFinding(r.Module, count, subject, since, r.now()).put(w); err != nil {
|
||||||
|
r.logf("views: the finding %s of %s is not written: %v", EventUnhandledKind, r.Module, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// clearUnhandled clears the module's finding `event-unhandled` at the process's start: a new build may handle the event
|
||||||
|
// (H-A-169).
|
||||||
|
func (r *ProcessReader) clearUnhandled() {
|
||||||
|
if r.Findings == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w := r.Findings()
|
||||||
|
if w == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := (viewFinding{kind: EventUnhandledKind, id: r.Module}).drop(w); err != nil {
|
||||||
|
r.logf("views: the finding %s of %s is not cleared at its start: %v", EventUnhandledKind, r.Module, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -847,7 +895,13 @@ func (r *ProcessReader) Start(ctx context.Context, js jetstream.JetStream, cred
|
|||||||
})
|
})
|
||||||
hear := r.Hear(ctx)
|
hear := r.Hear(ctx)
|
||||||
err := hub.Handle(pattern, hear)
|
err := hub.Handle(pattern, hear)
|
||||||
|
if r.Findings != nil {
|
||||||
|
// H-A-169: the events no handler of the module takes are its finding, cleared at each start
|
||||||
|
r.clearUnhandled()
|
||||||
|
hub.OnUnhandled(r.unhandled)
|
||||||
|
}
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
|
r.startedAt = r.now()
|
||||||
r.status = status
|
r.status = status
|
||||||
if !own {
|
if !own {
|
||||||
// an event adopted after the start is handled on the module's hub (Adopt); the reader's own hub takes every one
|
// an event adopted after the start is handled on the module's hub (Adopt); the reader's own hub takes every one
|
||||||
|
|||||||
+17
-2
@@ -955,8 +955,23 @@ func subViews(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
m := msg()
|
m := msg()
|
||||||
if pr.Hear(ctx)(m); len(pfs.m) != 0 || len(pr.Followers()) != 1 || len(*heard["pnext"]) != 1 || !m.acked {
|
if pr.Hear(ctx)(m); len(pr.Followers()) != 1 || len(*heard["pnext"]) != 1 || !m.acked {
|
||||||
t.Errorf("after the drop: findings %v, followers %d, heard %v, acked %v", pfs.m, len(pr.Followers()), *heard["pnext"], m.acked)
|
t.Errorf("after the drop: followers %d, heard %v, acked %v", len(pr.Followers()), *heard["pnext"], m.acked)
|
||||||
|
}
|
||||||
|
// H-A-166, H-A-169: the event no handler takes, the dropped view's here, is the module's one finding event-unhandled,
|
||||||
|
// counted since the start, and the next start clears it.
|
||||||
|
if fd := pfs.next(t); fd.Kind != EventUnhandledKind || fd.ID != "reader" || !strings.Contains(fd.Summary, "took 1 events") ||
|
||||||
|
!strings.Contains(fd.Summary, m.subject) || len(pfs.m) != 1 {
|
||||||
|
t.Errorf("after the drop: findings %v, the unhandled event's %+v", pfs.m, fd)
|
||||||
|
}
|
||||||
|
pr.Hear(ctx)(&seatMsg{subject: "mesh.seat.s.event.nobody"})
|
||||||
|
if fd := pfs.next(t); !strings.Contains(fd.Summary, "took 2 events") || !strings.Contains(fd.Summary, "mesh.seat.s.event.nobody") || len(pfs.m) != 1 {
|
||||||
|
t.Errorf("the second unhandled event: findings %v, %+v", pfs.m, fd)
|
||||||
|
}
|
||||||
|
again := &ProcessReader{Module: "reader", Findings: func() WireWriter { return pfs }, Now: now, Hub: process.NewEventHub(t.Logf)}
|
||||||
|
again.Start(ctx, nil, process.Account{}, nil)
|
||||||
|
if len(pfs.m) != 0 {
|
||||||
|
t.Errorf("the start left the finding %v", pfs.m)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// Design 47 G §717: a view found by its seat starts without a holder, waits naming the seat, follows the holder the
|
// Design 47 G §717: a view found by its seat starts without a holder, waits naming the seat, follows the holder the
|
||||||
|
|||||||
Reference in New Issue
Block a user