Search in sources :

Example 1 with ClusterStateRequest

use of org.opensearch.action.admin.cluster.state.ClusterStateRequest in project OpenSearch by opensearch-project.

the class CrossClusterSearchUnavailableClusterIT method startTransport.

private static MockTransportService startTransport(final String id, final List<DiscoveryNode> knownNodes, final Version version, final ThreadPool threadPool) {
    boolean success = false;
    final Settings s = Settings.builder().put("node.name", id).build();
    ClusterName clusterName = ClusterName.CLUSTER_NAME_SETTING.get(s);
    MockTransportService newService = MockTransportService.createNewService(s, version, threadPool, null);
    try {
        newService.registerRequestHandler(ClusterSearchShardsAction.NAME, ThreadPool.Names.SAME, ClusterSearchShardsRequest::new, (request, channel, task) -> {
            channel.sendResponse(new ClusterSearchShardsResponse(new ClusterSearchShardsGroup[0], knownNodes.toArray(new DiscoveryNode[0]), Collections.emptyMap()));
        });
        newService.registerRequestHandler(SearchAction.NAME, ThreadPool.Names.SAME, SearchRequest::new, (request, channel, task) -> {
            InternalSearchResponse response = new InternalSearchResponse(new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), Float.NaN), InternalAggregations.EMPTY, null, null, false, null, 1);
            SearchResponse searchResponse = new SearchResponse(response, null, 1, 1, 0, 100, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
            channel.sendResponse(searchResponse);
        });
        newService.registerRequestHandler(ClusterStateAction.NAME, ThreadPool.Names.SAME, ClusterStateRequest::new, (request, channel, task) -> {
            DiscoveryNodes.Builder builder = DiscoveryNodes.builder();
            for (DiscoveryNode node : knownNodes) {
                builder.add(node);
            }
            ClusterState build = ClusterState.builder(clusterName).nodes(builder.build()).build();
            channel.sendResponse(new ClusterStateResponse(clusterName, build, false));
        });
        newService.start();
        newService.acceptIncomingRequests();
        success = true;
        return newService;
    } finally {
        if (success == false) {
            newService.close();
        }
    }
}
Also used : ClusterSearchShardsResponse(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse) TotalHits(org.apache.lucene.search.TotalHits) SearchRequest(org.opensearch.action.search.SearchRequest) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransportService(org.opensearch.test.transport.MockTransportService) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) ClusterSearchShardsGroup(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsGroup) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) ClusterSearchShardsRequest(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest) ClusterName(org.opensearch.cluster.ClusterName) Settings(org.opensearch.common.settings.Settings) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 2 with ClusterStateRequest

use of org.opensearch.action.admin.cluster.state.ClusterStateRequest 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 3 with ClusterStateRequest

use of org.opensearch.action.admin.cluster.state.ClusterStateRequest in project OpenSearch by opensearch-project.

the class RestTemplatesAction method doCatRequest.

@Override
protected RestChannelConsumer doCatRequest(final RestRequest request, NodeClient client) {
    final String matchPattern = request.hasParam("name") ? request.param("name") : null;
    final ClusterStateRequest clusterStateRequest = new ClusterStateRequest();
    clusterStateRequest.clear().metadata(true);
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    clusterStateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", clusterStateRequest.masterNodeTimeout()));
    return channel -> client.admin().cluster().state(clusterStateRequest, new RestResponseListener<ClusterStateResponse>(channel) {

        @Override
        public RestResponse buildResponse(ClusterStateResponse clusterStateResponse) throws Exception {
            return RestTable.buildResponse(buildTable(request, clusterStateResponse, matchPattern), channel);
        }
    });
}
Also used : IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) NodeClient(org.opensearch.client.node.NodeClient) Metadata(org.opensearch.cluster.metadata.Metadata) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) Table(org.opensearch.common.Table) Regex(org.opensearch.common.regex.Regex) RestResponse(org.opensearch.rest.RestResponse) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) List(java.util.List) Arrays.asList(java.util.Arrays.asList) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Map(java.util.Map) RestResponseListener(org.opensearch.rest.action.RestResponseListener) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) RestResponse(org.opensearch.rest.RestResponse) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest)

Example 4 with ClusterStateRequest

use of org.opensearch.action.admin.cluster.state.ClusterStateRequest 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 5 with ClusterStateRequest

use of org.opensearch.action.admin.cluster.state.ClusterStateRequest in project OpenSearch by opensearch-project.

the class RestMasterAction 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 RestResponseListener<ClusterStateResponse>(channel) {

        @Override
        public RestResponse buildResponse(final ClusterStateResponse clusterStateResponse) throws Exception {
            return RestTable.buildResponse(buildTable(request, clusterStateResponse), channel);
        }
    });
}
Also used : ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) List(java.util.List) NodeClient(org.opensearch.client.node.NodeClient) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) Table(org.opensearch.common.Table) RestResponse(org.opensearch.rest.RestResponse) RestResponseListener(org.opensearch.rest.action.RestResponseListener) Collections.singletonList(java.util.Collections.singletonList) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) RestResponse(org.opensearch.rest.RestResponse) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest)

Aggregations

ClusterStateRequest (org.opensearch.action.admin.cluster.state.ClusterStateRequest)18 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)17 List (java.util.List)13 NodeClient (org.opensearch.client.node.NodeClient)12 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)12 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)11 RestRequest (org.opensearch.rest.RestRequest)11 GET (org.opensearch.rest.RestRequest.Method.GET)11 RestResponse (org.opensearch.rest.RestResponse)11 Table (org.opensearch.common.Table)9 RestResponseListener (org.opensearch.rest.action.RestResponseListener)9 Settings (org.opensearch.common.settings.Settings)8 Strings (org.opensearch.common.Strings)7 Arrays.asList (java.util.Arrays.asList)6 Collections.unmodifiableList (java.util.Collections.unmodifiableList)6 ClusterState (org.opensearch.cluster.ClusterState)6 RestActionListener (org.opensearch.rest.action.RestActionListener)6 IOException (java.io.IOException)5 Map (java.util.Map)5 Collections (java.util.Collections)4