Expand cross cluster search indices for search requests to the concrete index or to it's aliases (#24502)

This change will expand the shard level request to the actual concrete index or to the aliases
that expanded to the concrete index to ensure shard level requests won't see wildcard expressions as their original indices
This commit is contained in:
Simon Willnauer 2017-05-05 16:40:04 +02:00 committed by GitHub
parent 9f08a553d9
commit e3766d2828
3 changed files with 17 additions and 12 deletions

View File

@ -220,11 +220,6 @@ public class TransportSearchAction extends HandledTransportAction<SearchRequest,
Index remoteIndex = shardId.getIndex(); Index remoteIndex = shardId.getIndex();
Index index = new Index(RemoteClusterAware.buildRemoteIndexName(clusterAlias, remoteIndex.getName()), Index index = new Index(RemoteClusterAware.buildRemoteIndexName(clusterAlias, remoteIndex.getName()),
remoteIndex.getUUID()); remoteIndex.getUUID());
OriginalIndices originalIndices = remoteIndicesByCluster.get(clusterAlias);
assert originalIndices != null;
SearchShardIterator shardIterator = new SearchShardIterator(clusterAlias, new ShardId(index, shardId.getId()),
Arrays.asList(clusterSearchShardsGroup.getShards()), originalIndices);
remoteShardIterators.add(shardIterator);
final AliasFilter aliasFilter; final AliasFilter aliasFilter;
if (indicesAndFilters == null) { if (indicesAndFilters == null) {
aliasFilter = AliasFilter.EMPTY; aliasFilter = AliasFilter.EMPTY;
@ -232,8 +227,16 @@ public class TransportSearchAction extends HandledTransportAction<SearchRequest,
aliasFilter = indicesAndFilters.get(shardId.getIndexName()); aliasFilter = indicesAndFilters.get(shardId.getIndexName());
assert aliasFilter != null : "alias filter must not be null for index: " + shardId.getIndex(); assert aliasFilter != null : "alias filter must not be null for index: " + shardId.getIndex();
} }
String[] aliases = aliasFilter.getAliases();
String[] finalIndices = aliases.length == 0 ? new String[] {shardId.getIndexName()} : aliases;
// here we have to map the filters to the UUID since from now on we use the uuid for the lookup // 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); aliasFilterMap.put(remoteIndex.getUUID(), aliasFilter);
final OriginalIndices originalIndices = remoteIndicesByCluster.get(clusterAlias);
assert originalIndices != null : "original indices are null for clusterAlias: " + clusterAlias;
SearchShardIterator shardIterator = new SearchShardIterator(clusterAlias, new ShardId(index, shardId.getId()),
Arrays.asList(clusterSearchShardsGroup.getShards()), new OriginalIndices(finalIndices,
originalIndices.indicesOptions()));
remoteShardIterators.add(shardIterator);
} }
} }
return (clusterAlias, nodeId) -> { return (clusterAlias, nodeId) -> {

View File

@ -84,5 +84,4 @@ public class ArrayUtils {
System.arraycopy(other, 0, target, one.length, other.length); System.arraycopy(other, 0, target, one.length, other.length);
return target; return target;
} }
} }

View File

