From 0a6f1b7afc0a935cba4e9f0880d4563e8b2914a0 Mon Sep 17 00:00:00 2001 From: James Peru Date: Mon, 30 Nov 2026 08:13:07 +0300 Subject: [PATCH] server: reload the VM before recording its state when HA work fails When an HA attempt ends with an unchecked exception, processWork() stored the update count and state of the VM copy that was read before the attempt. The attempt has already changed the VM, so the next attempt found a different update count, treated the VM as changed by someone else and closed the work as done without restarting the VM. Read the VM again before recording its update count and state. Fixes: #14840 --- .../cloud/ha/HighAvailabilityManagerImpl.java | 12 ++++++-- .../ha/HighAvailabilityManagerImplTest.java | 28 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/com/cloud/ha/HighAvailabilityManagerImpl.java b/server/src/main/java/com/cloud/ha/HighAvailabilityManagerImpl.java index 07d51c5deb30..f85d33b94a4d 100644 --- a/server/src/main/java/com/cloud/ha/HighAvailabilityManagerImpl.java +++ b/server/src/main/java/com/cloud/ha/HighAvailabilityManagerImpl.java @@ -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())) { diff --git a/server/src/test/java/com/cloud/ha/HighAvailabilityManagerImplTest.java b/server/src/test/java/com/cloud/ha/HighAvailabilityManagerImplTest.java index 1fe4263afc59..893207fa0564 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; @@ -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; @@ -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 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