-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: backoff strategy implementation (#3758)
Implements an ExponentialBackOffStrategy that is used when Envoy retries management server connection. Risk Level: Low Testing: Added automated tests Docs Changes: N/A Release Notes: N/A Fixes #3737 Signed-off-by: Rama <rama.rao@salesforce.com>
- Loading branch information
1 parent
2c3c3e7
commit 07b13c0
Showing
11 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |