Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions server/src/main/java/com/cloud/ha/HighAvailabilityManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ public class HighAvailabilityManagerImpl extends ManagerBase implements Configur
protected static ConfigKey<Boolean> 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<Boolean> 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<ReasonType> CancellableWorkReasonTypes =
Arrays.asList(ReasonType.HostMaintenance, ReasonType.HostDown, ReasonType.HostDegraded);

Expand Down Expand Up @@ -616,6 +625,24 @@ private void startVm(VirtualMachine vm, Map<VirtualMachineProfile.Param, Object>
}
}

/**
* 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<VirtualMachineProfile.Param, Object> getRestartParams(final WorkType workType, final Long clusterId) {
Map<VirtualMachineProfile.Param, Object> 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<HaWorkVO> items = _haDao.listFutureHaWorkForVm(work.getInstanceId(), work.getId());
Expand Down Expand Up @@ -791,14 +818,7 @@ protected Long restart(final HaWorkVO work) {
}

try {
HashMap<VirtualMachineProfile.Param, Object> params = new HashMap<VirtualMachineProfile.Param, Object>();
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<VirtualMachineProfile.Param, Object> params = getRestartParams(work.getWorkType(), host.getClusterId());

try{
if (HypervisorType.KVM == host.getHypervisorType()) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +29,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -484,4 +487,36 @@ public void testCheckAndCancelWorkIfNeeded_HostStatusNotUp() {
assertFalse(result);
Mockito.verify(mockWork, Mockito.never()).setStep(Mockito.any());
}

private Map<VirtualMachineProfile.Param, Object> getRestartParams(boolean considerLastHost, WorkType workType) {
ConfigKey<Boolean> original = HighAvailabilityManagerImpl.VmHaRestartConsiderLastHost;
ConfigKey<Boolean> 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<VirtualMachineProfile.Param, Object> params = getRestartParams(true, WorkType.HA);
assertFalse(params.containsKey(VirtualMachineProfile.Param.ConsiderLastHost));
assertEquals(true, params.get(VirtualMachineProfile.Param.HaOperation));
}

@Test
public void getRestartParamsSkipsTheLastHostWhenTurnedOff() {
Map<VirtualMachineProfile.Param, Object> 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<VirtualMachineProfile.Param, Object> params = getRestartParams(true, WorkType.Migration);
assertFalse(params.containsKey(VirtualMachineProfile.Param.HaOperation));
}
}
Loading