use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class MultiEntityResultTests method testQueryErrorEndRunNotNow.
/**
* Test query error causes EndRunException but not end now
* @throws InterruptedException when the await are interrupted
* @throws IOException when failing to create anomaly detector
*/
public void testQueryErrorEndRunNotNow() throws InterruptedException, IOException {
setUpNormlaStateManager();
final CountDownLatch inProgressLatch = new CountDownLatch(1);
String allShardsFailedMsg = "all shards failed";
// make PageIterator.next return failure
doAnswer(invocation -> {
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onFailure(new SearchPhaseExecutionException("search", allShardsFailedMsg, new ShardSearchFailure[] { new ShardSearchFailure(new IllegalArgumentException("blah")) }));
inProgressLatch.countDown();
return null;
}).when(client).search(any(), any());
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
action.doExecute(null, request, listener);
AnomalyResultResponse response = listener.actionGet(10000L);
assertEquals(Double.NaN, response.getAnomalyGrade(), 0.001);
assertTrue(inProgressLatch.await(10000L, TimeUnit.MILLISECONDS));
PlainActionFuture<AnomalyResultResponse> listener2 = new PlainActionFuture<>();
action.doExecute(null, request, listener2);
Exception e = expectThrows(EndRunException.class, () -> listener2.actionGet(10000L));
// wrapped INVALID_SEARCH_QUERY_MSG around SearchPhaseExecutionException by convertedQueryFailureException
assertThat("actual message: " + e.getMessage(), e.getMessage(), containsString(CommonErrorMessages.INVALID_SEARCH_QUERY_MSG));
assertThat("actual message: " + e.getMessage(), e.getMessage(), containsString(allShardsFailedMsg));
// not end now
assertTrue(!((EndRunException) e).isEndNow());
}
use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class MultiEntityResultTests method testNotAck.
public void testNotAck() throws InterruptedException, IOException {
CountDownLatch inProgress = setUpSearchResponse();
setUpTransportInterceptor(this::unackEntityResultHandler);
// mock hashing ring response. This has to happen after setting up test nodes with the failure interceptor
when(hashRing.getOwningNodeWithSameLocalAdVersionForRealtimeAD(any(String.class))).thenReturn(Optional.of(testNodes[1].discoveryNode()));
setUpEntityResult(1);
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
action.doExecute(null, request, listener);
AnomalyResultResponse response = listener.actionGet(10000L);
assertEquals(Double.NaN, response.getAnomalyGrade(), 0.01);
assertTrue(inProgress.await(10000L, TimeUnit.MILLISECONDS));
verify(stateManager, times(1)).addPressure(anyString(), anyString());
}
use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class MultiEntityResultTests method testMultipleNode.
public void testMultipleNode() throws InterruptedException, IOException {
CountDownLatch inProgress = setUpSearchResponse();
setUpTransportInterceptor(this::entityResultHandler);
Entity entity1 = Entity.createEntityByReordering(attrs1);
Entity entity2 = Entity.createEntityByReordering(attrs2);
Entity entity3 = Entity.createEntityByReordering(attrs3);
// we use ordered attributes values as the key to hashring
when(hashRing.getOwningNodeWithSameLocalAdVersionForRealtimeAD(eq(entity1.toString()))).thenReturn(Optional.of(testNodes[2].discoveryNode()));
when(hashRing.getOwningNodeWithSameLocalAdVersionForRealtimeAD(eq(entity2.toString()))).thenReturn(Optional.of(testNodes[3].discoveryNode()));
when(hashRing.getOwningNodeWithSameLocalAdVersionForRealtimeAD(eq(entity3.toString()))).thenReturn(Optional.of(testNodes[4].discoveryNode()));
for (int i = 2; i <= 4; i++) {
setUpEntityResult(i);
}
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
action.doExecute(null, request, listener);
AnomalyResultResponse response = listener.actionGet(10000L);
assertEquals(Double.NaN, response.getAnomalyGrade(), 0.01);
assertTrue(inProgress.await(10000L, TimeUnit.MILLISECONDS));
// since we have 3 results in the first page
verify(resultWriteQueue, times(3)).put(any());
}
use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class MultiEntityResultTests method testIndexNotFound.
public void testIndexNotFound() throws InterruptedException, IOException {
setUpNormlaStateManager();
final CountDownLatch inProgressLatch = new CountDownLatch(1);
// make PageIterator.next return failure
doAnswer(invocation -> {
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onFailure(new IndexNotFoundException("", ""));
inProgressLatch.countDown();
return null;
}).when(client).search(any(), any());
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
action.doExecute(null, request, listener);
AnomalyResultResponse response = listener.actionGet(10000L);
assertEquals(Double.NaN, response.getAnomalyGrade(), 0.001);
assertTrue(inProgressLatch.await(10000L, TimeUnit.MILLISECONDS));
PlainActionFuture<AnomalyResultResponse> listener2 = new PlainActionFuture<>();
action.doExecute(null, request, listener2);
Exception e = expectThrows(EndRunException.class, () -> listener2.actionGet(10000L));
assertThat("actual message: " + e.getMessage(), e.getMessage(), containsString(AnomalyResultTransportAction.TROUBLE_QUERYING_ERR_MSG));
assertTrue(!((EndRunException) e).isEndNow());
}
use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class MultiEntityResultTests method testSelectHigherExceptionInModelNode.
/**
* Test that when both previous and current run returns exception, we return more
* important exception (EndRunException is more important)
* @throws InterruptedException when failing to wait for inProgress to finish
* @throws IOException when failing to set up transport layer
*/
public void testSelectHigherExceptionInModelNode() throws InterruptedException, IOException {
when(entityCache.get(any(), any())).thenThrow(EndRunException.class);
Pair<NodeStateManager, CountDownLatch> preparedFixture = setUpTestExceptionTestingInModelNode();
NodeStateManager modelNodeStateManager = preparedFixture.getLeft();
CountDownLatch inProgress = preparedFixture.getRight();
when(modelNodeStateManager.fetchExceptionAndClear(anyString())).thenReturn(Optional.of(new OpenSearchTimeoutException("blah")));
setUpEntityResult(1, modelNodeStateManager);
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
action.doExecute(null, request, listener);
AnomalyResultResponse response = listener.actionGet(10000L);
assertEquals(Double.NaN, response.getAnomalyGrade(), 0.01);
assertTrue(inProgress.await(10000L, TimeUnit.MILLISECONDS));
// since EndRunException is thrown before getting any result, we cannot save anything
verify(resultWriteQueue, never()).put(any());
ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
verify(stateManager).setException(anyString(), exceptionCaptor.capture());
EndRunException endRunException = (EndRunException) (exceptionCaptor.getValue());
assertTrue(!endRunException.isEndNow());
}
Aggregations