@@ -1164,8 +1164,8 @@ public Pair<List<? extends Host>, Integer> searchForServers(final ListHostsCmd c
11641164 final Object resourceState = cmd .getResourceState ();
11651165 final Object haHosts = cmd .getHaHost ();
11661166
1167- final Pair <List <HostVO >, Integer > result = searchForServers (cmd .getStartIndex (), cmd .getPageSizeVal (), name , type , state , zoneId , pod , cluster , id , keyword , resourceState , haHosts , null ,
1168- null );
1167+ final Pair <List <HostVO >, Integer > result = searchForServers (cmd .getStartIndex (), cmd .getPageSizeVal (), name , type , state , zoneId , pod ,
1168+ cluster , id , keyword , resourceState , haHosts , null , null );
11691169 return new Pair <List <? extends Host >, Integer >(result .first (), result .second ());
11701170 }
11711171
@@ -1267,19 +1267,20 @@ public Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Ho
12671267 final Type hostType = srcHost .getType ();
12681268 Pair <List <HostVO >, Integer > allHostsPair = null ;
12691269 List <HostVO > allHosts = null ;
1270+ List <HostVO > hostsForMigrationWithStorage = null ;
12701271 final Map <Host , Boolean > requiresStorageMotion = new HashMap <Host , Boolean >();
12711272 DataCenterDeployment plan = null ;
12721273 if (canMigrateWithStorage ) {
1273- allHostsPair = searchForServers (startIndex , pageSize , null , hostType , null , srcHost .getDataCenterId (), null , null , null , keyword , null , null , srcHost . getHypervisorType (),
1274- srcHost .getHypervisorVersion ());
1274+ allHostsPair = searchForServers (startIndex , pageSize , null , hostType , null , srcHost .getDataCenterId (), null , null , null , keyword ,
1275+ null , null , srcHost . getHypervisorType (), srcHost .getHypervisorVersion (), srcHost . getId ());
12751276 allHosts = allHostsPair .first ();
1276- allHosts . remove ( srcHost );
1277+ hostsForMigrationWithStorage = new ArrayList <>( allHosts );
12771278
12781279 for (final VolumeVO volume : volumes ) {
12791280 StoragePool storagePool = _poolDao .findById (volume .getPoolId ());
12801281 Long volClusterId = storagePool .getClusterId ();
12811282
1282- for (Iterator <HostVO > iterator = allHosts .iterator (); iterator .hasNext ();) {
1283+ for (Iterator <HostVO > iterator = hostsForMigrationWithStorage .iterator (); iterator .hasNext ();) {
12831284 final Host host = iterator .next ();
12841285
12851286 if (volClusterId != null ) {
@@ -1318,10 +1319,9 @@ public Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Ho
13181319 if (s_logger .isDebugEnabled ()) {
13191320 s_logger .debug ("Searching for all hosts in cluster " + cluster + " for migrating VM " + vm );
13201321 }
1321- allHostsPair = searchForServers (startIndex , pageSize , null , hostType , null , null , null , cluster , null , keyword , null , null , null , null );
1322- // Filter out the current host.
1322+ allHostsPair = searchForServers (startIndex , pageSize , null , hostType , null , null , null , cluster , null , keyword , null , null , null ,
1323+ null , srcHost . getId ());
13231324 allHosts = allHostsPair .first ();
1324- allHosts .remove (srcHost );
13251325 plan = new DataCenterDeployment (srcHost .getDataCenterId (), srcHost .getPodId (), srcHost .getClusterId (), null , null , null );
13261326 }
13271327
@@ -1350,7 +1350,7 @@ public Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Ho
13501350
13511351 for (final HostAllocator allocator : hostAllocators ) {
13521352 if (canMigrateWithStorage ) {
1353- suitableHosts = allocator .allocateTo (vmProfile , plan , Host .Type .Routing , excludes , allHosts , HostAllocator .RETURN_UPTO_ALL , false );
1353+ suitableHosts = allocator .allocateTo (vmProfile , plan , Host .Type .Routing , excludes , hostsForMigrationWithStorage , HostAllocator .RETURN_UPTO_ALL , false );
13541354 } else {
13551355 suitableHosts = allocator .allocateTo (vmProfile , plan , Host .Type .Routing , excludes , HostAllocator .RETURN_UPTO_ALL , false );
13561356 }
@@ -1542,12 +1542,14 @@ private List<StoragePool> findAllSuitableStoragePoolsForVm(final VolumeVO volume
15421542 return suitablePools ;
15431543 }
15441544
1545- private Pair <List <HostVO >, Integer > searchForServers (final Long startIndex , final Long pageSize , final Object name , final Object type , final Object state , final Object zone , final Object pod ,
1546- final Object cluster , final Object id , final Object keyword , final Object resourceState , final Object haHosts , final Object hypervisorType , final Object hypervisorVersion ) {
1545+ private Pair <List <HostVO >, Integer > searchForServers (final Long startIndex , final Long pageSize , final Object name , final Object type ,
1546+ final Object state , final Object zone , final Object pod , final Object cluster , final Object id , final Object keyword ,
1547+ final Object resourceState , final Object haHosts , final Object hypervisorType , final Object hypervisorVersion , final Object ... excludes ) {
15471548 final Filter searchFilter = new Filter (HostVO .class , "id" , Boolean .TRUE , startIndex , pageSize );
15481549
15491550 final SearchBuilder <HostVO > sb = _hostDao .createSearchBuilder ();
15501551 sb .and ("id" , sb .entity ().getId (), SearchCriteria .Op .EQ );
1552+ sb .and ("idsNotIn" , sb .entity ().getId (), SearchCriteria .Op .NOTIN );
15511553 sb .and ("name" , sb .entity ().getName (), SearchCriteria .Op .LIKE );
15521554 sb .and ("type" , sb .entity ().getType (), SearchCriteria .Op .LIKE );
15531555 sb .and ("status" , sb .entity ().getStatus (), SearchCriteria .Op .EQ );
@@ -1588,6 +1590,10 @@ private Pair<List<HostVO>, Integer> searchForServers(final Long startIndex, fina
15881590 sc .setParameters ("id" , id );
15891591 }
15901592
1593+ if (excludes != null && excludes .length > 0 ) {
1594+ sc .setParameters ("idsNotIn" , excludes );
1595+ }
1596+
15911597 if (name != null ) {
15921598 sc .setParameters ("name" , "%" + name + "%" );
15931599 }
0 commit comments