Search in sources :

Example 1 with NodesInfoRequest

use of org.opensearch.action.admin.cluster.node.info.NodesInfoRequest in project OpenSearch by opensearch-project.

the class RestNodesInfoAction method prepareRequest.

static NodesInfoRequest prepareRequest(final RestRequest request) {
    String[] nodeIds;
    Set<String> metrics;
    // this means one must differentiate between allowed metrics and arbitrary node ids in the same place
    if (request.hasParam("nodeId") && !request.hasParam("metrics")) {
        String nodeId = request.param("nodeId", "_all");
        Set<String> metricsOrNodeIds = Strings.tokenizeByCommaToSet(nodeId);
        boolean isMetricsOnly = ALLOWED_METRICS.containsAll(metricsOrNodeIds);
        if (isMetricsOnly) {
            nodeIds = new String[] { "_all" };
            metrics = metricsOrNodeIds;
        } else {
            nodeIds = Strings.tokenizeToStringArray(nodeId, ",");
            metrics = Sets.newHashSet("_all");
        }
    } else {
        nodeIds = Strings.tokenizeToStringArray(request.param("nodeId", "_all"), ",");
        metrics = Strings.tokenizeByCommaToSet(request.param("metrics", "_all"));
    }
    final NodesInfoRequest nodesInfoRequest = new NodesInfoRequest(nodeIds);
    nodesInfoRequest.timeout(request.param("timeout"));
    // shortcut, don't do checks if only all is specified
    if (metrics.size() == 1 && metrics.contains("_all")) {
        nodesInfoRequest.all();
    } else {
        nodesInfoRequest.clear();
        // disregard unknown metrics
        metrics.retainAll(ALLOWED_METRICS);
        nodesInfoRequest.addMetrics(metrics.stream().toArray(String[]::new));
    }
    return nodesInfoRequest;
}
Also used : NodesInfoRequest(org.opensearch.action.admin.cluster.node.info.NodesInfoRequest)

Example 2 with NodesInfoRequest

use of org.opensearch.action.admin.cluster.node.info.NodesInfoRequest in project OpenSearch by opensearch-project.

the class RestThreadPoolAction method doCatRequest.

@Override
public RestChannelConsumer doCatRequest(final RestRequest request, final NodeClient client) {
    final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
    clusterStateRequest.clear().nodes(true);
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout()));
    return channel -> client.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) {

        @Override
        public void processResponse(final ClusterStateResponse clusterStateResponse) {
            NodesInfoRequest nodesInfoRequest = new NodesInfoRequest();
            nodesInfoRequest.timeout(request.param("timeout"));
            nodesInfoRequest.clear().addMetrics(NodesInfoRequest.Metric.PROCESS.metricName(), NodesInfoRequest.Metric.THREAD_POOL.metricName());
            client.admin().cluster().nodesInfo(nodesInfoRequest, new RestActionListener<NodesInfoResponse>(channel) {

                @Override
                public void processResponse(final NodesInfoResponse nodesInfoResponse) {
                    NodesStatsRequest nodesStatsRequest = new NodesStatsRequest();
                    nodesStatsRequest.timeout(request.param("timeout"));
                    nodesStatsRequest.clear().addMetric(NodesStatsRequest.Metric.THREAD_POOL.metricName());
                    client.admin().cluster().nodesStats(nodesStatsRequest, new RestResponseListener<NodesStatsResponse>(channel) {

                        @Override
                        public RestResponse buildResponse(NodesStatsResponse nodesStatsResponse) throws Exception {
                            return RestTable.buildResponse(buildTable(request, clusterStateResponse, nodesInfoResponse, nodesStatsResponse), channel);
                        }
                    });
                }
            });
        }
    });
}
Also used : DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ThreadPool(org.opensearch.threadpool.ThreadPool) Table(org.opensearch.common.Table) HashMap(java.util.HashMap) Regex(org.opensearch.common.regex.Regex) HashSet(java.util.HashSet) RestActionListener(org.opensearch.rest.action.RestActionListener) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) RestResponseListener(org.opensearch.rest.action.RestResponseListener) ThreadPoolInfo(org.opensearch.threadpool.ThreadPoolInfo) NodeClient(org.opensearch.client.node.NodeClient) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) NodesInfoRequest(org.opensearch.action.admin.cluster.node.info.NodesInfoRequest) Set(java.util.Set) ThreadPoolStats(org.opensearch.threadpool.ThreadPoolStats) RestResponse(org.opensearch.rest.RestResponse) NodesInfoResponse(org.opensearch.action.admin.cluster.node.info.NodesInfoResponse) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) List(java.util.List) ProcessInfo(org.opensearch.monitor.process.ProcessInfo) TreeMap(java.util.TreeMap) NodeInfo(org.opensearch.action.admin.cluster.node.info.NodeInfo) NodesStatsResponse(org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse) NodeStats(org.opensearch.action.admin.cluster.node.stats.NodeStats) NodesStatsRequest(org.opensearch.action.admin.cluster.node.stats.NodesStatsRequest) Collections(java.util.Collections) NodesInfoResponse(org.opensearch.action.admin.cluster.node.info.NodesInfoResponse) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) RestResponse(org.opensearch.rest.RestResponse) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) RestActionListener(org.opensearch.rest.action.RestActionListener) NodesStatsRequest(org.opensearch.action.admin.cluster.node.stats.NodesStatsRequest) NodesStatsResponse(org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse) NodesInfoRequest(org.opensearch.action.admin.cluster.node.info.NodesInfoRequest)

Example 3 with NodesInfoRequest

use of org.opensearch.action.admin.cluster.node.info.NodesInfoRequest in project job-scheduler by opensearch-project.

