Skip to content

Commit 59f2608

Browse files
committed
wip: try VR deployment with arch specific template
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 42598f7 commit 59f2608

File tree

4 files changed

+149
-76
lines changed

4 files changed

+149
-76
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public interface VMTemplateDao extends GenericDao<VMTemplateVO, Long>, StateDao<
7878

7979
VMTemplateVO findRoutingTemplate(HypervisorType type, String templateName);
8080

81+
List<VMTemplateVO> findRoutingTemplates(HypervisorType type, String templateName);
82+
8183
VMTemplateVO findLatestTemplateByTypeAndHypervisorAndArch(HypervisorType hypervisorType, CPU.CPUArch arch, Storage.TemplateType type);
8284

8385
public Long countTemplatesForAccount(long accountId);

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,36 @@ public VMTemplateVO findRoutingTemplate(HypervisorType hType, String templateNam
661661
}
662662
}
663663

664+
@Override
665+
public List<VMTemplateVO> findRoutingTemplates(HypervisorType hType, String templateName) {
666+
SearchCriteria<VMTemplateVO> sc = tmpltTypeHyperSearch2.create();
667+
sc.setParameters("templateType", TemplateType.ROUTING);
668+
sc.setParameters("hypervisorType", hType);
669+
sc.setParameters("state", VirtualMachineTemplate.State.Active.toString());
670+
if (templateName != null) {
671+
sc.setParameters("templateName", templateName);
672+
}
673+
List<VMTemplateVO> templates = listBy(sc, new Filter(VMTemplateVO.class, "id", false, null, 1L));
674+
if (CollectionUtils.isEmpty(templates)) {
675+
sc = tmpltTypeHyperSearch2.create();
676+
sc.setParameters("templateType", TemplateType.SYSTEM);
677+
sc.setParameters("hypervisorType", hType);
678+
sc.setParameters("state", VirtualMachineTemplate.State.Active.toString());
679+
if (templateName != null) {
680+
sc.setParameters("templateName", templateName);
681+
}
682+
templates = listBy(sc, new Filter(VMTemplateVO.class, "id", false, null, 1L));
683+
}
684+
Map<Pair<HypervisorType, CPU.CPUArch>, VMTemplateVO> uniqueTemplates = new HashMap<>();
685+
for (VMTemplateVO template : templates) {
686+
Pair<HypervisorType, CPU.CPUArch> key = new Pair<>(template.getHypervisorType(), template.getArch());
687+
if (!uniqueTemplates.containsKey(key)) {
688+
uniqueTemplates.put(key, template);
689+
}
690+
}
691+
return new ArrayList<>(uniqueTemplates.values());
692+
}
693+
664694
@Override
665695
public VMTemplateVO findLatestTemplateByTypeAndHypervisorAndArch(HypervisorType hypervisorType, CPU.CPUArch arch, TemplateType type) {
666696
SearchCriteria<VMTemplateVO> sc = LatestTemplateByHypervisorTypeSearch.create();

plugins/network-elements/internal-loadbalancer/src/main/java/org/apache/cloudstack/network/lb/InternalLoadBalancerVMManagerImpl.java

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
// under the License.
1717
package org.apache.cloudstack.network.lb;
1818

19+
import static com.cloud.hypervisor.Hypervisor.HypervisorType.Hyperv;
20+
import static com.cloud.hypervisor.Hypervisor.HypervisorType.KVM;
21+
import static com.cloud.hypervisor.Hypervisor.HypervisorType.LXC;
22+
import static com.cloud.hypervisor.Hypervisor.HypervisorType.VMware;
23+
import static com.cloud.hypervisor.Hypervisor.HypervisorType.XenServer;
24+
1925
import java.util.ArrayList;
2026
import java.util.Arrays;
2127
import java.util.Iterator;
@@ -31,6 +37,7 @@
3137
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
3238
import org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO;
3339
import org.apache.cloudstack.lb.dao.ApplicationLoadBalancerRuleDao;
40+
import org.apache.commons.collections.CollectionUtils;
3441

3542
import com.cloud.agent.AgentManager;
3643
import com.cloud.agent.api.Answer;
@@ -118,12 +125,6 @@
118125
import com.cloud.vm.dao.DomainRouterDao;
119126
import com.cloud.vm.dao.NicDao;
120127

121-
import static com.cloud.hypervisor.Hypervisor.HypervisorType.Hyperv;
122-
import static com.cloud.hypervisor.Hypervisor.HypervisorType.KVM;
123-
import static com.cloud.hypervisor.Hypervisor.HypervisorType.LXC;
124-
import static com.cloud.hypervisor.Hypervisor.HypervisorType.VMware;
125-
import static com.cloud.hypervisor.Hypervisor.HypervisorType.XenServer;
126-
127128
public class InternalLoadBalancerVMManagerImpl extends ManagerBase implements InternalLoadBalancerVMManager, InternalLoadBalancerVMService, VirtualMachineGuru {
128129
static final private String InternalLbVmNamePrefix = "b";
129130

@@ -732,6 +733,49 @@ public List<DomainRouterVO> findInternalLbVms(final long guestNetworkId, final I
732733
return internalLbVms;
733734
}
734735

736+
protected String getRouterTemplateForHypervisor(HypervisorType hypervisorType, long dataCenterId) {
737+
String templateName = null;
738+
if (XenServer.equals(hypervisorType)) {
739+
templateName = VirtualNetworkApplianceManager.RouterTemplateXen.valueIn(dataCenterId);
740+
} else if (KVM.equals(hypervisorType)) {
741+
templateName = VirtualNetworkApplianceManager.RouterTemplateKvm.valueIn(dataCenterId);
742+
} else if (VMware.equals(hypervisorType)) {
743+
templateName = VirtualNetworkApplianceManager.RouterTemplateVmware.valueIn(dataCenterId);
744+
} else if (Hyperv.equals(hypervisorType)) {
745+
templateName = VirtualNetworkApplianceManager.RouterTemplateHyperV.valueIn(dataCenterId);
746+
} else if (LXC.equals(hypervisorType)) {
747+
templateName = VirtualNetworkApplianceManager.RouterTemplateLxc.valueIn(dataCenterId);
748+
}
749+
return templateName;
750+
}
751+
752+
protected DomainRouterVO deployInternalLbVmWithTemplates(final long id, final DeploymentPlan plan, final long internalLbProviderId,
753+
final Account owner, final long userId, final Long vpcId, final ServiceOffering routerOffering,
754+
final LinkedHashMap<Network, List<? extends NicProfile>> networks,
755+
final List<VMTemplateVO> templates) throws InsufficientCapacityException {
756+
for (final Iterator<VMTemplateVO> templatesIterator = templates.iterator(); templatesIterator.hasNext();) {
757+
final VMTemplateVO template = templatesIterator.next();
758+
try {
759+
DomainRouterVO internalLbVm = new DomainRouterVO(id, routerOffering.getId(), internalLbProviderId,
760+
VirtualMachineName.getSystemVmName(id, _instance, InternalLbVmNamePrefix),
761+
template.getId(), template.getHypervisorType(), template.getGuestOSId(),
762+
owner.getDomainId(), owner.getId(), userId, false,
763+
RedundantState.UNKNOWN, false, false, VirtualMachine.Type.InternalLoadBalancerVm, vpcId);
764+
internalLbVm.setRole(Role.INTERNAL_LB_VM);
765+
internalLbVm = _internalLbVmDao.persist(internalLbVm);
766+
_itMgr.allocate(internalLbVm.getInstanceName(), template, routerOffering, networks, plan, null);
767+
return _internalLbVmDao.findById(internalLbVm.getId());
768+
} catch (InsufficientCapacityException ex) {
769+
if (templatesIterator.hasNext()) {
770+
logger.debug("Failed to allocate the VR with hypervisor {} and {}, retrying with another template", template.getHypervisorType(), template);
771+
} else {
772+
throw ex;
773+
}
774+
}
775+
}
776+
return null;
777+
}
778+
735779
protected DomainRouterVO deployInternalLbVm(final Account owner, final DeployDestination dest, final DeploymentPlan plan, final Map<Param, Object> params, final long internalLbProviderId,
736780
final long svcOffId, final Long vpcId, final LinkedHashMap<Network, List<? extends NicProfile>> networks, final boolean startVm) throws ConcurrentOperationException,
737781
InsufficientAddressCapacityException, InsufficientServerCapacityException, InsufficientCapacityException, StorageUnavailableException,
@@ -743,6 +787,14 @@ protected DomainRouterVO deployInternalLbVm(final Account owner, final DeployDes
743787
// Try to allocate the internal lb twice using diff hypervisors, and when failed both times, throw the exception up
744788
final List<HypervisorType> hypervisors = getHypervisors(dest, plan, null);
745789

790+
long userId = CallContext.current().getCallingUserId();
791+
if (CallContext.current().getCallingAccount().getId() != owner.getId()) {
792+
List<UserVO> userVOs = _userDao.listByAccount(owner.getAccountId());
793+
if (!userVOs.isEmpty()) {
794+
userId = userVOs.get(0).getId();
795+
}
796+
}
797+
746798
int allocateRetry = 0;
747799
int startRetry = 0;
748800
DomainRouterVO internalLbVm = null;
@@ -751,45 +803,20 @@ protected DomainRouterVO deployInternalLbVm(final Account owner, final DeployDes
751803
try {
752804
final long id = _internalLbVmDao.getNextInSequence(Long.class, "id");
753805
if (logger.isDebugEnabled()) {
754-
logger.debug("Creating the internal lb vm " + id + " in datacenter " + dest.getDataCenter() + " with hypervisor type " + hType);
806+
logger.debug("Creating the internal lb vm {} in datacenter {} with hypervisor type {}",
807+
id, dest.getDataCenter(), hType);
755808
}
756-
String templateName = null;
757-
if (hType.equals(XenServer)) {
758-
templateName = VirtualNetworkApplianceManager.RouterTemplateXen.valueIn(dest.getDataCenter().getId());
759-
} else if (hType.equals(KVM)) {
760-
templateName = VirtualNetworkApplianceManager.RouterTemplateKvm.valueIn(dest.getDataCenter().getId());
761-
} else if (hType.equals(VMware)) {
762-
templateName = VirtualNetworkApplianceManager.RouterTemplateVmware.valueIn(dest.getDataCenter().getId());
763-
} else if (hType.equals(Hyperv)) {
764-
templateName = VirtualNetworkApplianceManager.RouterTemplateHyperV.valueIn(dest.getDataCenter().getId());
765-
} else if (hType.equals(LXC)) {
766-
templateName = VirtualNetworkApplianceManager.RouterTemplateLxc.valueIn(dest.getDataCenter().getId());
767-
}
768-
final VMTemplateVO template = _templateDao.findRoutingTemplate(hType, templateName);
769-
770-
if (template == null) {
771-
logger.debug(hType + " won't support system vm, skip it");
809+
final String templateName = getRouterTemplateForHypervisor(hType, dest.getDataCenter().getId());
810+
final List<VMTemplateVO> templates = _templateDao.findRoutingTemplates(hType, templateName);
811+
if (CollectionUtils.isEmpty(templates)) {
812+
logger.debug("{} won't support system vm, skip it", hType);
772813
continue;
773814
}
774-
775-
long userId = CallContext.current().getCallingUserId();
776-
if (CallContext.current().getCallingAccount().getId() != owner.getId()) {
777-
List<UserVO> userVOs = _userDao.listByAccount(owner.getAccountId());
778-
if (!userVOs.isEmpty()) {
779-
userId = userVOs.get(0).getId();
780-
}
781-
}
782-
783-
internalLbVm =
784-
new DomainRouterVO(id, routerOffering.getId(), internalLbProviderId, VirtualMachineName.getSystemVmName(id, _instance, InternalLbVmNamePrefix),
785-
template.getId(), template.getHypervisorType(), template.getGuestOSId(), owner.getDomainId(), owner.getId(), userId, false, RedundantState.UNKNOWN, false, false, VirtualMachine.Type.InternalLoadBalancerVm, vpcId);
786-
internalLbVm.setRole(Role.INTERNAL_LB_VM);
787-
internalLbVm = _internalLbVmDao.persist(internalLbVm);
788-
_itMgr.allocate(internalLbVm.getInstanceName(), template, routerOffering, networks, plan, null);
789-
internalLbVm = _internalLbVmDao.findById(internalLbVm.getId());
815+
internalLbVm = deployInternalLbVmWithTemplates(id, plan, internalLbProviderId, owner, userId, vpcId,
816+
routerOffering, networks, templates);
790817
} catch (final InsufficientCapacityException ex) {
791818
if (allocateRetry < 2 && iter.hasNext()) {
792-
logger.debug("Failed to allocate the Internal lb vm with hypervisor type " + hType + ", retrying one more time");
819+
logger.debug("Failed to allocate the Internal lb vm with hypervisor type {}, retrying one more time", hType);
793820
continue;
794821
} else {
795822
throw ex;
@@ -804,8 +831,7 @@ protected DomainRouterVO deployInternalLbVm(final Account owner, final DeployDes
804831
break;
805832
} catch (final InsufficientCapacityException ex) {
806833
if (startRetry < 2 && iter.hasNext()) {
807-
logger.debug("Failed to start the Internal lb vm " + internalLbVm + " with hypervisor type " + hType + ", " +
808-
"destroying it and recreating one more time");
834+
logger.debug("Failed to start the Internal lb vm {} with hypervisor type {}, destroying it and recreating one more time", internalLbVm, hType);
809835
// destroy the internal lb vm
810836
destroyInternalLbVm(internalLbVm.getId(), _accountMgr.getSystemAccount(), User.UID_SYSTEM);
811837
continue;

0 commit comments

Comments
 (0)