From 6f1fc5be8470726ef0eb75e56052bae6d50d3cc9 Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Wed, 4 Nov 2026 17:36:36 -0800 Subject: [PATCH] fix: batch pushes by size, not just row count (#8) A 500-row batch of content-heavy observations exceeded the receiver's 4 MiB body limit and returned 400, so the sender retried the same batch on every sweep and the log stalled. Pushes now trim to a 2 MiB budget (rows are ordered by HCL, so the tail simply arrives next time) and the receiver accepts up to 8 MiB. --- internal/watch/gossip.go | 26 +++++++++++++++++++++++++- internal/watch/gossip_test.go | 4 ++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/internal/watch/gossip.go b/internal/watch/gossip.go index 617837d..5c9e580 100644 --- a/internal/watch/gossip.go +++ b/internal/watch/gossip.go @@ -105,13 +105,22 @@ func nextCursor(rows []db.GossipObservation) int64 { return rows[len(rows)-1].HCL } +// maxPushBytes is the payload budget for one push: observations carry the full +// source text, so a row-count limit alone can still produce a batch larger than +// the receiver accepts, which then fails on every sweep. +const maxPushBytes = 2 << 20 + +// maxBatchBody is the largest body a node accepts from a peer. Comfortably +// above maxPushBytes so a sender using a different batching rule still lands. +const maxBatchBody = 8 << 20 + // maxBatchRows bounds the number of observations a peer may push in one POST. // Pull already pages at 500 rows, so any larger batch is at best redundant and // at worst a flood; capping keeps memory and insert work bounded. const maxBatchRows = 1000 func (n *Node) handleBatch(w http.ResponseWriter, r *http.Request) { - r.Body = http.MaxBytesReader(w, r.Body, 4<<20) // 4 MiB + r.Body = http.MaxBytesReader(w, r.Body, maxBatchBody) body, err := io.ReadAll(r.Body) if err != nil { http.Error(w, "batch too large or unreadable", http.StatusBadRequest) @@ -241,6 +250,21 @@ func (c *Client) Push(kdb *db.KnoxDB, peerVector map[string]int64) (int, error) if len(rows) == 0 { return 0, nil } + // Trim to a byte budget. Rows are ordered by HCL, so dropping the tail + // keeps the cursor monotonic and the remainder arrives on the next push. + budget, n := maxPushBytes, 0 + for _, r := range rows { + b, err := json.Marshal(r) + if err != nil { + break + } + if n > 0 && len(b) > budget { + break + } + budget -= len(b) + n++ + } + rows = rows[:n] body, _ := json.Marshal(rows) req, err := http.NewRequest(http.MethodPost, c.Addr+"/v1/obs/batch", bytes.NewReader(body)) if err != nil { diff --git a/internal/watch/gossip_test.go b/internal/watch/gossip_test.go index 11c40d6..c71f703 100644 --- a/internal/watch/gossip_test.go +++ b/internal/watch/gossip_test.go @@ -289,7 +289,7 @@ func TestHandleBatchRejectsOversizedBatch(t *testing.T) { } } -// TestHandleBatchRejectsHugeBody: an oversized body (beyond the 4 MiB cap) +// TestHandleBatchRejectsHugeBody: an oversized body (beyond maxBatchBody) // must be refused even when the row count is small. func TestHandleBatchRejectsHugeBody(t *testing.T) { b := tmpKnoxDB(t) @@ -300,7 +300,7 @@ func TestHandleBatchRejectsHugeBody(t *testing.T) { rows := []db.GossipObservation{{ NodeID: "0123456789abcdef0123456789abcdef", HCL: 1, Fingerprint: "fp:huge", SourceID: "test", Title: "t", - Summary: strings.Repeat("x", 5<<20), // 5 MiB summary + Summary: strings.Repeat("x", 9<<20), // 9 MiB, past maxBatchBody CollectedAt: "2026-08-29T00:00:00Z", }} body, _ := json.Marshal(rows)