Skip to content

Make fair task gc conditional on rangeid (#12283) - #12415

Merged
carlydf merged 1 commit into
release/v1.31.xfrom
fair-task-reader-bugfix-v1.31
Oct 7, 2026
Merged

carlydf merged 1 commit into
release/v1.31.xfrom
fair-task-reader-bugfix-v1.31

Conversation

@carlydf

@carlydf carlydf commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

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
  • covered by existing tests
  • added new unit test(s)
  • added new functional test(s)
  • validated in tla+ model (not in repo yet)

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.

Reviewed by Cursor Bugbot for commit 423de27. Bugbot is set up for automated code reviews on this repo. Configure here.

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
carlydf requested review from a team as code owners October 5, 2026 23:59
@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @carlydf's task in 2m 28s —— View job


Reviewed 10 files, 1 finding.

Comment thread service/matching/db.go
Comment on lines +712 to +715
// For fair tasks, we do an LWT to avoid issues with lost ownership, therefore we should
// serialize this with other LWTs by holding db.lock to avoid timeouts or conflicts.
db.Lock()
defer db.Unlock()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should consider: with this change, db.Lock() stays held for the whole conditional range delete. That can be a Paxos batch over up to MaxTaskDeleteBatchSize rows, bounded only by ioTimeout. fairTaskReader.advanceAckLevelLocked calls db.updateFairAckLevel while it holds tr.lock (fair_task_reader.go:598), and that call needs db.Lock(). So while a GC is running, every task ack on the subqueue waits behind it with tr.lock held. That stalls dispatch and the writer's merge path, which also needs tr.lock. The same thing already happens with CreateFairTasks, but GC adds a new and possibly slower LWT to that path. There's no deadlock, because doGC drops the db lock before it takes tr.lock.

If the main goal is to avoid LWT contention with CreateFairTasks and updateTaskQueueLocked, a separate mutex just for the LWT write path would keep the in-memory ack and backlog updates unblocked. Otherwise, it would help to note this latency trade-off in the comment.

@carlydf
carlydf merged commit efe6003 into release/v1.31.x Oct 7, 2026
67 of 77 checks passed
@carlydf
carlydf deleted the fair-task-reader-bugfix-v1.31 branch October 7, 2026 18:23
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.

2 participants