Search in sources :

Example 11 with PlainActionFuture

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());
}
Also used : EndRunException(org.opensearch.ad.common.exception.EndRunException) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) 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) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 12 with PlainActionFuture

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());
}
Also used : PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 13 with PlainActionFuture

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());
}
Also used : Entity(org.opensearch.ad.model.Entity) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 14 with PlainActionFuture

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());
}
Also used : EndRunException(org.opensearch.ad.common.exception.EndRunException) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) 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) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 15 with PlainActionFuture

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

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