Skip to content

Commit

Permalink
Update INIT and exclusion in one call
Browse files Browse the repository at this point in the history
Signed-off-by: Rishab Nahata <[email protected]>
  • Loading branch information
imRishN committed Nov 5, 2022
1 parent 236940f commit b634138
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
import java.util.stream.StreamSupport;

import static org.opensearch.cluster.coordination.NoClusterManagerBlockService.NO_CLUSTER_MANAGER_BLOCK_ID;
import static org.opensearch.cluster.decommission.DecommissionService.nodeCommissioned;
import static org.opensearch.cluster.decommission.DecommissionHelper.nodeCommissioned;
import static org.opensearch.gateway.ClusterStateUpdaters.hideStateIfNotRecovered;
import static org.opensearch.gateway.GatewayService.STATE_NOT_RECOVERED_BLOCK;
import static org.opensearch.monitor.StatusInfo.Status.UNHEALTHY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import static org.opensearch.cluster.decommission.DecommissionService.nodeCommissioned;
import static org.opensearch.cluster.decommission.DecommissionHelper.nodeCommissioned;
import static org.opensearch.gateway.GatewayService.STATE_NOT_RECOVERED_BLOCK;

/**
Expand Down
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;
}
}
Loading

0 comments on commit b634138

Please sign in to comment.