This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve waiting strategy for Integration tests
Instead of relying on okhttp async implementation, we use executor service to invoke recurring requests to the service till completion. This is designed to decrease the CPU load even lower while maintaining more readable and easier-to-maintain code. Signed-off-by: Moti Asayag <masayag@redhat.com>
- Loading branch information
Showing
4 changed files
with
128 additions
and
166 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
87 changes: 87 additions & 0 deletions
87
sdk-utils/src/main/java/com/redhat/parodos/sdkutils/RetryExecutorService.java
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.redhat.parodos.sdkutils; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class RetryExecutorService<T> implements AutoCloseable { | ||
|
||
private final ExecutorService executor; | ||
|
||
public RetryExecutorService() { | ||
executor = Executors.newFixedThreadPool(1); | ||
} | ||
|
||
/** | ||
* Submit a task to the executor service, retrying on failure until the task succeeds | ||
* @param task The task to submit | ||
* @return The result of the task | ||
*/ | ||
public T submitWithRetry(Callable<T> task) { | ||
// @formatter:off | ||
return submitWithRetry(task, () -> {}, () -> {}, 10 * 60 * 1000, 5000); | ||
// @formatter:on | ||
} | ||
|
||
/** | ||
* Submit a task to the executor service, retrying on failure until the task | ||
* @param task The task to submit | ||
* @param onSuccess A callback to invoke when the task succeeds | ||
* @param onFailure A callback to invoke when the task fails | ||
* @param maxRetryTime The maximum time to retry the task for | ||
* @param retryDelay The delay between retries | ||
* @return The result of the task | ||
*/ | ||
public T submitWithRetry(Callable<T> task, Runnable onSuccess, Runnable onFailure, long maxRetryTime, | ||
long retryDelay) { | ||
Future<T> future = executor.submit(() -> { | ||
long startTime = System.currentTimeMillis(); | ||
long endTime = startTime + maxRetryTime; | ||
|
||
while (System.currentTimeMillis() < endTime) { | ||
try { | ||
T result = task.call(); | ||
onSuccess.run(); | ||
return result; // Success, no need to retry | ||
} | ||
catch (Exception e) { | ||
// Task failed, invoke onFailure callback | ||
onFailure.run(); | ||
|
||
// Sleep for the retry delay | ||
try { | ||
// FIXME: This is a blocking call, we should use a non-blocking | ||
// sleep | ||
Thread.sleep(retryDelay); | ||
} | ||
catch (InterruptedException ex) { | ||
Thread.currentThread().interrupt(); | ||
return null; // Interrupted, exit the task | ||
} | ||
} | ||
} | ||
|
||
return null; // Retry limit reached | ||
}); | ||
|
||
try { | ||
return future.get(); | ||
} | ||
catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
executor.shutdown(); | ||
boolean awaited = executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); | ||
if (!awaited) { | ||
throw new RuntimeException("Failed to await termination of executor service"); | ||
} | ||
} | ||
|
||
} |
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