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
Original file line number Diff line number Diff line change
Expand Up @@ -1208,10 +1208,16 @@ private void processWork(final HaWorkVO work) {
rescheduleWork(work, nextTime);

// if restart failed in the middle due to exception, VM state may has been changed
// recapture into the HA worker so that it can really continue in it next turn
// recapture into the HA worker so that it can really continue in it next turn.
// The VM must be read again: the failed attempt has moved its state and update
// count, and a stale copy would make the next attempt see the VM as "changed"
// and close the work without restarting it.
if (vm != null) {
work.setUpdateTime(vm.getUpdated());
work.setPreviousState(vm.getState());
final VMInstanceVO reloadedVm = _instanceDao.findById(vm.getId());
if (reloadedVm != null) {
work.setUpdateTime(reloadedVm.getUpdated());
work.setPreviousState(reloadedVm.getState());
}
}
} finally {
if (!Step.Done.equals(work.getStep())) {
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 Down Expand Up @@ -75,6 +76,7 @@
import com.cloud.user.AccountManager;
import com.cloud.vm.UserVmManager;
import com.cloud.vm.VMInstanceVO;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineManager;
import com.cloud.vm.dao.VMInstanceDao;
Expand Down Expand Up @@ -426,6 +428,32 @@ private void processWorkWithRetryCount(int count, Step expectedStep) {
assertTrue(work.getStep() == expectedStep);
}

@Test
public void processWorkRecordsCurrentVmStateWhenRestartThrows() throws Exception {
assertNotNull(processWorkMethod);
HaWorkVO work = new HaWorkVO(1L, VirtualMachine.Type.User, WorkType.HA, Step.Scheduled, 1L, VirtualMachine.State.Running, 0, 17L, null);
Field workIdField = HaWorkVO.class.getDeclaredField("id");
workIdField.setAccessible(true);
workIdField.set(work, 1L);
ConfigKey<Boolean> haEnabled = Mockito.mock(ConfigKey.class);
highAvailabilityManager.VmHaEnabled = haEnabled;
Mockito.when(highAvailabilityManager.VmHaEnabled.valueIn(1L)).thenReturn(true);
VMInstanceVO vmBeforeAttempt = Mockito.mock(VMInstanceVO.class);
Mockito.when(vmBeforeAttempt.getId()).thenReturn(1L);
Mockito.when(vmBeforeAttempt.getDataCenterId()).thenReturn(1L);
VMInstanceVO vmAfterAttempt = Mockito.mock(VMInstanceVO.class);
Mockito.when(vmAfterAttempt.getUpdated()).thenReturn(20L);
Mockito.when(vmAfterAttempt.getState()).thenReturn(VirtualMachine.State.Stopped);
Mockito.when(_instanceDao.findById(1L)).thenReturn(vmBeforeAttempt, vmAfterAttempt);
Mockito.doThrow(new CloudRuntimeException("failed to start")).when(highAvailabilityManagerSpy).restart(work);

processWorkMethod.invoke(highAvailabilityManagerSpy, work);

assertEquals(Step.Scheduled, work.getStep());
assertEquals(20L, work.getUpdateTime());
assertEquals(VirtualMachine.State.Stopped, work.getPreviousState());
}

@Test
public void processWorkWithRetryCountExceeded() {
processWorkWithRetryCount(5, Step.Done); // max retry count is 5
Expand Down
Loading