Search in sources :

Example 6 with ModelProfileOnNode

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

the class ProfileResponse method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.field(COORDINATING_NODE, coordinatingNode);
    builder.field(SHINGLE_SIZE, shingleSize);
    builder.field(TOTAL_SIZE, totalSizeInBytes);
    builder.field(ACTIVE_ENTITY, activeEntities);
    builder.field(TOTAL_UPDATES, totalUpdates);
    if (modelCount > 0) {
        builder.field(MODEL_COUNT, modelCount);
    }
    builder.startArray(MODELS);
    for (ModelProfileOnNode profile : modelProfile) {
        profile.toXContent(builder, params);
    }
    builder.endArray();
    return builder;
}
Also used : ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode)

Example 7 with ModelProfileOnNode

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

the class AnomalyDetectorProfileRunnerTests method testProfileModels.

public void testProfileModels() throws InterruptedException, IOException {
    setUpClientGet(DetectorStatus.EXIST, JobStatus.ENABLED, RCFPollingStatus.EMPTY, ErrorResultStatus.NO_ERROR);
    setUpClientExecuteProfileAction();
    final CountDownLatch inProgressLatch = new CountDownLatch(1);
    runner.profile(detector.getDetectorId(), ActionListener.wrap(profileResponse -> {
        assertEquals(node1, profileResponse.getCoordinatingNode());
        assertEquals(shingleSize, profileResponse.getShingleSize());
        assertEquals(modelSize * 2, profileResponse.getTotalSizeInBytes());
        assertEquals(2, profileResponse.getModelProfile().length);
        for (ModelProfileOnNode profile : profileResponse.getModelProfile()) {
            assertTrue(node1.equals(profile.getNodeId()) || node2.equals(profile.getNodeId()));
            assertEquals(modelSize, profile.getModelSize());
            if (node1.equals(profile.getNodeId())) {
                assertEquals(model1Id, profile.getModelId());
            }
            if (node2.equals(profile.getNodeId())) {
                assertEquals(model0Id, profile.getModelId());
            }
        }
        inProgressLatch.countDown();
    }, exception -> {
        assertTrue("Should not reach here ", false);
        inProgressLatch.countDown();
    }), modelProfile);
    assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
Also used : Arrays(java.util.Arrays) ResourceNotFoundException(org.opensearch.ad.common.exception.ResourceNotFoundException) Version(org.opensearch.Version) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ANOMALY_DETECTOR_JOB_INDEX(org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) RCFPollingAction(org.opensearch.ad.transport.RCFPollingAction) ActionListener(org.opensearch.action.ActionListener) GetResponse(org.opensearch.action.get.GetResponse) ProfileNodeResponse(org.opensearch.ad.transport.ProfileNodeResponse) DetectorProfile(org.opensearch.ad.model.DetectorProfile) RemoteTransportException(org.opensearch.transport.RemoteTransportException) Set(java.util.Set) Instant(java.time.Instant) TransportAddress(org.opensearch.common.transport.TransportAddress) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) DetectorState(org.opensearch.ad.model.DetectorState) DetectorInternalState(org.opensearch.ad.model.DetectorInternalState) Optional(java.util.Optional) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) NotSerializableExceptionWrapper(org.opensearch.common.io.stream.NotSerializableExceptionWrapper) ADTask(org.opensearch.ad.model.ADTask) HashMap(java.util.HashMap) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) ArrayList(java.util.ArrayList) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) ProfileResponse(org.opensearch.ad.transport.ProfileResponse) Collections.emptyMap(java.util.Collections.emptyMap) CommonName(org.opensearch.ad.constant.CommonName) Collections.emptySet(java.util.Collections.emptySet) FailedNodeException(org.opensearch.action.FailedNodeException) 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) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) DetectorProfileName(org.opensearch.ad.model.DetectorProfileName) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) RCFPollingResponse(org.opensearch.ad.transport.RCFPollingResponse) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) ChronoUnit(java.time.temporal.ChronoUnit) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) ClusterName(org.opensearch.cluster.ClusterName) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) Collections(java.util.Collections) ProfileAction(org.opensearch.ad.transport.ProfileAction) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 8 with ModelProfileOnNode

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

