@@ -2317,18 +2317,24 @@ private void orchestrateStorageMigration(final String vmUuid, final Map<Long, Lo
23172317 }
23182318 }
23192319
2320- private Answer [] attemptHypervisorMigration (VMInstanceVO vm , Map <Volume , StoragePool > volumeToPool , Long hostId ) {
2321- if (hostId == null ) {
2322- return null ;
2323- }
2320+ private Answer [] attemptHypervisorMigration (StoragePool destPool , VMInstanceVO vm ) {
23242321 final HypervisorGuru hvGuru = _hvGuruMgr .getGuru (vm .getHypervisorType ());
23252322 // OfflineVmwareMigration: in case of vmware call vcenter to do it for us.
23262323 // OfflineVmwareMigration: should we check the proximity of source and destination
23272324 // OfflineVmwareMigration: if we are in the same cluster/datacentre/pool or whatever?
23282325 // OfflineVmwareMigration: we are checking on success to optionally delete an old vm if we are not
23292326 List <Command > commandsToSend = hvGuru .finalizeMigrate (vm , volumeToPool );
23302327
2331- if (CollectionUtils .isNotEmpty (commandsToSend )) {
2328+ Long hostId = vm .getHostId ();
2329+ // OfflineVmwareMigration: probably this is null when vm is stopped
2330+ if (hostId == null ) {
2331+ hostId = vm .getLastHostId ();
2332+ if (s_logger .isDebugEnabled ()) {
2333+ s_logger .debug (String .format ("host id is null, using last host id %d" , hostId ) );
2334+ }
2335+ }
2336+
2337+ if (CollectionUtils .isNotEmpty (commandsToSend )) {
23322338 Commands commandsContainer = new Commands (Command .OnError .Stop );
23332339 commandsContainer .addCommands (commandsToSend );
23342340 try {
@@ -2342,7 +2348,7 @@ private Answer[] attemptHypervisorMigration(VMInstanceVO vm, Map<Volume, Storage
23422348 return null ;
23432349 }
23442350
2345- private void afterHypervisorMigrationCleanup (VMInstanceVO vm , Map < Volume , StoragePool > volumeToPool , Long sourceClusterId , Answer [] hypervisorMigrationResults ) throws InsufficientCapacityException {
2351+ private void afterHypervisorMigrationCleanup (StoragePool destPool , VMInstanceVO vm , Answer [] hypervisorMigrationResults ) throws InsufficientCapacityException {
23462352 boolean isDebugEnabled = s_logger .isDebugEnabled ();
23472353 if (isDebugEnabled ) {
23482354 String msg = String .format ("Cleaning up after hypervisor pool migration volumes for VM %s(%s)" , vm .getInstanceName (), vm .getUuid ());
@@ -2367,8 +2373,8 @@ private void afterHypervisorMigrationCleanup(VMInstanceVO vm, Map<Volume, Storag
23672373 vm .setLastHostId (null );
23682374 vm .setPodIdToDeployIn (rootVolumePool .getPodId ());
23692375 // OfflineVmwareMigration: a consecutive migration will fail probably (no host not pod)
2370- }// else keep last host set for this vm
2371- markVolumesInPool (vm , hypervisorMigrationResults );
2376+ } // else keep last host set for this vm
2377+ markVolumesInPool (vm , destPool , hypervisorMigrationResults );
23722378 // OfflineVmwareMigration: deal with answers, if (hypervisorMigrationResults.length > 0)
23732379 // OfflineVmwareMigration: iterate over the volumes for data updates
23742380 }
@@ -2415,21 +2421,17 @@ private void markVolumesInPool(VMInstanceVO vm, Answer[] hypervisorMigrationResu
24152421
24162422 private void migrateThroughHypervisorOrStorage (VMInstanceVO vm , Map <Volume , StoragePool > volumeToPool ) throws StorageUnavailableException , InsufficientCapacityException {
24172423 final VirtualMachineProfile profile = new VirtualMachineProfileImpl (vm );
2418- Pair <Long , Long > vmClusterAndHost = findClusterAndHostIdForVm (vm );
2419- final Long sourceClusterId = vmClusterAndHost .first ();
2420- final Long sourceHostId = vmClusterAndHost .second ();
2421- Answer [] hypervisorMigrationResults = attemptHypervisorMigration (vm , volumeToPool , sourceHostId );
2422- boolean migrationResult = false ;
2424+ Answer [] hypervisorMigrationResults = attemptHypervisorMigration (destPool , vm );
24232425 if (hypervisorMigrationResults == null ) {
24242426 // OfflineVmwareMigration: if the HypervisorGuru can't do it, let the volume manager take care of it.
2425- migrationResult = volumeMgr .storageMigration (profile , volumeToPool );
2427+ boolean migrationResult = volumeMgr .storageMigration (profile , destPool );
24262428 if (migrationResult ) {
2427- postStorageMigrationCleanup ( vm , volumeToPool , _hostDao . findById ( sourceHostId ), sourceClusterId );
2429+ afterStorageMigrationCleanup ( destPool , vm );
24282430 } else {
24292431 s_logger .debug ("Storage migration failed" );
24302432 }
24312433 } else {
2432- afterHypervisorMigrationCleanup (vm , volumeToPool , sourceClusterId , hypervisorMigrationResults );
2434+ afterHypervisorMigrationCleanup (destPool , vm , hypervisorMigrationResults );
24332435 }
24342436 }
24352437
@@ -2521,6 +2523,9 @@ private void postStorageMigrationCleanup(VMInstanceVO vm, Map<Volume, StoragePoo
25212523 }
25222524 setDestinationPoolAndReallocateNetwork (rootVolumePool , vm );
25232525
2526+ private void afterStorageMigrationCleanup (StoragePool destPool , VMInstanceVO vm ) throws InsufficientCapacityException {
2527+ setDestinationPoolAndReallocateNetwork (destPool , vm );
2528+
25242529 //when start the vm next time, don;'t look at last_host_id, only choose the host based on volume/storage pool
25252530 vm .setLastHostId (null );
25262531 if (rootVolumePool != null ) {
@@ -2530,7 +2535,7 @@ private void postStorageMigrationCleanup(VMInstanceVO vm, Map<Volume, StoragePoo
25302535 // If VM was cold migrated between clusters belonging to two different VMware DCs,
25312536 // unregister the VM from the source host and cleanup the associated VM files.
25322537 if (vm .getHypervisorType ().equals (HypervisorType .VMware )) {
2533- afterStorageMigrationVmwareVMCleanup (rootVolumePool , vm , srcHost , srcClusterId );
2538+ afterStorageMigrationVmwareVMCleanup (destPool , vm );
25342539 }
25352540 }
25362541
@@ -2548,15 +2553,12 @@ private void setDestinationPoolAndReallocateNetwork(StoragePool destPool, VMInst
25482553 }
25492554 }
25502555
2551- private void afterStorageMigrationVmwareVMCleanup (StoragePool destPool , VMInstanceVO vm , HostVO srcHost , Long srcClusterId ) {
2556+ private void afterStorageMigrationVmwareVMCleanup (StoragePool destPool , VMInstanceVO vm ) {
25522557 // OfflineVmwareMigration: this should only happen on storage migration, else the guru would already have issued the command
2553- final Long destClusterId = destPool .getClusterId ();
2554- if (srcClusterId != null && destClusterId != null && ! srcClusterId .equals (destClusterId ) && srcHost != null ) {
2555- final String srcDcName = _clusterDetailsDao .getVmwareDcName (srcClusterId );
2556- final String destDcName = _clusterDetailsDao .getVmwareDcName (destClusterId );
2557- if (srcDcName != null && destDcName != null && !srcDcName .equals (destDcName )) {
2558- removeStaleVmFromSource (vm , srcHost );
2559- }
2558+ Long destClusterId = destPool .getClusterId ();
2559+ if (destClusterId == null ) {
2560+ s_logger .debug (String .format ("No Cluster destination ID was found when doing cleanup after Vmware" ));
2561+ return ;
25602562 }
25612563 Long srcHostId = vm .getHostId () != null ? vm .getHostId () : vm .getLastHostId ();
25622564 if (srcHostId == null ) {
0 commit comments