Search in sources :

Example 6 with PlainActionFuture

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));
}
Also used : Optional(java.util.Optional) ThreadPool(org.opensearch.threadpool.ThreadPool) ActionFilters(org.opensearch.action.support.ActionFilters) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) ActionListener(org.opensearch.action.ActionListener) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Example 7 with PlainActionFuture

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));
}
Also used : Optional(java.util.Optional) ActionListener(org.opensearch.action.ActionListener) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.anyString(org.mockito.Mockito.anyString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ActionFilters(org.opensearch.action.support.ActionFilters) NodeStateManager(org.opensearch.ad.NodeStateManager)

Example 8 with PlainActionFuture

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);
}
Also used : Arrays(java.util.Arrays) ModelManager(org.opensearch.ad.ml.ModelManager) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) HashRing(org.opensearch.ad.cluster.HashRing) ThresholdingResult(org.opensearch.ad.ml.ThresholdingResult) TransportInterceptor(org.opensearch.transport.TransportInterceptor) AbstractADTest(org.opensearch.ad.AbstractADTest) Version(org.opensearch.Version) LimitExceededException(org.opensearch.ad.common.exception.LimitExceededException) Transport(org.opensearch.transport.Transport) Pair(org.apache.commons.lang3.tuple.Pair) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) Bwc(org.opensearch.ad.util.Bwc) BACKOFF_MINUTES(org.opensearch.ad.settings.AnomalyDetectorSettings.BACKOFF_MINUTES) AfterClass(org.junit.AfterClass) Client(org.opensearch.client.Client) TimeValue(org.opensearch.common.unit.TimeValue) Clusters(org.opensearch.action.search.SearchResponse.Clusters) MAX_RETRY_FOR_UNRESPONSIVE_NODE(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_RETRY_FOR_UNRESPONSIVE_NODE) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) Set(java.util.Set) ADTaskManager(org.opensearch.ad.task.ADTaskManager) Settings(org.opensearch.common.settings.Settings) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) TransportService(org.opensearch.transport.TransportService) ColdEntityWorker(org.opensearch.ad.ratelimit.ColdEntityWorker) CountDownLatch(java.util.concurrent.CountDownLatch) ActionFilters(org.opensearch.action.support.ActionFilters) Matchers.argThat(org.mockito.Matchers.argThat) NodeStateManager(org.opensearch.ad.NodeStateManager) TestHelpers(org.opensearch.ad.TestHelpers) MLUtil(test.org.opensearch.ad.util.MLUtil) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) Matchers.containsString(org.hamcrest.Matchers.containsString) TransportException(org.opensearch.transport.TransportException) Mockito.mock(org.mockito.Mockito.mock) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) ADCircuitBreakerService(org.opensearch.ad.breaker.ADCircuitBreakerService) ThreadPool(org.opensearch.threadpool.ThreadPool) Aggregations(org.opensearch.search.aggregations.Aggregations) SearchHits(org.opensearch.search.SearchHits) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) PAGE_SIZE(org.opensearch.ad.settings.AnomalyDetectorSettings.PAGE_SIZE) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) Before(org.junit.Before) EntityFeatureRequest(org.opensearch.ad.ratelimit.EntityFeatureRequest) InternalFailure(org.opensearch.ad.common.exception.InternalFailure) CounterSupplier(org.opensearch.ad.stats.suppliers.CounterSupplier) FeatureManager(org.opensearch.ad.feature.FeatureManager) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) ADStat(org.opensearch.ad.stats.ADStat) CheckpointReadWorker(org.opensearch.ad.ratelimit.CheckpointReadWorker) BwcTests(org.opensearch.BwcTests) CompositeRetriever(org.opensearch.ad.feature.CompositeRetriever) Mockito.never(org.mockito.Mockito.never) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) ChronoUnit(java.time.temporal.ChronoUnit) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) ClusterService(org.opensearch.cluster.service.ClusterService) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) StatNames(org.opensearch.ad.stats.StatNames) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InternalMin(org.opensearch.search.aggregations.metrics.InternalMin) Aggregation(org.opensearch.search.aggregations.Aggregation) ADStats(org.opensearch.ad.stats.ADStats) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ArgumentMatcher(org.mockito.ArgumentMatcher) ResultWriteWorker(org.opensearch.ad.ratelimit.ResultWriteWorker) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) After(org.junit.After) RandomModelStateConfig(test.org.opensearch.ad.util.RandomModelStateConfig) GetResponse(org.opensearch.action.get.GetResponse) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) TransportResponse(org.opensearch.transport.TransportResponse) Instant(java.time.Instant) List(java.util.List) Optional(java.util.Optional) ClusterServiceUtils(org.opensearch.test.ClusterServiceUtils) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MAX_ENTITIES_PER_QUERY(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_ENTITIES_PER_QUERY) CacheProvider(org.opensearch.ad.caching.CacheProvider) BeforeClass(org.junit.BeforeClass) DocValueFormat(org.opensearch.search.DocValueFormat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Function(java.util.function.Function) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) EntityCache(org.opensearch.ad.caching.EntityCache) SearchResponse(org.opensearch.action.search.SearchResponse) ClusterSettings(org.opensearch.common.settings.ClusterSettings) NoSuchElementException(java.util.NoSuchElementException) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) EndRunException(org.opensearch.ad.common.exception.EndRunException) StreamInput(org.opensearch.common.io.stream.StreamInput) Setting(org.opensearch.common.settings.Setting) ClientUtil(org.opensearch.ad.util.ClientUtil) TransportRequest(org.opensearch.transport.TransportRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) GetRequest(org.opensearch.action.get.GetRequest) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) Mockito.when(org.mockito.Mockito.when) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Entity(org.opensearch.ad.model.Entity) SearchResponseSections(org.opensearch.action.search.SearchResponseSections) Clock(java.time.Clock) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) Collections(java.util.Collections) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) SearchResponseSections(org.opensearch.action.search.SearchResponseSections) Aggregations(org.opensearch.search.aggregations.Aggregations) CountDownLatch(java.util.concurrent.CountDownLatch) SearchResponse(org.opensearch.action.search.SearchResponse) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 9 with PlainActionFuture

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);
}
Also used : OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) InternalFailure(org.opensearch.ad.common.exception.InternalFailure) CountDownLatch(java.util.concurrent.CountDownLatch) LimitExceededException(org.opensearch.ad.common.exception.LimitExceededException) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) TransportException(org.opensearch.transport.TransportException) IOException(java.io.IOException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) NoSuchElementException(java.util.NoSuchElementException) EndRunException(org.opensearch.ad.common.exception.EndRunException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) NodeStateManager(org.opensearch.ad.NodeStateManager)

Example 10 with PlainActionFuture

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);
}
Also used : EndRunException(org.opensearch.ad.common.exception.EndRunException) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

PlainActionFuture (org.opensearch.action.support.PlainActionFuture)191 ActionListener (org.opensearch.action.ActionListener)59 ShardId (org.opensearch.index.shard.ShardId)55 Settings (org.opensearch.common.settings.Settings)46 IOException (java.io.IOException)43 ActionFilters (org.opensearch.action.support.ActionFilters)43 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)42 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)42 CountDownLatch (java.util.concurrent.CountDownLatch)41 ClusterState (org.opensearch.cluster.ClusterState)40 ShardRouting (org.opensearch.cluster.routing.ShardRouting)39 ExecutionException (java.util.concurrent.ExecutionException)38 List (java.util.List)37 ThreadPool (org.opensearch.threadpool.ThreadPool)37 ArrayList (java.util.ArrayList)35 Matchers.containsString (org.hamcrest.Matchers.containsString)31 HashSet (java.util.HashSet)30 IndexShard (org.opensearch.index.shard.IndexShard)29 Mockito.anyString (org.mockito.Mockito.anyString)28 TransportRequest (org.opensearch.transport.TransportRequest)28