Repository navigation
Make fair task gc conditional on rangeid - #12283
Merged
Merged
Conversation
Contributor
dnr
force-pushed
the
fair-gc-fix-2
branch
4 times, most recently
from
September 29, 2026 23:31
d328696 to
6fce1d6
Compare
…g on lost tasks Also make the backlog counter validation only count tasks above the ack level, since acked tasks may not have been gc'ed yet.
A stale owner that hasn't noticed it lost ownership can GC past the ack level that a new owner loaded. The new owner assigns passes starting from its loaded ack level, so the stale owner's GC can delete new, undispatched tasks. Add an optional ConditionRangeID to CompleteTasksLessThanRequest (supported by the fair v2 stores) and use it for fair task gc.
ShahabT
reviewed
Sep 30, 2026
| err := query.Exec() | ||
| } | ||
|
|
||
| if request.ConditionRangeID == 0 { |
Contributor
There was a problem hiding this comment.
should we support this case still?
Contributor
Author
There was a problem hiding this comment.
I think it's worth leaving as an option in the persistence layer so we don't have to mess with this later. I can think of a couple situations:
- we could decide to move fifo queues to v2 storage by using the fairness code but forcing pass=0. in that case we don't have this problem so we could do non-conditional delete.
- if we fix the takeover issue and then decide to do gc-below-persisted-ack-level only, then we can switch without touching this code again.
ShahabT
approved these changes
Sep 30, 2026
carlydf
approved these changes
Sep 30, 2026
carlydf
pushed a commit
that referenced
this pull request
Oct 5, 2026
Add ConditionRangeID to CompleteTasksLessThanRequest and make CompleteTasksLessThan conditional on unchanged rangeid, just like CreateTasks. When fair task reader gc's tasks, it's not protected by write fencing like adding tasks. So there's a small race window: a node advances its ack level to C, but it's not persisted yet. Another node takes over and loads the previous ack level, A. It writes a task above A but below C, call it B. The first node now deletes below C, deleting B. Note that this only happens with fair queues because fifo queues always write above the rangeid. - [ ] built - [ ] run locally and tested manually - [x] covered by existing tests - [ ] added new unit test(s) - [ ] added new functional test(s) - [x] validated in tla+ model (not in repo yet)
carlydf
added a commit
that referenced
this pull request
Oct 7, 2026
Add ConditionRangeID to CompleteTasksLessThanRequest and make CompleteTasksLessThan conditional on unchanged rangeid, just like CreateTasks. When fair task reader gc's tasks, it's not protected by write fencing like adding tasks. So there's a small race window: a node advances its ack level to C, but it's not persisted yet. Another node takes over and loads the previous ack level, A. It writes a task above A but below C, call it B. The first node now deletes below C, deleting B. Note that this only happens with fair queues because fifo queues always write above the rangeid. - [ ] built - [ ] run locally and tested manually - [x] covered by existing tests - [ ] added new unit test(s) - [ ] added new functional test(s) - [x] validated in tla+ model (not in repo yet) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **High Risk** > Changes fair-queue task deletion and ownership fencing; a bug could cause task loss or failed GC under partition handoff, though behavior mirrors existing CreateTasks range checks. > > **Overview** > Fair task queue **GC** (`CompleteTasksLessThan`) can now be **conditional on task-queue `range_id`**, closing a race where a stale owner deletes tasks written by a new owner after range steal. > > **Persistence:** `CompleteTasksLessThanRequest` gains optional `ConditionRangeID` (fair/v2 only). Classic v1 Cassandra/SQL reject it. Fair v2 Cassandra batches the delete with a range-id CAS; SQL runs delete inside a transaction after `lockTaskQueue`. Mismatch returns `ConditionFailedError`. A persistence suite test covers match vs wrong range id. > > **Matching:** `CompleteFairTasksLessThan` passes the in-memory `rangeID`, holds `db.lock` to serialize with other LWTs, and does not log store errors on ownership loss. Fair reader GC calls `signalIfFatal` so `ConditionFailedError` unloads the queue. > > **Tests:** Range-stealing engine tests are re-enabled with poll deadlines and relaxed post-GC task counts for the new matcher; backlog assertions use tasks above ack level. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c24b188. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-authored-by: David Reiss <[email protected]>
carlydf
added a commit
that referenced
this pull request
Oct 7, 2026
Add ConditionRangeID to CompleteTasksLessThanRequest and make CompleteTasksLessThan conditional on unchanged rangeid, just like CreateTasks. When fair task reader gc's tasks, it's not protected by write fencing like adding tasks. So there's a small race window: a node advances its ack level to C, but it's not persisted yet. Another node takes over and loads the previous ack level, A. It writes a task above A but below C, call it B. The first node now deletes below C, deleting B. Note that this only happens with fair queues because fifo queues always write above the rangeid. - [ ] built - [ ] run locally and tested manually - [x] covered by existing tests - [ ] added new unit test(s) - [ ] added new functional test(s) - [x] validated in tla+ model (not in repo yet) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Changes fair-queue task deletion and ownership fencing in matching persistence—a correctness-critical path—but scope is limited to v2 GC and adds conditional checks rather than changing dispatch semantics. > > **Overview** > Fixes a fair-queue race where a node could **unconditionally delete persisted tasks below its in-memory ack level** after another matcher had taken ownership and written tasks in that range. > > **Persistence:** `CompleteTasksLessThanRequest` gains optional **`ConditionRangeID`**. Fair (v2) deletes run only when the task queue metadata range ID still matches—Cassandra uses a logged batch CAS (delete + `templateCheckRangeIDQuery`); SQL locks subqueue-zero metadata in a transaction first. Classic v1 and SQL/Cassandra v1 **reject** non-zero `ConditionRangeID`. > > **Matching:** Fair backlog GC calls persistence with the current **`db.rangeID`**, holds **`db.lock`** during the LWT to avoid Cassandra conflicts, treats **`ConditionFailedError`** as lost ownership (not a store error), and **`fair_task_reader`** GC failures now **`signalIfFatal`** so the partition unloads on conflict. > > **Tests:** New fair-task persistence test for conditional delete; range-stealing engine tests re-enabled for the new matcher with poll deadlines and relaxed post-GC task-count assertions. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 423de27. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-authored-by: David Reiss <[email protected]>
carlydf
added a commit
that referenced
this pull request
Oct 7, 2026
Add ConditionRangeID to CompleteTasksLessThanRequest and make CompleteTasksLessThan conditional on unchanged rangeid, just like CreateTasks. When fair task reader gc's tasks, it's not protected by write fencing like adding tasks. So there's a small race window: a node advances its ack level to C, but it's not persisted yet. Another node takes over and loads the previous ack level, A. It writes a task above A but below C, call it B. The first node now deletes below C, deleting B. Note that this only happens with fair queues because fifo queues always write above the rangeid. - [ ] built - [ ] run locally and tested manually - [x] covered by existing tests - [ ] added new unit test(s) - [ ] added new functional test(s) - [x] validated in tla+ model (not in repo yet) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **High Risk** > Changes fair-queue persistence GC and ownership fencing; a bug could delete another owner's tasks or leave stale nodes running without unloading on conflict. > > **Overview** > Fair-queue task GC can no longer delete persisted tasks unless the store still shows the same **range ID** as the current owner, closing a race where a stale node could delete tasks written by a new owner after range steal. > > **Persistence:** `CompleteTasksLessThanRequest` gains optional `ConditionRangeID` (fair/v2 only). Cassandra v2 batches the delete with a CAS range-id check; SQL v2 runs delete inside a transaction after locking the queue row. Classic/v1 paths reject non-zero `ConditionRangeID`. > > **Matching:** `CompleteFairTasksLessThan` passes the in-memory `rangeID`, holds `db.lock` during the conditional LWT, treats `ConditionFailedError` as lost ownership (not a generic store error), and the fair task reader calls `signalIfFatal` on GC failure so the partition unloads. Tests cover conditional delete and tighten range-stealing / backlog assertions. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c658860. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-authored-by: David Reiss <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed?
Add ConditionRangeID to CompleteTasksLessThanRequest and make CompleteTasksLessThan conditional on unchanged rangeid, just like CreateTasks.
Why?
When fair task reader gc's tasks, it's not protected by write fencing like adding tasks. So there's a small race window: a node advances its ack level to C, but it's not persisted yet. Another node takes over and loads the previous ack level, A. It writes a task above A but below C, call it B. The first node now deletes below C, deleting B. Note that this only happens with fair queues because fifo queues always write above the rangeid.
How did you test it?