Skip to content

Commit cd39b9e

Browse files
authored
clean up the first try when a system vm retries with another template (#14376)
1 parent 1b20951 commit cd39b9e

5 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎engine/api/src/main/java/com/cloud/vm/VirtualMachineManager.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ void checkDeploymentPlan(VirtualMachine virtualMachine, VirtualMachineTemplate t
320320
ServiceOffering serviceOffering, Account systemAccount, DeploymentPlan plan)
321321
throws InsufficientServerCapacityException;
322322

323+
/**
324+
* Removes the volumes and NICs allocated for a VM, so it can be allocated again with another template.
325+
*/
326+
void cleanupAllocatedResources(VirtualMachine vm);
327+
323328
boolean isBlankInstanceDefaultTemplate(VirtualMachineTemplate template);
324329

325330
boolean isBlankInstance(VirtualMachineTemplate template);

‎engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6898,6 +6898,13 @@ public void checkDeploymentPlan(VirtualMachine virtualMachine, VirtualMachineTem
68986898
}
68996899
}
69006900

6901+
@Override
6902+
public void cleanupAllocatedResources(VirtualMachine vm) {
6903+
logger.debug("Cleaning up the volumes and NICs allocated for {}", vm);
6904+
volumeMgr.cleanupVolumes(vm.getId());
6905+
_networkMgr.cleanupNics(new VirtualMachineProfileImpl(vm));
6906+
}
6907+
69016908
@Override
69026909
public boolean isBlankInstanceDefaultTemplate(VirtualMachineTemplate template) {
69036910
return KVM_BLANK_VM_TEMPLATE_NAME.equals(template.getUniqueName());

‎engine/orchestration/src/test/java/com/cloud/vm/VirtualMachineManagerImplTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
import com.cloud.storage.clvm.ClvmPoolManager;
6666
import org.apache.cloudstack.api.ApiConstants;
6767
import org.apache.cloudstack.context.CallContext;
68+
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
69+
import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
6870
import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator;
6971
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
7072
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
@@ -255,6 +257,10 @@ public class VirtualMachineManagerImplTest {
255257
@Mock
256258
private NetworkDao networkDao;
257259
@Mock
260+
private VolumeOrchestrationService volumeOrchestrationServiceMock;
261+
@Mock
262+
private NetworkOrchestrationService networkOrchestrationServiceMock;
263+
@Mock
258264
private AccountDao accountDao;
259265
@Mock
260266
private DomainDao domainDao;
@@ -2398,4 +2404,13 @@ public void testUpdateClvmLockHostForVmVolumes_MultipleClvmPools() throws Except
23982404
verify(clvmPoolManagerMock, times(1)).setClvmLockHostId(3L, destHostId);
23992405
}
24002406

2407+
@Test
2408+
public void cleanupAllocatedResourcesRemovesVolumesAndNics() {
2409+
when(vmInstanceMock.getId()).thenReturn(1L);
2410+
2411+
virtualMachineManagerImpl.cleanupAllocatedResources(vmInstanceMock);
2412+
2413+
verify(volumeOrchestrationServiceMock).cleanupVolumes(1L);
2414+
verify(networkOrchestrationServiceMock).cleanupNics(Mockito.argThat(profile -> profile.getVirtualMachine() == vmInstanceMock));
2415+
}
24012416
}

‎server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ protected Map<String, Object> createProxyInstance(long dataCenterId, List<VMTemp
731731
} catch (InsufficientCapacityException e) {
732732
if (templateIterator.hasNext()) {
733733
logger.debug("Unable to allocate proxy {} with {} in {} due to [{}]. Retrying with another template", proxy, template, dc, e.getMessage(), e);
734+
virtualMachineManager.cleanupAllocatedResources(proxy);
734735
continue;
735736
}
736737
throw new CloudRuntimeException(String.format("Failed to allocate proxy [%s] in zone [%s] with available templates", proxy, dc), e);

‎services/secondary-storage/controller/src/main/java/org/apache/cloudstack/secondarystorage/SecondaryStorageManagerImpl.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ protected Map<String, Object> createSecStorageVmInstance(long dataCenterId, Seco
702702
} catch (InsufficientCapacityException e) {
703703
if (templateIterator.hasNext()) {
704704
logger.debug("Unable to allocate secondary storage {} with {} due to [{}]. Retrying with another template", secStorageVm, template, e.getMessage(), e);
705+
_itMgr.cleanupAllocatedResources(secStorageVm);
705706
continue;
706707
}
707708
throw new CloudRuntimeException("Failed to allocate secondary storage VM [%s] in zone [%s] with available templates", e);

0 commit comments

Comments
 (0)