Skip to content

Commit c91e84c

Browse files
authored
Avoid double counting primary storage allocated capacity for storage pools having a parent (#12181)
1 parent 9a38e75 commit c91e84c

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
213213

214214
private static final String LEFT_JOIN_VM_TEMPLATE = "LEFT JOIN vm_template ON vm_template.id = vi.vm_template_id ";
215215

216+
private static final String STORAGE_POOLS_WITH_CHILDREN = "SELECT DISTINCT parent FROM storage_pool WHERE parent != 0 AND removed IS NULL";
217+
216218
public CapacityDaoImpl() {
217219
_hostIdTypeSearch = createSearchBuilder();
218220
_hostIdTypeSearch.and("hostId", _hostIdTypeSearch.entity().getHostOrPoolId(), SearchCriteria.Op.EQ);
@@ -379,6 +381,11 @@ public List<SummedCapacity> listCapacitiesGroupedByLevelAndType(Integer capacity
379381
finalQuery.append(" AND capacity_type = ?");
380382
resourceIdList.add(capacityType.longValue());
381383
}
384+
385+
// Exclude storage pools with children from capacity calculations to avoid double counting
386+
finalQuery.append(" AND NOT (capacity.capacity_type = ").append(Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED)
387+
.append(" AND capacity.host_id IN (").append(STORAGE_POOLS_WITH_CHILDREN).append("))");
388+
382389
if (CollectionUtils.isNotEmpty(hostIds)) {
383390
finalQuery.append(String.format(" AND capacity.host_id IN (%s)", StringUtils.join(hostIds, ",")));
384391
if (capacityType == null) {
@@ -541,6 +548,10 @@ public List<SummedCapacity> findFilteredCapacityBy(Integer capacityType, Long zo
541548
StringBuilder sql = new StringBuilder(LIST_CAPACITY_GROUP_BY_CAPACITY_PART1);
542549
List<Long> resourceIdList = new ArrayList<Long>();
543550

551+
// Exclude storage pools with children from capacity calculations to avoid double counting
552+
sql.append(" AND NOT (capacity.capacity_type = ").append(Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED)
553+
.append(" AND capacity.host_id IN (").append(STORAGE_POOLS_WITH_CHILDREN).append("))");
554+
544555
if (zoneId != null) {
545556
sql.append(" AND capacity.data_center_id = ?");
546557
resourceIdList.add(zoneId);

0 commit comments

Comments
 (0)