diff --git a/go/views/held_writer.go b/go/views/held_writer.go index 58cbc5d9..c7957ac3 100644 --- a/go/views/held_writer.go +++ b/go/views/held_writer.go @@ -139,9 +139,10 @@ func (h *HeldWriter[F, C, S, A]) Held() []F { return out } -// Start seeds what the writer holds from the keys of its state `findings` (design 47 G A-40), what was written before it -// standing over them, writes what was written and clears what was cleared before it, and starts the writer. A part makes the -// writer before its module serves, so nothing is read from the state until now. +// Start seeds what the writer holds from the keys of its state `findings` of the kinds it owns (design 47 G A-40, §580, H +// H-A-242: another bundle's keys are its own to state), what was written before it standing over them, writes what was +// written and clears what was cleared before it, and starts the writer. A part makes the writer before its module serves, +// so nothing is read from the state until now. func (h *HeldWriter[F, C, S, A]) Start(ctx context.Context) error { keys, err := h.state.Keys() if err != nil { @@ -154,6 +155,9 @@ func (h *HeldWriter[F, C, S, A]) Start(ctx context.Context) error { h.mu.Unlock() return err } + if !h.Writer.ownsKey(k) { + continue // another bundle's kind: it states its own (design 47 G §580, H H-A-242 (1)) + } if _, ok := h.held[k]; ok { continue } diff --git a/go/views/writer.go b/go/views/writer.go index ac32c822..409afd32 100644 --- a/go/views/writer.go +++ b/go/views/writer.go @@ -212,6 +212,9 @@ type Writer[F, C, S, A any] struct { wake chan struct{} run context.Context end context.CancelFunc + // said are the kinds a refusal was said of in the journal, once each (design 47 H H-A-242 (3)). + saidMu sync.Mutex + said map[string]bool } // NewWriter is the writer for cfg. @@ -433,6 +436,33 @@ func (w *Writer[F, C, S, A]) loop(ctx context.Context) { } } +// unownedKind is the refusal of a finding kind the writer does not own (design 47 G §580, H H-A-240, H-A-242). +type unownedKind struct { + kind string + kinds []string +} + +func (e *unownedKind) Error() string { + return fmt.Sprintf("finding kind %q not declared: it is not one this writer owns (%v)", e.kind, e.kinds) +} + +// refuse is the refusal of kind, said in the journal the first time only (design 47 H H-A-242 (3)): a kind no writer list +// declares is a fault of the part's code, said once, never once a minute. +func (w *Writer[F, C, S, A]) refuse(kind string) error { + err := &unownedKind{kind: kind, kinds: w.cfg.Kinds} + w.saidMu.Lock() + first := !w.said[kind] + if w.said == nil { + w.said = map[string]bool{} + } + w.said[kind] = true + w.saidMu.Unlock() + if first { + w.cfg.Logf("%v: the part's code raises a kind its writer does not declare; said once", err) + } + return err +} + // owns says whether the writer's statement covers kind: every kind when none were given (design 47 G §580), else its // own and the views reader's. func (w *Writer[F, C, S, A]) owns(kind string) bool { @@ -523,7 +553,7 @@ func (w *Writer[F, C, S, A]) complete(finding F) (wire, wire, error) { return f, nil, err } if !w.owns(str(f, "kind")) { - return f, nil, fmt.Errorf("the finding kind %q is not one this writer owns (%v)", str(f, "kind"), w.cfg.Kinds) + return f, nil, w.refuse(str(f, "kind")) } old, err := w.read(keyOf(f)) if err != nil { @@ -590,7 +620,7 @@ func (w *Writer[F, C, S, A]) Write(ctx context.Context, finding F) error { func (w *Writer[F, C, S, A]) Clear(ctx context.Context, scope, id, kind, why string) error { key := FindingKey(scope, id, kind) if !w.owns(kind) { - return fmt.Errorf("the finding kind %q is not one this writer owns (%v)", kind, w.cfg.Kinds) + return w.refuse(kind) } w.mu.Lock() if err := w.cfg.State.Delete(key); err != nil { @@ -633,6 +663,10 @@ func (w *Writer[F, C, S, A]) stated(ctx context.Context, asked string) ([]byte, findings, pending := []wire{}, []wire{} for _, finding := range obs.Findings { f, _, err := w.complete(finding) + var refused *unownedKind + if errors.As(err, &refused) { + continue // said once in the journal by refuse, never a reason to state again (design 47 H H-A-242 (3)) + } if err != nil { return nil, err } diff --git a/go/views/writer_test.go b/go/views/writer_test.go index 542f2f96..5ea263e4 100644 --- a/go/views/writer_test.go +++ b/go/views/writer_test.go @@ -461,6 +461,14 @@ func subWriterKindsAndReaderFindings(t *testing.T) { if _, ok := r.state.m["module.m=2eo=2es.view-waits"]; ok { t.Errorf("the reader's finding was not cleared") } + // H-A-242 (3): an observed finding of a kind the writer does not own is left out of the statement, never a reason to state + // again once a minute + r.observe = func(context.Context) (testObserved, error) { + return testObserved{Findings: []testFinding{finding("a", "observed"), other}}, nil + } + if err := r.w.State(ctx, ""); err != nil { + t.Errorf("a statement with a finding of a kind not owned: %v", err) + } } // A views reader's four findings are laid out by mesh-sdk through the body's setters (design 47 G §687): the finding's @@ -637,8 +645,60 @@ func subHeldWriterSeedsAtStart(t *testing.T) { if held := h.Held(); len(held) != 2 { t.Fatalf("after Start Held answers the seeded and the early: %v", held) } - if _, ok := state.m["module.early.k"]; !ok { - t.Fatalf("the early finding was written at Start: %v", state.m) + if e, _ := state.Get("module.early.k"); e == nil { + t.Fatal("the early finding was not written at Start") + } + // Design 47 G §580, H H-A-240, H-A-242: a writer made with its kinds seeds only keys of those kinds; another bundle's key + // (kind b) is not held, so its statement is said and the key stays; a write of kind b is refused each time and said once. + owned := &fakeState{m: map[string][]byte{}} + owned.m["module.seeded.k"] = raw + other := finding("other", "another bundle's") + other.Kind = "b" + rawOther, _ := json.Marshal(other) + owned.m["module.other.b"] = rawOther + var logs []string + var sent []said + ocfg := testConfig{Module: "m", State: owned, Node: "n1", Asked: asked, Kinds: []string{"k"}, + Logf: func(f string, a ...any) { mu.Lock(); logs = append(logs, fmt.Sprintf(f, a...)); mu.Unlock() }, + Publish: func(_ context.Context, subject string, data []byte, _ string) error { + mu.Lock() + sent = append(sent, said{subject, data}) + mu.Unlock() + return nil + }, + Listen: func(context.Context, string, func([]byte)) (func(), error) { return func() {}, nil }} + oh, err := views.NewHeldWriter(ocfg) + if err != nil { + t.Fatal(err) + } + if err := oh.Start(ctx); err != nil { + t.Fatal(err) + } + defer oh.Close() + if held := oh.Held(); len(held) != 1 || held[0].Kind != "k" { + t.Fatalf("a writer of kinds [k] seeded %v", held) + } + if err := oh.State(ctx, ""); err != nil { + t.Fatalf("the statement of a writer over another bundle's key: %v", err) + } + if e, _ := owned.Get("module.other.b"); e == nil { + t.Fatal("the statement deleted another bundle's key") + } + for range 2 { + if err := oh.Write(ctx, other); err == nil || !strings.Contains(err.Error(), `finding kind "b" not declared`) { + t.Fatalf("a write of kind b: %v", err) + } + } + mu.Lock() + defer mu.Unlock() + refusals := 0 + for _, l := range logs { + if strings.Contains(l, `finding kind "b" not declared`) { + refusals++ + } + } + if refusals != 1 { + t.Fatalf("the refusal of kind b is said %d times, not once: %v", refusals, logs) } }