shwstppr commented on a change in pull request #4895:
URL: https://github.com/apache/cloudstack/pull/4895#discussion_r608380468



##########
File path: 
plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/guru/VMwareGuru.java
##########
@@ -209,16 +209,35 @@ protected VMwareGuru() {
         return vmwareVmImplementer.implement(vm, toVirtualMachineTO(vm), 
getClusterId(vm.getId()));
     }
 
-    long getClusterId(long vmId) {
-        long clusterId;
-        Long hostId;
-
-        hostId = _vmDao.findById(vmId).getHostId();
-        if (hostId == null) {
+    Long getClusterId(long vmId) {
+        Long clusterId = null;
+        Long hostId = null;
+        VMInstanceVO vm = _vmDao.findById(vmId);
+        if (vm != null) {
+            hostId = _vmDao.findById(vmId).getHostId();
+        }
+        if (vm != null && hostId == null) {
             // If VM is in stopped state then hostId would be undefined. Hence 
read last host's Id instead.
             hostId = _vmDao.findById(vmId).getLastHostId();
         }
-        clusterId = _hostDao.findById(hostId).getClusterId();
+        HostVO host = null;
+        if (hostId != null) {
+            host = _hostDao.findById(hostId);
+        }
+        if (host != null) {
+            clusterId = host.getClusterId();
+        } else {
+            List<VolumeVO> volumes = _volumeDao.findByInstanceAndType(vmId, 
Volume.Type.ROOT);
+            if (CollectionUtils.isNotEmpty(volumes)) {
+                VolumeVO rootVolume = volumes.get(0);
+                if (rootVolume.getPoolId() != null) {
+                    StoragePoolVO pool = 
_storagePoolDao.findById(rootVolume.getPoolId());
+                    if (pool != null && pool.getClusterId() != null) {
+                        clusterId = pool.getClusterId();
+                    }
+                }
+            }

Review comment:
       @DaanHoogland done

##########
File path: 
plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/guru/VMwareGuru.java
##########
@@ -1066,14 +1087,29 @@ private VirtualDisk getAttachedDisk(VirtualMachineMO 
vmMo, String diskPath) thro
             VolumeTO vol = new VolumeTO(volume, destination);
             vols.add(vol);
         }
-        MigrateVmToPoolCommand migrateVmToPoolCommand = new 
MigrateVmToPoolCommand(vm.getInstanceName(), vols, destination.getUuid(), true);
-        commands.add(migrateVmToPoolCommand);
 
-        // OfflineVmwareMigration: cleanup if needed
         final Long destClusterId = destination.getClusterId();
         final Long srcClusterId = getClusterId(vm.getId());
+        final boolean isInterClusterMigration = srcClusterId != null && 
destClusterId != null && ! srcClusterId.equals(destClusterId);
+        Host hostInTargetCluster = null;
+        if (isInterClusterMigration) {
+            // Without host vMotion might fail between non-shared storages 
with error similar to,
+            // https://kb.vmware.com/s/article/1003795
+            // As this is offline migration VM won't be started on this host
+            List<HostVO> hosts = 
_hostDao.findHypervisorHostInCluster(destClusterId);
+            if (CollectionUtils.isNotEmpty(hosts)) {
+                hostInTargetCluster = hosts.get(0);
+            }
+            if (hostInTargetCluster == null) {
+                throw new CloudRuntimeException("Migration failed, unable to 
find suitable target host for VM placement while migrating between storage 
pools of different clusters without shared storages");
+            }
+        }
+        MigrateVmToPoolCommand migrateVmToPoolCommand = new 
MigrateVmToPoolCommand(vm.getInstanceName(),
+                vols, destination.getUuid(), hostInTargetCluster == null ? 
null : hostInTargetCluster.getGuid(), true);
+        commands.add(migrateVmToPoolCommand);

Review comment:
       @DaanHoogland done

##########
File path: 
plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java
##########
@@ -4413,9 +4418,17 @@ protected Answer execute(MigrateVmToPoolCommand cmd) {
         }
     }
 
-    private Answer migrateAndAnswer(VirtualMachineMO vmMo, String poolUuid, 
VmwareHypervisorHost hyperHost, Command cmd) throws Exception {
-        ManagedObjectReference morDs = getTargetDatastoreMOReference(poolUuid, 
hyperHost);
-
+    private Answer migrateAndAnswer(VirtualMachineMO vmMo, String poolUuid, 
VmwareHypervisorHost sourceHyperHost,

Review comment:
       @DaanHoogland done




-- 
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to