Search in sources :

Example 1 with ADTaskProfile

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();
}
Also used : Entity.createSingleAttributeEntity(org.opensearch.ad.model.Entity.createSingleAttributeEntity) Entity(org.opensearch.ad.model.Entity) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ADTaskProfile(org.opensearch.ad.model.ADTaskProfile) RandomCutForest(com.amazon.randomcutforest.RandomCutForest) ThresholdedRandomCutForest(com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest) ExecutorService(java.util.concurrent.ExecutorService) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ThresholdedRandomCutForest(com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest)

Example 2 with ADTaskProfile

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());
}
Also used : ADTaskProfile(org.opensearch.ad.model.ADTaskProfile) ADTask(org.opensearch.ad.model.ADTask) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) TestHelpers.randomAnomalyDetector(org.opensearch.ad.TestHelpers.randomAnomalyDetector)

Example 3 with ADTaskProfile

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());
}
Also used : ADTaskProfile(org.opensearch.ad.model.ADTaskProfile) Version(org.opensearch.Version) StreamInput(org.opensearch.common.io.stream.StreamInput) ClusterName(org.opensearch.cluster.ClusterName) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 4 with ADTaskProfile

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());
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) TestHelpers.randomDiscoveryNode(org.opensearch.ad.TestHelpers.randomDiscoveryNode) ADTaskProfile(org.opensearch.ad.model.ADTaskProfile) ClusterName(org.opensearch.cluster.ClusterName) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Ignore(org.junit.Ignore)

Example 5 with ADTaskProfile

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());
}
Also used : ADTaskProfile(org.opensearch.ad.model.ADTaskProfile)

Aggregations

ADTaskProfile (org.opensearch.ad.model.ADTaskProfile)21 ADTask (org.opensearch.ad.model.ADTask)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)5 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 Version (org.opensearch.Version)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Instant (java.time.Instant)3 Map (java.util.Map)3 XContentParser (org.opensearch.common.xcontent.XContentParser)3 RandomCutForest (com.amazon.randomcutforest.RandomCutForest)2 ThresholdedRandomCutForest (com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest)2 ChronoUnit (java.time.temporal.ChronoUnit)2 Locale (java.util.Locale)2 Optional (java.util.Optional)2 ExecutorService (java.util.concurrent.ExecutorService)2 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)2