This is an automated email from the ASF dual-hosted git repository.

dsmiley pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git

commit 4ff3596176e16c66f19715a622f52168dc7c7ecb
Author: David Smiley <[email protected]>
AuthorDate: Thu Nov 7 22:27:19 2024 -0500

    SOLR-17535 ClusterState.collectionStream() in lieu of getCollectionStates() 
(#2834)
    
    Deprecate ClusterState.getCollectionStates()
    Add ClusterState.collectionStream(), switching many callers to this method.
    
    (cherry picked from commit 961e328f96a5a0d2af26d59e0895649b4f12b5b6)
---
 solr/CHANGES.txt                                   |  2 ++
 .../designer/SchemaDesignerConfigSetHelper.java    | 25 ++++------------
 .../solrj/impl/SolrClientNodeStateProvider.java    | 30 ++++++++-----------
 .../apache/solr/common/cloud/ZkStateReader.java    |  5 ++--
 .../org/apache/solr/common/cloud/ClusterState.java | 35 ++++++++++------------
 5 files changed, 37 insertions(+), 60 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index c72ffca0cdb..9c005b2034e 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -118,6 +118,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.java
index 0125e7988d8..b1644b8e4dc 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
@@ -77,7 +77,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;
@@ -167,24 +166,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)
+        .collect(Collectors.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.java
index 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, MapWriter
     if (clusterState == null) { // zkStateReader still initializing
       return;
     }
-    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());
+                }));
   }
 
   @Override
diff --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.java
index fee6da8a257..8daed99e75f 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
@@ -627,7 +627,7 @@ public class ZkStateReader implements SolrCloseable {
           collectionWatches.watchedCollections(),
           collectionWatches.activeCollections(),
           lazyCollectionStates.keySet(),
-          clusterState.getCollectionStates());
+          clusterState.collectionStream().collect(Collectors.toList()));
     }
 
     notifyCloudCollectionsListeners();
@@ -1425,8 +1425,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.java
index 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()}
    */
+  @Deprecated
   public 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 {

Reply via email to