weizhouapache commented on a change in pull request #4378:
URL: https://github.com/apache/cloudstack/pull/4378#discussion_r662924155



##########
File path: server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
##########
@@ -5895,8 +5897,46 @@ public VirtualMachine migrateVirtualMachine(Long vmId, 
Host destinationHost) thr
             throw new InvalidParameterValueException("Cannot migrate VM, host 
with id: " + srcHostId + " for VM not found");
         }
 
+        DeployDestination dest = null;
+        if (destinationHost == null) {
+            vm.setLastHostId(null); // Last host does not have higher priority 
in vm migration
+            final ServiceOfferingVO offering = 
_offeringDao.findById(vm.getId(), vm.getServiceOfferingId());
+            final VirtualMachineProfile profile = new 
VirtualMachineProfileImpl(vm, null, offering, null, null);
+            final Host host = _hostDao.findById(srcHostId);
+            final DataCenterDeployment plan = new 
DataCenterDeployment(host.getDataCenterId(), host.getPodId(), 
host.getClusterId(), null, null, null);
+            ExcludeList excludes = new ExcludeList();
+            excludes.addHost(srcHostId);
+            try {
+                dest = _planningMgr.planDeployment(profile, plan, excludes, 
null);
+            } catch (final AffinityConflictException e2) {
+                s_logger.warn("Unable to create deployment, affinity rules 
associted to the VM conflict", e2);
+                throw new CloudRuntimeException("Unable to create deployment, 
affinity rules associted to the VM conflict");
+            } catch (final InsufficientServerCapacityException e3) {
+                throw new CloudRuntimeException("Unable to find a server to 
migrate the vm to");
+            }
+        } else {
+            dest = checkVmMigrationDestination(vm, srcHost, destinationHost);
+        }
 
-        if (destinationHost.getId() == srcHostId) {
+        // If no suitable destination found then throw exception
+        if (dest == null) {
+            throw new CloudRuntimeException("Unable to find suitable 
destination to migrate VM " + vm.getInstanceName());
+        }
+
+        UserVmVO uservm = _vmDao.findById(vmId);
+        if (uservm != null) {
+            collectVmDiskStatistics(uservm);
+            collectVmNetworkStatistics(uservm);
+        }
+        _itMgr.migrate(vm.getUuid(), srcHostId, dest);

Review comment:
       thanks for review and suggestions @DaanHoogland 

##########
File path: server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
##########
@@ -5895,8 +5897,46 @@ public VirtualMachine migrateVirtualMachine(Long vmId, 
Host destinationHost) thr
             throw new InvalidParameterValueException("Cannot migrate VM, host 
with id: " + srcHostId + " for VM not found");
         }
 
+        DeployDestination dest = null;
+        if (destinationHost == null) {
+            vm.setLastHostId(null); // Last host does not have higher priority 
in vm migration
+            final ServiceOfferingVO offering = 
_offeringDao.findById(vm.getId(), vm.getServiceOfferingId());
+            final VirtualMachineProfile profile = new 
VirtualMachineProfileImpl(vm, null, offering, null, null);
+            final Host host = _hostDao.findById(srcHostId);
+            final DataCenterDeployment plan = new 
DataCenterDeployment(host.getDataCenterId(), host.getPodId(), 
host.getClusterId(), null, null, null);
+            ExcludeList excludes = new ExcludeList();
+            excludes.addHost(srcHostId);
+            try {
+                dest = _planningMgr.planDeployment(profile, plan, excludes, 
null);
+            } catch (final AffinityConflictException e2) {
+                s_logger.warn("Unable to create deployment, affinity rules 
associted to the VM conflict", e2);
+                throw new CloudRuntimeException("Unable to create deployment, 
affinity rules associted to the VM conflict");
+            } catch (final InsufficientServerCapacityException e3) {
+                throw new CloudRuntimeException("Unable to find a server to 
migrate the vm to");
+            }
+        } else {
+            dest = checkVmMigrationDestination(vm, srcHost, destinationHost);
+        }
 
-        if (destinationHost.getId() == srcHostId) {
+        // If no suitable destination found then throw exception
+        if (dest == null) {
+            throw new CloudRuntimeException("Unable to find suitable 
destination to migrate VM " + vm.getInstanceName());
+        }
+
+        UserVmVO uservm = _vmDao.findById(vmId);
+        if (uservm != null) {
+            collectVmDiskStatistics(uservm);
+            collectVmNetworkStatistics(uservm);
+        }
+        _itMgr.migrate(vm.getUuid(), srcHostId, dest);

Review comment:
       Done. thanks for review and suggestions @DaanHoogland 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to