Search in sources :

Example 21 with AnomalyDetector

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());
}
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 22 with AnomalyDetector

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());
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) TestHelpers.randomAnomalyDetector(org.opensearch.ad.TestHelpers.randomAnomalyDetector)

Example 23 with AnomalyDetector

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());
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) Optional(java.util.Optional) SearchHit(org.opensearch.search.SearchHit) AnomalyDetectorJobResponse(org.opensearch.ad.transport.AnomalyDetectorJobResponse) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) TestHelpers.randomAnomalyDetector(org.opensearch.ad.TestHelpers.randomAnomalyDetector) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) SearchHits(org.opensearch.search.SearchHits) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 24 with AnomalyDetector

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());
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) TestHelpers.randomAnomalyDetector(org.opensearch.ad.TestHelpers.randomAnomalyDetector)

Example 25 with AnomalyDetector

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

Aggregations

AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)226 IOException (java.io.IOException)71 ActionListener (org.opensearch.action.ActionListener)54 Response (org.opensearch.client.Response)45 Map (java.util.Map)43 List (java.util.List)41 Client (org.opensearch.client.Client)38 SearchRequest (org.opensearch.action.search.SearchRequest)37 LogManager (org.apache.logging.log4j.LogManager)35 Logger (org.apache.logging.log4j.Logger)35 ArrayList (java.util.ArrayList)33 Optional (java.util.Optional)33 Test (org.junit.Test)32 SearchResponse (org.opensearch.action.search.SearchResponse)32 Instant (java.time.Instant)30 TimeValue (org.opensearch.common.unit.TimeValue)30 HashMap (java.util.HashMap)29 Entity (org.opensearch.ad.model.Entity)29 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)29 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)29