Skip to content

test(proxy): wait on the writer's counters in seedStopReasonHistory - #1034

Merged
amiddavid merged 1 commit into
mainfrom
fix/flake-adapt
Nov 10, 2026
Merged

amiddavid merged 1 commit into
mainfrom
fix/flake-adapt

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

Fixes the flaky TestAdaptReportGateMatchesTheBuilder. Test-only, no behavior change.

What we found

  • Cause (a): the writer is slow, not lossy. It does not drop or hold rows.
  • The queue holds 4096 events. The test sends 160. The drop counter stays 0.
  • The fixture sets BatchSize: 1, so each row is its own transaction. Under -race and CPU load, 160 rows need more than 10 s.
  • Proof: on the box, 4 cores with 16 busy loops, the old helper failed 15 of 15 runs (14.3 to 15.2 s each).

What changed

  • seedStopReasonHistory (proxy/adaptcache_test.go) waits until Stats().Written >= Stats().Captured. It no longer polls for 10 s.
  • A drop or an error fails at once, with the counts.
  • A 2 minute bound stays as a safety net. Its message shows captured, written, dropped, errors and queued.
  • The other two tests that use the helper also gain.

How to reproduce and test

Load: taskset -c 0-3 sh -c 'while :; do :; done' & x16, killed by pid after.

CGO_ENABLED=1 GOMAXPROCS=4 taskset -c 0-3 go test -race -count=15 -run '^TestAdaptReportGateMatchesTheBuilder$' ./proxy/

  • Before: 15 of 15 fail ("the writer did not persist the rows").
  • After: 0 of 15 fail (runs take 16.8 to 18.2 s).
  • Negative check: the helper records 159 rows and waits for 160. The test fails at once: "captured 159, written 159". I reverted this change.
  • go test -race ./proxy/...: ok (538 s). go vet ./proxy/ and gofmt -l proxy: clean.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 70ba5463a120 approve

Verdict: approve

Test-only change. seedStopReasonHistory now waits on the recorder's own counters instead of a fixed 10 s poll. No product code changed. I found no real defect.

Findings

None.

Non-blocking

  • proxy/adaptcache_test.go:694-697 — the fast fatal cannot tell a query error from a missing row. Requests returns a nil page on a transient read error, and the message then says "the tenant has fewer than 160". Risk is low: the query runs only after Written >= Captured, when the writer has no DB work left, so a lock conflict is unlikely. Fix if you touch this again: print the query error and p.Total too.
  • proxy/adaptcache_test.go:690 — Dropped and Errors are lifetime counters. A drop or an error from rows captured before the seed would fail the seed wait. In these three tests the recorder sees no earlier rows, and the queue holds 4096 against 160, so this cannot fire today.
  • proxy/adaptcache_test.go:688 — a genuine failure now costs up to 2 minutes per test, 6 minutes across the three users of the helper. The PR names this a safety net, so it is a choice, not a defect.

What I checked

  • The full diff (one file, +14/−4) and the code around it.
  • The counter semantics the wait relies on:
    • Record counts captured before the queue send, and dropped only on a full queue (dash/capture.go:370-375). The queue holds 4096 (defaultQueueSize, dash/capture.go:121), the seed sends 160, so drops cannot happen in this fixture.
    • run/write count written only after insertBatch commits, and errors covers a failed batch (dash/capture.go:529-533). So Written >= Captured implies every captured row is committed. insertBatch inserts every event with no filtering (dash/store.go:552), so written rows are visible rows.
  • The fixture claim in the new comment: newCampaignFixture sets BatchSize: 1, FlushInterval: 1 ms, DBPath: ":memory:" (proxy/campaignbuild_test.go:39). All three callers of seedStopReasonHistory (lines 250, 551, 633) use this fixture.
  • The DB check: Requests with Filter{Tenant: tid} counts every row of the tenant (dash/query.go:271-313, where at dash/query.go:96), keep-alive rows included. The seeded 160 rows carry that tenant id and distinct timestamps.
  • Background jobs that could delete rows during the 2-minute wait: the janitor runs every 5 minutes and the archiver every 15 (dash/capture.go:132, :135), both outside the wait window.
  • No concurrent Record calls during the wait: all three tests seed before any further HTTP request.
  • The PR description matches the diff, including the reproduction and the negative check.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 70ba5463a120 approve

Verdict: approve

Findings

None.

