Skip to content

fix(cluster): Remove per-call srand in valkey-cli (9.1 backport)#3668

Merged
ranshid merged 1 commit into
valkey-io:9.1from
ranshid:backport/9.1-fix-srand-per-call
May 12, 2026
Merged

fix(cluster): Remove per-call srand in valkey-cli (9.1 backport)#3668
ranshid merged 1 commit into
valkey-io:9.1from
ranshid:backport/9.1-fix-srand-per-call

Conversation

@ranshid

@ranshid ranshid commented May 12, 2026

Copy link
Copy Markdown
Member

Backport of #3586 to the 9.1 branch.

Problem

clusterManagerNodePrimaryRandom() called srand(time(NULL)) on every invocation, then immediately rand() % primary_count. When called in a tight loop for uncovered slots, all calls within the same wall-clock second produce the identical seed, causing every uncovered slot to be assigned to the same primary node.

The same issue existed in clusterManagerOptimizeAntiAffinity, pipeMode, and LRUTestMode.

Solution

Remove all per-call srand() invocations and consolidate into a single srand(time(NULL) ^ getpid()) at the start of main(). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries.

(cherry picked from commit 7e2a2f7)

Backport of valkey-io#3586 to 9.1 branch.

clusterManagerNodePrimaryRandom() called srand(time(NULL)) on every
invocation, then immediately rand() % primary_count. When called in a
tight loop for uncovered slots, all calls within the same wall-clock
second produce the identical seed, causing every uncovered slot to be
assigned to the same primary node.

Remove all per-call srand() invocations and consolidate into a single
srand(time(NULL) ^ getpid()) at the start of main(). This allows rand()
to advance its state across calls, distributing uncovered slots randomly
across available primaries.

(cherry picked from commit 7e2a2f7)

Signed-off-by: Ran Shidlansik <ranshid@amazon.com>
@codecov

codecov Bot commented May 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.39%. Comparing base (c9b786f) to head (04cdb32).

Additional details and impacted files
@@            Coverage Diff             @@
##              9.1    #3668      +/-   ##
==========================================
- Coverage   76.40%   76.39%   -0.01%     
==========================================
  Files         161      161              
  Lines       80710    80707       -3     
==========================================
- Hits        61663    61658       -5     
- Misses      19047    19049       +2     
Files with missing lines Coverage Δ
src/valkey-cli.c 60.01% <100.00%> (+0.01%) ⬆️

... and 24 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ranshid ranshid merged commit d251245 into valkey-io:9.1 May 12, 2026
124 of 125 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants