Skip to content

Commit dc5ba75

Browse files
committed
Consider flexible tags as for detached volume migration
1 parent f1f779a commit dc5ba75

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

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

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
import java.util.Comparator;
2626
import java.util.Date;
2727
import java.util.HashMap;
28+
import java.util.HashSet;
2829
import java.util.Iterator;
2930
import java.util.List;
3031
import java.util.Map;
3132
import java.util.Objects;
32-
import java.util.Optional;
3333
import java.util.Set;
3434
import java.util.TimeZone;
3535
import java.util.UUID;
@@ -663,7 +663,6 @@
663663
import com.cloud.alert.dao.AlertDao;
664664
import com.cloud.api.ApiDBUtils;
665665
import com.cloud.api.query.dao.StoragePoolJoinDao;
666-
import com.cloud.api.query.vo.StoragePoolJoinVO;
667666
import com.cloud.capacity.Capacity;
668667
import com.cloud.capacity.CapacityVO;
669668
import com.cloud.capacity.dao.CapacityDao;
@@ -768,7 +767,6 @@
768767
import com.cloud.storage.Storage;
769768
import com.cloud.storage.StorageManager;
770769
import com.cloud.storage.StoragePool;
771-
import com.cloud.storage.StoragePoolStatus;
772770
import com.cloud.storage.VMTemplateVO;
773771
import com.cloud.storage.Volume;
774772
import com.cloud.storage.VolumeApiServiceImpl;
@@ -1991,23 +1989,51 @@ private List<StoragePool> findAllSuitableStoragePoolsForVm(final VolumeVO volume
19911989
private List<StoragePool> findAllSuitableStoragePoolsForDetachedVolume(Volume volume, Long diskOfferingId, List<? extends StoragePool> allPools) {
19921990
List<StoragePool> suitablePools = new ArrayList<>();
19931991
if (CollectionUtils.isEmpty(allPools)) {
1994-
return suitablePools;
1992+
return suitablePools;
19951993
}
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+
19962004
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+
}
20092034
}
20102035
}
2036+
20112037
return suitablePools;
20122038
}
20132039

0 commit comments

Comments
 (0)