Skip to content

Commit a2a7f5d

Browse files
committed
fix systemvm deployment retry
Check plan for a created instance so retry can be done if it fails for a particular template Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 59f2608 commit a2a7f5d

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

engine/schema/src/main/java/com/cloud/storage/dao/VMTemplateDaoImpl.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Collections;
21+
import java.util.Comparator;
2122
import java.util.Date;
2223
import java.util.HashMap;
2324
import java.util.List;
2425
import java.util.Map;
25-
import java.util.stream.Collectors;
2626

2727
import javax.inject.Inject;
2828
import javax.naming.ConfigurationException;
@@ -595,10 +595,10 @@ private List<VMTemplateVO> listAllReadySystemVMTemplatesWithArch(Long zoneId, Hy
595595
sc.setParameters("templateType", Storage.TemplateType.SYSTEM);
596596
sc.setParameters("state", VirtualMachineTemplate.State.Active);
597597
if (hypervisorType != null && !HypervisorType.Any.equals(hypervisorType)) {
598-
sc.setParameters("hypervisorType", List.of(hypervisorType));
598+
sc.setParameters("hypervisorType", List.of(hypervisorType).toArray());
599599
} else {
600600
sc.setParameters("hypervisorType",
601-
availableHypervisors.stream().map(Pair::first).collect(Collectors.toList()));
601+
availableHypervisors.stream().map(Pair::first).distinct().toArray());
602602
}
603603
sc.setJoinParameters("vmTemplateJoinTemplateStoreRef", "downloadState",
604604
List.of(VMTemplateStorageResourceAssoc.Status.DOWNLOADED,
@@ -612,7 +612,9 @@ private List<VMTemplateVO> listAllReadySystemVMTemplatesWithArch(Long zoneId, Hy
612612
uniqueTemplates.put(key, template);
613613
}
614614
}
615-
return new ArrayList<>(uniqueTemplates.values());
615+
List<VMTemplateVO> result = new ArrayList<>(uniqueTemplates.values());
616+
result.sort(Comparator.comparing(VMTemplateVO::getId).reversed());
617+
return result;
616618
}
617619

618620
@Override

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

+14-11
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import com.cloud.deploy.DataCenterDeployment;
7575
import com.cloud.deploy.DeployDestination;
7676
import com.cloud.deploy.DeploymentPlanner;
77+
import com.cloud.deploy.DeploymentPlanningManager;
7778
import com.cloud.event.ActionEvent;
7879
import com.cloud.event.EventTypes;
7980
import com.cloud.exception.ConcurrentOperationException;
@@ -145,6 +146,7 @@
145146
import com.cloud.vm.VirtualMachineManager;
146147
import com.cloud.vm.VirtualMachineName;
147148
import com.cloud.vm.VirtualMachineProfile;
149+
import com.cloud.vm.VirtualMachineProfileImpl;
148150
import com.cloud.vm.dao.ConsoleProxyDao;
149151
import com.cloud.vm.dao.UserVmDetailsDao;
150152
import com.cloud.vm.dao.VMInstanceDao;
@@ -227,6 +229,8 @@ public class ConsoleProxyManagerImpl extends ManagerBase implements ConsoleProxy
227229
private CAManager caManager;
228230
@Inject
229231
private NetworkOrchestrationService networkMgr;
232+
@Inject
233+
DeploymentPlanningManager deploymentPlanningManager;
230234

231235
private ConsoleProxyListener consoleProxyListener;
232236

