|
767 | 767 | import com.cloud.storage.Storage; |
768 | 768 | import com.cloud.storage.StorageManager; |
769 | 769 | import com.cloud.storage.StoragePool; |
| 770 | +import com.cloud.storage.StoragePoolStatus; |
770 | 771 | import com.cloud.storage.VMTemplateVO; |
771 | 772 | import com.cloud.storage.Volume; |
772 | 773 | import com.cloud.storage.VolumeApiServiceImpl; |
@@ -1998,42 +1999,76 @@ private List<StoragePool> findAllSuitableStoragePoolsForDetachedVolume(Volume vo |
1998 | 1999 | return suitablePools; |
1999 | 2000 | } |
2000 | 2001 |
|
2001 | | - HypervisorType hypervisorType = getHypervisorType(null, srcPool); |
2002 | | - List<ClusterVO> clusters = _clusterDao.listByDcHyType(srcPool.getDataCenterId(), hypervisorType.toString()); |
2003 | | - |
2004 | 2002 | 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<>(); |
2006 | 2005 |
|
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 | + } |
2008 | 2026 |
|
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 | + } |
2013 | 2037 |
|
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 | + } |
2017 | 2056 |
|
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()); |
2033 | 2065 | } |
2034 | 2066 | } |
2035 | 2067 | } |
2036 | 2068 |
|
| 2069 | + logger.debug("Found {} suitable pools out of {} total pools for detached volume {}", |
| 2070 | + suitablePools.size(), allPools.size(), volume.getUuid()); |
| 2071 | + |
2037 | 2072 | return suitablePools; |
2038 | 2073 | } |
2039 | 2074 |
|
|
0 commit comments