diff --git a/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java b/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java index fa8b7b6ca20..be88b4e7e25 100755 --- a/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java @@ -1283,13 +1283,27 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac s_logger.debug("Cleaning up resources for the vm " + vm + " in " + state + " state"); try { if (state == State.Starting) { - Step step = work.getStep(); - if (step == Step.Starting && !cleanUpEvenIfUnableToStop) { - s_logger.warn("Unable to cleanup vm " + vm + "; work state is incorrect: " + step); - return false; - } + if (work != null) { + Step step = work.getStep(); + if (step == Step.Starting && !cleanUpEvenIfUnableToStop) { + s_logger.warn("Unable to cleanup vm " + vm + "; work state is incorrect: " + step); + return false; + } - if (step == Step.Started || step == Step.Starting || step == Step.Release) { + if (step == Step.Started || step == Step.Starting || step == Step.Release) { + if (vm.getHostId() != null) { + if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) { + s_logger.warn("Failed to stop vm " + vm + " in " + State.Starting + " state as a part of cleanup process"); + return false; + } + } + } + + if (step != Step.Release && step != Step.Prepare && step != Step.Started && step != Step.Starting) { + s_logger.debug("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step); + return true; + } + } else { if (vm.getHostId() != null) { if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) { s_logger.warn("Failed to stop vm " + vm + " in " + State.Starting + " state as a part of cleanup process"); @@ -1298,10 +1312,6 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac } } - if (step != Step.Release && step != Step.Prepare && step != Step.Started && step != Step.Starting) { - s_logger.debug("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step); - return true; - } } else if (state == State.Stopping) { if (vm.getHostId() != null) { if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) { @@ -1453,20 +1463,10 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac if (!cleanUpEvenIfUnableToStop) { throw new CloudRuntimeException("We cannot stop " + vm + " when it is in state " + vm.getState()); } - boolean doCleanup = false; + boolean doCleanup = true; if (s_logger.isDebugEnabled()) { s_logger.debug("Unable to transition the state but we're moving on because it's forced stop"); } - if ((state == State.Starting) || (state == State.Migrating) || (state == State.Stopping)) { - if (work != null) { - doCleanup = true; - } else { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Unable to cleanup VM: " + vm + " ,since outstanding work item is not found"); - } - throw new CloudRuntimeException("Work item not found, We cannot stop " + vm + " when it is in state " + vm.getState()); - } - } if (doCleanup) { if (cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.StopRequested, cleanUpEvenIfUnableToStop)) { diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DataCenterDaoImpl.java index 67da956746f..373446e780a 100755 --- a/engine/schema/src/com/cloud/dc/dao/DataCenterDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/DataCenterDaoImpl.java @@ -250,6 +250,7 @@ public class DataCenterDaoImpl extends GenericDaoBase implem @Override public Pair allocatePrivateIpAddress(long dcId, long podId, long instanceId, String reservationId) { + _ipAllocDao.releaseIpAddress(instanceId); DataCenterIpAddressVO vo = _ipAllocDao.takeIpAddress(dcId, podId, instanceId, reservationId); if (vo == null) { return null; diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDao.java b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDao.java index 5a1572ec269..7f1ec4d40da 100644 --- a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDao.java +++ b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDao.java @@ -33,6 +33,8 @@ public interface DataCenterIpAddressDao extends GenericDao listByPodIdDcIdIpAddress(long podId, long dcId, String ipAddress); diff --git a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java index 80936c0d2a4..9bc373f8f3e 100755 --- a/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java +++ b/engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java @@ -177,6 +177,22 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase listByPodIdDcId(long podId, long dcId) { SearchCriteria sc = AllFieldsSearch.create();