Search in sources :

Example 1 with ClusterSearchShardsRequest

use of org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest in project OpenSearch by opensearch-project.

the class TransportSearchAction method collectSearchShards.

static void collectSearchShards(IndicesOptions indicesOptions, String preference, String routing, AtomicInteger skippedClusters, Map<String, OriginalIndices> remoteIndicesByCluster, RemoteClusterService remoteClusterService, ThreadPool threadPool, ActionListener<Map<String, ClusterSearchShardsResponse>> listener) {
    final CountDown responsesCountDown = new CountDown(remoteIndicesByCluster.size());
    final Map<String, ClusterSearchShardsResponse> searchShardsResponses = new ConcurrentHashMap<>();
    final AtomicReference<Exception> exceptions = new AtomicReference<>();
    for (Map.Entry<String, OriginalIndices> entry : remoteIndicesByCluster.entrySet()) {
        final String clusterAlias = entry.getKey();
        boolean skipUnavailable = remoteClusterService.isSkipUnavailable(clusterAlias);
        Client clusterClient = remoteClusterService.getRemoteClusterClient(threadPool, clusterAlias);
        final String[] indices = entry.getValue().indices();
        ClusterSearchShardsRequest searchShardsRequest = new ClusterSearchShardsRequest(indices).indicesOptions(indicesOptions).local(true).preference(preference).routing(routing);
        clusterClient.admin().cluster().searchShards(searchShardsRequest, new CCSActionListener<ClusterSearchShardsResponse, Map<String, ClusterSearchShardsResponse>>(clusterAlias, skipUnavailable, responsesCountDown, skippedClusters, exceptions, listener) {

            @Override
            void innerOnResponse(ClusterSearchShardsResponse clusterSearchShardsResponse) {
                searchShardsResponses.put(clusterAlias, clusterSearchShardsResponse);
            }

            @Override
            Map<String, ClusterSearchShardsResponse> createFinalResponse() {
                return searchShardsResponses;
            }
        });
    }
}
Also used : ClusterSearchShardsResponse(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDown(org.opensearch.common.util.concurrent.CountDown) RemoteTransportException(org.opensearch.transport.RemoteTransportException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) ClusterSearchShardsRequest(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Client(org.opensearch.client.Client) NodeClient(org.opensearch.client.node.NodeClient) OriginSettingClient(org.opensearch.client.OriginSettingClient) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) OriginalIndices(org.opensearch.action.OriginalIndices)

Example 2 with ClusterSearchShardsRequest

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

use of org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest in project OpenSearch by opensearch-project.

the class RestClusterSearchShardsAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final ClusterSearchShardsRequest clusterSearchShardsRequest = Requests.clusterSearchShardsRequest(indices);
    clusterSearchShardsRequest.local(request.paramAsBoolean("local", clusterSearchShardsRequest.local()));
    clusterSearchShardsRequest.routing(request.param("routing"));
    clusterSearchShardsRequest.preference(request.param("preference"));
    clusterSearchShardsRequest.indicesOptions(IndicesOptions.fromRequest(request, clusterSearchShardsRequest.indicesOptions()));
    return channel -> client.admin().cluster().searchShards(clusterSearchShardsRequest, new RestToXContentListener<>(channel));
}
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) IOException(java.io.IOException) IndicesOptions(org.opensearch.action.support.IndicesOptions) Strings(org.opensearch.common.Strings) ClusterSearchShardsRequest(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest) Requests(org.opensearch.client.Requests) List(java.util.List) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) ClusterSearchShardsRequest(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest)

Example 4 with ClusterSearchShardsRequest

use of org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest in project OpenSearch by opensearch-project.

the class RemoteClusterAwareClientTests method testSearchShards.

