-
Notifications
You must be signed in to change notification settings - Fork 5.3k
config: backoff strategy implementation #3758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
93007d2
d31d613
9ebfd2a
3b8ba1a
f2919b4
f8319f2
6f2d3e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #pragma once | ||
|
|
||
| #include "envoy/common/pure.h" | ||
|
|
||
| namespace Envoy { | ||
| /** | ||
| * Generic interface for all backoff strategy implementations. | ||
| */ | ||
| class BackOffStrategy { | ||
| public: | ||
| virtual ~BackOffStrategy() {} | ||
|
|
||
| /** | ||
| * @return the next backoff interval in milli seconds. | ||
| */ | ||
| virtual uint64_t nextBackOffMs() PURE; | ||
|
|
||
| /** | ||
| * Resets the intervals so that the back off intervals can start again. | ||
| */ | ||
| virtual void reset() PURE; | ||
| }; | ||
|
|
||
| typedef std::unique_ptr<BackOffStrategy> BackOffStrategyPtr; | ||
| } // namespace Envoy |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #include "common/common/backoff_strategy.h" | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| ExponentialBackOffStrategy::ExponentialBackOffStrategy(uint64_t initial_interval, | ||
| uint64_t max_interval, double multiplier) | ||
| : initial_interval_(initial_interval), max_interval_(max_interval), multiplier_(multiplier), | ||
| current_interval_(0) { | ||
| ASSERT(multiplier_ > 1.0); | ||
| ASSERT(initial_interval_ <= max_interval_); | ||
| ASSERT(initial_interval_ * multiplier_ <= max_interval_); | ||
| } | ||
|
|
||
| uint64_t ExponentialBackOffStrategy::nextBackOffMs() { return computeNextInterval(); } | ||
|
|
||
| void ExponentialBackOffStrategy::reset() { current_interval_ = 0; } | ||
|
|
||
| uint64_t ExponentialBackOffStrategy::computeNextInterval() { | ||
| if (current_interval_ == 0) { | ||
| current_interval_ = initial_interval_; | ||
| } else if (current_interval_ >= max_interval_) { | ||
| current_interval_ = max_interval_; | ||
| } else { | ||
| uint64_t new_interval = current_interval_; | ||
| new_interval = ceil(new_interval * multiplier_); | ||
| current_interval_ = new_interval > max_interval_ ? max_interval_ : new_interval; | ||
| } | ||
| return current_interval_; | ||
| } | ||
| } // namespace Envoy |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
|
|
||
| #include "envoy/common/backoff_strategy.h" | ||
|
|
||
| #include "common/common/assert.h" | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| /** | ||
| * Implementation of BackOffStrategy that increases the back off period for each retry attempt. When | ||
| * the interval has reached the max interval, it is no longer increased. | ||
| */ | ||
| class ExponentialBackOffStrategy : public BackOffStrategy { | ||
|
|
||
| public: | ||
| ExponentialBackOffStrategy(uint64_t initial_interval, uint64_t max_interval, double multiplier); | ||
|
|
||
| // BackOffStrategy methods | ||
| uint64_t nextBackOffMs() override; | ||
| void reset() override; | ||
|
|
||
| private: | ||
| uint64_t computeNextInterval(); | ||
|
|
||
| const uint64_t initial_interval_; | ||
| const uint64_t max_interval_; | ||
| const double multiplier_; | ||
| uint64_t current_interval_; | ||
| }; | ||
| } // namespace Envoy |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,7 @@ RetryStateImpl::RetryStateImpl(const RetryPolicy& route_policy, Http::HeaderMap& | |
| RetryStateImpl::~RetryStateImpl() { resetRetry(); } | ||
|
|
||
| void RetryStateImpl::enableBackoffTimer() { | ||
| // TODO(ramaraochavali): Implement JitteredExponentialBackOff and refactor this. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see, you have thought about jitter here..
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, and, honestly, you should be using jittered backoff for the management connection also. I would not support any backoff policy that is not jittered...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mattklein123 i am ok with that. Let us review the PR for jittered backoff #3791 and I can move management connection to use this. @htuch thoughts?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think have jitter as default (and only built-in) scheme makes sense, best not to provide sharp objects that aren't needed. |
||
| // We use a fully jittered exponential backoff algorithm. | ||
| current_retry_++; | ||
| uint32_t multiplier = (1 << current_retry_) - 1; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #include "common/common/backoff_strategy.h" | ||
|
|
||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| TEST(BackOffStrategyTest, ExponentialBackOffBasicTest) { | ||
| ExponentialBackOffStrategy exponential_back_off(10, 100, 2); | ||
| EXPECT_EQ(10, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(20, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(40, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(80, exponential_back_off.nextBackOffMs()); | ||
| } | ||
|
|
||
| TEST(BackOffStrategyTest, ExponentialBackOffFractionalMultiplier) { | ||
| ExponentialBackOffStrategy exponential_back_off(10, 50, 1.5); | ||
| EXPECT_EQ(10, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(15, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(23, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(35, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(50, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(50, exponential_back_off.nextBackOffMs()); | ||
| } | ||
|
|
||
| TEST(BackOffStrategyTest, ExponentialBackOffMaxIntervalReached) { | ||
| ExponentialBackOffStrategy exponential_back_off(10, 100, 2); | ||
| EXPECT_EQ(10, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(20, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(40, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(80, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(100, exponential_back_off.nextBackOffMs()); // Should return Max here | ||
| EXPECT_EQ(100, exponential_back_off.nextBackOffMs()); // Should return Max here | ||
| } | ||
|
|
||
| TEST(BackOffStrategyTest, ExponentialBackOfReset) { | ||
| ExponentialBackOffStrategy exponential_back_off(10, 100, 2); | ||
| EXPECT_EQ(10, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(20, exponential_back_off.nextBackOffMs()); | ||
|
|
||
| exponential_back_off.reset(); | ||
| EXPECT_EQ(10, exponential_back_off.nextBackOffMs()); // Should start from start | ||
| } | ||
|
|
||
| TEST(BackOffStrategyTest, ExponentialBackOfResetAfterMaxReached) { | ||
| ExponentialBackOffStrategy exponential_back_off(10, 100, 2); | ||
| EXPECT_EQ(10, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(20, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(40, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(80, exponential_back_off.nextBackOffMs()); | ||
| EXPECT_EQ(100, exponential_back_off.nextBackOffMs()); // Should return Max here | ||
|
|
||
| exponential_back_off.reset(); | ||
|
|
||
| EXPECT_EQ(10, exponential_back_off.nextBackOffMs()); // Should start from start | ||
| } | ||
|
|
||
| } // namespace Envoy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this affect any existing tests? If not, how come?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It did not actually.