use of org.opensearch.ad.model.ADTaskProfile in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method testGetLocalADTaskProfilesByDetectorId.
public void testGetLocalADTaskProfilesByDetectorId() {
doReturn(node1).when(clusterService).localNode();
when(adTaskCacheManager.isHCTaskRunning(anyString())).thenReturn(true);
when(adTaskCacheManager.isHCTaskCoordinatingNode(anyString())).thenReturn(true);
List<String> tasksOfDetector = ImmutableList.of(randomAlphaOfLength(5));
when(adTaskCacheManager.getTasksOfDetector(anyString())).thenReturn(tasksOfDetector);
Deque<Map.Entry<Long, Optional<double[]>>> shingle = new LinkedBlockingDeque<>();
when(adTaskCacheManager.getShingle(anyString())).thenReturn(shingle);
ThresholdedRandomCutForest trcf = mock(ThresholdedRandomCutForest.class);
when(adTaskCacheManager.getTRcfModel(anyString())).thenReturn(trcf);
RandomCutForest rcf = mock(RandomCutForest.class);
when(trcf.getForest()).thenReturn(rcf);
when(rcf.getTotalUpdates()).thenReturn(randomLongBetween(100, 1000));
when(adTaskCacheManager.isThresholdModelTrained(anyString())).thenReturn(true);
when(adTaskCacheManager.getThresholdModelTrainingDataSize(anyString())).thenReturn(randomIntBetween(100, 1000));
when(adTaskCacheManager.getModelSize(anyString())).thenReturn(randomLongBetween(100, 1000));
Entity entity = createSingleAttributeEntity(randomAlphaOfLength(5), randomAlphaOfLength(5));
when(adTaskCacheManager.getEntity(anyString())).thenReturn(entity);
String detectorId = randomAlphaOfLength(5);
ExecutorService executeService = mock(ExecutorService.class);
when(threadPool.executor(anyString())).thenReturn(executeService);
doAnswer(invocation -> {
Runnable runnable = invocation.getArgument(0);
runnable.run();
return null;
}).when(executeService).execute(any());
ADTaskProfile taskProfile = adTaskManager.getLocalADTaskProfilesByDetectorId(detectorId);
assertEquals(1, taskProfile.getEntityTaskProfiles().size());
verify(adTaskCacheManager, times(1)).cleanExpiredHCBatchTaskRunStates();
}
use of org.opensearch.ad.model.ADTaskProfile 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.ADTaskProfile in project anomaly-detection by opensearch-project.
the class ADTaskProfileResponseTests method testSerializeResponse.
public void testSerializeResponse() throws IOException {
String taskId = randomAlphaOfLength(5);
ADTaskProfile adTaskProfile = new ADTaskProfile();
adTaskProfile.setTaskId(taskId);
Version remoteAdVersion = Version.CURRENT;
ADTaskProfileNodeResponse nodeResponse = new ADTaskProfileNodeResponse(randomDiscoveryNode(), adTaskProfile, remoteAdVersion);
List<ADTaskProfileNodeResponse> nodeResponses = ImmutableList.of(nodeResponse);
ADTaskProfileResponse response = new ADTaskProfileResponse(new ClusterName("test"), nodeResponses, ImmutableList.of());
BytesStreamOutput output = new BytesStreamOutput();
response.writeNodesTo(output, nodeResponses);
StreamInput input = output.bytes().streamInput();
List<ADTaskProfileNodeResponse> adTaskProfileNodeResponses = response.readNodesFrom(input);
assertEquals(1, adTaskProfileNodeResponses.size());
assertEquals(taskId, adTaskProfileNodeResponses.get(0).getAdTaskProfile().getTaskId());
BytesStreamOutput output2 = new BytesStreamOutput();
response.writeTo(output2);
StreamInput input2 = output2.bytes().streamInput();
ADTaskProfileResponse response2 = new ADTaskProfileResponse(input2);
assertEquals(1, response2.getNodes().size());
assertEquals(taskId, response2.getNodes().get(0).getAdTaskProfile().getTaskId());
}
use of org.opensearch.ad.model.ADTaskProfile in project anomaly-detection by opensearch-project.
the class ADTaskProfileTests method testSerializeResponse.
@Ignore
public void testSerializeResponse() throws IOException {
DiscoveryNode node = randomDiscoveryNode();
ADTaskProfile profile = 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());
ADTaskProfileNodeResponse nodeResponse = new ADTaskProfileNodeResponse(node, profile, Version.CURRENT);
ImmutableList<ADTaskProfileNodeResponse> nodes = ImmutableList.of(nodeResponse);
ADTaskProfileResponse response = new ADTaskProfileResponse(new ClusterName("test"), nodes, ImmutableList.of());
BytesStreamOutput output = new BytesStreamOutput();
response.writeNodesTo(output, nodes);
NamedWriteableAwareStreamInput input = new NamedWriteableAwareStreamInput(output.bytes().streamInput(), writableRegistry());
List<ADTaskProfileNodeResponse> adTaskProfileNodeResponses = response.readNodesFrom(input);
assertEquals(1, adTaskProfileNodeResponses.size());
ADTaskProfileNodeResponse parsedProfile = adTaskProfileNodeResponses.get(0);
if (Version.CURRENT.onOrBefore(Version.V_1_0_0)) {
assertEquals(profile.getNodeId(), parsedProfile.getAdTaskProfile().getNodeId());
assertNull(parsedProfile.getAdTaskProfile().getTaskId());
} else {
assertEquals(profile.getTaskId(), parsedProfile.getAdTaskProfile().getTaskId());
}
// assertEquals(profile, adTaskProfileNodeResponses.get(0).getAdTaskProfile());
// assertEquals(profile, response2.getNodes().get(0).getAdTaskProfile());
}
use of org.opensearch.ad.model.ADTaskProfile in project anomaly-detection by opensearch-project.
the class ADTaskProfileTests method testADTaskProfileParse.
public void testADTaskProfileParse() throws IOException {
ADTaskProfile adTaskProfile = new ADTaskProfile(randomAlphaOfLength(5), randomInt(), randomLong(), randomBoolean(), randomInt(), randomLong(), randomAlphaOfLength(5));
String adTaskProfileString = TestHelpers.xContentBuilderToString(adTaskProfile.toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS));
ADTaskProfile parsedADTaskProfile = ADTaskProfile.parse(TestHelpers.parser(adTaskProfileString));
assertEquals(adTaskProfile, parsedADTaskProfile);
assertEquals(parsedADTaskProfile.toString(), adTaskProfile.toString());
}
Aggregations