2424import java .util .Comparator ;
2525import java .util .Date ;
2626import java .util .HashMap ;
27- import java .util .Iterator ;
2827import java .util .List ;
2928import java .util .Map ;
3029import java .util .TimeZone ;
@@ -1275,36 +1274,32 @@ public Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Ho
12751274 final Type hostType = srcHost .getType ();
12761275 Pair <List <HostVO >, Integer > allHostsPair = null ;
12771276 List <HostVO > allHosts = null ;
1277+ Integer allHostsCount = 0 ;
12781278 final Map <Host , Boolean > requiresStorageMotion = new HashMap <Host , Boolean >();
12791279 DataCenterDeployment plan = null ;
12801280 if (canMigrateWithStorage ) {
12811281 allHostsPair = searchForServers (startIndex , pageSize , null , hostType , null , srcHost .getDataCenterId (), null , null , null , keyword , null , null , srcHost .getHypervisorType (),
12821282 srcHost .getHypervisorVersion ());
12831283 allHosts = allHostsPair .first ();
1284- allHosts .remove (srcHost );
1284+ allHostsCount = allHostsPair .second ();
1285+ if (allHosts .remove (srcHost )) {
1286+ allHostsCount --;
1287+ }
12851288
12861289 for (final VolumeVO volume : volumes ) {
12871290 StoragePool storagePool = _poolDao .findById (volume .getPoolId ());
12881291 Long volClusterId = storagePool .getClusterId ();
12891292
1290- for (Iterator <HostVO > iterator = allHosts .iterator (); iterator .hasNext ();) {
1291- final Host host = iterator .next ();
1292-
1293+ for (HostVO host : allHosts ) {
12931294 if (volClusterId != null ) {
12941295 if (storagePool .isLocal () || !host .getClusterId ().equals (volClusterId ) || usesLocal ) {
1295- if (storagePool .isManaged ()) {
1296- // At the time being, we do not support storage migration of a volume from managed storage unless the managed storage
1297- // is at the zone level and the source and target storage pool is the same.
1298- // If the source and target storage pool is the same and it is managed, then we still have to perform a storage migration
1299- // because we need to create a new target volume and copy the contents of the source volume into it before deleting the
1300- // source volume.
1301- iterator .remove ();
1302- } else {
1303- if (hasSuitablePoolsForVolume (volume , host , vmProfile )) {
1304- requiresStorageMotion .put (host , true );
1305- } else {
1306- iterator .remove ();
1307- }
1296+ // At the time being, we do not support storage migration of a volume from managed storage unless the managed storage
1297+ // is at the zone level and the source and target storage pool is the same.
1298+ // If the source and target storage pool is the same and it is managed, then we still have to perform a storage migration
1299+ // because we need to create a new target volume and copy the contents of the source volume into it before deleting the
1300+ // source volume.
1301+ if (!storagePool .isManaged () && hasSuitablePoolsForVolume (volume , host , vmProfile )) {
1302+ requiresStorageMotion .put (host , true );
13081303 }
13091304 }
13101305 } else {
@@ -1329,11 +1324,14 @@ public Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Ho
13291324 allHostsPair = searchForServers (startIndex , pageSize , null , hostType , null , null , null , cluster , null , keyword , null , null , null , null );
13301325 // Filter out the current host.
13311326 allHosts = allHostsPair .first ();
1332- allHosts .remove (srcHost );
1327+ allHostsCount = allHostsPair .second ();
1328+ if (allHosts .remove (srcHost )) {
1329+ allHostsCount --;
1330+ }
13331331 plan = new DataCenterDeployment (srcHost .getDataCenterId (), srcHost .getPodId (), srcHost .getClusterId (), null , null , null );
13341332 }
13351333
1336- final Pair <List <? extends Host >, Integer > otherHosts = new Pair <List <? extends Host >, Integer >(allHosts , allHostsPair . second () );
1334+ final Pair <List <? extends Host >, Integer > otherHosts = new Pair <List <? extends Host >, Integer >(allHosts , allHostsCount );
13371335 List <Host > suitableHosts = new ArrayList <Host >();
13381336 final ExcludeList excludes = new ExcludeList ();
13391337 excludes .addHost (srcHostId );
0 commit comments