Search in sources :

Example 21 with DiscoveryNode

use of org.opensearch.cluster.node.DiscoveryNode in project k-NN by opensearch-project.

the class TrainingJobRouteDecisionInfoResponseTests method testToXContent.

public void testToXContent() throws IOException {
    // Set up the data
    String id1 = "id_1";
    DiscoveryNode discoveryNode1 = mock(DiscoveryNode.class);
    when(discoveryNode1.getId()).thenReturn(id1);
    Integer trainingJobCount1 = 1;
    TrainingJobRouteDecisionInfoNodeResponse nodeResponse1 = new TrainingJobRouteDecisionInfoNodeResponse(discoveryNode1, trainingJobCount1);
    String id2 = "id_2";
    DiscoveryNode discoveryNode2 = mock(DiscoveryNode.class);
    when(discoveryNode2.getId()).thenReturn(id2);
    Integer trainingJobCount2 = 2;
    TrainingJobRouteDecisionInfoNodeResponse nodeResponse2 = new TrainingJobRouteDecisionInfoNodeResponse(discoveryNode2, trainingJobCount2);
    String id3 = "id_3";
    DiscoveryNode discoveryNode3 = mock(DiscoveryNode.class);
    when(discoveryNode3.getId()).thenReturn(id3);
    Integer trainingJobCount3 = 3;
    TrainingJobRouteDecisionInfoNodeResponse nodeResponse3 = new TrainingJobRouteDecisionInfoNodeResponse(discoveryNode3, trainingJobCount3);
    // We expect this:
    // {
    // "nodes": {
    // "id_1": {
    // "training_job_count": 1
    // },
    // "id_2": {
    // "training_job_count": 2
    // },
    // "id_3": {
    // "training_job_count": 3
    // },
    // }
    // }
    XContentBuilder expectedXContentBuilder = XContentFactory.jsonBuilder().startObject().startObject(NODES_KEY).startObject(id1).field(TRAINING_JOB_COUNT_FIELD_NAME, trainingJobCount1).endObject().startObject(id2).field(TRAINING_JOB_COUNT_FIELD_NAME, trainingJobCount2).endObject().startObject(id3).field(TRAINING_JOB_COUNT_FIELD_NAME, trainingJobCount3).endObject().endObject().endObject();
    Map<String, Object> expected = xContentBuilderToMap(expectedXContentBuilder);
    // Configure response
    List<TrainingJobRouteDecisionInfoNodeResponse> nodeResponses = ImmutableList.of(nodeResponse1, nodeResponse2, nodeResponse3);
    List<FailedNodeException> failedNodeExceptions = Collections.emptyList();
    TrainingJobRouteDecisionInfoResponse response = new TrainingJobRouteDecisionInfoResponse(ClusterName.DEFAULT, nodeResponses, failedNodeExceptions);
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
    builder = response.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject();
    Map<String, Object> actual = xContentBuilderToMap(builder);
    // Check responses are equal
    assertTrue(Maps.difference(expected, actual).areEqual());
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) FailedNodeException(org.opensearch.action.FailedNodeException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 22 with DiscoveryNode

use of org.opensearch.cluster.node.DiscoveryNode in project anomaly-detection by opensearch-project.

the class NodeStateManagerTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    client = mock(Client.class);
    settings = Settings.builder().put("plugins.anomaly_detection.max_retry_for_unresponsive_node", 3).put("plugins.anomaly_detection.ad_mute_minutes", TimeValue.timeValueMinutes(10)).build();
    clock = mock(Clock.class);
    duration = Duration.ofHours(1);
    context = TestHelpers.createThreadPool();
    throttler = new Throttler(clock);
    clientUtil = new ClientUtil(Settings.EMPTY, client, throttler, mock(ThreadPool.class));
    Set<Setting<?>> nodestateSetting = new HashSet<>(ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    nodestateSetting.add(MAX_RETRY_FOR_UNRESPONSIVE_NODE);
    nodestateSetting.add(BACKOFF_MINUTES);
    clusterSettings = new ClusterSettings(Settings.EMPTY, nodestateSetting);
    DiscoveryNode discoveryNode = new DiscoveryNode("node1", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
    clusterService = ClusterServiceUtils.createClusterService(threadPool, discoveryNode, clusterSettings);
    stateManager = new NodeStateManager(client, xContentRegistry(), settings, clientUtil, clock, duration, clusterService);
    checkpointResponse = mock(GetResponse.class);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClientUtil(org.opensearch.ad.util.ClientUtil) Setting(org.opensearch.common.settings.Setting) Client(org.opensearch.client.Client) Clock(java.time.Clock) GetResponse(org.opensearch.action.get.GetResponse) Throttler(org.opensearch.ad.util.Throttler) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 23 with DiscoveryNode

use of org.opensearch.cluster.node.DiscoveryNode in project anomaly-detection by opensearch-project.

the class ADClusterEventListenerTests method testIsWarmNode.

public void testIsWarmNode() {
    HashMap<String, String> attributesForNode1 = new HashMap<>();
    attributesForNode1.put(CommonName.BOX_TYPE_KEY, CommonName.WARM_BOX_TYPE);
    dataNode1 = new DiscoveryNode(dataNode1Id, buildNewFakeTransportAddress(), attributesForNode1, BUILT_IN_ROLES, Version.CURRENT);
    ClusterState warmNodeClusterState = ClusterState.builder(new ClusterName(clusterName)).nodes(new DiscoveryNodes.Builder().masterNodeId(masterNodeId).localNodeId(dataNode1Id).add(masterNode).add(dataNode1)).blocks(ClusterBlocks.builder().addGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)).build();
    listener.clusterChanged(new ClusterChangedEvent("foo", warmNodeClusterState, oldClusterState));
    assertTrue(testAppender.containsMessage(ADClusterEventListener.NOT_RECOVERED_MSG));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) ClusterName(org.opensearch.cluster.ClusterName) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Example 24 with DiscoveryNode

use of org.opensearch.cluster.node.DiscoveryNode in project anomaly-detection by opensearch-project.

the class ADDataMigratorTests method testMigrateDataWithNormalJobResponse_ExistingDetector_ExistingInternalError.

public void testMigrateDataWithNormalJobResponse_ExistingDetector_ExistingInternalError() {
    when(detectionIndices.doesAnomalyDetectorJobIndexExist()).thenReturn(true);
    when(detectionIndices.doesDetectorStateIndexExist()).thenReturn(true);
    String detectorId = randomAlphaOfLength(10);
    doAnswer(invocation -> {
        // Return correct AD job when search job index
        ActionListener<SearchResponse> listener = invocation.getArgument(1);
        SearchHit job1 = SearchHit.fromXContent(TestHelpers.parser(jobContent));
        SearchHits searchHits = new SearchHits(new SearchHit[] { job1 }, 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;
    }).doAnswer(invocation -> {
        // Return null when search realtime tasks
        ActionListener<SearchResponse> listener = invocation.getArgument(1);
        listener.onResponse(null);
        return null;
    }).when(client).search(any(), any());
    doAnswer(invocation -> {
        // Return null when get detector internal error from index.
        ActionListener<GetResponse> listener = invocation.getArgument(1);
        XContentParser parser = TestHelpers.parser(internalError, false);
        GetResponse getResponse = GetResponse.fromXContent(parser);
        listener.onResponse(getResponse);
        return null;
    }).doAnswer(invocation -> {
        // Return correct detector when get detector index.
        ActionListener<GetResponse> listener = invocation.getArgument(1);
        XContentParser parser = TestHelpers.parser(detectorContent, false);
        GetResponse getResponse = GetResponse.fromXContent(parser);
        listener.onResponse(getResponse);
        return null;
    }).when(client).get(any(), any());
    doAnswer(invocation -> {
        ActionListener<IndexResponse> listener = invocation.getArgument(1);
        String taskId = randomAlphaOfLength(5);
        IndexResponse indexResponse = IndexResponse.fromXContent(TestHelpers.parser(indexResponseContent, false));
        listener.onResponse(indexResponse);
        return null;
    }).when(client).index(any(), any());
    DiscoveryNode localNode = createNode("localNodeId");
    doReturn(localNode).when(clusterService).localNode();
    adDataMigrator.migrateData();
    verify(adDataMigrator, times(2)).backfillRealtimeTask(any(), anyBoolean());
    verify(client, times(1)).index(any(), any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IndexResponse(org.opensearch.action.index.IndexResponse) SearchHits(org.opensearch.search.SearchHits) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) XContentParser(org.opensearch.common.xcontent.XContentParser) ADUnitTestCase(org.opensearch.ad.ADUnitTestCase) 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) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) Mockito.doReturn(org.mockito.Mockito.doReturn) GetResponse(org.opensearch.action.get.GetResponse) Before(org.junit.Before) Client(org.opensearch.client.Client) SearchHit(org.opensearch.search.SearchHit) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Mockito.times(org.mockito.Mockito.times) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) Mockito.when(org.mockito.Mockito.when) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) DETECTION_STATE_INDEX(org.opensearch.ad.constant.CommonName.DETECTION_STATE_INDEX) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) Mockito.verify(org.mockito.Mockito.verify) TotalHits(org.apache.lucene.search.TotalHits) ShardId(org.opensearch.index.shard.ShardId) Mockito.never(org.mockito.Mockito.never) TestHelpers(org.opensearch.ad.TestHelpers) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterService(org.opensearch.cluster.service.ClusterService) ResourceAlreadyExistsException(org.opensearch.ResourceAlreadyExistsException) NoShardAvailableActionException(org.opensearch.action.NoShardAvailableActionException) Mockito.mock(org.mockito.Mockito.mock) TotalHits(org.apache.lucene.search.TotalHits) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) SearchHit(org.opensearch.search.SearchHit) GetResponse(org.opensearch.action.get.GetResponse) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) ActionListener(org.opensearch.action.ActionListener) IndexResponse(org.opensearch.action.index.IndexResponse) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) SearchHits(org.opensearch.search.SearchHits) XContentParser(org.opensearch.common.xcontent.XContentParser) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 25 with DiscoveryNode

