Don't retry to re-create the vm when failed to allocate public ip

This commit is contained in:
Alena Prokharchyk 2012-01-18 11:21:04 -08:00
parent 566f101be1
commit d4d345a587
5 changed files with 48 additions and 42 deletions

View File

@ -19,6 +19,7 @@ package com.cloud.network.rules;
import java.util.List;
import com.cloud.exception.InsufficientAddressCapacityException;
import com.cloud.exception.NetworkRuleConflictException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.network.IpAddress;
@ -71,7 +72,7 @@ public interface RulesManager extends RulesService {
boolean applyStaticNatsForNetwork(long networkId, boolean continueOnError, Account caller);
boolean enableElasticIpAndStaticNatForVm(UserVm vm, boolean stopOnError);
void enableElasticIpAndStaticNatForVm(UserVm vm) throws InsufficientAddressCapacityException;
boolean disableStaticNat(long ipAddressId, Account caller, long callerUserId, boolean releaseIpIfElastic) throws ResourceUnavailableException;

View File

@ -36,6 +36,7 @@ import com.cloud.event.EventTypes;
import com.cloud.event.UsageEventVO;
import com.cloud.event.dao.EventDao;
import com.cloud.event.dao.UsageEventDao;
import com.cloud.exception.InsufficientAddressCapacityException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.NetworkRuleConflictException;
import com.cloud.exception.ResourceUnavailableException;
@ -1158,7 +1159,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager {
@Override
public boolean enableElasticIpAndStaticNatForVm(UserVm vm, boolean stopOnError) {
public void enableElasticIpAndStaticNatForVm(UserVm vm) throws InsufficientAddressCapacityException{
boolean success = true;
//enable static nat if eIp capability is supported
@ -1167,43 +1168,39 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager {
Network guestNetwork = _networkMgr.getNetwork(nic.getNetworkId());
NetworkOffering offering = _configMgr.getNetworkOffering(guestNetwork.getNetworkOfferingId());
if (offering.getElasticIp()) {
try {
//check if there is already static nat enabled
if (_ipAddressDao.findByAssociatedVmId(vm.getId()) != null) {
s_logger.debug("Vm " + vm + " already has elastic ip associated with it in guest network " + guestNetwork);
continue;
}
s_logger.debug("Allocating elastic ip and enabling static nat for it for the vm " + vm + " in guest network " + guestNetwork);
IpAddress ip = _networkMgr.assignElasticIp(guestNetwork.getId(), _accountMgr.getAccount(vm.getAccountId()), false, true);
if (ip == null) {
s_logger.warn("Failed to allocate elastic ip for vm " + vm + " in guest network " + guestNetwork);
return false;
}
s_logger.debug("Allocated elastic ip " + ip + ", now enabling static nat on it for vm " + vm);
try {
enableStaticNat(ip.getId(), vm.getId());
} catch (Exception ex) {
s_logger.warn("Failed to enable static nat as a part of enabling elasticIp and staticNat for vm " + vm + " in guest network " + guestNetwork + " due to exception ", ex);
success = false;
}
if (!success) {
s_logger.warn("Failed to enable static nat on elastic ip " + ip + " for the vm " + vm + ", releasing the ip...");
_networkMgr.handleElasticIpRelease(ip);
} else {
s_logger.warn("Succesfully enabled static nat on elastic ip " + ip + " for the vm " + vm);
}
} catch (Exception ex) {
s_logger.warn("Failed to acquire elastic ip and enable static nat for vm " + vm + " on the network " + guestNetwork + " due to exception ", ex);
} finally {
if (!success && stopOnError) {
return false;
}
}
//check if there is already static nat enabled
if (_ipAddressDao.findByAssociatedVmId(vm.getId()) != null) {
s_logger.debug("Vm " + vm + " already has elastic ip associated with it in guest network " + guestNetwork);
continue;
}
s_logger.debug("Allocating elastic ip and enabling static nat for it for the vm " + vm + " in guest network " + guestNetwork);
IpAddress ip = _networkMgr.assignElasticIp(guestNetwork.getId(), _accountMgr.getAccount(vm.getAccountId()), false, true);
if (ip == null) {
throw new CloudRuntimeException("Failed to allocate elastic ip for vm " + vm + " in guest network " + guestNetwork);
}
s_logger.debug("Allocated elastic ip " + ip + ", now enabling static nat on it for vm " + vm);
try {
enableStaticNat(ip.getId(), vm.getId());
} catch (NetworkRuleConflictException ex) {
s_logger.warn("Failed to enable static nat as a part of enabling elasticIp and staticNat for vm " + vm + " in guest network " + guestNetwork + " due to exception ", ex);
success = false;
} catch (ResourceUnavailableException ex) {
s_logger.warn("Failed to enable static nat as a part of enabling elasticIp and staticNat for vm " + vm + " in guest network " + guestNetwork + " due to exception ", ex);
success = false;
}
if (!success) {
s_logger.warn("Failed to enable static nat on elastic ip " + ip + " for the vm " + vm + ", releasing the ip...");
_networkMgr.handleElasticIpRelease(ip);
} else {
s_logger.warn("Succesfully enabled static nat on elastic ip " + ip + " for the vm " + vm);
}
}
}
return success;
}
}

View File

@ -102,6 +102,7 @@ import com.cloud.event.UsageEventVO;
import com.cloud.event.dao.EventDao;
import com.cloud.event.dao.UsageEventDao;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientAddressCapacityException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.ManagementServerException;
@ -2666,7 +2667,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
}
@Override
public boolean finalizeStart(VirtualMachineProfile<UserVmVO> profile, long hostId, Commands cmds, ReservationContext context) {
public boolean finalizeStart(VirtualMachineProfile<UserVmVO> profile, long hostId, Commands cmds, ReservationContext context) throws InsufficientAddressCapacityException{
UserVmVO vm = profile.getVirtualMachine();
Answer[] answersToCmds = cmds.getAnswers();
@ -2730,7 +2731,9 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
}
//enable elastic ip for vm
return _rulesMgr.enableElasticIpAndStaticNatForVm(profile.getVirtualMachine(), true);
_rulesMgr.enableElasticIpAndStaticNatForVm(profile.getVirtualMachine());
return true;
}
@Override

View File

@ -20,6 +20,7 @@ package com.cloud.vm;
import com.cloud.agent.api.StopAnswer;
import com.cloud.agent.manager.Commands;
import com.cloud.deploy.DeployDestination;
import com.cloud.exception.InsufficientAddressCapacityException;
import com.cloud.exception.ResourceUnavailableException;
/**
@ -55,8 +56,9 @@ public interface VirtualMachineGuru<T extends VirtualMachine> {
* @param profile virtual machine profile.
* @param dest destination it was sent to.
* @return true if deployment was fine; false if it didn't go well.
* @throws InsufficientAddressCapacityException
*/
boolean finalizeStart(VirtualMachineProfile<T> profile, long hostId, Commands cmds, ReservationContext context);
boolean finalizeStart(VirtualMachineProfile<T> profile, long hostId, Commands cmds, ReservationContext context) throws InsufficientAddressCapacityException;
boolean finalizeCommandsOnStart(Commands cmds, VirtualMachineProfile<T> profile);

View File

@ -1999,7 +1999,10 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene
} catch (ResourceUnavailableException e) {
s_logger.error("Exception during update for running vm: " + vm, e);
return null;
} catch (NoTransitionException e) {
}catch (InsufficientAddressCapacityException e) {
s_logger.error("Exception during update for running vm: " + vm, e);
return null;
}catch (NoTransitionException e) {
s_logger.warn(e.getMessage());
}
}
@ -2016,7 +2019,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene
return command;
}
private void ensureVmRunningContext(long hostId, VMInstanceVO vm, Event cause) throws OperationTimedoutException, ResourceUnavailableException, NoTransitionException {
private void ensureVmRunningContext(long hostId, VMInstanceVO vm, Event cause) throws OperationTimedoutException, ResourceUnavailableException, NoTransitionException, InsufficientAddressCapacityException {
VirtualMachineGuru<VMInstanceVO> vmGuru = getVmGuru(vm);
s_logger.debug("VM state is starting on full sync so updating it to running");