-
Notifications
You must be signed in to change notification settings - Fork 277
Add a random exponential wait strategy; Fixes #28 #50
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
Open
NJAldwin
wants to merge
1
commit into
rholder:master
Choose a base branch
from
NJAldwin:random-exponential-wait
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -157,6 +157,90 @@ public static WaitStrategy exponentialWait(long multiplier, | |
return new ExponentialWaitStrategy(multiplier, maximumTimeUnit.toMillis(maximumTime)); | ||
} | ||
|
||
/** | ||
* Returns a strategy which sleeps for a random amount of time between the minimumTime and an | ||
* exponentially increasing maximum time. The maximum time increases exponentially after each | ||
* failed attempt up to {@code Long.MAX_VALUE}. | ||
* | ||
* @return a wait strategy that waits a random amount of time and increments | ||
* the maximum bound with each failed attempt using exponential backoff | ||
*/ | ||
public static WaitStrategy randomExponentialWait() { | ||
return new RandomExponentialWaitStrategy(1, 0L, Long.MAX_VALUE); | ||
} | ||
|
||
/** | ||
* Returns a strategy which sleeps for an exponential amount of time after the first failed attempt, | ||
* and in exponentially incrementing amounts after each failed attempt up to the maximumTime. | ||
* | ||
* @param maximumTime the maximum time to sleep | ||
* @param maximumTimeUnit the unit of the maximum time | ||
* @return a wait strategy that waits a random amount of time and increments | ||
* the maximum bound with each failed attempt using exponential backoff | ||
* @throws IllegalStateException if the minimum sleep time is < 0. | ||
*/ | ||
public static WaitStrategy randomExponentialWait(long maximumTime, | ||
@Nonnull TimeUnit maximumTimeUnit) { | ||
Preconditions.checkNotNull(maximumTimeUnit, "The maximum time unit may not be null"); | ||
return new RandomExponentialWaitStrategy(1, 0L, maximumTimeUnit.toMillis(maximumTime)); | ||
} | ||
|
||
/** | ||
* Returns a strategy which sleeps for a random amount of time between the minimumTime and an | ||
* exponentially increasing maximum time. The maximum time increases exponentially after each | ||
* failed attempt up to the maximumTime. | ||
* | ||
* @param minimumTime the minimum time to sleep | ||
* @param minimumTimeUnit the unit of the minimum time | ||
* @param maximumTime the maximum time to sleep | ||
* @param maximumTimeUnit the unit of the maximum time | ||
* @return a wait strategy that waits a random amount of time and increments | ||
* the maximum bound with each failed attempt using exponential backoff | ||
* @throws IllegalStateException if the minimum sleep time is < 0, or if the | ||
* maximum sleep time is <= the minimum. | ||
*/ | ||
public static WaitStrategy randomExponentialWait(long minimumTime, | ||
@Nonnull TimeUnit minimumTimeUnit, | ||
long maximumTime, | ||
@Nonnull TimeUnit maximumTimeUnit) { | ||
Preconditions.checkNotNull(minimumTimeUnit, "The minimum time unit may not be null"); | ||
Preconditions.checkNotNull(maximumTimeUnit, "The maximum time unit may not be null"); | ||
return new RandomExponentialWaitStrategy(1, | ||
minimumTimeUnit.toMillis(minimumTime), | ||
maximumTimeUnit.toMillis(maximumTime)); | ||
} | ||
|
||
/** | ||
* Returns a strategy which sleeps for a random amount of time between the minimumTime and an | ||
* exponentially increasing maximum time. The maximum time increases exponentially after each | ||
* failed attempt up to the maximumTime. | ||
* The resulting wait time can be further controlled by the multiplier | ||
* (but is still held within the minimum and maximum time bounds) | ||
* nextWaitTime = randomExponentialIncrement * {@code multiplier} | ||
* | ||
* @param multiplier multiply the wait time calculated by this | ||
* @param minimumTime the minimum time to sleep | ||
* @param minimumTimeUnit the unit of the minimum time | ||
* @param maximumTime the maximum time to sleep | ||
* @param maximumTimeUnit the unit of the maximum time | ||
* @return a wait strategy that waits a random amount of time and increments | ||
* the maximum bound with each failed attempt using exponential backoff | ||
* @throws IllegalStateException if the minimum sleep time is < 0, or if the | ||
* maximum sleep time is <= the minimum, or if the | ||
* multiplier is > the maximum sleep time. | ||
*/ | ||
public static WaitStrategy randomExponentialWait(long multiplier, | ||
long minimumTime, | ||
@Nonnull TimeUnit minimumTimeUnit, | ||
long maximumTime, | ||
@Nonnull TimeUnit maximumTimeUnit) { | ||
Preconditions.checkNotNull(minimumTimeUnit, "The minimum time unit may not be null"); | ||
Preconditions.checkNotNull(maximumTimeUnit, "The maximum time unit may not be null"); | ||
return new RandomExponentialWaitStrategy(multiplier, | ||
minimumTimeUnit.toMillis(minimumTime), | ||
maximumTimeUnit.toMillis(maximumTime)); | ||
} | ||
|
||
/** | ||
* Returns a strategy which sleeps for an increasing amount of time after the first failed attempt, | ||
* and in Fibonacci increments after each failed attempt up to {@link Long#MAX_VALUE}. | ||
|
@@ -309,6 +393,40 @@ public long computeSleepTime(Attempt failedAttempt) { | |
} | ||
} | ||
|
||
@Immutable | ||
private static final class RandomExponentialWaitStrategy implements WaitStrategy { | ||
private static final Random RANDOM = new Random(); | ||
private final long multiplier; | ||
private final long minimumWait; | ||
private final long maximumWait; | ||
|
||
public RandomExponentialWaitStrategy(long multiplier, | ||
long minimumWait, | ||
long maximumWait) { | ||
Preconditions.checkArgument(multiplier > 0L, "multiplier must be > 0 but is %d", multiplier); | ||
Preconditions.checkArgument(minimumWait >= 0L, "minimumWait must be >= 0 but is %d", minimumWait); | ||
Preconditions.checkArgument(maximumWait > minimumWait, "maximumWait must be > minimumWait but is %d", maximumWait); | ||
Preconditions.checkArgument(multiplier < maximumWait, "multiplier must be < maximumWait but is %d", multiplier); | ||
this.multiplier = multiplier; | ||
this.minimumWait = minimumWait; | ||
this.maximumWait = maximumWait; | ||
} | ||
|
||
@Override | ||
public long computeSleepTime(Attempt failedAttempt) { | ||
long upperBound = Math.round(Math.pow(2, failedAttempt.getAttemptNumber())); | ||
long time = Math.abs(RANDOM.nextLong()) % upperBound; | ||
long result = Math.round(multiplier * time); | ||
if (result < minimumWait) { | ||
result = minimumWait; | ||
} | ||
if (result > maximumWait) { | ||
result = maximumWait; | ||
} | ||
return result >= 0L ? result : 0L; | ||
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.
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. I left it this way for consistency with the other code in this file. Is it worth it to replace them all to be consistent? |
||
} | ||
} | ||
|
||
@Immutable | ||
private static final class FibonacciWaitStrategy implements WaitStrategy { | ||
private final long multiplier; | ||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I assume you used a
private static final Random
for consistency with the existing code; I wonder if it's worth switching over to a sharedThreadLocalRandom
?