the class BwcTests method setUpProfileResponse.

@SuppressWarnings("serial")
private void setUpProfileResponse() {
    String node1 = "node1";
    String nodeName1 = "nodename1";
    DiscoveryNode discoveryNode1_1 = new DiscoveryNode(nodeName1, node1, new TransportAddress(TransportAddress.META_ADDRESS, 9300), emptyMap(), emptySet(), V_1_1_0);
    String node2 = "node2";
    String nodeName2 = "nodename2";
    DiscoveryNode discoveryNode2 = new DiscoveryNode(nodeName2, node2, new TransportAddress(TransportAddress.META_ADDRESS, 9301), emptyMap(), emptySet(), V_1_1_0);
    String model1Id = "model1";
    String model2Id = "model2";
    Map<String, Long> modelSizeMap1 = new HashMap<String, Long>() {

        {
            put(model1Id, modelSize);
            put(model2Id, modelSize2);
        }
    };
    Map<String, Long> modelSizeMap2 = new HashMap<String, Long>();
    int shingleSize = 8;
    ModelProfile modelProfile = new ModelProfile(model1Id, entity, modelSize);
    ModelProfile modelProfile2 = new ModelProfile(model2Id, entity2, modelSize2);
    ProfileNodeResponse profileNodeResponse1 = new ProfileNodeResponse(discoveryNode1_1, modelSizeMap1, shingleSize, 0, 0, Arrays.asList(modelProfile, modelProfile2), modelSizeMap1.size());
    ProfileNodeResponse profileNodeResponse2 = new ProfileNodeResponse(discoveryNode2, modelSizeMap2, -1, 0, 0, new ArrayList<>(), modelSizeMap2.size());
    List<ProfileNodeResponse> profileNodeResponses = Arrays.asList(profileNodeResponse1, profileNodeResponse2);
    List<FailedNodeException> failures = Collections.emptyList();
    ClusterName clusterName = new ClusterName("test-cluster-name");
    profileResponse1_1 = new ProfileResponse(clusterName, profileNodeResponses, failures);
    ProfileNodeResponse1_0 profileNodeResponse1_1_0 = new ProfileNodeResponse1_0(discoveryNode1_1, modelSizeMap1, shingleSize, 0, 0);
    ProfileNodeResponse1_0 profileNodeResponse2_1_0 = new ProfileNodeResponse1_0(discoveryNode2, modelSizeMap2, -1, 0, 0);
    List<ProfileNodeResponse1_0> profileNodeResponses1_0 = Arrays.asList(profileNodeResponse1_1_0, profileNodeResponse2_1_0);
    profileResponse1_0 = new ProfileResponse1_0(clusterName, profileNodeResponses1_0, failures);
    convertedModelProfileOnNodeArray = new ModelProfileOnNode[2];
    ModelProfile convertedModelProfile1 = new ModelProfile(model1Id, null, modelSize);
    convertedModelProfileOnNodeArray[0] = new ModelProfileOnNode(CommonName.EMPTY_FIELD, convertedModelProfile1);
    ModelProfile convertedModelProfile2 = new ModelProfile(model2Id, null, modelSize2);
    convertedModelProfileOnNodeArray[1] = new ModelProfileOnNode(CommonName.EMPTY_FIELD, convertedModelProfile2);
    convertedModelProfile = new ModelProfile1_0[2];
    convertedModelProfile[0] = new ModelProfile1_0(model1Id, modelSize, CommonName.EMPTY_FIELD);
    convertedModelProfile[1] = new ModelProfile1_0(model2Id, modelSize2, CommonName.EMPTY_FIELD);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) TransportAddress(org.opensearch.common.transport.TransportAddress) ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) ProfileNodeResponse(org.opensearch.ad.transport.ProfileNodeResponse) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse) ProfileResponse(org.opensearch.ad.transport.ProfileResponse) ModelProfile(org.opensearch.ad.model.ModelProfile) ClusterName(org.opensearch.cluster.ClusterName) FailedNodeException(org.opensearch.action.FailedNodeException)

