Search in sources :

Example 1 with DetectorState

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

the class AnomalyDetectorProfileRunnerTests method testInitOrRunningStateTemplate.

public void testInitOrRunningStateTemplate(RCFPollingStatus status, DetectorState expectedState) throws IOException, InterruptedException {
    setUpClientGet(DetectorStatus.EXIST, JobStatus.ENABLED, status, ErrorResultStatus.NO_ERROR);
    DetectorProfile expectedProfile = new DetectorProfile.Builder().state(expectedState).build();
    final CountDownLatch inProgressLatch = new CountDownLatch(1);
    runner.profile(detector.getDetectorId(), ActionListener.wrap(response -> {
        assertEquals(expectedProfile, response);
        inProgressLatch.countDown();
    }, exception -> {
        logger.error(exception);
        for (StackTraceElement ste : exception.getStackTrace()) {
            logger.info(ste);
        }
        assertTrue("Should not reach here ", false);
        inProgressLatch.countDown();
    }), stateOnly);
    assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) ResourceNotFoundException(org.opensearch.ad.common.exception.ResourceNotFoundException) Version(org.opensearch.Version) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) 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) Map(java.util.Map) RCFPollingAction(org.opensearch.ad.transport.RCFPollingAction) ActionListener(org.opensearch.action.ActionListener) GetResponse(org.opensearch.action.get.GetResponse) ProfileNodeResponse(org.opensearch.ad.transport.ProfileNodeResponse) DetectorProfile(org.opensearch.ad.model.DetectorProfile) RemoteTransportException(org.opensearch.transport.RemoteTransportException) Set(java.util.Set) Instant(java.time.Instant) TransportAddress(org.opensearch.common.transport.TransportAddress) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) DetectorState(org.opensearch.ad.model.DetectorState) DetectorInternalState(org.opensearch.ad.model.DetectorInternalState) Optional(java.util.Optional) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) NotSerializableExceptionWrapper(org.opensearch.common.io.stream.NotSerializableExceptionWrapper) ADTask(org.opensearch.ad.model.ADTask) HashMap(java.util.HashMap) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) ArrayList(java.util.ArrayList) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) ProfileResponse(org.opensearch.ad.transport.ProfileResponse) Collections.emptyMap(java.util.Collections.emptyMap) CommonName(org.opensearch.ad.constant.CommonName) Collections.emptySet(java.util.Collections.emptySet) FailedNodeException(org.opensearch.action.FailedNodeException) InitProgressProfile(org.opensearch.ad.model.InitProgressProfile) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) DetectorProfileName(org.opensearch.ad.model.DetectorProfileName) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) RCFPollingResponse(org.opensearch.ad.transport.RCFPollingResponse) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) ChronoUnit(java.time.temporal.ChronoUnit) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) ClusterName(org.opensearch.cluster.ClusterName) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) Collections(java.util.Collections) ProfileAction(org.opensearch.ad.transport.ProfileAction) DetectorProfile(org.opensearch.ad.model.DetectorProfile) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with DetectorState

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

the class AnomalyDetectorProfileRunnerTests method testErrorStateTemplate.

@SuppressWarnings("unchecked")
public void testErrorStateTemplate(RCFPollingStatus initStatus, ErrorResultStatus status, DetectorState state, String error, JobStatus jobStatus, Set<DetectorProfileName> profilesToCollect) throws IOException, InterruptedException {
    ADTask adTask = TestHelpers.randomAdTask();
    adTask.setError(getError(status));
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        Consumer<Optional<ADTask>> function = (Consumer<Optional<ADTask>>) args[2];
        function.accept(Optional.of(adTask));
        return null;
    }).when(adTaskManager).getAndExecuteOnLatestDetectorLevelTask(any(), any(), any(), any(), anyBoolean(), any());
    setUpClientExecuteRCFPollingAction(initStatus);
    setUpClientGet(DetectorStatus.EXIST, jobStatus, initStatus, status);
    DetectorProfile.Builder builder = new DetectorProfile.Builder();
    if (profilesToCollect.contains(DetectorProfileName.STATE)) {
        builder.state(state);
    }
    if (profilesToCollect.contains(DetectorProfileName.ERROR)) {
        builder.error(error);
    }
    DetectorProfile expectedProfile = builder.build();
    final CountDownLatch inProgressLatch = new CountDownLatch(1);
    runner.profile(detector.getDetectorId(), ActionListener.wrap(response -> {
        assertEquals(expectedProfile, response);
        inProgressLatch.countDown();
    }, exception -> {
        logger.info(exception);
        for (StackTraceElement ste : exception.getStackTrace()) {
            logger.info(ste);
        }
        assertTrue("Should not reach here", false);
        inProgressLatch.countDown();
    }), profilesToCollect);
    assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) ResourceNotFoundException(org.opensearch.ad.common.exception.ResourceNotFoundException) Version(org.opensearch.Version) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) 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) Map(java.util.Map) RCFPollingAction(org.opensearch.ad.transport.RCFPollingAction) ActionListener(org.opensearch.action.ActionListener) GetResponse(org.opensearch.action.get.GetResponse) ProfileNodeResponse(org.opensearch.ad.transport.ProfileNodeResponse) DetectorProfile(org.opensearch.ad.model.DetectorProfile) RemoteTransportException(org.opensearch.transport.RemoteTransportException) Set(java.util.Set) Instant(java.time.Instant) TransportAddress(org.opensearch.common.transport.TransportAddress) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) DetectorState(org.opensearch.ad.model.DetectorState) DetectorInternalState(org.opensearch.ad.model.DetectorInternalState) Optional(java.util.Optional) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) NotSerializableExceptionWrapper(org.opensearch.common.io.stream.NotSerializableExceptionWrapper) ADTask(org.opensearch.ad.model.ADTask) HashMap(java.util.HashMap) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) ArrayList(java.util.ArrayList) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) ProfileResponse(org.opensearch.ad.transport.ProfileResponse) Collections.emptyMap(java.util.Collections.emptyMap) CommonName(org.opensearch.ad.constant.CommonName) Collections.emptySet(java.util.Collections.emptySet) FailedNodeException(org.opensearch.action.FailedNodeException) InitProgressProfile(org.opensearch.ad.model.InitProgressProfile) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) DetectorProfileName(org.opensearch.ad.model.DetectorProfileName) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) RCFPollingResponse(org.opensearch.ad.transport.RCFPollingResponse) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) ChronoUnit(java.time.temporal.ChronoUnit) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) ClusterName(org.opensearch.cluster.ClusterName) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) Collections(java.util.Collections) ProfileAction(org.opensearch.ad.transport.ProfileAction) Optional(java.util.Optional) Consumer(java.util.function.Consumer) ADTask(org.opensearch.ad.model.ADTask) DetectorProfile(org.opensearch.ad.model.DetectorProfile) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

IOException (java.io.IOException)2 Instant (java.time.Instant)2 ChronoUnit (java.time.temporal.ChronoUnit)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Collections.emptyMap (java.util.Collections.emptyMap)2 Collections.emptySet (java.util.Collections.emptySet)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2 Consumer (java.util.function.Consumer)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 ArgumentMatchers.anyBoolean (org.mockito.ArgumentMatchers.anyBoolean)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 Mockito.when (org.mockito.Mockito.when)2