use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class AnomalyResultTests method testColdStartBecauseFailtoGetCheckpoint.
@SuppressWarnings({ "unchecked" })
public void testColdStartBecauseFailtoGetCheckpoint() {
ThreadPool mockThreadPool = mock(ThreadPool.class);
setUpColdStart(mockThreadPool, new ColdStartConfig.Builder().getCheckpointException(new IndexNotFoundException(CommonName.CHECKPOINT_INDEX_NAME)).build());
doAnswer(invocation -> {
ActionListener<Optional<double[][]>> listener = invocation.getArgument(1);
listener.onResponse(Optional.empty());
return null;
}).when(featureQuery).getColdStartData(any(AnomalyDetector.class), any(ActionListener.class));
AnomalyResultRequest request = new AnomalyResultRequest(adID, 100, 200);
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
AnomalyResultTransportAction action = new AnomalyResultTransportAction(new ActionFilters(Collections.emptySet()), transportService, settings, client, stateManager, featureQuery, normalModelManager, hashRing, clusterService, indexNameResolver, adCircuitBreakerService, adStats, mockThreadPool, NamedXContentRegistry.EMPTY, adTaskManager);
action.doExecute(null, request, listener);
AnomalyResultResponse response = listener.actionGet(10000L);
assertEquals(Double.NaN, response.getAnomalyGrade(), 0.001);
verify(featureQuery, times(1)).getColdStartData(any(AnomalyDetector.class), any(ActionListener.class));
}
use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class AnomalyResultTests method testMute.
@SuppressWarnings("unchecked")
public void testMute() {
NodeStateManager muteStateManager = mock(NodeStateManager.class);
when(muteStateManager.isMuted(any(String.class), any(String.class))).thenReturn(true);
doAnswer(invocation -> {
ActionListener<Optional<AnomalyDetector>> listener = invocation.getArgument(1);
listener.onResponse(Optional.of(detector));
return null;
}).when(muteStateManager).getAnomalyDetector(any(String.class), any(ActionListener.class));
AnomalyResultTransportAction action = new AnomalyResultTransportAction(new ActionFilters(Collections.emptySet()), transportService, settings, client, muteStateManager, featureQuery, normalModelManager, hashRing, clusterService, indexNameResolver, adCircuitBreakerService, adStats, threadPool, NamedXContentRegistry.EMPTY, adTaskManager);
AnomalyResultRequest request = new AnomalyResultRequest(adID, 100, 200);
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
action.doExecute(null, request, listener);
Throwable exception = assertException(listener, AnomalyDetectionException.class);
assertThat(exception.getMessage(), containsString(AnomalyResultTransportAction.NODE_UNRESPONSIVE_ERR_MSG));
}
use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class MultiEntityResultTests method testNullFeatures.
public void testNullFeatures() throws InterruptedException {
final CountDownLatch inProgressLatch = new CountDownLatch(1);
CompositeAggregation emptyComposite = mock(CompositeAggregation.class);
when(emptyComposite.getName()).thenReturn(null);
when(emptyComposite.afterKey()).thenReturn(null);
// empty bucket
when(emptyComposite.getBuckets()).thenAnswer((Answer<List<CompositeAggregation.Bucket>>) invocation -> {
return new ArrayList<CompositeAggregation.Bucket>();
});
Aggregations emptyAggs = new Aggregations(Collections.singletonList(emptyComposite));
SearchResponseSections emptySections = new SearchResponseSections(SearchHits.empty(), emptyAggs, null, false, null, null, 1);
SearchResponse nullResponse = new SearchResponse(emptySections, null, 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, Clusters.EMPTY);
doAnswer(invocation -> {
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(nullResponse);
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.01);
assertTrue(inProgressLatch.await(10000L, TimeUnit.MILLISECONDS));
PlainActionFuture<AnomalyResultResponse> listener2 = new PlainActionFuture<>();
action.doExecute(null, request, listener2);
AnomalyResultResponse response2 = listener2.actionGet(10000L);
assertEquals(Double.NaN, response2.getAnomalyGrade(), 0.01);
}
use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class MultiEntityResultTests method testTimeOutExceptionInModelNode.
/**
* Test that in model node, previously recorded exception is OpenSearchTimeoutException,
* @throws IOException when failing to set up transport layer
* @throws InterruptedException when failing to wait for inProgress to finish
*/
public void testTimeOutExceptionInModelNode() throws IOException, InterruptedException {
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 OpenSearchTimeoutException is not end run exception (now = true), the normal workflow continues
verify(resultWriteQueue, times(3)).put(any());
ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
verify(stateManager).setException(anyString(), exceptionCaptor.capture());
Exception actual = exceptionCaptor.getValue();
assertTrue("actual exception is " + actual, actual instanceof InternalFailure);
}
use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class MultiEntityResultTests method testColdStartEndRunException.
public void testColdStartEndRunException() {
when(stateManager.fetchExceptionAndClear(anyString())).thenReturn(Optional.of(new EndRunException(detectorId, CommonErrorMessages.INVALID_SEARCH_QUERY_MSG, new NoSuchElementException("No value present"), false)));
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
action.doExecute(null, request, listener);
assertException(listener, EndRunException.class, CommonErrorMessages.INVALID_SEARCH_QUERY_MSG);
}
Aggregations