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
Improve waiting strategy for Integration tests #399
Merged
openshift-merge-robot
merged 1 commit into
rhdhorchestrator:main
from
masayag:wait_strategy
Jun 7, 2023
Merged
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 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
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.
@gciavarrini what should be the timeout? was it used to be 1 minute in the previous implementation?
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.
In the bash implementation there were 100 retries.
I chose 1 minute because it seemed like a reasonable time interval and I didn't receive any feedback on this.
TBH, I'm not sure which is the best timeout value :)