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()));
}
}
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));
}
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());
}
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());
}
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);
}
Aggregations