Search in sources :

Example 1 with EntityProfileResponse

use of org.opensearch.ad.transport.EntityProfileResponse in project anomaly-detection by opensearch-project.

the class EntityProfileRunnerTests method setUpExecuteEntityProfileAction.

@SuppressWarnings("unchecked")
private void setUpExecuteEntityProfileAction(InittedEverResultStatus initted) {
    smallUpdates = 1;
    latestActiveTimestamp = 1603999189758L;
    isActive = Boolean.TRUE;
    modelId = "T4c3dXUBj-2IZN7itix__entity_" + entityValue;
    modelSize = 712480L;
    nodeId = "g6pmr547QR-CfpEvO67M4g";
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        ActionListener<EntityProfileResponse> listener = (ActionListener<EntityProfileResponse>) args[2];
        EntityProfileResponse.Builder profileResponseBuilder = new EntityProfileResponse.Builder();
        if (InittedEverResultStatus.UNKNOWN == initted) {
            profileResponseBuilder.setTotalUpdates(0L);
        } else if (InittedEverResultStatus.NOT_INITTED == initted) {
            profileResponseBuilder.setTotalUpdates(smallUpdates);
            profileResponseBuilder.setLastActiveMs(latestActiveTimestamp);
            profileResponseBuilder.setActive(isActive);
        } else {
            profileResponseBuilder.setTotalUpdates(requiredSamples + 1);
            ModelProfileOnNode model = new ModelProfileOnNode(nodeId, new ModelProfile(modelId, entity, modelSize));
            profileResponseBuilder.setModelProfile(model);
        }
        listener.onResponse(profileResponseBuilder.build());
        return null;
    }).when(client).execute(any(EntityProfileAction.class), any(), any());
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        SearchRequest request = (SearchRequest) args[0];
        String indexName = request.indices()[0];
        ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
        SearchResponse searchResponse = null;
        if (indexName.equals(CommonName.ANOMALY_RESULT_INDEX_ALIAS)) {
            InternalMax maxAgg = new InternalMax(CommonName.AGG_NAME_MAX_TIME, latestSampleTimestamp, DocValueFormat.RAW, emptyMap());
            InternalAggregations internalAggregations = InternalAggregations.from(Collections.singletonList(maxAgg));
            SearchHits hits = new SearchHits(new SearchHit[] {}, null, Float.NaN);
            SearchResponseSections searchSections = new SearchResponseSections(hits, internalAggregations, null, false, false, null, 1);
            searchResponse = new SearchResponse(searchSections, null, 1, 1, 0, 30, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
        } else {
            SearchHits collapsedHits = new SearchHits(new SearchHit[] { new SearchHit(2, "ID", new Text("type"), Collections.emptyMap(), Collections.emptyMap()), new SearchHit(3, "ID", new Text("type"), Collections.emptyMap(), Collections.emptyMap()) }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0F);
            InternalSearchResponse internalSearchResponse = new InternalSearchResponse(collapsedHits, null, null, null, false, null, 1);
            searchResponse = new SearchResponse(internalSearchResponse, null, 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
        }
        listener.onResponse(searchResponse);
        return null;
    }).when(client).search(any(), any());
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) EntityProfileAction(org.opensearch.ad.transport.EntityProfileAction) SearchRequest(org.opensearch.action.search.SearchRequest) SearchResponseSections(org.opensearch.action.search.SearchResponseSections) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) SearchHit(org.opensearch.search.SearchHit) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) Text(org.opensearch.common.text.Text) ModelProfile(org.opensearch.ad.model.ModelProfile) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ActionListener(org.opensearch.action.ActionListener) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse) SearchHits(org.opensearch.search.SearchHits) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 2 with EntityProfileResponse

use of org.opensearch.ad.transport.EntityProfileResponse in project anomaly-detection by opensearch-project.

the class BwcTests method testDeserializeEntityProfileResponse1_0.

/**
 * For EntityProfileResponse, the input is a 1.0 stream.
 * @throws IOException when serialization/deserialization has issues.
 */
