Search in sources :

Example 6 with NodeStateManager

use of org.opensearch.ad.NodeStateManager 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 7 with NodeStateManager

use of org.opensearch.ad.NodeStateManager in project anomaly-detection by opensearch-project.

the class DeleteModelTransportActionTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    ThreadPool threadPool = mock(ThreadPool.class);
    ClusterService clusterService = mock(ClusterService.class);
    localNodeID = "foo";
    when(clusterService.localNode()).thenReturn(new DiscoveryNode(localNodeID, buildNewFakeTransportAddress(), Version.CURRENT));
    when(clusterService.getClusterName()).thenReturn(new ClusterName("test"));
    TransportService transportService = mock(TransportService.class);
    ActionFilters actionFilters = mock(ActionFilters.class);
    NodeStateManager nodeStateManager = mock(NodeStateManager.class);
    ModelManager modelManager = mock(ModelManager.class);
    FeatureManager featureManager = mock(FeatureManager.class);
    CacheProvider cacheProvider = mock(CacheProvider.class);
    EntityCache entityCache = mock(EntityCache.class);
    when(cacheProvider.get()).thenReturn(entityCache);
    ADTaskCacheManager adTaskCacheManager = mock(ADTaskCacheManager.class);
    NodeStateManager stateManager = mock(NodeStateManager.class);
    action = new DeleteModelTransportAction(threadPool, clusterService, transportService, actionFilters, nodeStateManager, modelManager, featureManager, cacheProvider, adTaskCacheManager);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) EntityCache(org.opensearch.ad.caching.EntityCache) ThreadPool(org.opensearch.threadpool.ThreadPool) ActionFilters(org.opensearch.action.support.ActionFilters) ModelManager(org.opensearch.ad.ml.ModelManager) CacheProvider(org.opensearch.ad.caching.CacheProvider) NodeStateManager(org.opensearch.ad.NodeStateManager) ClusterService(org.opensearch.cluster.service.ClusterService) ADTaskCacheManager(org.opensearch.ad.task.ADTaskCacheManager) TransportService(org.opensearch.transport.TransportService) ClusterName(org.opensearch.cluster.ClusterName) FeatureManager(org.opensearch.ad.feature.FeatureManager) Before(org.junit.Before)

Example 8 with NodeStateManager

use of org.opensearch.ad.NodeStateManager in project anomaly-detection by opensearch-project.

the class MultiEntityResultTests method setUpNormlaStateManager.

@SuppressWarnings("unchecked")
public void setUpNormlaStateManager() throws IOException {
    ClientUtil clientUtil = mock(ClientUtil.class);
    AnomalyDetector detector = TestHelpers.AnomalyDetectorBuilder.newInstance().setDetectionInterval(new IntervalTimeConfiguration(1, ChronoUnit.MINUTES)).setCategoryFields(ImmutableList.of(randomAlphaOfLength(5))).build();
    doAnswer(invocation -> {
        ActionListener<GetResponse> listener = invocation.getArgument(2);
        listener.onResponse(TestHelpers.createGetResponse(detector, detectorId, AnomalyDetector.ANOMALY_DETECTORS_INDEX));
        return null;
    }).when(clientUtil).asyncRequest(any(GetRequest.class), any(), any(ActionListener.class));
    stateManager = new NodeStateManager(client, xContentRegistry(), settings, clientUtil, clock, AnomalyDetectorSettings.HOURLY_MAINTENANCE, clusterService);
    action = new AnomalyResultTransportAction(new ActionFilters(Collections.emptySet()), transportService, settings, client, stateManager, featureQuery, normalModelManager, hashRing, clusterService, indexNameResolver, adCircuitBreakerService, adStats, mockThreadPool, xContentRegistry(), adTaskManager);
}
Also used : ActionListener(org.opensearch.action.ActionListener) ClientUtil(org.opensearch.ad.util.ClientUtil) GetRequest(org.opensearch.action.get.GetRequest) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) ActionFilters(org.opensearch.action.support.ActionFilters) GetResponse(org.opensearch.action.get.GetResponse) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) NodeStateManager(org.opensearch.ad.NodeStateManager)

