Skip to content

Commit 2bfeed0

Browse files
Sina Kashipazhaustcweizhou
authored andcommitted
#4534 Fix Vms are migrated to same clusters in CloudStack caused by dedicated resources.
1 parent 9093af6 commit 2bfeed0

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
public interface CapacityDao extends GenericDao<CapacityVO, Long> {
2828
CapacityVO findByHostIdType(Long hostId, short capacityType);
2929

30-
List<Long> listClustersInZoneOrPodByHostCapacities(long id, int requiredCpu, long requiredRam, short capacityTypeForOrdering, boolean isZone);
30+
List<Long> listClustersInZoneOrPodByHostCapacities(long id, long vmId, int requiredCpu, long requiredRam, short capacityTypeForOrdering, boolean isZone);
3131

3232
List<Long> listHostsWithEnoughCapacity(int requiredCpu, long requiredRam, Long clusterId, String hostType);
3333

@@ -37,7 +37,7 @@ public interface CapacityDao extends GenericDao<CapacityVO, Long> {
3737

3838
List<SummedCapacity> findNonSharedStorageForClusterPodZone(Long zoneId, Long podId, Long clusterId);
3939

40-
Pair<List<Long>, Map<Long, Double>> orderClustersByAggregateCapacity(long id, short capacityType, boolean isZone);
40+
Pair<List<Long>, Map<Long, Double>> orderClustersByAggregateCapacity(long id, long vmId, short capacityType, boolean isZone);
4141

4242
List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Long podId, Long clusterId);
4343

engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,24 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
7575
+ " AND host_capacity.host_id IN (SELECT capacity.host_id FROM `cloud`.`op_host_capacity` capacity JOIN `cloud`.`cluster_details` cluster_details ON (capacity.cluster_id= cluster_details.cluster_id) where capacity_type='0' AND cluster_details.name='memoryOvercommitRatio' AND ((total_capacity* cluster_details.value) - used_capacity ) >= ?)) ";
7676

7777
private static final String ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_PART1 =
78-
"SELECT capacity.cluster_id, SUM(used_capacity+reserved_capacity)/SUM(total_capacity ) FROM `cloud`.`op_host_capacity` capacity WHERE ";
78+
"SELECT capacity.cluster_id, SUM(used_capacity+reserved_capacity)/SUM(total_capacity ) FROM `cloud`.`op_host_capacity` capacity ";
7979

8080
private static final String ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_PART2 =
81-
" AND capacity_type = ? AND cluster_details.name =? GROUP BY capacity.cluster_id ORDER BY SUM(used_capacity+reserved_capacity)/SUM(total_capacity * cluster_details.value) ASC";
81+
" AND capacity_type = ? GROUP BY capacity.cluster_id ORDER BY SUM(used_capacity+reserved_capacity)/SUM(total_capacity) ASC";
82+
83+
private static final String ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_JOIN_1 =
84+
"JOIN host ON capacity.host_id = host.id " +
85+
"LEFT JOIN (SELECT affinity_group.id, agvm.instance_id FROM affinity_group_vm_map agvm JOIN affinity_group ON agvm.affinity_group_id = affinity_group.id AND affinity_group.type='ExplicitDedication') AS ag ON ag.instance_id = ? " +
86+
"LEFT JOIN dedicated_resources dr_pod ON dr_pod.pod_id IS NOT NULL AND dr_pod.pod_id = host.pod_id " +
87+
"LEFT JOIN dedicated_resources dr_cluster ON dr_cluster.cluster_id IS NOT NULL AND dr_cluster.cluster_id = host.cluster_id " +
88+
"LEFT JOIN dedicated_resources dr_host ON dr_host.host_id IS NOT NULL AND dr_host.host_id = host.id ";
89+
90+
private static final String ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_JOIN_2 =
91+
" AND ((ag.id IS NULL AND dr_pod.pod_id IS NULL AND dr_cluster.cluster_id IS NULL AND dr_host.host_id IS NULL) OR " +
92+
"(dr_pod.affinity_group_id = ag.id OR dr_cluster.affinity_group_id = ag.id OR dr_host.affinity_group_id = ag.id))";
8293

8394
private static final String ORDER_CLUSTERS_BY_AGGREGATE_OVERCOMMIT_CAPACITY_PART1 =
84-
"SELECT capacity.cluster_id, SUM(used_capacity+reserved_capacity)/SUM(total_capacity * cluster_details.value) FROM `cloud`.`op_host_capacity` capacity INNER JOIN `cloud`.`cluster_details` cluster_details ON (capacity.cluster_id = cluster_details.cluster_id) WHERE ";
95+
"SELECT capacity.cluster_id, SUM(used_capacity+reserved_capacity)/SUM(total_capacity * cluster_details.value) FROM `cloud`.`op_host_capacity` capacity INNER JOIN `cloud`.`cluster_details` cluster_details ON (capacity.cluster_id = cluster_details.cluster_id) ";
8596

8697
private static final String ORDER_CLUSTERS_BY_AGGREGATE_OVERCOMMIT_CAPACITY_PART2 =
8798
" AND capacity_type = ? AND cluster_details.name =? GROUP BY capacity.cluster_id ORDER BY SUM(used_capacity+reserved_capacity)/SUM(total_capacity * cluster_details.value) ASC";
@@ -572,7 +583,7 @@ public CapacityVO findByHostIdType(Long hostId, short capacityType) {
572583
}
573584

574585
@Override
575-
public List<Long> listClustersInZoneOrPodByHostCapacities(long id, int requiredCpu, long requiredRam, short capacityTypeForOrdering, boolean isZone) {
586+
public List<Long> listClustersInZoneOrPodByHostCapacities(long id, long vmId, int requiredCpu, long requiredRam, short capacityTypeForOrdering, boolean isZone) {
576587
TransactionLegacy txn = TransactionLegacy.currentTxn();
577588
PreparedStatement pstmt = null;
578589
List<Long> result = new ArrayList<Long>();
@@ -854,7 +865,7 @@ public boolean removeBy(Short capacityType, Long zoneId, Long podId, Long cluste
854865
}
855866

856867
@Override
857-
public Pair<List<Long>, Map<Long, Double>> orderClustersByAggregateCapacity(long id, short capacityTypeForOrdering, boolean isZone) {
868+
public Pair<List<Long>, Map<Long, Double>> orderClustersByAggregateCapacity(long id, long vmId, short capacityTypeForOrdering, boolean isZone) {
858869
TransactionLegacy txn = TransactionLegacy.currentTxn();
859870
PreparedStatement pstmt = null;
860871
List<Long> result = new ArrayList<Long>();
@@ -866,11 +877,14 @@ public Pair<List<Long>, Map<Long, Double>> orderClustersByAggregateCapacity(long
866877
sql.append(ORDER_CLUSTERS_BY_AGGREGATE_OVERCOMMIT_CAPACITY_PART1);
867878
}
868879

880+
sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_JOIN_1);
869881
if (isZone) {
870-
sql.append(" data_center_id = ?");
882+
sql.append("WHERE capacity.capacity_state = 'Enabled' AND capacity.data_center_id = ?");
871883
} else {
872-
sql.append(" pod_id = ?");
884+
sql.append("WHERE capacity.capacity_state = 'Enabled' AND capacity.pod_id = ?");
873885
}
886+
sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_JOIN_2);
887+
874888
if (capacityTypeForOrdering != Capacity.CAPACITY_TYPE_CPU && capacityTypeForOrdering != Capacity.CAPACITY_TYPE_MEMORY) {
875889
sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_PART2);
876890
} else {
@@ -879,13 +893,14 @@ public Pair<List<Long>, Map<Long, Double>> orderClustersByAggregateCapacity(long
879893

880894
try {
881895
pstmt = txn.prepareAutoCloseStatement(sql.toString());
882-
pstmt.setLong(1, id);
883-
pstmt.setShort(2, capacityTypeForOrdering);
896+
pstmt.setLong(1, vmId);
897+
pstmt.setLong(2, id);
898+
pstmt.setShort(3, capacityTypeForOrdering);
884899

885900
if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_CPU) {
886-
pstmt.setString(3, "cpuOvercommitRatio");
901+
pstmt.setString(4, "cpuOvercommitRatio");
887902
} else if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_MEMORY) {
888-
pstmt.setString(3, "memoryOvercommitRatio");
903+
pstmt.setString(4, "memoryOvercommitRatio");
889904
}
890905

891906
ResultSet rs = pstmt.executeQuery();

plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,15 @@ private void initializeForTest(VirtualMachineProfileImpl vmProfile, DataCenterDe
382382
clustersWithEnoughCapacity.add(2L);
383383
clustersWithEnoughCapacity.add(3L);
384384
when(
385-
capacityDao.listClustersInZoneOrPodByHostCapacities(dataCenterId, noOfCpusInOffering * cpuSpeedInOffering, ramInOffering * 1024L * 1024L,
385+
capacityDao.listClustersInZoneOrPodByHostCapacities(dataCenterId, 12L, noOfCpusInOffering * cpuSpeedInOffering, ramInOffering * 1024L * 1024L,
386386
Capacity.CAPACITY_TYPE_CPU, true)).thenReturn(clustersWithEnoughCapacity);
387387

388388
Map<Long, Double> clusterCapacityMap = new HashMap<Long, Double>();
389389
clusterCapacityMap.put(1L, 2048D);
390390
clusterCapacityMap.put(2L, 2048D);
391391
clusterCapacityMap.put(3L, 2048D);
392392
Pair<List<Long>, Map<Long, Double>> clustersOrderedByCapacity = new Pair<List<Long>, Map<Long, Double>>(clustersWithEnoughCapacity, clusterCapacityMap);
393-
when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, Capacity.CAPACITY_TYPE_CPU, true)).thenReturn(clustersOrderedByCapacity);
393+
when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, Capacity.CAPACITY_TYPE_CPU, true)).thenReturn(clustersOrderedByCapacity);
394394

395395
List<Long> disabledClusters = new ArrayList<Long>();
396396
List<Long> clustersWithDisabledPods = new ArrayList<Long>();

server/src/main/java/com/cloud/deploy/FirstFitPlanner.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ private List<Long> scanClustersForDestinationInZoneOrPod(long id, boolean isZone
392392
long requiredRam = offering.getRamSize() * 1024L * 1024L;
393393

394394
//list clusters under this zone by cpu and ram capacity
395-
Pair<List<Long>, Map<Long, Double>> clusterCapacityInfo = listClustersByCapacity(id, requiredCpu, requiredRam, avoid, isZone);
395+
Pair<List<Long>, Map<Long, Double>> clusterCapacityInfo = listClustersByCapacity(id, vmProfile.getId(), requiredCpu, requiredRam, avoid, isZone);
396396
List<Long> prioritizedClusterIds = clusterCapacityInfo.first();
397397
if (!prioritizedClusterIds.isEmpty()) {
398398
if (avoid.getClustersToAvoid() != null) {
@@ -480,7 +480,7 @@ private List<Long> listDisabledPods(long zoneId) {
480480
return disabledPods;
481481
}
482482

483-
protected Pair<List<Long>, Map<Long, Double>> listClustersByCapacity(long id, int requiredCpu, long requiredRam, ExcludeList avoid, boolean isZone) {
483+
protected Pair<List<Long>, Map<Long, Double>> listClustersByCapacity(long id, long vmId, int requiredCpu, long requiredRam, ExcludeList avoid, boolean isZone) {
484484
//look at the aggregate available cpu and ram per cluster
485485
//although an aggregate value may be false indicator that a cluster can host a vm, it will at the least eliminate those clusters which definitely cannot
486486

@@ -495,11 +495,11 @@ protected Pair<List<Long>, Map<Long, Double>> listClustersByCapacity(long id, in
495495
capacityType = Capacity.CAPACITY_TYPE_MEMORY;
496496
}
497497

498-
List<Long> clusterIdswithEnoughCapacity = capacityDao.listClustersInZoneOrPodByHostCapacities(id, requiredCpu, requiredRam, capacityType, isZone);
498+
List<Long> clusterIdswithEnoughCapacity = capacityDao.listClustersInZoneOrPodByHostCapacities(id, vmId, requiredCpu, requiredRam, capacityType, isZone);
499499
if (s_logger.isTraceEnabled()) {
500500
s_logger.trace("ClusterId List having enough CPU and RAM capacity: " + clusterIdswithEnoughCapacity);
501501
}
502-
Pair<List<Long>, Map<Long, Double>> result = capacityDao.orderClustersByAggregateCapacity(id, capacityType, isZone);
502+
Pair<List<Long>, Map<Long, Double>> result = capacityDao.orderClustersByAggregateCapacity(id, vmId, capacityType, isZone);
503503
List<Long> clusterIdsOrderedByAggregateCapacity = result.first();
504504
//only keep the clusters that have enough capacity to host this VM
505505
if (s_logger.isTraceEnabled()) {

server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ private void initializeForTest(VirtualMachineProfileImpl vmProfile, DataCenterDe
291291
clustersWithEnoughCapacity.add(6L);
292292

293293
when(
294-
capacityDao.listClustersInZoneOrPodByHostCapacities(dataCenterId, noOfCpusInOffering * cpuSpeedInOffering, ramInOffering * 1024L * 1024L,
294+
capacityDao.listClustersInZoneOrPodByHostCapacities(dataCenterId, 12L, noOfCpusInOffering * cpuSpeedInOffering, ramInOffering * 1024L * 1024L,
295295
Capacity.CAPACITY_TYPE_CPU, true)).thenReturn(clustersWithEnoughCapacity);
296296

297297
Map<Long, Double> clusterCapacityMap = new HashMap<Long, Double>();
@@ -303,7 +303,7 @@ private void initializeForTest(VirtualMachineProfileImpl vmProfile, DataCenterDe
303303
clusterCapacityMap.put(6L, 2048D);
304304

305305
Pair<List<Long>, Map<Long, Double>> clustersOrderedByCapacity = new Pair<List<Long>, Map<Long, Double>>(clustersWithEnoughCapacity, clusterCapacityMap);
306-
when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, Capacity.CAPACITY_TYPE_CPU, true)).thenReturn(clustersOrderedByCapacity);
306+
when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, Capacity.CAPACITY_TYPE_CPU, true)).thenReturn(clustersOrderedByCapacity);
307307

308308
List<Long> disabledClusters = new ArrayList<Long>();
309309
List<Long> clustersWithDisabledPods = new ArrayList<Long>();

0 commit comments

Comments
 (0)