mirror of https://github.com/apache/jclouds.git
Fixes the exponential backoff for small delay values
This commit is contained in:
parent
c902fbf906
commit
f5523c9412
|
@ -16,6 +16,7 @@
|
|||
*/
|
||||
package org.jclouds.http.handlers;
|
||||
|
||||
import static java.lang.Math.max;
|
||||
import static org.jclouds.http.HttpUtils.releasePayload;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -124,10 +125,16 @@ public class BackoffLimitedRetryHandler implements HttpRetryHandler, IOException
|
|||
|
||||
public void imposeBackoffExponentialDelay(long period, long maxPeriod, int pow, int failureCount, int max,
|
||||
String commandDescription) {
|
||||
if (period == 0) {
|
||||
// Essentially disables the exponential backoff
|
||||
logger.debug("Retry %d/%d: delaying for %d ms: %s", failureCount, max, 0, commandDescription);
|
||||
return;
|
||||
}
|
||||
long delayMs = (long) (period * Math.pow(failureCount, pow));
|
||||
// Add random delay to avoid thundering herd problem when multiple
|
||||
// simultaneous failed requests retry after sleeping for the same delay.
|
||||
delayMs += new Random().nextInt((int) (delayMs / 10));
|
||||
// Throws an exception for a value of 0
|
||||
delayMs += new Random().nextInt((int) (max(delayMs / 10, 1) ));
|
||||
delayMs = delayMs > maxPeriod ? maxPeriod : delayMs;
|
||||
logger.debug("Retry %d/%d: delaying for %d ms: %s", failureCount, max, delayMs, commandDescription);
|
||||
try {
|
||||
|
@ -136,5 +143,4 @@ public class BackoffLimitedRetryHandler implements HttpRetryHandler, IOException
|
|||
Throwables.propagate(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -80,6 +80,42 @@ public class BackoffLimitedRetryHandlerTest {
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExponentialBackoffDelaySmallInterval5() throws InterruptedException {
|
||||
long period = 5;
|
||||
long acceptableDelay = period - 1;
|
||||
|
||||
long startTime = System.nanoTime();
|
||||
handler.imposeBackoffExponentialDelay(period, 2, 1, 5, "TEST FAILURE: 1");
|
||||
long elapsedTime = (System.nanoTime() - startTime) / 1000000;
|
||||
assert elapsedTime >= period - 1 : elapsedTime;
|
||||
assertTrue(elapsedTime < period + acceptableDelay);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExponentialBackoffDelaySmallInterval1() throws InterruptedException {
|
||||
long period = 1;
|
||||
long acceptableDelay = 5;
|
||||
|
||||
long startTime = System.nanoTime();
|
||||
handler.imposeBackoffExponentialDelay(period, 2, 1, 5, "TEST FAILURE: 1");
|
||||
long elapsedTime = (System.nanoTime() - startTime) / 1000000;
|
||||
assert elapsedTime >= period - 1 : elapsedTime;
|
||||
assertTrue(elapsedTime < period + acceptableDelay);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExponentialBackoffDelaySmallInterval0() throws InterruptedException {
|
||||
long period = 0;
|
||||
long acceptableDelay = 5;
|
||||
|
||||
long startTime = System.nanoTime();
|
||||
handler.imposeBackoffExponentialDelay(period, 2, 1, 5, "TEST FAILURE: 1");
|
||||
long elapsedTime = (System.nanoTime() - startTime) / 1000000;
|
||||
assert elapsedTime >= period - 1 : elapsedTime;
|
||||
assertTrue(elapsedTime < period + acceptableDelay);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClosesInputStream() throws InterruptedException, IOException, SecurityException, NoSuchMethodException {
|
||||
HttpCommand command = createCommand();
|
||||
|
|
Loading…
Reference in New Issue