-
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.
common: jittered backoff implementation (#3791)
Implements fully jittered exponential backoff algorithm and refactors router retry timer to use this. Risk Level: Low Testing: Added automated tests Docs Changes: N/A Release Notes: N/A Signed-off-by: Rama <rama.rao@salesforce.com>
- Loading branch information
1 parent
df7a291
commit b1f870a
Showing
18 changed files
with
110 additions
and
95 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
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
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 |
---|---|---|
@@ -1,57 +1,59 @@ | ||
#include "common/common/backoff_strategy.h" | ||
|
||
#include "test/mocks/runtime/mocks.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
using testing::NiceMock; | ||
using testing::Return; | ||
|
||
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, JitteredBackOffBasicFlow) { | ||
NiceMock<Runtime::MockRandomGenerator> random; | ||
ON_CALL(random, random()).WillByDefault(Return(27)); | ||
|
||
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()); | ||
JitteredBackOffStrategy jittered_back_off(25, 30, random); | ||
EXPECT_EQ(2, jittered_back_off.nextBackOffMs()); | ||
EXPECT_EQ(27, jittered_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, JitteredBackOffBasicReset) { | ||
NiceMock<Runtime::MockRandomGenerator> random; | ||
ON_CALL(random, random()).WillByDefault(Return(27)); | ||
|
||
TEST(BackOffStrategyTest, ExponentialBackOfReset) { | ||
ExponentialBackOffStrategy exponential_back_off(10, 100, 2); | ||
EXPECT_EQ(10, exponential_back_off.nextBackOffMs()); | ||
EXPECT_EQ(20, exponential_back_off.nextBackOffMs()); | ||
JitteredBackOffStrategy jittered_back_off(25, 30, random); | ||
EXPECT_EQ(2, jittered_back_off.nextBackOffMs()); | ||
EXPECT_EQ(27, jittered_back_off.nextBackOffMs()); | ||
|
||
exponential_back_off.reset(); | ||
EXPECT_EQ(10, exponential_back_off.nextBackOffMs()); // Should start from start | ||
jittered_back_off.reset(); | ||
EXPECT_EQ(2, jittered_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 | ||
TEST(BackOffStrategyTest, JitteredBackOffWithMaxInterval) { | ||
NiceMock<Runtime::MockRandomGenerator> random; | ||
ON_CALL(random, random()).WillByDefault(Return(1024)); | ||
|
||
JitteredBackOffStrategy jittered_back_off(5, 100, random); | ||
EXPECT_EQ(4, jittered_back_off.nextBackOffMs()); | ||
EXPECT_EQ(4, jittered_back_off.nextBackOffMs()); | ||
EXPECT_EQ(9, jittered_back_off.nextBackOffMs()); | ||
EXPECT_EQ(49, jittered_back_off.nextBackOffMs()); | ||
EXPECT_EQ(94, jittered_back_off.nextBackOffMs()); | ||
EXPECT_EQ(94, jittered_back_off.nextBackOffMs()); // Should return Max here | ||
} | ||
|
||
exponential_back_off.reset(); | ||
TEST(BackOffStrategyTest, JitteredBackOffWithMaxIntervalReset) { | ||
NiceMock<Runtime::MockRandomGenerator> random; | ||
ON_CALL(random, random()).WillByDefault(Return(1024)); | ||
|
||
EXPECT_EQ(10, exponential_back_off.nextBackOffMs()); // Should start from start | ||
} | ||
JitteredBackOffStrategy jittered_back_off(5, 100, random); | ||
EXPECT_EQ(4, jittered_back_off.nextBackOffMs()); | ||
EXPECT_EQ(4, jittered_back_off.nextBackOffMs()); | ||
EXPECT_EQ(9, jittered_back_off.nextBackOffMs()); | ||
EXPECT_EQ(49, jittered_back_off.nextBackOffMs()); | ||
|
||
jittered_back_off.reset(); | ||
EXPECT_EQ(4, jittered_back_off.nextBackOffMs()); // Should start from start | ||
} | ||
} // 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
Oops, something went wrong.