Search in sources :

Example 31 with LoadBalancerVMMapVO

use of com.cloud.network.dao.LoadBalancerVMMapVO in project cloudstack by apache.

the class VirtualNetworkApplianceManagerImpl method updateWithLbRules.

private void updateWithLbRules(final DomainRouterJoinVO routerJoinVO, final StringBuilder loadBalancingData) {
    List<? extends FirewallRuleVO> loadBalancerVOs = this.getLBRules(routerJoinVO);
    for (FirewallRuleVO firewallRuleVO : loadBalancerVOs) {
        List<LoadBalancerVMMapVO> vmMapVOs = _loadBalancerVMMapDao.listByLoadBalancerId(firewallRuleVO.getId(), false);
        if (vmMapVOs.size() > 0) {
            final NetworkOffering offering = _networkOfferingDao.findById(_networkDao.findById(routerJoinVO.getNetworkId()).getNetworkOfferingId());
            if (offering.getConcurrentConnections() == null) {
                loadBalancingData.append("maxconn=").append(_configDao.getValue(Config.NetworkLBHaproxyMaxConn.key()));
            } else {
                loadBalancingData.append("maxconn=").append(offering.getConcurrentConnections().toString());
            }
            loadBalancingData.append(",sourcePortStart=").append(firewallRuleVO.getSourcePortStart()).append(",sourcePortEnd=").append(firewallRuleVO.getSourcePortEnd());
            if (firewallRuleVO instanceof LoadBalancerVO) {
                LoadBalancerVO loadBalancerVO = (LoadBalancerVO) firewallRuleVO;
                loadBalancingData.append(",sourceIp=").append(_ipAddressDao.findById(loadBalancerVO.getSourceIpAddressId()).getAddress().toString()).append(",destPortStart=").append(loadBalancerVO.getDefaultPortStart()).append(",destPortEnd=").append(loadBalancerVO.getDefaultPortEnd()).append(",algorithm=").append(loadBalancerVO.getAlgorithm()).append(",protocol=").append(loadBalancerVO.getLbProtocol());
            } else if (firewallRuleVO instanceof ApplicationLoadBalancerRuleVO) {
                ApplicationLoadBalancerRuleVO appLoadBalancerVO = (ApplicationLoadBalancerRuleVO) firewallRuleVO;
                loadBalancingData.append(",sourceIp=").append(appLoadBalancerVO.getSourceIp()).append(",destPortStart=").append(appLoadBalancerVO.getDefaultPortStart()).append(",destPortEnd=").append(appLoadBalancerVO.getDefaultPortEnd()).append(",algorithm=").append(appLoadBalancerVO.getAlgorithm()).append(",protocol=").append(appLoadBalancerVO.getLbProtocol());
            }
            loadBalancingData.append(",stickiness=").append(getStickinessPolicies(firewallRuleVO.getId()));
            loadBalancingData.append(",keepAliveEnabled=").append(offering.isKeepAliveEnabled()).append(",vmIps=");
            for (LoadBalancerVMMapVO vmMapVO : vmMapVOs) {
                loadBalancingData.append(vmMapVO.getInstanceIp()).append(" ");
            }
            loadBalancingData.setCharAt(loadBalancingData.length() - 1, ';');
        }
    }
}
Also used : ApplicationLoadBalancerRuleVO(org.apache.cloudstack.lb.ApplicationLoadBalancerRuleVO) NetworkOffering(com.cloud.offering.NetworkOffering) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) FirewallRuleVO(com.cloud.network.rules.FirewallRuleVO)

Example 32 with LoadBalancerVMMapVO

use of com.cloud.network.dao.LoadBalancerVMMapVO in project cloudstack by apache.

the class LoadBalancingRulesManagerImpl method getLbInstances.

@Override
public Map<Ip, UserVm> getLbInstances(long lbId) {
    Map<Ip, UserVm> dstList = new HashMap<Ip, UserVm>();
    List<LoadBalancerVMMapVO> lbVmMaps = _lb2VmMapDao.listByLoadBalancerId(lbId);
    LoadBalancerVO lb = _lbDao.findById(lbId);
    for (LoadBalancerVMMapVO lbVmMap : lbVmMaps) {
        UserVm vm = _vmDao.findById(lbVmMap.getInstanceId());
        Nic nic = _nicDao.findByInstanceIdAndNetworkIdIncludingRemoved(lb.getNetworkId(), vm.getId());
        Ip ip = new Ip(nic.getIPv4Address());
        dstList.put(ip, vm);
    }
    return dstList;
}
Also used : UserVm(com.cloud.uservm.UserVm) HashMap(java.util.HashMap) Ip(com.cloud.utils.net.Ip) PublicIp(com.cloud.network.addr.PublicIp) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) Nic(com.cloud.vm.Nic) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO)

Example 33 with LoadBalancerVMMapVO

use of com.cloud.network.dao.LoadBalancerVMMapVO in project cloudstack by apache.

the class LoadBalancingRulesManagerImpl method deleteLBHealthCheckPolicy.

