Search in sources :

Example 16 with ADTaskProfile

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());
}
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 17 with ADTaskProfile

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());
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) IndexResponse(org.opensearch.action.index.IndexResponse) HashRing(org.opensearch.ad.cluster.HashRing) Version(org.opensearch.Version) ADTaskState(org.opensearch.ad.model.ADTaskState) AnomalyDetectorFunction(org.opensearch.ad.rest.handler.AnomalyDetectorFunction) Mockito.doThrow(org.mockito.Mockito.doThrow) ADUnitTestCase(org.opensearch.ad.ADUnitTestCase) Mockito.doAnswer(org.mockito.Mockito.doAnswer) TestHelpers.randomIntervalSchedule(org.opensearch.ad.TestHelpers.randomIntervalSchedule) MAX_OLD_AD_TASK_DOCS_PER_DETECTOR(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_OLD_AD_TASK_DOCS_PER_DETECTOR) Map(java.util.Map) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ActionListener(org.opensearch.action.ActionListener) Mockito.doReturn(org.mockito.Mockito.doReturn) ADStatsNodeResponse(org.opensearch.ad.transport.ADStatsNodeResponse) MAX_RUNNING_ENTITIES_PER_DETECTOR_FOR_HISTORICAL_ANALYSIS(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_RUNNING_ENTITIES_PER_DETECTOR_FOR_HISTORICAL_ANALYSIS) Client(org.opensearch.client.Client) TimeValue(org.opensearch.common.unit.TimeValue) Index(org.opensearch.index.Index) SearchHit(org.opensearch.search.SearchHit) GetResult(org.opensearch.index.get.GetResult) Settings(org.opensearch.common.settings.Settings) DETECTION_STATE_INDEX(org.opensearch.ad.constant.CommonName.DETECTION_STATE_INDEX) TransportService(org.opensearch.transport.TransportService) RandomCutForest(com.amazon.randomcutforest.RandomCutForest) ArgumentMatchers.anyFloat(org.mockito.ArgumentMatchers.anyFloat) TestHelpers(org.opensearch.ad.TestHelpers) DocWriteResponse(org.opensearch.action.DocWriteResponse) Mockito.mock(org.mockito.Mockito.mock) BATCH_TASK_PIECE_INTERVAL_SECONDS(org.opensearch.ad.settings.AnomalyDetectorSettings.BATCH_TASK_PIECE_INTERVAL_SECONDS) MAX_BATCH_TASK_PER_NODE(org.opensearch.ad.settings.AnomalyDetectorSettings.MAX_BATCH_TASK_PER_NODE) TestHelpers.randomFeature(org.opensearch.ad.TestHelpers.randomFeature) UpdateResponse(org.opensearch.action.update.UpdateResponse) ThreadPool(org.opensearch.threadpool.ThreadPool) ADTaskProfileResponse(org.opensearch.ad.transport.ADTaskProfileResponse) DocWriteRequest(org.opensearch.action.DocWriteRequest) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) TestHelpers.randomDetectionDateRange(org.opensearch.ad.TestHelpers.randomDetectionDateRange) SearchHits(org.opensearch.search.SearchHits) Mockito.spy(org.mockito.Mockito.spy) ADTaskProfile(org.opensearch.ad.model.ADTaskProfile) TestHelpers.randomAdTask(org.opensearch.ad.TestHelpers.randomAdTask) ArrayList(java.util.ArrayList) UNASSIGNED_SEQ_NO(org.opensearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) DeleteResponse(org.opensearch.action.delete.DeleteResponse) SearchRequest(org.opensearch.action.search.SearchRequest) CommonName(org.opensearch.ad.constant.CommonName) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) TotalHits(org.apache.lucene.search.TotalHits) Mockito.never(org.mockito.Mockito.never) ChronoUnit(java.time.temporal.ChronoUnit) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) ClusterService(org.opensearch.cluster.service.ClusterService) ADTaskAction(org.opensearch.ad.model.ADTaskAction) DeleteByQueryAction(org.opensearch.index.reindex.DeleteByQueryAction) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DELETE_AD_RESULT_WHEN_DELETE_DETECTOR(org.opensearch.ad.settings.AnomalyDetectorSettings.DELETE_AD_RESULT_WHEN_DELETE_DETECTOR) ToXContent(org.opensearch.common.xcontent.ToXContent) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) TestHelpers.randomAnomalyDetector(org.opensearch.ad.TestHelpers.randomAnomalyDetector) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MapperService(org.opensearch.index.mapper.MapperService) IndexAnomalyDetectorJobActionHandler(org.opensearch.ad.rest.handler.IndexAnomalyDetectorJobActionHandler) Locale(java.util.Locale) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) TestHelpers.randomDetector(org.opensearch.ad.TestHelpers.randomDetector) GetResponse(org.opensearch.action.get.GetResponse) InternalStatNames(org.opensearch.ad.stats.InternalStatNames) TestHelpers.randomIntervalTimeConfiguration(org.opensearch.ad.TestHelpers.randomIntervalTimeConfiguration) ImmutableMap(com.google.common.collect.ImmutableMap) ADTaskType(org.opensearch.ad.model.ADTaskType) ADTaskProfileNodeResponse(org.opensearch.ad.transport.ADTaskProfileNodeResponse) Instant(java.time.Instant) TransportAddress(org.opensearch.common.transport.TransportAddress) AnomalyDetectorJobResponse(org.opensearch.ad.transport.AnomalyDetectorJobResponse) List(java.util.List) ForwardADTaskRequest(org.opensearch.ad.transport.ForwardADTaskRequest) DuplicateTaskException(org.opensearch.ad.common.exception.DuplicateTaskException) DetectionDateRange(org.opensearch.ad.model.DetectionDateRange) Optional(java.util.Optional) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) ANOMALY_RESULT_INDEX_ALIAS(org.opensearch.ad.constant.CommonName.ANOMALY_RESULT_INDEX_ALIAS) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) REQUEST_TIMEOUT(org.opensearch.ad.settings.AnomalyDetectorSettings.REQUEST_TIMEOUT) ADStatsNodesResponse(org.opensearch.ad.transport.ADStatsNodesResponse) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) BytesReference(org.opensearch.common.bytes.BytesReference) ADTask(org.opensearch.ad.model.ADTask) TestHelpers.randomUser(org.opensearch.ad.TestHelpers.randomUser) Deque(java.util.Deque) Captor(org.mockito.Captor) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) SearchResponse(org.opensearch.action.search.SearchResponse) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) ExecutorService(java.util.concurrent.ExecutorService) Collections.emptyMap(java.util.Collections.emptyMap) Collections.emptySet(java.util.Collections.emptySet) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Entity.createSingleAttributeEntity(org.opensearch.ad.model.Entity.createSingleAttributeEntity) MockSimpleLog(org.opensearch.ad.mock.model.MockSimpleLog) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) Mockito.when(org.mockito.Mockito.when) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) Mockito.verify(org.mockito.Mockito.verify) CREATE_INDEX_NOT_ACKNOWLEDGED(org.opensearch.ad.constant.CommonErrorMessages.CREATE_INDEX_NOT_ACKNOWLEDGED) Consumer(java.util.function.Consumer) ShardId(org.opensearch.index.shard.ShardId) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) Entity(org.opensearch.ad.model.Entity) BulkResponse(org.opensearch.action.bulk.BulkResponse) User(org.opensearch.commons.authuser.User) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) ThresholdedRandomCutForest(com.amazon.randomcutforest.parkservices.ThresholdedRandomCutForest) ClusterName(org.opensearch.cluster.ClusterName) Collections(java.util.Collections) SearchHit(org.opensearch.search.SearchHit) ADTaskProfileNodeResponse(org.opensearch.ad.transport.ADTaskProfileNodeResponse) 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) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) ClusterName(org.opensearch.cluster.ClusterName) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SearchHits(org.opensearch.search.SearchHits) GetResult(org.opensearch.index.get.GetResult) AnomalyDetectorJobResponse(org.opensearch.ad.transport.AnomalyDetectorJobResponse) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) ADTaskProfileResponse(org.opensearch.ad.transport.ADTaskProfileResponse) GetResponse(org.opensearch.action.get.GetResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) ActionListener(org.opensearch.action.ActionListener) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 18 with ADTaskProfile

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

Example 19 with ADTaskProfile

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

Example 20 with ADTaskProfile

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);
}
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