Skip to content

Commit ab19edf

Browse files
author
Anthony Xu
committed
CLOUDSTACK-7742:
root cause: when vmsync reports system VM is down, CCP doesn't release the VM resource before starting it. fix: make sure cleanup is called for a VM when it is reported as Stopped
1 parent bd79965 commit ab19edf

4 files changed

Lines changed: 40 additions & 21 deletions

File tree

‎engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,13 +1276,27 @@ protected boolean cleanup(VirtualMachineGuru guru, VirtualMachineProfile profile
12761276
s_logger.debug("Cleaning up resources for the vm " + vm + " in " + state + " state");
12771277
try {
12781278
if (state == State.Starting) {
1279-
Step step = work.getStep();
1280-
if (step == Step.Starting && !cleanUpEvenIfUnableToStop) {
1281-
s_logger.warn("Unable to cleanup vm " + vm + "; work state is incorrect: " + step);
1282-
return false;
1283-
}
1279+
if (work != null) {
1280+
Step step = work.getStep();
1281+
if (step == Step.Starting && !cleanUpEvenIfUnableToStop) {
1282+
s_logger.warn("Unable to cleanup vm " + vm + "; work state is incorrect: " + step);
1283+
return false;
1284+
}
1285+
1286+
if (step == Step.Started || step == Step.Starting || step == Step.Release) {
1287+
if (vm.getHostId() != null) {
1288+
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
1289+
s_logger.warn("Failed to stop vm " + vm + " in " + State.Starting + " state as a part of cleanup process");
1290+
return false;
1291+
}
1292+
}
1293+
}
12841294

1285-
if (step == Step.Started || step == Step.Starting || step == Step.Release) {
1295+
if (step != Step.Release && step != Step.Prepare && step != Step.Started && step != Step.Starting) {
1296+
s_logger.debug("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step);
1297+
return true;
1298+
}
1299+
} else {
12861300
if (vm.getHostId() != null) {
12871301
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
12881302
s_logger.warn("Failed to stop vm " + vm + " in " + State.Starting + " state as a part of cleanup process");
@@ -1291,10 +1305,6 @@ protected boolean cleanup(VirtualMachineGuru guru, VirtualMachineProfile profile
12911305
}
12921306
}
12931307

1294-
if (step != Step.Release && step != Step.Prepare && step != Step.Started && step != Step.Starting) {
1295-
s_logger.debug("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step);
1296-
return true;
1297-
}
12981308
} else if (state == State.Stopping) {
12991309
if (vm.getHostId() != null) {
13001310
if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
@@ -1446,20 +1456,10 @@ private void advanceStop(VMInstanceVO vm, boolean cleanUpEvenIfUnableToStop) thr
14461456
if (!cleanUpEvenIfUnableToStop) {
14471457
throw new CloudRuntimeException("We cannot stop " + vm + " when it is in state " + vm.getState());
14481458
}
1449-
boolean doCleanup = false;
1459+
boolean doCleanup = true;
14501460
if (s_logger.isDebugEnabled()) {
14511461
s_logger.debug("Unable to transition the state but we're moving on because it's forced stop");
14521462
}
1453-
if ((state == State.Starting) || (state == State.Migrating) || (state == State.Stopping)) {
1454-
if (work != null) {
1455-
doCleanup = true;
1456-
} else {
1457-
if (s_logger.isDebugEnabled()) {
1458-
s_logger.debug("Unable to cleanup VM: " + vm + " ,since outstanding work item is not found");
1459-
}
1460-
throw new CloudRuntimeException("Work item not found, We cannot stop " + vm + " when it is in state " + vm.getState());
1461-
}
1462-
}
14631463

14641464
if (doCleanup) {
14651465
if (cleanup(vmGuru, new VirtualMachineProfileImpl(vm), work, Event.StopRequested, cleanUpEvenIfUnableToStop)) {

‎engine/schema/src/com/cloud/dc/dao/DataCenterDaoImpl.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ public String[] getNextAvailableMacAddressPair(long id, long mask) {
250250

251251
@Override
252252
public Pair<String, Long> allocatePrivateIpAddress(long dcId, long podId, long instanceId, String reservationId) {
253+
_ipAllocDao.releaseIpAddress(instanceId);
253254
DataCenterIpAddressVO vo = _ipAllocDao.takeIpAddress(dcId, podId, instanceId, reservationId);
254255
if (vo == null) {
255256
return null;

‎engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDao.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public interface DataCenterIpAddressDao extends GenericDao<DataCenterIpAddressVO
3333

3434
public void releaseIpAddress(long nicId, String reservationId);
3535

36+
public void releaseIpAddress(long nicId);
37+
3638
boolean mark(long dcId, long podId, String ip);
3739

3840
List<DataCenterIpAddressVO> listByPodIdDcIdIpAddress(long podId, long dcId, String ipAddress);

‎engine/schema/src/com/cloud/dc/dao/DataCenterIpAddressDaoImpl.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,22 @@ public void releaseIpAddress(long nicId, String reservationId) {
177177
update(vo, sc);
178178
}
179179

180+
@Override
181+
public void releaseIpAddress(long nicId) {
182+
if (s_logger.isDebugEnabled()) {
183+
s_logger.debug("Releasing ip address for instance=" + nicId);
184+
}
185+
SearchCriteria<DataCenterIpAddressVO> sc = AllFieldsSearch.create();
186+
sc.setParameters("instance", nicId);
187+
188+
DataCenterIpAddressVO vo = createForUpdate();
189+
vo.setTakenAt(null);
190+
vo.setInstanceId(null);
191+
vo.setReservationId(null);
192+
update(vo, sc);
193+
}
194+
195+
180196
@Override
181197
public List<DataCenterIpAddressVO> listByPodIdDcId(long podId, long dcId) {
182198
SearchCriteria<DataCenterIpAddressVO> sc = AllFieldsSearch.create();

0 commit comments

Comments
 (0)