|
@@ -99,6 +99,7 @@ import java.util.Collections;
|
|
|
import java.util.Comparator;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Optional;
|
|
@@ -245,111 +246,116 @@ public class TransportSearchActionTests extends ESTestCase {
|
|
|
}
|
|
|
|
|
|
public void testProcessRemoteShards() {
|
|
|
- try (
|
|
|
- TransportService transportService = MockTransportService.createNewService(
|
|
|
- Settings.EMPTY,
|
|
|
- Version.CURRENT,
|
|
|
- TransportVersion.CURRENT,
|
|
|
- threadPool,
|
|
|
- null
|
|
|
- )
|
|
|
- ) {
|
|
|
- RemoteClusterService service = transportService.getRemoteClusterService();
|
|
|
- assertFalse(service.isCrossClusterSearchEnabled());
|
|
|
- Map<String, ClusterSearchShardsResponse> searchShardsResponseMap = new HashMap<>();
|
|
|
- DiscoveryNode[] nodes = new DiscoveryNode[] { TestDiscoveryNode.create("node1"), TestDiscoveryNode.create("node2") };
|
|
|
- Map<String, AliasFilter> indicesAndAliases = new HashMap<>();
|
|
|
- indicesAndAliases.put("foo", AliasFilter.of(new TermsQueryBuilder("foo", "bar"), "some_alias_for_foo", "some_other_foo_alias"));
|
|
|
- indicesAndAliases.put("bar", AliasFilter.of(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, SearchShardsResponse> searchShardsResponseMap = new LinkedHashMap<>();
|
|
|
+ // first cluster - new response
|
|
|
+ {
|
|
|
+ List<DiscoveryNode> nodes = List.of(TestDiscoveryNode.create("node1"), TestDiscoveryNode.create("node2"));
|
|
|
+ Map<String, AliasFilter> aliasFilters1 = Map.of(
|
|
|
+ "foo_id",
|
|
|
+ AliasFilter.of(new TermsQueryBuilder("foo", "bar"), "some_alias_for_foo", "some_other_foo_alias"),
|
|
|
+ "bar_id",
|
|
|
+ AliasFilter.of(new MatchAllQueryBuilder(), Strings.EMPTY_ARRAY)
|
|
|
+ );
|
|
|
+ List<SearchShardsGroup> groups = List.of(
|
|
|
+ new SearchShardsGroup(new ShardId("foo", "foo_id", 0), List.of("node1", "node2"), false),
|
|
|
+ new SearchShardsGroup(new ShardId("foo", "foo_id", 1), List.of("node2", "node1"), true),
|
|
|
+ new SearchShardsGroup(new ShardId("bar", "bar_id", 0), List.of("node2", "node1"), false)
|
|
|
+ );
|
|
|
+ searchShardsResponseMap.put("test_cluster_1", new SearchShardsResponse(groups, nodes, aliasFilters1));
|
|
|
+ }
|
|
|
+ // second cluster - legacy response
|
|
|
+ {
|
|
|
DiscoveryNode[] nodes2 = new DiscoveryNode[] { TestDiscoveryNode.create("node3") };
|
|
|
ClusterSearchShardsGroup[] groups2 = new ClusterSearchShardsGroup[] {
|
|
|
new ClusterSearchShardsGroup(
|
|
|
new ShardId("xyz", "xyz_id", 0),
|
|
|
new ShardRouting[] { TestShardRouting.newShardRouting("xyz", 0, "node3", true, ShardRoutingState.STARTED) }
|
|
|
) };
|
|
|
- Map<String, AliasFilter> filter = new HashMap<>();
|
|
|
- filter.put("xyz", AliasFilter.of(null, "some_alias_for_xyz"));
|
|
|
- searchShardsResponseMap.put("test_cluster_2", new ClusterSearchShardsResponse(groups2, nodes2, filter));
|
|
|
-
|
|
|
- Map<String, OriginalIndices> remoteIndicesByCluster = new HashMap<>();
|
|
|
- remoteIndicesByCluster.put(
|
|
|
- "test_cluster_1",
|
|
|
- new OriginalIndices(new String[] { "fo*", "ba*" }, SearchRequest.DEFAULT_INDICES_OPTIONS)
|
|
|
- );
|
|
|
- remoteIndicesByCluster.put("test_cluster_2", new OriginalIndices(new String[] { "x*" }, SearchRequest.DEFAULT_INDICES_OPTIONS));
|
|
|
- Map<String, AliasFilter> remoteAliases = TransportSearchAction.getRemoteAliasFilters(searchShardsResponseMap);
|
|
|
- List<SearchShardIterator> iteratorList = TransportSearchAction.getRemoteShardsIterator(
|
|
|
- searchShardsResponseMap,
|
|
|
- remoteIndicesByCluster,
|
|
|
- remoteAliases
|
|
|
+ Map<String, AliasFilter> aliasFilters2 = Map.of("xyz", AliasFilter.of(null, "some_alias_for_xyz"));
|
|
|
+ searchShardsResponseMap.put(
|
|
|
+ "test_cluster_2",
|
|
|
+ SearchShardsResponse.fromLegacyResponse(new ClusterSearchShardsResponse(groups2, nodes2, aliasFilters2))
|
|
|
);
|
|
|
- assertEquals(4, iteratorList.size());
|
|
|
- for (SearchShardIterator iterator : iteratorList) {
|
|
|
- if (iterator.shardId().getIndexName().endsWith("foo")) {
|
|
|
- assertArrayEquals(
|
|
|
- new String[] { "some_alias_for_foo", "some_other_foo_alias" },
|
|
|
- iterator.getOriginalIndices().indices()
|
|
|
- );
|
|
|
- assertTrue(iterator.shardId().getId() == 0 || iterator.shardId().getId() == 1);
|
|
|
- assertEquals("test_cluster_1", iterator.getClusterAlias());
|
|
|
- assertEquals("foo", iterator.shardId().getIndexName());
|
|
|
- SearchShardTarget shard = iterator.nextOrNull();
|
|
|
- assertNotNull(shard);
|
|
|
- assertEquals(shard.getShardId().getIndexName(), "foo");
|
|
|
- shard = iterator.nextOrNull();
|
|
|
- assertNotNull(shard);
|
|
|
- assertEquals(shard.getShardId().getIndexName(), "foo");
|
|
|
- assertNull(iterator.nextOrNull());
|
|
|
- } else if (iterator.shardId().getIndexName().endsWith("bar")) {
|
|
|
- assertArrayEquals(new String[] { "bar" }, iterator.getOriginalIndices().indices());
|
|
|
- assertEquals(0, iterator.shardId().getId());
|
|
|
- assertEquals("test_cluster_1", iterator.getClusterAlias());
|
|
|
- assertEquals("bar", iterator.shardId().getIndexName());
|
|
|
- SearchShardTarget shard = iterator.nextOrNull();
|
|
|
- assertNotNull(shard);
|
|
|
- assertEquals(shard.getShardId().getIndexName(), "bar");
|
|
|
- shard = iterator.nextOrNull();
|
|
|
- assertNotNull(shard);
|
|
|
- assertEquals(shard.getShardId().getIndexName(), "bar");
|
|
|
- assertNull(iterator.nextOrNull());
|
|
|
- } else if (iterator.shardId().getIndexName().endsWith("xyz")) {
|
|
|
- assertArrayEquals(new String[] { "some_alias_for_xyz" }, iterator.getOriginalIndices().indices());
|
|
|
- assertEquals(0, iterator.shardId().getId());
|
|
|
- assertEquals("xyz", iterator.shardId().getIndexName());
|
|
|
- assertEquals("test_cluster_2", iterator.getClusterAlias());
|
|
|
- SearchShardTarget shard = iterator.nextOrNull();
|
|
|
- assertNotNull(shard);
|
|
|
- assertEquals(shard.getShardId().getIndexName(), "xyz");
|
|
|
- assertNull(iterator.nextOrNull());
|
|
|
- }
|
|
|
- }
|
|
|
- assertEquals(3, remoteAliases.size());
|
|
|
- assertTrue(remoteAliases.toString(), remoteAliases.containsKey("foo_id"));
|
|
|
- assertTrue(remoteAliases.toString(), remoteAliases.containsKey("bar_id"));
|
|
|
- assertTrue(remoteAliases.toString(), remoteAliases.containsKey("xyz_id"));
|
|
|
- assertEquals(new TermsQueryBuilder("foo", "bar"), remoteAliases.get("foo_id").getQueryBuilder());
|
|
|
- assertEquals(new MatchAllQueryBuilder(), remoteAliases.get("bar_id").getQueryBuilder());
|
|
|
- assertNull(remoteAliases.get("xyz_id").getQueryBuilder());
|
|
|
+ }
|
|
|
+ Map<String, OriginalIndices> remoteIndicesByCluster = Map.of(
|
|
|
+ "test_cluster_1",
|
|
|
+ new OriginalIndices(new String[] { "fo*", "ba*" }, SearchRequest.DEFAULT_INDICES_OPTIONS),
|
|
|
+ "test_cluster_2",
|
|
|
+ new OriginalIndices(new String[] { "x*" }, SearchRequest.DEFAULT_INDICES_OPTIONS)
|
|
|
+ );
|
|
|
+ Map<String, AliasFilter> aliasFilters = new HashMap<>();
|
|
|
+ searchShardsResponseMap.values().forEach(r -> aliasFilters.putAll(r.getAliasFilters()));
|
|
|
+ List<SearchShardIterator> iteratorList = TransportSearchAction.getRemoteShardsIterator(
|
|
|
+ searchShardsResponseMap,
|
|
|
+ remoteIndicesByCluster,
|
|
|
+ aliasFilters
|
|
|
+ );
|
|
|
+ assertThat(iteratorList, hasSize(4));
|
|
|
+ {
|
|
|
+ SearchShardIterator shardIt = iteratorList.get(0);
|
|
|
+ assertTrue(shardIt.prefiltered());
|
|
|
+ assertFalse(shardIt.skip());
|
|
|
+ assertThat(shardIt.shardId(), equalTo(new ShardId("foo", "foo_id", 0)));
|
|
|
+ assertArrayEquals(new String[] { "some_alias_for_foo", "some_other_foo_alias" }, shardIt.getOriginalIndices().indices());
|
|
|
+ assertEquals("test_cluster_1", shardIt.getClusterAlias());
|
|
|
+ assertEquals("foo", shardIt.shardId().getIndexName());
|
|
|
+ SearchShardTarget shard = shardIt.nextOrNull();
|
|
|
+ assertNotNull(shard);
|
|
|
+ assertEquals(shard.getShardId().getIndexName(), "foo");
|
|
|
+ assertThat(shard.getNodeId(), equalTo("node1"));
|
|
|
+ shard = shardIt.nextOrNull();
|
|
|
+ assertNotNull(shard);
|
|
|
+ assertEquals(shard.getShardId().getIndexName(), "foo");
|
|
|
+ assertThat(shard.getNodeId(), equalTo("node2"));
|
|
|
+ assertNull(shardIt.nextOrNull());
|
|
|
+ }
|
|
|
+ {
|
|
|
+ SearchShardIterator shardIt = iteratorList.get(1);
|
|
|
+ assertTrue(shardIt.prefiltered());
|
|
|
+ assertTrue(shardIt.skip());
|
|
|
+ assertThat(shardIt.shardId(), equalTo(new ShardId("foo", "foo_id", 1)));
|
|
|
+ assertArrayEquals(new String[] { "some_alias_for_foo", "some_other_foo_alias" }, shardIt.getOriginalIndices().indices());
|
|
|
+ assertEquals("test_cluster_1", shardIt.getClusterAlias());
|
|
|
+ assertEquals("foo", shardIt.shardId().getIndexName());
|
|
|
+ SearchShardTarget shard = shardIt.nextOrNull();
|
|
|
+ assertNotNull(shard);
|
|
|
+ assertEquals(shard.getShardId().getIndexName(), "foo");
|
|
|
+ assertThat(shard.getNodeId(), equalTo("node2"));
|
|
|
+ shard = shardIt.nextOrNull();
|
|
|
+ assertNotNull(shard);
|
|
|
+ assertEquals(shard.getShardId().getIndexName(), "foo");
|
|
|
+ assertThat(shard.getNodeId(), equalTo("node1"));
|
|
|
+ assertNull(shardIt.nextOrNull());
|
|
|
+ }
|
|
|
+ {
|
|
|
+ SearchShardIterator shardIt = iteratorList.get(2);
|
|
|
+ assertTrue(shardIt.prefiltered());
|
|
|
+ assertFalse(shardIt.skip());
|
|
|
+ assertThat(shardIt.shardId(), equalTo(new ShardId("bar", "bar_id", 0)));
|
|
|
+ assertArrayEquals(new String[] { "bar" }, shardIt.getOriginalIndices().indices());
|
|
|
+ assertEquals("test_cluster_1", shardIt.getClusterAlias());
|
|
|
+ SearchShardTarget shard = shardIt.nextOrNull();
|
|
|
+ assertNotNull(shard);
|
|
|
+ assertEquals(shard.getShardId().getIndexName(), "bar");
|
|
|
+ assertThat(shard.getNodeId(), equalTo("node2"));
|
|
|
+ shard = shardIt.nextOrNull();
|
|
|
+ assertNotNull(shard);
|
|
|
+ assertEquals(shard.getShardId().getIndexName(), "bar");
|
|
|
+ assertThat(shard.getNodeId(), equalTo("node1"));
|
|
|
+ assertNull(shardIt.nextOrNull());
|
|
|
+ }
|
|
|
+ {
|
|
|
+ SearchShardIterator shardIt = iteratorList.get(3);
|
|
|
+ assertFalse(shardIt.prefiltered());
|
|
|
+ assertFalse(shardIt.skip());
|
|
|
+ assertArrayEquals(new String[] { "some_alias_for_xyz" }, shardIt.getOriginalIndices().indices());
|
|
|
+ assertThat(shardIt.shardId(), equalTo(new ShardId("xyz", "xyz_id", 0)));
|
|
|
+ assertEquals("test_cluster_2", shardIt.getClusterAlias());
|
|
|
+ SearchShardTarget shard = shardIt.nextOrNull();
|
|
|
+ assertNotNull(shard);
|
|
|
+ assertEquals(shard.getShardId().getIndexName(), "xyz");
|
|
|
+ assertThat(shard.getNodeId(), equalTo("node3"));
|
|
|
+ assertNull(shardIt.nextOrNull());
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -827,31 +833,34 @@ public class TransportSearchActionTests extends ESTestCase {
|
|
|
) {
|
|
|
service.start();
|
|
|
service.acceptIncomingRequests();
|
|
|
+
|
|
|
RemoteClusterService remoteClusterService = service.getRemoteClusterService();
|
|
|
{
|
|
|
final CountDownLatch latch = new CountDownLatch(1);
|
|
|
- AtomicReference<Map<String, ClusterSearchShardsResponse>> response = new AtomicReference<>();
|
|
|
+ AtomicReference<Map<String, SearchShardsResponse>> response = new AtomicReference<>();
|
|
|
AtomicInteger skippedClusters = new AtomicInteger();
|
|
|
TransportSearchAction.collectSearchShards(
|
|
|
IndicesOptions.lenientExpandOpen(),
|
|
|
null,
|
|
|
null,
|
|
|
+ new MatchAllQueryBuilder(),
|
|
|
+ randomBoolean(),
|
|
|
+ null,
|
|
|
skippedClusters,
|
|
|
remoteIndicesByCluster,
|
|
|
- remoteClusterService,
|
|
|
- threadPool,
|
|
|
+ service,
|
|
|
new LatchedActionListener<>(ActionListener.wrap(response::set, e -> fail("no failures expected")), latch)
|
|
|
);
|
|
|
awaitLatch(latch, 5, TimeUnit.SECONDS);
|
|
|
assertEquals(0, skippedClusters.get());
|
|
|
assertNotNull(response.get());
|
|
|
- Map<String, ClusterSearchShardsResponse> map = response.get();
|
|
|
+ Map<String, SearchShardsResponse> map = response.get();
|
|
|
assertEquals(numClusters, map.size());
|
|
|
for (int i = 0; i < numClusters; i++) {
|
|
|
String clusterAlias = "remote" + i;
|
|
|
assertTrue(map.containsKey(clusterAlias));
|
|
|
- ClusterSearchShardsResponse shardsResponse = map.get(clusterAlias);
|
|
|
- assertEquals(1, shardsResponse.getNodes().length);
|
|
|
+ SearchShardsResponse shardsResponse = map.get(clusterAlias);
|
|
|
+ assertThat(shardsResponse.getNodes(), hasSize(1));
|
|
|
}
|
|
|
}
|
|
|
{
|
|
@@ -862,10 +871,12 @@ public class TransportSearchActionTests extends ESTestCase {
|
|
|
IndicesOptions.lenientExpandOpen(),
|
|
|
"index_not_found",
|
|
|
null,
|
|
|
+ new MatchAllQueryBuilder(),
|
|
|
+ randomBoolean(),
|
|
|
+ null,
|
|
|
skippedClusters,
|
|
|
remoteIndicesByCluster,
|
|
|
- remoteClusterService,
|
|
|
- threadPool,
|
|
|
+ service,
|
|
|
new LatchedActionListener<>(ActionListener.wrap(r -> fail("no response expected"), failure::set), latch)
|
|
|
);
|
|
|
awaitLatch(latch, 5, TimeUnit.SECONDS);
|
|
@@ -907,10 +918,12 @@ public class TransportSearchActionTests extends ESTestCase {
|
|
|
IndicesOptions.lenientExpandOpen(),
|
|
|
null,
|
|
|
null,
|
|
|
+ new MatchAllQueryBuilder(),
|
|
|
+ randomBoolean(),
|
|
|
+ null,
|
|
|
skippedClusters,
|
|
|
remoteIndicesByCluster,
|
|
|
- remoteClusterService,
|
|
|
- threadPool,
|
|
|
+ service,
|
|
|
new LatchedActionListener<>(ActionListener.wrap(r -> fail("no response expected"), failure::set), latch)
|
|
|
);
|
|
|
awaitLatch(latch, 5, TimeUnit.SECONDS);
|
|
@@ -929,20 +942,22 @@ public class TransportSearchActionTests extends ESTestCase {
|
|
|
{
|
|
|
final CountDownLatch latch = new CountDownLatch(1);
|
|
|
AtomicInteger skippedClusters = new AtomicInteger(0);
|
|
|
- AtomicReference<Map<String, ClusterSearchShardsResponse>> response = new AtomicReference<>();
|
|
|
+ AtomicReference<Map<String, SearchShardsResponse>> response = new AtomicReference<>();
|
|
|
TransportSearchAction.collectSearchShards(
|
|
|
IndicesOptions.lenientExpandOpen(),
|
|
|
null,
|
|
|
null,
|
|
|
+ new MatchAllQueryBuilder(),
|
|
|
+ randomBoolean(),
|
|
|
+ null,
|
|
|
skippedClusters,
|
|
|
remoteIndicesByCluster,
|
|
|
- remoteClusterService,
|
|
|
- threadPool,
|
|
|
+ service,
|
|
|
new LatchedActionListener<>(ActionListener.wrap(response::set, e -> fail("no failures expected")), latch)
|
|
|
);
|
|
|
awaitLatch(latch, 5, TimeUnit.SECONDS);
|
|
|
assertNotNull(response.get());
|
|
|
- Map<String, ClusterSearchShardsResponse> map = response.get();
|
|
|
+ Map<String, SearchShardsResponse> map = response.get();
|
|
|
assertEquals(numClusters - disconnectedNodesIndices.size(), map.size());
|
|
|
assertEquals(skippedClusters.get(), disconnectedNodesIndices.size());
|
|
|
for (int i = 0; i < numClusters; i++) {
|
|
@@ -972,21 +987,23 @@ public class TransportSearchActionTests extends ESTestCase {
|
|
|
assertBusy(() -> {
|
|
|
final CountDownLatch latch = new CountDownLatch(1);
|
|
|
AtomicInteger skippedClusters = new AtomicInteger(0);
|
|
|
- AtomicReference<Map<String, ClusterSearchShardsResponse>> response = new AtomicReference<>();
|
|
|
+ AtomicReference<Map<String, SearchShardsResponse>> response = new AtomicReference<>();
|
|
|
TransportSearchAction.collectSearchShards(
|
|
|
IndicesOptions.lenientExpandOpen(),
|
|
|
null,
|
|
|
null,
|
|
|
+ new MatchAllQueryBuilder(),
|
|
|
+ randomBoolean(),
|
|
|
+ null,
|
|
|
skippedClusters,
|
|
|
remoteIndicesByCluster,
|
|
|
- remoteClusterService,
|
|
|
- threadPool,
|
|
|
+ service,
|
|
|
new LatchedActionListener<>(ActionListener.wrap(response::set, e -> fail("no failures expected")), latch)
|
|
|
);
|
|
|
awaitLatch(latch, 5, TimeUnit.SECONDS);
|
|
|
assertEquals(0, skippedClusters.get());
|
|
|
assertNotNull(response.get());
|
|
|
- Map<String, ClusterSearchShardsResponse> map = response.get();
|
|
|
+ Map<String, SearchShardsResponse> map = response.get();
|
|
|
assertEquals(numClusters, map.size());
|
|
|
for (int i = 0; i < numClusters; i++) {
|
|
|
String clusterAlias = "remote" + i;
|