Search in sources :

Example 16 with Client

use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.

the class MultiEntityProfileRunnerTests method setUp.

@SuppressWarnings("unchecked")
@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    client = mock(Client.class);
    nodeFilter = mock(DiscoveryNodeFilterer.class);
    requiredSamples = 128;
    detectorId = "A69pa3UBHuCbh-emo9oR";
    detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList("a"));
    result = new DetectorInternalState.Builder().lastUpdateTime(Instant.now());
    job = TestHelpers.randomAnomalyDetectorJob(true);
    adTaskManager = mock(ADTaskManager.class);
    transportService = mock(TransportService.class);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        Consumer<Optional<ADTask>> function = (Consumer<Optional<ADTask>>) args[2];
        function.accept(Optional.of(TestHelpers.randomAdTask()));
        return null;
    }).when(adTaskManager).getAndExecuteOnLatestDetectorLevelTask(any(), any(), any(), any(), anyBoolean(), any());
    runner = new AnomalyDetectorProfileRunner(client, xContentRegistry(), nodeFilter, requiredSamples, transportService, adTaskManager);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        GetRequest request = (GetRequest) args[0];
        ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
        String indexName = request.index();
        if (indexName.equals(ANOMALY_DETECTORS_INDEX)) {
            listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
        } else if (indexName.equals(CommonName.DETECTION_STATE_INDEX)) {
            listener.onResponse(TestHelpers.createGetResponse(result.build(), detector.getDetectorId(), CommonName.DETECTION_STATE_INDEX));
        } else if (indexName.equals(ANOMALY_DETECTOR_JOB_INDEX)) {
            listener.onResponse(TestHelpers.createGetResponse(job, detector.getDetectorId(), AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX));
        }
        return null;
    }).when(client).get(any(), any());
    stateNError = new HashSet<DetectorProfileName>();
    stateNError.add(DetectorProfileName.ERROR);
    stateNError.add(DetectorProfileName.STATE);
}
Also used : DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) Optional(java.util.Optional) GetResponse(org.opensearch.action.get.GetResponse) Consumer(java.util.function.Consumer) ActionListener(org.opensearch.action.ActionListener) ADTaskManager(org.opensearch.ad.task.ADTaskManager) TransportService(org.opensearch.transport.TransportService) DetectorProfileName(org.opensearch.ad.model.DetectorProfileName) GetRequest(org.opensearch.action.get.GetRequest) ADTask(org.opensearch.ad.model.ADTask) Client(org.opensearch.client.Client) Before(org.junit.Before)

Example 17 with Client

use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.