Non-blocking

  • proxy/adaptcache_test.go:697 — the Requests read error is discarded, so a failed read is reported as lost rows.
    The loop reads p, _ := f.rec.DB().Requests(...). If the query fails, p is nil and the fatal says "the writer stored every captured row but the tenant has fewer than 160" — a wrong claim about the cause. The sibling helper f.record (proxy/manager_test.go:103-105) treats a read error as its own failure. I could not construct a live trigger (see What I checked), so this is hardening, not a defect. Fix: keep the error and print it, or treat p == nil as "not ready, retry".
  • proxy/adaptcache_test.go:690 — the bound grew from 10 s to 2 min per caller. A truly stuck writer now costs up to 2 min in each of the 3 tests that use this helper, against the old 10 s. The Makefile runs -timeout 20m and ./proxy/... takes about 7.5 min under -race, so it fits. The comment says the bound is a safety net only. Acceptable.

What I checked

  • The full diff: one commit, one function (seedStopReasonHistory), test code only. No production behavior, no data-dependence change.
  • The counter semantics the new wait rests on (dash/capture.go:370-374,530-533): Record counts a row as captured before the enqueue; the single writer goroutine adds each row to written after insertBatch commits, to errors on failure, or the full queue counts it dropped. So every captured row ends written, dropped or errored — the comment's claim is true.
  • The wait predicate Written >= Captured: all 160 rows are recorded before the loop starts, the channel is FIFO, and the writer is the only consumer. So Written >= Captured (with Captured >= 160) forces all 160 seeded rows to be committed. The fatal in that branch cannot fire while a seeded row is still in flight.
  • The drop/error fatal cannot fire spuriously: the queue cap is 4096 (dash/capture.go:121) against 160 rows, and both fixtures set BatchSize: 1, so the "one transaction per row" claim in the comment is true.
  • The tenant count (dash/query.go:271, Filter.where at dash/query.go:96): Filter{Tenant: tid} matches r.tenant_id = ? only — no time window, and Requests forces keep-alive rows in. Once the rows are committed, Total >= 160 holds.
  • Concurrent DB access during the wait: the in-memory DSN uses cache=shared (dash/store.go:96). The read runs only when the writer is quiescent. Other DB writers do not overlap it: the count worker needs BypassCount (absent on the seeded events, dash/capture.go:1066), the janitor first fires at 5 min (pruneInterval, dash/capture.go:135), and the startup migrations finish during sign-up. The existing helper f.record polls Requests every 5 ms during active writes and fatals on a read error, and the suite is stable — so a read at quiescence does not fail in practice.
  • The Stats() fields used in the messages (Captured, Written, Dropped, Errors, Queued) all exist (dash/capture.go:447-476), and Stats() is safe to poll: atomics, len(ch), and mutex-guarded reads.
  • The three callers of the helper (proxy/adaptcache_test.go:250, 551, 685) record no other dash events before or during the wait, so the per-test recorder's counters describe only the seeded rows.

The helper polled for 160 rows for a fixed 10 s. The fixture writes one
row per transaction, so under -race on a loaded runner the writer needs
more than 10 s. Nothing is lost: the queue holds 4096 events and the
drop counter stays 0.

The helper now waits until Written reaches Captured. A drop or an error
fails at once with the counts. A 2 minute bound stays as a safety net.

Test-only. No behavior change.

