Repository navigation
fix(cluster): Remove per-call srand in clusterManagerNodePrimaryRandom - #3586
Conversation
This is only called in LRUTestMode, right? Overall, it would make sense to just srand once |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## unstable #3586 +/- ##
============================================
- Coverage 76.66% 76.58% -0.08%
============================================
Files 160 160
Lines 80472 80469 -3
============================================
- Hits 61690 61625 -65
- Misses 18782 18844 +62
🚀 New features to boost your workflow:
|
|
Thanks for the feedback @murphyjacob4 . Addressed the issue and made a single srand call now. this should not have the DCO issue either. |
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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. Signed-off-by: Abhishek Mathur <[email protected]>
Move the PRNG seeding to a single srand(time(NULL) ^ getpid()) call in main(), removing redundant per-function srand() calls from clusterManagerOptimizeAntiAffinity(), pipeMode(), and LRUTestMode(). This ensures consistent entropy (pid mixing) across all code paths and prevents any future tight-loop reseeding bugs like the one fixed in clusterManagerNodePrimaryRandom(). Signed-off-by: Abhishek Mathur <[email protected]>
12f27a9 to
749f882
Compare
rainsupreme
left a comment
There was a problem hiding this comment.
LGTM! Good find on the bug :)
roshkhatri
left a comment
There was a problem hiding this comment.
LGTM,
Nit. can you please update the top comment and PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838), we can change it to this approach consolidated all srand calls into main()
valkey-io#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
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) Signed-off-by: Ran Shidlansik <[email protected]>
#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
#3586) 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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries. --------- Signed-off-by: Abhishek Mathur <[email protected]> Co-authored-by: Abhishek Mathur <[email protected]> Co-authored-by: Ran Shidlansik <[email protected]>
Remove three entries whose changes shipped before 9.2.0, so 9.1 users don't see them advertised as new: - #3533 (dismissHashtable madvise size) was backported to 9.1 and is in the 9.1.0, 9.1.1 and 9.1.2 tags. It was added here by mistake. - #3551 (hashtable iterator invalidate on exhaustion) is likewise in 9.1.0; it just never made the 9.1 notes. - #3586 (valkey-cli --cluster fix slot spreading) is already documented in both the 9.0 and 9.1 release notes and shipped in 9.0.6 and 9.1.0. Fix the #4076 config name: the registered config is priority-preemptive-poll-interval-us, not preemptive-poll-interval-us, so the name as written would be rejected by CONFIG SET. Rewrite the #4005 entry, which described only the internal tagging. The user-facing surface is two new configs, priority-subnets and maxclients-reserved, which together cap normal clients at maxclients minus the reserved count so administrative clients keep guaranteed slots, plus a connected_priority_clients field in INFO clients. Move the sorted set B+ tree note (#4359) back to Performance. Listing it as a behavior change for its OBJECT ENCODING output while #3212 changes hash encoding output from hashtable to listpack in the Performance section was inconsistent; both now sit together. Drop the Redis comparison from the #4629 entry, which no other entry in the 9.0, 9.1 or 9.2 notes makes. Signed-off-by: Sarthak Aggarwal <[email protected]>
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 the srand() call since the PRNG is already seeded at startup (srand(time(NULL) ^ getpid()) at line 9838). This allows rand() to advance its state across calls, distributing uncovered slots randomly across available primaries.