Search in sources :

Example 16 with PlainActionFuture

use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.

the class MultiEntityResultTests method testEndRunNowInModelNode.

public void testEndRunNowInModelNode() throws InterruptedException, IOException {
    Pair<NodeStateManager, CountDownLatch> preparedFixture = setUpTestExceptionTestingInModelNode();
    NodeStateManager modelNodeStateManager = preparedFixture.getLeft();
    CountDownLatch inProgress = preparedFixture.getRight();
    when(modelNodeStateManager.fetchExceptionAndClear(anyString())).thenReturn(Optional.of(new EndRunException(detectorId, CommonErrorMessages.INVALID_SEARCH_QUERY_MSG, new NoSuchElementException("No value present"), true)));
    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 it is end run now, we don't expect any of the normal workflow continues
    verify(resultWriteQueue, never()).put(any());
}
Also used : EndRunException(org.opensearch.ad.common.exception.EndRunException) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) CountDownLatch(java.util.concurrent.CountDownLatch) NoSuchElementException(java.util.NoSuchElementException) NodeStateManager(org.opensearch.ad.NodeStateManager)

Example 17 with PlainActionFuture

use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.

the class MultiEntityResultTests method testEmptyFeatures.

public void testEmptyFeatures() throws InterruptedException {
    final CountDownLatch inProgressLatch = new CountDownLatch(1);
    doAnswer(invocation -> {
        ActionListener<SearchResponse> listener = invocation.getArgument(1);
        listener.onResponse(createEmptyResponse());
        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 : PlainActionFuture(org.opensearch.action.support.PlainActionFuture) CountDownLatch(java.util.concurrent.CountDownLatch) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 18 with PlainActionFuture

use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.

the class AnomalyResultTests method nodeNotConnectedExceptionTemplate.

/**
 * Test whether we can handle NodeNotConnectedException when sending requests to
 * remote nodes.
 *
 * @param isRCF             whether RCF model node throws node connection
 *                          exception or not
 * @param temporary         whether node has only temporary connection issue. If
 *                          yes, we should not trigger hash ring rebuilding.
 * @param numberOfBuildCall the number of expected hash ring build call
 */
private void nodeNotConnectedExceptionTemplate(boolean isRCF, boolean temporary, int numberOfBuildCall) {
    ClusterService hackedClusterService = spy(clusterService);
    TransportService exceptionTransportService = spy(transportService);
    DiscoveryNode rcfNode = clusterService.state().nodes().getLocalNode();
    DiscoveryNode thresholdNode = testNodes[1].discoveryNode();
    if (isRCF) {
        doThrow(new NodeNotConnectedException(rcfNode, "rcf node not connected")).when(exceptionTransportService).getConnection(same(rcfNode));
    } else {
        when(hashRing.getOwningNodeWithSameLocalAdVersionForRealtimeAD(eq(thresholdModelID))).thenReturn(Optional.of(thresholdNode));
        when(hashRing.getNodeByAddress(any())).thenReturn(Optional.of(thresholdNode));
        doThrow(new NodeNotConnectedException(rcfNode, "rcf node not connected")).when(exceptionTransportService).getConnection(same(thresholdNode));
    }
    if (!temporary) {
        when(hackedClusterService.state()).thenReturn(ClusterState.builder(new ClusterName("test")).build());
    }
    // These constructors register handler in transport service
    new RCFResultTransportAction(new ActionFilters(Collections.emptySet()), exceptionTransportService, normalModelManager, adCircuitBreakerService, hashRing);
    AnomalyResultTransportAction action = new AnomalyResultTransportAction(new ActionFilters(Collections.emptySet()), exceptionTransportService, settings, client, stateManager, featureQuery, normalModelManager, hashRing, hackedClusterService, indexNameResolver, adCircuitBreakerService, adStats, threadPool, NamedXContentRegistry.EMPTY, adTaskManager);
    AnomalyResultRequest request = new AnomalyResultRequest(adID, 100, 200);
    PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
    action.doExecute(null, request, listener);
    assertException(listener, AnomalyDetectionException.class);
    if (!temporary) {
        verify(hashRing, times(numberOfBuildCall)).buildCirclesForRealtimeAD();
        verify(stateManager, never()).addPressure(any(String.class), any(String.class));
    } else {
        verify(hashRing, never()).buildCirclesForRealtimeAD();
        verify(stateManager, times(numberOfBuildCall)).addPressure(any(String.class), any(String.class));
    }
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ActionFilters(org.opensearch.action.support.ActionFilters) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.anyString(org.mockito.Mockito.anyString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ClusterService(org.opensearch.cluster.service.ClusterService) NodeNotConnectedException(org.opensearch.transport.NodeNotConnectedException) TransportService(org.opensearch.transport.TransportService) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ClusterName(org.opensearch.cluster.ClusterName)

Example 19 with PlainActionFuture

use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.

the class AnomalyResultTests method testAllFeaturesDisabled.

@SuppressWarnings("unchecked")
public void testAllFeaturesDisabled() throws IOException {
    doAnswer(invocation -> {
        ActionListener<Optional<AnomalyDetector>> listener = invocation.getArgument(1);
        listener.onFailure(new EndRunException(adID, CommonErrorMessages.ALL_FEATURES_DISABLED_ERR_MSG, true));
        return null;
    }).when(stateManager).getAnomalyDetector(any(String.class), any(ActionListener.class));
    AnomalyResultTransportAction action = new AnomalyResultTransportAction(new ActionFilters(Collections.emptySet()), transportService, settings, client, stateManager, 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);
    assertException(listener, EndRunException.class, CommonErrorMessages.ALL_FEATURES_DISABLED_ERR_MSG);
}
Also used : EndRunException(org.opensearch.ad.common.exception.EndRunException) 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)

Example 20 with PlainActionFuture

use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.

the class RCFPollingTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    clusterService = mock(ClusterService.class);
    hashRing = mock(HashRing.class);
    transportAddress1 = new TransportAddress(new InetSocketAddress(InetAddress.getByName("1.2.3.4"), 9300));
    manager = mock(ModelManager.class);
    transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    future = new PlainActionFuture<>();
    request = new RCFPollingRequest(detectorId);
    model0Id = SingleStreamModelIdMapper.getRcfModelId(detectorId, 0);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        @SuppressWarnings("unchecked") ActionListener<Long> listener = (ActionListener<Long>) args[2];
        listener.onResponse(totalUpdates);
        return null;
    }).when(manager).getTotalUpdates(any(String.class), any(String.class), any());
    normalTransportInterceptor = new TransportInterceptor() {

        @Override
        public AsyncSender interceptSender(AsyncSender sender) {
            return new AsyncSender() {

                @Override
                public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) {
                    if (RCFPollingAction.NAME.equals(action)) {
                        sender.sendRequest(connection, action, request, options, rcfRollingHandler(handler));
                    } else {
                        sender.sendRequest(connection, action, request, options, handler);
                    }
                }
            };
        }
    };
    failureTransportInterceptor = new TransportInterceptor() {

        @Override
        public AsyncSender interceptSender(AsyncSender sender) {
            return new AsyncSender() {

                @Override
                public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) {
                    if (RCFPollingAction.NAME.equals(action)) {
                        sender.sendRequest(connection, action, request, options, rcfFailureRollingHandler(handler));
                    } else {
                        sender.sendRequest(connection, action, request, options, handler);
                    }
                }
            };
        }
    };
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) JsonDeserializer(test.org.opensearch.ad.util.JsonDeserializer) BeforeClass(org.junit.BeforeClass) ModelManager(org.opensearch.ad.ml.ModelManager) HashRing(org.opensearch.ad.cluster.HashRing) ToXContent(org.opensearch.common.xcontent.ToXContent) TransportInterceptor(org.opensearch.transport.TransportInterceptor) AbstractADTest(org.opensearch.ad.AbstractADTest) Version(org.opensearch.Version) JsonPathNotFoundException(org.opensearch.ad.common.exception.JsonPathNotFoundException) GsonBuilder(com.google.gson.GsonBuilder) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) InetAddress(java.net.InetAddress) Transport(org.opensearch.transport.Transport) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Gson(com.google.gson.Gson) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActionListener(org.opensearch.action.ActionListener) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) AfterClass(org.junit.AfterClass) CommonName(org.opensearch.ad.constant.CommonName) TransportRequest(org.opensearch.transport.TransportRequest) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) IOException(java.io.IOException) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) Task(org.opensearch.tasks.Task) TransportResponse(org.opensearch.transport.TransportResponse) InetSocketAddress(java.net.InetSocketAddress) TransportService(org.opensearch.transport.TransportService) TransportAddress(org.opensearch.common.transport.TransportAddress) ActionFilters(org.opensearch.action.support.ActionFilters) TestHelpers(org.opensearch.ad.TestHelpers) FakeNode(test.org.opensearch.ad.util.FakeNode) ClusterService(org.opensearch.cluster.service.ClusterService) Optional(java.util.Optional) SingleStreamModelIdMapper(org.opensearch.ad.ml.SingleStreamModelIdMapper) ConnectTransportException(org.opensearch.transport.ConnectTransportException) Collections(java.util.Collections) TransportException(org.opensearch.transport.TransportException) Mockito.mock(org.mockito.Mockito.mock) TransportInterceptor(org.opensearch.transport.TransportInterceptor) TransportAddress(org.opensearch.common.transport.TransportAddress) InetSocketAddress(java.net.InetSocketAddress) HashRing(org.opensearch.ad.cluster.HashRing) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) TransportRequest(org.opensearch.transport.TransportRequest) ModelManager(org.opensearch.ad.ml.ModelManager) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) Transport(org.opensearch.transport.Transport) Before(org.junit.Before)

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