Handle a possible overflow (#13046)

Summary:
Stress test detects this variable could potentially overflow, so added some runtime handling to avoid it.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/13046

Test Plan: Existing tests

Reviewed By: hx235

Differential Revision: D63911396

Pulled By: jowlyzhang

fbshipit-source-id: 7c9abcd74ac9937b211c0ea4bb683677390837c5
This commit is contained in:
Yu Zhang 2024-10-04 16:48:12 -07:00 committed by Facebook GitHub Bot
parent bceb2dfe6a
commit 263fa15b44
1 changed files with 10 additions and 3 deletions

View File

@ -1968,9 +1968,16 @@ uint64_t VersionStorageInfo::GetEstimatedActiveKeys() const {
}
if (current_num_samples_ < file_count) {
// casting to avoid overflowing
return static_cast<uint64_t>(
(est * static_cast<double>(file_count) / current_num_samples_));
assert(current_num_samples_ != 0);
assert(est != 0);
double multiplier = static_cast<double>(file_count) / current_num_samples_;
double maximum_multiplier =
static_cast<double>(std::numeric_limits<uint64_t>::max()) / est;
// If it can overflow, we return the maximum unsigned long.
if (multiplier >= maximum_multiplier) {
return std::numeric_limits<uint64_t>::max();
}
return static_cast<uint64_t>(est * multiplier);
} else {
return est;
}