|
176 | 176 | import com.cloud.event.UsageEventUtils; |
177 | 177 | import com.cloud.event.UsageEventVO; |
178 | 178 | import com.cloud.event.dao.UsageEventDao; |
| 179 | +import com.cloud.exception.AffinityConflictException; |
179 | 180 | import com.cloud.exception.AgentUnavailableException; |
180 | 181 | import com.cloud.exception.CloudException; |
181 | 182 | import com.cloud.exception.ConcurrentOperationException; |
182 | 183 | import com.cloud.exception.InsufficientAddressCapacityException; |
183 | 184 | import com.cloud.exception.InsufficientCapacityException; |
| 185 | +import com.cloud.exception.InsufficientServerCapacityException; |
184 | 186 | import com.cloud.exception.InvalidParameterValueException; |
185 | 187 | import com.cloud.exception.ManagementServerException; |
186 | 188 | import com.cloud.exception.OperationTimedoutException; |
@@ -5627,6 +5629,50 @@ public VirtualMachine migrateVirtualMachine(Long vmId, Host destinationHost) thr |
5627 | 5629 |
|
5628 | 5630 | // check if migrating to same host |
5629 | 5631 | long srcHostId = vm.getHostId(); |
| 5632 | + |
| 5633 | + DeployDestination dest = null; |
| 5634 | + if (destinationHost == null) { |
| 5635 | + vm.setLastHostId(null); // Do not check last host |
| 5636 | + final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); |
| 5637 | + final Host host = _hostDao.findById(srcHostId); |
| 5638 | + final DataCenterDeployment plan = new DataCenterDeployment(host.getDataCenterId(), null, null, null, null, null); |
| 5639 | + ExcludeList excludes = new ExcludeList(); |
| 5640 | + excludes.addHost(srcHostId); |
| 5641 | + try { |
| 5642 | + dest = _planningMgr.planDeployment(profile, plan, excludes, null); |
| 5643 | + } catch (final AffinityConflictException e2) { |
| 5644 | + s_logger.warn("Unable to create deployment, affinity rules associted to the VM conflict", e2); |
| 5645 | + throw new CloudRuntimeException("Unable to create deployment, affinity rules associted to the VM conflict"); |
| 5646 | + } catch (final InsufficientServerCapacityException e3) { |
| 5647 | + throw new CloudRuntimeException("Unable to find a server to migrate the vm to"); |
| 5648 | + } |
| 5649 | + } else { |
| 5650 | + dest = checkVmMigrationDestination(vm, srcHostId, destinationHost); |
| 5651 | + } |
| 5652 | + |
| 5653 | + // If no suitable destination found then throw exception |
| 5654 | + if (dest == null) { |
| 5655 | + throw new RuntimeException("Unable to find suitable destination to migrate VM " + vm.getInstanceName()); |
| 5656 | + } |
| 5657 | + |
| 5658 | + UserVmVO uservm = _vmDao.findById(vmId); |
| 5659 | + if (uservm != null) { |
| 5660 | + collectVmDiskStatistics(uservm); |
| 5661 | + collectVmNetworkStatistics(uservm); |
| 5662 | + } |
| 5663 | + _itMgr.migrate(vm.getUuid(), srcHostId, dest); |
| 5664 | + VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId); |
| 5665 | + if (vmInstance.getType().equals(VirtualMachine.Type.User)) { |
| 5666 | + return _vmDao.findById(vmId); |
| 5667 | + } else { |
| 5668 | + return vmInstance; |
| 5669 | + } |
| 5670 | + } |
| 5671 | + |
| 5672 | + private DeployDestination checkVmMigrationDestination(VMInstanceVO vm, Long srcHostId, Host destinationHost) throws VirtualMachineMigrationException { |
| 5673 | + if (destinationHost == null) { |
| 5674 | + return null; |
| 5675 | + } |
5630 | 5676 | if (destinationHost.getId() == srcHostId) { |
5631 | 5677 | throw new InvalidParameterValueException("Cannot migrate VM, VM is already present on this host, please specify valid destination host to migrate the VM"); |
5632 | 5678 | } |
@@ -5671,24 +5717,13 @@ public VirtualMachine migrateVirtualMachine(Long vmId, Host destinationHost) thr |
5671 | 5717 | + " already has max Running VMs(count includes system VMs), cannot migrate to this host"); |
5672 | 5718 | } |
5673 | 5719 | //check if there are any ongoing volume snapshots on the volumes associated with the VM. |
| 5720 | + Long vmId = vm.getId(); |
5674 | 5721 | s_logger.debug("Checking if there are any ongoing snapshots volumes associated with VM with ID " + vmId); |
5675 | 5722 | if (checkStatusOfVolumeSnapshots(vmId, null)) { |
5676 | 5723 | throw new CloudRuntimeException("There is/are unbacked up snapshot(s) on volume(s) attached to this VM, VM Migration is not permitted, please try again later."); |
5677 | 5724 | } |
5678 | 5725 | s_logger.debug("Found no ongoing snapshots on volumes associated with the vm with id " + vmId); |
5679 | | - |
5680 | | - UserVmVO uservm = _vmDao.findById(vmId); |
5681 | | - if (uservm != null) { |
5682 | | - collectVmDiskStatistics(uservm); |
5683 | | - collectVmNetworkStatistics(uservm); |
5684 | | - } |
5685 | | - _itMgr.migrate(vm.getUuid(), srcHostId, dest); |
5686 | | - VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId); |
5687 | | - if (vmInstance.getType().equals(VirtualMachine.Type.User)) { |
5688 | | - return _vmDao.findById(vmId); |
5689 | | - } else { |
5690 | | - return vmInstance; |
5691 | | - } |
| 5726 | + return dest; |
5692 | 5727 | } |
5693 | 5728 |
|
5694 | 5729 | private boolean isOnSupportedHypevisorForMigration(VMInstanceVO vm) { |
|
0 commit comments