tflobbe commented on code in PR #1730: URL: https://github.com/apache/solr/pull/1730#discussion_r1244464085
########## solr/core/src/java/org/apache/solr/cloud/api/collections/MigrateReplicasCmd.java: ########## @@ -0,0 +1,158 @@ +/* + * 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.api.collections; + +import static org.apache.solr.common.params.CommonAdminParams.ASYNC; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.cloud.ClusterState; +import org.apache.solr.common.cloud.Replica; +import org.apache.solr.common.cloud.ReplicaPosition; +import org.apache.solr.common.cloud.ZkNodeProps; +import org.apache.solr.common.cloud.ZkStateReader; +import org.apache.solr.common.params.CollectionParams; +import org.apache.solr.common.params.CommonAdminParams; +import org.apache.solr.common.util.CollectionUtil; +import org.apache.solr.common.util.NamedList; + +public class MigrateReplicasCmd implements CollApiCmds.CollectionApiCommand { + + private final CollectionCommandContext ccc; + + public MigrateReplicasCmd(CollectionCommandContext ccc) { + this.ccc = ccc; + } + + @Override + public void call(ClusterState state, ZkNodeProps message, NamedList<Object> results) + throws Exception { + ZkStateReader zkStateReader = ccc.getZkStateReader(); + Set<String> sourceNodes = getNodesFromParam(message, CollectionParams.SOURCE_NODES); + Set<String> targetNodes = getNodesFromParam(message, CollectionParams.TARGET_NODES); + boolean waitForFinalState = message.getBool(CommonAdminParams.WAIT_FOR_FINAL_STATE, false); + if (sourceNodes.isEmpty()) { + throw new SolrException( + SolrException.ErrorCode.BAD_REQUEST, "sourceNodes is a required param"); + } + String async = message.getStr(ASYNC); + int timeout = message.getInt("timeout", 10 * 60); // 10 minutes + boolean parallel = message.getBool("parallel", false); + ClusterState clusterState = zkStateReader.getClusterState(); + + for (String sourceNode : sourceNodes) { + if (!clusterState.liveNodesContain(sourceNode)) { + throw new SolrException( + SolrException.ErrorCode.BAD_REQUEST, "Source Node: " + sourceNode + " is not live"); + } Review Comment: Should this be allowed? ########## solr/core/src/test/org/apache/solr/cloud/MigrateReplicasTest.java: ########## @@ -0,0 +1,372 @@ +/* + * 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; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import com.codahale.metrics.Metric; +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.util.ArrayList; +import java.util.Collections; +import java.util.EnumSet; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.entity.ContentType; +import org.apache.http.util.EntityUtils; +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.impl.CloudLegacySolrClient; +import org.apache.solr.client.solrj.impl.CloudSolrClient; +import org.apache.solr.client.solrj.request.CollectionAdminRequest; +import org.apache.solr.client.solrj.request.CoreAdminRequest; +import org.apache.solr.client.solrj.response.CoreAdminResponse; +import org.apache.solr.common.cloud.DocCollection; +import org.apache.solr.common.cloud.Replica; +import org.apache.solr.common.cloud.Slice; +import org.apache.solr.common.cloud.ZkStateReader; +import org.apache.solr.common.util.StrUtils; +import org.apache.solr.common.util.Utils; +import org.apache.solr.embedded.JettySolrRunner; +import org.apache.solr.handler.admin.api.MigrateReplicasAPI; +import org.apache.solr.metrics.MetricsMap; +import org.apache.solr.metrics.SolrMetricManager; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.noggit.JSONParser; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MigrateReplicasTest extends SolrCloudTestCase { + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @BeforeClass + public static void setupCluster() { + System.setProperty("metricsEnabled", "true"); + } + + @Before + public void clearPreviousCluster() throws Exception { + // Clear the previous cluster before each test, since they use different numbers of nodes. + shutdownCluster(); + } + + @Test + public void test() throws Exception { + configureCluster(6) + .addConfig( + "conf1", TEST_PATH().resolve("configsets").resolve("cloud-dynamic").resolve("conf")) + .configure(); + String coll = "replacenodetest_coll"; + if (log.isInfoEnabled()) { + log.info("total_jettys: {}", cluster.getJettySolrRunners().size()); + } + + CloudSolrClient cloudClient = cluster.getSolrClient(); + Set<String> liveNodes = cloudClient.getClusterState().getLiveNodes(); + ArrayList<String> l = new ArrayList<>(liveNodes); + Collections.shuffle(l, random()); + String emptyNode = l.remove(0); + String nodeToBeDecommissioned = l.get(0); + CollectionAdminRequest.Create create; + // NOTE: always using the createCollection that takes in 'int' for all types of replicas, so we + // never have to worry about null checking when comparing the Create command with the final + // Slices + + // TODO: tlog replicas do not work correctly in tests due to fault + // TestInjection#waitForInSyncWithLeader + create = + pickRandom( + CollectionAdminRequest.createCollection(coll, "conf1", 5, 2, 0, 0), + // CollectionAdminRequest.createCollection(coll, "conf1", 5, 1,1,0), + // CollectionAdminRequest.createCollection(coll, "conf1", 5, 0,1,1), + // CollectionAdminRequest.createCollection(coll, "conf1", 5, 1,0,1), + // CollectionAdminRequest.createCollection(coll, "conf1", 5, 0,2,0), + // check also replicationFactor 1 + CollectionAdminRequest.createCollection(coll, "conf1", 5, 1, 0, 0) + // CollectionAdminRequest.createCollection(coll, "conf1", 5, 0,1,0) + ); + create.setCreateNodeSet(StrUtils.join(l, ',')); + cloudClient.request(create); + + cluster.waitForActiveCollection( + coll, + 5, + 5 + * (create.getNumNrtReplicas() + + create.getNumPullReplicas() + + create.getNumTlogReplicas())); + + DocCollection collection = cloudClient.getClusterState().getCollection(coll); + log.debug("### Before decommission: {}", collection); + log.info("excluded_node : {} ", emptyNode); + Map<?, ?> response = + callMigrateReplicas( + cloudClient, + new MigrateReplicasAPI.MigrateReplicasRequestBody( + Set.of(nodeToBeDecommissioned), Set.of(emptyNode), true, null)); + assertEquals( + "MigrateReplicas request was unsuccessful", + 0L, + ((Map<?, ?>) response.get("responseHeader")).get("status")); + ZkStateReader zkStateReader = ZkStateReader.from(cloudClient); + try (SolrClient coreClient = + getHttpSolrClient(zkStateReader.getBaseUrlForNodeName(nodeToBeDecommissioned))) { + CoreAdminResponse status = CoreAdminRequest.getStatus(null, coreClient); + assertEquals(0, status.getCoreStatus().size()); + } + + Thread.sleep(5000); + collection = cloudClient.getClusterState().getCollection(coll); + log.debug("### After decommission: {}", collection); + // check what are replica states on the decommissioned node + List<Replica> replicas = collection.getReplicas(nodeToBeDecommissioned); + if (replicas == null) { + replicas = Collections.emptyList(); + } Review Comment: Isn't this supposed to always be empty? -- 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]
