-
Type:
Task
-
Resolution: Fixed
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
-
Cluster Scalability
-
Fully Compatible
-
N&O 2026-03-30
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Problem
MirroringSampler::threadSafeRandom() uses a StaticImmortal<synchronized_value<PseudoRandom>> — a single global PRNG protected by a stdx::mutex — called on every mirrorable command (find, insert, update, delete) via the request path: initializeCommandHooks → MirrorMaestroImpl::tryMirror → MirroringSampler::threadSafeRandom → synchronized_value<PseudoRandom>::operator-> → stdx::mutex. Profiling shows pthread_mutex_lock consuming 11.90% and threadSafeRandom consuming 20.56% of tryMirror's cumulative time. With the default mirroring rate of 1%, 99% of calls decide "don't mirror" but every call still acquires and releases the mutex, causing cache-line bouncing across all service threads at high thread counts.
Solution
Replace the global StaticImmortal<synchronized_value<PseudoRandom>> with a static thread_local PseudoRandom seeded once per thread from SecureRandom. Each service executor thread gets its own independently-seeded PRNG instance with no shared state, eliminating both the data race and the synchronization overhead in a 3-line change. This is the canonical fix noted in SERVER-75834 as an alternative to the mutex approach, and matches the existing pattern already used in rate_limiting.cpp and random_utils.cpp in the same codebase.
- is related to
-
SERVER-75834 Accessing `MirrorMaestroImpl::_random` should be thread-safe
-
- Closed
-