diff --git a/server/src/main/java/com/cloud/ha/HighAvailabilityManagerImpl.java b/server/src/main/java/com/cloud/ha/HighAvailabilityManagerImpl.java index 07d51c5deb30..96f0a4c06f23 100644 --- a/server/src/main/java/com/cloud/ha/HighAvailabilityManagerImpl.java +++ b/server/src/main/java/com/cloud/ha/HighAvailabilityManagerImpl.java @@ -145,6 +145,15 @@ public class HighAvailabilityManagerImpl extends ManagerBase implements Configur protected static ConfigKey VmHaAlertsEnabled = new ConfigKey<>("Advanced", Boolean.class, "vm.ha.alerts.enabled", "true", "Enable/Disable alerts for the VM HA operations, it is enabled by default.", true, Zone); + protected static ConfigKey VmHaRestartConsiderLastHost = new ConfigKey<>("Advanced", Boolean.class, + "vm.ha.restart.consider.last.host", "true", + "Whether the HA restart of a VM first tries the host the VM last ran on, when that host is Up and has capacity. " + + "When false, the last host is not preferred: the deployment planner and vm.allocation.algorithm choose the host, " + + "and may still choose the last host. Useful when a host stops VMs itself, for example killed for lack of memory. " + + "The value of the cluster of the host the VM ran on applies. Has no effect when ha.tag is set, as the last host " + + "is then never preferred.", + true, ConfigKey.Scope.Cluster); + protected static final List CancellableWorkReasonTypes = Arrays.asList(ReasonType.HostMaintenance, ReasonType.HostDown, ReasonType.HostDegraded); @@ -616,6 +625,24 @@ private void startVm(VirtualMachine vm, Map } } + /** + * Parameters for starting a VM in an HA restart. The last host is skipped when + * vm.ha.restart.consider.last.host is false in the cluster of the host the VM ran on. + */ + protected Map getRestartParams(final WorkType workType, final Long clusterId) { + Map params = new HashMap<>(); + if (_haTag != null) { + params.put(VirtualMachineProfile.Param.HaTag, _haTag); + } + if (WorkType.HA.equals(workType)) { + params.put(VirtualMachineProfile.Param.HaOperation, true); + } + if (!VmHaRestartConsiderLastHost.valueIn(clusterId)) { + params.put(VirtualMachineProfile.Param.ConsiderLastHost, Boolean.FALSE.toString()); + } + return params; + } + protected Long restart(final HaWorkVO work) { logger.debug("RESTART with HA WORK"); List items = _haDao.listFutureHaWorkForVm(work.getInstanceId(), work.getId()); @@ -791,14 +818,7 @@ protected Long restart(final HaWorkVO work) { } try { - HashMap params = new HashMap(); - if (_haTag != null) { - params.put(VirtualMachineProfile.Param.HaTag, _haTag); - } - WorkType wt = work.getWorkType(); - if (wt.equals(WorkType.HA)) { - params.put(VirtualMachineProfile.Param.HaOperation, true); - } + Map params = getRestartParams(work.getWorkType(), host.getClusterId()); try{ if (HypervisorType.KVM == host.getHypervisorType()) { @@ -1430,7 +1450,8 @@ public String getConfigComponentName() { public ConfigKey[] getConfigKeys() { return new ConfigKey[] {TimeBetweenCleanup, MigrationMaxRetries, TimeToSleep, TimeBetweenFailures, StopRetryInterval, RestartRetryInterval, MigrateRetryInterval, InvestigateRetryInterval, - HAWorkers, ForceHA, VmHaEnabled, VmHaAlertsEnabled, KvmHAFenceHostIfHeartbeatFailsOnStorage}; + HAWorkers, ForceHA, VmHaEnabled, VmHaAlertsEnabled, KvmHAFenceHostIfHeartbeatFailsOnStorage, + VmHaRestartConsiderLastHost}; } @Override diff --git a/server/src/test/java/com/cloud/ha/HighAvailabilityManagerImplTest.java b/server/src/test/java/com/cloud/ha/HighAvailabilityManagerImplTest.java index 1fe4263afc59..6b0ac257e013 100644 --- a/server/src/test/java/com/cloud/ha/HighAvailabilityManagerImplTest.java +++ b/server/src/test/java/com/cloud/ha/HighAvailabilityManagerImplTest.java @@ -16,6 +16,7 @@ // under the License. package com.cloud.ha; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -28,6 +29,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import javax.inject.Inject; @@ -77,6 +79,7 @@ import com.cloud.vm.VMInstanceVO; import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachineManager; +import com.cloud.vm.VirtualMachineProfile; import com.cloud.vm.dao.VMInstanceDao; import org.springframework.test.util.ReflectionTestUtils; @@ -484,4 +487,36 @@ public void testCheckAndCancelWorkIfNeeded_HostStatusNotUp() { assertFalse(result); Mockito.verify(mockWork, Mockito.never()).setStep(Mockito.any()); } + + private Map getRestartParams(boolean considerLastHost, WorkType workType) { + ConfigKey original = HighAvailabilityManagerImpl.VmHaRestartConsiderLastHost; + ConfigKey considerLastHostKey = Mockito.mock(ConfigKey.class); + Mockito.when(considerLastHostKey.valueIn(7L)).thenReturn(considerLastHost); + HighAvailabilityManagerImpl.VmHaRestartConsiderLastHost = considerLastHostKey; + try { + return highAvailabilityManager.getRestartParams(workType, 7L); + } finally { + HighAvailabilityManagerImpl.VmHaRestartConsiderLastHost = original; + } + } + + @Test + public void getRestartParamsKeepsTheLastHostByDefault() { + Map params = getRestartParams(true, WorkType.HA); + assertFalse(params.containsKey(VirtualMachineProfile.Param.ConsiderLastHost)); + assertEquals(true, params.get(VirtualMachineProfile.Param.HaOperation)); + } + + @Test + public void getRestartParamsSkipsTheLastHostWhenTurnedOff() { + Map params = getRestartParams(false, WorkType.HA); + assertEquals(Boolean.FALSE.toString(), params.get(VirtualMachineProfile.Param.ConsiderLastHost)); + assertEquals(true, params.get(VirtualMachineProfile.Param.HaOperation)); + } + + @Test + public void getRestartParamsSetsHaOperationOnlyForHaWork() { + Map params = getRestartParams(true, WorkType.Migration); + assertFalse(params.containsKey(VirtualMachineProfile.Param.HaOperation)); + } }