fix(bthread): refactor sharded priority queue with lock-free MPSC inb…#3270
Open
yannan-wyn wants to merge 3 commits intoapache:masterfrom
Open
fix(bthread): refactor sharded priority queue with lock-free MPSC inb…#3270yannan-wyn wants to merge 3 commits intoapache:masterfrom
yannan-wyn wants to merge 3 commits intoapache:masterfrom
Conversation
…ound Replace single WorkStealingQueue priority path with per-tag sharded PriorityShard (MPSCQueue inbound + WSQ + atomic owner lifecycle). Steal_task includes salvage logic for ownerless shards to prevent task starvation. Gated by FLAGS_enable_bthread_priority_queue.
There was a problem hiding this comment.
Pull request overview
Refactors the bthread “global priority” scheduling path to avoid unsafe multi-producer use of WorkStealingQueue by introducing a sharded design with an inbound lock-free queue per shard, plus adds new unit tests and a microbenchmark.
Changes:
- Introduces
PriorityShardper tag and routes priority submissions via shard inbound queues with owner bind/unbind lifecycle. - Updates scheduling/stealing logic to prefer owner shards, steal across shards, and provide fallback behavior during teardown.
- Adds unit tests for correctness/concurrency/owner changes and a microbenchmark suite for the new primitives.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| test/bthread_priority_queue_unittest.cpp | Adds correctness + concurrent-producer tests for priority queue behavior. |
| test/bthread_priority_queue_owner_unittest.cpp | Adds tests around dynamic worker/owner binding behavior under load. |
| test/bthread_priority_queue_benchmark.cpp | Adds microbenchmarks for inbound enqueue/dequeue, flush, steal, and baseline comparisons. |
| src/bthread/task_group.h | Adds _priority_shard_index for O(1) lookup of a TaskGroup’s owned shard. |
| src/bthread/task_control.h | Adds PriorityShard definition and TaskControl helper APIs for shard management. |
| src/bthread/task_control.cpp | Implements sharded priority queue logic, owner lifecycle, flushing, stealing, and fallback enqueue behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Author
|
@wwbmmm |
Contributor
|
LGTM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Problem Summary:
The original priority queue implementation misuses
WorkStealingQueue— multiple producers callpush()concurrently, butpush()is designed for single-owner use only. This leads to potential data races under contention.See also: #2819, #3055, #3078, #3096
What is changed and the side effects?
Changed:
WorkStealingQueueper tag with a sharded-mode design (which is PriorityShard), each shard containing:butil::MPSCQueueinbound (lock-free) for external producersWorkStealingQueuefor owner flush/pop and non-owner stealBTHREAD_GLOBAL_PRIORITYand re-enqueue to normal_remote_rqduring shard teardown_priority_shard_indexfield toTaskGroupfor O(1) owner shard lookupSide effects:
Performance effects:
_remote_rq~1050ns (3-4x faster)Breaking backward compatibility:
FLAGS_enable_bthread_priority_queue(default false).