Search in sources :

Example 21 with ADTask

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

the class HistoricalAnalysisRestApiIT method checkIfTaskCanFinishCorrectly.

private void checkIfTaskCanFinishCorrectly(String detectorId, String taskId, Set<String> states) throws InterruptedException {
    List<Object> results = waitUntilTaskDone(detectorId);
    ADTaskProfile endTaskProfile = (ADTaskProfile) results.get(0);
    Integer retryCount = (Integer) results.get(1);
    ADTask stoppedAdTask = endTaskProfile.getAdTask();
    assertEquals(taskId, stoppedAdTask.getTaskId());
    if (retryCount < MAX_RETRY_TIMES) {
        // It's possible that historical analysis still running after max retry times
        assertTrue(states.contains(stoppedAdTask.getState()));
    }
}
Also used : ADTaskProfile(org.opensearch.ad.model.ADTaskProfile) ADTask(org.opensearch.ad.model.ADTask) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject)

Example 22 with ADTask

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

the class ADTaskCacheManagerTests method testRemoveHistoricalTaskCacheIfNoRunningEntity.

public void testRemoveHistoricalTaskCacheIfNoRunningEntity() throws IOException {
    String detectorId = randomAlphaOfLength(5);
    adTaskCacheManager.removeHistoricalTaskCacheIfNoRunningEntity(detectorId);
    // Add pending entity should not impact remove historical task cache
    ADTask adTask = TestHelpers.randomAdTask(ADTaskType.HISTORICAL_HC_DETECTOR);
    adTaskCacheManager.add(detectorId, adTask);
    adTaskCacheManager.addPendingEntity(detectorId, randomAlphaOfLength(5));
    adTaskCacheManager.removeHistoricalTaskCacheIfNoRunningEntity(detectorId);
    // Add pending entity and move it to running should impact remove historical task cache
    adTaskCacheManager.add(detectorId, adTask);
    String entity = randomAlphaOfLength(5);
    adTaskCacheManager.addPendingEntity(detectorId, entity);
    String pollEntity = adTaskCacheManager.pollEntity(detectorId);
    assertEquals(entity, pollEntity);
    expectThrows(IllegalArgumentException.class, () -> adTaskCacheManager.removeHistoricalTaskCacheIfNoRunningEntity(detectorId));
}
Also used : ADTask(org.opensearch.ad.model.ADTask)

Example 23 with ADTask

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

the class ADTaskCacheManagerTests method testPutDuplicateTask.

public void testPutDuplicateTask() throws IOException {
    when(memoryTracker.canAllocateReserved(anyLong())).thenReturn(true);
    ADTask adTask1 = TestHelpers.randomAdTask();
    adTaskCacheManager.add(adTask1);
    assertEquals(1, adTaskCacheManager.size());
    DuplicateTaskException e1 = expectThrows(DuplicateTaskException.class, () -> adTaskCacheManager.add(adTask1));
    assertEquals(DETECTOR_IS_RUNNING, e1.getMessage());
    ADTask adTask2 = TestHelpers.randomAdTask(randomAlphaOfLength(5), ADTaskState.INIT, adTask1.getExecutionEndTime(), adTask1.getStoppedBy(), adTask1.getDetectorId(), adTask1.getDetector(), ADTaskType.HISTORICAL_SINGLE_ENTITY);
    DuplicateTaskException e2 = expectThrows(DuplicateTaskException.class, () -> adTaskCacheManager.add(adTask2));
    assertEquals(DETECTOR_IS_RUNNING, e2.getMessage());
}
Also used : ADTask(org.opensearch.ad.model.ADTask) DuplicateTaskException(org.opensearch.ad.common.exception.DuplicateTaskException)

Example 24 with ADTask

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

the class ADTaskCacheManagerTests method testAddDetector.

public void testAddDetector() throws IOException {
    String detectorId = randomAlphaOfLength(10);
    ADTask adTask = TestHelpers.randomAdTask(ADTaskType.HISTORICAL_HC_DETECTOR);
    adTaskCacheManager.add(detectorId, adTask);
    DuplicateTaskException e1 = expectThrows(DuplicateTaskException.class, () -> adTaskCacheManager.add(detectorId, adTask));
    assertEquals(DETECTOR_IS_RUNNING, e1.getMessage());
}
Also used : ADTask(org.opensearch.ad.model.ADTask) DuplicateTaskException(org.opensearch.ad.common.exception.DuplicateTaskException)

Example 25 with ADTask

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

the class ADTaskCacheManagerTests method addHCDetectorCache.

private List<String> addHCDetectorCache() throws IOException {
    when(memoryTracker.canAllocateReserved(anyLong())).thenReturn(true);
    AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableList.of(TestHelpers.randomFeature(true)), null, Instant.now(), true, ImmutableList.of(randomAlphaOfLength(5)));
    String detectorId = detector.getDetectorId();
    ADTask adDetectorTask = TestHelpers.randomAdTask(randomAlphaOfLength(5), ADTaskState.CREATED, Instant.now(), null, detectorId, detector, ADTaskType.HISTORICAL_HC_DETECTOR);
    ADTask adEntityTask = TestHelpers.randomAdTask(randomAlphaOfLength(5), ADTaskState.CREATED, Instant.now(), null, detectorId, detector, ADTaskType.HISTORICAL_HC_ENTITY);
    adTaskCacheManager.add(detectorId, adDetectorTask);
    adTaskCacheManager.add(adEntityTask);
    assertEquals(adEntityTask.getEntity(), adTaskCacheManager.getEntity(adEntityTask.getTaskId()));
    String entityValue = randomAlphaOfLength(5);
    adTaskCacheManager.addPendingEntities(detectorId, ImmutableList.of(entityValue));
    assertEquals(1, adTaskCacheManager.getUnfinishedEntityCount(detectorId));
    return ImmutableList.of(detectorId, adDetectorTask.getTaskId(), adEntityTask.getTaskId(), entityValue);
}
Also used : ADTask(org.opensearch.ad.model.ADTask) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector)

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