-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update INIT and exclusion in one call
Signed-off-by: Rishab Nahata <[email protected]>
- Loading branch information
Showing
4 changed files
with
214 additions
and
199 deletions.
There are no files selected for viewing
This file contains 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 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
108 changes: 108 additions & 0 deletions
108
server/src/main/java/org/opensearch/cluster/decommission/DecommissionHelper.java
This file contains 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,108 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.cluster.decommission; | ||
|
||
import org.opensearch.action.admin.cluster.configuration.AddVotingConfigExclusionsRequest; | ||
import org.opensearch.cluster.ClusterState; | ||
import org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion; | ||
import org.opensearch.cluster.metadata.Metadata; | ||
import org.opensearch.cluster.node.DiscoveryNode; | ||
import org.opensearch.common.Strings; | ||
import org.opensearch.common.unit.TimeValue; | ||
|
||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
import static org.opensearch.action.admin.cluster.configuration.AddVotingConfigExclusionsHelper.resolveVotingConfigExclusionsAndCheckMaximum; | ||
import static org.opensearch.action.admin.cluster.configuration.AddVotingConfigExclusionsHelper.updateExclusionAndGetState; | ||
|
||
/** | ||
* Static helper utilities to execute decommission | ||
*/ | ||
public class DecommissionHelper { | ||
|
||
static ClusterState registerDecommissionAttributeInClusterState(ClusterState currentState, DecommissionAttribute decommissionAttribute) { | ||
DecommissionAttributeMetadata decommissionAttributeMetadata = new DecommissionAttributeMetadata(decommissionAttribute); | ||
return ClusterState.builder(currentState) | ||
.metadata(Metadata.builder(currentState.metadata()).decommissionAttributeMetadata(decommissionAttributeMetadata)) | ||
.build(); | ||
} | ||
|
||
static ClusterState addVotingConfigExclusionsForToBeDecommissionedClusterManagerNodes( | ||
ClusterState currentState, | ||
Set<String> nodeIdsToBeExcluded, | ||
TimeValue decommissionActionTimeout | ||
) { | ||
AddVotingConfigExclusionsRequest request = new AddVotingConfigExclusionsRequest( | ||
Strings.EMPTY_ARRAY, | ||
nodeIdsToBeExcluded.toArray(String[]::new), | ||
Strings.EMPTY_ARRAY, | ||
decommissionActionTimeout | ||
); | ||
// TODO - update max count | ||
Set<VotingConfigExclusion> resolvedExclusion = resolveVotingConfigExclusionsAndCheckMaximum(request, currentState, 10); | ||
return updateExclusionAndGetState(currentState, resolvedExclusion, 10); | ||
} | ||
|
||
static Set<DiscoveryNode> filterNodesWithDecommissionAttribute( | ||
ClusterState clusterState, | ||
DecommissionAttribute decommissionAttribute, | ||
boolean onlyClusterManagerNodes | ||
) { | ||
Set<DiscoveryNode> nodesWithDecommissionAttribute = new HashSet<>(); | ||
Iterator<DiscoveryNode> nodesIter = onlyClusterManagerNodes | ||
? clusterState.nodes().getClusterManagerNodes().valuesIt() | ||
: clusterState.nodes().getNodes().valuesIt(); | ||
|
||
while (nodesIter.hasNext()) { | ||
final DiscoveryNode node = nodesIter.next(); | ||
if (nodeHasDecommissionedAttribute(node, decommissionAttribute)) { | ||
nodesWithDecommissionAttribute.add(node); | ||
} | ||
} | ||
return nodesWithDecommissionAttribute; | ||
} | ||
|
||
/** | ||
* Utility method to check if the node has decommissioned attribute | ||
* | ||
* @param discoveryNode node to check on | ||
* @param decommissionAttribute attribute to be checked with | ||
* @return true or false based on whether node has decommissioned attribute | ||
*/ | ||
public static boolean nodeHasDecommissionedAttribute(DiscoveryNode discoveryNode, DecommissionAttribute decommissionAttribute) { | ||
String nodeAttributeValue = discoveryNode.getAttributes().get(decommissionAttribute.attributeName()); | ||
return nodeAttributeValue != null && nodeAttributeValue.equals(decommissionAttribute.attributeValue()); | ||
} | ||
|
||
/** | ||
* Utility method to check if the node is commissioned or not | ||
* | ||
* @param discoveryNode node to check on | ||
* @param metadata metadata present current which will be used to check the commissioning status of the node | ||
* @return if the node is commissioned or not | ||
*/ | ||
public static boolean nodeCommissioned(DiscoveryNode discoveryNode, Metadata metadata) { | ||
DecommissionAttributeMetadata decommissionAttributeMetadata = metadata.decommissionAttributeMetadata(); | ||
if (decommissionAttributeMetadata != null) { | ||
DecommissionAttribute decommissionAttribute = decommissionAttributeMetadata.decommissionAttribute(); | ||
DecommissionStatus status = decommissionAttributeMetadata.status(); | ||
if (decommissionAttribute != null && status != null) { | ||
if (nodeHasDecommissionedAttribute(discoveryNode, decommissionAttribute) | ||
&& (status.equals(DecommissionStatus.IN_PROGRESS) | ||
|| status.equals(DecommissionStatus.SUCCESSFUL) | ||
|| status.equals(DecommissionStatus.DRAINING))) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} |
Oops, something went wrong.