Closes #1028

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Rebased onto main and hardened. New head: 12fa3441.

  • Rebase: onto 77a910b0 (fix(plugin): the band's window saving survives the #989 change #1035, the fix for the TestCGCachestate* failures on main). On the old head, build-test failed only on those 2 plugin tests. Main had the same failure since feat(plugin): show the band's subscription saving from the tokens fields #1018.
  • Both reviewers, non-blocking (a read error shows as lost rows): fixed. The helper keeps the Requests error and fails with it. When rows are missing, the message prints how many the tenant has.
  • The other non-blocking points stay as they are: the lifetime counters cannot fire in these 3 tests (queue 4096, 160 rows, no earlier rows). The 2-minute bound is a safety net only.
  • Tests on the box at this head: go test -race ./proxy/... passes (540 s). -count=3 -run '^TestAdaptReport' passes. go vet and gofmt are clean.
  • Diff vs main: 1 file, proxy/adaptcache_test.go (+22 −4).

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 offload-review skill, @amiddavid's session resumed 1 of 2 2 of 2 12fa34410e9a approve

Verdict: approve

The PR is one test-only commit, rebased onto main and hardened. Both round-1 reviews approved. The new head fixes the one shared non-blocking point. I found no defect, old or new.

Findings

None.

Status of earlier findings

  • Mine, non-blocking, adaptcache_test.go:694-697 (a read error reported as lost rows): fixed. The helper now keeps the Requests error and fails with its own message (adaptcache_test.go:694-697). The missing-row message prints the tenant's actual count (adaptcache_test.go:701-705).
  • Mine, non-blocking, lifetime Dropped/Errors counters: rejected by the author, reason accepted. The author says they cannot fire in these 3 tests. I verified this in round 1: the queue holds 4096 against 160 rows, and no earlier rows are captured before the seed.
  • Mine, non-blocking, the 2-minute bound: kept as a safety net, accepted. Reviewer 2 checked it fits: the Makefile allows 20 m and ./proxy/... takes about 7.5 m under -race.
  • Reviewer 2's two non-blocking points match mine. I checked both myself. The read-error point is the same fix; the bound point is the same acceptance.

Non-blocking

  • proxy/adaptcache_test.go:701-704 — the p != nil guards are dead code. Requests returns a non-nil page on every err == nil path (dash/query.go:293). Harmless defensive handling; no action needed.

What I checked

  • Last review: round 1, 70ba5463a1204831740ac6774fd9b9a15ef820c6, 2026-11-10T10:12:44Z.
  • New commits read: 3 in the range 70ba5463..12fa3441.
    • 12fa3441 — the PR's own commit, rewritten. Reviewed in full below.
    • a3495863 and 77a910b0 — main commits pulled in by the rebase (#1011, #1035). Both are on origin/main; neither is this PR's change. The PR diff vs the base 77a910b0 is one file, proxy/adaptcache_test.go (+22 −4).
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three saved files).
  • New items since my last review, and what I did about each:
    • Reviewer 2's round-1 review (10:15:11Z): read in full. Two non-blocking points, both overlapping mine. Checked each against the code myself; agree with both dispositions.
    • Author's comment (11:01:34Z): read in full. Claims the rebase reason (main was red on two plugin tests, fixed by #1035), the fix for the read-error point, the two kept points, and green tests on the box at the new head. The code matches every claim I can check by reading.
    • The new head 12fa3441: reviewed the full diff against both the base and the old head.
  • The hardening in 12fa3441: the error branch fails with the error and the counters; the success path is unchanged from round 1. The wait predicate and the counter semantics are unchanged, so my round-1 verification stands: Written >= Captured implies every seeded row is committed, the writer is quiescent when the read runs, and the janitor (5 m) and archiver (15 m) stay outside the 2-minute window.
  • Commit message: conventional title, DCO sign-off, Assisted-By: attribution per repo convention, Closes #1028 for the flake it fixes.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.322 offload-review skill, @amiddavid's session resumed 2 of 2 2 of 2 12fa3441 approve

Verdict: approve

Findings

None.

Earlier findings

  • My round-1 non-blocking (the discarded Requests error shows as lost rows): fixed. The helper now keeps the error and fails with it (proxy/adaptcache_test.go:697-699). When rows are missing, the message prints the tenant's actual total (proxy/adaptcache_test.go:708). This is the fix I proposed.
  • Reviewer 1's non-blocking (same read-error point): fixed by the same change. I checked it myself; the fix covers both wordings.
  • Reviewer 1's non-blocking (lifetime counters) and the 2-minute bound: rejected by the author, with reasons. The author's comment says the counters cannot fire in these 3 tests and the bound is a safety net. I verified the same facts in round 1: the queue holds 4096 against 160 rows, the three tests record no earlier rows, and the Makefile allows -timeout 20m. The reasons are right. Nothing to keep.

Non-blocking

  • proxy/adaptcache_test.go:702,706 — the p != nil guards are dead code. Requests returns a non-nil page whenever the error is nil, so after the err != nil fatal the two p != nil checks always pass. Harmless. Leave them or drop them.

What I checked

  • Last review: round 1, 70ba546, 2026-11-10T10:15:11Z.
  • New commits read: 3 (a3495863 feat(pi) #1011, 77a910b0 fix(plugin) #1035, 12fa3441 test(proxy) — the amended PR commit).
  • Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files .review-in-reviews.txt, .review-in-comments.txt, .review-in-inline.txt).
  • New items since my last review:
    • The rebase onto 77a910b0: verified with git range-diff that a3495863 and 77a910b0 in this branch are identical (=) to origin/main. They are main's own commits, reviewed in their own PRs, not re-authored here.
    • The PR's own diff vs main: 1 file, proxy/adaptcache_test.go (+22 −4). Read in full.
    • The amended error handling: err is now checked before the total, and the missing-rows fatal prints the real total. Requests returns (nil, err) or (page, nil), so the logic is correct.
    • The author's comment (2026-11-10T11:01:34Z): each claim checked — rebase verified, fix verified, the two kept non-blocking points verified in round 1, and the box test run (go test -race ./proxy/... passes, 540 s) is the author's report; live tests are not approved for me, so I did not run them.
  • The rest of the helper is unchanged from the head I approved in round 1; my round-1 checks (counter semantics, FIFO writer, queue capacity, tenant filter, no overlapping DB writers) still hold at this head.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant