use of org.opensearch.ad.model.ADTaskProfile in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method testGetAndExecuteOnLatestADTasksWithRunningHistoricalTask.
@SuppressWarnings("unchecked")
public void testGetAndExecuteOnLatestADTasksWithRunningHistoricalTask() 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(historicalTaskId).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), historicalTaskId, randomAlphaOfLength(5), randomInt(), randomBoolean(), randomInt(), randomInt(), 2, ImmutableList.of(randomAlphaOfLength(5), 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.ADTaskProfile in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method setupGetAndExecuteOnLatestADTasks.
@SuppressWarnings("unchecked")
private void setupGetAndExecuteOnLatestADTasks(ADTaskProfile adTaskProfile) {
String runningRealtimeHCTaskContent = runningHistoricalHCTaskContent.replace(ADTaskType.HISTORICAL_HC_DETECTOR.name(), ADTaskType.REALTIME_HC_DETECTOR.name()).replace(historicalTaskId, realtimeTaskId);
doAnswer(invocation -> {
ActionListener<SearchResponse> listener = invocation.getArgument(1);
SearchHit historicalTask = SearchHit.fromXContent(TestHelpers.parser(runningHistoricalHCTaskContent));
SearchHit realtimeTask = SearchHit.fromXContent(TestHelpers.parser(runningRealtimeHCTaskContent));
SearchHits searchHits = new SearchHits(new SearchHit[] { historicalTask, realtimeTask }, new TotalHits(2, 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);
listener.onResponse(searchResponse);
return null;
}).when(client).search(any(), any());
String detectorId = randomAlphaOfLength(5);
Consumer<List<ADTask>> function = mock(Consumer.class);
ActionListener<AnomalyDetectorJobResponse> listener = mock(ActionListener.class);
doAnswer(invocation -> {
Consumer<DiscoveryNode[]> getNodeFunction = invocation.getArgument(0);
getNodeFunction.accept(new DiscoveryNode[] { node1, node2 });
return null;
}).when(hashRing).getAllEligibleDataNodesWithKnownAdVersion(any(), any());
doAnswer(invocation -> {
ActionListener<ADTaskProfileResponse> taskProfileResponseListener = invocation.getArgument(2);
AnomalyDetector detector = TestHelpers.randomDetector(ImmutableList.of(randomFeature(true)), randomAlphaOfLength(5), randomIntBetween(1, 10), MockSimpleLog.TIME_FIELD, ImmutableList.of(randomAlphaOfLength(5)));
ADTaskProfileNodeResponse nodeResponse = new ADTaskProfileNodeResponse(node1, adTaskProfile, Version.CURRENT);
ImmutableList<ADTaskProfileNodeResponse> nodes = ImmutableList.of(nodeResponse);
ADTaskProfileResponse taskProfileResponse = new ADTaskProfileResponse(new ClusterName("test"), nodes, ImmutableList.of());
taskProfileResponseListener.onResponse(taskProfileResponse);
return null;
}).doAnswer(invocation -> {
ActionListener<BulkByScrollResponse> updateResponselistener = invocation.getArgument(2);
BulkByScrollResponse response = mock(BulkByScrollResponse.class);
when(response.getBulkFailures()).thenReturn(null);
updateResponselistener.onResponse(response);
return null;
}).when(client).execute(any(), any(), any());
when(nodeFilter.getEligibleDataNodes()).thenReturn(new DiscoveryNode[] { node1, node2 });
doAnswer(invocation -> {
ActionListener<UpdateResponse> updateResponselistener = invocation.getArgument(1);
UpdateResponse response = new UpdateResponse(ShardId.fromString("[test][1]"), CommonName.MAPPING_TYPE, "1", 0L, 1L, 1L, DocWriteResponse.Result.UPDATED);
updateResponselistener.onResponse(response);
return null;
}).when(client).update(any(), any());
doAnswer(invocation -> {
ActionListener<GetResponse> getResponselistener = invocation.getArgument(1);
GetResponse response = new GetResponse(new GetResult(AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX, MapperService.SINGLE_MAPPING_NAME, detectorId, UNASSIGNED_SEQ_NO, 0, -1, true, BytesReference.bytes(new AnomalyDetectorJob(detectorId, randomIntervalSchedule(), randomIntervalTimeConfiguration(), false, Instant.now().minusSeconds(60), Instant.now(), Instant.now(), 60L, TestHelpers.randomUser(), null).toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS)), Collections.emptyMap(), Collections.emptyMap()));
getResponselistener.onResponse(response);
return null;
}).when(client).get(any(), any());
}
use of org.opensearch.ad.model.ADTaskProfile in project anomaly-detection by opensearch-project.
the class AnomalyDetectorJobTransportActionTests method testProfileWithMultipleRunningTask.
public void testProfileWithMultipleRunningTask() throws IOException {
ADTask adTask1 = startHistoricalAnalysis(startTime, endTime);
ADTask adTask2 = startHistoricalAnalysis(startTime, endTime);
GetAnomalyDetectorRequest request1 = taskProfileRequest(adTask1.getDetectorId());
GetAnomalyDetectorRequest request2 = taskProfileRequest(adTask2.getDetectorId());
GetAnomalyDetectorResponse response1 = client().execute(GetAnomalyDetectorAction.INSTANCE, request1).actionGet(10000);
GetAnomalyDetectorResponse response2 = client().execute(GetAnomalyDetectorAction.INSTANCE, request2).actionGet(10000);
ADTaskProfile taskProfile1 = response1.getDetectorProfile().getAdTaskProfile();
ADTaskProfile taskProfile2 = response2.getDetectorProfile().getAdTaskProfile();
assertNotNull(taskProfile1.getNodeId());
assertNotNull(taskProfile2.getNodeId());
assertNotEquals(taskProfile1.getNodeId(), taskProfile2.getNodeId());
}
use of org.opensearch.ad.model.ADTaskProfile in project anomaly-detection by opensearch-project.
the class ADTaskProfileTests method testADTaskProfileNodeResponseReadMethod.
public void testADTaskProfileNodeResponseReadMethod() throws IOException {
ADTaskProfile adTaskProfile = new ADTaskProfile(randomAlphaOfLength(5), randomInt(), randomLong(), randomBoolean(), randomInt(), randomLong(), randomAlphaOfLength(5));
ADTaskProfileNodeResponse response = new ADTaskProfileNodeResponse(randomDiscoveryNode(), adTaskProfile, Version.CURRENT);
testADTaskProfileResponse(response);
}
use of org.opensearch.ad.model.ADTaskProfile in project anomaly-detection by opensearch-project.
the class ADTaskProfileTests method testADTaskProfileParseFullConstructor.
public void testADTaskProfileParseFullConstructor() throws IOException {
ADTaskProfile adTaskProfile = new ADTaskProfile(TestHelpers.randomAdTask(), randomInt(), randomLong(), randomBoolean(), randomInt(), randomLong(), randomAlphaOfLength(5), randomAlphaOfLength(5), randomAlphaOfLength(5), randomInt(), randomBoolean(), randomInt(), randomInt(), randomInt(), ImmutableList.of(randomAlphaOfLength(5)), Instant.now().toEpochMilli());
String adTaskProfileString = TestHelpers.xContentBuilderToString(adTaskProfile.toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS));
ADTaskProfile parsedADTaskProfile = ADTaskProfile.parse(TestHelpers.parser(adTaskProfileString));
assertEquals(adTaskProfile, parsedADTaskProfile);
}
Aggregations