diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index f163a3d52a5a..848d2bfb346c 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -468,14 +468,26 @@ private void avoidDifferentArchResources(VirtualMachineProfile vmProfile, DataCe } } + /** + * Whether to try the VM's last host before running the planner. + * Not for a migration plan, i.e. a VM moved off a host that is being evacuated (host maintenance, HA): each + * migration records its source host as the VM's last host, so preferring it sends VMs back to the hosts + * evacuated earlier, for example during a rolling maintenance, and skips the planner and the VM allocation + * algorithm. + */ + protected boolean shouldConsiderLastHost(VirtualMachineProfile vmProfile, DeploymentPlan plan, VirtualMachine vm) { + if (vm.getLastHostId() == null || plan.isMigrationPlan()) { + return false; + } + String considerLastHostStr = (String)vmProfile.getParameter(VirtualMachineProfile.Param.ConsiderLastHost); + String haVmTag = (String)vmProfile.getParameter(VirtualMachineProfile.Param.HaTag); + return haVmTag == null && !(Boolean.FALSE.toString().equalsIgnoreCase(considerLastHostStr)); + } + private DeployDestination deployInVmLastHost(VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoids, DeploymentPlanner planner, VirtualMachine vm, DataCenter dc, ServiceOffering offering, int cpuRequested, long ramRequested, boolean volumesRequireEncryption) throws InsufficientServerCapacityException { - String considerLastHostStr = (String)vmProfile.getParameter(VirtualMachineProfile.Param.ConsiderLastHost); - String haVmTag = (String)vmProfile.getParameter(VirtualMachineProfile.Param.HaTag); - boolean considerLastHost = vm.getLastHostId() != null && haVmTag == null && - !(Boolean.FALSE.toString().equalsIgnoreCase(considerLastHostStr)); - if (!considerLastHost) { + if (!shouldConsiderLastHost(vmProfile, plan, vm)) { return null; } diff --git a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java index ace043bcc526..7b51d5dc7992 100644 --- a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java +++ b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java @@ -1281,4 +1281,31 @@ public void avoidOtherClustersForDeploymentIfMigrationDisabledValid() { Assert.assertEquals(allClusters.size() - 1, avoidedClusters.size()); Assert.assertFalse(avoidedClusters.contains(allClusters.get(0))); } + + private boolean shouldConsiderLastHost(Long lastHostId, boolean migrationPlan, Map params) { + VMInstanceVO lastHostVm = Mockito.mock(VMInstanceVO.class); + Mockito.when(lastHostVm.getLastHostId()).thenReturn(lastHostId); + Mockito.lenient().when(lastHostVm.getType()).thenReturn(Type.User); + DataCenterDeployment deployment = new DataCenterDeployment(1L, 1L, 1L, null, null, null); + deployment.setMigrationPlan(migrationPlan); + return _dpm.shouldConsiderLastHost(new VirtualMachineProfileImpl(lastHostVm, null, null, null, params), deployment, lastHostVm); + } + + @Test + public void shouldConsiderLastHostForAStart() { + Assert.assertTrue(shouldConsiderLastHost(5L, false, null)); + } + + @Test + public void shouldNotConsiderLastHostForAMigrationPlan() { + Assert.assertFalse(shouldConsiderLastHost(5L, true, null)); + Assert.assertFalse(shouldConsiderLastHost(5L, true, new HashMap<>(Map.of(VirtualMachineProfile.Param.ConsiderLastHost, Boolean.TRUE.toString())))); + } + + @Test + public void shouldNotConsiderLastHostWithoutOneOrWhenTurnedOff() { + Assert.assertFalse(shouldConsiderLastHost(null, false, null)); + Assert.assertFalse(shouldConsiderLastHost(5L, false, new HashMap<>(Map.of(VirtualMachineProfile.Param.ConsiderLastHost, Boolean.FALSE.toString())))); + Assert.assertFalse(shouldConsiderLastHost(5L, false, new HashMap<>(Map.of(VirtualMachineProfile.Param.HaTag, "ha")))); + } }