-
Notifications
You must be signed in to change notification settings - Fork 166
Add test cases for RemoteConnector retry behavior #3504
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
114 changes: 114 additions & 0 deletions
114
...rch/ml/engine/algorithms/remote/RemoteConnectorExecutor_RetryableActionExtensionTest.java
This file contains hidden or 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,114 @@ | ||
package org.opensearch.ml.engine.algorithms.remote; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
import org.apache.logging.log4j.Logger; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.opensearch.action.bulk.BackoffPolicy; | ||
import org.opensearch.common.collect.Tuple; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.unit.TimeValue; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.ml.common.connector.ConnectorClientConfig; | ||
import org.opensearch.ml.common.input.MLInput; | ||
import org.opensearch.ml.common.output.model.ModelTensors; | ||
import org.opensearch.ml.engine.algorithms.remote.RemoteConnectorExecutor.RetryableActionExtension; | ||
import org.opensearch.ml.engine.algorithms.remote.RemoteConnectorExecutor.RetryableActionExtensionArgs; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class RemoteConnectorExecutor_RetryableActionExtensionTest { | ||
|
||
private static final int TEST_ATTEMPT_LIMIT = 5; | ||
|
||
@Mock | ||
Logger logger; | ||
@Mock | ||
ThreadPool threadPool; | ||
@Mock | ||
TimeValue initialDelay; | ||
@Mock | ||
TimeValue timeoutValue; | ||
@Mock | ||
ActionListener<Tuple<Integer, ModelTensors>> listener; | ||
@Mock | ||
BackoffPolicy backoffPolicy; | ||
@Mock | ||
ConnectorClientConfig connectorClientConfig; | ||
@Mock | ||
RemoteConnectorExecutor connectionExecutor; | ||
|
||
RetryableActionExtension retryableAction; | ||
|
||
@Before | ||
public void setup() { | ||
when(connectionExecutor.getConnectorClientConfig()).thenReturn(connectorClientConfig); | ||
when(connectionExecutor.getLogger()).thenReturn(logger); | ||
var args = RetryableActionExtensionArgs.builder() | ||
.action("action") | ||
.connectionExecutor(connectionExecutor) | ||
.mlInput(mock(MLInput.class)) | ||
.parameters(Map.of()) | ||
.executionContext(mock(ExecutionContext.class)) | ||
.payload("payload") | ||
.build(); | ||
var settings = Settings.builder().put("node.name", "test").build(); | ||
retryableAction = new RetryableActionExtension(logger, new ThreadPool(settings), TimeValue.timeValueMillis(5), TimeValue.timeValueMillis(500), listener, backoffPolicy, args); | ||
} | ||
|
||
@Test | ||
public void test_ShouldRetry_hitLimitOnRetries() { | ||
var attempts = retryAttempts(-1, this::createThrottleException); | ||
|
||
assertThat(attempts, equalTo(TEST_ATTEMPT_LIMIT)); | ||
} | ||
|
||
@Test | ||
@SuppressWarnings("unchecked") | ||
public void test_ShouldRetry_OnlyOnThrottleExceptions() { | ||
var exceptions = mock(Supplier.class); | ||
when(exceptions.get()) | ||
.thenReturn(createThrottleException()) | ||
.thenReturn(createThrottleException()) | ||
.thenReturn(new RuntimeException()); // Stop retrying on 3rd exception | ||
var attempts = retryAttempts(-1, exceptions); | ||
|
||
assertThat(attempts, equalTo(2)); | ||
verify(exceptions, times(3)).get(); | ||
} | ||
|
||
@Test | ||
public void test_ShouldRetry_stopAtMaxAttempts() { | ||
int maxAttempts = 3; | ||
var attempts = retryAttempts(maxAttempts, this::createThrottleException); | ||
|
||
assertThat(attempts, equalTo(maxAttempts)); | ||
} | ||
|
||
private int retryAttempts(int maxAttempts, Supplier<Exception> exception) { | ||
when(connectorClientConfig.getMaxRetryTimes()).thenReturn(maxAttempts); | ||
int attempt = 0; | ||
boolean shouldRetry; | ||
do { | ||
shouldRetry = retryableAction.shouldRetry(exception.get()); | ||
} while (shouldRetry && ++attempt < TEST_ATTEMPT_LIMIT); | ||
return attempt; | ||
} | ||
|
||
private RemoteConnectorThrottlingException createThrottleException() { | ||
return new RemoteConnectorThrottlingException("Throttle", RestStatus.TOO_MANY_REQUESTS); | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.