the class SampleExtensionPluginIT method testPluginsAreInstalled.

public void testPluginsAreInstalled() {
    ClusterHealthRequest request = new ClusterHealthRequest();
    ClusterHealthResponse response = OpenSearchIntegTestCase.client().admin().cluster().health(request).actionGet();
    Assert.assertEquals(ClusterHealthStatus.GREEN, response.getStatus());
    NodesInfoRequest nodesInfoRequest = new NodesInfoRequest();
    nodesInfoRequest.addMetric(NodesInfoRequest.Metric.PLUGINS.metricName());
    NodesInfoResponse nodesInfoResponse = OpenSearchIntegTestCase.client().admin().cluster().nodesInfo(nodesInfoRequest).actionGet();
    List<PluginInfo> pluginInfos = nodesInfoResponse.getNodes().get(0).getInfo(PluginsAndModules.class).getPluginInfos();
    Assert.assertTrue(pluginInfos.stream().anyMatch(pluginInfo -> pluginInfo.getName().equals("opensearch-job-scheduler")));
    Assert.assertTrue(pluginInfos.stream().anyMatch(pluginInfo -> pluginInfo.getName().equals("opensearch-job-scheduler-sample-extension")));
}
Also used : NodesInfoResponse(org.opensearch.action.admin.cluster.node.info.NodesInfoResponse) NodesInfoResponse(org.opensearch.action.admin.cluster.node.info.NodesInfoResponse) List(java.util.List) ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) NodesInfoRequest(org.opensearch.action.admin.cluster.node.info.NodesInfoRequest) PluginsAndModules(org.opensearch.action.admin.cluster.node.info.PluginsAndModules) PluginInfo(org.opensearch.plugins.PluginInfo) Assert(org.junit.Assert) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) ClusterHealthStatus(org.opensearch.cluster.health.ClusterHealthStatus) ClusterHealthResponse(org.opensearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) PluginsAndModules(org.opensearch.action.admin.cluster.node.info.PluginsAndModules) NodesInfoRequest(org.opensearch.action.admin.cluster.node.info.NodesInfoRequest) PluginInfo(org.opensearch.plugins.PluginInfo)

Example 4 with NodesInfoRequest

use of org.opensearch.action.admin.cluster.node.info.NodesInfoRequest in project OpenSearch by opensearch-project.

the class RestNodesInfoActionTests method testSeparateNodeIdsAndMetrics.

public void testSeparateNodeIdsAndMetrics() {
    Map<String, String> params = new HashMap<>();
    List<String> nodeIds = new ArrayList<>(5);
    List<String> metrics = new ArrayList<>(5);
    for (int i = 0; i < 5; i++) {
        nodeIds.add(randomValueOtherThanMany(ALLOWED_METRICS::contains, () -> randomAlphaOfLength(23)));
        metrics.add(randomFrom(ALLOWED_METRICS));
    }
    params.put("nodeId", String.join(",", nodeIds));
    params.put("metrics", String.join(",", metrics));
    RestRequest restRequest = buildRestRequest(params);
    NodesInfoRequest actual = RestNodesInfoAction.prepareRequest(restRequest);
    assertArrayEquals(nodeIds.toArray(), actual.nodesIds());
    assertMetrics(metrics, actual);
}
Also used : RestRequest(org.opensearch.rest.RestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodesInfoRequest(org.opensearch.action.admin.cluster.node.info.NodesInfoRequest)

Example 5 with NodesInfoRequest

use of org.opensearch.action.admin.cluster.node.info.NodesInfoRequest in project OpenSearch by opensearch-project.

the class RestNodesInfoActionTests method testOnlyMetrics.

public void testOnlyMetrics() {
    Map<String, String> params = new HashMap<>();
    int metricsCount = randomIntBetween(1, ALLOWED_METRICS.size());
    List<String> metrics = new ArrayList<>();
    for (int i = 0; i < metricsCount; i++) {
        metrics.add(randomFrom(ALLOWED_METRICS));
    }
    params.put("nodeId", String.join(",", metrics));
    RestRequest restRequest = buildRestRequest(params);
    NodesInfoRequest actual = RestNodesInfoAction.prepareRequest(restRequest);
    assertArrayEquals(new String[] { "_all" }, actual.nodesIds());
    assertMetrics(metrics, actual);
}
Also used : RestRequest(org.opensearch.rest.RestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodesInfoRequest(org.opensearch.action.admin.cluster.node.info.NodesInfoRequest)

Aggregations

NodesInfoRequest (org.opensearch.action.admin.cluster.node.info.NodesInfoRequest)15 RestRequest (org.opensearch.rest.RestRequest)11 HashMap (java.util.HashMap)8 List (java.util.List)6 NodesInfoResponse (org.opensearch.action.admin.cluster.node.info.NodesInfoResponse)6 NodeClient (org.opensearch.client.node.NodeClient)6 FakeRestRequest (org.opensearch.test.rest.FakeRestRequest)6 NodeInfo (org.opensearch.action.admin.cluster.node.info.NodeInfo)5 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)5 GET (org.opensearch.rest.RestRequest.Method.GET)5 ArrayList (java.util.ArrayList)4 ClusterStateRequest (org.opensearch.action.admin.cluster.state.ClusterStateRequest)4 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)4 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)4 Table (org.opensearch.common.Table)4 RestResponse (org.opensearch.rest.RestResponse)4 RestActionListener (org.opensearch.rest.action.RestActionListener)4 RestResponseListener (org.opensearch.rest.action.RestResponseListener)4 Collections.singletonList (java.util.Collections.singletonList)3 Map (java.util.Map)3