Skip to content

Apache Lucene open-source search software

SOLR-10397: Implement AutoAddReplicasPlanAction

Cao Manh Datb18cf30dd301
diff --git a/solr/core/src/java/org/apache/solr/cloud/autoscaling/AutoAddReplicasPlanAction.java b/solr/core/src/java/org/apache/solr/cloud/autoscaling/AutoAddReplicasPlanAction.java
index a9fc6e2e936..fac69f48fc6 100644
--- a/solr/core/src/java/org/apache/solr/cloud/autoscaling/AutoAddReplicasPlanAction.java
+++ b/solr/core/src/java/org/apache/solr/cloud/autoscaling/AutoAddReplicasPlanAction.java
@@ -17,5 +17,35 @@
package org.apache.solr.cloud.autoscaling;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.solr.client.solrj.cloud.autoscaling.Policy;
+import org.apache.solr.common.cloud.ClusterState;
+import org.apache.solr.common.cloud.DocCollection;
+import org.apache.solr.common.cloud.ZkStateReader;
+
public class AutoAddReplicasPlanAction extends ComputePlanAction {
+ Set<String> autoAddReplicasCollections;
+
+ @Override
+ protected Policy.Suggester getSuggester(Policy.Session session, TriggerEvent event, ZkStateReader zkStateReader) {
+ Policy.Suggester suggester = super.getSuggester(session, event, zkStateReader);
+ if (autoAddReplicasCollections == null) {
+ autoAddReplicasCollections = new HashSet<>();
+
+ ClusterState clusterState = zkStateReader.getClusterState();
+ for (DocCollection collection: clusterState.getCollectionsMap().values()) {
+ if (collection.getAutoAddReplicas()) {
+ autoAddReplicasCollections.add(collection.getName());
+ }
+ }
+ }
+
+ for (String collection : autoAddReplicasCollections) {
+ suggester.hint(Policy.Suggester.Hint.COLL, collection);
+ }
+
+ return suggester;
+ }
}
diff --git a/solr/core/src/java/org/apache/solr/cloud/autoscaling/ComputePlanAction.java b/solr/core/src/java/org/apache/solr/cloud/autoscaling/ComputePlanAction.java
index 809a149d868..cb876a59db1 100644
--- a/solr/core/src/java/org/apache/solr/cloud/autoscaling/ComputePlanAction.java
+++ b/solr/core/src/java/org/apache/solr/cloud/autoscaling/ComputePlanAction.java
@@ -80,7 +80,7 @@ public class ComputePlanAction implements TriggerAction {
AutoScalingConfig config = new AutoScalingConfig(autoScalingConf);
Policy policy = config.getPolicy();
Policy.Session session = policy.createSession(new SolrClientDataProvider(cloudSolrClient));
- Policy.Suggester suggester = getSuggester(session, event);
+ Policy.Suggester suggester = getSuggester(session, event, zkStateReader);
while (true) {
SolrRequest operation = suggester.getOperation();
if (operation == null) break;
@@ -93,7 +93,7 @@ public class ComputePlanAction implements TriggerAction {
return operations;
});
session = suggester.getSession();
- suggester = getSuggester(session, event);
+ suggester = getSuggester(session, event, zkStateReader);
}
}
} catch (KeeperException e) {
@@ -107,7 +107,7 @@ public class ComputePlanAction implements TriggerAction {
}
}
- private Policy.Suggester getSuggester(Policy.Session session, TriggerEvent event) {
+ protected Policy.Suggester getSuggester(Policy.Session session, TriggerEvent event, ZkStateReader zkStateReader) {
Policy.Suggester suggester;
switch (event.getEventType()) {
case NODEADDED:
diff --git a/solr/core/src/test/org/apache/solr/cloud/autoscaling/AutoAddReplicasPlanActionTest.java b/solr/core/src/test/org/apache/solr/cloud/autoscaling/AutoAddReplicasPlanActionTest.java
new file mode 100644
index 00000000000..ebefba24938
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/cloud/autoscaling/AutoAddReplicasPlanActionTest.java
@@ -0,0 +1,153 @@
+/*
+ * 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.cloud.autoscaling;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import org.apache.solr.client.solrj.SolrRequest;
+import org.apache.solr.client.solrj.embedded.JettySolrRunner;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.cloud.ClusterStateUtil;
+import org.apache.solr.common.cloud.ZkStateReader;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.util.TimeOut;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.apache.solr.cloud.autoscaling.AutoScalingHandlerTest.createAutoScalingRequest;
+
+public class AutoAddReplicasPlanActionTest extends SolrCloudTestCase{
+ @BeforeClass
+ public static void setupCluster() throws Exception {
+ configureCluster(3)
+ .addConfig("conf", configset("cloud-minimal"))
+ .configure();
+ }
+
+ @Test
+ public void testSimple() throws Exception {
+ JettySolrRunner jetty1 = cluster.getJettySolrRunner(0);
+ JettySolrRunner jetty2 = cluster.getJettySolrRunner(1);
+ JettySolrRunner jetty3 = cluster.getJettySolrRunner(2);
+
+ String collection1 = "testSimple1";
+ String collection2 = "testSimple2";
+ CollectionAdminRequest.createCollection(collection1, "conf", 2, 2)
+ .setCreateNodeSet(jetty1.getNodeName()+","+jetty2.getNodeName())
+ .setAutoAddReplicas(true)
+ .setMaxShardsPerNode(2)
+ .process(cluster.getSolrClient());
+ CollectionAdminRequest.createCollection(collection2, "conf", 1, 2)
+ .setCreateNodeSet(jetty2.getNodeName()+","+jetty3.getNodeName())
+ .setAutoAddReplicas(false)
+ .setMaxShardsPerNode(1)
+ .process(cluster.getSolrClient());
+ // the number of cores in jetty1 (5) will be larger than jetty3 (1)
+ CollectionAdminRequest.createCollection("testSimple3", "conf", 3, 1)
+ .setCreateNodeSet(jetty1.getNodeName())
+ .setAutoAddReplicas(false)
+ .setMaxShardsPerNode(3)
+ .process(cluster.getSolrClient());
+
+ // we remove the implicit created trigger, so the replicas won't be moved
+ String removeTriggerCommand = "{" +
+ "'remove-trigger' : {" +
+ "'name' : '.auto_add_replicas'" +
+ "}" +
+ "}";
+ SolrRequest req = createAutoScalingRequest(SolrRequest.METHOD.POST, removeTriggerCommand);
+ NamedList response = cluster.getSolrClient().request(req);
+ assertEquals(response.get("result").toString(), "success");
+
+ JettySolrRunner lostJetty = random().nextBoolean()? jetty1 : jetty2;
+ String lostNodeName = lostJetty.getNodeName();
+ Set<String> coreNodeNames = lostJetty.getCoreContainer().getCores().stream()
+ .map(solrCore -> solrCore.getCoreDescriptor().getCloudDescriptor().getCoreNodeName())
+ .collect(Collectors.toSet());
+ lostJetty.stop();
+ waitForNodeLeave(lostNodeName);
+
+ List<SolrRequest> operations = getOperations(jetty3, lostNodeName);
+ assertOperations(collection1, operations, lostNodeName,coreNodeNames, null);
+
+ lostJetty.start();
+ ClusterStateUtil.waitForAllActiveAndLiveReplicas(cluster.getSolrClient().getZkStateReader(), 30000);
+ String setClusterPreferencesCommand = "{" +
+ "'set-cluster-preferences': [" +
+ "{'minimize': 'cores','precision': 0}]" +
+ "}";
+ req = createAutoScalingRequest(SolrRequest.METHOD.POST, setClusterPreferencesCommand);
+ response = cluster.getSolrClient().request(req);
+ assertEquals(response.get("result").toString(), "success");
+
+ lostJetty = random().nextBoolean()? jetty1 : jetty2;
+ lostNodeName = lostJetty.getNodeName();
+ coreNodeNames = lostJetty.getCoreContainer().getCores().stream()
+ .map(solrCore -> solrCore.getCoreDescriptor().getCloudDescriptor().getCoreNodeName())
+ .collect(Collectors.toSet());
+ lostJetty.stop();
+ waitForNodeLeave(lostNodeName);
+
+ operations = getOperations(jetty3, lostNodeName);
+ assertOperations(collection1, operations, lostNodeName, coreNodeNames, jetty3);
+ }
+
+ private void waitForNodeLeave(String lostNodeName) throws InterruptedException {
+ ZkStateReader reader = cluster.getSolrClient().getZkStateReader();
+ TimeOut timeOut = new TimeOut(10, TimeUnit.SECONDS);
+ while (reader.getClusterState().getLiveNodes().contains(lostNodeName)) {
+ Thread.sleep(100);
+ if (timeOut.hasTimedOut()) fail("Wait for " + lostNodeName + " to leave failed!");
+ }
+ }
+
+ private List<SolrRequest> getOperations(JettySolrRunner actionJetty, String lostNodeName) {
+ AutoAddReplicasPlanAction action = new AutoAddReplicasPlanAction();
+ TriggerEvent lostNode = new NodeLostTrigger.NodeLostEvent(AutoScaling.EventType.NODELOST, ".auto_add_replicas", System.currentTimeMillis(), lostNodeName);
+ ActionContext context = new ActionContext(actionJetty.getCoreContainer(), null, new HashMap<>());
+ action.process(lostNode, context);
+ List<SolrRequest> operations = (List) context.getProperty("operations");
+ return operations;
+ }
+
+ private void assertOperations(String collection, List<SolrRequest> operations, String lostNodeName, Set<String> coreNodeNames, JettySolrRunner destJetty) {
+ assertEquals("Replicas of " + collection + " is not fully moved, operations="+operations,
+ 2, operations.size());
+ for (SolrRequest solrRequest : operations) {
+ assertTrue(solrRequest instanceof CollectionAdminRequest.MoveReplica);
+ SolrParams params = solrRequest.getParams();
+
+ assertEquals(params.get("collection"), collection);
+
+ String replica = params.get("replica");
+ assertTrue("Can not find "+replica+ " in node " + lostNodeName, coreNodeNames.contains(replica));
+
+ String targetNode = params.get("targetNode");
+ assertFalse("Target node match the lost one " + lostNodeName, lostNodeName.equals(targetNode));
+ if (destJetty != null) {
+ assertEquals("Target not is expected", destJetty.getNodeName(), targetNode);
+ }
+ }
+ }
+}
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/AddReplicaSuggester.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/AddReplicaSuggester.java
index 7b00c87e0d0..c79ec95a84f 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/AddReplicaSuggester.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/AddReplicaSuggester.java
@@ -18,6 +18,7 @@
package org.apache.solr.client.solrj.cloud.autoscaling;
import java.util.List;
+import java.util.Set;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.cloud.autoscaling.Policy.Suggester;
@@ -33,37 +34,39 @@ class AddReplicaSuggester extends Suggester {
}
SolrRequest tryEachNode(boolean strict) {
- String coll = (String) hints.get(Hint.COLL);
+ Set<String> collections = (Set<String>) hints.get(Hint.COLL);
String shard = (String) hints.get(Hint.SHARD);
- Replica.Type type = Replica.Type.get((String) hints.get(Hint.REPLICATYPE));
- if (coll == null || shard == null)
+ if (collections == null || shard == null) {
throw new RuntimeException("add-replica requires 'collection' and 'shard'");
- //iterate through elements and identify the least loaded
-
- List<Clause.Violation> leastSeriousViolation = null;
- Integer targetNodeIndex = null;
- for (int i = getMatrix().size() - 1; i >= 0; i--) {
- Row row = getMatrix().get(i);
- if(!row.isLive) continue;
- if (!isAllowed(row.node, Hint.TARGET_NODE)) continue;
- Row tmpRow = row.addReplica(coll, shard, type);
- tmpRow.violations.clear();
+ }
+ for (String coll : collections) {
+ Replica.Type type = Replica.Type.get((String) hints.get(Hint.REPLICATYPE));
+ //iterate through elements and identify the least loaded
+ List<Clause.Violation> leastSeriousViolation = null;
+ Integer targetNodeIndex = null;
+ for (int i = getMatrix().size() - 1; i >= 0; i--) {
+ Row row = getMatrix().get(i);
+ if (!row.isLive) continue;
+ if (!isAllowed(row.node, Hint.TARGET_NODE)) continue;
+ Row tmpRow = row.addReplica(coll, shard, type);
+ tmpRow.violations.clear();
- List<Clause.Violation> errs = testChangedMatrix(strict, getModifiedMatrix(getMatrix(), tmpRow, i));
- if(!containsNewErrors(errs)) {
- if(isLessSerious(errs, leastSeriousViolation)){
- leastSeriousViolation = errs;
- targetNodeIndex = i;
+ List<Clause.Violation> errs = testChangedMatrix(strict, getModifiedMatrix(getMatrix(), tmpRow, i));
+ if (!containsNewErrors(errs)) {
+ if (isLessSerious(errs, leastSeriousViolation)) {
+ leastSeriousViolation = errs;
+ targetNodeIndex = i;
+ }
}
}
- }
- if (targetNodeIndex != null) {// there are no rule violations
- getMatrix().set(targetNodeIndex, getMatrix().get(targetNodeIndex).addReplica(coll, shard, type));
- return CollectionAdminRequest
- .addReplicaToShard(coll, shard)
- .setType(type)
- .setNode(getMatrix().get(targetNodeIndex).node);
+ if (targetNodeIndex != null) {// there are no rule violations
+ getMatrix().set(targetNodeIndex, getMatrix().get(targetNodeIndex).addReplica(coll, shard, type));
+ return CollectionAdminRequest
+ .addReplicaToShard(coll, shard)
+ .setType(type)
+ .setNode(getMatrix().get(targetNodeIndex).node);
+ }
}
return null;
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Policy.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Policy.java
index 451c514296e..d013e46ac75 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Policy.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Policy.java
@@ -317,7 +317,7 @@ public class Policy implements MapWriter {
}
public Suggester hint(Hint hint, Object value) {
- if (hint == Hint.TARGET_NODE || hint == Hint.SRC_NODE) {
+ if (hint == Hint.TARGET_NODE || hint == Hint.SRC_NODE || hint == Hint.COLL) {
((Set) hints.computeIfAbsent(hint, h -> new HashSet<>())).add(value);
} else {
hints.put(hint, value == null ? null : String.valueOf(value));
@@ -330,22 +330,25 @@ public class Policy implements MapWriter {
public SolrRequest getOperation() {
if (!isInitialized) {
- String coll = (String) hints.get(Hint.COLL);
+ Set<String> collections = (Set<String>) hints.get(Hint.COLL);
String shard = (String) hints.get(Hint.SHARD);
- // if this is not a known collection from the existing clusterstate,
- // then add it
- if (session.matrix.stream().noneMatch(row -> row.collectionVsShardVsReplicas.containsKey(coll))) {
- session.addClausesForCollection(session.dataProvider, coll);
- Collections.sort(session.expandedClauses);
- }
- if (coll != null) {
- for (Row row : session.matrix) {
- if (!row.collectionVsShardVsReplicas.containsKey(coll)) row.collectionVsShardVsReplicas.put(coll, new HashMap<>());
- if (shard != null) {
- Map<String, List<ReplicaInfo>> shardInfo = row.collectionVsShardVsReplicas.get(coll);
- if (!shardInfo.containsKey(shard)) shardInfo.put(shard, new ArrayList<>());
+ if (collections != null) {
+ for (String coll : collections) {
+ // if this is not a known collection from the existing clusterstate,
+ // then add it
+ if (session.matrix.stream().noneMatch(row -> row.collectionVsShardVsReplicas.containsKey(coll))) {
+ session.addClausesForCollection(session.dataProvider, coll);
+ }
+ for (Row row : session.matrix) {
+ if (!row.collectionVsShardVsReplicas.containsKey(coll))
+ row.collectionVsShardVsReplicas.put(coll, new HashMap<>());
+ if (shard != null) {
+ Map<String, List<ReplicaInfo>> shardInfo = row.collectionVsShardVsReplicas.get(coll);
+ if (!shardInfo.containsKey(shard)) shardInfo.put(shard, new ArrayList<>());
+ }
}
}
+ Collections.sort(session.expandedClauses);
}
Set<String> srcNodes = (Set<String>) hints.get(Hint.SRC_NODE);
if (srcNodes != null && !srcNodes.isEmpty()) {
@@ -443,7 +446,7 @@ public class Policy implements MapWriter {
protected boolean isAllowed(Object v, Hint hint) {
Object hintVal = hints.get(hint);
- if (hint == Hint.TARGET_NODE || hint == Hint.SRC_NODE) {
+ if (hint == Hint.TARGET_NODE || hint == Hint.SRC_NODE || hint == Hint.COLL) {
Set set = (Set) hintVal;
return set == null || set.contains(v);
} else {