Search in sources :

Example 21 with Client

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

the class HashRingTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    localNodeId = "localNode";
    localNode = createNode(localNodeId, "127.0.0.1", 9200, emptyMap());
    newNodeId = "newNode";
    newNode = createNode(newNodeId, "127.0.0.2", 9201, emptyMap());
    warmNodeId = "warmNode";
    warmNode = createNode(warmNodeId, "127.0.0.3", 9202, ImmutableMap.of(CommonName.BOX_TYPE_KEY, CommonName.WARM_BOX_TYPE));
    settings = Settings.builder().put(COOLDOWN_MINUTES.getKey(), TimeValue.timeValueSeconds(5)).build();
    ClusterSettings clusterSettings = clusterSetting(settings, COOLDOWN_MINUTES);
    clusterService = spy(new ClusterService(settings, clusterSettings, null));
    nodeFilter = spy(new DiscoveryNodeFilterer(clusterService));
    client = mock(Client.class);
    dataMigrator = mock(ADDataMigrator.class);
    clock = mock(Clock.class);
    when(clock.millis()).thenReturn(700000L);
    delta = mock(DiscoveryNodes.Delta.class);
    adminClient = mock(AdminClient.class);
    when(client.admin()).thenReturn(adminClient);
    clusterAdminClient = mock(ClusterAdminClient.class);
    when(adminClient.cluster()).thenReturn(clusterAdminClient);
    String modelId = "123_model_threshold";
    modelManager = mock(ModelManager.class);
    doAnswer(invocation -> {
        Set<String> res = new HashSet<>();
        res.add(modelId);
        return res;
    }).when(modelManager).getAllModelIds();
    hashRing = spy(new HashRing(nodeFilter, clock, settings, client, clusterService, dataMigrator, modelManager));
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) Clock(java.time.Clock) ModelManager(org.opensearch.ad.ml.ModelManager) ClusterService(org.opensearch.cluster.service.ClusterService) Client(org.opensearch.client.Client) AdminClient(org.opensearch.client.AdminClient) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) AdminClient(org.opensearch.client.AdminClient) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 22 with Client

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

the class InitAnomalyDetectionIndicesTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    client = mock(Client.class);
    indicesClient = mock(IndicesAdminClient.class);
    AdminClient adminClient = mock(AdminClient.class);
    when(client.admin()).thenReturn(adminClient);
    when(adminClient.indices()).thenReturn(indicesClient);
    clusterService = mock(ClusterService.class);
    threadPool = mock(ThreadPool.class);
    numberOfHotNodes = 4;
    nodeFilter = mock(DiscoveryNodeFilterer.class);
    when(nodeFilter.getNumberOfEligibleDataNodes()).thenReturn(numberOfHotNodes);
    Settings settings = Settings.EMPTY;
    ClusterSettings clusterSettings = new ClusterSettings(settings, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.AD_RESULT_HISTORY_MAX_DOCS_PER_SHARD, AnomalyDetectorSettings.AD_RESULT_HISTORY_ROLLOVER_PERIOD, AnomalyDetectorSettings.AD_RESULT_HISTORY_RETENTION_PERIOD, AnomalyDetectorSettings.MAX_PRIMARY_SHARDS))));
    clusterName = new ClusterName("test");
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    clusterState = ClusterState.builder(clusterName).metadata(Metadata.builder().build()).build();
    when(clusterService.state()).thenReturn(clusterState);
    adIndices = new AnomalyDetectionIndices(client, clusterService, threadPool, settings, nodeFilter, AnomalyDetectorSettings.MAX_UPDATE_RETRY_TIMES);
}
Also used : ClusterService(org.opensearch.cluster.service.ClusterService) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ThreadPool(org.opensearch.threadpool.ThreadPool) ClusterName(org.opensearch.cluster.ClusterName) IndicesAdminClient(org.opensearch.client.IndicesAdminClient) Client(org.opensearch.client.Client) AdminClient(org.opensearch.client.AdminClient) IndicesAdminClient(org.opensearch.client.IndicesAdminClient) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) AdminClient(org.opensearch.client.AdminClient) IndicesAdminClient(org.opensearch.client.IndicesAdminClient) HashSet(java.util.HashSet)

Example 23 with Client

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

the class HourlyCronTests method templateHourlyCron.