Example 9 with NodeStateManager

use of org.opensearch.ad.NodeStateManager in project anomaly-detection by opensearch-project.

the class MultiEntityResultTests method testEndRunNowFalseInModelNode.

public void testEndRunNowFalseInModelNode() 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"), false)));
    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 = false, the normal workflow continues
    verify(resultWriteQueue, times(3)).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 : EndRunException(org.opensearch.ad.common.exception.EndRunException) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) CountDownLatch(java.util.concurrent.CountDownLatch) NoSuchElementException(java.util.NoSuchElementException) 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 NodeStateManager

use of org.opensearch.ad.NodeStateManager in project anomaly-detection by opensearch-project.

the class MultiEntityResultTests method testCircuitBreakerOpen.

@SuppressWarnings("unchecked")
public void testCircuitBreakerOpen() throws InterruptedException, IOException {
    ClientUtil clientUtil = mock(ClientUtil.class);
    doAnswer(invocation -> {
        ActionListener<GetResponse> listener = invocation.getArgument(2);
        listener.onResponse(TestHelpers.createGetResponse(detector, detectorId, AnomalyDetector.ANOMALY_DETECTORS_INDEX));
        return null;
    }).when(clientUtil).asyncRequest(any(GetRequest.class), any(), any(ActionListener.class));
    stateManager = new NodeStateManager(client, xContentRegistry(), settings, clientUtil, clock, AnomalyDetectorSettings.HOURLY_MAINTENANCE, clusterService);
    action = new AnomalyResultTransportAction(new ActionFilters(Collections.emptySet()), transportService, settings, client, stateManager, featureQuery, normalModelManager, hashRing, clusterService, indexNameResolver, adCircuitBreakerService, adStats, mockThreadPool, xContentRegistry(), adTaskManager);
    CountDownLatch inProgress = setUpSearchResponse();
    setUpTransportInterceptor(this::entityResultHandler);
    // 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()));
    ADCircuitBreakerService openBreaker = mock(ADCircuitBreakerService.class);
    when(openBreaker.isOpen()).thenReturn(true);
    // register entity result action
    new EntityResultTransportAction(new ActionFilters(Collections.emptySet()), // since we send requests to testNodes[1]
    testNodes[1].transportService, normalModelManager, openBreaker, provider, stateManager, indexUtil, resultWriteQueue, checkpointReadQueue, coldEntityQueue, threadPool);
    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));
    listener = new PlainActionFuture<>();
    action.doExecute(null, request, listener);
    assertException(listener, LimitExceededException.class, CommonErrorMessages.MEMORY_CIRCUIT_BROKEN_ERR_MSG);
}
Also used : ADCircuitBreakerService(org.opensearch.ad.breaker.ADCircuitBreakerService) ClientUtil(org.opensearch.ad.util.ClientUtil) ActionFilters(org.opensearch.action.support.ActionFilters) Matchers.containsString(org.hamcrest.Matchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) GetResponse(org.opensearch.action.get.GetResponse) NodeStateManager(org.opensearch.ad.NodeStateManager) ActionListener(org.opensearch.action.ActionListener) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) GetRequest(org.opensearch.action.get.GetRequest)

Aggregations

NodeStateManager (org.opensearch.ad.NodeStateManager)12 CountDownLatch (java.util.concurrent.CountDownLatch)7 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)6 ActionListener (org.opensearch.action.ActionListener)5 ActionFilters (org.opensearch.action.support.ActionFilters)5 NoSuchElementException (java.util.NoSuchElementException)4 EndRunException (org.opensearch.ad.common.exception.EndRunException)4 FeatureManager (org.opensearch.ad.feature.FeatureManager)4 IOException (java.io.IOException)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 OpenSearchTimeoutException (org.opensearch.OpenSearchTimeoutException)3 GetRequest (org.opensearch.action.get.GetRequest)3 GetResponse (org.opensearch.action.get.GetResponse)3 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)3 LimitExceededException (org.opensearch.ad.common.exception.LimitExceededException)3 ClientUtil (org.opensearch.ad.util.ClientUtil)3 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)3 ClusterService (org.opensearch.cluster.service.ClusterService)3 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)3