Search in sources :

Example 1 with NodesResponseRestListener

use of org.opensearch.rest.action.RestActions.NodesResponseRestListener in project OpenSearch by opensearch-project.

the class RestNodesStatsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    String[] nodesIds = Strings.splitStringByCommaToArray(request.param("nodeId"));
    Set<String> metrics = Strings.tokenizeByCommaToSet(request.param("metric", "_all"));
    NodesStatsRequest nodesStatsRequest = new NodesStatsRequest(nodesIds);
    nodesStatsRequest.timeout(request.param("timeout"));
    if (metrics.size() == 1 && metrics.contains("_all")) {
        if (request.hasParam("index_metric")) {
            throw new IllegalArgumentException(String.format(Locale.ROOT, "request [%s] contains index metrics [%s] but all stats requested", request.path(), request.param("index_metric")));
        }
        nodesStatsRequest.all();
        nodesStatsRequest.indices(CommonStatsFlags.ALL);
    } else if (metrics.contains("_all")) {
        throw new IllegalArgumentException(String.format(Locale.ROOT, "request [%s] contains _all and individual metrics [%s]", request.path(), request.param("metric")));
    } else {
        nodesStatsRequest.clear();
        // use a sorted set so the unrecognized parameters appear in a reliable sorted order
        final Set<String> invalidMetrics = new TreeSet<>();
        for (final String metric : metrics) {
            final Consumer<NodesStatsRequest> handler = METRICS.get(metric);
            if (handler != null) {
                handler.accept(nodesStatsRequest);
            } else {
                invalidMetrics.add(metric);
            }
        }
        if (!invalidMetrics.isEmpty()) {
            throw new IllegalArgumentException(unrecognized(request, invalidMetrics, METRICS.keySet(), "metric"));
        }
        // check for index specific metrics
        if (metrics.contains("indices")) {
            Set<String> indexMetrics = Strings.tokenizeByCommaToSet(request.param("index_metric", "_all"));
            if (indexMetrics.size() == 1 && indexMetrics.contains("_all")) {
                nodesStatsRequest.indices(CommonStatsFlags.ALL);
            } else {
                CommonStatsFlags flags = new CommonStatsFlags();
                flags.clear();
                // use a sorted set so the unrecognized parameters appear in a reliable sorted order
                final Set<String> invalidIndexMetrics = new TreeSet<>();
                for (final String indexMetric : indexMetrics) {
                    final Consumer<CommonStatsFlags> handler = FLAGS.get(indexMetric);
                    if (handler != null) {
                        handler.accept(flags);
                    } else {
                        invalidIndexMetrics.add(indexMetric);
                    }
                }
                if (!invalidIndexMetrics.isEmpty()) {
                    throw new IllegalArgumentException(unrecognized(request, invalidIndexMetrics, FLAGS.keySet(), "index metric"));
                }
                nodesStatsRequest.indices(flags);
            }
        } else if (request.hasParam("index_metric")) {
            throw new IllegalArgumentException(String.format(Locale.ROOT, "request [%s] contains index metrics [%s] but indices stats not requested", request.path(), request.param("index_metric")));
        }
    }
    if (nodesStatsRequest.indices().isSet(Flag.FieldData) && (request.hasParam("fields") || request.hasParam("fielddata_fields"))) {
        nodesStatsRequest.indices().fieldDataFields(request.paramAsStringArray("fielddata_fields", request.paramAsStringArray("fields", null)));
    }
    if (nodesStatsRequest.indices().isSet(Flag.Completion) && (request.hasParam("fields") || request.hasParam("completion_fields"))) {
        nodesStatsRequest.indices().completionDataFields(request.paramAsStringArray("completion_fields", request.paramAsStringArray("fields", null)));
    }
    if (nodesStatsRequest.indices().isSet(Flag.Search) && (request.hasParam("groups"))) {
        nodesStatsRequest.indices().groups(request.paramAsStringArray("groups", null));
    }
    if (nodesStatsRequest.indices().isSet(Flag.Segments)) {
        nodesStatsRequest.indices().includeSegmentFileSizes(request.paramAsBoolean("include_segment_file_sizes", false));
    }
    if (request.hasParam("include_all")) {
        nodesStatsRequest.indices().includeAllShardIndexingPressureTrackers(request.paramAsBoolean("include_all", false));
    }
    if (request.hasParam("top")) {
        nodesStatsRequest.indices().includeOnlyTopIndexingPressureMetrics(request.paramAsBoolean("top", false));
    }
    return channel -> client.admin().cluster().nodesStats(nodesStatsRequest, new NodesResponseRestListener<>(channel));
}
Also used : NodesStatsRequest(org.opensearch.action.admin.cluster.node.stats.NodesStatsRequest) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) CommonStatsFlags(org.opensearch.action.admin.indices.stats.CommonStatsFlags) TreeSet(java.util.TreeSet) Strings(org.opensearch.common.Strings) Consumer(java.util.function.Consumer) List(java.util.List) Flag(org.opensearch.action.admin.indices.stats.CommonStatsFlags.Flag) Locale(java.util.Locale) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) NodesStatsRequest(org.opensearch.action.admin.cluster.node.stats.NodesStatsRequest) BaseRestHandler(org.opensearch.rest.BaseRestHandler) Collections(java.util.Collections) NodesResponseRestListener(org.opensearch.rest.action.RestActions.NodesResponseRestListener) Set(java.util.Set) TreeSet(java.util.TreeSet) Consumer(java.util.function.Consumer) CommonStatsFlags(org.opensearch.action.admin.indices.stats.CommonStatsFlags)

