Apache Solr open-source search software
SOLR-17534: Add ClusterState.getCollectionNames (#2826)
David Smileycd681c073c3c
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex a5d34104a1a..35070264402 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -217,6 +217,8 @@ led to the suppression of exceptions. (Andrey Bozhko)* SOLR-11318: Introduce unit testing for AssertTool. (Eric Pugh, Jason Gerlowski)+* 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.javaindex 069128663b4..bd6d5f97cc2 100644--- a/solr/core/src/java/org/apache/solr/cli/DeleteTool.java+++ b/solr/core/src/java/org/apache/solr/cli/DeleteTool.java@@ -20,6 +20,7 @@ import static org.apache.solr.common.params.CommonParams.NAME;import java.io.PrintStream;import java.lang.invoke.MethodHandles;+import java.util.Collection;import java.util.List;import java.util.Locale;import java.util.Optional;@@ -155,7 +156,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 whileif (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.javaindex 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.javaindex 3b4cc30ab4d..72707e70f84 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.javaindex 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.javaindex 1350106457d..ae10c722b20 100644--- a/solr/core/src/test/org/apache/solr/cloud/OverseerTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/OverseerTest.java@@ -758,7 +758,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.javaindex 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.javaindex 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.javaindex 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();}@Overridediff --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.javaindex 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.javaindex df638085a2e..be5c4249d0a 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@@ -625,7 +625,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.javaindex 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.javaindex 568d8b7e373..dcdab4e86d1 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@@ -1813,7 +1813,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.javaindex 54f23d71260..c600c7e4ffa 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@@ -585,7 +585,7 @@ public class MiniSolrCloudCluster {reader.createClusterStateWatchersAndUpdate(); // up to date aliases & collectionsreader.aliasesManager.applyModificationAndExportToZk(aliases -> Aliases.EMPTY);- for (String collection : reader.getClusterState().getCollectionStates().keySet()) {+ for (String collection : reader.getClusterState().getCollectionNames()) {CollectionAdminRequest.deleteCollection(collection).process(solrClient);}@@ -595,7 +595,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);}}