From c95f5900acb0645a78ae94c1d8113e6ad4446fe5 Mon Sep 17 00:00:00 2001 From: Mark Robert Miller Date: Sun, 24 Nov 2013 20:19:43 +0000 Subject: [PATCH] SOLR-4553: Bug in finding remote node when proxying update request in a cluster with more than one collection. Also, attempt to proxy requests more aggressively. git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1545065 13f79535-47bb-0310-9956-ffa450edef68 --- .../solr/servlet/SolrDispatchFilter.java | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java index 4c454ec5fa7..756ddb49855 100644 --- a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java +++ b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java @@ -574,13 +574,14 @@ public class SolrDispatchFilter implements Filter ClusterState clusterState = cores.getZkController().getClusterState(); Collection slices = clusterState.getActiveSlices(collectionName); boolean byCoreName = false; + if (slices == null) { + slices = new ArrayList(); // look by core name byCoreName = true; - Set collections = clusterState.getCollections(); - for (String collection : collections) { - slices = new ArrayList(); - slices.addAll(clusterState.getSlices(collection)); + slices = getSlicesForCollections(clusterState, slices, true); + if (slices == null || slices.size() == 0) { + slices = getSlicesForCollections(clusterState, slices, false); } } @@ -588,6 +589,21 @@ public class SolrDispatchFilter implements Filter return null; } + String coreUrl = getCoreUrl(cores, collectionName, origCorename, clusterState, + slices, byCoreName, true); + + if (coreUrl == null) { + coreUrl = getCoreUrl(cores, collectionName, origCorename, clusterState, + slices, byCoreName, false); + } + + return coreUrl; + } + + private String getCoreUrl(CoreContainer cores, String collectionName, + String origCorename, ClusterState clusterState, Collection slices, + boolean byCoreName, boolean activeReplicas) { + String coreUrl; Set liveNodes = clusterState.getLiveNodes(); Iterator it = slices.iterator(); while (it.hasNext()) { @@ -595,8 +611,9 @@ public class SolrDispatchFilter implements Filter Map sliceShards = slice.getReplicasMap(); for (ZkNodeProps nodeProps : sliceShards.values()) { ZkCoreNodeProps coreNodeProps = new ZkCoreNodeProps(nodeProps); - if (liveNodes.contains(coreNodeProps.getNodeName()) - && coreNodeProps.getState().equals(ZkStateReader.ACTIVE)) { + if (!activeReplicas || (liveNodes.contains(coreNodeProps.getNodeName()) + && coreNodeProps.getState().equals(ZkStateReader.ACTIVE))) { + if (byCoreName && !collectionName.equals(coreNodeProps.getCoreName())) { // if it's by core name, make sure they match continue; @@ -605,7 +622,7 @@ public class SolrDispatchFilter implements Filter // don't count a local core continue; } - String coreUrl; + if (origCorename != null) { coreUrl = coreNodeProps.getBaseUrl() + "/" + origCorename; } else { @@ -621,6 +638,19 @@ public class SolrDispatchFilter implements Filter } return null; } + + private Collection getSlicesForCollections(ClusterState clusterState, + Collection slices, boolean activeSlices) { + Set collections = clusterState.getCollections(); + for (String collection : collections) { + if (activeSlices) { + slices.addAll(clusterState.getActiveSlices(collection)); + } else { + slices.addAll(clusterState.getSlices(collection)); + } + } + return slices; + } private SolrCore getCoreByCollection(CoreContainer cores, String corename, String path) { String collection = corename;