public void testDeserializeEntityProfileResponse1_0() throws IOException {
    setUpEntityProfileResponse();
    entityProfileResponse1_0.writeTo(output1_0);
    StreamInput streamInput = output1_0.bytes().streamInput();
    streamInput.setVersion(Version.V_1_0_0);
    EntityProfileResponse readResponse = EntityProfileAction.INSTANCE.getResponseReader().read(streamInput);
    assertThat(readResponse.getModelProfile(), equalTo(convertedModelProfileOnNode));
    assertThat(readResponse.getLastActiveMs(), equalTo(entityProfileResponse1_0.getLastActiveMs()));
    assertThat(readResponse.getTotalUpdates(), equalTo(entityProfileResponse1_0.getTotalUpdates()));
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse)

Example 3 with EntityProfileResponse

use of org.opensearch.ad.transport.EntityProfileResponse in project anomaly-detection by opensearch-project.

the class BwcTests method testDeserializeEntityProfileResponse1_1.

/**
 * For EntityProfileResponse, the input is a 1.1 stream.
 * @throws IOException when serialization/deserialization has issues.
 */
public void testDeserializeEntityProfileResponse1_1() throws IOException {
    setUpEntityProfileResponse();
    entityProfileResponse1_1.writeTo(output1_1);
    StreamInput streamInput = output1_1.bytes().streamInput();
    streamInput.setVersion(V_1_1_0);
    EntityProfileResponse readResponse = EntityProfileAction.INSTANCE.getResponseReader().read(streamInput);
    assertThat(readResponse.getModelProfile(), equalTo(entityProfileResponse1_1.getModelProfile()));
    assertThat(readResponse.getLastActiveMs(), equalTo(entityProfileResponse1_1.getLastActiveMs()));
    assertThat(readResponse.getTotalUpdates(), equalTo(entityProfileResponse1_0.getTotalUpdates()));
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse)

Example 4 with EntityProfileResponse

use of org.opensearch.ad.transport.EntityProfileResponse in project anomaly-detection by opensearch-project.

the class EntityProfileRunner method getJob.

