Skip to content

Scheduler: dequeue same-priority queries in arrival order - #7958

Open
mayank-gupta16 wants to merge 1 commit into
cortexproject:masterfrom
mayank-gupta16:fix/7950-priority-queue-fifo
Open

mayank-gupta16 wants to merge 1 commit into
cortexproject:masterfrom
mayank-gupta16:fix/7950-priority-queue-fifo

Conversation

@mayank-gupta16

Copy link
Copy Markdown

What this PR does:

util.PriorityQueue orders its heap only by Priority(), with no tie-breaker, so queries that share a priority level are dequeued in roughly reverse arrival order. With query priority enabled (and with the query-scheduler, which enqueues everything at the same priority, see #7949) older queries can starve under steady load, as described in #7950.

This records an enqueue sequence number with each op and uses it to break ties in Less, so ops with equal priority are dequeued FIFO. Ordering between different priorities is unchanged, and the public API of PriorityQueue is unchanged.

Tests: added three cases to pkg/util/priority_queue_test.go: six same-priority items come out in arrival order, mixed priorities keep FIFO within each level, and the "serve one, receive one" starvation scenario from the issue. Two of them fail on master (1 6 5 4 3 2 ordering) and all pass with the fix. go test ./pkg/util/... ./pkg/scheduler/... ./pkg/frontend/... passes except pkg/util/validation (TestValidateNativeHistogram panics), which fails the same way on master.

This change was prepared with AI assistance (Claude Code); I reviewed and tested it before submitting.

Which issue(s) this PR fixes:
Fixes #7950

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags

🤖 Generated with Claude Code

@mayank-gupta16
mayank-gupta16 requested a review from a team as a code owner November 20, 2026 20:22
util.PriorityQueue was a plain container/heap ordered only by priority,
with no tie-breaker. Queries sharing a priority came out in roughly
reverse arrival order, so with query priority enabled (and with the
query-scheduler, which queues everything at the same priority) older
queries could starve under steady load.

Record an enqueue sequence number with each op and use it to break ties,
so ops with equal priority are dequeued FIFO.

Fixes cortexproject#7950

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Mayank Gupta <mayankg1609@gmail.com>
@mayank-gupta16
mayank-gupta16 force-pushed the fix/7950-priority-queue-fifo branch from ac169ec to b0ce0e4 Compare November 27, 2026 10:26

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query priority: queries with the same priority are not dequeued in arrival order, and can starve under load

1 participant