Skip to content

Commit 00fe25a

Browse files
authored
Fix allocation of VMs with multiple clusters (#8611)
* Fix allocation of VMs with multiple clusters * Readd debug guard
1 parent ed86dc9 commit 00fe25a

File tree

6 files changed

+32
-34
lines changed

6 files changed

+32
-34
lines changed

plugins/host-allocators/random/src/main/java/com/cloud/agent/manager/allocator/impl/RandomAllocator.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import javax.inject.Inject;
2424

25-
import com.cloud.utils.exception.CloudRuntimeException;
2625
import org.apache.commons.collections.CollectionUtils;
2726
import org.apache.commons.collections.ListUtils;
2827
import org.apache.log4j.Logger;
@@ -36,7 +35,6 @@
3635
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
3736
import com.cloud.host.Host;
3837
import com.cloud.host.Host.Type;
39-
import com.cloud.host.HostVO;
4038
import com.cloud.host.dao.HostDao;
4139
import com.cloud.offering.ServiceOffering;
4240
import com.cloud.resource.ResourceManager;
@@ -66,18 +64,20 @@ private List<Host> findSuitableHosts(VirtualMachineProfile vmProfile, Deployment
6664
Long podId = plan.getPodId();
6765
Long clusterId = plan.getClusterId();
6866
ServiceOffering offering = vmProfile.getServiceOffering();
69-
List<? extends Host> hostsCopy = null;
70-
List<Host> suitableHosts = new ArrayList<Host>();
67+
List<Host> suitableHosts = new ArrayList<>();
7168

7269
if (type == Host.Type.Storage) {
7370
return suitableHosts;
7471
}
72+
7573
String hostTag = offering.getHostTag();
76-
if (hostTag != null) {
77-
s_logger.debug(String.format("Looking for hosts in dc [%s], pod [%s], cluster [%s] and complying with host tag [%s].", dcId, podId, clusterId, hostTag));
78-
} else {
79-
s_logger.debug("Looking for hosts in dc: " + dcId + " pod:" + podId + " cluster:" + clusterId);
80-
}
74+
String hostTagToLog = hostTag != null ? String.format("and complying with host tags [%s]", hostTag) : "";
75+
String paramAsStringToLog = String.format("zone [%s], pod [%s], cluster [%s] %s", dcId, podId, clusterId, hostTagToLog);
76+
77+
s_logger.debug(String.format("Looking for hosts in %s.", paramAsStringToLog));
78+
79+
List<? extends Host> hostsCopy;
80+
8181
if (hosts != null) {
8282
// retain all computing hosts, regardless of whether they support routing...it's random after all
8383
hostsCopy = new ArrayList<Host>(hosts);
@@ -88,7 +88,6 @@ private List<Host> findSuitableHosts(VirtualMachineProfile vmProfile, Deployment
8888
}
8989
} else {
9090
// list all computing hosts, regardless of whether they support routing...it's random after all
91-
hostsCopy = new ArrayList<HostVO>();
9291
if (hostTag != null) {
9392
hostsCopy = _hostDao.listByHostTag(type, clusterId, podId, dcId, hostTag);
9493
} else {
@@ -98,14 +97,15 @@ private List<Host> findSuitableHosts(VirtualMachineProfile vmProfile, Deployment
9897
hostsCopy = ListUtils.union(hostsCopy, _hostDao.findHostsWithTagRuleThatMatchComputeOferringTags(hostTag));
9998

10099
if (hostsCopy.isEmpty()) {
101-
s_logger.error(String.format("No suitable host found for vm [%s] with tags [%s].", vmProfile, hostTag));
102-
throw new CloudRuntimeException(String.format("No suitable host found for vm [%s].", vmProfile));
100+
s_logger.info(String.format("No suitable host found for VM [%s] in %s.", vmProfile, paramAsStringToLog));
101+
return null;
103102
}
104103

105104
s_logger.debug("Random Allocator found " + hostsCopy.size() + " hosts");
106-
if (hostsCopy.size() == 0) {
105+
if (hostsCopy.isEmpty()) {
107106
return suitableHosts;
108107
}
108+
109109
Collections.shuffle(hostsCopy);
110110
for (Host host : hostsCopy) {
111111
if (suitableHosts.size() == returnUpTo) {

server/src/main/java/com/cloud/agent/manager/allocator/impl/FirstFitAllocator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import javax.inject.Inject;
2626
import javax.naming.ConfigurationException;
2727

28-
import com.cloud.utils.exception.CloudRuntimeException;
2928
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
3029
import org.apache.log4j.Logger;
3130
import org.springframework.stereotype.Component;
@@ -132,8 +131,10 @@ public List<Host> allocateTo(VirtualMachineProfile vmProfile, DeploymentPlan pla
132131
return new ArrayList<Host>();
133132
}
134133

134+
String paramAsStringToLog = String.format("zone [%s], pod [%s], cluster [%s]", dcId, podId, clusterId);
135+
135136
if (s_logger.isDebugEnabled()) {
136-
s_logger.debug("Looking for hosts in dc: " + dcId + " pod:" + podId + " cluster:" + clusterId);
137+
s_logger.debug(String.format("Looking for hosts in %s.", paramAsStringToLog));
137138
}
138139

139140
String hostTagOnOffering = offering.getHostTag();
@@ -206,8 +207,8 @@ public List<Host> allocateTo(VirtualMachineProfile vmProfile, DeploymentPlan pla
206207

207208

208209
if (clusterHosts.isEmpty()) {
209-
s_logger.error(String.format("No suitable host found for vm [%s] with tags [%s].", vmProfile, hostTagOnOffering));
210-
throw new CloudRuntimeException(String.format("No suitable host found for vm [%s].", vmProfile));
210+
s_logger.info(String.format("No suitable host found for VM [%s] with tags [%s] in %s.", vmProfile, hostTagOnOffering, paramAsStringToLog));
211+
return null;
211212
}
212213
// add all hosts that we are not considering to the avoid list
213214
List<HostVO> allhostsInCluster = _hostDao.listAllUpAndEnabledNonHAHosts(type, clusterId, podId, dcId, null);

server/src/main/java/com/cloud/agent/manager/allocator/impl/RecreateHostAllocator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.inject.Inject;
2727
import javax.naming.ConfigurationException;
2828

29+
import org.apache.commons.collections.CollectionUtils;
2930
import org.apache.log4j.Logger;
3031
import org.springframework.stereotype.Component;
3132

@@ -75,7 +76,7 @@ public class RecreateHostAllocator extends FirstFitRoutingAllocator {
7576
public List<Host> allocateTo(VirtualMachineProfile vm, DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo) {
7677

7778
List<Host> hosts = super.allocateTo(vm, plan, type, avoid, returnUpTo);
78-
if (hosts != null && !hosts.isEmpty()) {
79+
if (CollectionUtils.isNotEmpty(hosts)) {
7980
return hosts;
8081
}
8182

server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ private DeployDestination checkClustersforDestination(List<Long> clusterList, Vi
12731273
List<Host> suitableHosts = findSuitableHosts(vmProfile, potentialPlan, avoid, HostAllocator.RETURN_UPTO_ALL);
12741274
// if found suitable hosts in this cluster, find suitable storage
12751275
// pools for each volume of the VM
1276-
if (suitableHosts != null && !suitableHosts.isEmpty()) {
1276+
if (CollectionUtils.isNotEmpty(suitableHosts)) {
12771277
if (vmProfile.getHypervisorType() == HypervisorType.BareMetal) {
12781278
DeployDestination dest = new DeployDestination(dc, pod, clusterVO, suitableHosts.get(0));
12791279
return dest;
@@ -1621,18 +1621,17 @@ protected List<Host> findSuitableHosts(VirtualMachineProfile vmProfile, Deployme
16211621
List<Host> suitableHosts = new ArrayList<Host>();
16221622
for (HostAllocator allocator : _hostAllocators) {
16231623
suitableHosts = allocator.allocateTo(vmProfile, plan, Host.Type.Routing, avoid, returnUpTo);
1624-
if (suitableHosts != null && !suitableHosts.isEmpty()) {
1624+
if (CollectionUtils.isNotEmpty(suitableHosts)) {
16251625
break;
16261626
}
16271627
}
16281628

1629-
if (suitableHosts.isEmpty()) {
1630-
s_logger.debug("No suitable hosts found");
1629+
if (CollectionUtils.isEmpty(suitableHosts)) {
1630+
s_logger.debug("No suitable hosts found.");
1631+
} else {
1632+
reorderHostsByPriority(plan.getHostPriorities(), suitableHosts);
16311633
}
16321634

1633-
// re-order hosts by priority
1634-
reorderHostsByPriority(plan.getHostPriorities(), suitableHosts);
1635-
16361635
return suitableHosts;
16371636
}
16381637

server/src/main/java/com/cloud/deploy/FirstFitPlanner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ private void removeClustersWithoutMatchingTag(List<Long> clusterListForVmAllocat
525525
matchingClusters.addAll(hostDao.findClustersThatMatchHostTagRule(hostTagOnOffering));
526526

527527
if (matchingClusters.isEmpty()) {
528-
s_logger.error(String.format("No suitable host found for the following compute offering tags [%s].", hostTagOnOffering));
528+
s_logger.error(String.format("No suitable host found in any cluster for the following compute offering tags [%s].", hostTagOnOffering));
529529
throw new CloudRuntimeException("No suitable host found.");
530530
}
531531

server/src/main/java/com/cloud/server/ManagementServerImpl.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,20 +1590,17 @@ public Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Ho
15901590
suitableHosts = allocator.allocateTo(vmProfile, plan, Host.Type.Routing, excludes, HostAllocator.RETURN_UPTO_ALL, false);
15911591
}
15921592

1593-
if (suitableHosts != null && !suitableHosts.isEmpty()) {
1593+
if (CollectionUtils.isNotEmpty(suitableHosts)) {
15941594
break;
15951595
}
15961596
}
15971597

1598-
// re-order hosts by priority
15991598
_dpMgr.reorderHostsByPriority(plan.getHostPriorities(), suitableHosts);
16001599

1601-
if (s_logger.isDebugEnabled()) {
1602-
if (suitableHosts.isEmpty()) {
1603-
s_logger.debug("No suitable hosts found");
1604-
} else {
1605-
s_logger.debug("Hosts having capacity and suitable for migration: " + suitableHosts);
1606-
}
1600+
if (suitableHosts.isEmpty()) {
1601+
s_logger.warn("No suitable hosts found.");
1602+
} else {
1603+
s_logger.debug("Hosts having capacity and suitable for migration: " + suitableHosts);
16071604
}
16081605

16091606
return new Ternary<>(otherHosts, suitableHosts, requiresStorageMotion);

0 commit comments

Comments
 (0)