Skip to content

Commit cd681c0

Browse files
authored
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.
1 parent 206904e commit cd681c0

File tree

14 files changed

+37
-36
lines changed

14 files changed

+37
-36
lines changed

solr/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ led to the suppression of exceptions. (Andrey Bozhko)
217217

218218
* SOLR-11318: Introduce unit testing for AssertTool. (Eric Pugh, Jason Gerlowski)
219219

220+
* SOLR-17534: Introduce ClusterState.getCollectionNames, a convenience method (David Smiley)
221+
220222
================== 9.7.1 ==================
221223
Bug Fixes
222224
---------------------

solr/core/src/java/org/apache/solr/cli/DeleteTool.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.PrintStream;
2222
import java.lang.invoke.MethodHandles;
23+
import java.util.Collection;
2324
import java.util.List;
2425
import java.util.Locale;
2526
import java.util.Optional;
@@ -155,7 +156,7 @@ protected void deleteCollection(CloudSolrClient cloudSolrClient, CommandLine cli
155156
configName);
156157
} else {
157158
// need to scan all Collections to see if any are using the config
158-
Set<String> collections = zkStateReader.getClusterState().getCollectionsMap().keySet();
159+
Collection<String> collections = zkStateReader.getClusterState().getCollectionNames();
159160

160161
// give a little note to the user if there are many collections in case it takes a while
161162
if (collections.size() > 50)

solr/core/src/java/org/apache/solr/handler/admin/ColStatus.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.lang.invoke.MethodHandles;
2222
import java.util.Collection;
2323
import java.util.Collections;
24-
import java.util.HashSet;
2524
import java.util.Map;
2625
import java.util.Set;
2726
import java.util.TreeMap;
@@ -76,7 +75,7 @@ public void getColStatus(NamedList<Object> results) {
7675
Collection<String> collections;
7776
String col = props.getStr(ZkStateReader.COLLECTION_PROP);
7877
if (col == null) {
79-
collections = new HashSet<>(clusterState.getCollectionStates().keySet());
78+
collections = clusterState.getCollectionNames();
8079
} else {
8180
collections = Collections.singleton(col);
8281
}

solr/core/src/test/org/apache/solr/cloud/ClusterStateMockUtilTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void testBuildClusterState_Simple() {
3636
ClusterStateMockUtil.buildClusterState("csr", "baseUrl1:8983_")) {
3737
ClusterState clusterState = zkStateReader.getClusterState();
3838
assertNotNull(clusterState);
39-
assertEquals(1, clusterState.getCollectionStates().size());
39+
assertEquals(1, clusterState.size());
4040
DocCollection collection1 = clusterState.getCollectionOrNull("collection1");
4141
assertNotNull(collection1);
4242
assertEquals(DocRouter.DEFAULT, collection1.getRouter());
@@ -62,7 +62,7 @@ public void testBuildClusterState_ReplicaTypes() {
6262
ClusterStateMockUtil.buildClusterState("csntp", "baseUrl1:8983_")) {
6363
ClusterState clusterState = zkStateReader.getClusterState();
6464
assertNotNull(clusterState);
65-
assertEquals(1, clusterState.getCollectionStates().size());
65+
assertEquals(1, clusterState.size());
6666
DocCollection collection1 = clusterState.getCollectionOrNull("collection1");
6767
assertNotNull(collection1);
6868
assertEquals(DocRouter.DEFAULT, collection1.getRouter());
@@ -83,7 +83,7 @@ public void testBuildClusterState_ReplicaStateAndType() {
8383
ClusterStateMockUtil.buildClusterState("csrStRpDnF", "baseUrl1:8983_")) {
8484
ClusterState clusterState = zkStateReader.getClusterState();
8585
assertNotNull(clusterState);
86-
assertEquals(1, clusterState.getCollectionStates().size());
86+
assertEquals(1, clusterState.size());
8787
DocCollection collection1 = clusterState.getCollectionOrNull("collection1");
8888
assertNotNull(collection1);
8989
assertEquals(DocRouter.DEFAULT, collection1.getRouter());

solr/core/src/test/org/apache/solr/cloud/ClusterStateTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void testStoreAndRead() {
8181

8282
assertEquals(
8383
"Provided liveNodes not used properly", 2, loadedClusterState.getLiveNodes().size());
84-
assertEquals("No collections found", 2, loadedClusterState.getCollectionsMap().size());
84+
assertEquals("No collections found", 2, loadedClusterState.size());
8585
assertEquals(
8686
"Properties not copied properly",
8787
replica.getStr("prop1"),
@@ -109,13 +109,13 @@ public void testStoreAndRead() {
109109

110110
assertEquals(
111111
"Provided liveNodes not used properly", 2, loadedClusterState.getLiveNodes().size());
112-
assertEquals("Should not have collections", 0, loadedClusterState.getCollectionsMap().size());
112+
assertEquals("Should not have collections", 0, loadedClusterState.size());
113113

114114
loadedClusterState =
115115
ClusterState.createFromJson(-1, (byte[]) null, liveNodes, Instant.now(), null);
116116

117117
assertEquals(
118118
"Provided liveNodes not used properly", 2, loadedClusterState.getLiveNodes().size());
119-
assertEquals("Should not have collections", 0, loadedClusterState.getCollectionsMap().size());
119+
assertEquals("Should not have collections", 0, loadedClusterState.size());
120120
}
121121
}

solr/core/src/test/org/apache/solr/cloud/OverseerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ private void waitForCollections(ZkStateReader stateReader, String... collections
758758
while (0 < maxIterations--) {
759759

760760
final ClusterState state = stateReader.getClusterState();
761-
Set<String> availableCollections = state.getCollectionsMap().keySet();
761+
Set<String> availableCollections = (Set<String>) state.getCollectionNames();
762762
int availableCount = 0;
763763
for (String requiredCollection : collections) {
764764
stateReader.waitForState(

solr/core/src/test/org/apache/solr/cloud/ReindexCollectionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private void doTestSameTargetReindexing(boolean sourceRemove, boolean followAlia
192192
String prefix = ReindexCollectionCmd.TARGET_COL_PREFIX + targetCollection;
193193
while (!timeOut.hasTimedOut()) {
194194
timeOut.sleep(500);
195-
for (String name : cloudManager.getClusterState().getCollectionsMap().keySet()) {
195+
for (String name : cloudManager.getClusterState().getCollectionNames()) {
196196
if (name.startsWith(prefix)) {
197197
realTargetCollection = name;
198198
break;

solr/modules/sql/src/java/org/apache/solr/handler/sql/SolrSchema.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.Closeable;
2020
import java.io.IOException;
21+
import java.util.Collection;
2122
import java.util.Date;
2223
import java.util.EnumSet;
2324
import java.util.HashMap;
@@ -87,23 +88,12 @@ protected Map<String, Table> getTableMap() {
8788
String zk = this.properties.getProperty("zk");
8889
CloudSolrClient cloudSolrClient = solrClientCache.getCloudSolrClient(zk);
8990
ClusterState clusterState = cloudSolrClient.getClusterState();
90-
91-
final Map<String, Table> builder = new HashMap<>();
92-
93-
Set<String> collections = clusterState.getCollectionsMap().keySet();
94-
for (String collection : collections) {
95-
builder.put(collection, new SolrTable(this, collection));
96-
}
97-
9891
Aliases aliases = ZkStateReader.from(cloudSolrClient).getAliases();
99-
for (String alias : aliases.getCollectionAliasListMap().keySet()) {
100-
// don't create duplicate entries
101-
if (!collections.contains(alias)) {
102-
builder.put(alias, new SolrTable(this, alias));
103-
}
104-
}
10592

106-
return Map.copyOf(builder);
93+
Collection<String> collectionNames = clusterState.getCollectionNames();
94+
Set<String> aliasNames = aliases.getCollectionAliasListMap().keySet();
95+
return Stream.concat(collectionNames.stream(), aliasNames.stream())
96+
.collect(Collectors.toMap(n -> n, n -> new SolrTable(this, n), (t1, t2) -> t1));
10797
}
10898

10999
private Map<String, LukeResponse.FieldInfo> getFieldInfo(final String collection) {

solr/prometheus-exporter/src/java/org/apache/solr/prometheus/scraper/SolrCloudScraper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.github.benmanes.caffeine.cache.Cache;
2020
import com.github.benmanes.caffeine.cache.Caffeine;
2121
import java.io.IOException;
22+
import java.util.Collection;
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.Set;
@@ -137,8 +138,8 @@ private Set<String> getBaseUrls() throws IOException {
137138
.collect(Collectors.toSet());
138139
}
139140

140-
private Set<String> getCollections() throws IOException {
141-
return solrClient.getClusterState().getCollectionStates().keySet();
141+
private Collection<String> getCollections() throws IOException {
142+
return solrClient.getClusterState().getCollectionNames();
142143
}
143144

144145
@Override

solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import java.util.Arrays;
3030
import java.util.List;
3131
import java.util.Properties;
32-
import java.util.Set;
33-
import java.util.SortedSet;
3432
import java.util.TreeSet;
3533
import org.apache.lucene.tests.util.LuceneTestCase;
3634
import org.apache.solr.client.solrj.impl.CloudSolrClient;
@@ -652,8 +650,7 @@ private void testJDBCMethods(
652650
solrClient.connect();
653651
ZkStateReader zkStateReader = ZkStateReader.from(solrClient);
654652

655-
Set<String> collectionsSet = zkStateReader.getClusterState().getCollectionsMap().keySet();
656-
SortedSet<String> tables = new TreeSet<>(collectionsSet);
653+
var tables = new TreeSet<String>(zkStateReader.getClusterState().getCollectionNames());
657654

658655
Aliases aliases = zkStateReader.getAliases();
659656
tables.addAll(aliases.getCollectionAliasListMap().keySet());

0 commit comments

Comments
 (0)