|
@@ -22,8 +22,6 @@ package org.elasticsearch.client;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
|
|
-import org.elasticsearch.client.DeadHostState.TimeSupplier;
|
|
|
-
|
|
|
import static org.hamcrest.MatcherAssert.assertThat;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.greaterThan;
|
|
@@ -38,14 +36,14 @@ public class DeadHostStateTests extends RestClientTestCase {
|
|
|
private static long[] EXPECTED_TIMEOUTS_SECONDS = new long[]{60, 84, 120, 169, 240, 339, 480, 678, 960, 1357, 1800};
|
|
|
|
|
|
public void testInitialDeadHostStateDefaultTimeSupplier() {
|
|
|
- DeadHostState deadHostState = new DeadHostState(DeadHostState.TimeSupplier.DEFAULT);
|
|
|
+ DeadHostState deadHostState = new DeadHostState(DeadHostState.DEFAULT_TIME_SUPPLIER);
|
|
|
long currentTime = System.nanoTime();
|
|
|
assertThat(deadHostState.getDeadUntilNanos(), greaterThanOrEqualTo(currentTime));
|
|
|
assertThat(deadHostState.getFailedAttempts(), equalTo(1));
|
|
|
}
|
|
|
|
|
|
public void testDeadHostStateFromPreviousDefaultTimeSupplier() {
|
|
|
- DeadHostState previous = new DeadHostState(DeadHostState.TimeSupplier.DEFAULT);
|
|
|
+ DeadHostState previous = new DeadHostState(DeadHostState.DEFAULT_TIME_SUPPLIER);
|
|
|
int iters = randomIntBetween(5, 30);
|
|
|
for (int i = 0; i < iters; i++) {
|
|
|
DeadHostState deadHostState = new DeadHostState(previous);
|
|
@@ -58,10 +56,13 @@ public class DeadHostStateTests extends RestClientTestCase {
|
|
|
public void testCompareToTimeSupplier() {
|
|
|
int numObjects = randomIntBetween(EXPECTED_TIMEOUTS_SECONDS.length, 30);
|
|
|
DeadHostState[] deadHostStates = new DeadHostState[numObjects];
|
|
|
+ final AtomicLong time = new AtomicLong(0);
|
|
|
for (int i = 0; i < numObjects; i++) {
|
|
|
if (i == 0) {
|
|
|
- // this test requires a strictly increasing timer
|
|
|
- deadHostStates[i] = new DeadHostState(new StrictMonotonicTimeSupplier());
|
|
|
+ // this test requires a strictly increasing timer. This ensures that even if we call this time supplier in a very tight
|
|
|
+ // loop we always notice time moving forward. This does not happen for real timer implementations
|
|
|
+ // (e.g. on Linux <code>clock_gettime</code> provides microsecond resolution).
|
|
|
+ deadHostStates[i] = new DeadHostState(time::incrementAndGet);
|
|
|
} else {
|
|
|
deadHostStates[i] = new DeadHostState(deadHostStates[i - 1]);
|
|
|
}
|
|
@@ -74,42 +75,39 @@ public class DeadHostStateTests extends RestClientTestCase {
|
|
|
|
|
|
public void testCompareToDifferingTimeSupplier() {
|
|
|
try {
|
|
|
- new DeadHostState(TimeSupplier.DEFAULT).compareTo(
|
|
|
- new DeadHostState(new ConfigurableTimeSupplier()));
|
|
|
+ new DeadHostState(DeadHostState.DEFAULT_TIME_SUPPLIER).compareTo(
|
|
|
+ new DeadHostState(() -> 0L));
|
|
|
fail("expected failure");
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
- assertEquals("can't compare DeadHostStates with different clocks [nanoTime != configured[0]]",
|
|
|
- e.getMessage());
|
|
|
+ assertEquals("can't compare DeadHostStates holding different time suppliers as they may " +
|
|
|
+ "be based on different clocks", e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void testShallBeRetried() {
|
|
|
- ConfigurableTimeSupplier timeSupplier = new ConfigurableTimeSupplier();
|
|
|
+ final AtomicLong time = new AtomicLong(0);
|
|
|
DeadHostState deadHostState = null;
|
|
|
for (int i = 0; i < EXPECTED_TIMEOUTS_SECONDS.length; i++) {
|
|
|
long expectedTimeoutSecond = EXPECTED_TIMEOUTS_SECONDS[i];
|
|
|
- timeSupplier.nanoTime = 0;
|
|
|
if (i == 0) {
|
|
|
- deadHostState = new DeadHostState(timeSupplier);
|
|
|
+ deadHostState = new DeadHostState(time::get);
|
|
|
} else {
|
|
|
deadHostState = new DeadHostState(deadHostState);
|
|
|
}
|
|
|
for (int j = 0; j < expectedTimeoutSecond; j++) {
|
|
|
- timeSupplier.nanoTime += TimeUnit.SECONDS.toNanos(1);
|
|
|
+ time.addAndGet(TimeUnit.SECONDS.toNanos(1));
|
|
|
assertThat(deadHostState.shallBeRetried(), is(false));
|
|
|
}
|
|
|
int iters = randomIntBetween(5, 30);
|
|
|
for (int j = 0; j < iters; j++) {
|
|
|
- timeSupplier.nanoTime += TimeUnit.SECONDS.toNanos(1);
|
|
|
+ time.addAndGet(TimeUnit.SECONDS.toNanos(1));
|
|
|
assertThat(deadHostState.shallBeRetried(), is(true));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void testDeadHostStateTimeouts() {
|
|
|
- ConfigurableTimeSupplier zeroTimeSupplier = new ConfigurableTimeSupplier();
|
|
|
- zeroTimeSupplier.nanoTime = 0L;
|
|
|
- DeadHostState previous = new DeadHostState(zeroTimeSupplier);
|
|
|
+ DeadHostState previous = new DeadHostState(() -> 0L);
|
|
|
for (long expectedTimeoutsSecond : EXPECTED_TIMEOUTS_SECONDS) {
|
|
|
assertThat(TimeUnit.NANOSECONDS.toSeconds(previous.getDeadUntilNanos()), equalTo(expectedTimeoutsSecond));
|
|
|
previous = new DeadHostState(previous);
|
|
@@ -123,37 +121,4 @@ public class DeadHostStateTests extends RestClientTestCase {
|
|
|
previous = deadHostState;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- static class ConfigurableTimeSupplier implements DeadHostState.TimeSupplier {
|
|
|
- long nanoTime;
|
|
|
-
|
|
|
- @Override
|
|
|
- public long nanoTime() {
|
|
|
- return nanoTime;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String toString() {
|
|
|
- return "configured[" + nanoTime + "]";
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Simulates a monotonically strict increasing time (i.e. the value increases on every call to <code>#nanoTime()</code>). This ensures
|
|
|
- * that even if we call this time supplier in a very tight loop we always notice time moving forward. This does not happen for real
|
|
|
- * timer implementations (e.g. on Linux <code>clock_gettime</code> provides microsecond resolution).
|
|
|
- */
|
|
|
- static class StrictMonotonicTimeSupplier implements DeadHostState.TimeSupplier {
|
|
|
- private final AtomicLong time = new AtomicLong(0);
|
|
|
-
|
|
|
- @Override
|
|
|
- public long nanoTime() {
|
|
|
- return time.incrementAndGet();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String toString() {
|
|
|
- return "strict monotonic[" + time.get() + "]";
|
|
|
- }
|
|
|
- }
|
|
|
}
|