-
Notifications
You must be signed in to change notification settings - Fork 709
cloud_storage_clients: rewrite pick_two_random_shards for O(1) sampling #29099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nvartolomei
wants to merge
1
commit into
redpanda-data:dev
Choose a base branch
from
nvartolomei:nv/client-pool-fast-pick-random
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+41
−20
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,47 @@ namespace { | |
| constexpr auto self_configure_attempts = 3; | ||
| constexpr auto self_configure_backoff = 1s; | ||
| constexpr auto pool_ready_timeout = 15s; | ||
|
|
||
| /// Picks two distinct random shards, excluding self. Used for "power of two | ||
| /// choices" load balancing. | ||
| /// | ||
| /// Uses rejection-free sampling: instead of picking from [0,n) and retrying on | ||
| /// excluded values, we pick from a smaller range [0,n-k) and map to valid | ||
| /// values by skipping over excluded ones. This guarantees O(1) with exactly one | ||
| /// random draw per selection. | ||
| [[nodiscard]] std::pair<ss::shard_id, ss::shard_id> pick_two_random_shards() { | ||
| using dist_t = std::uniform_int_distribution<ss::shard_id>; | ||
|
|
||
| static thread_local std::mt19937 gen{std::random_device{}()}; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use our random generator library? Travis updated it a few months ago to have different seeding policies to improve reproducibility, along with other improvements. |
||
|
|
||
| const ss::shard_id n = ss::smp::count; | ||
| const ss::shard_id self = ss::this_shard_id(); | ||
|
|
||
| vassert(n > 1, "At least two shards are required"); | ||
|
|
||
| if (n == 2) { | ||
| ss::shard_id other = (self == 0) ? 1 : 0; | ||
| return {other, other}; | ||
| } | ||
|
|
||
| // Pick cpu_a from [0, n), excluding self | ||
| dist_t dist(0, n - 2); | ||
| auto r = dist(gen); | ||
| const ss::shard_id cpu_a = r < self ? r : r + 1; | ||
|
|
||
| // Pick cpu_b from [0, n), excluding self and cpu_a | ||
| dist.param(dist_t::param_type{0, n - 3}); | ||
| auto cpu_b = dist(gen); | ||
| const auto [lo, hi] = std::minmax(self, cpu_a); | ||
| if (cpu_b >= lo) { | ||
| ++cpu_b; | ||
| } | ||
| if (cpu_b >= hi) { | ||
| ++cpu_b; | ||
| } | ||
|
|
||
| return {cpu_a, cpu_b}; | ||
| } | ||
| } // namespace | ||
|
|
||
| namespace cloud_storage_clients { | ||
|
|
@@ -247,26 +288,6 @@ void client_pool::shutdown_connections() { | |
|
|
||
| bool client_pool::shutdown_initiated() { return _as.abort_requested(); } | ||
|
|
||
| std::tuple<unsigned int, unsigned int> pick_two_random_shards() { | ||
| static thread_local std::vector<unsigned> shards = [] { | ||
| std::vector<unsigned> res; | ||
| for (auto i = 0UL; i < ss::smp::count; i++) { | ||
| if (i != ss::this_shard_id()) { | ||
| res.push_back(i); | ||
| } | ||
| } | ||
| return res; | ||
| }(); | ||
| vassert(ss::smp::count > 1, "At least two shards are required"); | ||
| if (shards.size() == 1) { | ||
| return std::tie(shards.at(0), shards.at(0)); | ||
| } | ||
| std::random_device rd; | ||
| std::mt19937 gen(rd()); | ||
| std::shuffle(shards.begin(), shards.end(), gen); | ||
| return std::tie(shards.at(0), shards.at(1)); | ||
| } | ||
|
|
||
| /// \brief Acquire http client from the pool. | ||
| /// | ||
| /// as: An abort source which must outlive the lease, that will | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gemini
Random Number Generation: std::random_device is often implemented as a read from /dev/urandom (syscall), which is too slow for a "Zero-Cost" abstraction, and std::mt19937 is large (2.5KB state).
Fix: Use a lightweight PRNG (like PCG or Xorshift) or simply a thread_local std::default_random_engine seeded once (not on every jitter call).