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


The following commit(s) were added to refs/heads/branch_9x by this push:
     new 64c33161433 SOLR-17534: Add ClusterState.getCollectionNames (#2826)
64c33161433 is described below

commit 64c33161433736977899d0004b2d23720d957808
Author: David Smiley <[email protected]>
AuthorDate: Mon Nov 4 13:03:05 2024 -0500

    SOLR-17534: Add ClusterState.getCollectionNames (#2826)
    
    Refactoring to introduce getCollectionNames(). The motivation is to reduce 
callers of getCollectionsMap and getCollectionStates, which will go away soon.
    
    (cherry picked from commit cd681c073c3c0201aa49a3f243bb284c9880f26c)
---
 solr/CHANGES.txt                                     |  2 ++
 .../src/java/org/apache/solr/cli/DeleteTool.java     |  3 ++-
 .../org/apache/solr/handler/admin/ColStatus.java     |  3 +--
 .../apache/solr/cloud/ClusterStateMockUtilTest.java  |  6 +++---
 .../test/org/apache/solr/cloud/ClusterStateTest.java |  6 +++---
 .../src/test/org/apache/solr/cloud/OverseerTest.java |  2 +-
 .../org/apache/solr/cloud/ReindexCollectionTest.java |  2 +-
 .../java/org/apache/solr/handler/sql/SolrSchema.java | 20 +++++---------------
 .../solr/prometheus/scraper/SolrCloudScraper.java    |  5 +++--
 .../apache/solr/client/solrj/io/sql/JdbcTest.java    |  5 +----
 .../org/apache/solr/common/cloud/ZkStateReader.java  |  2 +-
 .../org/apache/solr/common/cloud/ClusterState.java   | 11 +++++++++++
 .../solr/cloud/AbstractFullDistribZkTestBase.java    |  2 +-
 .../org/apache/solr/cloud/MiniSolrCloudCluster.java  |  4 ++--
 14 files changed, 37 insertions(+), 36 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 860c5a5e599..633e70b5741 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -112,6 +112,8 @@ led to the suppression of exceptions. (Andrey Bozhko)
 
 * SOLR-17494: Document deprecation status of language specific writer types 
(wt=python,ruby,php,phps).  (Eric Pugh)
 
+* SOLR-17534: Introduce ClusterState.getCollectionNames, a convenience method 
(David Smiley)
+
 ==================  9.7.1 ==================
 Bug Fixes
 ---------------------
diff --git a/solr/core/src/java/org/apache/solr/cli/DeleteTool.java 
b/solr/core/src/java/org/apache/solr/cli/DeleteTool.java
index 4e1f95440d4..535b87ce1ff 100644
--- a/solr/core/src/java/org/apache/solr/cli/DeleteTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/DeleteTool.java
@@ -21,6 +21,7 @@ import static 
org.apache.solr.common.params.CommonParams.SYSTEM_INFO_PATH;
 
 import java.io.PrintStream;
 import java.lang.invoke.MethodHandles;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -235,7 +236,7 @@ public class DeleteTool extends ToolBase {
             configName);
       } else {
         // need to scan all Collections to see if any are using the config
-        Set<String> collections = 
zkStateReader.getClusterState().getCollectionsMap().keySet();
+        Collection<String> collections = 
zkStateReader.getClusterState().getCollectionNames();
 
         // give a little note to the user if there are many collections in 
case it takes a while
         if (collections.size() > 50)
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/ColStatus.java 
b/solr/core/src/java/org/apache/solr/handler/admin/ColStatus.java
index 5bc0884a144..e7fe44e0fe6 100644
--- a/solr/core/src/java/org/apache/solr/handler/admin/ColStatus.java
+++ b/solr/core/src/java/org/apache/solr/handler/admin/ColStatus.java
@@ -21,7 +21,6 @@ import java.io.IOException;
 import java.lang.invoke.MethodHandles;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
@@ -76,7 +75,7 @@ public class ColStatus {
     Collection<String> collections;
     String col = props.getStr(ZkStateReader.COLLECTION_PROP);
     if (col == null) {
-      collections = new HashSet<>(clusterState.getCollectionStates().keySet());
+      collections = clusterState.getCollectionNames();
     } else {
       collections = Collections.singleton(col);
     }
diff --git 
a/solr/core/src/test/org/apache/solr/cloud/ClusterStateMockUtilTest.java 
b/solr/core/src/test/org/apache/solr/cloud/ClusterStateMockUtilTest.java
index 601e92e89b9..f48dd3c1878 100644
--- a/solr/core/src/test/org/apache/solr/cloud/ClusterStateMockUtilTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/ClusterStateMockUtilTest.java
@@ -36,7 +36,7 @@ public class ClusterStateMockUtilTest extends SolrTestCaseJ4 {
         ClusterStateMockUtil.buildClusterState("csr", "baseUrl1:8983_")) {
       ClusterState clusterState = zkStateReader.getClusterState();
       assertNotNull(clusterState);
-      assertEquals(1, clusterState.getCollectionStates().size());
+      assertEquals(1, clusterState.size());
       DocCollection collection1 = 
clusterState.getCollectionOrNull("collection1");
       assertNotNull(collection1);
       assertEquals(DocRouter.DEFAULT, collection1.getRouter());
@@ -62,7 +62,7 @@ public class ClusterStateMockUtilTest extends SolrTestCaseJ4 {
         ClusterStateMockUtil.buildClusterState("csntp", "baseUrl1:8983_")) {
       ClusterState clusterState = zkStateReader.getClusterState();
       assertNotNull(clusterState);
-      assertEquals(1, clusterState.getCollectionStates().size());
+      assertEquals(1, clusterState.size());
       DocCollection collection1 = 
clusterState.getCollectionOrNull("collection1");
       assertNotNull(collection1);
       assertEquals(DocRouter.DEFAULT, collection1.getRouter());
@@ -83,7 +83,7 @@ public class ClusterStateMockUtilTest extends SolrTestCaseJ4 {
         ClusterStateMockUtil.buildClusterState("csrStRpDnF", 
"baseUrl1:8983_")) {
       ClusterState clusterState = zkStateReader.getClusterState();
       assertNotNull(clusterState);
-      assertEquals(1, clusterState.getCollectionStates().size());
+      assertEquals(1, clusterState.size());
       DocCollection collection1 = 
clusterState.getCollectionOrNull("collection1");
       assertNotNull(collection1);
       assertEquals(DocRouter.DEFAULT, collection1.getRouter());
diff --git a/solr/core/src/test/org/apache/solr/cloud/ClusterStateTest.java 
b/solr/core/src/test/org/apache/solr/cloud/ClusterStateTest.java
index 068a8f38a4d..e88193b4a3c 100644
--- a/solr/core/src/test/org/apache/solr/cloud/ClusterStateTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/ClusterStateTest.java
@@ -81,7 +81,7 @@ public class ClusterStateTest extends SolrTestCaseJ4 {
 
     assertEquals(
         "Provided liveNodes not used properly", 2, 
loadedClusterState.getLiveNodes().size());
-    assertEquals("No collections found", 2, 
loadedClusterState.getCollectionsMap().size());
+    assertEquals("No collections found", 2, loadedClusterState.size());
     assertEquals(
         "Properties not copied properly",
         replica.getStr("prop1"),
@@ -109,13 +109,13 @@ public class ClusterStateTest extends SolrTestCaseJ4 {
 
     assertEquals(
         "Provided liveNodes not used properly", 2, 
loadedClusterState.getLiveNodes().size());
-    assertEquals("Should not have collections", 0, 
loadedClusterState.getCollectionsMap().size());
+    assertEquals("Should not have collections", 0, loadedClusterState.size());
 
     loadedClusterState =
         ClusterState.createFromJson(-1, (byte[]) null, liveNodes, 
Instant.now(), null);
 
     assertEquals(
         "Provided liveNodes not used properly", 2, 
loadedClusterState.getLiveNodes().size());
-    assertEquals("Should not have collections", 0, 
loadedClusterState.getCollectionsMap().size());
+    assertEquals("Should not have collections", 0, loadedClusterState.size());
   }
 }
diff --git a/solr/core/src/test/org/apache/solr/cloud/OverseerTest.java 
b/solr/core/src/test/org/apache/solr/cloud/OverseerTest.java
index ace200a87fb..b11801ed3ad 100644
--- a/solr/core/src/test/org/apache/solr/cloud/OverseerTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/OverseerTest.java
@@ -759,7 +759,7 @@ public class OverseerTest extends SolrTestCaseJ4 {
     while (0 < maxIterations--) {
 
       final ClusterState state = stateReader.getClusterState();
-      Set<String> availableCollections = state.getCollectionsMap().keySet();
+      Set<String> availableCollections = (Set<String>) 
state.getCollectionNames();
       int availableCount = 0;
       for (String requiredCollection : collections) {
         stateReader.waitForState(
diff --git 
a/solr/core/src/test/org/apache/solr/cloud/ReindexCollectionTest.java 
b/solr/core/src/test/org/apache/solr/cloud/ReindexCollectionTest.java
index 29e85f66905..200e7b68974 100644
--- a/solr/core/src/test/org/apache/solr/cloud/ReindexCollectionTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/ReindexCollectionTest.java
@@ -192,7 +192,7 @@ public class ReindexCollectionTest extends 
SolrCloudTestCase {
     String prefix = ReindexCollectionCmd.TARGET_COL_PREFIX + targetCollection;
     while (!timeOut.hasTimedOut()) {
       timeOut.sleep(500);
-      for (String name : 
cloudManager.getClusterState().getCollectionsMap().keySet()) {
+      for (String name : cloudManager.getClusterState().getCollectionNames()) {
         if (name.startsWith(prefix)) {
           realTargetCollection = name;
           break;
diff --git 
a/solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrSchema.java 
b/solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrSchema.java
index 6d63e67c3dc..376c5df343e 100644
--- a/solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrSchema.java
+++ b/solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrSchema.java
@@ -18,6 +18,7 @@ package org.apache.solr.handler.sql;
 
 import java.io.Closeable;
 import java.io.IOException;
+import java.util.Collection;
 import java.util.Date;
 import java.util.EnumSet;
 import java.util.HashMap;
@@ -87,23 +88,12 @@ class SolrSchema extends AbstractSchema implements 
Closeable {
     String zk = this.properties.getProperty("zk");
     CloudSolrClient cloudSolrClient = solrClientCache.getCloudSolrClient(zk);
     ClusterState clusterState = cloudSolrClient.getClusterState();
-
-    final Map<String, Table> builder = new HashMap<>();
-
-    Set<String> collections = clusterState.getCollectionsMap().keySet();
-    for (String collection : collections) {
-      builder.put(collection, new SolrTable(this, collection));
-    }
-
     Aliases aliases = ZkStateReader.from(cloudSolrClient).getAliases();
-    for (String alias : aliases.getCollectionAliasListMap().keySet()) {
-      // don't create duplicate entries
-      if (!collections.contains(alias)) {
-        builder.put(alias, new SolrTable(this, alias));
-      }
-    }
 
-    return Map.copyOf(builder);
+    Collection<String> collectionNames = clusterState.getCollectionNames();
+    Set<String> aliasNames = aliases.getCollectionAliasListMap().keySet();
+    return Stream.concat(collectionNames.stream(), aliasNames.stream())
+        .collect(Collectors.toMap(n -> n, n -> new SolrTable(this, n), (t1, 
t2) -> t1));
   }
 
   private Map<String, LukeResponse.FieldInfo> getFieldInfo(final String 
collection) {
diff --git 
a/solr/prometheus-exporter/src/java/org/apache/solr/prometheus/scraper/SolrCloudScraper.java
 
b/solr/prometheus-exporter/src/java/org/apache/solr/prometheus/scraper/SolrCloudScraper.java
index 55ded335c1d..e333076dbe2 100644
--- 
a/solr/prometheus-exporter/src/java/org/apache/solr/prometheus/scraper/SolrCloudScraper.java
+++ 
b/solr/prometheus-exporter/src/java/org/apache/solr/prometheus/scraper/SolrCloudScraper.java
@@ -19,6 +19,7 @@ package org.apache.solr.prometheus.scraper;
 import com.github.benmanes.caffeine.cache.Cache;
 import com.github.benmanes.caffeine.cache.Caffeine;
 import java.io.IOException;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -137,8 +138,8 @@ public class SolrCloudScraper extends SolrScraper {
         .collect(Collectors.toSet());
   }
 
-  private Set<String> getCollections() throws IOException {
-    return solrClient.getClusterState().getCollectionStates().keySet();
+  private Collection<String> getCollections() throws IOException {
+    return solrClient.getClusterState().getCollectionNames();
   }
 
   @Override
diff --git 
a/solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java
 
b/solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java
index 822f7d38350..12b9f807847 100644
--- 
a/solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java
+++ 
b/solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java
@@ -29,8 +29,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Properties;
-import java.util.Set;
-import java.util.SortedSet;
 import java.util.TreeSet;
 import org.apache.lucene.tests.util.LuceneTestCase;
 import org.apache.solr.client.solrj.impl.CloudSolrClient;
@@ -652,8 +650,7 @@ public class JdbcTest extends SolrCloudTestCase {
       solrClient.connect();
       ZkStateReader zkStateReader = ZkStateReader.from(solrClient);
 
-      Set<String> collectionsSet = 
zkStateReader.getClusterState().getCollectionsMap().keySet();
-      SortedSet<String> tables = new TreeSet<>(collectionsSet);
+      var tables = new 
TreeSet<String>(zkStateReader.getClusterState().getCollectionNames());
 
       Aliases aliases = zkStateReader.getAliases();
       tables.addAll(aliases.getCollectionAliasListMap().keySet());
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 c49ab9fc6c0..fee6da8a257 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
@@ -618,7 +618,7 @@ public class ZkStateReader implements SolrCloseable {
           collectionWatches.watchedCollections().size(),
           collectionWatches.activeCollectionCount(),
           lazyCollectionStates.keySet().size(),
-          clusterState.getCollectionStates().size());
+          clusterState.size());
     }
 
     if (log.isTraceEnabled()) {
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 b1d8782cb10..b1a42644a9d 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
@@ -154,6 +154,16 @@ public class ClusterState implements MapWriter {
     return ref == null ? null : ref.get(allowCached);
   }
 
+  /**
+   * The collection names. Like a Set but might not implement it. Immutable; 
non-null. Some names
+   * returned might not resolve via {@link #getCollectionOrNull(String)}, so 
consider this a close
+   * approximation.
+   */
+  public Collection<String> getCollectionNames() {
+    // should we document we are sorted too?  Albeit that ties our hands.
+    return immutableCollectionStates.keySet();
+  }
+
   /**
    * Get a map of collection name vs DocCollection objects
    *
@@ -471,6 +481,7 @@ public class ClusterState implements MapWriter {
     }
   }
 
+  /** The approximate number of collections. */
   public int size() {
     return collectionStates.size();
   }
diff --git 
a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java
 
b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java
index dfa70afcc84..cd451253893 100644
--- 
a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java
+++ 
b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractFullDistribZkTestBase.java
@@ -1816,7 +1816,7 @@ public abstract class AbstractFullDistribZkTestBase 
extends AbstractDistribZkTes
           "Could not find collection "
               + DEFAULT_COLLECTION
               + " in "
-              + clusterState.getCollectionsMap().keySet());
+              + clusterState.getCollectionNames());
     }
 
     for (CloudJettyRunner cjetty : cloudJettys) {
diff --git 
a/solr/test-framework/src/java/org/apache/solr/cloud/MiniSolrCloudCluster.java 
b/solr/test-framework/src/java/org/apache/solr/cloud/MiniSolrCloudCluster.java
index 5f96d0a3c89..824457c59e5 100644
--- 
a/solr/test-framework/src/java/org/apache/solr/cloud/MiniSolrCloudCluster.java
+++ 
b/solr/test-framework/src/java/org/apache/solr/cloud/MiniSolrCloudCluster.java
@@ -654,7 +654,7 @@ public class MiniSolrCloudCluster {
 
       reader.createClusterStateWatchersAndUpdate(); // up to date aliases & 
collections
       reader.aliasesManager.applyModificationAndExportToZk(aliases -> 
Aliases.EMPTY);
-      for (String collection : 
reader.getClusterState().getCollectionStates().keySet()) {
+      for (String collection : reader.getClusterState().getCollectionNames()) {
         
CollectionAdminRequest.deleteCollection(collection).process(solrClient);
       }
 
@@ -664,7 +664,7 @@ public class MiniSolrCloudCluster {
             "Still waiting to see all collections removed from clusterstate.");
       }
 
-      for (String collection : 
reader.getClusterState().getCollectionStates().keySet()) {
+      for (String collection : reader.getClusterState().getCollectionNames()) {
         reader.waitForState(collection, 15, TimeUnit.SECONDS, Objects::isNull);
       }
     }

Reply via email to