Search in sources :

Example 1 with ClusterSearchShardsGroup

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

the class RemoteClusterService method processRemoteShards.

Function<String, Transport.Connection> processRemoteShards(Map<String, ClusterSearchShardsResponse> searchShardsResponses, List<ShardIterator> remoteShardIterators, Map<String, AliasFilter> aliasFilterMap) {
    Map<String, Supplier<Transport.Connection>> nodeToCluster = new HashMap<>();
    for (Map.Entry<String, ClusterSearchShardsResponse> entry : searchShardsResponses.entrySet()) {
        String clusterName = entry.getKey();
        ClusterSearchShardsResponse searchShardsResponse = entry.getValue();
        for (DiscoveryNode remoteNode : searchShardsResponse.getNodes()) {
            nodeToCluster.put(remoteNode.getId(), () -> getConnection(remoteNode, clusterName));
        }
        Map<String, AliasFilter> indicesAndFilters = searchShardsResponse.getIndicesAndFilters();
        for (ClusterSearchShardsGroup clusterSearchShardsGroup : searchShardsResponse.getGroups()) {
            //add the cluster name to the remote index names for indices disambiguation
            //this ends up in the hits returned with the search response
            ShardId shardId = clusterSearchShardsGroup.getShardId();
            Index remoteIndex = shardId.getIndex();
            Index index = new Index(clusterName + REMOTE_CLUSTER_INDEX_SEPARATOR + remoteIndex.getName(), remoteIndex.getUUID());
            ShardIterator shardIterator = new PlainShardIterator(new ShardId(index, shardId.getId()), Arrays.asList(clusterSearchShardsGroup.getShards()));
            remoteShardIterators.add(shardIterator);
            AliasFilter aliasFilter;
            if (indicesAndFilters == null) {
                aliasFilter = new AliasFilter(null, Strings.EMPTY_ARRAY);
            } else {
                aliasFilter = indicesAndFilters.get(shardId.getIndexName());
                assert aliasFilter != null;
            }
            // here we have to map the filters to the UUID since from now on we use the uuid for the lookup
            aliasFilterMap.put(remoteIndex.getUUID(), aliasFilter);
        }
    }
    return (nodeId) -> {
        Supplier<Transport.Connection> supplier = nodeToCluster.get(nodeId);
        if (supplier == null) {
            throw new IllegalArgumentException("unknown remote node: " + nodeId);
        }
        return supplier.get();
    };
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) ShardId(org.elasticsearch.index.shard.ShardId) Arrays(java.util.Arrays) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) TimeoutException(java.util.concurrent.TimeoutException) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Strings(org.elasticsearch.common.Strings) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) TimeValue(org.elasticsearch.common.unit.TimeValue) Map(java.util.Map) CountDown(org.elasticsearch.common.util.concurrent.CountDown) TransportService(org.elasticsearch.transport.TransportService) ClusterSearchShardsGroup(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup) Transport(org.elasticsearch.transport.Transport) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) Setting(org.elasticsearch.common.settings.Setting) Predicate(java.util.function.Predicate) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) TimeUnit(java.util.concurrent.TimeUnit) AliasFilter(org.elasticsearch.search.internal.AliasFilter) List(java.util.List) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Supplier(org.apache.logging.log4j.util.Supplier) Closeable(java.io.Closeable) TransportException(org.elasticsearch.transport.TransportException) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) AliasFilter(org.elasticsearch.search.internal.AliasFilter) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) Index(org.elasticsearch.index.Index) ClusterSearchShardsGroup(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup) ShardId(org.elasticsearch.index.shard.ShardId) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) Supplier(org.apache.logging.log4j.util.Supplier) Transport(org.elasticsearch.transport.Transport) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with ClusterSearchShardsGroup

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

use of org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup 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 4 with ClusterSearchShardsGroup

use of org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup 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)

Aggregations

ClusterSearchShardsGroup (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup)4 ClusterSearchShardsResponse (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse)4 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ShardIterator (org.elasticsearch.cluster.routing.ShardIterator)2 ShardId (org.elasticsearch.index.shard.ShardId)2 AliasFilter (org.elasticsearch.search.internal.AliasFilter)2 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 UnknownHostException (java.net.UnknownHostException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 TimeUnit (java.util.concurrent.TimeUnit)1