Skip to content

Commit 7df8733

Browse files
committed
use any available host if last host is not there
1 parent 2c18496 commit 7df8733

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,17 +2049,32 @@ public Boolean doInTransaction(TransactionStatus status) {
20492049

20502050
void persistDomainForKVM(VMInstanceVO vm) {
20512051
Long hostId = vm.getHostId();
2052+
String vmName = vm.getName();
20522053
UnmanageInstanceCommand unmanageInstanceCommand;
20532054
if (State.Stopped.equals(vm.getState())) {
2054-
hostId = vm.getLastHostId();
2055+
Pair<Long, Long> clusterAndHostId = findClusterAndHostIdForVm(vm.getLastHostId());
2056+
Long clusterId = clusterAndHostId.first();
2057+
hostId = clusterAndHostId.second();
2058+
if (hostId == null) {
2059+
logger.debug("No previous host found for Instance: {}. " +
2060+
"Searching for any available hosts in cluster with ID: {}.", vmName, clusterId);
2061+
List<HostVO> availableHosts = _resourceMgr.listAllUpAndEnabledHosts(Host.Type.Routing, clusterId,
2062+
null, vm.getDataCenterId());
2063+
if (availableHosts.isEmpty()) {
2064+
String errorMsg = "No available host to persist domainXML for Instance: " + vmName;
2065+
logger.debug(errorMsg);
2066+
throw new CloudRuntimeException(errorMsg);
2067+
}
2068+
hostId = availableHosts.get(0).getId();
2069+
}
20552070
unmanageInstanceCommand = new UnmanageInstanceCommand(prepVmSpecForUnmanageCmd(vm.getId(), hostId)); // reconstruct vmSpec for stopped instance
20562071
} else {
2057-
unmanageInstanceCommand = new UnmanageInstanceCommand(vm.getName());
2072+
unmanageInstanceCommand = new UnmanageInstanceCommand(vmName);
20582073
}
20592074
try {
20602075
Answer answer = _agentMgr.send(hostId, unmanageInstanceCommand);
20612076
if (!answer.getResult()) {
2062-
String errorMsg = "Failed to persist domainXML for instance: " + vm.getName();
2077+
String errorMsg = "Failed to persist domainXML for instance: " + vmName;
20632078
logger.debug(errorMsg);
20642079
throw new CloudRuntimeException(errorMsg);
20652080
}

0 commit comments

Comments
 (0)