public void testSearchShards() throws Exception {
    List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>();
    try (MockTransportService seedTransport = startTransport("seed_node", knownNodes);
        MockTransportService discoverableTransport = startTransport("discoverable_node", knownNodes)) {
        knownNodes.add(seedTransport.getLocalDiscoNode());
        knownNodes.add(discoverableTransport.getLocalDiscoNode());
        Collections.shuffle(knownNodes, random());
        Settings.Builder builder = Settings.builder();
        builder.putList("cluster.remote.cluster1.seeds", seedTransport.getLocalDiscoNode().getAddress().toString());
        try (MockTransportService service = MockTransportService.createNewService(builder.build(), Version.CURRENT, threadPool, null)) {
            service.start();
            service.acceptIncomingRequests();
            try (RemoteClusterAwareClient client = new RemoteClusterAwareClient(Settings.EMPTY, threadPool, service, "cluster1")) {
                SearchRequest request = new SearchRequest("test-index");
                CountDownLatch responseLatch = new CountDownLatch(1);
                AtomicReference<ClusterSearchShardsResponse> reference = new AtomicReference<>();
                ClusterSearchShardsRequest searchShardsRequest = new ClusterSearchShardsRequest("test-index").indicesOptions(request.indicesOptions()).local(true).preference(request.preference()).routing(request.routing());
                client.admin().cluster().searchShards(searchShardsRequest, new LatchedActionListener<>(ActionListener.wrap(reference::set, e -> fail("no failures expected")), responseLatch));
                responseLatch.await();
                assertNotNull(reference.get());
                ClusterSearchShardsResponse clusterSearchShardsResponse = reference.get();
                assertEquals(knownNodes, Arrays.asList(clusterSearchShardsResponse.getNodes()));
            }
        }
    }
}
Also used : ClusterSearchShardsResponse(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse) SearchRequest(org.opensearch.action.search.SearchRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransportService(org.opensearch.test.transport.MockTransportService) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterSearchShardsRequest(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest) Settings(org.opensearch.common.settings.Settings) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 5 with ClusterSearchShardsRequest

use of org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest in project OpenSearch by opensearch-project.

the class RemoteClusterAwareClientTests method testSearchShardsThreadContextHeader.

public void testSearchShardsThreadContextHeader() {
    List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>();
    try (MockTransportService seedTransport = startTransport("seed_node", knownNodes);
        MockTransportService discoverableTransport = startTransport("discoverable_node", knownNodes)) {
        knownNodes.add(seedTransport.getLocalDiscoNode());
        knownNodes.add(discoverableTransport.getLocalDiscoNode());
        Collections.shuffle(knownNodes, random());
        Settings.Builder builder = Settings.builder();
        builder.putList("cluster.remote.cluster1.seeds", seedTransport.getLocalDiscoNode().getAddress().toString());
        try (MockTransportService service = MockTransportService.createNewService(builder.build(), Version.CURRENT, threadPool, null)) {
            service.start();
            service.acceptIncomingRequests();
            try (RemoteClusterAwareClient client = new RemoteClusterAwareClient(Settings.EMPTY, threadPool, service, "cluster1")) {
                SearchRequest request = new SearchRequest("test-index");
                int numThreads = 10;
                ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
                for (int i = 0; i < numThreads; i++) {
                    final String threadId = Integer.toString(i);
                    executorService.submit(() -> {
                        ThreadContext threadContext = seedTransport.threadPool.getThreadContext();
                        threadContext.putHeader("threadId", threadId);
                        AtomicReference<ClusterSearchShardsResponse> reference = new AtomicReference<>();
                        final ClusterSearchShardsRequest searchShardsRequest = new ClusterSearchShardsRequest("test-index").indicesOptions(request.indicesOptions()).local(true).preference(request.preference()).routing(request.routing());
                        CountDownLatch responseLatch = new CountDownLatch(1);
                        client.admin().cluster().searchShards(searchShardsRequest, new LatchedActionListener<>(ActionListener.wrap(resp -> {
                            reference.set(resp);
                            assertEquals(threadId, seedTransport.threadPool.getThreadContext().getHeader("threadId"));
                        }, e -> fail("no failures expected")), responseLatch));
                        try {
                            responseLatch.await();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        assertNotNull(reference.get());
                        ClusterSearchShardsResponse clusterSearchShardsResponse = reference.get();
                        assertEquals(knownNodes, Arrays.asList(clusterSearchShardsResponse.getNodes()));
                    });
                }
                ThreadPool.terminate(executorService, 5, TimeUnit.SECONDS);
            }
        }
    }
}
Also used : ClusterSearchShardsResponse(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse) SearchRequest(org.opensearch.action.search.SearchRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransportService(org.opensearch.test.transport.MockTransportService) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) ClusterSearchShardsRequest(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest) Settings(org.opensearch.common.settings.Settings) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

ClusterSearchShardsRequest (org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest)7 ClusterSearchShardsResponse (org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse)6 SearchRequest (org.opensearch.action.search.SearchRequest)4 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)4 Settings (org.opensearch.common.settings.Settings)4 MockTransportService (org.opensearch.test.transport.MockTransportService)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 TotalHits (org.apache.lucene.search.TotalHits)2 ClusterSearchShardsGroup (org.opensearch.action.admin.cluster.shards.ClusterSearchShardsGroup)2 ClusterStateRequest (org.opensearch.action.admin.cluster.state.ClusterStateRequest)2 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)2 SearchResponse (org.opensearch.action.search.SearchResponse)2 NodeClient (org.opensearch.client.node.NodeClient)2 ClusterName (org.opensearch.cluster.ClusterName)2 ClusterState (org.opensearch.cluster.ClusterState)2 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)2 IOException (java.io.IOException)1 Arrays.asList (java.util.Arrays.asList)1