private void getJob(String detectorId, Entity entityValue, Set<EntityProfileName> profilesToCollect, AnomalyDetector detector, EntityProfileResponse entityProfileResponse, ActionListener<EntityProfile> listener) {
    GetRequest getRequest = new GetRequest(ANOMALY_DETECTOR_JOB_INDEX, detectorId);
    client.get(getRequest, ActionListener.wrap(getResponse -> {
        if (getResponse != null && getResponse.isExists()) {
            try (XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, getResponse.getSourceAsString())) {
                ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
                AnomalyDetectorJob job = AnomalyDetectorJob.parse(parser);
                int totalResponsesToWait = 0;
                if (profilesToCollect.contains(EntityProfileName.INIT_PROGRESS) || profilesToCollect.contains(EntityProfileName.STATE)) {
                    totalResponsesToWait++;
                }
                if (profilesToCollect.contains(EntityProfileName.ENTITY_INFO)) {
                    totalResponsesToWait++;
                }
                if (profilesToCollect.contains(EntityProfileName.MODELS)) {
                    totalResponsesToWait++;
                }
                MultiResponsesDelegateActionListener<EntityProfile> delegateListener = new MultiResponsesDelegateActionListener<EntityProfile>(listener, totalResponsesToWait, CommonErrorMessages.FAIL_FETCH_ERR_MSG + entityValue + " of detector " + detectorId, false);
                if (profilesToCollect.contains(EntityProfileName.MODELS)) {
                    EntityProfile.Builder builder = new EntityProfile.Builder();
                    if (false == job.isEnabled()) {
                        delegateListener.onResponse(builder.build());
                    } else {
                        delegateListener.onResponse(builder.modelProfile(entityProfileResponse.getModelProfile()).build());
                    }
                }
                if (profilesToCollect.contains(EntityProfileName.INIT_PROGRESS) || profilesToCollect.contains(EntityProfileName.STATE)) {
                    profileStateRelated(entityProfileResponse.getTotalUpdates(), detectorId, entityValue, profilesToCollect, detector, job, delegateListener);
                }
                if (profilesToCollect.contains(EntityProfileName.ENTITY_INFO)) {
                    long enabledTimeMs = job.getEnabledTime().toEpochMilli();
                    SearchRequest lastSampleTimeRequest = createLastSampleTimeRequest(detectorId, enabledTimeMs, entityValue, detector.getResultIndex());
                    EntityProfile.Builder builder = new EntityProfile.Builder();
                    Optional<Boolean> isActiveOp = entityProfileResponse.isActive();
                    if (isActiveOp.isPresent()) {
                        builder.isActive(isActiveOp.get());
                    }
                    builder.lastActiveTimestampMs(entityProfileResponse.getLastActiveMs());
                    client.search(lastSampleTimeRequest, ActionListener.wrap(searchResponse -> {
                        Optional<Long> latestSampleTimeMs = ParseUtils.getLatestDataTime(searchResponse);
                        if (latestSampleTimeMs.isPresent()) {
                            builder.lastSampleTimestampMs(latestSampleTimeMs.get());
                        }
                        delegateListener.onResponse(builder.build());
                    }, exception -> {
                        // sth wrong like result index not created. Return what we have
                        if (exception instanceof IndexNotFoundException) {
                            // don't print out stack trace since it is not helpful
                            logger.info("Result index hasn't been created", exception.getMessage());
                        } else {
                            logger.warn("fail to get last sample time", exception);
                        }
                        delegateListener.onResponse(builder.build());
                    }));
                }
            } catch (Exception e) {
                logger.error(CommonErrorMessages.FAIL_TO_GET_PROFILE_MSG, e);
                listener.onFailure(e);
            }
        } else {
            sendUnknownState(profilesToCollect, entityValue, true, listener);
        }
    }, exception -> {
        if (exception instanceof IndexNotFoundException) {
            logger.info(exception.getMessage());
            sendUnknownState(profilesToCollect, entityValue, true, listener);
        } else {
            logger.error(CommonErrorMessages.FAIL_TO_GET_PROFILE_MSG + detectorId, exception);
            listener.onFailure(exception);
        }
    }));
}
Also used : EntityProfileRequest(org.opensearch.ad.transport.EntityProfileRequest) EntityProfileName(org.opensearch.ad.model.EntityProfileName) XContentParser(org.opensearch.common.xcontent.XContentParser) ANOMALY_DETECTOR_JOB_INDEX(org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse) Map(java.util.Map) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) EntityProfile(org.opensearch.ad.model.EntityProfile) NestedQueryBuilder(org.opensearch.index.query.NestedQueryBuilder) QueryBuilders(org.opensearch.index.query.QueryBuilders) Client(org.opensearch.client.Client) CommonName(org.opensearch.ad.constant.CommonName) NumericSetting(org.opensearch.ad.settings.NumericSetting) Preference(org.opensearch.cluster.routing.Preference) InitProgressProfile(org.opensearch.ad.model.InitProgressProfile) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) LoggingDeprecationHandler(org.opensearch.common.xcontent.LoggingDeprecationHandler) Set(java.util.Set) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) ScoreMode(org.apache.lucene.search.join.ScoreMode) MultiResponsesDelegateActionListener(org.opensearch.ad.util.MultiResponsesDelegateActionListener) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) List(java.util.List) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) Logger(org.apache.logging.log4j.Logger) AnomalyResult(org.opensearch.ad.model.AnomalyResult) Entity(org.opensearch.ad.model.Entity) EntityState(org.opensearch.ad.model.EntityState) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) Optional(java.util.Optional) XContentType(org.opensearch.common.xcontent.XContentType) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) LogManager(org.apache.logging.log4j.LogManager) EntityProfileAction(org.opensearch.ad.transport.EntityProfileAction) ParseUtils(org.opensearch.ad.util.ParseUtils) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) SearchRequest(org.opensearch.action.search.SearchRequest) Optional(java.util.Optional) NestedQueryBuilder(org.opensearch.index.query.NestedQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) MultiResponsesDelegateActionListener(org.opensearch.ad.util.MultiResponsesDelegateActionListener) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) GetRequest(org.opensearch.action.get.GetRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) EntityProfile(org.opensearch.ad.model.EntityProfile) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

EntityProfileResponse (org.opensearch.ad.transport.EntityProfileResponse)4 ActionListener (org.opensearch.action.ActionListener)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 EntityProfileAction (org.opensearch.ad.transport.EntityProfileAction)2 StreamInput (org.opensearch.common.io.stream.StreamInput)2 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 TotalHits (org.apache.lucene.search.TotalHits)1 ScoreMode (org.apache.lucene.search.join.ScoreMode)1 GetRequest (org.opensearch.action.get.GetRequest)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 SearchResponseSections (org.opensearch.action.search.SearchResponseSections)1 CommonErrorMessages (org.opensearch.ad.constant.CommonErrorMessages)1 CommonName (org.opensearch.ad.constant.CommonName)1 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)1 ANOMALY_DETECTORS_INDEX (org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX)1