Skip to content

Commit f48cc9c

Browse files
committed
tweak logic to consider tags and rule tags at zone and cluster scope
1 parent dc5ba75 commit f48cc9c

1 file changed

Lines changed: 62 additions & 27 deletions

File tree

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

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@
767767
import com.cloud.storage.Storage;
768768
import com.cloud.storage.StorageManager;
769769
import com.cloud.storage.StoragePool;
770+
import com.cloud.storage.StoragePoolStatus;
770771
import com.cloud.storage.VMTemplateVO;
771772
import com.cloud.storage.Volume;
772773
import com.cloud.storage.VolumeApiServiceImpl;
@@ -1998,42 +1999,76 @@ private List<StoragePool> findAllSuitableStoragePoolsForDetachedVolume(Volume vo
19981999
return suitablePools;
19992000
}
20002001

2001-
HypervisorType hypervisorType = getHypervisorType(null, srcPool);
2002-
List<ClusterVO> clusters = _clusterDao.listByDcHyType(srcPool.getDataCenterId(), hypervisorType.toString());
2003-
20042002
DiskOfferingVO diskOffering = _diskOfferingDao.findById(diskOfferingId);
2005-
DiskProfile diskProfile = new DiskProfile(volume, diskOffering, hypervisorType);
2003+
String[] tagsArray = diskOffering.getTagsArray();
2004+
List<String> tags = (tagsArray != null && tagsArray.length > 0) ? Arrays.asList(tagsArray) : new ArrayList<>();
20062005

2007-
ExcludeList avoid = new ExcludeList();
2006+
HypervisorType hypervisorType = getHypervisorType(null, srcPool);
2007+
Long dcId = srcPool.getDataCenterId();
2008+
2009+
logger.debug("Finding suitable pools for detached volume {} with offering tags: {}, hypervisor: {}",
2010+
volume.getUuid(), tags, hypervisorType);
2011+
2012+
// Create a map for quick lookup and deduplication
2013+
Map<Long, StoragePool> allPoolsMap = allPools.stream()
2014+
.collect(Collectors.toMap(StoragePool::getId, pool -> pool));
2015+
Set<Long> matchingPoolIds = new HashSet<>();
2016+
2017+
List<StoragePoolVO> zonePoolsLiteral = _poolDao.findZoneWideStoragePoolsByTags(dcId,
2018+
tags.isEmpty() ? null : tags.toArray(new String[0]), true);
2019+
for (StoragePoolVO pool : zonePoolsLiteral) {
2020+
if (pool.getHypervisor() == null || pool.getHypervisor().equals(HypervisorType.Any) ||
2021+
pool.getHypervisor().equals(hypervisorType)) {
2022+
matchingPoolIds.add(pool.getId());
2023+
logger.debug("Found zone-wide pool with literal tags: {} ({})", pool.getName(), pool.getId());
2024+
}
2025+
}
20082026

2009-
Set<Long> allPoolIds = allPools.stream()
2010-
.map(StoragePool::getId)
2011-
.collect(Collectors.toSet());
2012-
Set<Long> addedPoolIds = new HashSet<>();
2027+
List<StoragePoolVO> zonePoolsRules = _poolJoinDao.findStoragePoolByScopeAndRuleTags(dcId, null, null,
2028+
ScopeType.ZONE, tags);
2029+
for (StoragePoolVO pool : zonePoolsRules) {
2030+
StoragePoolVO poolVO = _poolDao.findById(pool.getId());
2031+
if (poolVO != null && (poolVO.getHypervisor() == null || poolVO.getHypervisor().equals(HypervisorType.Any) ||
2032+
poolVO.getHypervisor().equals(hypervisorType))) {
2033+
matchingPoolIds.add(pool.getId());
2034+
logger.debug("Found zone-wide pool with rule-based tags: {} ({})", pool.getName(), pool.getId());
2035+
}
2036+
}
20132037

2014-
for (Cluster cluster : clusters) {
2015-
DataCenterDeployment plan = new DataCenterDeployment(srcPool.getDataCenterId(), cluster.getPodId(), cluster.getId(),
2016-
null, null, null, null);
2038+
List<ClusterVO> clusters = _clusterDao.listByDcHyType(dcId, hypervisorType.toString());
2039+
for (ClusterVO cluster : clusters) {
2040+
List<StoragePoolVO> clusterPoolsLiteral = _poolDao.findPoolsByTags(dcId, cluster.getPodId(),
2041+
cluster.getId(), tags.isEmpty() ? null : tags.toArray(new String[0]), true,
2042+
VolumeApiServiceImpl.storageTagRuleExecutionTimeout.value());
2043+
for (StoragePoolVO pool : clusterPoolsLiteral) {
2044+
matchingPoolIds.add(pool.getId());
2045+
logger.debug("Found cluster-scoped pool with literal tags: {} ({}) in cluster {}",
2046+
pool.getName(), pool.getId(), cluster.getName());
2047+
}
2048+
List<StoragePoolVO> clusterPoolsRules = _poolJoinDao.findStoragePoolByScopeAndRuleTags(dcId,
2049+
cluster.getPodId(), cluster.getId(), ScopeType.CLUSTER, tags);
2050+
for (StoragePoolVO pool : clusterPoolsRules) {
2051+
matchingPoolIds.add(pool.getId());
2052+
logger.debug("Found cluster-scoped pool with rule-based tags: {} ({}) in cluster {}",
2053+
pool.getName(), pool.getId(), cluster.getName());
2054+
}
2055+
}
20172056

2018-
for (StoragePoolAllocator allocator : _storagePoolAllocators) {
2019-
try {
2020-
List<StoragePool> pools = allocator.allocateToPool(diskProfile, null, plan, avoid, StoragePoolAllocator.RETURN_UPTO_ALL,
2021-
false, null);
2022-
if (CollectionUtils.isNotEmpty(pools)) {
2023-
for (StoragePool pool : pools) {
2024-
if (allPoolIds.contains(pool.getId()) && !addedPoolIds.contains(pool.getId())) {
2025-
suitablePools.add(pool);
2026-
addedPoolIds.add(pool.getId());
2027-
}
2028-
}
2029-
}
2030-
} catch (Exception e) {
2031-
logger.warn("Allocator {} failed to find storage pools for detached volume {} due to {}",
2032-
allocator.getClass().getSimpleName(), volume.getId(), e.getMessage());
2057+
for (Long poolId : matchingPoolIds) {
2058+
if (allPoolsMap.containsKey(poolId)) {
2059+
StoragePool pool = allPoolsMap.get(poolId);
2060+
if (StoragePoolStatus.Up.equals(pool.getStatus())) {
2061+
suitablePools.add(pool);
2062+
logger.debug("Added pool {} to suitable pools", pool.getName());
2063+
} else {
2064+
logger.debug("Skipping pool {} - status is {}, not Up", pool.getName(), pool.getStatus());
20332065
}
20342066
}
20352067
}
20362068

2069+
logger.debug("Found {} suitable pools out of {} total pools for detached volume {}",
2070+
suitablePools.size(), allPools.size(), volume.getUuid());
2071+
20372072
return suitablePools;
20382073
}
20392074

0 commit comments

Comments
 (0)