Repository navigation
Make fair task gc conditional on rangeid (#12283) - #12415
Conversation
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)
| // 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() |
There was a problem hiding this comment.
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.
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.
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:
CompleteTasksLessThanRequestgains optionalConditionRangeID. 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-zeroConditionRangeID.Matching: Fair backlog GC calls persistence with the current
db.rangeID, holdsdb.lockduring the LWT to avoid Cassandra conflicts, treatsConditionFailedErroras lost ownership (not a store error), andfair_task_readerGC failures nowsignalIfFatalso 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.