Search in sources :

Example 1 with RestResponse

use of org.opensearch.rest.RestResponse in project OpenSearch by opensearch-project.

the class RestGetMappingAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final GetMappingsRequest getMappingsRequest = new GetMappingsRequest();
    getMappingsRequest.indices(indices);
    getMappingsRequest.indicesOptions(IndicesOptions.fromRequest(request, getMappingsRequest.indicesOptions()));
    final TimeValue timeout = request.paramAsTime("master_timeout", getMappingsRequest.masterNodeTimeout());
    getMappingsRequest.masterNodeTimeout(timeout);
    getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local()));
    return channel -> client.admin().indices().getMappings(getMappingsRequest, new RestActionListener<GetMappingsResponse>(channel) {

        @Override
        protected void processResponse(GetMappingsResponse getMappingsResponse) {
            final long startTimeMs = threadPool.relativeTimeInMillis();
            // Process serialization on GENERIC pool since the serialization of the raw mappings to XContent can be too slow to execute
            // on an IO thread
            threadPool.executor(ThreadPool.Names.MANAGEMENT).execute(ActionRunnable.wrap(this, l -> new RestBuilderListener<GetMappingsResponse>(channel) {

                @Override
                public RestResponse buildResponse(final GetMappingsResponse response, final XContentBuilder builder) throws Exception {
                    if (threadPool.relativeTimeInMillis() - startTimeMs > timeout.millis()) {
                        throw new OpenSearchTimeoutException("Timed out getting mappings");
                    }
                    builder.startObject();
                    response.toXContent(builder, request);
                    builder.endObject();
                    return new BytesRestResponse(RestStatus.OK, builder);
                }
            }.onResponse(getMappingsResponse)));
        }
    });
}
Also used : TimeValue(org.opensearch.common.unit.TimeValue) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) ActionRunnable(org.opensearch.action.ActionRunnable) RestRequest(org.opensearch.rest.RestRequest) GetMappingsResponse(org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse) ThreadPool(org.opensearch.threadpool.ThreadPool) IOException(java.io.IOException) IndicesOptions(org.opensearch.action.support.IndicesOptions) RestStatus(org.opensearch.rest.RestStatus) GetMappingsRequest(org.opensearch.action.admin.indices.mapping.get.GetMappingsRequest) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) Strings(org.opensearch.common.Strings) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) RestActionListener(org.opensearch.rest.action.RestActionListener) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) List(java.util.List) Arrays.asList(java.util.Arrays.asList) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) BaseRestHandler(org.opensearch.rest.BaseRestHandler) OpenSearchTimeoutException(org.opensearch.OpenSearchTimeoutException) BytesRestResponse(org.opensearch.rest.BytesRestResponse) GetMappingsRequest(org.opensearch.action.admin.indices.mapping.get.GetMappingsRequest) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) TimeValue(org.opensearch.common.unit.TimeValue) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) GetMappingsResponse(org.opensearch.action.admin.indices.mapping.get.GetMappingsResponse)

Example 2 with RestResponse

use of org.opensearch.rest.RestResponse in project OpenSearch by opensearch-project.

