From 276adbd9903907ef6aaba713c6752605e6782f44 Mon Sep 17 00:00:00 2001 From: javanna Date: Thu, 24 Nov 2016 15:35:52 +0100 Subject: [PATCH] Enable cross cluster search only when at least one remote cluster is configured If no remote clusters are registered, we shouldn't even try resolving remote clusters as part of indices names. Just go ahead and treat the index as a local one, which may or may not exist. In fact, the character that we use a separator may be part of an alias name, or part of a date math expression. We will go ahead with the remote search only if the prefix before the first occurrence of the separator is an actual registered cluster. Otherwise we will treat the index as a local one. Previously we would go remotely anytime we'd find the separator in an index name, which caused false positives with aliases and date math expressions. It is also safer to not go remotely unless there is some remote cluster registered. We'd also throw exception whenever an unknown remote cluster was referred to, but this could again conflict with date math expressions or aliases: say we have some remote cluster configured, and we are using the separator in a date math expression, we only want to go remotely when the prefix matches a configured cluster, stay local otherwise, as the index name may still be valid locally. --- .../action/search/SearchTransportService.java | 8 ++++ .../action/search/TransportSearchAction.java | 41 +++++++++++-------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/search/SearchTransportService.java b/core/src/main/java/org/elasticsearch/action/search/SearchTransportService.java index 08d7d907661..e26f248d081 100644 --- a/core/src/main/java/org/elasticsearch/action/search/SearchTransportService.java +++ b/core/src/main/java/org/elasticsearch/action/search/SearchTransportService.java @@ -175,6 +175,14 @@ public class SearchTransportService extends AbstractComponent { remoteClustersSeeds = buildRemoteClustersSeeds(settings); } + boolean isCrossClusterSearchEnabled() { + return remoteClustersSeeds.isEmpty() == false; + } + + boolean isRemoteClusterRegistered(String clusterName) { + return remoteClustersSeeds.containsKey(clusterName); + } + private DiscoveryNode connectToRemoteCluster(String clusterName) { List nodes = remoteClustersSeeds.get(clusterName); if (nodes == null) { diff --git a/core/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java b/core/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java index fb767dc4dbf..912c0d30ead 100644 --- a/core/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java +++ b/core/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java @@ -109,29 +109,34 @@ public class TransportSearchAction extends HandledTransportAction localIndicesList = new ArrayList<>(); + final String[] localIndices; final Map> remoteIndicesByCluster = new HashMap<>(); - for (String index : searchRequest.indices()) { - int i = index.indexOf(REMOTE_CLUSTER_INDEX_SEPARATOR); - if (i >= 0) { - String remoteCluster = index.substring(0, i); - String remoteIndex = index.substring(i + 1); - List indices = remoteIndicesByCluster.get(remoteCluster); - if (indices == null) { - indices = new ArrayList<>(); - remoteIndicesByCluster.put(remoteCluster, indices); + if (searchTransportService.isCrossClusterSearchEnabled()) { + List localIndicesList = new ArrayList<>(); + for (String index : searchRequest.indices()) { + int i = index.indexOf(REMOTE_CLUSTER_INDEX_SEPARATOR); + if (i >= 0) { + String remoteCluster = index.substring(0, i); + if (searchTransportService.isRemoteClusterRegistered(remoteCluster)) { + String remoteIndex = index.substring(i + 1); + List indices = remoteIndicesByCluster.get(remoteCluster); + if (indices == null) { + indices = new ArrayList<>(); + remoteIndicesByCluster.put(remoteCluster, indices); + } + indices.add(remoteIndex); + } else { + localIndicesList.add(index); + } + } else { + localIndicesList.add(index); } - indices.add(remoteIndex); - } else { - localIndicesList.add(index); } + localIndices = localIndicesList.toArray(new String[localIndicesList.size()]); + } else { + localIndices = searchRequest.indices(); } - String[] localIndices = localIndicesList.toArray(new String[localIndicesList.size()]); - if (remoteIndicesByCluster.isEmpty()) { executeSearch((SearchTask)task, startTimeInMillis, searchRequest, localIndices, Strings.EMPTY_ARRAY, Collections.emptyList(), Collections.emptySet(), listener);