AdministrationAgent implements InitializingBean { protected void shutdownIdleHosts(ClusterVO cluster, ClusterAdministrationHeuristicAlgorithm administrationAlgorithm) { if (!administrationAlgorithm.canHeuristicShutdownHosts()) { return; } List<HostResources> idleHosts = getClusterIdleHosts(cluster); long clusterId = cluster.getId(); if (CollectionUtils.isEmpty(idleHosts)) { logger.info(String.format("There are no idle hosts in cluster[%d].", clusterId)); return; } logger.info(String.format("Ranking hosts to shutdown on cluster[%d]", clusterId)); List<HostResources> orderedHostsToPowerOff = administrationAlgorithm.rankHostToPowerOff(idleHosts); for (HostResources host : orderedHostsToPowerOff) { CloudResources cloudResources = cloudResourcesService.createCloudResources(createAllClustersResourcesUp()); if (!administrationAlgorithm.canPowerOffAnotherHostInCloud(cloudResources)) { logger.info("The agent cannot shut down more hosts given the result of canPowerOffAnotherHostInCloud[false]."); break; } long hostId = host.getHostId(); if (!administrationAlgorithm.canPowerOffHost(host, cloudResources)) { logger.info(String.format("Could not shut down host[id=%d] for cluster[id=%d]. Result of canPowerOffHost[false]", hostId, clusterId)); continue; } logger.info(String.format("Shutting down host[id=%d] from cluster[id=%d]. Result of canPowerOffHost[true]", hostId, clusterId)); shutdownHost(hostId); } }  void receiveClusterToBeManaged(Long clusterId); Long searchClusterToBeManaged(); List<ClusterResourcesUp> createAllClustersResourcesUp(); @Override void afterPropertiesSet();  }
@Test public void shutdownIdleHostsTestFullExecution() { ClusterVO cluster = Mockito.mock(ClusterVO.class); List<HostResources> idleHosts = createHosts(1); shutdownIdleHostsTestSetupMethodsExecution(idleHosts, true, true, true); administrationAgent.shutdownIdleHosts(cluster, clusterAdministrationHeuristicAlgorithm); verifyShutdownIdleHostsCalledMethods(1, 1, 1, 1); }
@Test public void shutdownIdleHostsTestHeuristicCannotShutdownHosts() { ClusterVO cluster = Mockito.mock(ClusterVO.class); List<HostResources> idleHosts = createHosts(1); shutdownIdleHostsTestSetupMethodsExecution(idleHosts, false, true, true); administrationAgent.shutdownIdleHosts(cluster, clusterAdministrationHeuristicAlgorithm); verifyShutdownIdleHostsCalledMethods(0, 0, 0, 0); }
@Test public void shutdownIdleHostsTestEmptyIdleHostsList() { ClusterVO cluster = Mockito.mock(ClusterVO.class); List<HostResources> idleHosts = new ArrayList<>(); shutdownIdleHostsTestSetupMethodsExecution(idleHosts, true, true, true); administrationAgent.shutdownIdleHosts(cluster, clusterAdministrationHeuristicAlgorithm); verifyShutdownIdleHostsCalledMethods(1, 0, 0, 0); }
@Test public void shutdownIdleHostsTestHeuristicCannotPowerOffAnotherHosts() { ClusterVO cluster = Mockito.mock(ClusterVO.class); List<HostResources> idleHosts = createHosts(1); shutdownIdleHostsTestSetupMethodsExecution(idleHosts, true, false, true); administrationAgent.shutdownIdleHosts(cluster, clusterAdministrationHeuristicAlgorithm); verifyShutdownIdleHostsCalledMethods(1, 1, 0, 0); }
@Test public void shutdownIdleHostsTestHeuristicCannotPowerOffHost() { ClusterVO cluster = Mockito.mock(ClusterVO.class); List<HostResources> idleHosts = createHosts(1); shutdownIdleHostsTestSetupMethodsExecution(idleHosts, true, true, false); administrationAgent.shutdownIdleHosts(cluster, clusterAdministrationHeuristicAlgorithm); verifyShutdownIdleHostsCalledMethods(1, 1, 1, 0); }