diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go index 1ac846fc4..e9c5afe16 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/findings.go @@ -32,32 +32,40 @@ func NewFindingWriter() (*views.HeldWriter[meshconditions.Finding, meshcondition Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{HeadUntaken, RecordTooLarge}}) } -// recordTooLarge raises record-too-large for a delivery record's entry the gate refused over EntryLimit, or clears it when -// the writer holds it (design 47 H2 §809): the record's records hook. -func (h *Holder) recordTooLarge(instance string, bytes int, raised bool) { +// recordTooLarge raises record-too-large for a delivery record's entry the gate refused over EntryLimit, its subject the +// record (design 47 H2 §809; H-A-460), or clears it when the writer holds it; with no instance it clears every one the +// writer holds whose subject is the record (its end, H-A-455 (3)): the record's records hook. +func (h *Holder) recordTooLarge(record, instance string, bytes int, raised bool) { if h.Findings == nil { return } - held := false + var held []string for _, f := range h.Findings.Held() { - if f.Scope == FindingScope && f.ID == instance && f.Kind == RecordTooLarge { - held = true + if f.Scope != FindingScope || f.Kind != RecordTooLarge { + continue + } + if instance == "" && f.Subject.ID == record || instance != "" && f.ID == instance { + held = append(held, f.ID) } } var err error switch { - case raised && !held: + case raised && len(held) == 0: now := h.Now().UTC() err = h.Findings.Write(context.Background(), meshconditions.Finding{Scope: FindingScope, ID: instance, Kind: RecordTooLarge, Observer: Module, Severity: meshconditions.FindingSeverityWarning, Resolver: "operator", Since: now, At: now, Summary: fmt.Sprintf("%s would hold %d bytes, over the %d one entry holds: the write is refused", instance, bytes, EntryLimit), - Headline: "A delivery record's entry is too large", Subject: meshconditions.Subject{Scope: FindingScope, ID: instance}, + Headline: "A delivery record's entry is too large", Subject: meshconditions.Subject{Scope: FindingScope, ID: record}, Explanation: "The gate keeps one entry per record and one per mover, each within 64 KiB (design 47 H2 §809); " + "this entry would pass it, so the write is refused. It clears when the entry is next written within 64 KiB or the record ends."}) - case !raised && held: - err = h.Findings.Clear(context.Background(), FindingScope, instance, RecordTooLarge, "the entry is written within 64 KiB, or its record ended") + case !raised: + for _, id := range held { + if e := h.Findings.Clear(context.Background(), FindingScope, id, RecordTooLarge, "the entry is written within 64 KiB, or its record ended"); e != nil { + err = e + } + } } if err != nil && h.Logf != nil { - h.Logf("[mesh-delivery] record-too-large of %s is not said: %v", instance, err) + h.Logf("[mesh-delivery] record-too-large of %s (%s) is not said: %v", instance, record, err) } } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/movers.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/movers.go index f70f710ed..8e3ca90ea 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/movers.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/movers.go @@ -211,12 +211,21 @@ func (rs *Records) write(r *Record, instance, operation string, body, state any) if err != nil { return 0, err } - rs.fitted(instance) + rs.fitted(r, instance) return seq, nil } -// fits refuses an entry of r whose state would pass EntryLimit, raising record-too-large for it (§809); the entry is -// remembered with its record, so the record's end clears it whichever entry it was (H-A-455 (3)). +// recordOf is the instance of r's head entry, the subject of its entries' findings (H-A-460). +func recordOf(r *Record) string { + if r.Instance != "" { + return r.Instance + } + inst, _ := RecordInstance(r.Milestone) + return inst +} + +// fits refuses an entry of r whose state would pass EntryLimit, raising record-too-large for it with its record as the +// finding's subject (§809; H-A-460), so the record's end clears it whichever entry it was (H-A-455 (3)). func (rs *Records) fits(r *Record, instance string, state any) error { raw, err := json.Marshal(state) if err != nil { @@ -225,46 +234,24 @@ func (rs *Records) fits(r *Record, instance string, state any) error { if len(raw) <= EntryLimit { return nil } - rs.mu.Lock() - if rs.refused == nil { - rs.refused = map[string]string{} - } - rs.refused[instance] = r.Milestone - rs.mu.Unlock() if rs.TooLarge != nil { - rs.TooLarge(instance, len(raw), true) + rs.TooLarge(recordOf(r), instance, len(raw), true) } return fmt.Errorf("the delivery record's entry %s would hold %d bytes, over the %d an entry holds: refused (%s)", instance, len(raw), EntryLimit, RecordTooLarge) } -// fitted clears record-too-large for an entry written within EntryLimit (TooLarge clears only one it holds). -func (rs *Records) fitted(instance string) { +// fitted clears record-too-large for an entry of r written within EntryLimit (TooLarge clears only one it holds). +func (rs *Records) fitted(r *Record, instance string) { if rs.TooLarge != nil { - rs.TooLarge(instance, 0, false) + rs.TooLarge(recordOf(r), instance, 0, false) } } -// clearTooLarge clears record-too-large for every entry of a record at its end: its head, its movers, its acts and every -// entry of it a write was refused for. +// clearTooLarge clears, at a record's end, every record-too-large the gate's findings hold whose subject is the record, +// whichever entry it was refused for and whenever it was raised: read from the findings state, nothing kept in process +// memory (§809, H-A-455 (3), H-A-460). func (rs *Records) clearTooLarge(r *Record) { - insts := []string{r.Instance} - for _, m := range r.movers { - insts = append(insts, m.Instance) - } - for id := range r.Acts { - if inst, err := ActInstance(r.Milestone, id); err == nil { - insts = append(insts, inst) - } - } - rs.mu.Lock() - for inst, of := range rs.refused { - if of == r.Milestone { - insts = append(insts, inst) - delete(rs.refused, inst) - } - } - rs.mu.Unlock() - for _, inst := range insts { - rs.fitted(inst) + if rs.TooLarge != nil { + rs.TooLarge(recordOf(r), "", 0, false) } } diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedended.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedended.go index 8424195e9..d8e6e6319 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedended.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedended.go @@ -110,7 +110,7 @@ func (rs *Records) Act(r *Record, a RecordAct) error { return err } delete(r.Acts, a.ID) - rs.fitted(inst) + rs.fitted(r, inst) return nil } a.Record = r.Milestone diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go index e80ff1ea7..b7f4804ee 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record.go @@ -379,10 +379,9 @@ type Records struct { // Class reads a mover's class from its catalog entry by its fingerprint (design 47 H2 §810; H-A-394 (2)), once, when its // entry opens; nil reads none, every mover a leaf. Class func(fingerprint string) (string, error) - // TooLarge raises (raised) or clears record-too-large for an entry (§809); nil raises none. - TooLarge func(instance string, bytes int, raised bool) - // refused are the entries a write was refused for over EntryLimit, each with its record's key, until the record ends. - refused map[string]string + // TooLarge raises (raised) or clears record-too-large for an entry of the record named by its head's instance, the + // finding's subject (§809; H-A-460); an empty instance clears every one held of the record (its end). nil raises none. + TooLarge func(record, instance string, bytes int, raised bool) // held are the records kept, by instance, read once from the ledger (Current) and kept as the gate writes them: the // gate is their one writer. diff --git a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go index e31a8b5d0..513150b59 100644 --- a/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go +++ b/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/record_test.go @@ -13,6 +13,7 @@ import ( "time" "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient" + "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery" stdio "git.novox.be/novox/mesh-sdk/go" "git.novox.be/novox/mesh-sdk/go/buscalls" @@ -199,14 +200,11 @@ func TestTheDeliveryRecordIsALedgerInstanceKeyedByItsSourceRevision(t *testing.T // 64 KiB, the head counting 126; an entry made too large is refused with record-too-large, cleared at the record's end. t.Run("a record of 126 movers writes 127 entries, each within 64 KiB", func(t *testing.T) { l := newLedgerFake(t) - found := map[string]bool{} - rs := &Records{Ledger: l, Now: func() time.Time { return at }, TooLarge: func(inst string, _ int, raised bool) { - if raised { - found[inst] = true - } else { - delete(found, inst) - } - }} + // The findings are the gate's own findings state (H-A-460): a fresh Records, as after a restart, reads them there. + fw := &findingsFake{held: map[string]meshconditions.Finding{}} + h := &Holder{Findings: fw, Now: func() time.Time { return at }} + found := fw.held + rs := &Records{Ledger: l, Now: func() time.Time { return at }, TooLarge: h.recordTooLarge} r, _, err := rs.Open(opening("main", 90, heads)) if err != nil { t.Fatal(err) @@ -253,8 +251,26 @@ func TestTheDeliveryRecordIsALedgerInstanceKeyedByItsSourceRevision(t *testing.T Owed: meshdelivery.Owed{Act: meshdelivery.OwedActView, Text: why}}); err == nil || !strings.Contains(err.Error(), RecordTooLarge) || len(found) != 2 { t.Fatalf("an act over 64 KiB was taken (%v), findings %v", err, found) } - if err := rs.End(read, "every present node runs its entries", ""); err != nil || len(found) != 0 { - t.Fatalf("the record's end left %v (%v)", found, err) + for _, f := range found { + if f.Subject.ID != read.Instance { + t.Fatalf("a refusal of %s names %q as its subject, not its record %s", f.ID, f.Subject.ID, read.Instance) + } + } + // Another record's refusal is not this record's to clear. + other, _ := RecordInstance("main.other") + h.recordTooLarge(other, "mover/main_other_x", EntryLimit+1, true) + // A fresh Records, as after a restart, ends the record: every refusal of it clears, the act never written among + // them, read from the findings state alone (§809, H-A-455 (3), H-A-460). + restarted := &Records{Ledger: l, Now: func() time.Time { return at }, TooLarge: h.recordTooLarge} + again, err := restarted.Read(r.Instance) + if err != nil { + t.Fatal(err) + } + if err := restarted.End(again, "every present node runs its entries", ""); err != nil || len(found) != 1 { + t.Fatalf("the record's end after a restart left %v (%v)", found, err) + } + if _, ok := found[FindingScope+".mover/main_other_x."+RecordTooLarge]; !ok { + t.Fatalf("the record's end cleared another record's refusal: %v", found) } })