Skip to content

Make fair task gc conditional on rangeid - #12283

Merged
dnr merged 3 commits into
temporalio:mainfrom
dnr:fair-gc-fix-2
Sep 30, 2026
Merged

dnr merged 3 commits into
temporalio:mainfrom
dnr:fair-gc-fix-2

Conversation

@dnr

@dnr dnr commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

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?

  • 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)

@dnr
dnr requested review from a team as code owners September 29, 2026 19:33
@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @dnr's task in 1m 17s —— View job


Reviewed 9 files, 1 finding.

Comment thread service/matching/db.go Resolved
@dnr
dnr force-pushed the fair-gc-fix-2 branch 4 times, most recently from d328696 to 6fce1d6 Compare September 29, 2026 23:31
dnr added 3 commits September 29, 2026 17:32
…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.
err := query.Exec()
}

if request.ConditionRangeID == 0 {

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 we support this case still?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@dnr
dnr merged commit ce3c417 into temporalio:main Sep 30, 2026
91 checks passed
@dnr
dnr deleted the fair-gc-fix-2 branch September 30, 2026 19:05
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 5, 2026
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]>
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.

3 participants