@ -64,7 +64,6 @@ public class TransportSearchActionTests extends ESTestCase {
ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS); ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS);
} }
public void testMergeShardsIterators() throws IOException { public void testMergeShardsIterators() throws IOException {
List<ShardIterator> localShardIterators = new ArrayList<>(); List<ShardIterator> localShardIterators = new ArrayList<>();
{ {
@ -159,7 +158,8 @@ public class TransportSearchActionTests extends ESTestCase {
new DiscoveryNode("node2", buildNewFakeTransportAddress(), Version.CURRENT) new DiscoveryNode("node2", buildNewFakeTransportAddress(), Version.CURRENT)
}; };
Map<String, AliasFilter> indicesAndAliases = new HashMap<>(); Map<String, AliasFilter> indicesAndAliases = new HashMap<>();
indicesAndAliases.put("foo", new AliasFilter(new TermsQueryBuilder("foo", "bar"), Strings.EMPTY_ARRAY)); indicesAndAliases.put("foo", new AliasFilter(new TermsQueryBuilder("foo", "bar"), "some_alias_for_foo",
"some_other_foo_alias"));
indicesAndAliases.put("bar", new AliasFilter(new MatchAllQueryBuilder(), Strings.EMPTY_ARRAY)); indicesAndAliases.put("bar", new AliasFilter(new MatchAllQueryBuilder(), Strings.EMPTY_ARRAY));
ClusterSearchShardsGroup[] groups = new ClusterSearchShardsGroup[] { ClusterSearchShardsGroup[] groups = new ClusterSearchShardsGroup[] {
new ClusterSearchShardsGroup(new ShardId("foo", "foo_id", 0), new ClusterSearchShardsGroup(new ShardId("foo", "foo_id", 0),
@ -180,7 +180,9 @@ public class TransportSearchActionTests extends ESTestCase {
new ClusterSearchShardsGroup(new ShardId("xyz", "xyz_id", 0), new ClusterSearchShardsGroup(new ShardId("xyz", "xyz_id", 0),
new ShardRouting[] {TestShardRouting.newShardRouting("xyz", 0, "node3", true, ShardRoutingState.STARTED)}) new ShardRouting[] {TestShardRouting.newShardRouting("xyz", 0, "node3", true, ShardRoutingState.STARTED)})
}; };
searchShardsResponseMap.put("test_cluster_2", new ClusterSearchShardsResponse(groups2, nodes2, null)); Map<String, AliasFilter> filter = new HashMap<>();
filter.put("xyz", new AliasFilter(null, "some_alias_for_xyz"));
searchShardsResponseMap.put("test_cluster_2", new ClusterSearchShardsResponse(groups2, nodes2, filter));
Map<String, OriginalIndices> remoteIndicesByCluster = new HashMap<>(); Map<String, OriginalIndices> remoteIndicesByCluster = new HashMap<>();
remoteIndicesByCluster.put("test_cluster_1", remoteIndicesByCluster.put("test_cluster_1",
@ -193,7 +195,8 @@ public class TransportSearchActionTests extends ESTestCase {
assertEquals(4, iteratorList.size()); assertEquals(4, iteratorList.size());
for (SearchShardIterator iterator : iteratorList) { for (SearchShardIterator iterator : iteratorList) {
if (iterator.shardId().getIndexName().endsWith("foo")) { if (iterator.shardId().getIndexName().endsWith("foo")) {
assertArrayEquals(new String[]{"fo*", "ba*"}, iterator.getOriginalIndices().indices()); assertArrayEquals(new String[]{"some_alias_for_foo", "some_other_foo_alias"},
iterator.getOriginalIndices().indices());
assertTrue(iterator.shardId().getId() == 0 || iterator.shardId().getId() == 1); assertTrue(iterator.shardId().getId() == 0 || iterator.shardId().getId() == 1);
assertEquals("test_cluster_1:foo", iterator.shardId().getIndexName()); assertEquals("test_cluster_1:foo", iterator.shardId().getIndexName());
ShardRouting shardRouting = iterator.nextOrNull(); ShardRouting shardRouting = iterator.nextOrNull();
@ -204,7 +207,7 @@ public class TransportSearchActionTests extends ESTestCase {
assertEquals(shardRouting.getIndexName(), "foo"); assertEquals(shardRouting.getIndexName(), "foo");
assertNull(iterator.nextOrNull()); assertNull(iterator.nextOrNull());
} else if (iterator.shardId().getIndexName().endsWith("bar")) { } else if (iterator.shardId().getIndexName().endsWith("bar")) {
assertArrayEquals(new String[]{"fo*", "ba*"}, iterator.getOriginalIndices().indices()); assertArrayEquals(new String[]{"bar"}, iterator.getOriginalIndices().indices());
assertEquals(0, iterator.shardId().getId()); assertEquals(0, iterator.shardId().getId());
assertEquals("test_cluster_1:bar", iterator.shardId().getIndexName()); assertEquals("test_cluster_1:bar", iterator.shardId().getIndexName());
ShardRouting shardRouting = iterator.nextOrNull(); ShardRouting shardRouting = iterator.nextOrNull();
@ -215,7 +218,7 @@ public class TransportSearchActionTests extends ESTestCase {
assertEquals(shardRouting.getIndexName(), "bar"); assertEquals(shardRouting.getIndexName(), "bar");
assertNull(iterator.nextOrNull()); assertNull(iterator.nextOrNull());
} else if (iterator.shardId().getIndexName().endsWith("xyz")) { } else if (iterator.shardId().getIndexName().endsWith("xyz")) {
assertArrayEquals(new String[]{"x*"}, iterator.getOriginalIndices().indices()); assertArrayEquals(new String[]{"some_alias_for_xyz"}, iterator.getOriginalIndices().indices());
assertEquals(0, iterator.shardId().getId()); assertEquals(0, iterator.shardId().getId());
assertEquals("test_cluster_2:xyz", iterator.shardId().getIndexName()); assertEquals("test_cluster_2:xyz", iterator.shardId().getIndexName());
ShardRouting shardRouting = iterator.nextOrNull(); ShardRouting shardRouting = iterator.nextOrNull();