fix(platform-connectors): keep store writes in order and drain the queues at shutdown #2322
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #2322"
general
weight: 0.35
KaivalyaMDabhadkar
Summary
Fixes #2321.
The node-local store connector requeued a batch whose insert failed and took the next one while the failed batch waited out its backoff, so one failed insert could store a recovery before the fault it follows, or a new fault before the recovery it follows. The loop now holds the batch and retries it in place before it takes the next one, with the same retry count and the backoff the ring buffer was configured with. That's what #1747 did for the Kubernetes connector. Each retry still counts in the queue's retries metric, and the queue's work_duration_seconds metric now includes the backoff. A document the datastore can never store as sent, a duplicate under another unique index or one it rejects for its content (store-client now marks PostgreSQL's data exceptions, not null and check violations, and MongoDB's validation failure as permanent), is dropped at once at Error instead of holding the queue through the backoff, the way #1747 skips permanent failures, and the events after it in the batch are still stored. The deployment platform connector refuses such a batch for good.
Shutdown drained the queues with ShutDownWithDrain and then closed the datastore client. That drain only waits until no batch is in flight, which also happens between two batches, and nothing waited for the loops, so a backlog at SIGTERM was partly cut off even with a healthy MongoDB, and a batch waiting for its retry was never stored. Shutdown now stops the queues taking batches and waits for the loops, which return once their queue is empty, before it closes the sink and the datastore. That's how ADR-006 describes queue shutdown.
The wait is bounded at 10 s, and a loop still running then is stopped and gets 5 s more to return. Counting from SIGTERM, requests in flight take up to 10 s, so the queued events are safe by 20 s, the stopped loops take up to 5 s, and the disconnect, the metrics server and the trace exporter take up to 5 s each after that, 40 s in all. The DaemonSet ran with the default 30 s grace period, so the chart now gives its pod 45 s. A loop still running at 10 s, behind a datastore that stays down for example, is stopped through its context and named in a Warn line, and the store connector logs at Error the batch it was retrying and how many were still queued. A loop that still hasn't returned 5 s later, a PostgreSQL insert whose host stopped answering for example, is left behind so the connections still close, and it's named at Error with the batches it still holds, the one it's on included. The queues don't use ShutDownWithDrain anymore, since it would wait with no bound for a batch the store loop holds. The Kubernetes loop drains its queue like the others now. stopCh no longer closes before the drain, only once the loops are done or at the drain timeout, where it still cuts a write that keeps retrying. The gRPC sink's loop now gets the same bounded wait before the sink is closed. The sink also retries a failed send in place now, like the store, so a send that fails during the drain isn't handed back to a queue that no longer takes it. While it retries, only the sink's queue waits, up to 3.5 s per failing batch with the chart defaults and up to about 43.5 s when the target doesn't answer. A batch the target refuses with InvalidArgument, PermissionDenied or Unimplemented isn't retried, and if the drain times out the sink logs at Error how many batches it leaves queued, like the store. With a sink target that never answers, main's Shutdown took 19.7 s and logged nothing, and with this change it takes 10 s and names the sink's loop. The deployment role has no queues, so nothing changes there.
Type of Change
Component(s) Affected
Testing
The new store tests use the module's mock datastore client, so CI runs them without a database. One fails an insert ahead of a queued recovery and checks the fault is retried before the recovery is stored and the retry is counted, one shuts the queue down while a batch waits for its retry and checks it's still stored, and two cancel the loop in the middle of a retry and check it returns right away and logs what it drops at Error. Another checks a batch that duplicates a document under another unique index, or one the datastore rejects for its content, isn't retried and doesn't hold the batches behind it, and the store-client tests pin which PostgreSQL and MongoDB answers count as permanent. In bootstrap one drives the real Connectors.Shutdown while the loop is between two batches and checks it doesn't return until the queue is empty, one sends the gRPC sink's queued batches to an in-process server and checks they all arrive before the sink is closed, and one checks that a loop still busy at the drain timeout gets stopped and named. Without the fixes those fail. Another pins the new order for the Kubernetes loop, where stopCh stays open while the queues drain and closes at the drain timeout, and two run the real Kubernetes connector over a fake clientset through Shutdown, one checking every queued batch is written before it returns and one checking a write that keeps failing is retried during the drain and cut at the timeout. For the sink, tests check a send that fails after the queue is shut down is sent again before the batch behind it, a failed fault goes out before the recovery queued behind it, a canceled context ends the backoff at once, a refused status isn't retried and the batch behind it goes out at once, and the batches left queued at the drain timeout are counted at Error. One more runs a loop that ignores its stop next to one that returns and checks Shutdown comes back after the two timeouts and names only the stuck one. A test commit pins the retry count and the 0.5, 1 and 2 s backoff on synctest's fake clock, so the retry in place keeps the schedule the requeue had, and a helm unittest pins the DaemonSet's 45 s. Every commit passes the module's lint-test on its own.
I also ran the store path against MongoDB 8.0.29 in Docker, a single member replica set with test commands on, with a harness I didn't commit since it uses the driver directly and needs a live database. With failCommand failing one insert, main stored the second of two batches first in all 20 runs every time I ran the harness, and this branch never did. Through the real Connectors.Shutdown with 200 batches queued and a healthy MongoDB, three runs on main lost batches in 1, 4 and 6 of 10 shutdowns, 161, 208 and 500 of 2,000, and this branch lost none in any run, with Shutdown taking under 0.6 s. With SIGTERM landing while the first batch waited for its retry, main lost that batch in all 10 shutdowns of every run and this branch stored all 50 every time. With MongoDB paused in the middle of a write and 19 more batches queued, main's Shutdown still hadn't returned after 90 s and logged nothing at Error about them, and this branch returned after 10 s and logged the interrupted batch and the 19 still queued at Error.
After the review I ran all three queues through Shutdown together, the store on MongoDB, the Kubernetes connector on an envtest API server at the chart's 5 QPS, and the sink against an in-process server failing one send after the shutdown started, 40 batches queued and 5 shutdowns a run. Before this round the Kubernetes queue lost 170 of its 200 batches and the sink 5 of 200 in every run, and with the round's commits nothing is lost, with Shutdown taking 6 s. With PostgreSQL 16 paused while an insert was in flight and 19 batches queued, Shutdown hadn't returned after 60 s before and returns after 15 s now, naming the store loop and its 20 batches at Error. A check name of 150 characters, longer than its column, and a message with a NUL character each made the good batch queued behind them wait about 3.5 s on PostgreSQL, and a collection validator did the same on MongoDB; with the permanent flag the good batch is stored within 10 ms in all three cases and the rejected one isn't retried.
These are the commands I ran.
On my Mac the module tests need a short TMPDIR for nodelocal's socket test. make lint-test-all can't complete on my Mac because protos-lint needs protoc, so CI on Linux is the full run.
Checklist
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
<!-- coderabbit-ai-generated:v1 -->
Summary by CodeRabbit
Reliability
Failed data batches are retried in order before newer batches are processed. Permanent content errors and duplicate-key failures are dropped without retry; other failures are retried within configured limits.
During shutdown, queued work gets time to finish before connections close. Work that cannot finish within the drain period is stopped and logged.
Kubernetes workloads allow up to 45 seconds for graceful termination.
Documentation
Updated connector guidance to describe retry behavior, shutdown draining, and logging for dropped batches.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->