Skip to content

Commit 81c524e

Browse files
authored
Fixing count for findHostsForMigration (#4375)
1 parent 5316810 commit 81c524e

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

server/src/main/java/com/cloud/server/ManagementServerImpl.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,8 +1172,8 @@ public Pair<List<? extends Host>, Integer> searchForServers(final ListHostsCmd c
11721172
final Object resourceState = cmd.getResourceState();
11731173
final Object haHosts = cmd.getHaHost();
11741174

1175-
final Pair<List<HostVO>, Integer> result = searchForServers(cmd.getStartIndex(), cmd.getPageSizeVal(), name, type, state, zoneId, pod, cluster, id, keyword, resourceState, haHosts, null,
1176-
null);
1175+
final Pair<List<HostVO>, Integer> result = searchForServers(cmd.getStartIndex(), cmd.getPageSizeVal(), name, type, state, zoneId, pod,
1176+
cluster, id, keyword, resourceState, haHosts, null, null);
11771177
return new Pair<List<? extends Host>, Integer>(result.first(), result.second());
11781178
}
11791179

@@ -1275,19 +1275,20 @@ public Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Ho
12751275
final Type hostType = srcHost.getType();
12761276
Pair<List<HostVO>, Integer> allHostsPair = null;
12771277
List<HostVO> allHosts = null;
1278+
List<HostVO> hostsForMigrationWithStorage = null;
12781279
final Map<Host, Boolean> requiresStorageMotion = new HashMap<Host, Boolean>();
12791280
DataCenterDeployment plan = null;
12801281
if (canMigrateWithStorage) {
1281-
allHostsPair = searchForServers(startIndex, pageSize, null, hostType, null, srcHost.getDataCenterId(), null, null, null, keyword, null, null, srcHost.getHypervisorType(),
1282-
srcHost.getHypervisorVersion());
1282+
allHostsPair = searchForServers(startIndex, pageSize, null, hostType, null, srcHost.getDataCenterId(), null, null, null, keyword,
1283+
null, null, srcHost.getHypervisorType(), srcHost.getHypervisorVersion(), srcHost.getId());
12831284
allHosts = allHostsPair.first();
1284-
allHosts.remove(srcHost);
1285+
hostsForMigrationWithStorage = new ArrayList<>(allHosts);
12851286

12861287
for (final VolumeVO volume : volumes) {
12871288
StoragePool storagePool = _poolDao.findById(volume.getPoolId());
12881289
Long volClusterId = storagePool.getClusterId();
12891290

1290-
for (Iterator<HostVO> iterator = allHosts.iterator(); iterator.hasNext();) {
1291+
for (Iterator<HostVO> iterator = hostsForMigrationWithStorage.iterator(); iterator.hasNext();) {
12911292
final Host host = iterator.next();
12921293

12931294
if (volClusterId != null) {
@@ -1326,10 +1327,9 @@ public Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Ho
13261327
if (s_logger.isDebugEnabled()) {
13271328
s_logger.debug("Searching for all hosts in cluster " + cluster + " for migrating VM " + vm);
13281329
}
1329-
allHostsPair = searchForServers(startIndex, pageSize, null, hostType, null, null, null, cluster, null, keyword, null, null, null, null);
1330-
// Filter out the current host.
1330+
allHostsPair = searchForServers(startIndex, pageSize, null, hostType, null, null, null, cluster, null, keyword, null, null, null,
1331+
null, srcHost.getId());
13311332
allHosts = allHostsPair.first();
1332-
allHosts.remove(srcHost);
13331333
plan = new DataCenterDeployment(srcHost.getDataCenterId(), srcHost.getPodId(), srcHost.getClusterId(), null, null, null);
13341334
}
13351335

@@ -1358,7 +1358,7 @@ public Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Ho
13581358

13591359
for (final HostAllocator allocator : hostAllocators) {
13601360
if (canMigrateWithStorage) {
1361-
suitableHosts = allocator.allocateTo(vmProfile, plan, Host.Type.Routing, excludes, allHosts, HostAllocator.RETURN_UPTO_ALL, false);
1361+
suitableHosts = allocator.allocateTo(vmProfile, plan, Host.Type.Routing, excludes, hostsForMigrationWithStorage, HostAllocator.RETURN_UPTO_ALL, false);
13621362
} else {
13631363
suitableHosts = allocator.allocateTo(vmProfile, plan, Host.Type.Routing, excludes, HostAllocator.RETURN_UPTO_ALL, false);
13641364
}
@@ -1550,12 +1550,14 @@ private List<StoragePool> findAllSuitableStoragePoolsForVm(final VolumeVO volume
15501550
return suitablePools;
15511551
}
15521552

1553-
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,
1554-
final Object cluster, final Object id, final Object keyword, final Object resourceState, final Object haHosts, final Object hypervisorType, final Object hypervisorVersion) {
1553+
private Pair<List<HostVO>, Integer> searchForServers(final Long startIndex, final Long pageSize, final Object name, final Object type,
1554+
final Object state, final Object zone, final Object pod, final Object cluster, final Object id, final Object keyword,
1555+
final Object resourceState, final Object haHosts, final Object hypervisorType, final Object hypervisorVersion, final Object... excludes) {
15551556
final Filter searchFilter = new Filter(HostVO.class, "id", Boolean.TRUE, startIndex, pageSize);
15561557

15571558
final SearchBuilder<HostVO> sb = _hostDao.createSearchBuilder();
15581559
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
1560+
sb.and("idsNotIn", sb.entity().getId(), SearchCriteria.Op.NOTIN);
15591561
sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
15601562
sb.and("type", sb.entity().getType(), SearchCriteria.Op.LIKE);
15611563
sb.and("status", sb.entity().getStatus(), SearchCriteria.Op.EQ);
@@ -1596,6 +1598,10 @@ private Pair<List<HostVO>, Integer> searchForServers(final Long startIndex, fina
15961598
sc.setParameters("id", id);
15971599
}
15981600

1601+
if (excludes != null && excludes.length > 0) {
1602+
sc.setParameters("idsNotIn", excludes);
1603+
}
1604+
15991605
if (name != null) {
16001606
sc.setParameters("name", "%" + name + "%");
16011607
}

0 commit comments

Comments
 (0)