Skip to content

Commit 2a4c2c2

Browse files
raveningRakesh Venkatesh
andauthored
Global setting to select preferred storage pool (#5249)
* Global setting to select preferred storage pool Currently all the volumes are allocated on storage pools based on the capacity or the algorithm selected. Sometimes we need to deploy all volumes of particular account in a specific storage pool and in that case its not possible. with this change, we can specify the uuid of the preferred storage pool, so that all volumes of the account will be deployed in this pool * code feedback Co-authored-by: Rakesh Venkatesh <rakeshv@apache.org>
1 parent 0011d45 commit 2a4c2c2

4 files changed

Lines changed: 76 additions & 4 deletions

File tree

engine/components-api/src/main/java/com/cloud/storage/StorageManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ public interface StorageManager extends StorageService {
149149
"If set to true, the disk is created only when there is a suitable storage pool that supports the disk provisioning type specified by the service/disk offering. " +
150150
"If set to false, the disk is created with a disk provisioning type supported by the pool. Default value is false, and this is currently supported for VMware only.",
151151
true, ConfigKey.Scope.Zone);
152+
ConfigKey<String> PreferredStoragePool = new ConfigKey<String>(String.class, "preferred.storage.pool", "Advanced", "",
153+
"The UUID of preferred storage pool for allocation.", true, ConfigKey.Scope.Account, null);
154+
152155
/**
153156
* Returns a comma separated list of tags for the specified storage pool
154157
* @param poolId

engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.HashSet;
2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.Optional;
2930
import java.util.Set;
3031
import java.util.UUID;
3132
import java.util.concurrent.ExecutionException;
@@ -300,6 +301,31 @@ public VolumeVO allocateDuplicateVolumeVO(Volume oldVol, Long templateId) {
300301
return _volsDao.persist(newVol);
301302
}
302303

304+
private Optional<StoragePool> getMatchingStoragePool(String preferredPoolId, List<StoragePool> storagePools) {
305+
if (preferredPoolId == null) {
306+
return Optional.empty();
307+
}
308+
return storagePools.stream()
309+
.filter(pool -> pool.getUuid().equalsIgnoreCase(preferredPoolId))
310+
.findFirst();
311+
}
312+
313+
private Optional<StoragePool> getPreferredStoragePool(List<StoragePool> poolList, VirtualMachine vm) {
314+
String accountStoragePoolUuid = StorageManager.PreferredStoragePool.valueIn(vm.getAccountId());
315+
Optional<StoragePool> storagePool = getMatchingStoragePool(accountStoragePoolUuid, poolList);
316+
317+
if (storagePool.isPresent()) {
318+
s_logger.debug("A storage pool is specified for this account, so we will use this storage pool for allocation: "
319+
+ storagePool.get().getUuid());
320+
} else {
321+
String globalStoragePoolUuid = StorageManager.PreferredStoragePool.value();
322+
storagePool = getMatchingStoragePool(globalStoragePoolUuid, poolList);
323+
storagePool.ifPresent(pool -> s_logger.debug("A storage pool is specified in global setting, so we will use this storage pool for allocation: "
324+
+ pool.getUuid()));
325+
}
326+
return storagePool;
327+
}
328+
303329
@Override
304330
public StoragePool findStoragePool(DiskProfile dskCh, DataCenter dc, Pod pod, Long clusterId, Long hostId, VirtualMachine vm, final Set<StoragePool> avoid) {
305331
Long podId = null;
@@ -321,9 +347,13 @@ public StoragePool findStoragePool(DiskProfile dskCh, DataCenter dc, Pod pod, Lo
321347
}
322348
DataCenterDeployment plan = new DataCenterDeployment(dc.getId(), podId, clusterId, hostId, null, null);
323349

324-
final List<StoragePool> poolList = allocator.allocateToPool(dskCh, profile, plan, avoidList, 1);
350+
final List<StoragePool> poolList = allocator.allocateToPool(dskCh, profile, plan, avoidList, StoragePoolAllocator.RETURN_UPTO_ALL);
325351
if (poolList != null && !poolList.isEmpty()) {
326-
return (StoragePool)dataStoreMgr.getDataStore(poolList.get(0).getId(), DataStoreRole.Primary);
352+
// Check if the preferred storage pool can be used. If yes, use it.
353+
Optional<StoragePool> storagePool = getPreferredStoragePool(poolList, vm);
354+
355+
return (storagePool.isPresent()) ? (StoragePool) this.dataStoreMgr.getDataStore(storagePool.get().getId(), DataStoreRole.Primary) :
356+
(StoragePool)dataStoreMgr.getDataStore(poolList.get(0).getId(), DataStoreRole.Primary);
327357
}
328358
}
329359
return null;

server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.HashSet;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.Optional;
2627
import java.util.Set;
2728
import java.util.Timer;
2829
import java.util.TreeSet;
@@ -1674,7 +1675,7 @@ protected Pair<Map<Volume, List<StoragePool>>, List<Volume>> findSuitablePoolsFo
16741675
for (StoragePoolAllocator allocator : _storagePoolAllocators) {
16751676
final List<StoragePool> suitablePools = allocator.allocateToPool(diskProfile, vmProfile, plan, avoid, returnUpTo);
16761677
if (suitablePools != null && !suitablePools.isEmpty()) {
1677-
suitableVolumeStoragePools.put(toBeCreated, suitablePools);
1678+
checkForPreferredStoragePool(suitablePools, vmProfile.getVirtualMachine(), suitableVolumeStoragePools, toBeCreated);
16781679
foundPotentialPools = true;
16791680
break;
16801681
}
@@ -1715,6 +1716,43 @@ protected Pair<Map<Volume, List<StoragePool>>, List<Volume>> findSuitablePoolsFo
17151716
return new Pair<Map<Volume, List<StoragePool>>, List<Volume>>(suitableVolumeStoragePools, readyAndReusedVolumes);
17161717
}
17171718

1719+
private void checkForPreferredStoragePool(List<StoragePool> suitablePools,
1720+
VirtualMachine vm,
1721+
Map<Volume, List<StoragePool>> suitableVolumeStoragePools,
1722+
VolumeVO toBeCreated) {
1723+
List<StoragePool> pools = new ArrayList<>();
1724+
Optional<StoragePool> storagePool = getPreferredStoragePool(suitablePools, vm);
1725+
storagePool.ifPresent(pools::add);
1726+
1727+
pools.addAll(suitablePools);
1728+
suitableVolumeStoragePools.put(toBeCreated, pools);
1729+
}
1730+
1731+
private Optional<StoragePool> getMatchingStoragePool(String preferredPoolId, List<StoragePool> storagePools) {
1732+
if (preferredPoolId == null) {
1733+
return Optional.empty();
1734+
}
1735+
return storagePools.stream()
1736+
.filter(pool -> pool.getUuid().equalsIgnoreCase(preferredPoolId))
1737+
.findFirst();
1738+
}
1739+
1740+
private Optional<StoragePool> getPreferredStoragePool(List<StoragePool> poolList, VirtualMachine vm) {
1741+
String accountStoragePoolUuid = StorageManager.PreferredStoragePool.valueIn(vm.getAccountId());
1742+
Optional<StoragePool> storagePool = getMatchingStoragePool(accountStoragePoolUuid, poolList);
1743+
1744+
if (storagePool.isPresent()) {
1745+
s_logger.debug("A storage pool is specified for this account, so we will use this storage pool for allocation: "
1746+
+ storagePool.get().getUuid());
1747+
} else {
1748+
String globalStoragePoolUuid = StorageManager.PreferredStoragePool.value();
1749+
storagePool = getMatchingStoragePool(globalStoragePoolUuid, poolList);
1750+
storagePool.ifPresent(pool -> s_logger.debug("A storage pool is specified in global setting, so we will use this storage pool for allocation: "
1751+
+ pool.getUuid()));
1752+
}
1753+
return storagePool;
1754+
}
1755+
17181756
private boolean isEnabledForAllocation(long zoneId, Long podId, Long clusterId) {
17191757
// Check if the zone exists in the system
17201758
DataCenterVO zone = _dcDao.findById(zoneId);

server/src/main/java/com/cloud/storage/StorageManagerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3212,7 +3212,8 @@ public ConfigKey<?>[] getConfigKeys() {
32123212
PRIMARY_STORAGE_DOWNLOAD_WAIT,
32133213
SecStorageMaxMigrateSessions,
32143214
MaxDataMigrationWaitTime,
3215-
DiskProvisioningStrictness
3215+
DiskProvisioningStrictness,
3216+
PreferredStoragePool
32163217
};
32173218
}
32183219

0 commit comments

Comments
 (0)