From 3ba3731efcb226a1fc332f38b34aa657f9e5e077 Mon Sep 17 00:00:00 2001 From: Brad House Date: Sat, 21 Nov 2026 20:59:53 +0000 Subject: [PATCH] Don't send an evacuated VM back to its last host When a host is put into maintenance, or HA moves its VMs away, each VM went back to its last host whenever that host was Up and had capacity. That skipped the planner and the VM allocation algorithm. Every migration records its source as the VM's last host, so during a rolling maintenance the VMs piled up on the hosts evacuated earlier. With memory overcommit, those hosts could take far more VM memory than they have. deployInVmLastHost() no longer tries the last host for a migration plan. Only the two host-evacuation paths mark a plan as one: VirtualMachineManagerImpl.orchestrateMigrateAway() and ResourceManagerImpl.migrateAwayVmWithVolumes(). Starting a stopped VM still prefers its last host. Signed-off-by: Brad House --- .../deploy/DeploymentPlanningManagerImpl.java | 22 +++++++++++---- .../DeploymentPlanningManagerImplTest.java | 27 +++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) 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")))); + } }