}
}
public void decommisionAmbariNodes(Long stackId, Set<HostGroupAdjustmentJson> hosts) {
Stack stack = stackRepository.findOneWithLists(stackId);
Cluster cluster = stack.getCluster();
MDCBuilder.buildMdcContext(cluster);
LOGGER.info("Decommision requested");
try {
stackUpdater.updateStackStatus(stack.getId(), Status.UPDATE_IN_PROGRESS);
AmbariClient ambariClient = createAmbariClient(stack.getAmbariIp());
Set<HostMetadata> metadataToRemove = new HashSet<>();
for (HostGroupAdjustmentJson hostGroupAdjustment : hosts) {
LOGGER.info("Decommisioning {} hosts", -1 * hostGroupAdjustment.getScalingAdjustment());
Set<HostMetadata> hostsInHostGroup = hostMetadataRepository.findHostsInHostgroup(hostGroupAdjustment.getHostGroup(), cluster.getId());
int i = 0;
for (HostMetadata hostMetadata : hostsInHostGroup) {
String hostName = hostMetadata.getHostName();
InstanceMetaData instanceMetaData = instanceMetadataRepository.findHostInStack(stack.getId(), hostName);
if (!instanceMetaData.getAmbariServer()) {
if (i < -1 * hostGroupAdjustment.getScalingAdjustment()) {
LOGGER.info("Host '{}' will be removed from Ambari cluster", hostName);
metadataToRemove.add(hostMetadata);
Set<String> components = ambariClient.getHostComponentsMap(hostName).keySet();
Map<String, Integer> installRequests = new HashMap<>();
if (components.contains("NODEMANAGER")) {
Integer requestId = ambariClient.decommissionNodeManager(hostName);
installRequests.put("NODEMANAGER_DECOMMISION", requestId);
}
if (components.contains("DATANODE")) {
Integer requestId = ambariClient.decommissionDataNode(hostName);
installRequests.put("DATANODE_DECOMMISION", requestId);
}
List<String> componentsList = new ArrayList<>();
componentsList.addAll(components);
Map<String, Integer> stopRequests = ambariClient.stopComponentsOnHost(hostName, componentsList);
installRequests.putAll(stopRequests);
waitForAmbariOperations(stack, ambariClient, installRequests);
ambariClient.deleteHostComponents(hostName, componentsList);
ambariClient.deleteHost(hostName);
installRequests = new HashMap<>();
Integer zookeeperRequestId = ambariClient.restartServiceComponents("ZOOKEEPER", Arrays.asList("ZOOKEEPER_SERVER"));
installRequests.put("ZOOKEEPER", zookeeperRequestId);
if (ambariClient.getServiceComponentsMap().containsKey("NAGIOS")) {
Integer nagiosRequestId = ambariClient.restartServiceComponents("NAGIOS", Arrays.asList("NAGIOS_SERVER"));
installRequests.put("NAGIOS", nagiosRequestId);
}
waitForAmbariOperations(stack, ambariClient, installRequests);
} else {
break;
}
i++;
}
}
}
cluster = clusterRepository.findOneWithLists(stack.getCluster().getId());
cluster.getHostMetadata().removeAll(metadataToRemove);
clusterRepository.save(cluster);
Set<String> hostsRemoved = new HashSet<>();
for (HostMetadata hostMetadata : metadataToRemove) {
hostsRemoved.add(hostMetadata.getHostName());
}