-
Notifications
You must be signed in to change notification settings - Fork 830
SOLR-16855: Add a MigrateReplicas API #1730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1c36757
Add migrate replicas command and API
HoustonPutman 3e65228
Add docs for migrateReplicas, cleanup related docs
HoustonPutman f548b91
Add changelog entry
HoustonPutman e0e50f4
Add first tests
HoustonPutman 5742d45
Add integration test
HoustonPutman dc26b3d
tidy
HoustonPutman 4eea768
Fix orderedPlacement computation for bad edge case
HoustonPutman 219d575
Merge remote-tracking branch 'apache' into migrate-replicas
HoustonPutman e83b96c
fixes
HoustonPutman b965927
Tidy
HoustonPutman 60458d2
Make tie-logic full-featured
HoustonPutman 577f566
Small fixes
HoustonPutman 7203d4b
Refactor shared code into method.
HoustonPutman dd1b36e
Use dequeue instead of linkedList
HoustonPutman c556115
Fix scenario when the placementRequest doesn't actually request any r…
HoustonPutman 2d849f0
Merge remote-tracking branch 'apache' into migrate-replicas
HoustonPutman b18e749
Add docs for the new internal classes
HoustonPutman 607352e
Merge remote-tracking branch 'apache' into migrate-replicas
HoustonPutman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
solr/core/src/java/org/apache/solr/cloud/api/collections/MigrateReplicasCmd.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| } | ||
| } | ||
| for (String targetNode : targetNodes) { | ||
| if (!clusterState.liveNodesContain(targetNode)) { | ||
| throw new SolrException( | ||
| SolrException.ErrorCode.BAD_REQUEST, "Target Node: " + targetNode + " is not live"); | ||
| } | ||
| } | ||
|
|
||
| if (targetNodes.isEmpty()) { | ||
| // If no target nodes are provided, use all other live nodes that are not the sourceNodes | ||
| targetNodes = | ||
| clusterState.getLiveNodes().stream() | ||
| .filter(n -> !sourceNodes.contains(n)) | ||
| .collect(Collectors.toSet()); | ||
| if (targetNodes.isEmpty()) { | ||
| throw new SolrException( | ||
| SolrException.ErrorCode.BAD_REQUEST, | ||
| "No nodes other than the source nodes are live, therefore replicas cannot be migrated"); | ||
| } | ||
| } | ||
| List<Replica> sourceReplicas = | ||
| ReplicaMigrationUtils.getReplicasOfNodes(sourceNodes, clusterState); | ||
| Map<Replica, String> replicaMovements = CollectionUtil.newHashMap(sourceReplicas.size()); | ||
|
|
||
| if (targetNodes.size() > 1) { | ||
| List<Assign.AssignRequest> assignRequests = new ArrayList<>(sourceReplicas.size()); | ||
| List<String> targetNodeList = new ArrayList<>(targetNodes); | ||
| for (Replica sourceReplica : sourceReplicas) { | ||
| Replica.Type replicaType = sourceReplica.getType(); | ||
| Assign.AssignRequest assignRequest = | ||
| new Assign.AssignRequestBuilder() | ||
| .forCollection(sourceReplica.getCollection()) | ||
| .forShard(Collections.singletonList(sourceReplica.getShard())) | ||
| .assignNrtReplicas(replicaType == Replica.Type.NRT ? 1 : 0) | ||
| .assignTlogReplicas(replicaType == Replica.Type.TLOG ? 1 : 0) | ||
| .assignPullReplicas(replicaType == Replica.Type.PULL ? 1 : 0) | ||
| .onNodes(targetNodeList) | ||
| .build(); | ||
| assignRequests.add(assignRequest); | ||
| } | ||
| Assign.AssignStrategy assignStrategy = Assign.createAssignStrategy(ccc.getCoreContainer()); | ||
| List<ReplicaPosition> replicaPositions = | ||
| assignStrategy.assign(ccc.getSolrCloudManager(), assignRequests); | ||
| int position = 0; | ||
| for (Replica sourceReplica : sourceReplicas) { | ||
| replicaMovements.put(sourceReplica, replicaPositions.get(position++).node); | ||
| } | ||
| } else { | ||
| String targetNode = targetNodes.stream().findFirst().get(); | ||
| for (Replica sourceReplica : sourceReplicas) { | ||
| replicaMovements.put(sourceReplica, targetNode); | ||
| } | ||
| } | ||
|
|
||
| boolean migrationSuccessful = | ||
| ReplicaMigrationUtils.migrateReplicas( | ||
| ccc, replicaMovements, parallel, waitForFinalState, timeout, async, results); | ||
| if (migrationSuccessful) { | ||
| results.add( | ||
| "success", | ||
| "MIGRATE_REPLICAS action completed successfully from : [" | ||
| + String.join(",", sourceNodes) | ||
| + "] to : [" | ||
| + String.join(",", targetNodes) | ||
| + "]"); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings({"unchecked"}) | ||
| protected Set<String> getNodesFromParam(ZkNodeProps message, String paramName) { | ||
| Object rawParam = message.get(paramName); | ||
| if (rawParam == null) { | ||
| return Collections.emptySet(); | ||
| } else if (rawParam instanceof Set) { | ||
| return (Set<String>) rawParam; | ||
| } else if (rawParam instanceof Collection) { | ||
| return new HashSet<>((Collection<String>) rawParam); | ||
| } else if (rawParam instanceof String) { | ||
| return Set.of(((String) rawParam).split(",")); | ||
| } else { | ||
| throw new SolrException( | ||
| SolrException.ErrorCode.BAD_REQUEST, | ||
| "'" | ||
| + paramName | ||
| + "' was not passed as a correct type (Set/List/String): " | ||
| + rawParam.getClass().getName()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be allowed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently the ReplaceNode/BalanceReplicas logic does not allow for moving replicas off of non-live nodes. I think that's something that we should think about for a separate ticket. Especially now that cores are not deleted on startup if they don't exist in Zookeeper. #1321
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how this will work currently. My thinking is, nodes that are removed, you want to "vacate" them and have the replicas be created somewhere else, so that:
Would this API be the one to use in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I do think this is the correct API for that, but it (REPLACENODE) is not currently able to do that. AFAIK.
I think we should make this a param as a part of MigrateReplicas and ReplaceNode. (
migrateFromNonLiveNodesor something like that). That would somewhat necessitatesolr.deleteUnknownCores, from the above linked PR, to be set totrue, so that when the node is started back up, it deletes the data directories from those nodes.Again, I think this should be done in a separate JIRA/PR.