Search in sources :

Example 6 with ClusterSearchShardsResponse

use of org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse in project elasticsearch by elastic.

the class RemoteClusterConnectionTests method startTransport.

public static MockTransportService startTransport(String id, List<DiscoveryNode> knownNodes, Version version, ThreadPool threadPool) {
    boolean success = false;
    MockTransportService newService = MockTransportService.createNewService(Settings.EMPTY, version, threadPool, null);
    try {
        newService.registerRequestHandler(ClusterSearchShardsAction.NAME, ClusterSearchShardsRequest::new, ThreadPool.Names.SAME, (request, channel) -> {
            channel.sendResponse(new ClusterSearchShardsResponse(new ClusterSearchShardsGroup[0], knownNodes.toArray(new DiscoveryNode[0]), Collections.emptyMap()));
        });
        newService.registerRequestHandler(ClusterStateAction.NAME, ClusterStateRequest::new, ThreadPool.Names.SAME, (request, channel) -> {
            DiscoveryNodes.Builder builder = DiscoveryNodes.builder();
            for (DiscoveryNode node : knownNodes) {
                builder.add(node);
            }
            ClusterState build = ClusterState.builder(ClusterName.DEFAULT).nodes(builder.build()).build();
            channel.sendResponse(new ClusterStateResponse(ClusterName.DEFAULT, build, 0L));
        });
        newService.start();
        newService.acceptIncomingRequests();
        success = true;
        return newService;
    } finally {
        if (success == false) {
            newService.close();
        }
    }
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) ClusterSearchShardsRequest(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest) ClusterSearchShardsGroup(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 7 with ClusterSearchShardsResponse

use of org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse in project elasticsearch by elastic.

the class RemoteClusterConnectionTests method testFetchShards.

public void testFetchShards() throws Exception {
    List<DiscoveryNode> knownNodes = new CopyOnWriteArrayList<>();
    try (MockTransportService seedTransport = startTransport("seed_node", knownNodes, Version.CURRENT);
        MockTransportService discoverableTransport = startTransport("discoverable_node", knownNodes, Version.CURRENT)) {
        DiscoveryNode seedNode = seedTransport.getLocalDiscoNode();
        knownNodes.add(seedTransport.getLocalDiscoNode());
        knownNodes.add(discoverableTransport.getLocalDiscoNode());
        Collections.shuffle(knownNodes, random());
        try (MockTransportService service = MockTransportService.createNewService(Settings.EMPTY, Version.CURRENT, threadPool, null)) {
            service.start();
            service.acceptIncomingRequests();
            try (RemoteClusterConnection connection = new RemoteClusterConnection(Settings.EMPTY, "test-cluster", Arrays.asList(seedNode), service, Integer.MAX_VALUE, n -> true)) {
                if (randomBoolean()) {
                    updateSeedNodes(connection, Arrays.asList(seedNode));
                }
                SearchRequest request = new SearchRequest("test-index");
                CountDownLatch responseLatch = new CountDownLatch(1);
                AtomicReference<ClusterSearchShardsResponse> reference = new AtomicReference<>();
                AtomicReference<Exception> failReference = new AtomicReference<>();
                ActionListener<ClusterSearchShardsResponse> shardsListener = ActionListener.wrap(x -> {
                    reference.set(x);
                    responseLatch.countDown();
                }, x -> {
                    failReference.set(x);
                    responseLatch.countDown();
                });
                connection.fetchSearchShards(request, Arrays.asList("test-index"), shardsListener);
                responseLatch.await();
                assertNull(failReference.get());
                assertNotNull(reference.get());
                ClusterSearchShardsResponse clusterSearchShardsResponse = reference.get();
                assertEquals(knownNodes, Arrays.asList(clusterSearchShardsResponse.getNodes()));
                assertTrue(connection.assertNoRunningConnections());
            }
        }
    }
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) AlreadyConnectedException(java.nio.channels.AlreadyConnectedException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) UnknownHostException(java.net.UnknownHostException) UncheckedIOException(java.io.UncheckedIOException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 8 with ClusterSearchShardsResponse

use of org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse in project elasticsearch by elastic.

the class RemoteClusterServiceTests method testProcessRemoteShards.

public void testProcessRemoteShards() throws IOException {
    try (RemoteClusterService service = new RemoteClusterService(Settings.EMPTY, null)) {
        assertFalse(service.isCrossClusterSearchEnabled());
        List<ShardIterator> iteratorList = new ArrayList<>();
        Map<String, ClusterSearchShardsResponse> searchShardsResponseMap = new HashMap<>();
        DiscoveryNode[] nodes = new DiscoveryNode[] { new DiscoveryNode("node1", buildNewFakeTransportAddress(), Version.CURRENT), new DiscoveryNode("node2", buildNewFakeTransportAddress(), Version.CURRENT) };
        Map<String, AliasFilter> indicesAndAliases = new HashMap<>();
        indicesAndAliases.put("foo", new AliasFilter(new TermsQueryBuilder("foo", "bar"), Strings.EMPTY_ARRAY));
        indicesAndAliases.put("bar", new AliasFilter(new MatchAllQueryBuilder(), Strings.EMPTY_ARRAY));
        ClusterSearchShardsGroup[] groups = new ClusterSearchShardsGroup[] { new ClusterSearchShardsGroup(new ShardId("foo", "foo_id", 0), new ShardRouting[] { TestShardRouting.newShardRouting("foo", 0, "node1", true, ShardRoutingState.STARTED), TestShardRouting.newShardRouting("foo", 0, "node2", false, ShardRoutingState.STARTED) }), new ClusterSearchShardsGroup(new ShardId("foo", "foo_id", 1), new ShardRouting[] { TestShardRouting.newShardRouting("foo", 0, "node1", true, ShardRoutingState.STARTED), TestShardRouting.newShardRouting("foo", 1, "node2", false, ShardRoutingState.STARTED) }), new ClusterSearchShardsGroup(new ShardId("bar", "bar_id", 0), new ShardRouting[] { TestShardRouting.newShardRouting("bar", 0, "node2", true, ShardRoutingState.STARTED), TestShardRouting.newShardRouting("bar", 0, "node1", false, ShardRoutingState.STARTED) }) };
        searchShardsResponseMap.put("test_cluster_1", new ClusterSearchShardsResponse(groups, nodes, indicesAndAliases));
        Map<String, AliasFilter> remoteAliases = new HashMap<>();
        service.processRemoteShards(searchShardsResponseMap, iteratorList, remoteAliases);
        assertEquals(3, iteratorList.size());
        for (ShardIterator iterator : iteratorList) {
            if (iterator.shardId().getIndexName().endsWith("foo")) {
                assertTrue(iterator.shardId().getId() == 0 || iterator.shardId().getId() == 1);
                assertEquals("test_cluster_1:foo", iterator.shardId().getIndexName());
                ShardRouting shardRouting = iterator.nextOrNull();
                assertNotNull(shardRouting);
                assertEquals(shardRouting.getIndexName(), "foo");
                shardRouting = iterator.nextOrNull();
                assertNotNull(shardRouting);
                assertEquals(shardRouting.getIndexName(), "foo");
                assertNull(iterator.nextOrNull());
            } else {
                assertEquals(0, iterator.shardId().getId());
                assertEquals("test_cluster_1:bar", iterator.shardId().getIndexName());
                ShardRouting shardRouting = iterator.nextOrNull();
                assertNotNull(shardRouting);
                assertEquals(shardRouting.getIndexName(), "bar");
                shardRouting = iterator.nextOrNull();
                assertNotNull(shardRouting);
                assertEquals(shardRouting.getIndexName(), "bar");
                assertNull(iterator.nextOrNull());
            }
        }
        assertEquals(2, remoteAliases.size());
        assertTrue(remoteAliases.toString(), remoteAliases.containsKey("foo_id"));
        assertTrue(remoteAliases.toString(), remoteAliases.containsKey("bar_id"));
        assertEquals(new TermsQueryBuilder("foo", "bar"), remoteAliases.get("foo_id").getQueryBuilder());
        assertEquals(new MatchAllQueryBuilder(), remoteAliases.get("bar_id").getQueryBuilder());
    }
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) AliasFilter(org.elasticsearch.search.internal.AliasFilter) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ClusterSearchShardsGroup(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup) ShardId(org.elasticsearch.index.shard.ShardId) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 9 with ClusterSearchShardsResponse

use of org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse in project elasticsearch by elastic.

the class ClusterSearchShardsIT method testMultipleIndicesAllocation.

public void testMultipleIndicesAllocation() throws Exception {
    client().admin().indices().prepareCreate("test1").setSettings(Settings.builder().put("index.number_of_shards", "4").put("index.number_of_replicas", 1)).execute().actionGet();
    client().admin().indices().prepareCreate("test2").setSettings(Settings.builder().put("index.number_of_shards", "4").put("index.number_of_replicas", 1)).execute().actionGet();
    client().admin().indices().prepareAliases().addAliasAction(AliasActions.add().index("test1").alias("routing_alias").routing("ABC")).addAliasAction(AliasActions.add().index("test2").alias("routing_alias").routing("EFG")).get();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
    ClusterSearchShardsResponse response = client().admin().cluster().prepareSearchShards("routing_alias").execute().actionGet();
    assertThat(response.getGroups().length, equalTo(2));
    assertThat(response.getGroups()[0].getShards().length, equalTo(2));
    assertThat(response.getGroups()[1].getShards().length, equalTo(2));
    boolean seenTest1 = false;
    boolean seenTest2 = false;
    for (ClusterSearchShardsGroup group : response.getGroups()) {
        if (group.getShardId().getIndexName().equals("test1")) {
            seenTest1 = true;
            assertThat(group.getShards().length, equalTo(2));
        } else if (group.getShardId().getIndexName().equals("test2")) {
            seenTest2 = true;
            assertThat(group.getShards().length, equalTo(2));
        } else {
            fail();
        }
    }
    assertThat(seenTest1, equalTo(true));
    assertThat(seenTest2, equalTo(true));
    assertThat(response.getNodes().length, equalTo(2));
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) ClusterSearchShardsGroup(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup)

Example 10 with ClusterSearchShardsResponse

use of org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse in project elasticsearch by elastic.

the class ClusterSearchShardsIT method testClusterSearchShardsWithBlocks.

public void testClusterSearchShardsWithBlocks() {
    createIndex("test-blocks");
    NumShards numShards = getNumShards("test-blocks");
    int docs = between(10, 100);
    for (int i = 0; i < docs; i++) {
        client().prepareIndex("test-blocks", "type", "" + i).setSource("test", "init").execute().actionGet();
    }
    ensureGreen("test-blocks");
    // Request is not blocked
    for (String blockSetting : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE, SETTING_READ_ONLY)) {
        try {
            enableIndexBlock("test-blocks", blockSetting);
            ClusterSearchShardsResponse response = client().admin().cluster().prepareSearchShards("test-blocks").execute().actionGet();
            assertThat(response.getGroups().length, equalTo(numShards.numPrimaries));
        } finally {
            disableIndexBlock("test-blocks", blockSetting);
        }
    }
    // Request is blocked
    try {
        enableIndexBlock("test-blocks", SETTING_BLOCKS_METADATA);
        assertBlocked(client().admin().cluster().prepareSearchShards("test-blocks"));
    } finally {
        disableIndexBlock("test-blocks", SETTING_BLOCKS_METADATA);
    }
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse)

Aggregations

ClusterSearchShardsResponse (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse)11 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 ClusterSearchShardsGroup (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup)4 UnknownHostException (java.net.UnknownHostException)3 List (java.util.List)3 Map (java.util.Map)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 TransportException (org.elasticsearch.transport.TransportException)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 TimeoutException (java.util.concurrent.TimeoutException)2 Collectors (java.util.stream.Collectors)2 ClusterSearchShardsRequest (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest)2 ShardIterator (org.elasticsearch.cluster.routing.ShardIterator)2 CountDown (org.elasticsearch.common.util.concurrent.CountDown)2