Address review feedback on server selection (PR #5508)

- Fix truncation: use uint64_t modulo instead of unsigned int cast
  when computing target from total_weight (prevents skewed selection
  when total_weight > UINT_MAX)
- Remove unused #include <cstdlib>
- Add docstring note about max_latency_us=0 semantics: in production
  this means "use thread default", this extraction treats it as "no
  limit" — callers should resolve defaults before populating the struct
- Remove trailing "fix" comment from test
pull/5508/head
René Cannaò 1 month ago
parent 7697d4f43e
commit 7e49c41975

@ -55,6 +55,11 @@ struct ServerCandidate {
* - current_latency_us <= max_latency_us (or max_latency_us == 0)
* - current_repl_lag <= max_repl_lag (or max_repl_lag == 0)
*
* @note In production, max_latency_us == 0 on a per-server basis means
* "use the thread default max latency." This extraction treats 0
* as "no limit" for simplicity. Callers should resolve defaults
* before populating the ServerCandidate.
*
* @return true if the candidate is eligible.
*/
bool is_candidate_eligible(const ServerCandidate &candidate);

@ -7,7 +7,6 @@
*/
#include "ServerSelection.h"
#include <cstdlib>
bool is_candidate_eligible(const ServerCandidate &c) {
if (c.status != SERVER_ONLINE) {
@ -51,7 +50,8 @@ int select_server_from_candidates(
// LCG: next = (a * seed + c) mod m (Numerical Recipes parameters)
unsigned int rng_state = random_seed;
rng_state = rng_state * 1664525u + 1013904223u;
int64_t target = (int64_t)(rng_state % (unsigned int)total_weight) + 1;
// Use 64-bit modulo to avoid truncation when total_weight > UINT_MAX
int64_t target = (int64_t)(rng_state % (uint64_t)total_weight) + 1;
// Second pass: weighted selection
int64_t cumulative = 0;

@ -216,7 +216,7 @@ int main() {
test_weighted_distribution(); // 1
test_determinism(); // 1
test_mixed_eligibility(); // 1
// Total: 1+12+1+1+1+1+1+1+1+1 = 21... fix
// Total: 1+12+1+1+1+1+1+1+1+1 = 21
test_cleanup_minimal();
return exit_status();

Loading…
Cancel
Save