use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method testGetAndExecuteOnLatestADTasksWithRunningRealtimeTaskWithTaskStopped.
@SuppressWarnings("unchecked")
public void testGetAndExecuteOnLatestADTasksWithRunningRealtimeTaskWithTaskStopped() throws IOException {
String detectorId = randomAlphaOfLength(5);
Consumer<List<ADTask>> function = mock(Consumer.class);
AnomalyDetector detector = TestHelpers.randomDetector(ImmutableList.of(randomFeature(true)), randomAlphaOfLength(5), randomIntBetween(1, 10), MockSimpleLog.TIME_FIELD, ImmutableList.of(randomAlphaOfLength(5)));
ADTask adTask = ADTask.builder().taskId(randomAlphaOfLength(5)).taskType(ADTaskType.HISTORICAL_HC_DETECTOR.name()).detectorId(randomAlphaOfLength(5)).detector(detector).entity(null).state(ADTaskState.RUNNING.name()).taskProgress(0.5f).initProgress(1.0f).currentPiece(Instant.now().truncatedTo(ChronoUnit.SECONDS).minus(randomIntBetween(1, 100), ChronoUnit.MINUTES)).executionStartTime(Instant.now().truncatedTo(ChronoUnit.SECONDS).minus(100, ChronoUnit.MINUTES)).isLatest(true).error(randomAlphaOfLength(5)).checkpointId(randomAlphaOfLength(5)).lastUpdateTime(Instant.now().truncatedTo(ChronoUnit.SECONDS)).startedBy(randomAlphaOfLength(5)).lastUpdateTime(Instant.now().truncatedTo(ChronoUnit.SECONDS)).coordinatingNode(node1.getId()).build();
ADTaskProfile profile = new ADTaskProfile(adTask, randomInt(), randomLong(), randomBoolean(), randomInt(), randomLong(), randomAlphaOfLength(5), randomAlphaOfLength(5), randomAlphaOfLength(5), randomInt(), randomBoolean(), randomInt(), randomInt(), randomInt(), ImmutableList.of(randomAlphaOfLength(5)), Instant.now().toEpochMilli());
setupGetAndExecuteOnLatestADTasks(profile);
adTaskManager.getAndExecuteOnLatestADTasks(detectorId, null, null, ADTaskType.ALL_DETECTOR_TASK_TYPES, function, transportService, true, 10, listener);
verify(client, times(2)).update(any(), any());
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method testCreateTaskIndexWithException.
public void testCreateTaskIndexWithException() throws IOException {
String error = randomAlphaOfLength(5);
doAnswer(invocation -> {
ActionListener<CreateIndexResponse> listener = invocation.getArgument(0);
listener.onFailure(new RuntimeException(error));
return null;
}).when(detectionIndices).initDetectionStateIndex(any());
doReturn(false).when(detectionIndices).doesDetectorStateIndexExist();
AnomalyDetector detector = randomDetector(ImmutableList.of(randomFeature(true)), randomAlphaOfLength(5), 1, randomAlphaOfLength(5));
setupGetDetector(detector);
adTaskManager.startDetector(detector, detectionDateRange, randomUser(), transportService, listener);
verify(listener, times(1)).onFailure(exceptionCaptor.capture());
assertEquals(error, exceptionCaptor.getValue().getMessage());
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method testStopDetectorWithTaskDone.
@SuppressWarnings("unchecked")
public void testStopDetectorWithTaskDone() {
String detectorId = randomAlphaOfLength(5);
boolean historical = true;
ActionListener<AnomalyDetectorJobResponse> listener = mock(ActionListener.class);
doAnswer(invocation -> {
Consumer<Optional<AnomalyDetector>> function = invocation.getArgument(1);
AnomalyDetector detector = randomAnomalyDetector(ImmutableList.of(randomFeature(true)));
function.accept(Optional.of(detector));
return null;
}).when(adTaskManager).getDetector(anyString(), any(), any());
doAnswer(invocation -> {
ActionListener<SearchResponse> actionListener = invocation.getArgument(1);
SearchHit task = SearchHit.fromXContent(TestHelpers.parser(taskContent));
SearchHits searchHits = new SearchHits(new SearchHit[] { task }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), Float.NaN);
InternalSearchResponse response = new InternalSearchResponse(searchHits, InternalAggregations.EMPTY, null, null, false, null, 1);
SearchResponse searchResponse = new SearchResponse(response, null, 1, 1, 0, 100, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
actionListener.onResponse(searchResponse);
return null;
}).when(client).search(any(), any());
adTaskManager.stopDetector(detectorId, historical, indexAnomalyDetectorJobActionHandler, null, transportService, listener);
verify(listener, times(1)).onFailure(any());
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method testCreateTaskIndexNotAcknowledged.
public void testCreateTaskIndexNotAcknowledged() throws IOException {
doAnswer(invocation -> {
ActionListener<CreateIndexResponse> listener = invocation.getArgument(0);
listener.onResponse(new CreateIndexResponse(false, false, ANOMALY_RESULT_INDEX_ALIAS));
return null;
}).when(detectionIndices).initDetectionStateIndex(any());
doReturn(false).when(detectionIndices).doesDetectorStateIndexExist();
AnomalyDetector detector = randomDetector(ImmutableList.of(randomFeature(true)), randomAlphaOfLength(5), 1, randomAlphaOfLength(5));
setupGetDetector(detector);
adTaskManager.startDetector(detector, detectionDateRange, randomUser(), transportService, listener);
verify(listener, times(1)).onFailure(exceptionCaptor.capture());
String error = String.format(Locale.ROOT, CREATE_INDEX_NOT_ACKNOWLEDGED, DETECTION_STATE_INDEX);
assertEquals(error, exceptionCaptor.getValue().getMessage());
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method testStartDetectorForHistoricalAnalysis.
@SuppressWarnings("unchecked")
public void testStartDetectorForHistoricalAnalysis() throws IOException {
AnomalyDetector detector = randomDetector(ImmutableList.of(randomFeature(true)), randomAlphaOfLength(5), 1, randomAlphaOfLength(5));
setupGetDetector(detector);
setupHashRingWithOwningNode();
adTaskManager.startDetector(detector.getDetectorId(), detectionDateRange, indexAnomalyDetectorJobActionHandler, randomUser(), transportService, context, listener);
verify(adTaskManager, times(1)).forwardRequestToLeadNode(any(), any(), any());
}
Aggregations