use of org.opensearch.ad.model.ADTask in project anomaly-detection by opensearch-project.
the class ADTaskCacheManagerTests method testAcquireTaskUpdatingSemaphore.
public void testAcquireTaskUpdatingSemaphore() throws IOException, InterruptedException {
String detectorId = randomAlphaOfLength(10);
ADTask adTask = TestHelpers.randomAdTask(ADTaskType.HISTORICAL_HC_DETECTOR);
adTaskCacheManager.add(detectorId, adTask);
assertTrue(adTaskCacheManager.tryAcquireTaskUpdatingSemaphore(detectorId, 0));
assertFalse(adTaskCacheManager.tryAcquireTaskUpdatingSemaphore(detectorId, 0));
}
use of org.opensearch.ad.model.ADTask in project anomaly-detection by opensearch-project.
the class ADTaskCacheManagerTests method testPutTask.
public void testPutTask() throws IOException {
when(memoryTracker.canAllocateReserved(anyLong())).thenReturn(true);
ADTask adTask = TestHelpers.randomAdTask();
adTaskCacheManager.add(adTask);
assertEquals(1, adTaskCacheManager.size());
assertTrue(adTaskCacheManager.contains(adTask.getTaskId()));
assertTrue(adTaskCacheManager.containsTaskOfDetector(adTask.getDetectorId()));
assertNotNull(adTaskCacheManager.getTRcfModel(adTask.getTaskId()));
assertNotNull(adTaskCacheManager.getShingle(adTask.getTaskId()));
assertFalse(adTaskCacheManager.isThresholdModelTrained(adTask.getTaskId()));
adTaskCacheManager.remove(adTask.getTaskId(), randomAlphaOfLength(5), randomAlphaOfLength(5));
assertEquals(0, adTaskCacheManager.size());
}
use of org.opensearch.ad.model.ADTask 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);
}
use of org.opensearch.ad.model.ADTask in project anomaly-detection by opensearch-project.
the class ADBackwardsCompatibilityIT method verifyAdTasks.
private void verifyAdTasks() throws InterruptedException, IOException {
boolean realtimeTaskMissing = false;
int i = 0;
int maxRetryTimes = 10;
do {
i++;
for (String detectorId : runningRealtimeDetectors) {
Map<String, Object> jobAndTask = getDetectorWithJobAndTask(client(), detectorId);
AnomalyDetectorJob job = (AnomalyDetectorJob) jobAndTask.get(ANOMALY_DETECTOR_JOB);
ADTask historicalTask = (ADTask) jobAndTask.get(HISTORICAL_ANALYSIS_TASK);
ADTask realtimeTask = (ADTask) jobAndTask.get(REALTIME_TASK);
assertTrue(job.isEnabled());
assertNotNull(historicalTask);
if (realtimeTask == null) {
realtimeTaskMissing = true;
}
if (i >= maxRetryTimes) {
assertNotNull(realtimeTask);
assertFalse(realtimeTask.isDone());
}
}
if (realtimeTaskMissing) {
// sleep 10 seconds to wait for realtime job to backfill realtime task
Thread.sleep(10_000);
}
} while (realtimeTaskMissing && i < maxRetryTimes);
}
use of org.opensearch.ad.model.ADTask in project anomaly-detection by opensearch-project.
the class ADTaskManager method cleanDetectorCache.
protected void cleanDetectorCache(ADTask adTask, TransportService transportService, AnomalyDetectorFunction function) {
String detectorId = adTask.getDetectorId();
String taskId = adTask.getTaskId();
cleanDetectorCache(adTask, transportService, function, ActionListener.wrap(r -> {
logger.debug("Successfully cleaned cache for detector {}, task {}", detectorId, taskId);
}, e -> {
logger.error("Failed to clean cache for detector " + detectorId + ", task " + taskId, e);
}));
}
Aggregations