Skip to content

Use TimeValue for timeouts in safeAwait etc. #126509

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2398,8 +2398,9 @@ protected static SecureRandom secureRandomFips(final byte[] seed) throws NoSuchA
* complete are a big drag on CI times which slows everyone down.
* <p>
* For instance, tests which verify things that require the passage of time ought to simulate this (e.g. using a {@link
* org.elasticsearch.common.util.concurrent.DeterministicTaskQueue}). Excessive busy-waits ought to be replaced by blocking waits (e.g.
* using a {@link CountDownLatch}) which release as soon as the condition is satisfied.
* org.elasticsearch.common.util.concurrent.DeterministicTaskQueue}). Excessive busy-waits ought to be replaced by blocking waits. For
* instance, use a {@link CountDownLatch} or {@link CyclicBarrier} or similar to continue execution as soon as a condition is satisfied.
* To wait for a particular cluster state, use {@link ClusterServiceUtils#addTemporaryStateListener} rather than busy-waiting on an API.
*/
public static final TimeValue SAFE_AWAIT_TIMEOUT = TimeValue.timeValueSeconds(10);

Expand Down Expand Up @@ -2477,7 +2478,7 @@ public static void safeAcquire(int permits, Semaphore semaphore) {
* @return The value with which the {@code listener} was completed.
*/
public static <T> T safeAwait(SubscribableListener<T> listener) {
return safeAwait(listener, SAFE_AWAIT_TIMEOUT.getMillis(), TimeUnit.MILLISECONDS);
return safeAwait(listener, SAFE_AWAIT_TIMEOUT);
}

/**
Expand All @@ -2486,10 +2487,10 @@ public static <T> T safeAwait(SubscribableListener<T> listener) {
*
* @return The value with which the {@code listener} was completed.
*/
public static <T> T safeAwait(SubscribableListener<T> listener, long timeout, TimeUnit unit) {
public static <T> T safeAwait(SubscribableListener<T> listener, TimeValue timeout) {
final var future = new TestPlainActionFuture<T>();
listener.addListener(future);
return safeGet(future, timeout, unit);
return safeGet(future, timeout);
}

/**
Expand Down Expand Up @@ -2519,7 +2520,7 @@ public static <T extends ActionResponse> T safeExecute(ElasticsearchClient clien
* @return The value with which the {@code future} was completed.
*/
public static <T> T safeGet(Future<T> future) {
return safeGet(future, SAFE_AWAIT_TIMEOUT.millis(), TimeUnit.MILLISECONDS);
return safeGet(future, SAFE_AWAIT_TIMEOUT);
}

/**
Expand All @@ -2528,9 +2529,10 @@ public static <T> T safeGet(Future<T> future) {
*
* @return The value with which the {@code future} was completed.
*/
public static <T> T safeGet(Future<T> future, long timeout, TimeUnit unit) {
// NB private because tests should be designed not to need to wait for longer than SAFE_AWAIT_TIMEOUT.
private static <T> T safeGet(Future<T> future, TimeValue timeout) {
try {
return future.get(timeout, unit);
return future.get(timeout.millis(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new AssertionError("safeGet: interrupted waiting for SubscribableListener", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.elasticsearch.cluster.metadata.IndexMetadata.INDEX_DOWNSAMPLE_STATUS;
import static org.elasticsearch.xpack.downsample.DataStreamLifecycleDriver.getBackingIndices;
Expand Down Expand Up @@ -116,6 +115,6 @@ private void ensureDownsamplingStatus(String downsampledIndex, IndexMetadata.Dow
}
return false;
});
safeAwait(listener, timeout.millis(), TimeUnit.MILLISECONDS);
safeAwait(listener, timeout);
}
}