diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index 464d3711b72..bc329d9043e 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -378,31 +378,11 @@ StateListener, Configurable { planner = getDeploymentPlannerByName(plannerName); } - Host lastHost = null; - - String considerLastHostStr = (String)vmProfile.getParameter(VirtualMachineProfile.Param.ConsiderLastHost); - boolean considerLastHost = vm.getLastHostId() != null && haVmTag == null && - (considerLastHostStr == null || Boolean.TRUE.toString().equalsIgnoreCase(considerLastHostStr)); - if (considerLastHost) { - logger.debug("This VM has last host_id: {}", vm.getLastHostId()); - HostVO host = _hostDao.findById(vm.getLastHostId()); - if (host == null) { - if (Boolean.TRUE.toString().equalsIgnoreCase(considerLastHostStr)) { - throw new CloudRuntimeException(String.format("Failed to deploy VM %s, last host doesn't exist", vm.getName())); - } - } else { - logger.debug("VM's last host is {}, trying to choose the same host if it is not in maintenance state", host); - if (host.isInMaintenanceStates()) { - if (Boolean.TRUE.toString().equalsIgnoreCase(considerLastHostStr)) { - throw new CloudRuntimeException(String.format("Failed to deploy VM %s, last host %s is in maintenance state", vm.getName(), host.getName())); - } - } else { - lastHost = host; - DeployDestination deployDestination = deployInVmLastHost(vmProfile, plan, avoids, planner, vm, dc, offering, cpuRequested, ramRequested, volumesRequireEncryption); - if (deployDestination != null) { - return deployDestination; - } - } + Host lastHost = checkDeployInVmLastHost(vmProfile, vm); + if (lastHost != null) { + DeployDestination deployDestination = deployInVmLastHost(vmProfile, plan, avoids, planner, vm, dc, offering, cpuRequested, ramRequested, volumesRequireEncryption); + if (deployDestination != null) { + return deployDestination; } } @@ -468,6 +448,31 @@ StateListener, Configurable { return dest; } + private Host checkDeployInVmLastHost(VirtualMachineProfile vmProfile, VirtualMachine vm) { + String considerLastHostStr = (String)vmProfile.getParameter(VirtualMachineProfile.Param.ConsiderLastHost); + String haVmTag = (String)vmProfile.getParameter(VirtualMachineProfile.Param.HaTag); + boolean considerLastHost = vm.getLastHostId() != null && haVmTag == null && + (considerLastHostStr == null || Boolean.TRUE.toString().equalsIgnoreCase(considerLastHostStr)); + if (!considerLastHost) { + return null; + } + + logger.debug("This VM has last host_id: {}", vm.getLastHostId()); + HostVO lastHost = _hostDao.findById(vm.getLastHostId()); + if (lastHost == null) { + logger.debug("Unable to deploy VM {} in the last host, last host doesn't exist", vm.getName()); + return null; + } + + logger.debug("VM's last host is {}, trying to choose the same host if it is not in maintenance state", lastHost); + if (lastHost.isInMaintenanceStates()) { + logger.debug("Unable to deploy VM {} in the last host, last host {} is in maintenance state", vm.getName(), lastHost.getName()); + return null; + } + + return lastHost; + } + private void avoidDifferentArchResources(VirtualMachineProfile vmProfile, DataCenter dc, ExcludeList avoids) { VirtualMachineTemplate template = vmProfile.getTemplate(); for (CPU.CPUArch arch : clusterArchTypes) {