Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 41 additions & 20 deletions src/v/cloud_storage_clients/client_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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{}()};
Copy link
Contributor Author

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

Copy link
Member

Choose a reason for hiding this comment

The 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 {
Expand Down Expand Up @@ -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
Expand Down