@DB
@Override
@ActionEvent(eventType = EventTypes.EVENT_LB_HEALTHCHECKPOLICY_DELETE, eventDescription = "revoking LB HealthCheck policy ", async = true)
public boolean deleteLBHealthCheckPolicy(long healthCheckPolicyId, boolean apply) {
    boolean success = true;
    CallContext caller = CallContext.current();
    LBHealthCheckPolicyVO healthCheckPolicy = _lb2healthcheckDao.findById(healthCheckPolicyId);
    if (healthCheckPolicy == null) {
        throw new InvalidParameterException("Invalid HealthCheck policy id value: " + healthCheckPolicyId);
    }
    LoadBalancerVO loadBalancer = _lbDao.findById(Long.valueOf(healthCheckPolicy.getLoadBalancerId()));
    if (loadBalancer == null) {
        throw new InvalidParameterException("Invalid Load balancer : " + healthCheckPolicy.getLoadBalancerId() + " for HealthCheck policy id: " + healthCheckPolicyId);
    }
    final long loadBalancerId = loadBalancer.getId();
    FirewallRule.State backupState = loadBalancer.getState();
    _accountMgr.checkAccess(caller.getCallingAccount(), null, true, loadBalancer);
    if (apply) {
        if (loadBalancer.getState() == FirewallRule.State.Active) {
            loadBalancer.setState(FirewallRule.State.Add);
            _lbDao.persist(loadBalancer);
        }
        boolean backupStickyState = healthCheckPolicy.isRevoke();
        healthCheckPolicy.setRevoke(true);
        _lb2healthcheckDao.persist(healthCheckPolicy);
        s_logger.debug("Set health check policy to revoke for loadbalancing rule id : " + loadBalancerId + ", healthCheckpolicyID " + healthCheckPolicyId);
        // removing the state of services set by the monitor.
        final List<LoadBalancerVMMapVO> maps = _lb2VmMapDao.listByLoadBalancerId(loadBalancerId);
        if (maps != null) {
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    s_logger.debug("Resetting health state policy for services in loadbalancing rule id : " + loadBalancerId);
                    for (LoadBalancerVMMapVO map : maps) {
                        map.setState(null);
                        _lb2VmMapDao.persist(map);
                    }
                }
            });
        }
        try {
            if (!applyLoadBalancerConfig(loadBalancerId)) {
                s_logger.warn("Failed to remove load balancer rule id " + loadBalancerId + " for healthCheckpolicyID " + healthCheckPolicyId);
                throw new CloudRuntimeException("Failed to remove load balancer rule id " + loadBalancerId + " for healthCheckpolicyID " + healthCheckPolicyId);
            }
        } catch (ResourceUnavailableException e) {
            if (isRollBackAllowedForProvider(loadBalancer)) {
                healthCheckPolicy.setRevoke(backupStickyState);
                _lb2healthcheckDao.persist(healthCheckPolicy);
                loadBalancer.setState(backupState);
                _lbDao.persist(loadBalancer);
                s_logger.debug("LB Rollback rule id: " + loadBalancer.getId() + "  while deleting healthcheck policy: " + healthCheckPolicyId);
            }
            s_logger.warn("Unable to apply the load balancer config because resource is unavaliable.", e);
            success = false;
        }
    } else {
        _lb2healthcheckDao.remove(healthCheckPolicy.getLoadBalancerId());
    }
    return success;
}
Also used : LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) LBHealthCheckPolicyVO(com.cloud.network.LBHealthCheckPolicyVO) CallContext(org.apache.cloudstack.context.CallContext) InvalidParameterException(java.security.InvalidParameterException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) FirewallRule(com.cloud.network.rules.FirewallRule) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 34 with LoadBalancerVMMapVO

use of com.cloud.network.dao.LoadBalancerVMMapVO in project cloudstack by apache.

the class LoadBalancingRulesManagerImpl method removeVmFromLoadBalancers.

@Override
public boolean removeVmFromLoadBalancers(long instanceId) {
    boolean success = true;
    List<LoadBalancerVMMapVO> maps = _lb2VmMapDao.listByInstanceId(instanceId);
    if (maps == null || maps.isEmpty()) {
        return true;
    }
    Map<Long, List<Long>> lbsToReconfigure = new HashMap<Long, List<Long>>();
    // first set all existing lb mappings with Revoke state
    for (LoadBalancerVMMapVO map : maps) {
        long lbId = map.getLoadBalancerId();
        List<Long> instances = lbsToReconfigure.get(lbId);
        if (instances == null) {
            instances = new ArrayList<Long>();
        }
        instances.add(map.getInstanceId());
        lbsToReconfigure.put(lbId, instances);
        map.setRevoke(true);
        _lb2VmMapDao.persist(map);
        s_logger.debug("Set load balancer rule for revoke: rule id " + map.getLoadBalancerId() + ", vmId " + instanceId);
    }
    // Reapply all lbs that had the vm assigned
    if (lbsToReconfigure != null) {
        for (Map.Entry<Long, List<Long>> lb : lbsToReconfigure.entrySet()) {
            if (!removeFromLoadBalancerInternal(lb.getKey(), lb.getValue(), false, new HashMap<Long, List<String>>())) {
                success = false;
            }
        }
    }
    return success;
}
Also used : HashMap(java.util.HashMap) LoadBalancerVMMapVO(com.cloud.network.dao.LoadBalancerVMMapVO) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

LoadBalancerVMMapVO (com.cloud.network.dao.LoadBalancerVMMapVO)34 ArrayList (java.util.ArrayList)28 LoadBalancerVO (com.cloud.network.dao.LoadBalancerVO)23 List (java.util.List)18 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)12 DB (com.cloud.utils.db.DB)12 HashMap (java.util.HashMap)11 TransactionStatus (com.cloud.utils.db.TransactionStatus)10 FirewallRule (com.cloud.network.rules.FirewallRule)8 Account (com.cloud.user.Account)8 UserVm (com.cloud.uservm.UserVm)8 Nic (com.cloud.vm.Nic)8 ActionEvent (com.cloud.event.ActionEvent)6 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)6 NetworkVO (com.cloud.network.dao.NetworkVO)6 FirewallRuleVO (com.cloud.network.rules.FirewallRuleVO)6 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)6 Network (com.cloud.network.Network)5 Pair (com.cloud.utils.Pair)5