HoustonPutman commented on a change in pull request #512:
URL: https://github.com/apache/solr/pull/512#discussion_r783237167



##########
File path: 
solr/core/src/java/org/apache/solr/cluster/placement/plugins/DefaultPlacementFactory.java
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.cluster.placement.plugins;
+
+import org.apache.solr.cluster.Node;
+import org.apache.solr.cluster.Replica;
+import org.apache.solr.cluster.Shard;
+import org.apache.solr.cluster.SolrCollection;
+import org.apache.solr.cluster.placement.PlacementContext;
+import org.apache.solr.cluster.placement.PlacementException;
+import org.apache.solr.cluster.placement.PlacementPlan;
+import org.apache.solr.cluster.placement.PlacementPlugin;
+import org.apache.solr.cluster.placement.PlacementPluginFactory;
+import org.apache.solr.cluster.placement.PlacementRequest;
+import org.apache.solr.cluster.placement.ReplicaPlacement;
+import org.apache.solr.common.SolrException;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * <p>Factory for creating {@link DefaultPlacementPlugin}, a placement plugin 
implementing a smart placement for new
+ * replicas, picking nodes with the fewest cores (especially cores of the same 
collection).</p>
+ *
+ * <p>See {@link AffinityPlacementFactory} for a more realistic example and 
documentation.</p>
+ */
+public class DefaultPlacementFactory implements 
PlacementPluginFactory<PlacementPluginFactory.NoConfig> {
+
+  @Override
+  public PlacementPlugin createPluginInstance() {
+    return new DefaultPlacementPlugin();
+  }
+
+  public static class DefaultPlacementPlugin implements PlacementPlugin {
+    @Override
+    public List<PlacementPlan> computePlacements(Collection<PlacementRequest> 
requests, PlacementContext placementContext) throws PlacementException {
+      List<PlacementPlan> placementPlans = new ArrayList<>(requests.size());
+      Map<Node, ReplicaCount> nodeVsShardCount = 
getNodeVsShardCount(placementContext);
+      for (PlacementRequest request : requests) {
+        int totalReplicasPerShard = 0;
+        for (Replica.ReplicaType rt : Replica.ReplicaType.values()) {
+          totalReplicasPerShard += request.getCountReplicasToCreate(rt);
+        }
+
+        Set<ReplicaPlacement> replicaPlacements = new 
HashSet<>(totalReplicasPerShard * request.getShardNames().size());
+
+        Collection<ReplicaCount> replicaCounts = nodeVsShardCount.values();
+
+        if (request.getTargetNodes().size() < replicaCounts.size()) {
+          replicaCounts = replicaCounts.stream().filter(rc -> 
request.getTargetNodes().contains(rc.node())).collect(Collectors.toList());
+        } else if (placementContext.getCluster().getLiveDataNodes().isEmpty()) 
{

Review comment:
       Good call, I wrote this originally without that information, then tried 
to fix it all when I figured out how `getTargetNodes()` was populated. I'll 
scan and make sure no other checks made it through.

##########
File path: 
solr/core/src/java/org/apache/solr/cluster/placement/plugins/DefaultPlacementFactory.java
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.cluster.placement.plugins;
+
+import org.apache.solr.cluster.Node;
+import org.apache.solr.cluster.Replica;
+import org.apache.solr.cluster.Shard;
+import org.apache.solr.cluster.SolrCollection;
+import org.apache.solr.cluster.placement.PlacementContext;
+import org.apache.solr.cluster.placement.PlacementException;
+import org.apache.solr.cluster.placement.PlacementPlan;
+import org.apache.solr.cluster.placement.PlacementPlugin;
+import org.apache.solr.cluster.placement.PlacementPluginFactory;
+import org.apache.solr.cluster.placement.PlacementRequest;
+import org.apache.solr.cluster.placement.ReplicaPlacement;
+import org.apache.solr.common.SolrException;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * <p>Factory for creating {@link DefaultPlacementPlugin}, a placement plugin 
implementing a smart placement for new
+ * replicas, picking nodes with the fewest cores (especially cores of the same 
collection).</p>
+ *
+ * <p>See {@link AffinityPlacementFactory} for a more realistic example and 
documentation.</p>
+ */
+public class DefaultPlacementFactory implements 
PlacementPluginFactory<PlacementPluginFactory.NoConfig> {
+
+  @Override
+  public PlacementPlugin createPluginInstance() {
+    return new DefaultPlacementPlugin();
+  }
+
+  public static class DefaultPlacementPlugin implements PlacementPlugin {
+    @Override
+    public List<PlacementPlan> computePlacements(Collection<PlacementRequest> 
requests, PlacementContext placementContext) throws PlacementException {
+      List<PlacementPlan> placementPlans = new ArrayList<>(requests.size());
+      Map<Node, ReplicaCount> nodeVsShardCount = 
getNodeVsShardCount(placementContext);
+      for (PlacementRequest request : requests) {
+        int totalReplicasPerShard = 0;
+        for (Replica.ReplicaType rt : Replica.ReplicaType.values()) {
+          totalReplicasPerShard += request.getCountReplicasToCreate(rt);
+        }
+
+        Set<ReplicaPlacement> replicaPlacements = new 
HashSet<>(totalReplicasPerShard * request.getShardNames().size());
+
+        Collection<ReplicaCount> replicaCounts = nodeVsShardCount.values();
+
+        if (request.getTargetNodes().size() < replicaCounts.size()) {
+          replicaCounts = replicaCounts.stream().filter(rc -> 
request.getTargetNodes().contains(rc.node())).collect(Collectors.toList());
+        } else if (placementContext.getCluster().getLiveDataNodes().isEmpty()) 
{
+          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "There 
are no live nodes in the cluster");
+        }
+
+        if (replicaCounts.size() < totalReplicasPerShard) {
+          throw new PlacementException("Cluster size too small for number of 
replicas per shard");

Review comment:
       Yeah that quickly broke many tests. Will remove, that's a good call.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to