Apache Solr open-source search software
SOLR-10799: Refator HttpShardHandler.prepDistributed collection of shard replicas
Tomas Fernandez Lobbe3618fc529dff
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 195443dd7b1..6b664e4485e 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -225,6 +225,9 @@ Other Changessupport in favor of the <mergePolicyFactory> element introduced by SOLR-8621 in Solr 5.5.0.(Christine Poerschke, hossman)+* SOLR-10799: Extracted functionality to collect eligible replicas from HttpShardHandler.prepDistributed()+ to a new method (Domenico Fabio Marino via Tomás Fernández Löbbe)+================== 6.7.0 ==================Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.diff --git a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.javaindex bc620b61418..2954cff54da 100644--- a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java@@ -30,6 +30,7 @@ import java.util.concurrent.CompletionService;import java.util.concurrent.ExecutionException;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;+import java.util.function.Predicate;import org.apache.http.client.HttpClient;import org.apache.solr.client.solrj.SolrClient;@@ -398,18 +399,11 @@ public class HttpShardHandler extends ShardHandler {continue;// throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "no such shard: " + sliceName);}- Replica shardLeader = null;-- final Collection<Replica> allSliceReplicas = slice.getReplicasMap().values();- final List<Replica> eligibleSliceReplicas = new ArrayList<>(allSliceReplicas.size());- for (Replica replica : allSliceReplicas) {- if (!clusterState.liveNodesContain(replica.getNodeName())- || replica.getState() != Replica.State.ACTIVE- || (onlyNrtReplicas && replica.getType() == Replica.Type.PULL)) {- continue;- }-- if (onlyNrtReplicas && replica.getType() == Replica.Type.TLOG) {+ final Predicate<Replica> isShardLeader = new Predicate<Replica>() {+ private Replica shardLeader = null;++ @Override+ public boolean test(Replica replica) {if (shardLeader == null) {try {shardLeader = zkController.getZkStateReader().getLeaderRetry(cloudDescriptor.getCollectionName(), slice.getName());@@ -424,12 +418,11 @@ public class HttpShardHandler extends ShardHandler {throw e;}}- if (!replica.getName().equals(shardLeader.getName())) {- continue;- }+ return replica.getName().equals(shardLeader.getName());}- eligibleSliceReplicas.add(replica);- }+ };++ final List<Replica> eligibleSliceReplicas = collectEligibleReplicas(slice, clusterState, onlyNrtReplicas, isShardLeader);replicaListTransformer.transform(eligibleSliceReplicas);@@ -462,6 +455,26 @@ public class HttpShardHandler extends ShardHandler {}}+ private static List<Replica> collectEligibleReplicas(Slice slice, ClusterState clusterState, boolean onlyNrtReplicas, Predicate<Replica> isShardLeader) {+ final Collection<Replica> allSliceReplicas = slice.getReplicasMap().values();+ final List<Replica> eligibleSliceReplicas = new ArrayList<>(allSliceReplicas.size());+ for (Replica replica : allSliceReplicas) {+ if (!clusterState.liveNodesContain(replica.getNodeName())+ || replica.getState() != Replica.State.ACTIVE+ || (onlyNrtReplicas && replica.getType() == Replica.Type.PULL)) {+ continue;+ }++ if (onlyNrtReplicas && replica.getType() == Replica.Type.TLOG) {+ if (!isShardLeader.test(replica)) {+ continue;+ }+ }+ eligibleSliceReplicas.add(replica);+ }+ return eligibleSliceReplicas;+ }+private static String createSliceShardsStr(final List<String> shardUrls) {final StringBuilder sliceShardsStr = new StringBuilder();boolean first = true;