@@ -719,26 +723,25 @@ protected Map<String, Object> createProxyInstance(long dataCenterId, List<VMTemp
719723
serviceOffering = serviceOfferingDao.findDefaultSystemOffering(ServiceOffering.consoleProxyDefaultOffUniqueName, ConfigurationManagerImpl.SystemVMUseLocalStorage.valueIn(dataCenterId));
720724
}
721725
ConsoleProxyVO proxy = null;
722-
InsufficientCapacityException lastException = null;
723-
for (VMTemplateVO template : templates) {
726+
for (final Iterator<VMTemplateVO> templateIterator = templates.iterator(); templateIterator.hasNext();) {
727+
VMTemplateVO template = templateIterator.next();
724728
proxy = createOrUpdateConsoleProxy(proxy, dataCenterId, id, name, serviceOffering, template, systemAcct);
725729
try {
726730
virtualMachineManager.allocate(name, template, serviceOffering, networks, plan, null);
727731
proxy = consoleProxyDao.findById(proxy.getId());
728-
lastException = null;
732+
final VirtualMachineProfileImpl vmProfile =
733+
new VirtualMachineProfileImpl(proxy, template, serviceOffering, systemAcct, null);
734+
deploymentPlanningManager.planDeployment(vmProfile, plan, new DeploymentPlanner.ExcludeList(), null);
729735
break;
730736
} catch (InsufficientCapacityException e) {
731-
String message = String.format("Unable to allocate proxy [%s] on zone [%s] with %s due to [%s].",
732-
proxy.toString(), dataCenterId, template, e.getMessage());
733-
logger.warn(message, e);
734-
lastException = e;
737+
if (templateIterator.hasNext()) {
738+
logger.debug("Unable to allocate proxy {} with {} in {} due to [{}]. Retrying with another template", proxy, template, dc, e.getMessage(), e);
739+
continue;
740+
}
741+
throw new CloudRuntimeException("Failed to allocate proxy [%s] in zone [%s] with available templates", e);
735742
}
736743
}
737744

738-
if (lastException != null) {
739-
throw new CloudRuntimeException("Failed to allocate proxy [%s] on zone [%s] with available templates");
740-
}
741-
742745
Map<String, Object> context = new HashMap<>();
743746
context.put("dc", dc);
744747
HostPodVO pod = hostPodDao.findById(proxy.getPodIdToDeployIn());

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

+16-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Collections;
2626
import java.util.Date;
2727
import java.util.HashMap;
28+
import java.util.Iterator;
2829
import java.util.LinkedHashMap;
2930
import java.util.List;
3031
import java.util.Map;
@@ -82,6 +83,7 @@
8283
import com.cloud.deploy.DataCenterDeployment;
8384
import com.cloud.deploy.DeployDestination;
8485
import com.cloud.deploy.DeploymentPlanner;
86+
import com.cloud.deploy.DeploymentPlanningManager;
8587
import com.cloud.event.ActionEvent;
8688
import com.cloud.event.EventTypes;
8789
import com.cloud.exception.ConcurrentOperationException;
@@ -155,6 +157,7 @@
155157
import com.cloud.vm.VirtualMachineManager;
156158
import com.cloud.vm.VirtualMachineName;
157159
import com.cloud.vm.VirtualMachineProfile;
160+
import com.cloud.vm.VirtualMachineProfileImpl;
158161
import com.cloud.vm.dao.SecondaryStorageVmDao;
159162
import com.cloud.vm.dao.UserVmDetailsDao;
160163
import com.cloud.vm.dao.VMInstanceDao;
@@ -254,6 +257,8 @@ public class SecondaryStorageManagerImpl extends ManagerBase implements Secondar
254257
private IndirectAgentLB indirectAgentLB;
255258
@Inject
256259
private CAManager caManager;
260+
@Inject
261+
DeploymentPlanningManager deploymentPlanningManager;
257262
private int _secStorageVmMtuSize;
258263

259264
private String _instance;
@@ -686,26 +691,26 @@ protected Map<String, Object> createSecStorageVmInstance(long dataCenterId, Seco
686691
serviceOffering = _offeringDao.findDefaultSystemOffering(ServiceOffering.ssvmDefaultOffUniqueName, ConfigurationManagerImpl.SystemVMUseLocalStorage.valueIn(dataCenterId));
687692
}
688693
SecondaryStorageVmVO secStorageVm = null;
689-
InsufficientCapacityException lastException = null;
690-
for (VMTemplateVO template : templates) {
691-
secStorageVm = createOrUpdateSecondaryStorageVm(secStorageVm, id, dataCenterId, name, serviceOffering,
694+
for (final Iterator<VMTemplateVO> templateIterator = templates.iterator(); templateIterator.hasNext();) {
695+
VMTemplateVO template = templateIterator.next();
696+
secStorageVm = createOrUpdateSecondaryStorageVm(secStorageVm, dataCenterId, id, name, serviceOffering,
692697
template, systemAcct, role);
693698
try {
694699
_itMgr.allocate(name, template, serviceOffering, networks, plan, null);
695700
secStorageVm = _secStorageVmDao.findById(secStorageVm.getId());
696-
lastException = null;
701+
final VirtualMachineProfileImpl vmProfile =
702+
new VirtualMachineProfileImpl(secStorageVm, template, serviceOffering, systemAcct, null);
703+
deploymentPlanningManager.planDeployment(vmProfile, plan, new DeploymentPlanner.ExcludeList(), null);
697704
break;
698705
} catch (InsufficientCapacityException e) {
699-
String errorMessage = String.format("Unable to allocate secondary storage VM [%s] due to [%s].", name, e.getMessage());
700-
logger.warn(errorMessage, e);
701-
lastException = e;
706+
if (templateIterator.hasNext()) {
707+
logger.debug("Unable to allocate secondary storage {} with {} due to [{}]. Retrying with another template", secStorageVm, template, e.getMessage(), e);
708+
continue;
709+
}
710+
throw new CloudRuntimeException("Failed to allocate secondary storage VM [%s] in zone [%s] with available templates", e);
702711
}
703712
}
704713

705-
if (lastException != null) {
706-
throw new CloudRuntimeException("Failed to allocate secondary storage VM [%s] on zone [%s] with available templates");
707-
}
708-
709714
Map<String, Object> context = new HashMap<>();
710715
context.put("secStorageVmId", secStorageVm.getId());
711716
return context;

0 commit comments

Comments
 (0)