@Test void collectHostsToRemoveShouldCollectDeletedOnProviderSideNodes() throws Exception { HostGroup hostGroup = new HostGroup(); InstanceGroup instanceGroup = new InstanceGroup(); InstanceMetaData deletedInstanceMetaData = createInstanceMetadata(InstanceStatus.DELETED_ON_PROVIDER_SIDE, DELETED_INSTANCE_FQD); InstanceMetaData runningInstanceMetaData = createInstanceMetadata(InstanceStatus.SERVICES_HEALTHY, RUNNING_INSTANCE_FQDN); instanceGroup.setInstanceMetaData(Set.of(deletedInstanceMetaData, runningInstanceMetaData)); hostGroup.setInstanceGroup(instanceGroup); ApiHostRefList apiHostRefList = new ApiHostRefList(); apiHostRefList.setItems(List.of(createApiHostRef(DELETED_INSTANCE_FQD), createApiHostRef(RUNNING_INSTANCE_FQDN))); when(clustersResourceApi.listHosts(STACK_NAME, null, null)).thenReturn(apiHostRefList); when(clouderaManagerApiFactory.getClustersResourceApi(client)).thenReturn(clustersResourceApi); Set<String> hostNames = Set.of(DELETED_INSTANCE_FQD, RUNNING_INSTANCE_FQDN); Map<String, InstanceMetaData> result = underTest.collectHostsToRemove(getStack(), hostGroup, hostNames, client); assertThat(result) .containsValue(runningInstanceMetaData) .containsValue(deletedInstanceMetaData); }
public Map<String, InstanceMetaData> collectHostsToRemove(Stack stack, HostGroup hostGroup, Set<String> hostNames, ApiClient client) { Set<InstanceMetaData> hostsInHostGroup = hostGroup.getInstanceGroup().getNotTerminatedInstanceMetaDataSet(); Map<String, InstanceMetaData> hostsToRemove = hostsInHostGroup.stream() .filter(hostMetadata -> hostNames.contains(hostMetadata.getDiscoveryFQDN())) .collect(Collectors.toMap(InstanceMetaData::getDiscoveryFQDN, hostMetadata -> hostMetadata)); if (hostsToRemove.size() != hostNames.size()) { LOGGER.debug("Not all hosts found in the given host group. [{}, {}]", hostGroup.getName(), hostNames); } ClustersResourceApi clustersResourceApi = clouderaManagerApiFactory.getClustersResourceApi(client); try { ApiHostRefList hostRefList = clustersResourceApi.listHosts(stack.getName(), null, null); List<String> runningHosts = hostRefList.getItems().stream() .map(ApiHostRef::getHostname) .collect(Collectors.toList()); Sets.newHashSet(hostsToRemove.keySet()).stream() .filter(hostName -> !runningHosts.contains(hostName)) .forEach(hostsToRemove::remove); LOGGER.debug("Collected hosts to remove: [{}]", hostsToRemove); return hostsToRemove; } catch (ApiException e) { LOGGER.error("Failed to get host list for cluster: {}", stack.getName(), e); throw new CloudbreakServiceException(e.getMessage(), e); } }
ClouderaManagerDecomissioner { public Map<String, InstanceMetaData> collectHostsToRemove(Stack stack, HostGroup hostGroup, Set<String> hostNames, ApiClient client) { Set<InstanceMetaData> hostsInHostGroup = hostGroup.getInstanceGroup().getNotTerminatedInstanceMetaDataSet(); Map<String, InstanceMetaData> hostsToRemove = hostsInHostGroup.stream() .filter(hostMetadata -> hostNames.contains(hostMetadata.getDiscoveryFQDN())) .collect(Collectors.toMap(InstanceMetaData::getDiscoveryFQDN, hostMetadata -> hostMetadata)); if (hostsToRemove.size() != hostNames.size()) { LOGGER.debug("Not all hosts found in the given host group. [{}, {}]", hostGroup.getName(), hostNames); } ClustersResourceApi clustersResourceApi = clouderaManagerApiFactory.getClustersResourceApi(client); try { ApiHostRefList hostRefList = clustersResourceApi.listHosts(stack.getName(), null, null); List<String> runningHosts = hostRefList.getItems().stream() .map(ApiHostRef::getHostname) .collect(Collectors.toList()); Sets.newHashSet(hostsToRemove.keySet()).stream() .filter(hostName -> !runningHosts.contains(hostName)) .forEach(hostsToRemove::remove); LOGGER.debug("Collected hosts to remove: [{}]", hostsToRemove); return hostsToRemove; } catch (ApiException e) { LOGGER.error("Failed to get host list for cluster: {}", stack.getName(), e); throw new CloudbreakServiceException(e.getMessage(), e); } } }
ClouderaManagerDecomissioner { public Map<String, InstanceMetaData> collectHostsToRemove(Stack stack, HostGroup hostGroup, Set<String> hostNames, ApiClient client) { Set<InstanceMetaData> hostsInHostGroup = hostGroup.getInstanceGroup().getNotTerminatedInstanceMetaDataSet(); Map<String, InstanceMetaData> hostsToRemove = hostsInHostGroup.stream() .filter(hostMetadata -> hostNames.contains(hostMetadata.getDiscoveryFQDN())) .collect(Collectors.toMap(InstanceMetaData::getDiscoveryFQDN, hostMetadata -> hostMetadata)); if (hostsToRemove.size() != hostNames.size()) { LOGGER.debug("Not all hosts found in the given host group. [{}, {}]", hostGroup.getName(), hostNames); } ClustersResourceApi clustersResourceApi = clouderaManagerApiFactory.getClustersResourceApi(client); try { ApiHostRefList hostRefList = clustersResourceApi.listHosts(stack.getName(), null, null); List<String> runningHosts = hostRefList.getItems().stream() .map(ApiHostRef::getHostname) .collect(Collectors.toList()); Sets.newHashSet(hostsToRemove.keySet()).stream() .filter(hostName -> !runningHosts.contains(hostName)) .forEach(hostsToRemove::remove); LOGGER.debug("Collected hosts to remove: [{}]", hostsToRemove); return hostsToRemove; } catch (ApiException e) { LOGGER.error("Failed to get host list for cluster: {}", stack.getName(), e); throw new CloudbreakServiceException(e.getMessage(), e); } }  }
ClouderaManagerDecomissioner { public Map<String, InstanceMetaData> collectHostsToRemove(Stack stack, HostGroup hostGroup, Set<String> hostNames, ApiClient client) { Set<InstanceMetaData> hostsInHostGroup = hostGroup.getInstanceGroup().getNotTerminatedInstanceMetaDataSet(); Map<String, InstanceMetaData> hostsToRemove = hostsInHostGroup.stream() .filter(hostMetadata -> hostNames.contains(hostMetadata.getDiscoveryFQDN())) .collect(Collectors.toMap(InstanceMetaData::getDiscoveryFQDN, hostMetadata -> hostMetadata)); if (hostsToRemove.size() != hostNames.size()) { LOGGER.debug("Not all hosts found in the given host group. [{}, {}]", hostGroup.getName(), hostNames); } ClustersResourceApi clustersResourceApi = clouderaManagerApiFactory.getClustersResourceApi(client); try { ApiHostRefList hostRefList = clustersResourceApi.listHosts(stack.getName(), null, null); List<String> runningHosts = hostRefList.getItems().stream() .map(ApiHostRef::getHostname) .collect(Collectors.toList()); Sets.newHashSet(hostsToRemove.keySet()).stream() .filter(hostName -> !runningHosts.contains(hostName)) .forEach(hostsToRemove::remove); LOGGER.debug("Collected hosts to remove: [{}]", hostsToRemove); return hostsToRemove; } catch (ApiException e) { LOGGER.error("Failed to get host list for cluster: {}", stack.getName(), e); throw new CloudbreakServiceException(e.getMessage(), e); } }  void verifyNodesAreRemovable(Stack stack, Collection<InstanceMetaData> removableInstances, ApiClient client); Set<InstanceMetaData> collectDownscaleCandidates(ApiClient client, Stack stack, HostGroup hostGroup, Integer scalingAdjustment,
            int defaultRootVolumeSize, Set<InstanceMetaData> instanceMetaDatasInStack); Map<String, InstanceMetaData> collectHostsToRemove(Stack stack, HostGroup hostGroup, Set<String> hostNames, ApiClient client); Set<String> decommissionNodes(Stack stack, Map<String, InstanceMetaData> hostsToRemove, ApiClient client); void deleteHost(Stack stack, InstanceMetaData data, ApiClient client); void deleteUnusedCredentialsFromCluster(Stack stack, ApiClient client); void stopAndRemoveMgmtService(Stack stack, ApiClient client); }
