Merge pull request #8973 from sfc-gh-ajbeamon/fix-invalid-seed

Allow a seed value of zero in DeterministicRandom
This commit is contained in:
A.J. Beamon 2022-12-05 11:07:32 -08:00 committed by GitHub
commit 8ef9bab9a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 10 additions and 29 deletions

View File

@ -422,8 +422,7 @@ ACTOR Future<bool> checkDataConsistency(Database cx,
for (int k = 0; k < ranges.size(); k++)
shardOrder.push_back(k);
if (shuffleShards) {
uint32_t seed = sharedRandomNumber + repetitions;
DeterministicRandom sharedRandom(seed == 0 ? 1 : seed);
DeterministicRandom sharedRandom(sharedRandomNumber + repetitions);
sharedRandom.randomShuffle(shardOrder);
}

View File

@ -33,9 +33,7 @@ uint64_t DeterministicRandom::gen64() {
}
DeterministicRandom::DeterministicRandom(uint32_t seed, bool useRandLog)
: random((unsigned long)seed), next((uint64_t(random()) << 32) ^ random()), useRandLog(useRandLog) {
UNSTOPPABLE_ASSERT(seed != 0); // docs for mersenne twister say x0>0
};
: random((unsigned long)seed), next((uint64_t(random()) << 32) ^ random()), useRandLog(useRandLog) {}
double DeterministicRandom::random01() {
double d = gen64() / double(uint64_t(-1));

View File

@ -71,7 +71,6 @@ void FlowKnobs::initialize(Randomize randomize, IsSimulated isSimulated) {
init( SATURATION_PROFILING_MAX_LOG_INTERVAL, 5.0 );
init( SATURATION_PROFILING_LOG_BACKOFF, 2.0 );
init( RANDOMSEED_RETRY_LIMIT, 4 );
init( FAST_ALLOC_LOGGING_BYTES, 10e6 );
init( FAST_ALLOC_ALLOW_GUARD_PAGES, false );
init( HUGE_ARENA_LOGGING_BYTES, 100e6 );

View File

@ -2246,35 +2246,21 @@ namespace platform {
int getRandomSeed() {
INJECT_FAULT(platform_error, "getRandomSeed"); // getting a random seed failed
int randomSeed;
int retryCount = 0;
#ifdef _WIN32
do {
retryCount++;
if (rand_s((unsigned int*)&randomSeed) != 0) {
TraceEvent(SevError, "WindowsRandomSeedError").log();
throw platform_error();
}
} while (randomSeed == 0 &&
retryCount <
FLOW_KNOBS->RANDOMSEED_RETRY_LIMIT); // randomSeed cannot be 0 since we use mersenne twister in
// DeterministicRandom. Get a new one if randomSeed is 0.
if (rand_s((unsigned int*)&randomSeed) != 0) {
TraceEvent(SevError, "WindowsRandomSeedError").log();
throw platform_error();
}
#else
int devRandom = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
do {
retryCount++;
if (read(devRandom, &randomSeed, sizeof(randomSeed)) != sizeof(randomSeed)) {
TraceEvent(SevError, "OpenURandom").GetLastError();
throw platform_error();
}
} while (randomSeed == 0 && retryCount < FLOW_KNOBS->RANDOMSEED_RETRY_LIMIT);
if (read(devRandom, &randomSeed, sizeof(randomSeed)) != sizeof(randomSeed)) {
TraceEvent(SevError, "OpenURandom").GetLastError();
throw platform_error();
}
close(devRandom);
#endif
if (randomSeed == 0) {
TraceEvent(SevError, "RandomSeedZeroError").log();
throw platform_error();
}
return randomSeed;
}
} // namespace platform

View File

@ -129,7 +129,6 @@ public:
int DISABLE_ASSERTS;
double QUEUE_MODEL_SMOOTHING_AMOUNT;
int RANDOMSEED_RETRY_LIMIT;
double FAST_ALLOC_LOGGING_BYTES;
bool FAST_ALLOC_ALLOW_GUARD_PAGES;
double HUGE_ARENA_LOGGING_BYTES;