This is an automated email from the ASF dual-hosted git repository.
dsmiley pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/main by this push:
new 961e328f96a SOLR-17535 ClusterState.collectionStream() in lieu of
getCollectionStates() (#2834)
961e328f96a is described below
commit 961e328f96a5a0d2af26d59e0895649b4f12b5b6
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.
---
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 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.java
index 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.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 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.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 {