diff --git a/engine/schema/src/org/apache/cloudstack/lb/dao/ApplicationLoadBalancerRuleDao.java b/engine/schema/src/org/apache/cloudstack/lb/dao/ApplicationLoadBalancerRuleDao.java index 47f1d361216..aa765ad1bba 100644 --- a/engine/schema/src/org/apache/cloudstack/lb/dao/ApplicationLoadBalancerRuleDao.java +++ b/engine/schema/src/org/apache/cloudstack/lb/dao/ApplicationLoadBalancerRuleDao.java @@ -32,5 +32,6 @@ public interface ApplicationLoadBalancerRuleDao extends GenericDao listBySourceIpAndNotRevoked(Ip sourceIp, long sourceNetworkId); List listLbIpsBySourceIpNetworkIdAndScheme(long sourceIpNetworkId, Scheme scheme); long countBySourceIpAndNotRevoked(Ip sourceIp, long sourceIpNetworkId); + long countActiveBySourceIp(Ip sourceIp, long sourceIpNetworkId); } diff --git a/engine/schema/src/org/apache/cloudstack/lb/dao/ApplicationLoadBalancerRuleDaoImpl.java b/engine/schema/src/org/apache/cloudstack/lb/dao/ApplicationLoadBalancerRuleDaoImpl.java index 6036b5a2d60..67d0a78cd09 100644 --- a/engine/schema/src/org/apache/cloudstack/lb/dao/ApplicationLoadBalancerRuleDaoImpl.java +++ b/engine/schema/src/org/apache/cloudstack/lb/dao/ApplicationLoadBalancerRuleDaoImpl.java @@ -43,6 +43,7 @@ public class ApplicationLoadBalancerRuleDaoImpl extends GenericDaoBase CountBy; protected final SearchBuilder NotRevokedSearch; final GenericSearchBuilder CountNotRevoked; + final GenericSearchBuilder CountActive; protected ApplicationLoadBalancerRuleDaoImpl() { @@ -77,6 +78,13 @@ public class ApplicationLoadBalancerRuleDaoImpl extends GenericDaoBase results = customSearch(sc, null); return results.get(0); } + + @Override + public long countActiveBySourceIp(Ip sourceIp, long sourceIpNetworkId) { + SearchCriteria sc = CountActive.create(); + sc.setParameters("sourceIp", sourceIp); + sc.setParameters("sourceIpNetworkId", sourceIpNetworkId); + sc.setParameters("state", State.Active); + List results = customSearch(sc, null); + return results.get(0); + } } diff --git a/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/element/InternalLoadBalancerElement.java b/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/element/InternalLoadBalancerElement.java index 14b616cb749..18ac13baef4 100644 --- a/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/element/InternalLoadBalancerElement.java +++ b/plugins/network-elements/internal-loadbalancer/src/org/apache/cloudstack/network/element/InternalLoadBalancerElement.java @@ -165,30 +165,7 @@ public class InternalLoadBalancerElement extends AdapterBase implements LoadBala return true; } - //1) Get all the Ips from the network having LB rules assigned - List ips = _appLbDao.listLbIpsBySourceIpNetworkIdAndScheme(network.getId(), Scheme.Internal); - - //2) Start those vms - for (String ip : ips) { - Ip sourceIp = new Ip(ip); - List internalLbVms; - try { - internalLbVms = _internalLbMgr.deployInternalLbVm(network, sourceIp, dest, _accountMgr.getAccount(network.getAccountId()), null); - } catch (InsufficientCapacityException e) { - s_logger.warn("Failed to deploy element " + this.getName() + " for ip " + sourceIp + " due to:", e); - return false; - } catch (ConcurrentOperationException e) { - s_logger.warn("Failed to deploy element " + this.getName() + " for ip " + sourceIp + " due to:", e); - return false; - } - - if (internalLbVms == null || internalLbVms.isEmpty()) { - throw new ResourceUnavailableException("Can't deploy " + this.getName() + " to handle LB rules", - DataCenter.class, network.getDataCenterId()); - } - } - - return true; + return implementInternalLbVms(network, dest); } @@ -202,12 +179,23 @@ public class InternalLoadBalancerElement extends AdapterBase implements LoadBala } if (vm.getType() == VirtualMachine.Type.User) { - //1) Get all the Ips from the network having LB rules assigned - List ips = _appLbDao.listLbIpsBySourceIpNetworkIdAndScheme(network.getId(), Scheme.Internal); - - //2) Start those vms - for (String ip : ips) { - Ip sourceIp = new Ip(ip); + return implementInternalLbVms(network, dest); + } + return true; + } + + + protected boolean implementInternalLbVms(Network network, DeployDestination dest) throws ResourceUnavailableException { + //1) Get all the Ips from the network having LB rules assigned + List ips = _appLbDao.listLbIpsBySourceIpNetworkIdAndScheme(network.getId(), Scheme.Internal); + + //2) Start internal lb vms for the ips having active rules + for (String ip : ips) { + Ip sourceIp = new Ip(ip); + long active = _appLbDao.countActiveBySourceIp(sourceIp, network.getId()); + if (active > 0) { + s_logger.debug("Have to implement internal lb vm for source ip " + sourceIp + " as a part of network " + network + + " implement as there are " + active + " internal lb rules exist for this ip"); List internalLbVms; try { internalLbVms = _internalLbMgr.deployInternalLbVm(network, sourceIp, dest, _accountMgr.getAccount(network.getAccountId()), null); @@ -223,7 +211,7 @@ public class InternalLoadBalancerElement extends AdapterBase implements LoadBala throw new ResourceUnavailableException("Can't deploy " + this.getName() + " to handle LB rules", DataCenter.class, network.getDataCenterId()); } - } + } } return true; @@ -410,17 +398,21 @@ public class InternalLoadBalancerElement extends AdapterBase implements LoadBala protected Map> groupBySourceIp(List rules) { Map> groupedRules = new HashMap>(); for (LoadBalancingRule rule : rules) { - Ip sourceIp = rule.getSourceIp(); - if (!groupedRules.containsKey(sourceIp)) { - groupedRules.put(sourceIp, null); + if (rule.getDestinations() != null && !rule.getDestinations().isEmpty()) { + Ip sourceIp = rule.getSourceIp(); + if (!groupedRules.containsKey(sourceIp)) { + groupedRules.put(sourceIp, null); + } + + List rulesToApply = groupedRules.get(sourceIp); + if (rulesToApply == null) { + rulesToApply = new ArrayList(); + } + rulesToApply.add(rule); + groupedRules.put(sourceIp, rulesToApply); + } else { + s_logger.debug("Internal lb rule " + rule + " doesn't have any vms assigned, skipping"); } - - List rulesToApply = groupedRules.get(sourceIp); - if (rulesToApply == null) { - rulesToApply = new ArrayList(); - } - rulesToApply.add(rule); - groupedRules.put(sourceIp, rulesToApply); } return groupedRules; }