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.
This commit is contained in:
@@ -105,13 +105,22 @@ func nextCursor(rows []db.GossipObservation) int64 {
|
|||||||
return rows[len(rows)-1].HCL
|
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.
|
// 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
|
// 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.
|
// at worst a flood; capping keeps memory and insert work bounded.
|
||||||
const maxBatchRows = 1000
|
const maxBatchRows = 1000
|
||||||
|
|
||||||
func (n *Node) handleBatch(w http.ResponseWriter, r *http.Request) {
|
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)
|
body, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "batch too large or unreadable", http.StatusBadRequest)
|
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 {
|
if len(rows) == 0 {
|
||||||
return 0, nil
|
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)
|
body, _ := json.Marshal(rows)
|
||||||
req, err := http.NewRequest(http.MethodPost, c.Addr+"/v1/obs/batch", bytes.NewReader(body))
|
req, err := http.NewRequest(http.MethodPost, c.Addr+"/v1/obs/batch", bytes.NewReader(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -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.
|
// must be refused even when the row count is small.
|
||||||
func TestHandleBatchRejectsHugeBody(t *testing.T) {
|
func TestHandleBatchRejectsHugeBody(t *testing.T) {
|
||||||
b := tmpKnoxDB(t)
|
b := tmpKnoxDB(t)
|
||||||
@@ -300,7 +300,7 @@ func TestHandleBatchRejectsHugeBody(t *testing.T) {
|
|||||||
rows := []db.GossipObservation{{
|
rows := []db.GossipObservation{{
|
||||||
NodeID: "0123456789abcdef0123456789abcdef", HCL: 1,
|
NodeID: "0123456789abcdef0123456789abcdef", HCL: 1,
|
||||||
Fingerprint: "fp:huge", SourceID: "test", Title: "t",
|
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",
|
CollectedAt: "2026-08-29T00:00:00Z",
|
||||||
}}
|
}}
|
||||||
body, _ := json.Marshal(rows)
|
body, _ := json.Marshal(rows)
|
||||||
|
|||||||
Reference in New Issue
Block a user