Example 2 with NodesResponseRestListener

use of org.opensearch.rest.action.RestActions.NodesResponseRestListener in project OpenSearch by opensearch-project.

the class RestClusterStatsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClusterStatsRequest clusterStatsRequest = new ClusterStatsRequest().nodesIds(request.paramAsStringArray("nodeId", null));
    clusterStatsRequest.timeout(request.param("timeout"));
    return channel -> client.admin().cluster().clusterStats(clusterStatsRequest, new NodesResponseRestListener<>(channel));
}
Also used : List(java.util.List) ClusterStatsRequest(org.opensearch.action.admin.cluster.stats.ClusterStatsRequest) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Arrays.asList(java.util.Arrays.asList) IOException(java.io.IOException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) NodesResponseRestListener(org.opensearch.rest.action.RestActions.NodesResponseRestListener) ClusterStatsRequest(org.opensearch.action.admin.cluster.stats.ClusterStatsRequest)

Example 3 with NodesResponseRestListener

use of org.opensearch.rest.action.RestActions.NodesResponseRestListener in project OpenSearch by opensearch-project.

the class RestNodesInfoAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final NodesInfoRequest nodesInfoRequest = prepareRequest(request);
    nodesInfoRequest.timeout(request.param("timeout"));
    settingsFilter.addFilterSettingParams(request);
    return channel -> client.admin().cluster().nodesInfo(nodesInfoRequest, new NodesResponseRestListener<>(channel));
}
Also used : SettingsFilter(org.opensearch.common.settings.SettingsFilter) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) 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) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) Strings(org.opensearch.common.Strings) Sets(org.opensearch.common.util.set.Sets) List(java.util.List) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) NodesResponseRestListener(org.opensearch.rest.action.RestActions.NodesResponseRestListener) NodesInfoRequest(org.opensearch.action.admin.cluster.node.info.NodesInfoRequest)

Aggregations

IOException (java.io.IOException)3 Arrays.asList (java.util.Arrays.asList)3 Collections.unmodifiableList (java.util.Collections.unmodifiableList)3 List (java.util.List)3 NodeClient (org.opensearch.client.node.NodeClient)3 BaseRestHandler (org.opensearch.rest.BaseRestHandler)3 RestRequest (org.opensearch.rest.RestRequest)3 GET (org.opensearch.rest.RestRequest.Method.GET)3 NodesResponseRestListener (org.opensearch.rest.action.RestActions.NodesResponseRestListener)3 Set (java.util.Set)2 Strings (org.opensearch.common.Strings)2 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 Consumer (java.util.function.Consumer)1 NodesInfoRequest (org.opensearch.action.admin.cluster.node.info.NodesInfoRequest)1 NodesStatsRequest (org.opensearch.action.admin.cluster.node.stats.NodesStatsRequest)1 ClusterStatsRequest (org.opensearch.action.admin.cluster.stats.ClusterStatsRequest)1