the class NodeStateManagerTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    client = mock(Client.class);
    settings = Settings.builder().put("plugins.anomaly_detection.max_retry_for_unresponsive_node", 3).put("plugins.anomaly_detection.ad_mute_minutes", TimeValue.timeValueMinutes(10)).build();
    clock = mock(Clock.class);
    duration = Duration.ofHours(1);
    context = TestHelpers.createThreadPool();
    throttler = new Throttler(clock);
    clientUtil = new ClientUtil(Settings.EMPTY, client, throttler, mock(ThreadPool.class));
    Set<Setting<?>> nodestateSetting = new HashSet<>(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    nodestateSetting.add(MAX_RETRY_FOR_UNRESPONSIVE_NODE);
    nodestateSetting.add(BACKOFF_MINUTES);
    clusterSettings = new ClusterSettings(Settings.EMPTY, nodestateSetting);
    DiscoveryNode discoveryNode = new DiscoveryNode("node1", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
    clusterService = ClusterServiceUtils.createClusterService(threadPool, discoveryNode, clusterSettings);
    stateManager = new NodeStateManager(client, xContentRegistry(), settings, clientUtil, clock, duration, clusterService);
    checkpointResponse = mock(GetResponse.class);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClientUtil(org.opensearch.ad.util.ClientUtil) Setting(org.opensearch.common.settings.Setting) Client(org.opensearch.client.Client) Clock(java.time.Clock) GetResponse(org.opensearch.action.get.GetResponse) Throttler(org.opensearch.ad.util.Throttler) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 18 with Client

use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.

the class NodeStateManagerTests method testRepeatedGetAnomalyDetector.

/**
 * Test that we caches anomaly detector definition after the first call
 * @throws IOException if client throws exception
 * @throws InterruptedException  if the current thread is interrupted while waiting
 */
@SuppressWarnings("unchecked")
public void testRepeatedGetAnomalyDetector() throws IOException, InterruptedException {
    String detectorId = setupDetector();
    final CountDownLatch inProgressLatch = new CountDownLatch(2);
    stateManager.getAnomalyDetector(detectorId, ActionListener.wrap(asDetector -> {
        assertEquals(detectorToCheck, asDetector.get());
        inProgressLatch.countDown();
    }, exception -> {
        assertTrue(false);
        inProgressLatch.countDown();
    }));
    stateManager.getAnomalyDetector(detectorId, ActionListener.wrap(asDetector -> {
        assertEquals(detectorToCheck, asDetector.get());
        inProgressLatch.countDown();
    }, exception -> {
        assertTrue(false);
        inProgressLatch.countDown();
    }));
    assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
    verify(client, times(1)).get(any(), any(ActionListener.class));
}
Also used : IntStream(java.util.stream.IntStream) AnomalyResultTests(org.opensearch.ad.transport.AnomalyResultTests) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) ThreadPool(org.opensearch.threadpool.ThreadPool) Version(org.opensearch.Version) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) HashSet(java.util.HashSet) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) After(org.junit.After) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) ClusterSettings(org.opensearch.common.settings.ClusterSettings) GetResponse(org.opensearch.action.get.GetResponse) Before(org.junit.Before) BACKOFF_MINUTES(org.opensearch.ad.settings.AnomalyDetectorSettings.BACKOFF_MINUTES) AfterClass(org.junit.AfterClass) Client(org.opensearch.client.Client) Throttler(org.opensearch.ad.util.Throttler) Setting(org.opensearch.common.settings.Setting) TimeValue(org.opensearch.common.unit.TimeValue) ClientUtil(org.opensearch.ad.util.ClientUtil) MAX_RETRY_FOR_UNRESPONSIVE_NODE(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_RETRY_FOR_UNRESPONSIVE_NODE) ImmutableMap(com.google.common.collect.ImmutableMap) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) GetRequest(org.opensearch.action.get.GetRequest) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) Instant(java.time.Instant) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) Clock(java.time.Clock) Collections(java.util.Collections) ClusterServiceUtils(org.opensearch.test.ClusterServiceUtils) Mockito.mock(org.mockito.Mockito.mock) SearchModule(org.opensearch.search.SearchModule) ActionListener(org.opensearch.action.ActionListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 19 with Client

use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.

the class ADDataMigratorTests method testMigrateDataWithNormalJobResponse_ExistingDetector_ExistingInternalError.

public void testMigrateDataWithNormalJobResponse_ExistingDetector_ExistingInternalError() {
    when(detectionIndices.doesAnomalyDetectorJobIndexExist()).thenReturn(true);
    when(detectionIndices.doesDetectorStateIndexExist()).thenReturn(true);
    String detectorId = randomAlphaOfLength(10);
    doAnswer(invocation -> {
        // Return correct AD job when search job index
        ActionListener<SearchResponse> listener = invocation.getArgument(1);
        SearchHit job1 = SearchHit.fromXContent(TestHelpers.parser(jobContent));
        SearchHits searchHits = new SearchHits(new SearchHit[] { job1 }, new TotalHits(2, TotalHits.Relation.EQUAL_TO), Float.NaN);
        InternalSearchResponse response = new InternalSearchResponse(searchHits, InternalAggregations.EMPTY, null, null, false, null, 1);
        SearchResponse searchResponse = new SearchResponse(response, null, 1, 1, 0, 100, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
        listener.onResponse(searchResponse);
        return null;
    }).doAnswer(invocation -> {
        // Return null when search realtime tasks
        ActionListener<SearchResponse> listener = invocation.getArgument(1);
        listener.onResponse(null);
        return null;
    }).when(client).search(any(), any());
    doAnswer(invocation -> {
        // Return null when get detector internal error from index.
        ActionListener<GetResponse> listener = invocation.getArgument(1);
        XContentParser parser = TestHelpers.parser(internalError, false);
        GetResponse getResponse = GetResponse.fromXContent(parser);
        listener.onResponse(getResponse);
        return null;
    }).doAnswer(invocation -> {
        // Return correct detector when get detector index.
        ActionListener<GetResponse> listener = invocation.getArgument(1);
        XContentParser parser = TestHelpers.parser(detectorContent, false);
        GetResponse getResponse = GetResponse.fromXContent(parser);
        listener.onResponse(getResponse);
        return null;
    }).when(client).get(any(), any());
    doAnswer(invocation -> {
        ActionListener<IndexResponse> listener = invocation.getArgument(1);
        String taskId = randomAlphaOfLength(5);
        IndexResponse indexResponse = IndexResponse.fromXContent(TestHelpers.parser(indexResponseContent, false));
        listener.onResponse(indexResponse);
        return null;
    }).when(client).index(any(), any());
    DiscoveryNode localNode = createNode("localNodeId");
    doReturn(localNode).when(clusterService).localNode();
    adDataMigrator.migrateData();
    verify(adDataMigrator, times(2)).backfillRealtimeTask(any(), anyBoolean());
    verify(client, times(1)).index(any(), any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IndexResponse(org.opensearch.action.index.IndexResponse) SearchHits(org.opensearch.search.SearchHits) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) XContentParser(org.opensearch.common.xcontent.XContentParser) ADUnitTestCase(org.opensearch.ad.ADUnitTestCase) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ANOMALY_DETECTOR_JOB_INDEX(org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) Mockito.doReturn(org.mockito.Mockito.doReturn) GetResponse(org.opensearch.action.get.GetResponse) Before(org.junit.Before) Client(org.opensearch.client.Client) SearchHit(org.opensearch.search.SearchHit) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Mockito.times(org.mockito.Mockito.times) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) Mockito.when(org.mockito.Mockito.when) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) DETECTION_STATE_INDEX(org.opensearch.ad.constant.CommonName.DETECTION_STATE_INDEX) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) Mockito.verify(org.mockito.Mockito.verify) TotalHits(org.apache.lucene.search.TotalHits) ShardId(org.opensearch.index.shard.ShardId) Mockito.never(org.mockito.Mockito.never) TestHelpers(org.opensearch.ad.TestHelpers) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) NoShardAvailableActionException(org.opensearch.action.NoShardAvailableActionException) Mockito.mock(org.mockito.Mockito.mock) TotalHits(org.apache.lucene.search.TotalHits) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) SearchHit(org.opensearch.search.SearchHit) GetResponse(org.opensearch.action.get.GetResponse) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) ActionListener(org.opensearch.action.ActionListener) IndexResponse(org.opensearch.action.index.IndexResponse) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) SearchHits(org.opensearch.search.SearchHits) XContentParser(org.opensearch.common.xcontent.XContentParser) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 20 with Client

