-
Type:
Task
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Networking & Observability
-
N&O Prioritized List
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Summary
MirroringSampler::threadSafeRandom() uses a synchronized_value<PseudoRandom> (mutex-protected) to generate a random number on every operation for mirror sampling decisions. With the default mirroring rate of 1%, 99% of calls determine "don't mirror" — but every call still acquires and releases a mutex.
This multipatch using the below proposed fix showed a .34% improvement in our 100 read workload.
SERVER-75834 identified that PseudoRandom is not thread-safe and noted two valid solutions: a synchronization primitive, or thread-local instances. The original fix chose the mutex approach. The thread-local approach is cheaper and equally correct.
Proposed Fix:
See proposed change in this patch.
Replace the synchronized_value<PseudoRandom> with a thread_local PseudoRandom:
static int threadSafeRandom() {
static thread_local PseudoRandom random{SecureRandom{}.nextInt64()};
return random.nextInt32(defaultRandomMax());
}
Each service executor thread gets its own independently-seeded PRNG instance. No shared state means no data race and no synchronization needed.
Precedent
The codebase already uses this exact pattern in multiple places:
- src/mongo/db/query/query_stats/rate_limiting.cpp:80 — thread_local PseudoRandom for query stats sampling
- src/mongo/db/query/random_utils.cpp:35 — thread_local PseudoRandom for query random utilities
- is related to
-
SERVER-75834 Accessing `MirrorMaestroImpl::_random` should be thread-safe
-
- Closed
-