Search in sources :

Example 16 with ADTask

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

the class AnomalyDetectorJobTransportActionTests method testStopRealtimeDetector.

public void testStopRealtimeDetector() throws IOException {
    List<String> realtimeResult = startRealtimeDetector();
    String detectorId = realtimeResult.get(0);
    String jobId = realtimeResult.get(1);
    AnomalyDetectorJobRequest request = stopDetectorJobRequest(detectorId, false);
    client().execute(AnomalyDetectorJobAction.INSTANCE, request).actionGet(10000);
    GetResponse doc = getDoc(AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX, detectorId);
    AnomalyDetectorJob job = toADJob(doc);
    assertFalse(job.isEnabled());
    assertEquals(detectorId, job.getName());
    List<ADTask> adTasks = searchADTasks(detectorId, true, 10);
    assertEquals(1, adTasks.size());
    assertEquals(ADTaskType.REALTIME_SINGLE_ENTITY.name(), adTasks.get(0).getTaskType());
    assertNotEquals(jobId, adTasks.get(0).getTaskId());
    assertEquals(ADTaskState.STOPPED.name(), adTasks.get(0).getState());
}
Also used : ADTask(org.opensearch.ad.model.ADTask) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) GetResponse(org.opensearch.action.get.GetResponse)

Example 17 with ADTask

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

the class AnomalyDetectorJobTransportActionTests method testValidHistoricalAnalysis.

public void testValidHistoricalAnalysis() throws IOException, InterruptedException {
    ADTask adTask = startHistoricalAnalysis(startTime, endTime);
    Thread.sleep(10000);
    ADTask finishedTask = getADTask(adTask.getTaskId());
    assertTrue(HISTORICAL_ANALYSIS_FINISHED_FAILED_STATS.contains(finishedTask.getState()));
}
Also used : ADTask(org.opensearch.ad.model.ADTask)

Example 18 with ADTask

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

the class AnomalyDetectorJobTransportActionTests method testStartRealtimeDetector.

public void testStartRealtimeDetector() throws IOException {
    List<String> realtimeResult = startRealtimeDetector();
    String detectorId = realtimeResult.get(0);
    String jobId = realtimeResult.get(1);
    GetResponse jobDoc = getDoc(AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX, detectorId);
    AnomalyDetectorJob job = toADJob(jobDoc);
    assertTrue(job.isEnabled());
    assertEquals(detectorId, job.getName());
    List<ADTask> adTasks = searchADTasks(detectorId, true, 10);
    assertEquals(1, adTasks.size());
    assertEquals(ADTaskType.REALTIME_SINGLE_ENTITY.name(), adTasks.get(0).getTaskType());
    assertNotEquals(jobId, adTasks.get(0).getTaskId());
}
Also used : ADTask(org.opensearch.ad.model.ADTask) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) GetResponse(org.opensearch.action.get.GetResponse)

Example 19 with ADTask

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

the class AnomalyDetectorJobTransportActionTests method testProfileHistoricalDetector.

public void testProfileHistoricalDetector() throws IOException, InterruptedException {
    ADTask adTask = startHistoricalAnalysis(startTime, endTime);
    GetAnomalyDetectorRequest request = taskProfileRequest(adTask.getDetectorId());
    GetAnomalyDetectorResponse response = client().execute(GetAnomalyDetectorAction.INSTANCE, request).actionGet(10000);
    assertTrue(response.getDetectorProfile().getAdTaskProfile() != null);
    ADTask finishedTask = getADTask(adTask.getTaskId());
    int i = 0;
    while (TestHelpers.HISTORICAL_ANALYSIS_RUNNING_STATS.contains(finishedTask.getState()) && i < 10) {
        finishedTask = getADTask(adTask.getTaskId());
        Thread.sleep(2000);
        i++;
    }
    assertTrue(HISTORICAL_ANALYSIS_FINISHED_FAILED_STATS.contains(finishedTask.getState()));
    response = client().execute(GetAnomalyDetectorAction.INSTANCE, request).actionGet(10000);
    assertNull(response.getDetectorProfile().getAdTaskProfile().getNodeId());
    ADTask profileAdTask = response.getDetectorProfile().getAdTaskProfile().getAdTask();
    assertEquals(finishedTask.getTaskId(), profileAdTask.getTaskId());
    assertEquals(finishedTask.getDetectorId(), profileAdTask.getDetectorId());
    assertEquals(finishedTask.getDetector(), profileAdTask.getDetector());
    assertEquals(finishedTask.getState(), profileAdTask.getState());
}
Also used : ADTask(org.opensearch.ad.model.ADTask)

Example 20 with ADTask

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

the class AnomalyDetectorJobTransportActionTests method testRunMultipleTasksForHistoricalAnalysis.

public void testRunMultipleTasksForHistoricalAnalysis() throws IOException, InterruptedException {
    AnomalyDetector detector = TestHelpers.randomDetector(ImmutableList.of(maxValueFeature()), testIndex, detectionIntervalInMinutes, timeField);
    String detectorId = createDetector(detector);
    AnomalyDetectorJobRequest request = startDetectorJobRequest(detectorId, dateRange);
    AnomalyDetectorJobResponse response = client().execute(AnomalyDetectorJobAction.INSTANCE, request).actionGet(10000);
    assertNotNull(response.getId());
    OpenSearchStatusException exception = null;
    // Add retry to solve the flaky test
    for (int i = 0; i < 10; i++) {
        exception = expectThrows(OpenSearchStatusException.class, () -> client().execute(AnomalyDetectorJobAction.INSTANCE, request).actionGet(10000));
        if (exception.getMessage().contains(DETECTOR_IS_RUNNING)) {
            break;
        } else {
            logger.error("Unexpected error happened when rerun detector", exception);
        }
        Thread.sleep(1000);
    }
    assertNotNull(exception);
    assertTrue(exception.getMessage().contains(DETECTOR_IS_RUNNING));
    assertEquals(DETECTOR_IS_RUNNING, exception.getMessage());
    Thread.sleep(20000);
    List<ADTask> adTasks = searchADTasks(detectorId, null, 100);
    assertEquals(1, adTasks.size());
    assertTrue(HISTORICAL_ANALYSIS_FINISHED_FAILED_STATS.contains(adTasks.get(0).getState()));
}
Also used : ADTask(org.opensearch.ad.model.ADTask) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Aggregations

ADTask (org.opensearch.ad.model.ADTask)108 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)44 List (java.util.List)36 ArrayList (java.util.ArrayList)35 ActionListener (org.opensearch.action.ActionListener)35 Client (org.opensearch.client.Client)35 ImmutableMap (com.google.common.collect.ImmutableMap)34 AnomalyDetectorJob (org.opensearch.ad.model.AnomalyDetectorJob)34 ImmutableList (com.google.common.collect.ImmutableList)32 Map (java.util.Map)32 Optional (java.util.Optional)32 Entity (org.opensearch.ad.model.Entity)32 Instant (java.time.Instant)31 ADTaskState (org.opensearch.ad.model.ADTaskState)31 ADTaskType (org.opensearch.ad.model.ADTaskType)31 HashMap (java.util.HashMap)30 Collectors (java.util.stream.Collectors)30 DetectionDateRange (org.opensearch.ad.model.DetectionDateRange)30 TransportService (org.opensearch.transport.TransportService)30 LogManager (org.apache.logging.log4j.LogManager)29