Search in sources :

Example 1 with ADTask

use of org.opensearch.ad.model.ADTask in project anomaly-detection by opensearch-project.

the class ADTaskManagerTests method testScaleTaskLaneOnCoordinatingNode.

@SuppressWarnings("unchecked")
public void testScaleTaskLaneOnCoordinatingNode() {
    ADTask adTask = mock(ADTask.class);
    when(adTask.getCoordinatingNode()).thenReturn(node1.getId());
    when(nodeFilter.getEligibleDataNodes()).thenReturn(new DiscoveryNode[] { node1, node2 });
    ActionListener<AnomalyDetectorJobResponse> listener = mock(ActionListener.class);
    adTaskManager.scaleTaskLaneOnCoordinatingNode(adTask, 2, transportService, listener);
}
Also used : ADTask(org.opensearch.ad.model.ADTask) AnomalyDetectorJobResponse(org.opensearch.ad.transport.AnomalyDetectorJobResponse)

Example 2 with ADTask

use of org.opensearch.ad.model.ADTask in project anomaly-detection by opensearch-project.

the class ADTaskManagerTests method testParseEntityForMultiCategoryHC.

public void testParseEntityForMultiCategoryHC() throws IOException {
    ADTask adTask = randomAdTask(randomAlphaOfLength(5), ADTaskState.INIT, Instant.now(), randomAlphaOfLength(5), TestHelpers.randomAnomalyDetectorUsingCategoryFields(randomAlphaOfLength(5), ImmutableList.of(randomAlphaOfLength(5), randomAlphaOfLength(5))));
    String entityValue = adTaskManager.convertEntityToString(adTask);
    Entity entity = adTaskManager.parseEntityFromString(entityValue, adTask);
    assertEquals(entity, adTask.getEntity());
}
Also used : Entity.createSingleAttributeEntity(org.opensearch.ad.model.Entity.createSingleAttributeEntity) Entity(org.opensearch.ad.model.Entity) ADTask(org.opensearch.ad.model.ADTask) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 3 with ADTask

use of org.opensearch.ad.model.ADTask in project anomaly-detection by opensearch-project.

the class ADTaskManagerTests method testForwardRequestToLeadNodeWithNotExistingNode.

@SuppressWarnings("unchecked")
public void testForwardRequestToLeadNodeWithNotExistingNode() throws IOException {
    ADTask adTask = randomAdTask(ADTaskType.HISTORICAL_HC_ENTITY);
    ForwardADTaskRequest forwardADTaskRequest = new ForwardADTaskRequest(adTask, ADTaskAction.APPLY_FOR_TASK_SLOTS);
    ActionListener<AnomalyDetectorJobResponse> listener = mock(ActionListener.class);
    doAnswer(invocation -> {
        Consumer<Optional<DiscoveryNode>> function = invocation.getArgument(1);
        function.accept(Optional.empty());
        return null;
    }).when(hashRing).buildAndGetOwningNodeWithSameLocalAdVersion(any(), any(), any());
    adTaskManager.forwardRequestToLeadNode(forwardADTaskRequest, transportService, listener);
    verify(listener, times(1)).onFailure(any());
}
Also used : ForwardADTaskRequest(org.opensearch.ad.transport.ForwardADTaskRequest) Optional(java.util.Optional) ADTask(org.opensearch.ad.model.ADTask) AnomalyDetectorJobResponse(org.opensearch.ad.transport.AnomalyDetectorJobResponse)

Example 4 with ADTask

use of org.opensearch.ad.model.ADTask in project anomaly-detection by opensearch-project.

the class ADTaskManagerTests method testScaleUpTaskSlots.

@SuppressWarnings("unchecked")
public void testScaleUpTaskSlots() throws IOException {
    ADTask adTask = randomAdTask(ADTaskType.HISTORICAL_HC_ENTITY);
    ActionListener<AnomalyDetectorJobResponse> listener = mock(ActionListener.class);
    when(adTaskCacheManager.getAvailableNewEntityTaskLanes(anyString())).thenReturn(0);
    doReturn(2).when(adTaskManager).detectorTaskSlotScaleDelta(anyString());
    when(adTaskCacheManager.getLastScaleEntityTaskLaneTime(anyString())).thenReturn(null);
    assertEquals(0, adTaskManager.scaleTaskSlots(adTask, transportService, listener));
    when(adTaskCacheManager.getLastScaleEntityTaskLaneTime(anyString())).thenReturn(Instant.now());
    assertEquals(2, adTaskManager.scaleTaskSlots(adTask, transportService, listener));
    when(adTaskCacheManager.getLastScaleEntityTaskLaneTime(anyString())).thenReturn(Instant.now().minus(10, ChronoUnit.DAYS));
    assertEquals(2, adTaskManager.scaleTaskSlots(adTask, transportService, listener));
    verify(adTaskCacheManager, times(1)).refreshLastScaleEntityTaskLaneTime(anyString());
    verify(adTaskManager, times(1)).forwardScaleTaskSlotRequestToLeadNode(any(), any(), any());
}
Also used : ADTask(org.opensearch.ad.model.ADTask) AnomalyDetectorJobResponse(org.opensearch.ad.transport.AnomalyDetectorJobResponse)

Example 5 with ADTask

use of org.opensearch.ad.model.ADTask 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)

Aggregations

ADTask (org.opensearch.ad.model.ADTask)108 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)44 List (java.util.List)36 ArrayList (java.util.ArrayList)35 ActionListener (org.opensearch.action.ActionListener)35 Client (org.opensearch.client.Client)35 ImmutableMap (com.google.common.collect.ImmutableMap)34 AnomalyDetectorJob (org.opensearch.ad.model.AnomalyDetectorJob)34 ImmutableList (com.google.common.collect.ImmutableList)32 Map (java.util.Map)32 Optional (java.util.Optional)32 Entity (org.opensearch.ad.model.Entity)32 Instant (java.time.Instant)31 ADTaskState (org.opensearch.ad.model.ADTaskState)31 ADTaskType (org.opensearch.ad.model.ADTaskType)31 HashMap (java.util.HashMap)30 Collectors (java.util.stream.Collectors)30 DetectionDateRange (org.opensearch.ad.model.DetectionDateRange)30 TransportService (org.opensearch.transport.TransportService)30 LogManager (org.apache.logging.log4j.LogManager)29