|
25 | 25 | import java.util.Comparator; |
26 | 26 | import java.util.Date; |
27 | 27 | import java.util.HashMap; |
| 28 | +import java.util.HashSet; |
28 | 29 | import java.util.Iterator; |
29 | 30 | import java.util.List; |
30 | 31 | import java.util.Map; |
31 | 32 | import java.util.Objects; |
32 | | -import java.util.Optional; |
33 | 33 | import java.util.Set; |
34 | 34 | import java.util.TimeZone; |
35 | 35 | import java.util.UUID; |
|
663 | 663 | import com.cloud.alert.dao.AlertDao; |
664 | 664 | import com.cloud.api.ApiDBUtils; |
665 | 665 | import com.cloud.api.query.dao.StoragePoolJoinDao; |
666 | | -import com.cloud.api.query.vo.StoragePoolJoinVO; |
667 | 666 | import com.cloud.capacity.Capacity; |
668 | 667 | import com.cloud.capacity.CapacityVO; |
669 | 668 | import com.cloud.capacity.dao.CapacityDao; |
|
768 | 767 | import com.cloud.storage.Storage; |
769 | 768 | import com.cloud.storage.StorageManager; |
770 | 769 | import com.cloud.storage.StoragePool; |
771 | | -import com.cloud.storage.StoragePoolStatus; |
772 | 770 | import com.cloud.storage.VMTemplateVO; |
773 | 771 | import com.cloud.storage.Volume; |
774 | 772 | import com.cloud.storage.VolumeApiServiceImpl; |
@@ -1991,23 +1989,51 @@ private List<StoragePool> findAllSuitableStoragePoolsForVm(final VolumeVO volume |
1991 | 1989 | private List<StoragePool> findAllSuitableStoragePoolsForDetachedVolume(Volume volume, Long diskOfferingId, List<? extends StoragePool> allPools) { |
1992 | 1990 | List<StoragePool> suitablePools = new ArrayList<>(); |
1993 | 1991 | if (CollectionUtils.isEmpty(allPools)) { |
1994 | | - return suitablePools; |
| 1992 | + return suitablePools; |
1995 | 1993 | } |
| 1994 | + |
| 1995 | + StoragePoolVO srcPool = _poolDao.findById(volume.getPoolId()); |
| 1996 | + if (srcPool == null) { |
| 1997 | + logger.warn("Source pool not found for volume {}: {}", volume.getName(), volume.getUuid()); |
| 1998 | + return suitablePools; |
| 1999 | + } |
| 2000 | + |
| 2001 | + HypervisorType hypervisorType = getHypervisorType(null, srcPool); |
| 2002 | + List<ClusterVO> clusters = _clusterDao.listByDcHyType(srcPool.getDataCenterId(), hypervisorType.toString()); |
| 2003 | + |
1996 | 2004 | DiskOfferingVO diskOffering = _diskOfferingDao.findById(diskOfferingId); |
1997 | | - List<String> tags = new ArrayList<>(); |
1998 | | - String[] tagsArray = diskOffering.getTagsArray(); |
1999 | | - if (tagsArray != null && tagsArray.length > 0) { |
2000 | | - tags = Arrays.asList(tagsArray); |
2001 | | - } |
2002 | | - Long[] poolIds = allPools.stream().map(StoragePool::getId).toArray(Long[]::new); |
2003 | | - List<StoragePoolJoinVO> pools = _poolJoinDao.searchByIds(poolIds); |
2004 | | - for (StoragePoolJoinVO storagePool : pools) { |
2005 | | - if (StoragePoolStatus.Up.equals(storagePool.getStatus()) && |
2006 | | - (CollectionUtils.isEmpty(tags) || tags.contains(storagePool.getTag()))) { |
2007 | | - Optional<? extends StoragePool> match = allPools.stream().filter(x -> x.getId() == storagePool.getId()).findFirst(); |
2008 | | - match.ifPresent(suitablePools::add); |
| 2005 | + DiskProfile diskProfile = new DiskProfile(volume, diskOffering, hypervisorType); |
| 2006 | + |
| 2007 | + ExcludeList avoid = new ExcludeList(); |
| 2008 | + |
| 2009 | + Set<Long> allPoolIds = allPools.stream() |
| 2010 | + .map(StoragePool::getId) |
| 2011 | + .collect(Collectors.toSet()); |
| 2012 | + Set<Long> addedPoolIds = new HashSet<>(); |
| 2013 | + |
| 2014 | + for (Cluster cluster : clusters) { |
| 2015 | + DataCenterDeployment plan = new DataCenterDeployment(srcPool.getDataCenterId(), cluster.getPodId(), cluster.getId(), |
| 2016 | + null, null, null, null); |
| 2017 | + |
| 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()); |
| 2033 | + } |
2009 | 2034 | } |
2010 | 2035 | } |
| 2036 | + |
2011 | 2037 | return suitablePools; |
2012 | 2038 | } |
2013 | 2039 |
|
|
0 commit comments