Apache Lucene open-source search software
SOLR-9320: A REPLACENODE command to decommission an existing node with another new node and SOLR-9318 the DELETENODE command that deletes all replicas in a node
Noble Paulae60c74f8c6e
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 4da2d0eef94..889611f6b9a 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -100,6 +100,11 @@ New Features* SOLR-9275: XML QueryParser support (defType=xmlparser) now extensible via configuration.(Christine Poerschke)+* SOLR-9320: A REPLACENODE command to decommission an existing node with another new node+ (noble, Nitin Sharma, Varun Thacker)++* SOLR-9318: A DELETENODE command to delete all replicas in that node (noble, Nitin Sharma, Varun Thacker)+Bug Fixes----------------------diff --git a/solr/core/src/java/org/apache/solr/cloud/DeleteNodeCmd.java b/solr/core/src/java/org/apache/solr/cloud/DeleteNodeCmd.javanew file mode 100644index 00000000000..cbcfa8847df--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cloud/DeleteNodeCmd.java@@ -0,0 +1,86 @@+/*+ * 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 java.lang.invoke.MethodHandles;+import java.util.List;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.TimeUnit;++import org.apache.solr.common.SolrException;+import org.apache.solr.common.cloud.ClusterState;+import org.apache.solr.common.cloud.ZkNodeProps;+import org.apache.solr.common.util.NamedList;+import org.apache.solr.common.util.Utils;+import org.apache.zookeeper.KeeperException;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class DeleteNodeCmd implements OverseerCollectionMessageHandler.Cmd {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ private final OverseerCollectionMessageHandler ocmh;++ public DeleteNodeCmd(OverseerCollectionMessageHandler ocmh) {+ this.ocmh = ocmh;+ }++ @Override+ public Object call(ClusterState state, ZkNodeProps message, NamedList results) throws Exception {+ ocmh.checkRequired(message, "node");+ String node = message.getStr("node");+ if (!state.liveNodesContain(node)) {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Source Node: " + node + " is not live");+ }+ List<ZkNodeProps> sourceReplicas = ReplaceNodeCmd.getReplicasOfNode(node, state);+ cleanupReplicas(results, state, sourceReplicas, ocmh);+ return null;+ }++ static void cleanupReplicas(NamedList results,+ ClusterState clusterState,+ List<ZkNodeProps> sourceReplicas,+ OverseerCollectionMessageHandler ocmh) throws InterruptedException {+ CountDownLatch cleanupLatch = new CountDownLatch(sourceReplicas.size());+ for (ZkNodeProps sourceReplica : sourceReplicas) {+ log.info("deleting replica from from node {} ", Utils.toJSONString(sourceReplica));+ NamedList deleteResult = new NamedList();+ try {+ ocmh.deleteReplica(clusterState, sourceReplica.plus("parallel", "true"), deleteResult, () -> {+ cleanupLatch.countDown();+ if (deleteResult.get("failure") != null) {+ synchronized (results) {+ results.add("failure", "could not delete because " + deleteResult.get("failure") + " " + Utils.toJSONString(sourceReplica));+ }+ }+ });+ } catch (KeeperException e) {+ log.info("Error deleting ", e);+ cleanupLatch.countDown();+ } catch (Exception e) {+ cleanupLatch.countDown();+ throw e;+ }+ }+ log.info("Waiting for deletes to complete");+ cleanupLatch.await(5, TimeUnit.MINUTES);+ }+++}diff --git a/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionConfigSetProcessor.java b/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionConfigSetProcessor.javaindex f8f84462df0..8c7a056299e 100644--- a/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionConfigSetProcessor.java+++ b/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionConfigSetProcessor.java@@ -16,6 +16,10 @@*/package org.apache.solr.cloud;+import java.io.IOException;++import org.apache.commons.io.IOUtils;+import org.apache.solr.common.cloud.ZkNodeProps;import org.apache.solr.common.cloud.ZkStateReader;import org.apache.solr.handler.component.ShardHandler;import org.apache.solr.handler.component.ShardHandlerFactory;@@ -83,12 +87,20 @@ public class OverseerCollectionConfigSetProcessor extends OverseerTaskProcessorzkStateReader, myId, shardHandlerFactory, adminPath, stats, overseer, overseerNodePrioritizer);final OverseerConfigSetMessageHandler configMessageHandler = new OverseerConfigSetMessageHandler(zkStateReader);- return message -> {- String operation = message.getStr(Overseer.QUEUE_OPERATION);- if (operation != null && operation.startsWith(CONFIGSETS_ACTION_PREFIX)) {- return configMessageHandler;+ return new OverseerMessageHandlerSelector() {+ @Override+ public void close() throws IOException {+ IOUtils.closeQuietly(collMessageHandler);+ }++ @Override+ public OverseerMessageHandler selectOverseerMessageHandler(ZkNodeProps message) {+ String operation = message.getStr(Overseer.QUEUE_OPERATION);+ if (operation != null && operation.startsWith(CONFIGSETS_ACTION_PREFIX)) {+ return configMessageHandler;+ }+ return collMessageHandler;}- return collMessageHandler;};}}diff --git a/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionMessageHandler.java b/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionMessageHandler.javaindex 4e7e429bb75..908d35c3e6b 100644--- a/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionMessageHandler.java+++ b/solr/core/src/java/org/apache/solr/cloud/OverseerCollectionMessageHandler.java@@ -16,6 +16,7 @@*/package org.apache.solr.cloud;+import java.io.Closeable;import java.io.IOException;import java.lang.invoke.MethodHandles;import java.net.URI;@@ -35,8 +36,13 @@ import java.util.Optional;import java.util.Properties;import java.util.Random;import java.util.Set;+import java.util.concurrent.Callable;+import java.util.concurrent.ExecutorService;+import java.util.concurrent.SynchronousQueue;import java.util.concurrent.TimeUnit;+import java.util.concurrent.atomic.AtomicReference;+import com.google.common.collect.ImmutableMap;import org.apache.commons.lang.StringUtils;import org.apache.solr.client.solrj.SolrResponse;import org.apache.solr.client.solrj.SolrServerException;@@ -75,6 +81,7 @@ import org.apache.solr.common.params.CoreAdminParams;import org.apache.solr.common.params.CoreAdminParams.CoreAdminAction;import org.apache.solr.common.params.ModifiableSolrParams;import org.apache.solr.common.params.ShardParams;+import org.apache.solr.common.util.ExecutorUtil;import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SimpleOrderedMap;import org.apache.solr.common.util.StrUtils;@@ -88,6 +95,7 @@ import org.apache.solr.handler.component.ShardHandlerFactory;import org.apache.solr.handler.component.ShardRequest;import org.apache.solr.handler.component.ShardResponse;import org.apache.solr.update.SolrIndexSplitter;+import org.apache.solr.util.DefaultSolrThreadFactory;import org.apache.solr.util.RTimer;import org.apache.solr.util.TimeOut;import org.apache.solr.util.stats.Snapshot;@@ -119,10 +127,12 @@ import static org.apache.solr.common.params.CollectionParams.CollectionAction.BAimport static org.apache.solr.common.params.CollectionParams.CollectionAction.CREATE;import static org.apache.solr.common.params.CollectionParams.CollectionAction.CREATESHARD;import static org.apache.solr.common.params.CollectionParams.CollectionAction.DELETE;+import static org.apache.solr.common.params.CollectionParams.CollectionAction.DELETENODE;import static org.apache.solr.common.params.CollectionParams.CollectionAction.DELETEREPLICAPROP;import static org.apache.solr.common.params.CollectionParams.CollectionAction.DELETESHARD;import static org.apache.solr.common.params.CollectionParams.CollectionAction.MIGRATESTATEFORMAT;import static org.apache.solr.common.params.CollectionParams.CollectionAction.REMOVEROLE;+import static org.apache.solr.common.params.CollectionParams.CollectionAction.REPLACENODE;import static org.apache.solr.common.params.CommonAdminParams.ASYNC;import static org.apache.solr.common.params.CommonParams.NAME;import static org.apache.solr.common.util.StrUtils.formatString;@@ -132,7 +142,7 @@ import static org.apache.solr.common.util.Utils.makeMap;* A {@link OverseerMessageHandler} that handles Collections API related* overseer messages.*/-public class OverseerCollectionMessageHandler implements OverseerMessageHandler {+public class OverseerCollectionMessageHandler implements OverseerMessageHandler , Closeable {public static final String NUM_SLICES = "numShards";@@ -172,7 +182,7 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlerprivate Overseer overseer;private ShardHandlerFactory shardHandlerFactory;private String adminPath;- private ZkStateReader zkStateReader;+ ZkStateReader zkStateReader;private String myId;private Overseer.Stats stats;private OverseerNodePrioritizer overseerPrioritizer;@@ -181,6 +191,9 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandler// This is used for handling mutual exclusion of the tasks.final private LockTree lockTree = new LockTree();+ ExecutorService tpe = new ExecutorUtil.MDCAwareThreadPoolExecutor(5, 10, 0L, TimeUnit.MILLISECONDS,+ new SynchronousQueue<>(),+ new DefaultSolrThreadFactory("OverseerCollectionMessageHandlerThreadFactory"));static final Random RANDOM;static {@@ -193,6 +206,7 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlerRANDOM = new Random(seed.hashCode());}}+ private final Map<CollectionParams.CollectionAction, Cmd> commandMap;public OverseerCollectionMessageHandler(ZkStateReader zkStateReader, String myId,final ShardHandlerFactory shardHandlerFactory,@@ -207,6 +221,11 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlerthis.stats = stats;this.overseer = overseer;this.overseerPrioritizer = overseerPrioritizer;+ commandMap = new ImmutableMap.Builder<CollectionParams.CollectionAction, Cmd>()+ .put(REPLACENODE, new ReplaceNodeCmd(this))+ .put(DELETENODE, new DeleteNodeCmd(this))+ .build()+ ;}@Override@@ -244,7 +263,7 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlercreateShard(zkStateReader.getClusterState(), message, results);break;case DELETEREPLICA:- deleteReplica(zkStateReader.getClusterState(), message, results);+ deleteReplica(zkStateReader.getClusterState(), message, results, null);break;case MIGRATE:migrate(zkStateReader.getClusterState(), message, results);@@ -256,7 +275,7 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlerprocessRoleCommand(message, operation);break;case ADDREPLICA:- addReplica(zkStateReader.getClusterState(), message, results);+ addReplica(zkStateReader.getClusterState(), message, results, null);break;case OVERSEERSTATUS:getOverseerStatus(message, results);@@ -294,9 +313,15 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlerresults.add("MOCK_FINISHED", System.currentTimeMillis());break;}- default:- throw new SolrException(ErrorCode.BAD_REQUEST, "Unknown operation:"- + operation);+ default: {+ Cmd command = commandMap.get(action);+ if (command != null) {+ command.call(zkStateReader.getClusterState(),message, results);+ } else {+ throw new SolrException(ErrorCode.BAD_REQUEST, "Unknown operation:"+ + operation);+ }+ }}} catch (Exception e) {String collName = message.getStr("collection");@@ -590,12 +615,14 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandler}@SuppressWarnings("unchecked")- private void deleteReplica(ClusterState clusterState, ZkNodeProps message, NamedList results)+ void deleteReplica(ClusterState clusterState, ZkNodeProps message, NamedList results, Runnable onComplete)throws KeeperException, InterruptedException {+ log.info("deleteReplica() : {}", Utils.toJSONString(message));checkRequired(message, COLLECTION_PROP, SHARD_ID_PROP, REPLICA_PROP);String collectionName = message.getStr(COLLECTION_PROP);String shard = message.getStr(SHARD_ID_PROP);String replicaName = message.getStr(REPLICA_PROP);+ boolean parallel = message.getBool("parallel", false);DocCollection coll = clusterState.getCollection(collectionName);Slice slice = coll.getSlice(shard);@@ -623,9 +650,9 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlerShardHandler shardHandler = shardHandlerFactory.getShardHandler();String core = replica.getStr(ZkStateReader.CORE_NAME_PROP);String asyncId = message.getStr(ASYNC);- Map<String, String> requestMap = null;+ AtomicReference<Map<String, String>> requestMap = new AtomicReference<>(null);if (asyncId != null) {- requestMap = new HashMap<>(1, 1.0f);+ requestMap.set(new HashMap<>(1, 1.0f));}ModifiableSolrParams params = new ModifiableSolrParams();@@ -636,19 +663,42 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlerparams.set(CoreAdminParams.DELETE_INSTANCE_DIR, message.getBool(CoreAdminParams.DELETE_INSTANCE_DIR, true));params.set(CoreAdminParams.DELETE_DATA_DIR, message.getBool(CoreAdminParams.DELETE_DATA_DIR, true));- sendShardRequest(replica.getNodeName(), params, shardHandler, asyncId, requestMap);+ sendShardRequest(replica.getNodeName(), params, shardHandler, asyncId, requestMap.get());+ AtomicReference<Exception> exp = new AtomicReference<>();- processResponses(results, shardHandler, false, null, asyncId, requestMap);+ Callable<Boolean> callable = () -> {+ try {+ processResponses(results, shardHandler, false, null, asyncId, requestMap.get());- //check if the core unload removed the corenode zk entry- if (waitForCoreNodeGone(collectionName, shard, replicaName, 5000)) return;+ //check if the core unload removed the corenode zk entry+ if (waitForCoreNodeGone(collectionName, shard, replicaName, 5000)) return Boolean.TRUE;- // try and ensure core info is removed from cluster state- deleteCoreNode(collectionName, replicaName, replica, core);- if (waitForCoreNodeGone(collectionName, shard, replicaName, 30000)) return;-- throw new SolrException(ErrorCode.SERVER_ERROR,- "Could not remove replica : " + collectionName + "/" + shard + "/" + replicaName);+ // try and ensure core info is removed from cluster state+ deleteCoreNode(collectionName, replicaName, replica, core);+ if (waitForCoreNodeGone(collectionName, shard, replicaName, 30000)) return Boolean.TRUE;+ return Boolean.FALSE;+ } catch (Exception e) {+ results.add("failure", "Could not complete delete " + e.getMessage());+ throw e;+ } finally {+ if (onComplete != null) onComplete.run();+ }+ };++ if (!parallel) {+ try {+ if (!callable.call())+ throw new SolrException(ErrorCode.SERVER_ERROR,+ "Could not remove replica : " + collectionName + "/" + shard + "/" + replicaName);+ } catch (InterruptedException | KeeperException e) {+ throw e;+ } catch (Exception ex) {+ throw new SolrException(ErrorCode.UNKNOWN, "Error waiting for corenode gone", ex);+ }++ } else {+ tpe.submit(callable);+ }}private boolean waitForCoreNodeGone(String collectionName, String shard, String replicaName, int timeoutms) throws InterruptedException {@@ -679,7 +729,7 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlerOverseer.getStateUpdateQueue(zkStateReader.getZkClient()).offer(Utils.toJSON(m));}- private void checkRequired(ZkNodeProps message, String... props) {+ void checkRequired(ZkNodeProps message, String... props) {for (String prop : props) {if(message.get(prop) == null){throw new SolrException(ErrorCode.BAD_REQUEST, StrUtils.join(Arrays.asList(props),',') +" are required params" );@@ -1137,7 +1187,7 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlerif (asyncId != null) {propMap.put(ASYNC, asyncId);}- addReplica(clusterState, new ZkNodeProps(propMap), results);+ addReplica(clusterState, new ZkNodeProps(propMap), results, null);}processResponses(results, shardHandler, true, "SPLITSHARD failed to create subshard leaders", asyncId, requestMap);@@ -1307,7 +1357,7 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandler// now actually create replica cores on sub shard nodesfor (Map<String, Object> replica : replicas) {- addReplica(clusterState, new ZkNodeProps(replica), results);+ addReplica(clusterState, new ZkNodeProps(replica), results, null);}processResponses(results, shardHandler, true, "SPLITSHARD failed to create subshard replicas", asyncId, requestMap);@@ -1681,7 +1731,7 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlerif(asyncId != null) {props.put(ASYNC, asyncId);}- addReplica(clusterState, new ZkNodeProps(props), results);+ addReplica(clusterState, new ZkNodeProps(props), results, null);processResponses(results, shardHandler, true, "MIGRATE failed to create replica of " +"temporary collection in target leader node.", asyncId, requestMap);@@ -2110,12 +2160,14 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandler}}- private void addReplica(ClusterState clusterState, ZkNodeProps message, NamedList results)+ ZkNodeProps addReplica(ClusterState clusterState, ZkNodeProps message, NamedList results, Runnable onComplete)throws KeeperException, InterruptedException {+ log.info("addReplica() : {}", Utils.toJSONString(message));String collection = message.getStr(COLLECTION_PROP);String node = message.getStr(CoreAdminParams.NODE);String shard = message.getStr(SHARD_ID_PROP);String coreName = message.getStr(CoreAdminParams.NAME);+ boolean parallel = message.getBool("parallel", false);if (StringUtils.isBlank(coreName)) {coreName = message.getStr(CoreAdminParams.PROPERTY_PREFIX + CoreAdminParams.NAME);}@@ -2138,7 +2190,7 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlernode = getNodesForNewReplicas(clusterState, collection, shard, 1, node,overseer.getZkController().getCoreContainer()).get(0).nodeName;}- log.info("Node not provided, Identified {} for creating new replica", node);+ log.info("Node Identified {} for creating new replica", node);if (!clusterState.liveNodesContain(node)) {throw new SolrException(ErrorCode.BAD_REQUEST, "Node: " + node + " is not live");@@ -2161,10 +2213,14 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlerif (!Overseer.isLegacy(zkStateReader)) {if (!skipCreateReplicaInClusterState) {- ZkNodeProps props = new ZkNodeProps(Overseer.QUEUE_OPERATION, ADDREPLICA.toLower(), ZkStateReader.COLLECTION_PROP,- collection, ZkStateReader.SHARD_ID_PROP, shard, ZkStateReader.CORE_NAME_PROP, coreName,- ZkStateReader.STATE_PROP, Replica.State.DOWN.toString(), ZkStateReader.BASE_URL_PROP,- zkStateReader.getBaseUrlForNodeName(node), ZkStateReader.NODE_NAME_PROP, node);+ ZkNodeProps props = new ZkNodeProps(+ Overseer.QUEUE_OPERATION, ADDREPLICA.toLower(),+ ZkStateReader.COLLECTION_PROP, collection,+ ZkStateReader.SHARD_ID_PROP, shard,+ ZkStateReader.CORE_NAME_PROP, coreName,+ ZkStateReader.STATE_PROP, Replica.State.DOWN.toString(),+ ZkStateReader.BASE_URL_PROP, zkStateReader.getBaseUrlForNodeName(node),+ ZkStateReader.NODE_NAME_PROP, node);Overseer.getStateUpdateQueue(zkStateReader.getZkClient()).offer(Utils.toJSON(props));}params.set(CoreAdminParams.CORE_NODE_NAME,@@ -2204,9 +2260,28 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandlerMap<String,String> requestMap = new HashMap<>();sendShardRequest(node, params, shardHandler, asyncId, requestMap);- processResponses(results, shardHandler, true, "ADDREPLICA failed to create replica", asyncId, requestMap);+ final String fnode = node;+ final String fcoreName = coreName;++ Runnable runnable = () -> {+ processResponses(results, shardHandler, true, "ADDREPLICA failed to create replica", asyncId, requestMap);+ waitForCoreNodeName(collection, fnode, fcoreName);+ if (onComplete != null) onComplete.run();+ };- waitForCoreNodeName(collection, node, coreName);+ if (!parallel) {+ runnable.run();+ } else {+ tpe.submit(runnable);+ }+++ return new ZkNodeProps(+ ZkStateReader.COLLECTION_PROP, collection,+ ZkStateReader.SHARD_ID_PROP, shard,+ ZkStateReader.CORE_NAME_PROP, coreName,+ ZkStateReader.NODE_NAME_PROP, node+ );}private void processBackupAction(ZkNodeProps message, NamedList results) throws IOException, KeeperException, InterruptedException {@@ -2394,7 +2469,7 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandler}addPropertyParams(message, propMap);- addReplica(clusterState, new ZkNodeProps(propMap), new NamedList());+ addReplica(clusterState, new ZkNodeProps(propMap), new NamedList(), null);}//refresh the location copy of collection state@@ -2443,7 +2518,7 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandler}addPropertyParams(message, propMap);- addReplica(zkStateReader.getClusterState(), new ZkNodeProps(propMap), results);+ addReplica(zkStateReader.getClusterState(), new ZkNodeProps(propMap), results, null);}}}@@ -2503,7 +2578,7 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandler}return configName;}-+private void validateConfigOrThrowSolrException(String configName) throws KeeperException, InterruptedException {boolean isValid = zkStateReader.getZkClient().exists(ZkConfigManager.CONFIGS_ZKNODE + "/" + configName, true);if(!isValid) {@@ -2723,4 +2798,19 @@ public class OverseerCollectionMessageHandler implements OverseerMessageHandler);}+ @Override+ public void close() throws IOException {+ if (tpe != null) {+ if (!tpe.isShutdown()) {+ ExecutorUtil.shutdownAndAwaitTermination(tpe);+ }+ }+ }++ interface Cmd {++ Object call(ClusterState state, ZkNodeProps message, NamedList results) throws Exception;++ }+}diff --git a/solr/core/src/java/org/apache/solr/cloud/OverseerTaskProcessor.java b/solr/core/src/java/org/apache/solr/cloud/OverseerTaskProcessor.javaindex e3bc1f3af18..074cf163c4a 100644--- a/solr/core/src/java/org/apache/solr/cloud/OverseerTaskProcessor.java+++ b/solr/core/src/java/org/apache/solr/cloud/OverseerTaskProcessor.java@@ -31,6 +31,7 @@ import java.util.concurrent.TimeUnit;import java.util.function.Predicate;import com.google.common.collect.ImmutableSet;+import org.apache.commons.io.IOUtils;import org.apache.solr.client.solrj.SolrResponse;import org.apache.solr.cloud.OverseerTaskQueue.QueueEvent;import org.apache.solr.cloud.Overseer.LeaderStatus;@@ -115,7 +116,7 @@ public class OverseerTaskProcessor implements Runnable, Closeable {private final Object waitLock = new Object();- private OverseerMessageHandlerSelector selector;+ protected OverseerMessageHandlerSelector selector;private OverseerNodePrioritizer prioritizer;@@ -328,6 +329,7 @@ public class OverseerTaskProcessor implements Runnable, Closeable {ExecutorUtil.shutdownAndAwaitTermination(tpe);}}+ IOUtils.closeQuietly(selector);}public static List<String> getSortedOverseerNodeNames(SolrZkClient zk) throws KeeperException, InterruptedException {@@ -588,7 +590,7 @@ public class OverseerTaskProcessor implements Runnable, Closeable {* messages only) , or a different handler could be selected based on the* contents of the message.*/- public interface OverseerMessageHandlerSelector {+ public interface OverseerMessageHandlerSelector extends Closeable {OverseerMessageHandler selectOverseerMessageHandler(ZkNodeProps message);}diff --git a/solr/core/src/java/org/apache/solr/cloud/ReplaceNodeCmd.java b/solr/core/src/java/org/apache/solr/cloud/ReplaceNodeCmd.javanew file mode 100644index 00000000000..0cfd0894ffe--- /dev/null+++ b/solr/core/src/java/org/apache/solr/cloud/ReplaceNodeCmd.java@@ -0,0 +1,164 @@+/*+ * 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 java.lang.invoke.MethodHandles;+import java.util.ArrayList;+import java.util.List;+import java.util.Map;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.atomic.AtomicBoolean;++import org.apache.solr.common.SolrException;+import org.apache.solr.common.cloud.ClusterState;+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.ZkNodeProps;+import org.apache.solr.common.cloud.ZkStateReader;+import org.apache.solr.common.params.CoreAdminParams;+import org.apache.solr.common.util.NamedList;+import org.apache.solr.common.util.Utils;+import org.apache.zookeeper.KeeperException;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++import static org.apache.solr.common.cloud.ZkStateReader.COLLECTION_PROP;+import static org.apache.solr.common.cloud.ZkStateReader.SHARD_ID_PROP;++public class ReplaceNodeCmd implements OverseerCollectionMessageHandler.Cmd {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ private final OverseerCollectionMessageHandler ocmh;++ public ReplaceNodeCmd(OverseerCollectionMessageHandler ocmh) {+ this.ocmh = ocmh;+ }++ @Override+ public Object call(ClusterState state, ZkNodeProps message, NamedList results) throws Exception {+ ZkStateReader zkStateReader = ocmh.zkStateReader;+ ocmh.checkRequired(message, "source", "target");+ String source = message.getStr("source");+ String target = message.getStr("target");+ boolean parallel = message.getBool("parallel", false);+ ClusterState clusterState = zkStateReader.getClusterState();++ if (!clusterState.liveNodesContain(source)) {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Source Node: " + source + " is not live");+ }+ if (!clusterState.liveNodesContain(target)) {+ throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Target Node: " + target + " is not live");+ }+ List<ZkNodeProps> sourceReplicas = getReplicasOfNode(source, clusterState);++ List<ZkNodeProps> createdReplicas = new ArrayList<>();++ AtomicBoolean anyOneFailed = new AtomicBoolean(false);+ CountDownLatch countDownLatch = new CountDownLatch(sourceReplicas.size());++ for (ZkNodeProps sourceReplica : sourceReplicas) {+ NamedList nl = new NamedList();+ log.info("going to create replica {}", Utils.toJSONString(sourceReplica));+ ZkNodeProps msg = sourceReplica.plus("parallel", String.valueOf(parallel)).plus(CoreAdminParams.NODE, target);+ final ZkNodeProps addedReplica = ocmh.addReplica(clusterState,+ msg, nl, () -> {+ countDownLatch.countDown();+ if (nl.get("failure") != null) {+ log.warn("failed to create : " + Utils.toJSONString(msg));+ // one replica creation failed. Make the best attempt to+ // delete all the replicas created so far in the target+ // and exit+ synchronized (results) {+ results.add("failure", "Could not create copy of replica " + Utils.toJSONString(sourceReplica));+ anyOneFailed.set(true);+ }+ } else {+ log.info("successfully created : " + Utils.toJSONString(msg));++ }+ });++ if (addedReplica != null) {+ createdReplicas.add(addedReplica);+ }+ }++ log.info("Waiting for creates to complete ");+ countDownLatch.await(5, TimeUnit.MINUTES);+ log.info("Waiting over for creates to complete ");++ if (anyOneFailed.get()) {+ log.info("failed to create some cores delete all " + Utils.toJSONString(createdReplicas));+ CountDownLatch cleanupLatch = new CountDownLatch(createdReplicas.size());+ for (ZkNodeProps createdReplica : createdReplicas) {+ NamedList deleteResult = new NamedList();+ try {+ ocmh.deleteReplica(zkStateReader.getClusterState(), createdReplica.plus("parallel", "true"), deleteResult, () -> {+ cleanupLatch.countDown();+ if (deleteResult.get("failure") != null) {+ synchronized (results) {+ results.add("failure", "could not cleanup, because : " + deleteResult.get("failure") + " " + Utils.toJSONString(createdReplica));+ }+ }+ });+ } catch (KeeperException e) {+ cleanupLatch.countDown();+ log.info("Error deleting ", e);+ } catch (Exception e) {+ log.error("Unknown Error deleteing", e);+ cleanupLatch.countDown();+ throw e;+ }+ }+ cleanupLatch.await(5, TimeUnit.MINUTES);+ return null;+ }+++ // we have reached this far means all replicas could be recreated+ //now cleanup the replicas in the source node+ DeleteNodeCmd.cleanupReplicas(results, state, sourceReplicas, ocmh);+ results.add("success", "REPLACENODE completed successfully from : " + source + " to : " + target);+ return null;+ }++ static List<ZkNodeProps> getReplicasOfNode(String source, ClusterState state) {+ List<ZkNodeProps> sourceReplicas = new ArrayList<>();+ for (Map.Entry<String, DocCollection> e : state.getCollectionsMap().entrySet()) {+ for (Slice slice : e.getValue().getSlices()) {+ for (Replica replica : slice.getReplicas()) {+ if (source.equals(replica.getNodeName())) {+ ZkNodeProps props = new ZkNodeProps(+ COLLECTION_PROP, e.getKey(),+ SHARD_ID_PROP, slice.getName(),+ ZkStateReader.CORE_NAME_PROP, replica.getCoreName(),+ ZkStateReader.REPLICA_PROP, replica.getName(),+ CoreAdminParams.NODE, source);+ log.info("src_core : {}", Utils.toJSONString(props));+ sourceReplicas.add(props+ );+ }+ }+ }+ }+ return sourceReplicas;+ }+}diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.javaindex cb7279080ea..2e906aed55b 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java@@ -781,7 +781,10 @@ public class CollectionsHandler extends RequestHandlerBase implements Permissionreq.getParams().getAll(params, COLL_CONF, REPLICATION_FACTOR, MAX_SHARDS_PER_NODE, STATE_FORMAT, AUTO_ADD_REPLICAS);copyPropertiesWithPrefix(req.getParams(), params, COLL_PROP_PREFIX);return params;- });+ }),++ REPLACENODE_OP(REPLACENODE, (req, rsp, h) -> req.getParams().required().getAll(req.getParams().getAll(null, "parallel"), "source", "target")),+ DELETENODE_OP(DELETENODE, (req, rsp, h) -> req.getParams().required().getAll(null, "node"));public final CollectionOp fun;CollectionAction action;long timeOut;diff --git a/solr/core/src/test/org/apache/solr/cloud/DeleteNodeTest.java b/solr/core/src/test/org/apache/solr/cloud/DeleteNodeTest.javanew file mode 100644index 00000000000..8d2f6f25a78--- /dev/null+++ b/solr/core/src/test/org/apache/solr/cloud/DeleteNodeTest.java@@ -0,0 +1,75 @@+/*+ * 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 java.lang.invoke.MethodHandles;+import java.util.ArrayList;+import java.util.Collections;+import java.util.Set;++import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.request.CollectionAdminRequest;+import org.apache.solr.client.solrj.response.RequestStatusState;+import org.apache.solr.common.util.StrUtils;+import org.junit.BeforeClass;+import org.junit.Test;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class DeleteNodeTest extends SolrCloudTestCase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ @BeforeClass+ public static void setupCluster() throws Exception {+ configureCluster(6)+ .addConfig("conf1", TEST_PATH().resolve("configsets").resolve("cloud-dynamic").resolve("conf"))+ .configure();+ }++ protected String getSolrXml() {+ return "solr.xml";+ }++ @Test+ public void test() throws Exception {+ cluster.waitForAllNodes(5000);+ CloudSolrClient cloudClient = cluster.getSolrClient();+ String coll = "deletenodetest_coll";+ Set<String> liveNodes = cloudClient.getZkStateReader().getClusterState().getLiveNodes();+ ArrayList<String> l = new ArrayList<>(liveNodes);+ Collections.shuffle(l, random());+ CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(coll, "conf1", 5, 2);+ create.setCreateNodeSet(StrUtils.join(l, ',')).setMaxShardsPerNode(3);+ cloudClient.request(create);+ String node2bdecommissioned = l.get(0);+ new CollectionAdminRequest.DeleteNode(node2bdecommissioned).processAsync("003", cloudClient);+ CollectionAdminRequest.RequestStatus requestStatus = CollectionAdminRequest.requestStatus("003");+ boolean success = false;+ for (int i = 0; i < 200; i++) {+ CollectionAdminRequest.RequestStatusResponse rsp = requestStatus.process(cloudClient);+ if (rsp.getRequestStatus() == RequestStatusState.COMPLETED) {+ success = true;+ break;+ }+ assertFalse(rsp.getRequestStatus() == RequestStatusState.FAILED);+ Thread.sleep(50);+ }+ assertTrue(success);+ }+}diff --git a/solr/core/src/test/org/apache/solr/cloud/ReplaceNodeTest.java b/solr/core/src/test/org/apache/solr/cloud/ReplaceNodeTest.javanew file mode 100644index 00000000000..1c7575d5225--- /dev/null+++ b/solr/core/src/test/org/apache/solr/cloud/ReplaceNodeTest.java@@ -0,0 +1,104 @@+/*+ * 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 java.lang.invoke.MethodHandles;+import java.util.ArrayList;+import java.util.Collections;+import java.util.Set;++import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.impl.HttpSolrClient;+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.client.solrj.response.RequestStatusState;+import org.apache.solr.common.util.StrUtils;+import org.junit.BeforeClass;+import org.junit.Test;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class ReplaceNodeTest extends SolrCloudTestCase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());+ @BeforeClass+ public static void setupCluster() throws Exception {+ configureCluster(6)+ .addConfig("conf1", TEST_PATH().resolve("configsets").resolve("cloud-dynamic").resolve("conf"))+ .configure();+ }++ protected String getSolrXml() {+ return "solr.xml";+ }++ @Test+ public void test() throws Exception {+ cluster.waitForAllNodes(5000);+ String coll = "replacenodetest_coll";+ log.info("total_jettys: " + cluster.getJettySolrRunners().size());++ CloudSolrClient cloudClient = cluster.getSolrClient();+ Set<String> liveNodes = cloudClient.getZkStateReader().getClusterState().getLiveNodes();+ ArrayList<String> l = new ArrayList<>(liveNodes);+ Collections.shuffle(l, random());+ String emptyNode = l.remove(0);+ String node2bdecommissioned = l.get(0);+ CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(coll, "conf1", 5, 2);+ create.setCreateNodeSet(StrUtils.join(l, ',')).setMaxShardsPerNode(3);+ cloudClient.request(create);+ log.info("excluded_node : {} ", emptyNode);+ new CollectionAdminRequest.ReplaceNode(node2bdecommissioned, emptyNode).processAsync("000", cloudClient);+ CollectionAdminRequest.RequestStatus requestStatus = CollectionAdminRequest.requestStatus("000");+ boolean success = false;+ for (int i = 0; i < 200; i++) {+ CollectionAdminRequest.RequestStatusResponse rsp = requestStatus.process(cloudClient);+ if (rsp.getRequestStatus() == RequestStatusState.COMPLETED) {+ success = true;+ break;+ }+ assertFalse(rsp.getRequestStatus() == RequestStatusState.FAILED);+ Thread.sleep(50);+ }+ assertTrue(success);+ try (HttpSolrClient coreclient = getHttpSolrClient(cloudClient.getZkStateReader().getBaseUrlForNodeName(node2bdecommissioned))) {+ CoreAdminResponse status = CoreAdminRequest.getStatus(null, coreclient);+ assertTrue(status.getCoreStatus().size() == 0);+ }++ //let's do it back+ new CollectionAdminRequest.ReplaceNode(emptyNode, node2bdecommissioned).setParallel(Boolean.TRUE).processAsync("001", cloudClient);+ requestStatus = CollectionAdminRequest.requestStatus("001");++ for (int i = 0; i < 200; i++) {+ CollectionAdminRequest.RequestStatusResponse rsp = requestStatus.process(cloudClient);+ if (rsp.getRequestStatus() == RequestStatusState.COMPLETED) {+ success = true;+ break;+ }+ assertFalse(rsp.getRequestStatus() == RequestStatusState.FAILED);+ Thread.sleep(50);+ }+ assertTrue(success);+ try (HttpSolrClient coreclient = getHttpSolrClient(cloudClient.getZkStateReader().getBaseUrlForNodeName(emptyNode))) {+ CoreAdminResponse status = CoreAdminRequest.getStatus(null, coreclient);+ assertTrue(status.getCoreStatus().size() == 0);+ }+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionAdminRequest.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionAdminRequest.javaindex 7bc9e4fd931..0a0a191ab1d 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionAdminRequest.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionAdminRequest.java@@ -112,7 +112,7 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>* @deprecated Use {@link #processAsync(String, SolrClient)} or {@link #processAsync(SolrClient)}*/@Deprecated- public abstract AsyncCollectionAdminRequest setAsyncId(String id);+ public AsyncCollectionAdminRequest setAsyncId(String id){return this;};/*** Process this request asynchronously, generating and returning a request id@@ -491,6 +491,56 @@ public abstract class CollectionAdminRequest<T extends CollectionAdminResponse>}}+ public static class DeleteNode extends AsyncCollectionAdminRequest {+ String node;++ /**+ * @param node The node to be deleted+ */+ public DeleteNode(String node) {+ super(CollectionAction.DELETENODE);+ this.node = node;+ }+ @Override+ public SolrParams getParams() {+ ModifiableSolrParams params = (ModifiableSolrParams) super.getParams();+ params.set("node", node);+ return params;+ }+++ }++ public static class ReplaceNode extends AsyncCollectionAdminRequest {+ String source, target;+ Boolean parallel;++ /**+ * @param source node to be cleaned up+ * @param target node where the new replicas are to be created+ */+ public ReplaceNode(String source, String target) {+ super(CollectionAction.REPLACENODE);+ this.source = source;+ this.target = target;+ }++ public ReplaceNode setParallel(Boolean flag) {+ this.parallel = flag;+ return this;+ }++ @Override+ public SolrParams getParams() {+ ModifiableSolrParams params = (ModifiableSolrParams) super.getParams();+ params.set("source", source);+ params.set("target", target);+ if (parallel != null) params.set("parallel", parallel.toString());+ return params;+ }++ }+/** Returns a RebalanceLeaders object to rebalance leaders for a collection*/diff --git a/solr/solrj/src/java/org/apache/solr/common/cloud/ZkNodeProps.java b/solr/solrj/src/java/org/apache/solr/common/cloud/ZkNodeProps.javaindex 320ad132583..94a673e9941 100644--- a/solr/solrj/src/java/org/apache/solr/common/cloud/ZkNodeProps.java+++ b/solr/solrj/src/java/org/apache/solr/common/cloud/ZkNodeProps.java@@ -20,6 +20,7 @@ import org.apache.solr.common.util.Utils;import org.noggit.JSONUtil;import org.noggit.JSONWriter;+import java.util.Collections;import java.util.LinkedHashMap;import java.util.Map;import java.util.Set;@@ -40,6 +41,17 @@ public class ZkNodeProps implements JSONWriter.Writable {// Always wrapping introduces a memory leak.}+ public ZkNodeProps plus(String key , Object val) {+ return plus(Collections.singletonMap(key,val));+ }++ public ZkNodeProps plus(Map<String, Object> newVals) {+ LinkedHashMap<String, Object> copy = new LinkedHashMap<>(propMap);+ if (newVals == null || newVals.isEmpty()) return new ZkNodeProps(copy);+ copy.putAll(newVals);+ return new ZkNodeProps(copy);+ }+/*** Constructor that populates the from array of Strings in form key1, value1,diff --git a/solr/solrj/src/java/org/apache/solr/common/params/CollectionParams.java b/solr/solrj/src/java/org/apache/solr/common/params/CollectionParams.javaindex e38ab4fbb11..f10f0895b2e 100644--- a/solr/solrj/src/java/org/apache/solr/common/params/CollectionParams.java+++ b/solr/solrj/src/java/org/apache/solr/common/params/CollectionParams.java@@ -96,6 +96,9 @@ public interface CollectionParams {// but the overseer is aware of these tasksMOCK_COLL_TASK(false, LockLevel.COLLECTION),MOCK_SHARD_TASK(false, LockLevel.SHARD),+ //TODO when we have a node level lock use it here+ REPLACENODE(true, LockLevel.NONE),+ DELETENODE(true, LockLevel.NONE),MOCK_REPLICA_TASK(false, LockLevel.REPLICA);public final boolean isWrite;