Skip to content

fix(batch): shuffle record drain page per replica to stop admission contention - #24500

Merged
Git-on-my-level merged 1 commit into
mainfrom
lead/drain-shuffle
Nov 3, 2026
Merged

Git-on-my-level merged 1 commit into
mainfrom
lead/drain-shuffle

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Requested by David

Outage fix, follow-up to #24494. Every backend-integration replica selects the same oldest record_pending page, and its workers all start on the same rows. Admissions collide and fail with FirestoreContentionExhausted: 734 retries in 4 minutes at 12:10Z, and the backlog did not shrink (about 5,070 rows).

This shuffles each pass's page per replica, so replicas work on different rows. A row a replica reaches after another replica admitted it is skipped by the existing state != record_pending check.

David asked to ship this without waiting for CI (break glass, 12:17Z).

🤖 Generated with Claude Code

View guided diff Turn on auto-fix

…ontention

Every backend-integration replica selects the same oldest record_pending page
and its workers start on the same rows, so admissions collide and fail with
FirestoreContentionExhausted (734 retries in 4 min, 0 drain progress, 12:10Z).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-11-03T12:18:49.781707Z
We couldn’t complete this review. Comment @codex review to try again.
4a04512 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

1 issue found across 1 file

Confidence score: 3/5

  • live.py can make test_drain_bounds_concurrency_and_passes_slow_partial hang when row '0' is shuffled outside the first four rows. Make the test’s row order deterministic or ensure the partial coordinator is included early.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="backend/utils/batch_pipeline/live.py">

<violation number="1" location="backend/utils/batch_pipeline/live.py:435">
P2: `random.shuffle` makes `test_drain_bounds_concurrency_and_passes_slow_partial` nondeterministic. That test needs the partial coordinator (row `'0'`) to be pulled among the first 4 rows; otherwise it deadlocks and hangs in `await task`. Patch the shuffle to a no-op in that test, or inject the order, and keep the randomness out of the deterministic concurrency test.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

segments = await run_blocking(db_executor, store.due, 'record_pending', time.time(), batch_size)
# Every replica selects the same oldest page; a per-replica order keeps
# replicas from admitting the same rows at once and exhausting contention.
random.shuffle(segments)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: random.shuffle makes test_drain_bounds_concurrency_and_passes_slow_partial nondeterministic. That test needs the partial coordinator (row '0') to be pulled among the first 4 rows; otherwise it deadlocks and hangs in await task. Patch the shuffle to a no-op in that test, or inject the order, and keep the randomness out of the deterministic concurrency test.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At backend/utils/batch_pipeline/live.py, line 435:

<comment>`random.shuffle` makes `test_drain_bounds_concurrency_and_passes_slow_partial` nondeterministic. That test needs the partial coordinator (row `'0'`) to be pulled among the first 4 rows; otherwise it deadlocks and hangs in `await task`. Patch the shuffle to a no-op in that test, or inject the order, and keep the randomness out of the deterministic concurrency test.</comment>

<file context>
@@ -429,6 +430,9 @@ async def worker():
         segments = await run_blocking(db_executor, store.due, 'record_pending', time.time(), batch_size)
+        # Every replica selects the same oldest page; a per-replica order keeps
+        # replicas from admitting the same rows at once and exhausting contention.
+        random.shuffle(segments)
         remaining = iter(segments)
         # Join every worker even on failure; a retry must not overlap unfinished
</file context>

@Git-on-my-level
Git-on-my-level merged commit a89b702 into main Nov 3, 2026
60 of 62 checks passed
@Git-on-my-level
Git-on-my-level deleted the lead/drain-shuffle branch November 3, 2026 12:27
Git-on-my-level added a commit that referenced this pull request Nov 3, 2026
Every admission rewrites the owner's scheduler row (owners/<key>), so local
drain workers admitting the same owner's rows exhaust each other's contention
retries. After #24500, FirestoreContentionExhausted was still about 100 a
minute and the backlog fell only about 17 rows a minute (13:00Z). Skip a row
whose owner is already being admitted on this replica; it stays due for the
next pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Git-on-my-level added a commit that referenced this pull request Nov 3, 2026
…4506)

Every admission rewrites the owner's scheduler row (owners/<key>), so local
drain workers admitting the same owner's rows exhaust each other's contention
retries. After #24500, FirestoreContentionExhausted was still about 100 a
minute and the backlog fell only about 17 rows a minute (13:00Z). Skip a row
whose owner is already being admitted on this replica; it stays due for the
next pass.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant