Apache Solr open-source search software
Refactor for code clarity, add some comments.
Gus Heckb8ff4c76361e
diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.java b/solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.javaindex cfc401d5ba8..b577340845e 100644--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.java+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.java@@ -200,7 +200,7 @@ public class Assign {}return defaultValue;}-+private static int defaultCounterValue(DocCollection collection, boolean newCollection) {if (newCollection) return 0;int defaultValue = collection.getReplicas().size();@@ -332,7 +332,7 @@ public class Assign {, shard, nrtReplicas, tlogReplicas, pullReplicas, createNodeSet);DocCollection coll = clusterState.getCollection(collectionName);int maxShardsPerNode = coll.getMaxShardsPerNode() == -1 ? Integer.MAX_VALUE : coll.getMaxShardsPerNode();- List<String> createNodeList = null;+ List<String> createNodeList;if (createNodeSet instanceof List) {createNodeList = (List<String>) createNodeSet;@@ -341,9 +341,13 @@ public class Assign {createNodeList = createNodeSet == null ? null : new ArrayList<>(new LinkedHashSet<>(StrUtils.splitSmart((String) createNodeSet, ",", true)));}- HashMap<String, ReplicaCount> nodeNameVsShardCount = getNodeNameVsShardCount(collectionName, clusterState, createNodeList);+ // produces clear message when down nodes are the root cause, without this the user just+ // gets a log message of detail about the nodes that are up, and a message that policies could not+ // be satisfied which then requires study to diagnose the issue.+ checkLiveNodes(createNodeList,clusterState);if (createNodeList == null) { // We only care if we haven't been told to put new replicas on specific nodes.+ HashMap<String, ReplicaCount> nodeNameVsShardCount = getNodeNameVsShardCount(collectionName, clusterState, null);long availableSlots = 0;for (Map.Entry<String, ReplicaCount> ent : nodeNameVsShardCount.entrySet()) {//ADDREPLICA can put more than maxShardsPerNode on an instance, so this test is necessary.@@ -411,24 +415,21 @@ public class Assign {static HashMap<String, ReplicaCount> getNodeNameVsShardCount(String collectionName,ClusterState clusterState, List<String> createNodeList) {- Set<String> nodes = clusterState.getLiveNodes();-- List<String> nodeList = new ArrayList<>(nodes.size());- nodeList.addAll(nodes);- if (createNodeList != null) nodeList.retainAll(createNodeList);-HashMap<String, ReplicaCount> nodeNameVsShardCount = new HashMap<>();- for (String s : nodeList) {+ List<String> liveNodes = createNodeList == null || createNodeList.isEmpty() ?+ new ArrayList<>(clusterState.getLiveNodes()) :+ checkLiveNodes(createNodeList, clusterState);++ for (String s : liveNodes) {nodeNameVsShardCount.put(s, new ReplicaCount(s));}++ // if we were given a list, just use that, don't worry about countsif (createNodeList != null) { // Overrides petty considerations about maxShardsPerNode- if (createNodeList.size() != nodeNameVsShardCount.size()) {- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,- "At least one of the node(s) specified " + createNodeList + " are not currently active in "- + nodeNameVsShardCount.keySet() + ", no action taken.");- }return nodeNameVsShardCount;}++ // if we get here we were not given a createNodeList, build a map with real counts.DocCollection coll = clusterState.getCollection(collectionName);int maxShardsPerNode = coll.getMaxShardsPerNode() == -1 ? Integer.MAX_VALUE : coll.getMaxShardsPerNode();Map<String, DocCollection> collections = clusterState.getCollectionsMap();@@ -453,6 +454,22 @@ public class Assign {return nodeNameVsShardCount;}+ // throw an exception if any node int the supplied list is not live.+ // Empty or null list always succeeds and returns the input.+ private static List<String> checkLiveNodes(List<String> createNodeList, ClusterState clusterState) {+ Set<String> liveNodes = clusterState.getLiveNodes();+ if (createNodeList != null) {+ if (!liveNodes.containsAll(createNodeList)) {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,+ "At least one of the node(s) specified " + createNodeList + " are not currently active in "+ + createNodeList + ", no action taken.");+ }+ // the logic that was extracted to this method used to create a defensive copy but no code+ // was modifying the copy, if this method is made protected or public we want to go back to that+ }+ return createNodeList; // unmodified, but return for inline use+ }+/*** Thrown if there is an exception while assigning nodes for replicas*/@@ -550,29 +567,41 @@ public class Assign {@Overridepublic List<ReplicaPosition> assign(SolrCloudManager solrCloudManager, AssignRequest assignRequest) throws Assign.AssignmentException, IOException, InterruptedException {ClusterState clusterState = solrCloudManager.getClusterStateProvider().getClusterState();- List<String> nodeList = assignRequest.nodes;+ List<String> nodeList = assignRequest.nodes; // can this be empty list?- HashMap<String, Assign.ReplicaCount> nodeNameVsShardCount = Assign.getNodeNameVsShardCount(assignRequest.collectionName, clusterState, assignRequest.nodes);if (nodeList == null || nodeList.isEmpty()) {+ HashMap<String, Assign.ReplicaCount> nodeNameVsShardCount =+ Assign.getNodeNameVsShardCount(assignRequest.collectionName, clusterState, nodeList);+ // if nodelist was empty, this map will be empty too. (passing null above however gets a full map)ArrayList<Assign.ReplicaCount> sortedNodeList = new ArrayList<>(nodeNameVsShardCount.values());sortedNodeList.sort(Comparator.comparingInt(Assign.ReplicaCount::weight));nodeList = sortedNodeList.stream().map(replicaCount -> replicaCount.nodeName).collect(Collectors.toList());}+ // otherwise we get a div/0 below+ assert !nodeList.isEmpty();+int i = 0;List<ReplicaPosition> result = new ArrayList<>();- for (String aShard : assignRequest.shardNames)- for (Map.Entry<Replica.Type, Integer> e : ImmutableMap.of(Replica.Type.NRT, assignRequest.numNrtReplicas,- Replica.Type.TLOG, assignRequest.numTlogReplicas,- Replica.Type.PULL, assignRequest.numPullReplicas- ).entrySet()) {+ for (String aShard : assignRequest.shardNames) {+ for (Map.Entry<Replica.Type, Integer> e : countsPerReplicaType(assignRequest).entrySet()) {for (int j = 0; j < e.getValue(); j++) {result.add(new ReplicaPosition(aShard, j, e.getKey(), nodeList.get(i % nodeList.size())));i++;}}+ }return result;}++ // keeps this big ugly construction block out of otherwise legible code+ private ImmutableMap<Replica.Type, Integer> countsPerReplicaType(AssignRequest assignRequest) {+ return ImmutableMap.of(+ Replica.Type.NRT, assignRequest.numNrtReplicas,+ Replica.Type.TLOG, assignRequest.numTlogReplicas,+ Replica.Type.PULL, assignRequest.numPullReplicas+ );+ }}public static class RulesBasedAssignStrategy implements AssignStrategy {