Example 9 with ModelProfileOnNode

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

the class BwcTests method testDeserializeProfileResponse1_0.

/**
 * For ProfileResponse, the input is a 1.0 stream.
 * @throws IOException when serialization/deserialization has issues.
 */
public void testDeserializeProfileResponse1_0() throws IOException {
    setUpProfileResponse();
    profileResponse1_0.writeTo(output1_0);
    StreamInput streamInput = output1_0.bytes().streamInput();
    streamInput.setVersion(Version.V_1_0_0);
    ProfileResponse readResponse = new ProfileResponse(streamInput);
    ModelProfileOnNode[] actualModelProfileOnNode = readResponse.getModelProfile();
    // since ProfileResponse1_0's constructor iterates modelSize and modelSize is
    // a HashMap. The iteration order is not deterministic. We have to sort the
    // results in an ordered fashion to compare with expected value.
    Arrays.sort(actualModelProfileOnNode, new Comparator<ModelProfileOnNode>() {

        @Override
        public int compare(ModelProfileOnNode o1, ModelProfileOnNode o2) {
            return o1.getModelId().compareTo(o2.getModelId());
        }
    });
    assertThat(actualModelProfileOnNode, equalTo(convertedModelProfileOnNodeArray));
    assertThat(readResponse.getShingleSize(), equalTo(profileResponse1_1.getShingleSize()));
    assertThat(readResponse.getActiveEntities(), equalTo(profileResponse1_1.getActiveEntities()));
    assertThat(readResponse.getTotalUpdates(), equalTo(profileResponse1_1.getTotalUpdates()));
    assertThat(readResponse.getCoordinatingNode(), equalTo(profileResponse1_1.getCoordinatingNode()));
    assertThat(readResponse.getTotalSizeInBytes(), equalTo(profileResponse1_1.getTotalSizeInBytes()));
    assertThat(readResponse.getModelCount(), equalTo(0L));
}
Also used : ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) StreamInput(org.opensearch.common.io.stream.StreamInput) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse) ProfileResponse(org.opensearch.ad.transport.ProfileResponse)

Example 10 with ModelProfileOnNode

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

the class EntityProfileTests method testResponseHashCodeEquals.

public void testResponseHashCodeEquals() {
    EntityProfileResponse.Builder builder = new EntityProfileResponse.Builder();
    builder.setLastActiveMs(lastActiveTimestamp).build();
    ModelProfileOnNode model = new ModelProfileOnNode(nodeId, new ModelProfile(modelId, entity, modelSize));
    builder.setModelProfile(model);
    EntityProfileResponse response = builder.build();
    HashSet<EntityProfileResponse> set = new HashSet<>();
    assertTrue(false == set.contains(response));
    set.add(response);
    assertTrue(set.contains(response));
}
Also used : ModelProfileOnNode(org.opensearch.ad.model.ModelProfileOnNode) ModelProfile(org.opensearch.ad.model.ModelProfile) HashSet(java.util.HashSet)

Aggregations

ModelProfileOnNode (org.opensearch.ad.model.ModelProfileOnNode)12 ModelProfile (org.opensearch.ad.model.ModelProfile)8 EntityProfileResponse (org.opensearch.ad.transport.EntityProfileResponse)5 IOException (java.io.IOException)3 ActionListener (org.opensearch.action.ActionListener)3 FailedNodeException (org.opensearch.action.FailedNodeException)3 ProfileResponse (org.opensearch.ad.transport.ProfileResponse)3 ClusterName (org.opensearch.cluster.ClusterName)3 ChronoUnit (java.time.temporal.ChronoUnit)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Collections.emptyMap (java.util.Collections.emptyMap)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2 TotalHits (org.apache.lucene.search.TotalHits)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2