Commit 51669993 authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook GitHub Bot

Fix retryingJitteredExponentialBackoffDur when jitter_param=0

Summary:
`retryingJitteredExponentialBackoffDur()` supports `jitter_param=0` (sanctioned in tests, and widely used to disable jitter based on a runtime parameter), but `std::normal_distribution` does not, so just special-case it.

In practice, libstdc++ already returns 0.

Reviewed By: yfeldblum

Differential Revision: D31722687

fbshipit-source-id: d81b1736379c596c4fc352b0067a4ad5788b0f8c
parent e83309fe
......@@ -200,12 +200,13 @@ Duration retryingJitteredExponentialBackoffDur(
Duration backoff_max,
double jitter_param,
URNG& rng) {
using dist = std::normal_distribution<double>;
// short-circuit to avoid 0 * inf = nan when computing backoff_rep below
if (UNLIKELY(backoff_min == Duration(0))) {
return Duration(0);
}
auto dist = std::normal_distribution<double>(0.0, jitter_param);
auto jitter = std::exp(dist(rng));
auto jitter = jitter_param > 0 ? std::exp(dist{0., jitter_param}(rng)) : 1.;
auto backoff_rep = jitter * backoff_min.count() * std::pow(2, n - 1);
if (UNLIKELY(backoff_rep >= static_cast<double>(backoff_max.count()))) {
return std::max(backoff_min, backoff_max);
......
......@@ -48,9 +48,9 @@ void multiAttemptExpectDurationWithin(
}
sort(durations.begin(), durations.end());
for (auto d : durations) {
EXPECT_GE(d, min_duration);
EXPECT_GE(d.count(), min_duration.count());
}
EXPECT_LE(durations[0], max_duration);
EXPECT_LE(durations[0].count(), max_duration.count());
}
TEST(RetryingTest, has_op_call) {
......@@ -272,17 +272,17 @@ TEST(RetryingTest, policy_capped_jittered_exponential_backoff_many_retries) {
Duration max_backoff(10000000);
Duration backoff = retryingJitteredExponentialBackoffDur(
80, min_backoff, max_backoff, 0, rng);
EXPECT_EQ(backoff, max_backoff);
EXPECT_EQ(backoff.count(), max_backoff.count());
max_backoff = Duration(std::numeric_limits<int64_t>::max());
backoff = retryingJitteredExponentialBackoffDur(
63, min_backoff, max_backoff, 0, rng);
EXPECT_LT(backoff, max_backoff);
EXPECT_LT(backoff.count(), max_backoff.count());
max_backoff = Duration(std::numeric_limits<int64_t>::max());
backoff = retryingJitteredExponentialBackoffDur(
64, min_backoff, max_backoff, 0, rng);
EXPECT_EQ(backoff, max_backoff);
EXPECT_EQ(backoff.count(), max_backoff.count());
}
TEST(RetryingTest, policy_capped_jittered_exponential_backoff_min_zero) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment