Skip to content

Commit

Permalink
SOLR-17535: Deprecate ClusterState.forEachCollection (#2854)
Browse files Browse the repository at this point in the history
Use collectionStream() instead.  Redirect callers.  A simple refactoring.
  • Loading branch information
dsmiley authored Nov 12, 2024
1 parent 981f678 commit c694258
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 49 deletions.
3 changes: 2 additions & 1 deletion solr/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,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-17535: Introduce ClusterState.collectionStream to replace getCollectionStates, getCollectionsMap,
and forEachCollection, which are now deprecated. (David Smiley)

* SOLR-17545: Upgrade to Gradle 8.10 (Houston Putman)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.apache.solr.cluster.events.ClusterEvent;
import org.apache.solr.cluster.events.ClusterEventListener;
import org.apache.solr.cluster.events.NodesDownEvent;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.ReplicaCount;
import org.apache.solr.common.cloud.ReplicaPosition;
import org.apache.solr.common.util.SolrNamedThreadFactory;
Expand Down Expand Up @@ -168,38 +167,44 @@ private void runRepair() {
// collection / positions
Map<String, List<ReplicaPosition>> newPositions = new HashMap<>();
try {
ClusterState clusterState = solrCloudManager.getClusterState();
clusterState.forEachCollection(
coll -> {
// shard / number of replicas per type
Map<String, ReplicaCount> lostReplicas = new HashMap<>();
coll.forEachReplica(
(shard, replica) -> {
if (reallyLostNodes.contains(replica.getNodeName())) {
lostReplicas
.computeIfAbsent(shard, s -> ReplicaCount.empty())
.increment(replica.type);
}
});
Assign.AssignStrategy assignStrategy = Assign.createAssignStrategy(cc);
lostReplicas.forEach(
(shard, types) -> {
Assign.AssignRequest assignRequest =
new Assign.AssignRequestBuilder()
.forCollection(coll.getName())
.forShard(Collections.singletonList(shard))
.assignReplicas(types)
.build();
try {
List<ReplicaPosition> positions =
assignStrategy.assign(solrCloudManager, assignRequest);
newPositions.put(coll.getName(), positions);
} catch (Exception e) {
log.warn(
"Exception computing positions for {}/{}: {}", coll.getName(), shard, e);
}
});
});
// shard / number of replicas per type
solrCloudManager
.getClusterState()
.collectionStream()
.forEach(
coll -> {
// shard / number of replicas per type
Map<String, ReplicaCount> lostReplicas = new HashMap<>();
coll.forEachReplica(
(shard, replica) -> {
if (reallyLostNodes.contains(replica.getNodeName())) {
lostReplicas
.computeIfAbsent(shard, s -> ReplicaCount.empty())
.increment(replica.type);
}
});
Assign.AssignStrategy assignStrategy = Assign.createAssignStrategy(cc);
lostReplicas.forEach(
(shard, types) -> {
Assign.AssignRequest assignRequest =
new Assign.AssignRequestBuilder()
.forCollection(coll.getName())
.forShard(Collections.singletonList(shard))
.assignReplicas(types)
.build();
try {
List<ReplicaPosition> positions =
assignStrategy.assign(solrCloudManager, assignRequest);
newPositions.put(coll.getName(), positions);
} catch (Exception e) {
log.warn(
"Exception computing positions for {}/{}: {}",
coll.getName(),
shard,
e);
}
});
});
} catch (IOException e) {
log.warn("Exception getting cluster state", e);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ public void testFailure() throws Exception {
// verify that the target and checkpoint collections don't exist
cloudManager
.getClusterState()
.forEachCollection(
.collectionStream()
.forEach(
coll -> {
assertFalse(
coll.getName() + " still exists",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,21 @@ protected void readReplicaDetails() throws IOException {
if (clusterState == null) { // zkStateReader still initializing
return;
}
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());
}));
clusterState
.collectionStream()
.forEach(
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ public SolrCollection get(String key) {
public void forEachEntry(BiConsumer<String, ? super SolrCollection> fun) {
zkStateReader
.getClusterState()
.forEachCollection(
coll -> fun.accept(coll.getName(), _collection(coll.getName(), coll)));
.collectionStream()
.forEach(coll -> fun.accept(coll.getName(), _collection(coll.getName(), coll)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,10 @@ public Stream<DocCollection> collectionStream() {
/**
* Calls {@code consumer} with a resolved {@link DocCollection}s for all collections. Use this
* sparingly in case there are many collections.
*
* @deprecated see {@link #collectionStream()}
*/
@Deprecated
public void forEachCollection(Consumer<DocCollection> consumer) {
collectionStream().forEach(consumer);
}
Expand Down

0 comments on commit c694258

Please sign in to comment.