ClouderaManagerDecomissioner { public Map<String, InstanceMetaData> collectHostsToRemove(Stack stack, HostGroup hostGroup, Set<String> hostNames, ApiClient client) { Set<InstanceMetaData> hostsInHostGroup = hostGroup.getInstanceGroup().getNotTerminatedInstanceMetaDataSet(); Map<String, InstanceMetaData> hostsToRemove = hostsInHostGroup.stream() .filter(hostMetadata -> hostNames.contains(hostMetadata.getDiscoveryFQDN())) .collect(Collectors.toMap(InstanceMetaData::getDiscoveryFQDN, hostMetadata -> hostMetadata)); if (hostsToRemove.size() != hostNames.size()) { LOGGER.debug("Not all hosts found in the given host group. [{}, {}]", hostGroup.getName(), hostNames); } ClustersResourceApi clustersResourceApi = clouderaManagerApiFactory.getClustersResourceApi(client); try { ApiHostRefList hostRefList = clustersResourceApi.listHosts(stack.getName(), null, null); List<String> runningHosts = hostRefList.getItems().stream() .map(ApiHostRef::getHostname) .collect(Collectors.toList()); Sets.newHashSet(hostsToRemove.keySet()).stream() .filter(hostName -> !runningHosts.contains(hostName)) .forEach(hostsToRemove::remove); LOGGER.debug("Collected hosts to remove: [{}]", hostsToRemove); return hostsToRemove; } catch (ApiException e) { LOGGER.error("Failed to get host list for cluster: {}", stack.getName(), e); throw new CloudbreakServiceException(e.getMessage(), e); } }  void verifyNodesAreRemovable(Stack stack, Collection<InstanceMetaData> removableInstances, ApiClient client); Set<InstanceMetaData> collectDownscaleCandidates(ApiClient client, Stack stack, HostGroup hostGroup, Integer scalingAdjustment,
            int defaultRootVolumeSize, Set<InstanceMetaData> instanceMetaDatasInStack); Map<String, InstanceMetaData> collectHostsToRemove(Stack stack, HostGroup hostGroup, Set<String> hostNames, ApiClient client); Set<String> decommissionNodes(Stack stack, Map<String, InstanceMetaData> hostsToRemove, ApiClient client); void deleteHost(Stack stack, InstanceMetaData data, ApiClient client); void deleteUnusedCredentialsFromCluster(Stack stack, ApiClient client); void stopAndRemoveMgmtService(Stack stack, ApiClient client);  }