winterhazel commented on code in PR #9074:
URL: https://github.com/apache/cloudstack/pull/9074#discussion_r2211701483


##########
server/src/main/java/com/cloud/server/ManagementServerImpl.java:
##########
@@ -1653,10 +1649,10 @@ public Ternary<Pair<List<? extends Host>, Integer>, 
List<? extends Host>, Map<Ho
 
         _dpMgr.reorderHostsByPriority(plan.getHostPriorities(), suitableHosts);
 
-        if (suitableHosts.isEmpty()) {
+        if (CollectionUtils.isEmpty(suitableHosts)) {
             logger.warn("No suitable hosts found.");
         } else {
-            logger.debug("Hosts having capacity and suitable for migration: 
{}", suitableHosts);
+            logger.debug("Hosts having capacity and are suitable for 
migration: {} ", suitableHosts);

Review Comment:
   ```suggestion
               logger.debug("Hosts having capacity and are suitable for 
migration: {}", suitableHosts);
   ```



##########
server/src/main/java/com/cloud/agent/manager/allocator/impl/FirstFitAllocator.java:
##########
@@ -93,286 +93,190 @@ public class FirstFitAllocator extends AdapterBase 
implements HostAllocator {
     @Inject
     protected ResourceManager _resourceMgr;
     @Inject
-    ClusterDao _clusterDao;
-    @Inject
-    ClusterDetailsDao _clusterDetailsDao;
-    @Inject
     ServiceOfferingDetailsDao _serviceOfferingDetailsDao;
     @Inject
-    CapacityManager _capacityMgr;
-    @Inject
     CapacityDao _capacityDao;
     @Inject
     UserVmDetailsDao _userVmDetailsDao;
 
     boolean _checkHvm = true;
+
     static DecimalFormat decimalFormat = new DecimalFormat("#.##");
 
     @Override
     public List<Host> allocateTo(VirtualMachineProfile vmProfile, 
DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo) {
-        return allocateTo(vmProfile, plan, type, avoid, returnUpTo, true);
+        return allocateTo(vmProfile, plan, type, avoid, null, returnUpTo, 
true);
     }
 
     @Override
-    public List<Host> allocateTo(VirtualMachineProfile vmProfile, 
DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo, boolean 
considerReservedCapacity) {
+    public List<Host> allocateTo(VirtualMachineProfile vmProfile, 
DeploymentPlan plan, Type type, ExcludeList avoid, List<? extends Host> hosts, 
int returnUpTo,
+                                 boolean considerReservedCapacity) {
+        if (type == Host.Type.Storage) {
+            return null;
+        }
 
         long dcId = plan.getDataCenterId();
         Long podId = plan.getPodId();
         Long clusterId = plan.getClusterId();
         ServiceOffering offering = vmProfile.getServiceOffering();
-        VMTemplateVO template = (VMTemplateVO)vmProfile.getTemplate();
+        VMTemplateVO template = (VMTemplateVO) vmProfile.getTemplate();
         Account account = vmProfile.getOwner();
 
-        boolean isVMDeployedWithUefi = false;
-        UserVmDetailVO userVmDetailVO = 
_userVmDetailsDao.findDetail(vmProfile.getId(), "UEFI");
-        if(userVmDetailVO != null){
-            if ("secure".equalsIgnoreCase(userVmDetailVO.getValue()) || 
"legacy".equalsIgnoreCase(userVmDetailVO.getValue())) {
-                isVMDeployedWithUefi = true;
-            }
-        }
-        logger.info(" Guest VM is requested with Custom[UEFI] Boot Type "+ 
isVMDeployedWithUefi);
+        String hostTagOnOffering = offering.getHostTag();
+        String hostTagOnTemplate = template.getTemplateTag();
+        String paramAsStringToLog = String.format("zone [%s], pod [%s], 
cluster [%s]", dcId, podId, clusterId);
 
+        List<HostVO> suitableHosts = retrieveHosts(vmProfile, type, 
(List<HostVO>) hosts, clusterId, podId, dcId, hostTagOnOffering, 
hostTagOnTemplate);
 
-        if (type == Host.Type.Storage) {
-            // FirstFitAllocator should be used for user VMs only since it 
won't care whether the host is capable of routing or not
-            return new ArrayList<>();
+        if (suitableHosts.isEmpty()) {
+            logger.info("No suitable host found for VM [{}] in {}.", 
vmProfile, paramAsStringToLog);
+            return null;
         }
-        String paramAsStringToLog = String.format("zone [%s], pod [%s], 
cluster [%s]", dcId, podId, clusterId);
-        logger.debug("Looking for hosts in {}", paramAsStringToLog);
 
-        String hostTagOnOffering = offering.getHostTag();
-        String hostTagOnTemplate = template.getTemplateTag();
-        String hostTagUefi = "UEFI";
+        if (CollectionUtils.isEmpty(hosts)) {
+            addHostsToAvoidSet(type, avoid, clusterId, podId, dcId, 
suitableHosts);
+        }
+
+        return allocateTo(plan, offering, template, avoid, suitableHosts, 
returnUpTo, considerReservedCapacity, account);
+    }
 
-        boolean hasSvcOfferingTag = hostTagOnOffering != null ? true : false;
-        boolean hasTemplateTag = hostTagOnTemplate != null ? true : false;
+    protected List<HostVO> retrieveHosts(VirtualMachineProfile vmProfile, Type 
type, List<HostVO> hostsToFilter, Long clusterId, Long podId, long dcId, String 
hostTagOnOffering,
+                                         String hostTagOnTemplate) {
+        String haVmTag = (String) 
vmProfile.getParameter(VirtualMachineProfile.Param.HaTag);
+        List<HostVO> clusterHosts;
 
-        List<HostVO> clusterHosts = new ArrayList<>();
-        List<HostVO> hostsMatchingUefiTag = new ArrayList<>();
-        if(isVMDeployedWithUefi){
-            hostsMatchingUefiTag = _hostDao.listByHostCapability(type, 
clusterId, podId, dcId, Host.HOST_UEFI_ENABLE);
-            if (logger.isDebugEnabled()) {
-                logger.debug("Hosts with tag '" + hostTagUefi + "' are:" + 
hostsMatchingUefiTag);
-            }
+        if (CollectionUtils.isNotEmpty(hostsToFilter)) {
+            clusterHosts = new ArrayList<>(hostsToFilter);
+        } else {
+            clusterHosts = _resourceMgr.listAllUpAndEnabledHosts(type, 
clusterId, podId, dcId);
         }
 
-
-        String haVmTag = 
(String)vmProfile.getParameter(VirtualMachineProfile.Param.HaTag);
         if (haVmTag != null) {
-            clusterHosts = _hostDao.listByHostTag(type, clusterId, podId, 
dcId, haVmTag);
+            clusterHosts.retainAll(hostDao.listByHostTag(type, clusterId, 
podId, dcId, haVmTag));
+        } else if (ObjectUtils.allNull(hostTagOnOffering, hostTagOnTemplate)) {
+            
clusterHosts.retainAll(_resourceMgr.listAllUpAndEnabledNonHAHosts(type, 
clusterId, podId, dcId));
         } else {
-            if (hostTagOnOffering == null && hostTagOnTemplate == null) {
-                clusterHosts = 
_resourceMgr.listAllUpAndEnabledNonHAHosts(type, clusterId, podId, dcId);
-            } else {
-                List<HostVO> hostsMatchingOfferingTag = new ArrayList<>();
-                List<HostVO> hostsMatchingTemplateTag = new ArrayList<>();
-                if (hasSvcOfferingTag) {
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Looking for hosts having tag specified 
on SvcOffering:" + hostTagOnOffering);
-                    }
-                    hostsMatchingOfferingTag = _hostDao.listByHostTag(type, 
clusterId, podId, dcId, hostTagOnOffering);
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Hosts with tag '" + hostTagOnOffering + 
"' are:" + hostsMatchingOfferingTag);
-                    }
-                }
-                if (hasTemplateTag) {
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Looking for hosts having tag specified 
on Template:" + hostTagOnTemplate);
-                    }
-                    hostsMatchingTemplateTag = _hostDao.listByHostTag(type, 
clusterId, podId, dcId, hostTagOnTemplate);
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Hosts with tag '" + hostTagOnTemplate + 
"' are:" + hostsMatchingTemplateTag);
-                    }
-                }
-
-                if (hasSvcOfferingTag && hasTemplateTag) {
-                    
hostsMatchingOfferingTag.retainAll(hostsMatchingTemplateTag);
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Found " + 
hostsMatchingOfferingTag.size() + " Hosts satisfying both tags, host ids are:" 
+ hostsMatchingOfferingTag);
-                    }
-
-                    clusterHosts = hostsMatchingOfferingTag;
-                } else {
-                    if (hasSvcOfferingTag) {
-                        clusterHosts = hostsMatchingOfferingTag;
-                    } else {
-                        clusterHosts = hostsMatchingTemplateTag;
-                    }
-                }
-            }
+            retainHostsMatchingServiceOfferingAndTemplateTags(clusterHosts, 
type, dcId, podId, clusterId, hostTagOnOffering, hostTagOnTemplate);
         }
 
-        if (isVMDeployedWithUefi) {
-            clusterHosts.retainAll(hostsMatchingUefiTag);
-        }
+        filterHostsWithUefiEnabled(type, vmProfile, clusterId, podId, dcId, 
clusterHosts);
 
-        
clusterHosts.addAll(_hostDao.findHostsWithTagRuleThatMatchComputeOferringTags(hostTagOnOffering));
+        addHostsBasedOnTagRules(hostTagOnOffering, clusterHosts);
 
+        return clusterHosts;
 
-        if (clusterHosts.isEmpty()) {
-            logger.warn("No suitable host found for VM [{}] with tags {} in 
{}.", vmProfile, hostTagOnOffering, paramAsStringToLog);
-            return null;
-        }
-        // add all hosts that we are not considering to the avoid list
-        List<HostVO> allhostsInCluster = 
_hostDao.listAllUpAndEnabledNonHAHosts(type, clusterId, podId, dcId, null);
-        allhostsInCluster.removeAll(clusterHosts);
+    }
+
+    /**
+     * Add all hosts to the avoid set that were not considered during the 
allocation
+     */
+    protected void addHostsToAvoidSet(Type type, ExcludeList avoid, Long 
clusterId, Long podId, long dcId, List<HostVO> suitableHosts) {
+        List<HostVO> allHostsInCluster = 
hostDao.listAllUpAndEnabledNonHAHosts(type, clusterId, podId, dcId, null);
 
-        logger.debug(() -> String.format("Adding hosts [%s] to the avoid set 
because these hosts do not support HA.",
-                
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(allhostsInCluster, 
"uuid", "name")));
+        allHostsInCluster.removeAll(suitableHosts);
 
-        for (HostVO host : allhostsInCluster) {
+        logger.debug("Adding hosts [{}] to the avoid set because these hosts 
were not considered for allocation.",
+                () -> 
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(allHostsInCluster, 
"uuid", "name"));
+
+        for (HostVO host : allHostsInCluster) {
             avoid.addHost(host.getId());
         }
-
-        return allocateTo(plan, offering, template, avoid, clusterHosts, 
returnUpTo, considerReservedCapacity, account);
     }
 
-    @Override
-    public List<Host> allocateTo(VirtualMachineProfile vmProfile, 
DeploymentPlan plan, Type type, ExcludeList avoid, List<? extends Host> hosts, 
int returnUpTo,
-        boolean considerReservedCapacity) {
-        long dcId = plan.getDataCenterId();
-        Long podId = plan.getPodId();
-        Long clusterId = plan.getClusterId();
-        ServiceOffering offering = vmProfile.getServiceOffering();
-        VMTemplateVO template = (VMTemplateVO)vmProfile.getTemplate();
-        Account account = vmProfile.getOwner();
-        List<Host> suitableHosts = new ArrayList<>();
-        List<Host> hostsCopy = new ArrayList<>(hosts);
+    protected void filterHostsWithUefiEnabled(Type type, VirtualMachineProfile 
vmProfile, Long clusterId, Long podId, long dcId, List<HostVO> clusterHosts) {
+        UserVmDetailVO userVmDetailVO = 
_userVmDetailsDao.findDetail(vmProfile.getId(), "UEFI");
 
-        if (type == Host.Type.Storage) {
-            // FirstFitAllocator should be used for user VMs only since it 
won't care whether the host is capable of
-            // routing or not.
-            return suitableHosts;
+        if (userVmDetailVO == null) {
+            return;
         }
 
-        String hostTagOnOffering = offering.getHostTag();
-        String hostTagOnTemplate = template.getTemplateTag();
-        boolean hasSvcOfferingTag = hostTagOnOffering != null ? true : false;
-        boolean hasTemplateTag = hostTagOnTemplate != null ? true : false;
-
-        String haVmTag = 
(String)vmProfile.getParameter(VirtualMachineProfile.Param.HaTag);
-        if (haVmTag != null) {
-            hostsCopy.retainAll(_hostDao.listByHostTag(type, clusterId, podId, 
dcId, haVmTag));
-        } else {
-            if (hostTagOnOffering == null && hostTagOnTemplate == null) {
-                
hostsCopy.retainAll(_resourceMgr.listAllUpAndEnabledNonHAHosts(type, clusterId, 
podId, dcId));
-            } else {
-                if (hasSvcOfferingTag) {
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Looking for hosts having tag specified 
on SvcOffering:" + hostTagOnOffering);
-                    }
-                    hostsCopy.retainAll(_hostDao.listByHostTag(type, 
clusterId, podId, dcId, hostTagOnOffering));
-
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Hosts with tag '" + hostTagOnOffering + 
"' are:" + hostsCopy);
-                    }
-                }
-
-                if (hasTemplateTag) {
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Looking for hosts having tag specified 
on Template:" + hostTagOnTemplate);
-                    }
-
-                    hostsCopy.retainAll(_hostDao.listByHostTag(type, 
clusterId, podId, dcId, hostTagOnTemplate));
-
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Hosts with tag '" + hostTagOnTemplate + 
"' are:" + hostsCopy);
-                    }
-                }
-            }
+        if (!StringUtils.equalsAnyIgnoreCase(userVmDetailVO.getValue(), 
ApiConstants.BootMode.SECURE.toString(), 
ApiConstants.BootMode.LEGACY.toString())) {
+            return;
         }
 
-        
hostsCopy.addAll(_hostDao.findHostsWithTagRuleThatMatchComputeOferringTags(hostTagOnOffering));
+        logger.info("Guest VM is requested with Custom[UEFI] Boot Type 
enabled.");
 
-        if (!hostsCopy.isEmpty()) {
-            suitableHosts = allocateTo(plan, offering, template, avoid, 
hostsCopy, returnUpTo, considerReservedCapacity, account);
-        }
+        List<HostVO> hostsMatchingUefiTag = hostDao.listByHostCapability(type, 
clusterId, podId, dcId, Host.HOST_UEFI_ENABLE);
 
-        return suitableHosts;
+        logger.debug("Hosts with UEFI enabled are {}.", hostsMatchingUefiTag);
+        clusterHosts.retainAll(hostsMatchingUefiTag);
     }
 
     protected List<Host> allocateTo(DeploymentPlan plan, ServiceOffering 
offering, VMTemplateVO template, ExcludeList avoid, List<? extends Host> hosts, 
int returnUpTo,
-        boolean considerReservedCapacity, Account account) {
+                                    boolean considerReservedCapacity, Account 
account) {
+
         String vmAllocationAlgorithm = 
DeploymentClusterPlanner.VmAllocationAlgorithm.value();
-        if (vmAllocationAlgorithm.equals("random") || 
vmAllocationAlgorithm.equals("userconcentratedpod_random")) {
-            // Shuffle this so that we don't check the hosts in the same order.
+        if (List.of(random.toString(), 
userconcentratedpod_random.toString()).contains(vmAllocationAlgorithm)) {
             Collections.shuffle(hosts);
-        } else if (vmAllocationAlgorithm.equals("userdispersing")) {
+        } else if (userdispersing.toString().equals(vmAllocationAlgorithm)) {
             hosts = reorderHostsByNumberOfVms(plan, hosts, account);
-        } else if(vmAllocationAlgorithm.equals("firstfitleastconsumed")){
+        } else if 
(firstfitleastconsumed.toString().equals(vmAllocationAlgorithm)) {
             hosts = reorderHostsByCapacity(plan, hosts);
         }
 
-        if (logger.isDebugEnabled()) {
-            logger.debug("FirstFitAllocator has " + hosts.size() + " hosts to 
check for allocation: " + hosts);
-        }
-
-        // We will try to reorder the host lists such that we give priority to 
hosts that have
-        // the minimums to support a VM's requirements
+        logger.debug("FirstFitAllocator has {} hosts to check for allocation 
{}.", hosts.size(), hosts);
         hosts = prioritizeHosts(template, offering, hosts);
+        logger.debug("Found {} hosts for allocation after prioritization: 
{}.", hosts.size(), hosts);
 
-        if (logger.isDebugEnabled()) {
-            logger.debug("Found " + hosts.size() + " hosts for allocation 
after prioritization: " + hosts);
-        }
+        List<Host> suitableHosts = checkHostsCompatibilities(offering, avoid, 
hosts, returnUpTo, considerReservedCapacity);
+        logger.debug("Host Allocator returning {} suitable hosts.", 
suitableHosts.size());
 
-        if (logger.isDebugEnabled()) {
-            logger.debug("Looking for speed=" + (offering.getCpu() * 
offering.getSpeed()) + "Mhz, Ram=" + offering.getRamSize() + " MB");
-        }
+        return suitableHosts;
+    }
 
-        long serviceOfferingId = offering.getId();
+    protected List<Host> checkHostsCompatibilities(ServiceOffering offering, 
ExcludeList avoid, List<? extends Host> hosts, int returnUpTo, boolean 
considerReservedCapacity) {
         List<Host> suitableHosts = new ArrayList<>();
-        ServiceOfferingDetailsVO offeringDetails = null;
+        logger.debug("Checking compatibility for the following hosts {}.", 
suitableHosts);
 
         for (Host host : hosts) {
             if (suitableHosts.size() == returnUpTo) {
                 break;
             }
+
             if (avoid.shouldAvoid(host)) {
-                if (logger.isDebugEnabled()) {
-                    logger.debug("Host: {} is in avoid set, skipping this and 
trying other available hosts", host);
-                }
+                logger.debug("Host [{}] is in avoid set, skipping this and 
trying other available hosts", () -> host);

Review Comment:
   ```suggestion
                   logger.debug("Host [{}] is in avoid set, skipping this and 
trying other available hosts", host);
   ```



##########
server/src/main/java/com/cloud/agent/manager/allocator/impl/FirstFitAllocator.java:
##########
@@ -93,286 +93,190 @@ public class FirstFitAllocator extends AdapterBase 
implements HostAllocator {
     @Inject
     protected ResourceManager _resourceMgr;
     @Inject
-    ClusterDao _clusterDao;
-    @Inject
-    ClusterDetailsDao _clusterDetailsDao;
-    @Inject
     ServiceOfferingDetailsDao _serviceOfferingDetailsDao;
     @Inject
-    CapacityManager _capacityMgr;
-    @Inject
     CapacityDao _capacityDao;
     @Inject
     UserVmDetailsDao _userVmDetailsDao;
 
     boolean _checkHvm = true;
+
     static DecimalFormat decimalFormat = new DecimalFormat("#.##");
 
     @Override
     public List<Host> allocateTo(VirtualMachineProfile vmProfile, 
DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo) {
-        return allocateTo(vmProfile, plan, type, avoid, returnUpTo, true);
+        return allocateTo(vmProfile, plan, type, avoid, null, returnUpTo, 
true);
     }
 
     @Override
-    public List<Host> allocateTo(VirtualMachineProfile vmProfile, 
DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo, boolean 
considerReservedCapacity) {
+    public List<Host> allocateTo(VirtualMachineProfile vmProfile, 
DeploymentPlan plan, Type type, ExcludeList avoid, List<? extends Host> hosts, 
int returnUpTo,
+                                 boolean considerReservedCapacity) {
+        if (type == Host.Type.Storage) {
+            return null;
+        }
 
         long dcId = plan.getDataCenterId();
         Long podId = plan.getPodId();
         Long clusterId = plan.getClusterId();
         ServiceOffering offering = vmProfile.getServiceOffering();
-        VMTemplateVO template = (VMTemplateVO)vmProfile.getTemplate();
+        VMTemplateVO template = (VMTemplateVO) vmProfile.getTemplate();
         Account account = vmProfile.getOwner();
 
-        boolean isVMDeployedWithUefi = false;
-        UserVmDetailVO userVmDetailVO = 
_userVmDetailsDao.findDetail(vmProfile.getId(), "UEFI");
-        if(userVmDetailVO != null){
-            if ("secure".equalsIgnoreCase(userVmDetailVO.getValue()) || 
"legacy".equalsIgnoreCase(userVmDetailVO.getValue())) {
-                isVMDeployedWithUefi = true;
-            }
-        }
-        logger.info(" Guest VM is requested with Custom[UEFI] Boot Type "+ 
isVMDeployedWithUefi);
+        String hostTagOnOffering = offering.getHostTag();
+        String hostTagOnTemplate = template.getTemplateTag();
+        String paramAsStringToLog = String.format("zone [%s], pod [%s], 
cluster [%s]", dcId, podId, clusterId);
 
+        List<HostVO> suitableHosts = retrieveHosts(vmProfile, type, 
(List<HostVO>) hosts, clusterId, podId, dcId, hostTagOnOffering, 
hostTagOnTemplate);
 
-        if (type == Host.Type.Storage) {
-            // FirstFitAllocator should be used for user VMs only since it 
won't care whether the host is capable of routing or not
-            return new ArrayList<>();
+        if (suitableHosts.isEmpty()) {
+            logger.info("No suitable host found for VM [{}] in {}.", 
vmProfile, paramAsStringToLog);
+            return null;
         }
-        String paramAsStringToLog = String.format("zone [%s], pod [%s], 
cluster [%s]", dcId, podId, clusterId);
-        logger.debug("Looking for hosts in {}", paramAsStringToLog);
 
-        String hostTagOnOffering = offering.getHostTag();
-        String hostTagOnTemplate = template.getTemplateTag();
-        String hostTagUefi = "UEFI";
+        if (CollectionUtils.isEmpty(hosts)) {
+            addHostsToAvoidSet(type, avoid, clusterId, podId, dcId, 
suitableHosts);
+        }
+
+        return allocateTo(plan, offering, template, avoid, suitableHosts, 
returnUpTo, considerReservedCapacity, account);
+    }
 
-        boolean hasSvcOfferingTag = hostTagOnOffering != null ? true : false;
-        boolean hasTemplateTag = hostTagOnTemplate != null ? true : false;
+    protected List<HostVO> retrieveHosts(VirtualMachineProfile vmProfile, Type 
type, List<HostVO> hostsToFilter, Long clusterId, Long podId, long dcId, String 
hostTagOnOffering,
+                                         String hostTagOnTemplate) {
+        String haVmTag = (String) 
vmProfile.getParameter(VirtualMachineProfile.Param.HaTag);
+        List<HostVO> clusterHosts;
 
-        List<HostVO> clusterHosts = new ArrayList<>();
-        List<HostVO> hostsMatchingUefiTag = new ArrayList<>();
-        if(isVMDeployedWithUefi){
-            hostsMatchingUefiTag = _hostDao.listByHostCapability(type, 
clusterId, podId, dcId, Host.HOST_UEFI_ENABLE);
-            if (logger.isDebugEnabled()) {
-                logger.debug("Hosts with tag '" + hostTagUefi + "' are:" + 
hostsMatchingUefiTag);
-            }
+        if (CollectionUtils.isNotEmpty(hostsToFilter)) {
+            clusterHosts = new ArrayList<>(hostsToFilter);
+        } else {
+            clusterHosts = _resourceMgr.listAllUpAndEnabledHosts(type, 
clusterId, podId, dcId);
         }
 
-
-        String haVmTag = 
(String)vmProfile.getParameter(VirtualMachineProfile.Param.HaTag);
         if (haVmTag != null) {
-            clusterHosts = _hostDao.listByHostTag(type, clusterId, podId, 
dcId, haVmTag);
+            clusterHosts.retainAll(hostDao.listByHostTag(type, clusterId, 
podId, dcId, haVmTag));
+        } else if (ObjectUtils.allNull(hostTagOnOffering, hostTagOnTemplate)) {
+            
clusterHosts.retainAll(_resourceMgr.listAllUpAndEnabledNonHAHosts(type, 
clusterId, podId, dcId));
         } else {
-            if (hostTagOnOffering == null && hostTagOnTemplate == null) {
-                clusterHosts = 
_resourceMgr.listAllUpAndEnabledNonHAHosts(type, clusterId, podId, dcId);
-            } else {
-                List<HostVO> hostsMatchingOfferingTag = new ArrayList<>();
-                List<HostVO> hostsMatchingTemplateTag = new ArrayList<>();
-                if (hasSvcOfferingTag) {
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Looking for hosts having tag specified 
on SvcOffering:" + hostTagOnOffering);
-                    }
-                    hostsMatchingOfferingTag = _hostDao.listByHostTag(type, 
clusterId, podId, dcId, hostTagOnOffering);
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Hosts with tag '" + hostTagOnOffering + 
"' are:" + hostsMatchingOfferingTag);
-                    }
-                }
-                if (hasTemplateTag) {
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Looking for hosts having tag specified 
on Template:" + hostTagOnTemplate);
-                    }
-                    hostsMatchingTemplateTag = _hostDao.listByHostTag(type, 
clusterId, podId, dcId, hostTagOnTemplate);
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Hosts with tag '" + hostTagOnTemplate + 
"' are:" + hostsMatchingTemplateTag);
-                    }
-                }
-
-                if (hasSvcOfferingTag && hasTemplateTag) {
-                    
hostsMatchingOfferingTag.retainAll(hostsMatchingTemplateTag);
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Found " + 
hostsMatchingOfferingTag.size() + " Hosts satisfying both tags, host ids are:" 
+ hostsMatchingOfferingTag);
-                    }
-
-                    clusterHosts = hostsMatchingOfferingTag;
-                } else {
-                    if (hasSvcOfferingTag) {
-                        clusterHosts = hostsMatchingOfferingTag;
-                    } else {
-                        clusterHosts = hostsMatchingTemplateTag;
-                    }
-                }
-            }
+            retainHostsMatchingServiceOfferingAndTemplateTags(clusterHosts, 
type, dcId, podId, clusterId, hostTagOnOffering, hostTagOnTemplate);
         }
 
-        if (isVMDeployedWithUefi) {
-            clusterHosts.retainAll(hostsMatchingUefiTag);
-        }
+        filterHostsWithUefiEnabled(type, vmProfile, clusterId, podId, dcId, 
clusterHosts);
 
-        
clusterHosts.addAll(_hostDao.findHostsWithTagRuleThatMatchComputeOferringTags(hostTagOnOffering));
+        addHostsBasedOnTagRules(hostTagOnOffering, clusterHosts);
 
+        return clusterHosts;
 
-        if (clusterHosts.isEmpty()) {
-            logger.warn("No suitable host found for VM [{}] with tags {} in 
{}.", vmProfile, hostTagOnOffering, paramAsStringToLog);
-            return null;
-        }
-        // add all hosts that we are not considering to the avoid list
-        List<HostVO> allhostsInCluster = 
_hostDao.listAllUpAndEnabledNonHAHosts(type, clusterId, podId, dcId, null);
-        allhostsInCluster.removeAll(clusterHosts);
+    }
+
+    /**
+     * Add all hosts to the avoid set that were not considered during the 
allocation
+     */
+    protected void addHostsToAvoidSet(Type type, ExcludeList avoid, Long 
clusterId, Long podId, long dcId, List<HostVO> suitableHosts) {
+        List<HostVO> allHostsInCluster = 
hostDao.listAllUpAndEnabledNonHAHosts(type, clusterId, podId, dcId, null);
 
-        logger.debug(() -> String.format("Adding hosts [%s] to the avoid set 
because these hosts do not support HA.",
-                
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(allhostsInCluster, 
"uuid", "name")));
+        allHostsInCluster.removeAll(suitableHosts);
 
-        for (HostVO host : allhostsInCluster) {
+        logger.debug("Adding hosts [{}] to the avoid set because these hosts 
were not considered for allocation.",
+                () -> 
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(allHostsInCluster, 
"uuid", "name"));
+
+        for (HostVO host : allHostsInCluster) {
             avoid.addHost(host.getId());
         }
-
-        return allocateTo(plan, offering, template, avoid, clusterHosts, 
returnUpTo, considerReservedCapacity, account);
     }
 
-    @Override
-    public List<Host> allocateTo(VirtualMachineProfile vmProfile, 
DeploymentPlan plan, Type type, ExcludeList avoid, List<? extends Host> hosts, 
int returnUpTo,
-        boolean considerReservedCapacity) {
-        long dcId = plan.getDataCenterId();
-        Long podId = plan.getPodId();
-        Long clusterId = plan.getClusterId();
-        ServiceOffering offering = vmProfile.getServiceOffering();
-        VMTemplateVO template = (VMTemplateVO)vmProfile.getTemplate();
-        Account account = vmProfile.getOwner();
-        List<Host> suitableHosts = new ArrayList<>();
-        List<Host> hostsCopy = new ArrayList<>(hosts);
+    protected void filterHostsWithUefiEnabled(Type type, VirtualMachineProfile 
vmProfile, Long clusterId, Long podId, long dcId, List<HostVO> clusterHosts) {
+        UserVmDetailVO userVmDetailVO = 
_userVmDetailsDao.findDetail(vmProfile.getId(), "UEFI");
 
-        if (type == Host.Type.Storage) {
-            // FirstFitAllocator should be used for user VMs only since it 
won't care whether the host is capable of
-            // routing or not.
-            return suitableHosts;
+        if (userVmDetailVO == null) {
+            return;
         }
 
-        String hostTagOnOffering = offering.getHostTag();
-        String hostTagOnTemplate = template.getTemplateTag();
-        boolean hasSvcOfferingTag = hostTagOnOffering != null ? true : false;
-        boolean hasTemplateTag = hostTagOnTemplate != null ? true : false;
-
-        String haVmTag = 
(String)vmProfile.getParameter(VirtualMachineProfile.Param.HaTag);
-        if (haVmTag != null) {
-            hostsCopy.retainAll(_hostDao.listByHostTag(type, clusterId, podId, 
dcId, haVmTag));
-        } else {
-            if (hostTagOnOffering == null && hostTagOnTemplate == null) {
-                
hostsCopy.retainAll(_resourceMgr.listAllUpAndEnabledNonHAHosts(type, clusterId, 
podId, dcId));
-            } else {
-                if (hasSvcOfferingTag) {
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Looking for hosts having tag specified 
on SvcOffering:" + hostTagOnOffering);
-                    }
-                    hostsCopy.retainAll(_hostDao.listByHostTag(type, 
clusterId, podId, dcId, hostTagOnOffering));
-
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Hosts with tag '" + hostTagOnOffering + 
"' are:" + hostsCopy);
-                    }
-                }
-
-                if (hasTemplateTag) {
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Looking for hosts having tag specified 
on Template:" + hostTagOnTemplate);
-                    }
-
-                    hostsCopy.retainAll(_hostDao.listByHostTag(type, 
clusterId, podId, dcId, hostTagOnTemplate));
-
-                    if (logger.isDebugEnabled()) {
-                        logger.debug("Hosts with tag '" + hostTagOnTemplate + 
"' are:" + hostsCopy);
-                    }
-                }
-            }
+        if (!StringUtils.equalsAnyIgnoreCase(userVmDetailVO.getValue(), 
ApiConstants.BootMode.SECURE.toString(), 
ApiConstants.BootMode.LEGACY.toString())) {
+            return;
         }
 
-        
hostsCopy.addAll(_hostDao.findHostsWithTagRuleThatMatchComputeOferringTags(hostTagOnOffering));
+        logger.info("Guest VM is requested with Custom[UEFI] Boot Type 
enabled.");
 
-        if (!hostsCopy.isEmpty()) {
-            suitableHosts = allocateTo(plan, offering, template, avoid, 
hostsCopy, returnUpTo, considerReservedCapacity, account);
-        }
+        List<HostVO> hostsMatchingUefiTag = hostDao.listByHostCapability(type, 
clusterId, podId, dcId, Host.HOST_UEFI_ENABLE);
 
-        return suitableHosts;
+        logger.debug("Hosts with UEFI enabled are {}.", hostsMatchingUefiTag);
+        clusterHosts.retainAll(hostsMatchingUefiTag);
     }
 
     protected List<Host> allocateTo(DeploymentPlan plan, ServiceOffering 
offering, VMTemplateVO template, ExcludeList avoid, List<? extends Host> hosts, 
int returnUpTo,
-        boolean considerReservedCapacity, Account account) {
+                                    boolean considerReservedCapacity, Account 
account) {
+
         String vmAllocationAlgorithm = 
DeploymentClusterPlanner.VmAllocationAlgorithm.value();
-        if (vmAllocationAlgorithm.equals("random") || 
vmAllocationAlgorithm.equals("userconcentratedpod_random")) {
-            // Shuffle this so that we don't check the hosts in the same order.
+        if (List.of(random.toString(), 
userconcentratedpod_random.toString()).contains(vmAllocationAlgorithm)) {
             Collections.shuffle(hosts);
-        } else if (vmAllocationAlgorithm.equals("userdispersing")) {
+        } else if (userdispersing.toString().equals(vmAllocationAlgorithm)) {
             hosts = reorderHostsByNumberOfVms(plan, hosts, account);
-        } else if(vmAllocationAlgorithm.equals("firstfitleastconsumed")){
+        } else if 
(firstfitleastconsumed.toString().equals(vmAllocationAlgorithm)) {
             hosts = reorderHostsByCapacity(plan, hosts);
         }
 
-        if (logger.isDebugEnabled()) {
-            logger.debug("FirstFitAllocator has " + hosts.size() + " hosts to 
check for allocation: " + hosts);
-        }
-
-        // We will try to reorder the host lists such that we give priority to 
hosts that have
-        // the minimums to support a VM's requirements
+        logger.debug("FirstFitAllocator has {} hosts to check for allocation 
{}.", hosts.size(), hosts);
         hosts = prioritizeHosts(template, offering, hosts);
+        logger.debug("Found {} hosts for allocation after prioritization: 
{}.", hosts.size(), hosts);
 
-        if (logger.isDebugEnabled()) {
-            logger.debug("Found " + hosts.size() + " hosts for allocation 
after prioritization: " + hosts);
-        }
+        List<Host> suitableHosts = checkHostsCompatibilities(offering, avoid, 
hosts, returnUpTo, considerReservedCapacity);
+        logger.debug("Host Allocator returning {} suitable hosts.", 
suitableHosts.size());
 
-        if (logger.isDebugEnabled()) {
-            logger.debug("Looking for speed=" + (offering.getCpu() * 
offering.getSpeed()) + "Mhz, Ram=" + offering.getRamSize() + " MB");
-        }
+        return suitableHosts;
+    }
 
-        long serviceOfferingId = offering.getId();
+    protected List<Host> checkHostsCompatibilities(ServiceOffering offering, 
ExcludeList avoid, List<? extends Host> hosts, int returnUpTo, boolean 
considerReservedCapacity) {
         List<Host> suitableHosts = new ArrayList<>();
-        ServiceOfferingDetailsVO offeringDetails = null;
+        logger.debug("Checking compatibility for the following hosts {}.", 
suitableHosts);
 
         for (Host host : hosts) {
             if (suitableHosts.size() == returnUpTo) {
                 break;
             }
+
             if (avoid.shouldAvoid(host)) {
-                if (logger.isDebugEnabled()) {
-                    logger.debug("Host: {} is in avoid set, skipping this and 
trying other available hosts", host);
-                }
+                logger.debug("Host [{}] is in avoid set, skipping this and 
trying other available hosts", () -> host);
                 continue;
             }
 
-            //find number of guest VMs occupying capacity on this host.
-            if (_capacityMgr.checkIfHostReachMaxGuestLimit(host)) {
-                logger.debug("Adding host [{}] to the avoid set because this 
host already has the max number of running (user and/or system) VMs.", host);
+            if (capacityManager.checkIfHostReachMaxGuestLimit(host)) {
+                logger.debug("Adding host [{}] to the avoid set because this 
host already has the max number of running (user and/or system) VMs.", () ->  
host);

Review Comment:
   ```suggestion
                   logger.debug("Adding host [{}] to the avoid set because this 
host already has the max number of running (user and/or system) VMs.", host);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to