@SuppressWarnings("unchecked")
public void templateHourlyCron(HourlyCronTestExecutionMode mode) {
    super.setUpLog4jForJUnit(HourlyCron.class);
    ClusterService clusterService = mock(ClusterService.class);
    ClusterState state = ClusterCreation.state(1);
    when(clusterService.state()).thenReturn(state);
    HashMap<String, String> ignoredAttributes = new HashMap<String, String>();
    ignoredAttributes.put(CommonName.BOX_TYPE_KEY, CommonName.WARM_BOX_TYPE);
    DiscoveryNodeFilterer nodeFilter = new DiscoveryNodeFilterer(clusterService);
    Client client = mock(Client.class);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        assertTrue(String.format("The size of args is %d.  Its content is %s", args.length, Arrays.toString(args)), args.length == 3);
        assertTrue(args[2] instanceof ActionListener);
        ActionListener<CronResponse> listener = (ActionListener<CronResponse>) args[2];
        if (mode == HourlyCronTestExecutionMode.NODE_FAIL) {
            listener.onResponse(new CronResponse(new ClusterName("test"), Collections.singletonList(new CronNodeResponse(state.nodes().getLocalNode())), Collections.singletonList(new FailedNodeException("foo0", "blah", new OpenSearchException("bar")))));
        } else if (mode == HourlyCronTestExecutionMode.ALL_FAIL) {
            listener.onFailure(new OpenSearchException("bar"));
        } else {
            CronNodeResponse nodeResponse = new CronNodeResponse(state.nodes().getLocalNode());
            BytesStreamOutput nodeResponseOut = new BytesStreamOutput();
            nodeResponseOut.setVersion(Version.CURRENT);
            nodeResponse.writeTo(nodeResponseOut);
            StreamInput siNode = nodeResponseOut.bytes().streamInput();
            CronNodeResponse nodeResponseRead = new CronNodeResponse(siNode);
            CronResponse response = new CronResponse(new ClusterName("test"), Collections.singletonList(nodeResponseRead), Collections.EMPTY_LIST);
            BytesStreamOutput out = new BytesStreamOutput();
            out.setVersion(Version.CURRENT);
            response.writeTo(out);
            StreamInput si = out.bytes().streamInput();
            CronResponse responseRead = new CronResponse(si);
            listener.onResponse(responseRead);
        }
        return null;
    }).when(client).execute(eq(CronAction.INSTANCE), any(), any());
    HourlyCron cron = new HourlyCron(client, nodeFilter);
    cron.run();
    Logger LOG = LogManager.getLogger(HourlyCron.class);
    LOG.info(testAppender.messages);
    if (mode == HourlyCronTestExecutionMode.NODE_FAIL) {
        assertTrue(testAppender.containsMessage(HourlyCron.NODE_EXCEPTION_LOG_MSG));
    } else if (mode == HourlyCronTestExecutionMode.ALL_FAIL) {
        assertTrue(testAppender.containsMessage(HourlyCron.EXCEPTION_LOG_MSG));
    } else {
        assertTrue(testAppender.containsMessage(HourlyCron.SUCCEEDS_LOG_MSG));
    }
    super.tearDownLog4jForJUnit();
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) HashMap(java.util.HashMap) CronResponse(org.opensearch.ad.transport.CronResponse) Logger(org.apache.logging.log4j.Logger) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) CronNodeResponse(org.opensearch.ad.transport.CronNodeResponse) StreamInput(org.opensearch.common.io.stream.StreamInput) ClusterName(org.opensearch.cluster.ClusterName) FailedNodeException(org.opensearch.action.FailedNodeException) OpenSearchException(org.opensearch.OpenSearchException) Client(org.opensearch.client.Client)

Example 24 with Client

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

the class EntityProfileRunnerTests method testNotMultiEntityDetector.

