Repository navigation
fix(usage): keep usage events when an insert fails - #14638
Conversation
StatsUsage acknowledged each message once its events were folded into the open batch, then logged and discarded the batch if the insert failed, so a ClickHouse outage or a rejected insert lost every event in it. A batch whose insert fails is now retained. The worker retries it every 20 seconds and refuses new messages until it is written, so they stay in the queue (failed list on Redis, redelivery with backoff on JetStream) instead of being acknowledged into memory. The accumulator keeps only the batches it could not write, so a retry never writes a batch twice. This matches the cloud worker's handling in appwrite-labs/cloud#6554. The outage E2E breaks every usage insert on its stack, so it runs in a lane of its own. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟡 1 medium 💬 Inline comments (1)
🧹 Nitpicks (1) — 🟢 1 low
This PR makes the stats-usage worker resilient to a ClickHouse outage: a failed flush retains the batch and schedules a delayed retry instead of losing metrics, with a shutdown path that flushes retained data. Supporting changes wire a new
Reviewed commit: ee2454d |
🟢 Tier S · Ready to mergeThis PR retains failed usage-event batches in the StatsUsage worker and retries them while refusing new messages until they can be written. It adds an API regression test for ClickHouse insert failures and isolates that test in a dedicated Usage Outage E2E lane. Latest changes: The newest worker change limits timer-based retries to one attempt and lets later messages trigger retries after the delay.
Note Not approving while a bug finding is open: Retry retained batches even when no new message arrives 📂 Walkthrough · 4
Reviewed the commits since |
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
There was a problem hiding this comment.
Adds ClickHouse-outage retry handling to the stats-usage worker plus a dedicated CI lane and tests; two moderate robustness findings around retry-timer shutdown and test-layer policy.
Key findings
- 🟡 20s retry timer exceeds Docker's 10s stop timeout — StatsUsage.php:196
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> # Conflicts: # tests/e2e/Services/Usage/UsageCustomServerTest.php
A stopping worker waits for its pending retry timer, so a 20s retry outlived Docker's 10s stop timeout and the container was killed with the retained batch still in memory. Retrying every 5s keeps a pending retry inside the same budget the flush window already respects. The worker unit test drove action() inside Swoole coroutines in the shared unit process, which the test boundaries rule out; the usage outage E2E covers the same behaviour through the real worker, so the test and its ClickHouse stand-in go, along with the constant seams they needed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A retry that failed re-armed its own timer, so during an outage that outlived shutdown the worker kept its event loop alive past Docker's stop timeout and was killed. Only the flush that fails now arms a retry timer. Later attempts run on the next message once the retry delay has passed, so a stopping worker waits for at most one retry. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Problem
StatsUsagefolds each message into an open batch and returns, so the queue acknowledges the message. Five seconds later a timer runs the insert. If the insert failed,flush()logged the error and dropped the batch, and every event in it was lost. This happened during a ClickHouse outage, a 5xx, or a rejected insert such as a missing column. appwrite-labs/cloud#6554 fixed this in cloud's worker and listed the server-ce worker as a known gap.Fix
This follows the cloud worker's approach (
src/Appwrite/Platform/Workers/StatsUsage.php):flush()returns false, is kept in$retained.Accumulator::flush()already removes each batch it writes, so a retry writes only what is left and never writes a batch twice.action()refuses each new message by throwing. The message is not acknowledged: on Redis it goes to the failed list, whichqueue-retrycan replay, and on JetStream it is redelivered with backoff. Memory stays bounded because nothing new is accepted.RETRY_DELAY_MS(5 s).action()on the next message, at most once perRETRY_DELAY_MS.Two trade-offs:
Tests
API E2E:
UsageCustomServerTest::testUsageEventsSurviveAFailedInsertCHECK 0 = 1constraint toprojects_usage_events, so ClickHouse rejects every insert (Code 469).PATCH /users/:id/prefsand waits past the batch window, then drops the constraint./v1/usage/eventsto count that PATCH.Because the test breaks inserts for the whole stack, it is in the new
usageOutagegroup. That group runs in its owne2e_groupedlane ("Usage Outage") and is excluded from the regular Usage run.A unit test that drove
action()under Swoole coroutines was dropped in review, because the unit suite must not run worker coroutines (AGENTS.md). The E2E is the regression test.testUsageEventsSurviveAFailedInsert, 8 s outagenetwork.requests= 0. The worker loggedCode: 469 ... VIOLATED_CONSTRAINTand dropped the batch.The before run used main's worker unchanged, on a local stack built from
main.🤖 Generated with Claude Code