use of org.ovirt.engine.core.bll.scheduling.utils.FindVmAndDestinations in project ovirt-engine by oVirt.
the class CpuAndMemoryBalancingPolicyUnit method balance.
@Override
public Optional<BalanceResult> balance(final Cluster cluster, List<VDS> hosts, Map<String, String> parameters, ArrayList<String> messages) {
Objects.requireNonNull(hosts);
Objects.requireNonNull(cluster);
if (hosts.size() < 2) {
log.debug("No balancing for cluster '{}', contains only {} host(s)", cluster.getName(), hosts.size());
return Optional.empty();
}
final List<VDS> overUtilizedPrimaryHosts = getPrimarySources(cluster, hosts, parameters);
final List<VDS> overUtilizedSecondaryHosts = getSecondarySources(cluster, hosts, parameters);
// if there aren't any overutilized hosts, then there is nothing to balance...
if ((overUtilizedPrimaryHosts == null || overUtilizedPrimaryHosts.size() == 0) && (overUtilizedSecondaryHosts == null || overUtilizedSecondaryHosts.size() == 0)) {
log.debug("There is no over-utilized host in cluster '{}'", cluster.getName());
return Optional.empty();
}
FindVmAndDestinations findVmAndDestinations = getFindVmAndDestinations(cluster, parameters);
Optional<BalanceResult> result = Optional.empty();
// try balancing based on CPU first
if (overUtilizedPrimaryHosts != null && overUtilizedPrimaryHosts.size() > 0) {
// returns hosts with utilization lower than the specified threshold
List<VDS> underUtilizedHosts = getPrimaryDestinations(cluster, hosts, parameters);
/* if no host has a spare power, then there is nothing we can do to balance it here, try
the secondary aporoach */
if (underUtilizedHosts == null || underUtilizedHosts.size() == 0) {
log.warn("All candidate hosts have been filtered, can't balance the cluster '{}'" + " based on the CPU usage, will try memory based approach", cluster.getName());
} else {
result = getBalance(findVmAndDestinations, overUtilizedPrimaryHosts, underUtilizedHosts);
}
}
// if it is not possible (or necessary) to balance based on CPU, try with memory
if (!result.isPresent() && (overUtilizedSecondaryHosts != null && overUtilizedSecondaryHosts.size() > 0)) {
// returns hosts with more free memory than the specified threshold
List<VDS> underUtilizedHosts = getSecondaryDestinations(cluster, hosts, parameters);
// if no host has memory to spare, then there is nothing we can do to balance it..
if (underUtilizedHosts == null || underUtilizedHosts.size() == 0) {
log.warn("All candidate hosts have been filtered, can't balance the cluster '{}'" + " using memory based approach", cluster.getName());
return Optional.empty();
}
result = getBalance(findVmAndDestinations, overUtilizedSecondaryHosts, underUtilizedHosts);
}
// add the current host, it is possible it is the best host after all,
// because the balancer does not know about affinity for example
Optional<BalanceResult> finalResult = result;
result.map(BalanceResult::getCurrentHost).filter(Objects::nonNull).ifPresent(h -> finalResult.ifPresent(res -> res.getCandidateHosts().add(h)));
return result;
}
Aggregations