@SuppressWarnings("unchecked")
public void testNotMultiEntityDetector() throws IOException, InterruptedException {
    detector = TestHelpers.randomAnomalyDetectorWithInterval(new IntervalTimeConfiguration(detectorIntervalMin, ChronoUnit.MINUTES));
    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));
        }
        return null;
    }).when(client).get(any(), any());
    final CountDownLatch inProgressLatch = new CountDownLatch(1);
    runner.profile(detectorId, entity, state, ActionListener.wrap(response -> {
        assertTrue("Should not reach here", false);
        inProgressLatch.countDown();
    }, exception -> {
        assertTrue(exception.getMessage().contains(EntityProfileRunner.NOT_HC_DETECTOR_ERR_MSG));
        inProgressLatch.countDown();
    }));
    assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) ModelProfile(org.opensearch.ad.model.ModelProfile) DocValueFormat(org.opensearch.search.DocValueFormat) SearchHits(org.opensearch.search.SearchHits) EntityProfileName(org.opensearch.ad.model.EntityProfileName) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) HashSet(java.util.HashSet) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) ANOMALY_DETECTOR_JOB_INDEX(org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse) Mockito.doAnswer(org.mockito.Mockito.doAnswer) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) GetResponse(org.opensearch.action.get.GetResponse) EntityProfile(org.opensearch.ad.model.EntityProfile) Collections.emptyMap(java.util.Collections.emptyMap) Client(org.opensearch.client.Client) CommonName(org.opensearch.ad.constant.CommonName) InitProgressProfile(org.opensearch.ad.model.InitProgressProfile) SearchHit(org.opensearch.search.SearchHit) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) Set(java.util.Set) IOException(java.io.IOException) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) TimeUnit(java.util.concurrent.TimeUnit) TotalHits(org.apache.lucene.search.TotalHits) CountDownLatch(java.util.concurrent.CountDownLatch) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) ChronoUnit(java.time.temporal.ChronoUnit) Entity(org.opensearch.ad.model.Entity) SearchResponseSections(org.opensearch.action.search.SearchResponseSections) EntityState(org.opensearch.ad.model.EntityState) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) Collections(java.util.Collections) EntityProfileAction(org.opensearch.ad.transport.EntityProfileAction) Text(org.opensearch.common.text.Text) Mockito.mock(org.mockito.Mockito.mock) ActionListener(org.opensearch.action.ActionListener) GetRequest(org.opensearch.action.get.GetRequest) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) GetResponse(org.opensearch.action.get.GetResponse)

Example 25 with Client

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

the class EntityProfileRunnerTests method testJobIndexNotFound.

@SuppressWarnings("unchecked")
public void testJobIndexNotFound() throws InterruptedException {
    setUpExecuteEntityProfileAction(InittedEverResultStatus.INITTED);
    final CountDownLatch inProgressLatch = new CountDownLatch(1);
    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(ANOMALY_DETECTOR_JOB_INDEX)) {
            listener.onFailure(new IndexNotFoundException(ANOMALY_DETECTOR_JOB_INDEX));
        }
        return null;
    }).when(client).get(any(), any());
    EntityProfile expectedProfile = new EntityProfile.Builder().build();
    runner.profile(detectorId, entity, initNInfo, ActionListener.wrap(response -> {
        assertEquals(expectedProfile, response);
        inProgressLatch.countDown();
    }, exception -> {
        LOG.error("Unexpected error", exception);
        assertTrue("Should not reach here", false);
        inProgressLatch.countDown();
    }));
    assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) ModelProfile(org.opensearch.ad.model.ModelProfile) DocValueFormat(org.opensearch.search.DocValueFormat) SearchHits(org.opensearch.search.SearchHits) EntityProfileName(org.opensearch.ad.model.EntityProfileName) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) HashSet(java.util.HashSet) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) ANOMALY_DETECTOR_JOB_INDEX(org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse) Mockito.doAnswer(org.mockito.Mockito.doAnswer) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) GetResponse(org.opensearch.action.get.GetResponse) EntityProfile(org.opensearch.ad.model.EntityProfile) Collections.emptyMap(java.util.Collections.emptyMap) Client(org.opensearch.client.Client) CommonName(org.opensearch.ad.constant.CommonName) InitProgressProfile(org.opensearch.ad.model.InitProgressProfile) SearchHit(org.opensearch.search.SearchHit) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) Set(java.util.Set) IOException(java.io.IOException) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) TimeUnit(java.util.concurrent.TimeUnit) TotalHits(org.apache.lucene.search.TotalHits) CountDownLatch(java.util.concurrent.CountDownLatch) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) ChronoUnit(java.time.temporal.ChronoUnit) Entity(org.opensearch.ad.model.Entity) SearchResponseSections(org.opensearch.action.search.SearchResponseSections) EntityState(org.opensearch.ad.model.EntityState) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) Collections(java.util.Collections) EntityProfileAction(org.opensearch.ad.transport.EntityProfileAction) Text(org.opensearch.common.text.Text) Mockito.mock(org.mockito.Mockito.mock) ActionListener(org.opensearch.action.ActionListener) GetRequest(org.opensearch.action.get.GetRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) EntityProfile(org.opensearch.ad.model.EntityProfile) CountDownLatch(java.util.concurrent.CountDownLatch) GetResponse(org.opensearch.action.get.GetResponse)

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