This is an automated email from the ASF dual-hosted git repository.

RosiKyu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/main by this push:
     new cd39b9e4892 clean up the first try when a system vm retries with 
another template (#14376)
cd39b9e4892 is described below

commit cd39b9e48924c9f9feccf2a618a533df1ace1099
Author: Daman Arora <[email protected]>
AuthorDate: Thu Nov 12 08:14:22 2026 -0500

    clean up the first try when a system vm retries with another template 
(#14376)
---
 .../src/main/java/com/cloud/vm/VirtualMachineManager.java |  5 +++++
 .../main/java/com/cloud/vm/VirtualMachineManagerImpl.java |  7 +++++++
 .../java/com/cloud/vm/VirtualMachineManagerImplTest.java  | 15 +++++++++++++++
 .../com/cloud/consoleproxy/ConsoleProxyManagerImpl.java   |  1 +
 .../secondarystorage/SecondaryStorageManagerImpl.java     |  1 +
 5 files changed, 29 insertions(+)

diff --git a/engine/api/src/main/java/com/cloud/vm/VirtualMachineManager.java 
b/engine/api/src/main/java/com/cloud/vm/VirtualMachineManager.java
index 77f3379ee70..9ae4c3048d0 100644
--- a/engine/api/src/main/java/com/cloud/vm/VirtualMachineManager.java
+++ b/engine/api/src/main/java/com/cloud/vm/VirtualMachineManager.java
@@ -320,6 +320,11 @@ public interface VirtualMachineManager extends Manager {
                 ServiceOffering serviceOffering, Account systemAccount, 
DeploymentPlan plan)
             throws InsufficientServerCapacityException;
 
+    /**
+     * Removes the volumes and NICs allocated for a VM, so it can be allocated 
again with another template.
+     */
+    void cleanupAllocatedResources(VirtualMachine vm);
+
     boolean isBlankInstanceDefaultTemplate(VirtualMachineTemplate template);
 
     boolean isBlankInstance(VirtualMachineTemplate template);
diff --git 
a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java
 
b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java
index a514fddce05..cd5cac7228e 100755
--- 
a/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java
+++ 
b/engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java
@@ -6898,6 +6898,13 @@ public class VirtualMachineManagerImpl extends 
ManagerBase implements VirtualMac
         }
     }
 
+    @Override
+    public void cleanupAllocatedResources(VirtualMachine vm) {
+        logger.debug("Cleaning up the volumes and NICs allocated for {}", vm);
+        volumeMgr.cleanupVolumes(vm.getId());
+        _networkMgr.cleanupNics(new VirtualMachineProfileImpl(vm));
+    }
+
     @Override
     public boolean isBlankInstanceDefaultTemplate(VirtualMachineTemplate 
template) {
         return KVM_BLANK_VM_TEMPLATE_NAME.equals(template.getUniqueName());
diff --git 
a/engine/orchestration/src/test/java/com/cloud/vm/VirtualMachineManagerImplTest.java
 
b/engine/orchestration/src/test/java/com/cloud/vm/VirtualMachineManagerImplTest.java
index fbd535c33d4..fe51440bd81 100644
--- 
a/engine/orchestration/src/test/java/com/cloud/vm/VirtualMachineManagerImplTest.java
+++ 
b/engine/orchestration/src/test/java/com/cloud/vm/VirtualMachineManagerImplTest.java
@@ -65,6 +65,8 @@ import com.cloud.resource.ResourceManager;
 import com.cloud.storage.clvm.ClvmPoolManager;
 import org.apache.cloudstack.api.ApiConstants;
 import org.apache.cloudstack.context.CallContext;
+import 
org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
+import 
org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
 import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator;
 import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
 import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
@@ -255,6 +257,10 @@ public class VirtualMachineManagerImplTest {
     @Mock
     private NetworkDao networkDao;
     @Mock
+    private VolumeOrchestrationService volumeOrchestrationServiceMock;
+    @Mock
+    private NetworkOrchestrationService networkOrchestrationServiceMock;
+    @Mock
     private AccountDao accountDao;
     @Mock
     private DomainDao domainDao;
@@ -2398,4 +2404,13 @@ public class VirtualMachineManagerImplTest {
         verify(clvmPoolManagerMock, times(1)).setClvmLockHostId(3L, 
destHostId);
     }
 
+    @Test
+    public void cleanupAllocatedResourcesRemovesVolumesAndNics() {
+        when(vmInstanceMock.getId()).thenReturn(1L);
+
+        virtualMachineManagerImpl.cleanupAllocatedResources(vmInstanceMock);
+
+        verify(volumeOrchestrationServiceMock).cleanupVolumes(1L);
+        
verify(networkOrchestrationServiceMock).cleanupNics(Mockito.argThat(profile -> 
profile.getVirtualMachine() == vmInstanceMock));
+    }
 }
diff --git 
a/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java 
b/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
index 4401dc2d152..289964f078a 100644
--- a/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
+++ b/server/src/main/java/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
@@ -731,6 +731,7 @@ public class ConsoleProxyManagerImpl extends ManagerBase 
implements ConsoleProxy
             } catch (InsufficientCapacityException e) {
                 if (templateIterator.hasNext()) {
                     logger.debug("Unable to allocate proxy {} with {} in {} 
due to [{}]. Retrying with another template", proxy, template, dc, 
e.getMessage(), e);
+                    virtualMachineManager.cleanupAllocatedResources(proxy);
                     continue;
                 }
                 throw new CloudRuntimeException(String.format("Failed to 
allocate proxy [%s] in zone [%s] with available templates", proxy, dc), e);
diff --git 
a/services/secondary-storage/controller/src/main/java/org/apache/cloudstack/secondarystorage/SecondaryStorageManagerImpl.java
 
b/services/secondary-storage/controller/src/main/java/org/apache/cloudstack/secondarystorage/SecondaryStorageManagerImpl.java
index 9d4c7311159..081c89ea8f9 100644
--- 
a/services/secondary-storage/controller/src/main/java/org/apache/cloudstack/secondarystorage/SecondaryStorageManagerImpl.java
+++ 
b/services/secondary-storage/controller/src/main/java/org/apache/cloudstack/secondarystorage/SecondaryStorageManagerImpl.java
@@ -702,6 +702,7 @@ public class SecondaryStorageManagerImpl extends 
ManagerBase implements Secondar
             } catch (InsufficientCapacityException e) {
                 if (templateIterator.hasNext()) {
                     logger.debug("Unable to allocate secondary storage {} with 
{} due to [{}]. Retrying with another template", secStorageVm, template, 
e.getMessage(), e);
+                    _itMgr.cleanupAllocatedResources(secStorageVm);
                     continue;
                 }
                 throw new CloudRuntimeException("Failed to allocate secondary 
storage VM [%s] in zone [%s] with available templates", e);

Reply via email to