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;