Search in sources :

Example 6 with GetRequest

use of org.opensearch.action.get.GetRequest 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 7 with GetRequest

use of org.opensearch.action.get.GetRequest in project anomaly-detection by opensearch-project.

the class NodeStateManagerTests method setupCheckpoint.

@SuppressWarnings("unchecked")
private void setupCheckpoint(boolean responseExists) throws IOException {
    when(checkpointResponse.isExists()).thenReturn(responseExists);
    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 >= 2);
        GetRequest request = null;
        ActionListener<GetResponse> listener = null;
        if (args[0] instanceof GetRequest) {
            request = (GetRequest) args[0];
        }
        if (args[1] instanceof ActionListener) {
            listener = (ActionListener<GetResponse>) args[1];
        }
        assertTrue(request != null && listener != null);
        listener.onResponse(checkpointResponse);
        return null;
    }).when(client).get(any(), any(ActionListener.class));
}
Also used : ActionListener(org.opensearch.action.ActionListener) GetRequest(org.opensearch.action.get.GetRequest) GetResponse(org.opensearch.action.get.GetResponse)

Example 8 with GetRequest

use of org.opensearch.action.get.GetRequest 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 9 with GetRequest

use of org.opensearch.action.get.GetRequest 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)

Example 10 with GetRequest

use of org.opensearch.action.get.GetRequest in project anomaly-detection by opensearch-project.

the class AnomalyDetectorProfileRunnerTests method setUpClientGet.

/**
 * Convenience methods for single-stream detector profile tests set up
 * @param detectorStatus Detector config status
 * @param jobStatus Detector job status
 * @param rcfPollingStatus RCF polling result status
 * @param errorResultStatus Error result status
 * @throws IOException when failing the getting request
 */
@SuppressWarnings("unchecked")
private void setUpClientGet(DetectorStatus detectorStatus, JobStatus jobStatus, RCFPollingStatus rcfPollingStatus, ErrorResultStatus errorResultStatus) throws IOException {
    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];
        if (request.index().equals(ANOMALY_DETECTORS_INDEX)) {
            switch(detectorStatus) {
                case EXIST:
                    listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
                    break;
                case INDEX_NOT_EXIST:
                    listener.onFailure(new IndexNotFoundException(ANOMALY_DETECTORS_INDEX));
                    break;
                case NO_DOC:
                    when(detectorGetReponse.isExists()).thenReturn(false);
                    listener.onResponse(detectorGetReponse);
                    break;
                default:
                    assertTrue("should not reach here", false);
                    break;
            }
        } else if (request.index().equals(ANOMALY_DETECTOR_JOB_INDEX)) {
            AnomalyDetectorJob job = null;
            switch(jobStatus) {
                case INDEX_NOT_EXIT:
                    listener.onFailure(new IndexNotFoundException(ANOMALY_DETECTOR_JOB_INDEX));
                    break;
                case DISABLED:
                    job = TestHelpers.randomAnomalyDetectorJob(false, jobEnabledTime, null);
                    listener.onResponse(TestHelpers.createGetResponse(job, detector.getDetectorId(), AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX));
                    break;
                case ENABLED:
                    job = TestHelpers.randomAnomalyDetectorJob(true, jobEnabledTime, null);
                    listener.onResponse(TestHelpers.createGetResponse(job, detector.getDetectorId(), AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX));
                    break;
                default:
                    assertTrue("should not reach here", false);
                    break;
            }
        } else {
            if (errorResultStatus == ErrorResultStatus.INDEX_NOT_EXIT) {
                listener.onFailure(new IndexNotFoundException(CommonName.DETECTION_STATE_INDEX));
                return null;
            }
            DetectorInternalState.Builder result = new DetectorInternalState.Builder().lastUpdateTime(Instant.now());
            String error = getError(errorResultStatus);
            if (error != null) {
                result.error(error);
            }
            listener.onResponse(TestHelpers.createGetResponse(result.build(), detector.getDetectorId(), CommonName.DETECTION_STATE_INDEX));
        }
        return null;
    }).when(client).get(any(), any());
    setUpClientExecuteRCFPollingAction(rcfPollingStatus);
}
Also used : ActionListener(org.opensearch.action.ActionListener) GetRequest(org.opensearch.action.get.GetRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) GetResponse(org.opensearch.action.get.GetResponse)

Aggregations

GetRequest (org.opensearch.action.get.GetRequest)81 GetResponse (org.opensearch.action.get.GetResponse)50 ActionListener (org.opensearch.action.ActionListener)48 IOException (java.io.IOException)34 Client (org.opensearch.client.Client)32 IndexRequest (org.opensearch.action.index.IndexRequest)25 XContentParser (org.opensearch.common.xcontent.XContentParser)24 SearchRequest (org.opensearch.action.search.SearchRequest)22 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)21 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)21 LogManager (org.apache.logging.log4j.LogManager)20 Logger (org.apache.logging.log4j.Logger)20 AnomalyDetectorJob (org.opensearch.ad.model.AnomalyDetectorJob)19 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)19 List (java.util.List)18 XContentParserUtils.ensureExpectedToken (org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken)17 Optional (java.util.Optional)15 Set (java.util.Set)15 IntervalTimeConfiguration (org.opensearch.ad.model.IntervalTimeConfiguration)15 TransportService (org.opensearch.transport.TransportService)15