use of org.opensearch.cluster.node.DiscoveryNode in project anomaly-detection by opensearch-project.

the class HashRingTests method testGetOwningNode.

public void testGetOwningNode() throws UnknownHostException {
    List<DiscoveryNode> addedNodes = setupNodeDelta();
    // Add first node,
    hashRing.buildCircles(delta, ActionListener.wrap(r -> {
        Optional<DiscoveryNode> node = hashRing.getOwningNodeWithSameLocalAdVersionForRealtimeAD("http-latency-rcf-1");
        assertTrue(node.isPresent());
        assertTrue(asList(newNodeId, localNodeId).contains(node.get().getId()));
        DiscoveryNode[] nodesWithSameLocalAdVersion = hashRing.getNodesWithSameLocalAdVersion();
        Set<String> nodesWithSameLocalAdVersionIds = new HashSet<>();
        for (DiscoveryNode n : nodesWithSameLocalAdVersion) {
            nodesWithSameLocalAdVersionIds.add(n.getId());
        }
        assertFalse("Should not build warm node into hash ring", nodesWithSameLocalAdVersionIds.contains(warmNodeId));
        assertEquals("Wrong hash ring size", 2, nodesWithSameLocalAdVersion.length);
        assertEquals("Wrong hash ring size for historical analysis", 2, hashRing.getNodesWithSameAdVersion(Version.V_1_1_0, false).size());
        // Circles for realtime AD will change as it's eligible to build for when its empty
        assertEquals("Wrong hash ring size for realtime AD", 2, hashRing.getNodesWithSameAdVersion(Version.V_1_1_0, true).size());
    }, e -> {
        e.printStackTrace();
        assertFalse("Build hash ring failed", true);
    }));
    // Second new node joins cluster, test realtime circles will not update.
    String newNodeId2 = "newNode2";
    DiscoveryNode newNode2 = createNode(newNodeId2, "127.0.0.4", 9200, emptyMap());
    addedNodes.add(newNode2);
    when(delta.addedNodes()).thenReturn(addedNodes);
    setupClusterAdminClient(localNode, newNode, newNode2);
    hashRing.buildCircles(delta, ActionListener.wrap(r -> {
        assertEquals("Wrong hash ring size for historical analysis", 3, hashRing.getNodesWithSameAdVersion(Version.V_1_1_0, false).size());
        // Circles for realtime AD will not change as it's eligible to rebuild
        assertEquals("Wrong hash ring size for realtime AD", 2, hashRing.getNodesWithSameAdVersion(Version.V_1_1_0, true).size());
    }, e -> {
        e.printStackTrace();
        assertFalse("Build hash ring failed", true);
    }));
    // Mock it's eligible to rebuild circles for realtime AD, then add new node. Realtime circles should change.
    when(hashRing.eligibleToRebuildCirclesForRealtimeAD()).thenReturn(true);
    String newNodeId3 = "newNode3";
    DiscoveryNode newNode3 = createNode(newNodeId3, "127.0.0.5", 9200, emptyMap());
    addedNodes.add(newNode3);
    when(delta.addedNodes()).thenReturn(addedNodes);
    setupClusterAdminClient(localNode, newNode, newNode2, newNode3);
    hashRing.buildCircles(delta, ActionListener.wrap(r -> {
        assertEquals("Wrong hash ring size for historical analysis", 4, hashRing.getNodesWithSameAdVersion(Version.V_1_1_0, false).size());
        assertEquals("Wrong hash ring size for realtime AD", 4, hashRing.getNodesWithSameAdVersion(Version.V_1_1_0, true).size());
    }, e -> {
        e.printStackTrace();
        assertFalse("Failed to build hash ring", true);
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) ModelManager(org.opensearch.ad.ml.ModelManager) Version(org.opensearch.Version) Build(org.opensearch.Build) PluginInfo(org.opensearch.plugins.PluginInfo) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ADUnitTestCase(org.opensearch.ad.ADUnitTestCase) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ImmutableList(com.google.common.collect.ImmutableList) Arrays.asList(java.util.Arrays.asList) Mockito.doAnswer(org.mockito.Mockito.doAnswer) PluginsAndModules(org.opensearch.action.admin.cluster.node.info.PluginsAndModules) ActionListener(org.opensearch.action.ActionListener) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) COOLDOWN_MINUTES(org.opensearch.ad.settings.AnomalyDetectorSettings.COOLDOWN_MINUTES) Client(org.opensearch.client.Client) CommonName(org.opensearch.ad.constant.CommonName) TimeValue(org.opensearch.common.unit.TimeValue) AdminClient(org.opensearch.client.AdminClient) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) UnknownHostException(java.net.UnknownHostException) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) NodesInfoResponse(org.opensearch.action.admin.cluster.node.info.NodesInfoResponse) List(java.util.List) NodeInfo(org.opensearch.action.admin.cluster.node.info.NodeInfo) ClusterService(org.opensearch.cluster.service.ClusterService) Clock(java.time.Clock) Optional(java.util.Optional) ClusterName(org.opensearch.cluster.ClusterName) Mockito.mock(org.mockito.Mockito.mock) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) HashSet(java.util.HashSet) Set(java.util.Set) Optional(java.util.Optional)

Aggregations

DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)673 ClusterState (org.opensearch.cluster.ClusterState)164 Settings (org.opensearch.common.settings.Settings)152 ArrayList (java.util.ArrayList)137 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)123 ThreadPool (org.opensearch.threadpool.ThreadPool)103 ClusterService (org.opensearch.cluster.service.ClusterService)100 CountDownLatch (java.util.concurrent.CountDownLatch)98 HashSet (java.util.HashSet)93 TransportService (org.opensearch.transport.TransportService)92 IOException (java.io.IOException)89 ActionListener (org.opensearch.action.ActionListener)88 Matchers.containsString (org.hamcrest.Matchers.containsString)84 ClusterName (org.opensearch.cluster.ClusterName)82 Version (org.opensearch.Version)80 List (java.util.List)78 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)78 HashMap (java.util.HashMap)77 TimeValue (org.opensearch.common.unit.TimeValue)77 ShardId (org.opensearch.index.shard.ShardId)76