the class RestIndicesShardStoresAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    IndicesShardStoresRequest indicesShardStoresRequest = new IndicesShardStoresRequest(Strings.splitStringByCommaToArray(request.param("index")));
    if (request.hasParam("status")) {
        indicesShardStoresRequest.shardStatuses(Strings.splitStringByCommaToArray(request.param("status")));
    }
    indicesShardStoresRequest.indicesOptions(IndicesOptions.fromRequest(request, indicesShardStoresRequest.indicesOptions()));
    return channel -> client.admin().indices().shardStores(indicesShardStoresRequest, new RestBuilderListener<IndicesShardStoresResponse>(channel) {

        @Override
        public RestResponse buildResponse(IndicesShardStoresResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            response.toXContent(builder, request);
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
Also used : IndicesShardStoresResponse(org.opensearch.action.admin.indices.shards.IndicesShardStoresResponse) OK(org.opensearch.rest.RestStatus.OK) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) IndicesOptions(org.opensearch.action.support.IndicesOptions) IndicesShardStoresAction(org.opensearch.action.admin.indices.shards.IndicesShardStoresAction) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) Strings(org.opensearch.common.Strings) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IndicesShardStoresRequest(org.opensearch.action.admin.indices.shards.IndicesShardStoresRequest) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) List(java.util.List) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) IndicesShardStoresRequest(org.opensearch.action.admin.indices.shards.IndicesShardStoresRequest) IndicesShardStoresResponse(org.opensearch.action.admin.indices.shards.IndicesShardStoresResponse) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) BytesRestResponse(org.opensearch.rest.BytesRestResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 3 with RestResponse

use of org.opensearch.rest.RestResponse in project OpenSearch by opensearch-project.

the class RestNodesUsageAction method prepareRequest.

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    String[] nodesIds = Strings.splitStringByCommaToArray(request.param("nodeId"));
    Set<String> metrics = Strings.tokenizeByCommaToSet(request.param("metric", "_all"));
    NodesUsageRequest nodesUsageRequest = new NodesUsageRequest(nodesIds);
    nodesUsageRequest.timeout(request.param("timeout"));
    if (metrics.size() == 1 && metrics.contains("_all")) {
        nodesUsageRequest.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 {
        nodesUsageRequest.clear();
        nodesUsageRequest.restActions(metrics.contains("rest_actions"));
        nodesUsageRequest.aggregations(metrics.contains("aggregations"));
    }
    return channel -> client.admin().cluster().nodesUsage(nodesUsageRequest, new RestBuilderListener<NodesUsageResponse>(channel) {

        @Override
        public RestResponse buildResponse(NodesUsageResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            RestActions.buildNodesHeader(builder, channel.request(), response);
            builder.field("cluster_name", response.getClusterName().value());
            response.toXContent(builder, channel.request());
            builder.endObject();
            return new BytesRestResponse(RestStatus.OK, builder);
        }
    });
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) NodesUsageResponse(org.opensearch.action.admin.cluster.node.usage.NodesUsageResponse) Set(java.util.Set) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) Strings(org.opensearch.common.Strings) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) NodesUsageRequest(org.opensearch.action.admin.cluster.node.usage.NodesUsageRequest) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) List(java.util.List) RestActions(org.opensearch.rest.action.RestActions) Locale(java.util.Locale) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) NodesUsageResponse(org.opensearch.action.admin.cluster.node.usage.NodesUsageResponse) IOException(java.io.IOException) BytesRestResponse(org.opensearch.rest.BytesRestResponse) NodesUsageRequest(org.opensearch.action.admin.cluster.node.usage.NodesUsageRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 4 with RestResponse

use of org.opensearch.rest.RestResponse in project OpenSearch by opensearch-project.

the class RestClusterStateAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    final ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest();
    clusterStateRequest.indicesOptions(IndicesOptions.fromRequest(request, clusterStateRequest.indicesOptions()));
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout()));
    if (request.hasParam("wait_for_metadata_version")) {
        clusterStateRequest.waitForMetadataVersion(request.paramAsLong("wait_for_metadata_version", 0));
    }
    clusterStateRequest.waitForTimeout(request.paramAsTime("wait_for_timeout", ClusterStateRequest.DEFAULT_WAIT_FOR_NODE_TIMEOUT));
    final String[] indices = Strings.splitStringByCommaToArray(request.param("indices", "_all"));
    boolean isAllIndicesOnly = indices.length == 1 && "_all".equals(indices[0]);
    if (!isAllIndicesOnly) {
        clusterStateRequest.indices(indices);
    }
    if (request.hasParam("metric")) {
        EnumSet<ClusterState.Metric> metrics = ClusterState.Metric.parseString(request.param("metric"), true);
        // do not ask for what we do not need.
        clusterStateRequest.nodes(metrics.contains(ClusterState.Metric.NODES) || metrics.contains(ClusterState.Metric.MASTER_NODE));
        /*
             * there is no distinction in Java api between routing_table and routing_nodes, it's the same info set over the wire, one single
             * flag to ask for it
             */
        clusterStateRequest.routingTable(metrics.contains(ClusterState.Metric.ROUTING_TABLE) || metrics.contains(ClusterState.Metric.ROUTING_NODES));
        clusterStateRequest.metadata(metrics.contains(ClusterState.Metric.METADATA));
        clusterStateRequest.blocks(metrics.contains(ClusterState.Metric.BLOCKS));
        clusterStateRequest.customs(metrics.contains(ClusterState.Metric.CUSTOMS));
    }
    settingsFilter.addFilterSettingParams(request);
    return channel -> client.admin().cluster().state(clusterStateRequest, new RestBuilderListener<ClusterStateResponse>(channel) {

        @Override
        public RestResponse buildResponse(ClusterStateResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            if (clusterStateRequest.waitForMetadataVersion() != null) {
                builder.field(Fields.WAIT_FOR_TIMED_OUT, response.isWaitForTimedOut());
            }
            builder.field(Fields.CLUSTER_NAME, response.getClusterName().value());
            ToXContent.Params params = new ToXContent.DelegatingMapParams(singletonMap(Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_API), request);
            response.getState().toXContent(builder, params);
            builder.endObject();
            return new BytesRestResponse(RestStatus.OK, builder);
        }
    });
}
Also used : Metadata(org.opensearch.cluster.metadata.Metadata) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ToXContent(org.opensearch.common.xcontent.ToXContent) IndicesOptions(org.opensearch.action.support.IndicesOptions) Strings(org.opensearch.common.Strings) HashSet(java.util.HashSet) Requests(org.opensearch.client.Requests) ClusterState(org.opensearch.cluster.ClusterState) Arrays.asList(java.util.Arrays.asList) Collections.singletonMap(java.util.Collections.singletonMap) BaseRestHandler(org.opensearch.rest.BaseRestHandler) EnumSet(java.util.EnumSet) SettingsFilter(org.opensearch.common.settings.SettingsFilter) NodeClient(org.opensearch.client.node.NodeClient) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) List(java.util.List) Collections(java.util.Collections) ToXContent(org.opensearch.common.xcontent.ToXContent) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) IOException(java.io.IOException) BytesRestResponse(org.opensearch.rest.BytesRestResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 5 with RestResponse

use of org.opensearch.rest.RestResponse in project OpenSearch by opensearch-project.

the class RestNodesHotThreadsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    String[] nodesIds = Strings.splitStringByCommaToArray(request.param("nodeId"));
    NodesHotThreadsRequest nodesHotThreadsRequest = new NodesHotThreadsRequest(nodesIds);
    nodesHotThreadsRequest.threads(request.paramAsInt("threads", nodesHotThreadsRequest.threads()));
    nodesHotThreadsRequest.ignoreIdleThreads(request.paramAsBoolean("ignore_idle_threads", nodesHotThreadsRequest.ignoreIdleThreads()));
    nodesHotThreadsRequest.type(request.param("type", nodesHotThreadsRequest.type()));
    nodesHotThreadsRequest.interval(TimeValue.parseTimeValue(request.param("interval"), nodesHotThreadsRequest.interval(), "interval"));
    nodesHotThreadsRequest.snapshots(request.paramAsInt("snapshots", nodesHotThreadsRequest.snapshots()));
    nodesHotThreadsRequest.timeout(request.param("timeout"));
    return channel -> client.admin().cluster().nodesHotThreads(nodesHotThreadsRequest, new RestResponseListener<NodesHotThreadsResponse>(channel) {

        @Override
        public RestResponse buildResponse(NodesHotThreadsResponse response) throws Exception {
            StringBuilder sb = new StringBuilder();
            for (NodeHotThreads node : response.getNodes()) {
                sb.append("::: ").append(node.getNode().toString()).append("\n");
                Strings.spaceify(3, node.getHotThreads(), sb);
                sb.append('\n');
            }
            return new BytesRestResponse(RestStatus.OK, sb.toString());
        }
    });
}
Also used : TimeValue(org.opensearch.common.unit.TimeValue) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) NodesHotThreadsRequest(org.opensearch.action.admin.cluster.node.hotthreads.NodesHotThreadsRequest) GET(org.opensearch.rest.RestRequest.Method.GET) NodesHotThreadsResponse(org.opensearch.action.admin.cluster.node.hotthreads.NodesHotThreadsResponse) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) BytesRestResponse(org.opensearch.rest.BytesRestResponse) NodeHotThreads(org.opensearch.action.admin.cluster.node.hotthreads.NodeHotThreads) RestResponse(org.opensearch.rest.RestResponse) Strings(org.opensearch.common.Strings) List(java.util.List) Locale(java.util.Locale) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) RestResponseListener(org.opensearch.rest.action.RestResponseListener) NodesHotThreadsRequest(org.opensearch.action.admin.cluster.node.hotthreads.NodesHotThreadsRequest) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) BytesRestResponse(org.opensearch.rest.BytesRestResponse) NodeHotThreads(org.opensearch.action.admin.cluster.node.hotthreads.NodeHotThreads) NodesHotThreadsResponse(org.opensearch.action.admin.cluster.node.hotthreads.NodesHotThreadsResponse) IOException(java.io.IOException)

Aggregations

RestResponse (org.opensearch.rest.RestResponse)44 List (java.util.List)37 NodeClient (org.opensearch.client.node.NodeClient)32 RestRequest (org.opensearch.rest.RestRequest)32 GET (org.opensearch.rest.RestRequest.Method.GET)29 Arrays.asList (java.util.Arrays.asList)21 Collections.unmodifiableList (java.util.Collections.unmodifiableList)21 Strings (org.opensearch.common.Strings)18 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)18 RestResponseListener (org.opensearch.rest.action.RestResponseListener)18 Table (org.opensearch.common.Table)17 BytesRestResponse (org.opensearch.rest.BytesRestResponse)15 IOException (java.io.IOException)14 BaseRestHandler (org.opensearch.rest.BaseRestHandler)13 ClusterStateRequest (org.opensearch.action.admin.cluster.state.ClusterStateRequest)12 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)12 RestStatus (org.opensearch.rest.RestStatus)11 RestBuilderListener (org.opensearch.rest.action.RestBuilderListener)11 IndicesOptions (org.opensearch.action.support.IndicesOptions)9 Collections.singletonList (java.util.Collections.singletonList)8