use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.

the class ADDataMigratorTests method testMigrateDataWithNormalJobResponseButMissingDetector.

public void testMigrateDataWithNormalJobResponseButMissingDetector() {
    when(detectionIndices.doesAnomalyDetectorJobIndexExist()).thenReturn(true);
    when(detectionIndices.doesDetectorStateIndexExist()).thenReturn(true);
    doAnswer(invocation -> {
        // Return correct AD job when search job index
        ActionListener<SearchResponse> listener = invocation.getArgument(1);
        String detectorId = randomAlphaOfLength(10);
        SearchHit job = SearchHit.fromXContent(TestHelpers.parser(jobContent));
        SearchHits searchHits = new SearchHits(new SearchHit[] { job }, new TotalHits(2, TotalHits.Relation.EQUAL_TO), Float.NaN);
        InternalSearchResponse response = new InternalSearchResponse(searchHits, InternalAggregations.EMPTY, null, null, false, null, 1);
        SearchResponse searchResponse = new SearchResponse(response, null, 1, 1, 0, 100, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
        listener.onResponse(searchResponse);
        return null;
    }).doAnswer(invocation -> {
        // Return null when search realtime tasks
        ActionListener<SearchResponse> listener = invocation.getArgument(1);
        listener.onResponse(null);
        return null;
    }).when(client).search(any(), any());
    // Return null when get detector and internal error from index.
    doAnswer(invocation -> {
        ActionListener<SearchResponse> listener = invocation.getArgument(1);
        listener.onResponse(null);
        return null;
    }).when(client).get(any(), any());
    adDataMigrator.migrateData();
    verify(adDataMigrator, times(2)).backfillRealtimeTask(any(), anyBoolean());
    verify(client, never()).index(any(), any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IndexResponse(org.opensearch.action.index.IndexResponse) SearchHits(org.opensearch.search.SearchHits) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) XContentParser(org.opensearch.common.xcontent.XContentParser) ADUnitTestCase(org.opensearch.ad.ADUnitTestCase) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ANOMALY_DETECTOR_JOB_INDEX(org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX) Mockito.doAnswer(org.mockito.Mockito.doAnswer) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) Mockito.doReturn(org.mockito.Mockito.doReturn) GetResponse(org.opensearch.action.get.GetResponse) Before(org.junit.Before) Client(org.opensearch.client.Client) SearchHit(org.opensearch.search.SearchHit) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Mockito.times(org.mockito.Mockito.times) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) Mockito.when(org.mockito.Mockito.when) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) DETECTION_STATE_INDEX(org.opensearch.ad.constant.CommonName.DETECTION_STATE_INDEX) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) Mockito.verify(org.mockito.Mockito.verify) TotalHits(org.apache.lucene.search.TotalHits) ShardId(org.opensearch.index.shard.ShardId) Mockito.never(org.mockito.Mockito.never) TestHelpers(org.opensearch.ad.TestHelpers) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) NoShardAvailableActionException(org.opensearch.action.NoShardAvailableActionException) Mockito.mock(org.mockito.Mockito.mock) TotalHits(org.apache.lucene.search.TotalHits) ActionListener(org.opensearch.action.ActionListener) SearchHit(org.opensearch.search.SearchHit) SearchHits(org.opensearch.search.SearchHits) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Aggregations

Client (org.opensearch.client.Client)324 Settings (org.opensearch.common.settings.Settings)130 Test (org.junit.Test)90 IndexRequest (org.opensearch.action.index.IndexRequest)86 RestHelper (org.opensearch.security.test.helper.rest.RestHelper)69 ClusterService (org.opensearch.cluster.service.ClusterService)65 ArrayList (java.util.ArrayList)60 ActionListener (org.opensearch.action.ActionListener)59 SingleClusterTest (org.opensearch.security.test.SingleClusterTest)57 HttpResponse (org.opensearch.security.test.helper.rest.RestHelper.HttpResponse)57 List (java.util.List)53 Matchers.containsString (org.hamcrest.Matchers.containsString)51 SearchResponse (org.opensearch.action.search.SearchResponse)49 IOException (java.io.IOException)48 Map (java.util.Map)48 ThreadPool (org.opensearch.threadpool.ThreadPool)48 CreateIndexRequest (org.opensearch.action.admin.indices.create.CreateIndexRequest)46 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)43 HashSet (java.util.HashSet)39 GetResponse (org.opensearch.action.get.GetResponse)38