Skip to content

Fix priority backlog acknowledgement after expired tasks - #12595

Merged
cbsandeep10 merged 5 commits into
mainfrom
codex/skip-expired-task-flakiness
Oct 20, 2026
Merged

cbsandeep10 merged 5 commits into
mainfrom
codex/skip-expired-task-flakiness

Conversation

@cbsandeep10

@cbsandeep10 cbsandeep10 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

What changed?

Advance the priority backlog reader's acknowledgement level across scanned ranges when no tasks remain outstanding, including when expired tasks have moved the read level ahead of the acknowledgement level. Preserve the stale-read guard and protection for unfinished tasks.

Add deterministic regressions for expired tails with and without trailing ID gaps. Extend the existing drain test to verify that scanning a gap does not advance acknowledgement or reset the backlog count while tasks remain unfinished. Enable the expired-only priority backlog test.

Why?

Fixes #12472.

If valid tasks finish before the reader scans an expired tail, or if all tasks have expired, the priority reader can leave acknowledgement stuck until another task is written and completed. Expired tasks advance the read level without entering the outstanding-task map, so requiring equal acknowledgement and read levels prevents further acknowledgement.

This affects production queue behavior: the persisted acknowledgement level stays stale, expired tasks cannot be garbage-collected, and the approximate backlog count is not reset. Advancing acknowledgement when no tasks remain outstanding lets the queue move past expired tasks without waiting for a new task.

How did you test it?

  • covered by existing tests
  • added new unit test(s)
  • Passed 100 race-enabled local repetitions of the priority backlog drain, gap-handling, expired-task, and out-of-order completion tests using disable_grpc_modules,test_dep build tags.
  • Passed both complete backlog manager suites (priority and fair) with race detection.
  • Earlier CI stress run: 100 repetitions with race detection for the priority backlog regression tests.
  • Passed make lint-code-fast with zero issues.

Full-package race validation also encountered a pre-existing backlog-count assertion in TestMatchingEngine_Fair_Suite/TestMultipleWorkersLesserNumberOfPollersThanTasksDBErrors; it reproduced 15/20 times on unchanged base commit b88a7ebdd.

@cbsandeep10 cbsandeep10 changed the title Advance priority backlog acknowledgement past expired tasks Fix backlog manager test flakiness Oct 16, 2026
@cbsandeep10
cbsandeep10 force-pushed the codex/skip-expired-task-flakiness branch from b0f5594 to 36d12bd Compare October 16, 2026 19:29
@cbsandeep10
cbsandeep10 marked this pull request as ready for review October 16, 2026 19:54
@cbsandeep10
cbsandeep10 requested a review from a team as a code owner October 16, 2026 19:54
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @cbsandeep10's task in 47s —— View job


Reviewed 2 files, 2 findings.

Comment thread service/matching/pri_task_reader.go Outdated
// but where the rangeid has moved higher, to prevent excessive reads on the next load.
if tr.outstandingTasks.Empty() {
// Expired tasks advance readLevel without entering outstandingTasks. Once no tasks
// remain outstanding, they and the empty range can be acknowledged.

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.

nit (textual): This change looks correct to me. It's safe because getTasksPump is the only thing that moves readLevel after init, and the stale-read guard above it still runs first. But the new comment drops the explanation of why acking [readLevel, newReadLevel] is safe. That reasoning was the most useful part of the old comment. Consider keeping it, e.g.:

		// This is called after reading the range (tr.readLevel, newReadLevel] and finding no tasks
		// (the stale-read check above guards against signalNewTasks having moved readLevel). Every
		// task at or below tr.readLevel was either loaded into outstandingTasks or dropped as
		// expired/already-acked, so if nothing is outstanding, everything up to newReadLevel is acked.
		// This lets us advance the ack level on an idle queue whose range ID has moved higher.

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.

Updated the comment.

Comment on lines +510 to +513
t := s.T()
s := &BacklogManagerTestSuite{}
s.SetT(t)
s.SetupTest()

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.

nit (testing): Building a new BacklogManagerTestSuite inside s.Run and shadowing s doesn't match how the rest of this file is written. It also throws away the outer SetupTest state that was already built for this test, and it silently resets fields like fairness. That's harmless today because of the skip, but easy to break later. Testify's s.Run already points s.T() at the subtest, so calling s.SetupTest() directly gives the same isolation with less code:

Suggested change
t := s.T()
s := &BacklogManagerTestSuite{}
s.SetT(t)
s.SetupTest()
s.SetupTest()

Another option is to split this into two top-level tests (..._ExpiredTail and ..._ExpiredTailWithGap).

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.

Updated the test.

@cbsandeep10 cbsandeep10 changed the title Fix backlog manager test flakiness Fix priority backlog acknowledgement after expired tasks Oct 19, 2026
@cbsandeep10
cbsandeep10 force-pushed the codex/skip-expired-task-flakiness branch from f4bd6a9 to 60d2a59 Compare October 19, 2026 18:57
{name: "no_gap", maxReadLevelOffset: 2},
{name: "with_gap", maxReadLevelOffset: 100},
} {
s.Run(tc.name, func() {

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.

slight preference for the style where these are two suite-level tests and they pass a parameter into a shared helper. but it's fine if you want to leave it

@cbsandeep10
cbsandeep10 merged commit 4fa3ed4 into main Oct 20, 2026
55 checks passed
@cbsandeep10
cbsandeep10 deleted the codex/skip-expired-task-flakiness branch October 20, 2026 16:50
yycptt pushed a commit that referenced this pull request Oct 31, 2026
Advance the priority backlog reader's acknowledgement level across
scanned ranges when no tasks remain outstanding, including when expired
tasks have moved the read level ahead of the acknowledgement level.
Preserve the stale-read guard and protection for unfinished tasks.

Add deterministic regressions for expired tails with and without
trailing ID gaps. Extend the existing drain test to verify that scanning
a gap does not advance acknowledgement or reset the backlog count while
tasks remain unfinished. Enable the expired-only priority backlog test.

Fixes #12472.

If valid tasks finish before the reader scans an expired tail, or if all
tasks have expired, the priority reader can leave acknowledgement stuck
until another task is written and completed. Expired tasks advance the
read level without entering the outstanding-task map, so requiring equal
acknowledgement and read levels prevents further acknowledgement.

This affects production queue behavior: the persisted acknowledgement
level stays stale, expired tasks cannot be garbage-collected, and the
approximate backlog count is not reset. Advancing acknowledgement when
no tasks remain outstanding lets the queue move past expired tasks
without waiting for a new task.

- [x] covered by existing tests
- [x] added new unit test(s)
- Passed 100 race-enabled local repetitions of the priority backlog
drain, gap-handling, expired-task, and out-of-order completion tests
using `disable_grpc_modules,test_dep` build tags.
- Passed both complete backlog manager suites (priority and fair) with
race detection.
- Earlier CI stress run: [100 repetitions with race detection for the
priority backlog regression
tests](https://github.com/temporalio/temporal/actions/runs/38959302448).
- Passed `make lint-code-fast` with zero issues.

Full-package race validation also encountered a pre-existing
backlog-count assertion in
`TestMatchingEngine_Fair_Suite/TestMultipleWorkersLesserNumberOfPollersThanTasksDBErrors`;
it reproduced 15/20 times on unchanged base commit `b88a7ebdd`.

(cherry picked from commit 4fa3ed4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

priTaskReader ack level stalls when trailing tasks are expired (causes flaky TestSkipExpiredTasks_ValidThenExpired)

3 participants