Apache Solr open-source search software
SOLR-17535 ClusterState.collectionStream() in lieu of getCollectionStates() (#2834)
David Smiley961e328f96a5
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 907827a6fd0..0076975aec7 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -225,6 +225,8 @@ led to the suppression of exceptions. (Andrey Bozhko)* SOLR-17534: Introduce ClusterState.getCollectionNames, a convenience method (David Smiley)+* SOLR-17535: Introduce ClusterState.collectionStream to replace getCollectionStates and getCollectionsMap (David Smiley)+* SOLR-17545: Upgrade to Gradle 8.10 (Houston Putman)================== 9.7.1 ==================diff --git a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.javaindex 90eb3eb0c1c..9b5e7a82bf8 100644--- a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java+++ b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java@@ -78,7 +78,6 @@ import org.apache.solr.cloud.ZkConfigSetService;import org.apache.solr.cloud.ZkSolrResourceLoader;import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrInputDocument;-import org.apache.solr.common.cloud.ClusterState;import org.apache.solr.common.cloud.DocCollection;import org.apache.solr.common.cloud.Replica;import org.apache.solr.common.cloud.SolrZkClient;@@ -168,24 +167,12 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {}List<String> listCollectionsForConfig(String configSet) {- final List<String> collections = new ArrayList<>();- Map<String, ClusterState.CollectionRef> states =- zkStateReader().getClusterState().getCollectionStates();- for (Map.Entry<String, ClusterState.CollectionRef> e : states.entrySet()) {- final String coll = e.getKey();- if (coll.startsWith(DESIGNER_PREFIX)) {- continue; // ignore temp- }-- try {- if (configSet.equals(e.getValue().get().getConfigName()) && e.getValue().get() != null) {- collections.add(coll);- }- } catch (Exception exc) {- log.warn("Failed to get config name for {}", coll, exc);- }- }- return collections;+ return zkStateReader()+ .getClusterState()+ .collectionStream()+ .filter(c -> configSet.equals(c.getConfigName()))+ .map(DocCollection::getName)+ .toList();}@SuppressWarnings("unchecked")diff --git a/solr/solrj-zookeeper/src/java/org/apache/solr/client/solrj/impl/SolrClientNodeStateProvider.java b/solr/solrj-zookeeper/src/java/org/apache/solr/client/solrj/impl/SolrClientNodeStateProvider.javaindex a9789c8a141..186788c8914 100644--- a/solr/solrj-zookeeper/src/java/org/apache/solr/client/solrj/impl/SolrClientNodeStateProvider.java+++ b/solr/solrj-zookeeper/src/java/org/apache/solr/client/solrj/impl/SolrClientNodeStateProvider.java@@ -39,7 +39,6 @@ import org.apache.solr.common.MapWriter;import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;import org.apache.solr.common.cloud.ClusterState;-import org.apache.solr.common.cloud.DocCollection;import org.apache.solr.common.cloud.Replica;import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.ModifiableSolrParams;@@ -79,23 +78,18 @@ public class SolrClientNodeStateProvider implements NodeStateProvider, MapWriterif (clusterState == null) { // zkStateReader still initializingreturn;}- Map<String, ClusterState.CollectionRef> all =- clusterStateProvider.getClusterState().getCollectionStates();- all.forEach(- (collName, ref) -> {- DocCollection coll = ref.get();- if (coll == null) return;- coll.forEachReplica(- (shard, replica) -> {- Map<String, Map<String, List<Replica>>> nodeData =- nodeVsCollectionVsShardVsReplicaInfo.computeIfAbsent(- replica.getNodeName(), k -> new HashMap<>());- Map<String, List<Replica>> collData =- nodeData.computeIfAbsent(collName, k -> new HashMap<>());- List<Replica> replicas = collData.computeIfAbsent(shard, k -> new ArrayList<>());- replicas.add((Replica) replica.clone());- });- });+ clusterState.forEachCollection(+ coll ->+ coll.forEachReplica(+ (shard, replica) -> {+ Map<String, Map<String, List<Replica>>> nodeData =+ nodeVsCollectionVsShardVsReplicaInfo.computeIfAbsent(+ replica.getNodeName(), k -> new HashMap<>());+ Map<String, List<Replica>> collData =+ nodeData.computeIfAbsent(coll.getName(), k -> new HashMap<>());+ List<Replica> replicas = collData.computeIfAbsent(shard, k -> new ArrayList<>());+ replicas.add((Replica) replica.clone());+ }));}@Overridediff --git a/solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/ZkStateReader.java b/solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/ZkStateReader.javaindex be5c4249d0a..ff559e5f84b 100644--- a/solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/ZkStateReader.java+++ b/solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/ZkStateReader.java@@ -634,7 +634,7 @@ public class ZkStateReader implements SolrCloseable {collectionWatches.watchedCollections(),collectionWatches.activeCollections(),lazyCollectionStates.keySet(),- clusterState.getCollectionStates());+ clusterState.collectionStream().toList());}notifyCloudCollectionsListeners();@@ -1432,8 +1432,7 @@ public class ZkStateReader implements SolrCloseable {zkClient,Instant.ofEpochMilli(stat.getCtime()));- ClusterState.CollectionRef collectionRef = state.getCollectionStates().get(coll);- return collectionRef == null ? null : collectionRef.get();+ return state.getCollectionOrNull(coll);} catch (KeeperException.NoNodeException e) {if (watcher != null) {// Leave an exists watch in place in case a state.json is created later.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.javaindex b1a42644a9d..ce607ac8637 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@@ -35,6 +35,7 @@ import java.util.concurrent.atomic.AtomicInteger;import java.util.function.Consumer;import java.util.function.Function;import java.util.stream.Collectors;+import java.util.stream.Stream;import org.apache.solr.common.MapWriter;import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;@@ -391,7 +392,10 @@ public class ClusterState implements MapWriter {/*** Be aware that this may return collections which may not exist now. You can confirm that this* collection exists after verifying CollectionRef.get() != null+ *+ * @deprecated see {@link #collectionStream()}*/+ @Deprecatedpublic Map<String, CollectionRef> getCollectionStates() {return immutableCollectionStates;}@@ -411,28 +415,19 @@ public class ClusterState implements MapWriter {}/**- * Iterate over collections. Unlike {@link #getCollectionStates()} collections passed to the- * consumer are guaranteed to exist.- *- * @param consumer collection consumer.+ * Streams the resolved {@link DocCollection}s. Use this sparingly in case there are many+ * collections.+ */+ public Stream<DocCollection> collectionStream() {+ return collectionStates.values().stream().map(CollectionRef::get).filter(Objects::nonNull);+ }++ /**+ * Calls {@code consumer} with a resolved {@link DocCollection}s for all collections. Use this+ * sparingly in case there are many collections.*/public void forEachCollection(Consumer<DocCollection> consumer) {- collectionStates.forEach(- (s, collectionRef) -> {- try {- DocCollection collection = collectionRef.get();- if (collection != null) {- consumer.accept(collection);- }- } catch (SolrException e) {- if (e.getCause() != null- && e.getCause().getClass().getName().endsWith("NoNodeException")) {- // don't do anything. This collection does not exist- } else {- throw e;- }- }- });+ collectionStream().forEach(consumer);}public static class CollectionRef {