From 0ad365cbd069230bc638684b30bc4dc338e3a66d Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Fri, 22 Jul 2016 13:56:58 +0530 Subject: [PATCH] SOLR-9323: Expose ClusterSate.getCollectionStates which returns unverified list of collection names --- .../org/apache/solr/handler/SQLHandler.java | 4 +-- .../solrj/io/stream/CloudSolrStream.java | 25 +++++++++---------- .../client/solrj/io/stream/TopicStream.java | 15 ++--------- .../solr/common/cloud/ClusterState.java | 13 ++++++---- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/handler/SQLHandler.java b/solr/core/src/java/org/apache/solr/handler/SQLHandler.java index 403185838c9..f83c43f639d 100644 --- a/solr/core/src/java/org/apache/solr/handler/SQLHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/SQLHandler.java @@ -1514,9 +1514,9 @@ public class SQLHandler extends RequestHandlerBase implements SolrCoreAware , Pe CloudSolrClient cloudSolrClient = this.context.getSolrClientCache().getCloudSolrClient(this.zkHost); cloudSolrClient.connect(); ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader(); - Map collections = zkStateReader.getClusterState().getCollectionsMap(); + Set collections = zkStateReader.getClusterState().getCollectionStates().keySet(); if (collections.size() != 0) { - this.tables.addAll(collections.keySet()); + this.tables.addAll(collections); } Collections.sort(this.tables); } diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/CloudSolrStream.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/CloudSolrStream.java index 8aba89c2e66..2fb56ee37b2 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/CloudSolrStream.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/CloudSolrStream.java @@ -352,6 +352,16 @@ public class CloudSolrStream extends TupleStream implements Expressible { } } + public static Collection getSlicesIgnoreCase(String name, ClusterState clusterState) { + for (String coll : clusterState.getCollectionStates().keySet()) { + if (coll.equalsIgnoreCase(name)) { + DocCollection collection = clusterState.getCollectionOrNull(coll); + if (collection != null) return collection.getActiveSlices(); + } + } + return null; + } + protected void constructStreams() throws IOException { try { @@ -362,20 +372,9 @@ public class CloudSolrStream extends TupleStream implements Expressible { //System.out.println("Connected to zk an got cluster state."); Collection slices = clusterState.getActiveSlices(this.collection); - + if (slices == null) slices = getSlicesIgnoreCase(this.collection, clusterState); if (slices == null) { - //Try case insensitive match - Map collectionsMap = clusterState.getCollectionsMap(); - for (Map.Entry entry : collectionsMap.entrySet()) { - if (entry.getKey().equalsIgnoreCase(collection)) { - slices = entry.getValue().getActiveSlices(); - break; - } - } - - if (slices == null) { - throw new Exception("Collection not found:" + this.collection); - } + throw new Exception("Collection not found:" + this.collection); } ModifiableSolrParams mParams = new ModifiableSolrParams(params); diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/TopicStream.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/TopicStream.java index c4343c687e1..c8bf61048df 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/TopicStream.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/stream/TopicStream.java @@ -519,20 +519,9 @@ public class TopicStream extends CloudSolrStream implements Expressible { //System.out.println("Connected to zk an got cluster state."); Collection slices = clusterState.getActiveSlices(this.collection); - + if (slices == null) slices = getSlicesIgnoreCase(this.collection, clusterState); if (slices == null) { - //Try case insensitive match - Map collectionsMap = clusterState.getCollectionsMap(); - for (Map.Entry entry : collectionsMap.entrySet()) { - if (entry.getKey().equalsIgnoreCase(collection)) { - slices = entry.getValue().getActiveSlices(); - break; - } - } - - if (slices == null) { - throw new Exception("Collection not found:" + this.collection); - } + throw new Exception("Collection not found:" + this.collection); } diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterState.java b/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterState.java index 55df27189bd..3ab5a1f8c1a 100644 --- a/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterState.java +++ b/solr/solrj/src/java/org/apache/solr/common/cloud/ClusterState.java @@ -39,8 +39,8 @@ import org.noggit.JSONWriter; public class ClusterState implements JSONWriter.Writable { private final Integer znodeVersion; - - private final Map collectionStates; + + private final Map collectionStates, immutableCollectionStates; private Set liveNodes; /** @@ -67,6 +67,7 @@ public class ClusterState implements JSONWriter.Writable { this.liveNodes = new HashSet<>(liveNodes.size()); this.liveNodes.addAll(liveNodes); this.collectionStates = new LinkedHashMap<>(collectionStates); + this.immutableCollectionStates = Collections.unmodifiableMap(collectionStates); } @@ -432,10 +433,12 @@ public class ClusterState implements JSONWriter.Writable { this.liveNodes = liveNodes; } - /**For internal use only + /** Be aware that this may return collections which may not exist now. + * You can confirm that this collection exists after verifying + * CollectionRef.get() != null */ - Map getCollectionStates() { - return collectionStates; + public Map getCollectionStates() { + return immutableCollectionStates; } public static class CollectionRef {