protected DomainRouterVO findElbVmWithCapacity(final IPAddressVO ipAddr) { final List<DomainRouterVO> unusedElbVms = _elbVmMapDao.listUnusedElbVms(); if (unusedElbVms.size() > 0) { final List<DomainRouterVO> candidateVms = new ArrayList<DomainRouterVO>(); for (final DomainRouterVO candidateVm : unusedElbVms) { addCandidateVmIsPodIpMatches(candidateVm, getPodIdForDirectIp(ipAddr), candidateVms); } return candidateVms.size() == 0 ? null : candidateVms.get(new Random().nextInt(candidateVms.size())); } return null; }
@Override public LoadBalancer handleCreateLoadBalancerRule(CreateLoadBalancerRuleCmd lb, Account account, long networkId) throws InsufficientAddressCapacityException, NetworkRuleConflictException { return loadBalanceRuleHandler.handleCreateLoadBalancerRule(lb, account, networkId); }
Long ipId = null; boolean newIp = false; List<LoadBalancerVO> existingLbs = findExistingLoadBalancers(lb.getName(), lb.getSourceIpAddressId(), lb.getAccountId(), lb.getDomainId(), lb.getSourcePortStart()); if (existingLbs == null) { existingLbs = findExistingLoadBalancers(lb.getName(), lb.getSourceIpAddressId(), lb.getAccountId(), lb.getDomainId(), null); if (existingLbs == null) { if (lb.getSourceIpAddressId() != null) { throwExceptionIfSuppliedlLbNameIsNotAssociatedWithIpAddress(lb); } else { s_logger.debug("Could not find any existing frontend ips for this account for this LB rule, acquiring a new frontent IP for ELB"); final PublicIp ip = allocDirectIp(account, networkId); ipId = ip.getId(); newIp = true; s_logger.warn("Failed to create LB rule, not continuing with ELB deployment"); if (newIp) { releaseIp(ipId, CallContext.current().getCallingUserId(), account); elbVm = findElbVmWithCapacity(ipAddr); if (elbVm == null) { elbVm = deployLoadBalancerVM(networkId, ipAddr); if (elbVm == null) { final Network network = _networkModel.getNetwork(networkId); s_logger.warn("Failed to deploy a new ELB vm for ip " + ipAddr + " in network " + network + "lb name=" + lb.getName()); if (newIp) { releaseIp(ipId, CallContext.current().getCallingUserId(), account);
private DomainRouterVO deployLoadBalancerVM(final Long networkId, final IPAddressVO ipAddr) { final NetworkVO network = _networkDao.findById(networkId); final DataCenter dc = _dcDao.findById(network.getDataCenterId()); final Long podId = getPodIdForDirectIp(ipAddr); final Pod pod = podId == null ? null : _podDao.findById(podId); final Map<VirtualMachineProfile.Param, Object> params = new HashMap<VirtualMachineProfile.Param, Object>(1); params.put(VirtualMachineProfile.Param.ReProgramGuestNetworks, true); final Account owner = _accountService.getActiveAccountByName("system", new Long(1)); final DeployDestination dest = new DeployDestination(dc, pod, null, null); s_logger.debug("About to deploy ELB vm "); try { final DomainRouterVO elbVm = deployELBVm(network, dest, owner, params); if (elbVm == null) { throw new InvalidParameterValueException("Could not deploy or find existing ELB VM"); } s_logger.debug("Deployed ELB vm = " + elbVm); return elbVm; } catch (final Throwable t) { s_logger.warn("Error while deploying ELB VM: ", t); return null; } }
private void throwExceptionIfSuppliedlLbNameIsNotAssociatedWithIpAddress(final CreateLoadBalancerRuleCmd lb) { final List<LoadBalancerVO> existingLbs = findExistingLoadBalancers(lb.getName(), null, lb.getAccountId(), lb.getDomainId(), null); if (existingLbs != null) { throw new InvalidParameterValueException("Supplied LB name " + lb.getName() + " is not associated with IP " + lb.getSourceIpAddressId()); } }
loadBalanceRuleHandler = new LoadBalanceRuleHandler(_instance, _systemAcct);
public LoadBalancer handleCreateLoadBalancerRule(final CreateLoadBalancerRuleCmd lb, Account account, final long networkId) throws InsufficientAddressCapacityException, NetworkRuleConflictException { //this part of code is executed when the LB provider is Elastic Load Balancer vm if (!_networkModel.isProviderSupportServiceInNetwork(lb.getNetworkId(), Service.Lb, Provider.ElasticLoadBalancerVm)) { return null; } final Long ipId = lb.getSourceIpAddressId(); if (ipId != null) { return null; } account = _accountDao.acquireInLockTable(account.getId()); if (account == null) { s_logger.warn("ELB: CreateLoadBalancer: Failed to acquire lock on account"); throw new CloudRuntimeException("Failed to acquire lock on account"); } try { return handleCreateLoadBalancerRuleWithLock(lb, account, networkId); } finally { if (account != null) { _accountDao.releaseFromLockTable(account.getId()); } } }