if (!svcComponents.containsKey(masterCompType)) {
throw new AmbariException("Specified component " + masterCompType +
" does not belong to service " + serviceName + ".");
}
ServiceComponent masterComponent = svcComponents.get(masterCompType);
if (!masterComponent.isMasterComponent()) {
throw new AmbariException("Specified component " + masterCompType +
" is not a MASTER for service " + serviceName + ".");
}
if (!masterToSlaveMappingForDecom.containsKey(masterCompType)) {
throw new AmbariException("Decommissioning is not supported for " + masterCompType);
}
// Find the slave component
if (slaveCompType == null || slaveCompType.equals("")) {
slaveCompType = masterToSlaveMappingForDecom.get(masterCompType);
} else if (!masterToSlaveMappingForDecom.get(masterCompType).equals(slaveCompType)) {
throw new AmbariException("Component " + slaveCompType + " is not supported for decommissioning.");
}
String isDrainOnlyRequest = actionExecutionContext.getParameters().get(HBASE_MARK_DRAINING_ONLY);
if (isDrainOnlyRequest != null && !slaveCompType.equals(Role.HBASE_REGIONSERVER.name())) {
throw new AmbariException(HBASE_MARK_DRAINING_ONLY + " is not a valid parameter for " + masterCompType);
}
// Decommission only if the sch is in state STARTED or INSTALLED
for (ServiceComponentHost sch : svcComponents.get(slaveCompType).getServiceComponentHosts().values()) {
if (excludedHosts.contains(sch.getHostName())
&& !"true".equals(isDrainOnlyRequest)
&& sch.getState() != State.STARTED) {
throw new AmbariException("Component " + slaveCompType + " on host " + sch.getHostName() + " cannot be " +
"decommissioned as its not in STARTED state. Aborting the whole request.");
}
}
String alignMtnStateStr = actionExecutionContext.getParameters().get(ALIGN_MAINTENANCE_STATE);
boolean alignMtnState = "true".equals(alignMtnStateStr);
// Set/reset decommissioned flag on all components
List<String> listOfExcludedHosts = new ArrayList<String>();
for (ServiceComponentHost sch : svcComponents.get(slaveCompType).getServiceComponentHosts().values()) {
if (excludedHosts.contains(sch.getHostName())) {
sch.setComponentAdminState(HostComponentAdminState.DECOMMISSIONED);
listOfExcludedHosts.add(sch.getHostName());
if (alignMtnState) {
sch.setMaintenanceState(MaintenanceState.ON);
}
LOG.info("Decommissioning " + slaveCompType + " and marking Maintenance=ON on " + sch.getHostName());
}
if (includedHosts.contains(sch.getHostName())) {
sch.setComponentAdminState(HostComponentAdminState.INSERVICE);
if (alignMtnState) {
sch.setMaintenanceState(MaintenanceState.OFF);
}
LOG.info("Recommissioning " + slaveCompType + " and marking Maintenance=OFF on " + sch.getHostName());
}
}
// In the event there are more than one master host the following logic is applied
// -- HDFS/DN, MR1/TT, YARN/NM call refresh node on both
// -- HBASE/RS call only on one host
// Ensure host is active
Map<String, ServiceComponentHost> masterSchs = masterComponent.getServiceComponentHosts();
String primaryCandidate = null;
for (String hostName : masterSchs.keySet()) {
if (primaryCandidate == null) {
primaryCandidate = hostName;
} else {
ServiceComponentHost sch = masterSchs.get(hostName);
if (sch.getState() == State.STARTED) {
primaryCandidate = hostName;
}
}
}
StringBuilder commandDetail = getReadableDecommissionCommandDetail
(actionExecutionContext, includedHosts, listOfExcludedHosts);
for (String hostName : masterSchs.keySet()) {
RequestResourceFilter commandFilter = new RequestResourceFilter(serviceName,
masterComponent.getName(), Collections.singletonList(hostName));
List<RequestResourceFilter> resourceFilters = new ArrayList<RequestResourceFilter>();
resourceFilters.add(commandFilter);
ActionExecutionContext commandContext = new ActionExecutionContext(
clusterName, actionExecutionContext.getActionName(), resourceFilters