From c84465582532dcaacf623c21c9e7abbce92d432a Mon Sep 17 00:00:00 2001 From: alena Date: Thu, 21 Apr 2011 11:48:59 -0700 Subject: [PATCH] bug 9488: throw InsufficientVirtualNetworkCapcityException when fail to allocate guest vnet as a part of network implement() call status 9488: resolved fixed --- ...ficientVirtualNetworkCapcityException.java | 4 +- .../com/cloud/network/guru/NetworkGuru.java | 3 +- .../network/guru/ControlNetworkGuru.java | 2 +- .../cloud/network/guru/DirectNetworkGuru.java | 2 +- .../cloud/network/guru/GuestNetworkGuru.java | 112 ++++++++++-------- .../network/guru/OvsGuestNetworkGuru.java | 3 +- .../network/guru/PodBasedNetworkGuru.java | 2 +- .../cloud/network/guru/PublicNetworkGuru.java | 2 +- 8 files changed, 70 insertions(+), 60 deletions(-) diff --git a/api/src/com/cloud/exception/InsufficientVirtualNetworkCapcityException.java b/api/src/com/cloud/exception/InsufficientVirtualNetworkCapcityException.java index 70e90e47170..a692ae41826 100644 --- a/api/src/com/cloud/exception/InsufficientVirtualNetworkCapcityException.java +++ b/api/src/com/cloud/exception/InsufficientVirtualNetworkCapcityException.java @@ -27,7 +27,7 @@ public class InsufficientVirtualNetworkCapcityException extends InsufficientNetw super(msg, scope, id); } - public InsufficientVirtualNetworkCapcityException(String msg, long id) { - this(msg, Pod.class, id); + public InsufficientVirtualNetworkCapcityException(String msg, long podId) { + this(msg, Pod.class, podId); } } diff --git a/api/src/com/cloud/network/guru/NetworkGuru.java b/api/src/com/cloud/network/guru/NetworkGuru.java index 36a0482ca31..5ead472300e 100644 --- a/api/src/com/cloud/network/guru/NetworkGuru.java +++ b/api/src/com/cloud/network/guru/NetworkGuru.java @@ -71,8 +71,9 @@ public interface NetworkGuru extends Adapter { * @param offering offering that the network configuration was based on. * @param destination where were deploying to. * @return a fully implemented NetworkConfiguration. + * @throws InsufficientVirtualNetworkCapcityException TODO */ - Network implement(Network network, NetworkOffering offering, DeployDestination destination, ReservationContext context); + Network implement(Network network, NetworkOffering offering, DeployDestination destination, ReservationContext context) throws InsufficientVirtualNetworkCapcityException; /** * reserve a nic for this VM in this network. diff --git a/server/src/com/cloud/network/guru/ControlNetworkGuru.java b/server/src/com/cloud/network/guru/ControlNetworkGuru.java index ca187e40211..ac2142c91d8 100644 --- a/server/src/com/cloud/network/guru/ControlNetworkGuru.java +++ b/server/src/com/cloud/network/guru/ControlNetworkGuru.java @@ -151,7 +151,7 @@ public class ControlNetworkGuru extends PodBasedNetworkGuru implements NetworkGu } @Override - public Network implement(Network config, NetworkOffering offering, DeployDestination destination, ReservationContext context) { + public Network implement(Network config, NetworkOffering offering, DeployDestination destination, ReservationContext context) throws InsufficientVirtualNetworkCapcityException { assert config.getTrafficType() == TrafficType.Control : "Why are you sending this configuration to me " + config; return config; } diff --git a/server/src/com/cloud/network/guru/DirectNetworkGuru.java b/server/src/com/cloud/network/guru/DirectNetworkGuru.java index 5ae3b62d891..c95845b8153 100644 --- a/server/src/com/cloud/network/guru/DirectNetworkGuru.java +++ b/server/src/com/cloud/network/guru/DirectNetworkGuru.java @@ -200,7 +200,7 @@ public class DirectNetworkGuru extends AdapterBase implements NetworkGuru { } @Override - public Network implement(Network network, NetworkOffering offering, DeployDestination destination, ReservationContext context) { + public Network implement(Network network, NetworkOffering offering, DeployDestination destination, ReservationContext context) throws InsufficientVirtualNetworkCapcityException { return network; } diff --git a/server/src/com/cloud/network/guru/GuestNetworkGuru.java b/server/src/com/cloud/network/guru/GuestNetworkGuru.java index b58505beab1..b4993093a14 100644 --- a/server/src/com/cloud/network/guru/GuestNetworkGuru.java +++ b/server/src/com/cloud/network/guru/GuestNetworkGuru.java @@ -59,33 +59,38 @@ import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachineProfile; import com.cloud.vm.dao.NicDao; -@Local(value=NetworkGuru.class) +@Local(value = NetworkGuru.class) public class GuestNetworkGuru extends AdapterBase implements NetworkGuru { private static final Logger s_logger = Logger.getLogger(GuestNetworkGuru.class); - @Inject protected NetworkManager _networkMgr; - @Inject protected DataCenterDao _dcDao; - @Inject protected VlanDao _vlanDao; - @Inject protected NicDao _nicDao; - @Inject protected NetworkDao _networkDao; - + @Inject + protected NetworkManager _networkMgr; + @Inject + protected DataCenterDao _dcDao; + @Inject + protected VlanDao _vlanDao; + @Inject + protected NicDao _nicDao; + @Inject + protected NetworkDao _networkDao; + String _defaultGateway; String _defaultCidr; Random _rand = new Random(System.currentTimeMillis()); - + protected GuestNetworkGuru() { super(); - } - + } + protected boolean canHandle(NetworkOffering offering, DataCenter dc) { - //This guru handles only non-system Guest network - if (dc.getNetworkType() == NetworkType.Advanced && offering.getTrafficType() == TrafficType.Guest && offering.getGuestType() == GuestIpType.Virtual && !offering.isSystemOnly()) { + // This guru handles only non-system Guest network + if (dc.getNetworkType() == NetworkType.Advanced && offering.getTrafficType() == TrafficType.Guest && offering.getGuestType() == GuestIpType.Virtual && !offering.isSystemOnly()) { return true; } else { s_logger.trace("We only take care of Guest Virtual networks in zone of type " + NetworkType.Advanced); return false; } } - + @Override public Network design(NetworkOffering offering, DeploymentPlan plan, Network userSpecified, Account owner) { DataCenter dc = _dcDao.findById(plan.getDataCenterId()); @@ -95,24 +100,23 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru { NetworkVO network = new NetworkVO(offering.getTrafficType(), offering.getGuestType(), Mode.Dhcp, BroadcastDomainType.Vlan, offering.getId(), plan.getDataCenterId(), State.Allocated); if (userSpecified != null) { - if ((userSpecified.getCidr() == null && userSpecified.getGateway() != null) || - (userSpecified.getCidr() != null && userSpecified.getGateway() == null)) { + if ((userSpecified.getCidr() == null && userSpecified.getGateway() != null) || (userSpecified.getCidr() != null && userSpecified.getGateway() == null)) { throw new InvalidParameterValueException("cidr and gateway must be specified together."); } - + if (userSpecified.getCidr() != null) { network.setCidr(userSpecified.getCidr()); network.setGateway(userSpecified.getGateway()); } else { String guestNetworkCidr = dc.getGuestNetworkCidr(); - //guest network cidr can be null for Basic zone + // guest network cidr can be null for Basic zone if (guestNetworkCidr != null) { String[] cidrTuple = guestNetworkCidr.split("\\/"); network.setGateway(NetUtils.getIpRangeStartIpFromCidr(cidrTuple[0], Long.parseLong(cidrTuple[1]))); - network.setCidr(guestNetworkCidr); + network.setCidr(guestNetworkCidr); } } - + if (userSpecified.getBroadcastUri() != null) { network.setBroadcastUri(userSpecified.getBroadcastUri()); network.setState(State.Setup); @@ -120,99 +124,103 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru { if (userSpecified.getNetworkDomain() != null) { network.setNetworkDomain(userSpecified.getNetworkDomain()); } - + } else { String guestNetworkCidr = dc.getGuestNetworkCidr(); String[] cidrTuple = guestNetworkCidr.split("\\/"); network.setGateway(NetUtils.getIpRangeStartIpFromCidr(cidrTuple[0], Long.parseLong(cidrTuple[1]))); - network.setCidr(guestNetworkCidr);; + network.setCidr(guestNetworkCidr); + ; } - + return network; } @Override public void deallocate(Network network, NicProfile nic, VirtualMachineProfile vm) { } - + @Override - public Network implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) { + public Network implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException { assert (network.getState() == State.Implementing) : "Why are we implementing " + network; - + long dcId = dest.getDataCenter().getId(); - - - NetworkVO implemented = new NetworkVO(network.getTrafficType(), network.getGuestType(), network.getMode(), network.getBroadcastDomainType(), network.getNetworkOfferingId(), network.getDataCenterId(), State.Allocated); - + + NetworkVO implemented = new NetworkVO(network.getTrafficType(), network.getGuestType(), network.getMode(), network.getBroadcastDomainType(), network.getNetworkOfferingId(), + network.getDataCenterId(), State.Allocated); + if (network.getBroadcastUri() == null) { String vnet = _dcDao.allocateVnet(dcId, network.getAccountId(), context.getReservationId()); + if (vnet == null) { + throw new InsufficientVirtualNetworkCapcityException("Unable to allocate vnet as a part of network " + network + " implement ", DataCenter.class, dcId); + } implemented.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vnet)); } else { implemented.setBroadcastUri(network.getBroadcastUri()); } - + if (network.getGateway() != null) { implemented.setGateway(network.getGateway()); } - + if (network.getCidr() != null) { implemented.setCidr(network.getCidr()); } - + return implemented; } @Override - public NicProfile allocate(Network network, NicProfile nic, VirtualMachineProfile vm) - throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { - + public NicProfile allocate(Network network, NicProfile nic, VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapcityException, + InsufficientAddressCapacityException { + assert (network.getTrafficType() == TrafficType.Guest) : "Look at my name! Why are you calling me when the traffic type is : " + network.getTrafficType(); - + if (nic == null) { nic = new NicProfile(ReservationStrategy.Start, null, null, null, null); - } - + } + DataCenter dc = _dcDao.findById(network.getDataCenterId()); - - if (nic.getIp4Address() == null){ + + if (nic.getIp4Address() == null) { nic.setBroadcastUri(network.getBroadcastUri()); nic.setIsolationUri(network.getBroadcastUri()); nic.setGateway(network.getGateway()); nic.setIp4Address(acquireGuestIpAddress(network)); nic.setNetmask(NetUtils.cidr2Netmask(network.getCidr())); nic.setFormat(AddressFormat.Ip4); - + nic.setDns1(dc.getDns1()); nic.setDns2(dc.getDns2()); } - + nic.setStrategy(ReservationStrategy.Start); - + if (nic.getMacAddress() == null) { nic.setMacAddress(_networkMgr.getNextAvailableMacAddressInNetwork(network.getId())); if (nic.getMacAddress() == null) { throw new InsufficientAddressCapacityException("Unable to allocate more mac addresses", Network.class, network.getId()); } } - + return nic; } - + @Override public void updateNicProfile(NicProfile profile, Network network) { DataCenter dc = _dcDao.findById(network.getDataCenterId()); if (profile != null) { profile.setDns1(dc.getDns1()); profile.setDns2(dc.getDns2()); - } + } } - + @DB protected String acquireGuestIpAddress(Network network) { List ips = _nicDao.listIpAddressInNetwork(network.getId()); String[] cidr = network.getCidr().split("/"); Set allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1])); - Set usedIps = new TreeSet (); + Set usedIps = new TreeSet(); for (String ip : ips) { usedIps.add(NetUtils.ip2Long(ip)); } @@ -224,11 +232,11 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru { } Long[] array = allPossibleIps.toArray(new Long[allPossibleIps.size()]); return NetUtils.long2Ip(array[_rand.nextInt(array.length)]); - } + } @Override - public void reserve(NicProfile nic, Network network, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException, - InsufficientAddressCapacityException { + public void reserve(NicProfile nic, Network network, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) + throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException { assert (nic.getReservationStrategy() == ReservationStrategy.Start) : "What can I do for nics that are not allocated at start? "; nic.setBroadcastUri(network.getBroadcastUri()); @@ -253,7 +261,7 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru { public boolean trash(Network network, NetworkOffering offering, Account owner) { return true; } - + @Override public void updateNetworkProfile(NetworkProfile networkProfile) { DataCenter dc = _dcDao.findById(networkProfile.getDataCenterId()); diff --git a/server/src/com/cloud/network/guru/OvsGuestNetworkGuru.java b/server/src/com/cloud/network/guru/OvsGuestNetworkGuru.java index ede22924cb8..b61b5a42000 100644 --- a/server/src/com/cloud/network/guru/OvsGuestNetworkGuru.java +++ b/server/src/com/cloud/network/guru/OvsGuestNetworkGuru.java @@ -22,6 +22,7 @@ import javax.ejb.Local; import com.cloud.deploy.DeployDestination; import com.cloud.deploy.DeploymentPlan; +import com.cloud.exception.InsufficientVirtualNetworkCapcityException; import com.cloud.network.Network; import com.cloud.network.NetworkManager; import com.cloud.network.NetworkVO; @@ -58,7 +59,7 @@ public class OvsGuestNetworkGuru extends GuestNetworkGuru { } @Override - public Network implement(Network config, NetworkOffering offering, DeployDestination dest, ReservationContext context) { + public Network implement(Network config, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException { assert (config.getState() == State.Implementing) : "Why are we implementing " + config; if (!_ovsNetworkMgr.isOvsNetworkEnabled()&& !_ovsTunnelMgr.isOvsTunnelEnabled()) { return null; diff --git a/server/src/com/cloud/network/guru/PodBasedNetworkGuru.java b/server/src/com/cloud/network/guru/PodBasedNetworkGuru.java index d1bf4cfb1c3..9cb4abcfa41 100644 --- a/server/src/com/cloud/network/guru/PodBasedNetworkGuru.java +++ b/server/src/com/cloud/network/guru/PodBasedNetworkGuru.java @@ -137,7 +137,7 @@ public class PodBasedNetworkGuru extends AdapterBase implements NetworkGuru { } @Override - public Network implement(Network config, NetworkOffering offering, DeployDestination destination, ReservationContext context) { + public Network implement(Network config, NetworkOffering offering, DeployDestination destination, ReservationContext context) throws InsufficientVirtualNetworkCapcityException { return config; } diff --git a/server/src/com/cloud/network/guru/PublicNetworkGuru.java b/server/src/com/cloud/network/guru/PublicNetworkGuru.java index 69318bc874c..36640203c3a 100644 --- a/server/src/com/cloud/network/guru/PublicNetworkGuru.java +++ b/server/src/com/cloud/network/guru/PublicNetworkGuru.java @@ -165,7 +165,7 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru { } @Override - public Network implement(Network network, NetworkOffering offering, DeployDestination destination, ReservationContext context) { + public Network implement(Network network, NetworkOffering offering, DeployDestination destination, ReservationContext context) throws InsufficientVirtualNetworkCapcityException { return network; }