Search in sources :

Example 1 with RestBuilderListener

use of org.opensearch.rest.action.RestBuilderListener 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 RestBuilderListener

use of org.opensearch.rest.action.RestBuilderListener 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 RestBuilderListener

use of org.opensearch.rest.action.RestBuilderListener 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 RestBuilderListener

use of org.opensearch.rest.action.RestBuilderListener 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 RestBuilderListener

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

the class RestClusterAllocationExplainAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClusterAllocationExplainRequest req;
    if (request.hasContentOrSourceParam() == false) {
        // Empty request signals "explain the first unassigned shard you find"
        req = new ClusterAllocationExplainRequest();
    } else {
        try (XContentParser parser = request.contentOrSourceParamParser()) {
            req = ClusterAllocationExplainRequest.parse(parser);
        }
    }
    req.includeYesDecisions(request.paramAsBoolean("include_yes_decisions", false));
    req.includeDiskInfo(request.paramAsBoolean("include_disk_info", false));
    return channel -> client.admin().cluster().allocationExplain(req, new RestBuilderListener<ClusterAllocationExplainResponse>(channel) {

        @Override
        public RestResponse buildResponse(ClusterAllocationExplainResponse response, XContentBuilder builder) throws IOException {
            response.getExplanation().toXContent(builder, ToXContent.EMPTY_PARAMS);
            return new BytesRestResponse(RestStatus.OK, builder);
        }
    });
}
Also used : POST(org.opensearch.rest.RestRequest.Method.POST) NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) ToXContent(org.opensearch.common.xcontent.ToXContent) 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) XContentParser(org.opensearch.common.xcontent.XContentParser) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) List(java.util.List) Arrays.asList(java.util.Arrays.asList) ClusterAllocationExplainRequest(org.opensearch.action.admin.cluster.allocation.ClusterAllocationExplainRequest) ClusterAllocationExplainResponse(org.opensearch.action.admin.cluster.allocation.ClusterAllocationExplainResponse) BaseRestHandler(org.opensearch.rest.BaseRestHandler) ClusterAllocationExplainRequest(org.opensearch.action.admin.cluster.allocation.ClusterAllocationExplainRequest) ClusterAllocationExplainResponse(org.opensearch.action.admin.cluster.allocation.ClusterAllocationExplainResponse) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) BytesRestResponse(org.opensearch.rest.BytesRestResponse) IOException(java.io.IOException) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

IOException (java.io.IOException)11 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)11 BytesRestResponse (org.opensearch.rest.BytesRestResponse)11 RestResponse (org.opensearch.rest.RestResponse)11 RestBuilderListener (org.opensearch.rest.action.RestBuilderListener)11 List (java.util.List)10 NodeClient (org.opensearch.client.node.NodeClient)10 BaseRestHandler (org.opensearch.rest.BaseRestHandler)10 RestRequest (org.opensearch.rest.RestRequest)10 GET (org.opensearch.rest.RestRequest.Method.GET)10 Arrays.asList (java.util.Arrays.asList)9 Collections.unmodifiableList (java.util.Collections.unmodifiableList)9 Strings (org.opensearch.common.Strings)8 RestStatus (org.opensearch.rest.RestStatus)8 IndicesOptions (org.opensearch.action.support.IndicesOptions)6 Set (java.util.Set)5 ToXContent (org.opensearch.common.xcontent.ToXContent)4 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 Locale (java.util.Locale)2