Skip to content

Commit 6bf92fb

Browse files
authored
server: Search zone-wide storage pool when allocation algothrim is firstfitleastconsumed (#4002)
1 parent a651eaa commit 6bf92fb

File tree

5 files changed

+58
-10
lines changed

5 files changed

+58
-10
lines changed

engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId,
5656

5757
float findClusterConsumption(Long clusterId, short capacityType, long computeRequested);
5858

59-
List<Long> orderHostsByFreeCapacity(Long clusterId, short capacityType);
59+
List<Long> orderHostsByFreeCapacity(Long zoneId, Long clusterId, short capacityType);
6060
}

engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -903,20 +903,28 @@ public Pair<List<Long>, Map<Long, Double>> orderClustersByAggregateCapacity(long
903903
}
904904

905905
@Override
906-
public List<Long> orderHostsByFreeCapacity(Long clusterId, short capacityTypeForOrdering){
906+
public List<Long> orderHostsByFreeCapacity(Long zoneId, Long clusterId, short capacityTypeForOrdering){
907907
TransactionLegacy txn = TransactionLegacy.currentTxn();
908908
PreparedStatement pstmt = null;
909909
List<Long> result = new ArrayList<Long>();
910910
StringBuilder sql = new StringBuilder(ORDER_HOSTS_BY_FREE_CAPACITY_PART1);
911-
if(clusterId != null) {
912-
sql.append("AND cluster_id = ?");
913-
}
914-
sql.append(ORDER_HOSTS_BY_FREE_CAPACITY_PART2);
911+
if (zoneId != null) {
912+
sql.append(" AND data_center_id = ?");
913+
}
914+
if (clusterId != null) {
915+
sql.append(" AND cluster_id = ?");
916+
}
917+
sql.append(ORDER_HOSTS_BY_FREE_CAPACITY_PART2);
915918
try {
916919
pstmt = txn.prepareAutoCloseStatement(sql.toString());
917920
pstmt.setShort(1, capacityTypeForOrdering);
918-
if(clusterId != null) {
919-
pstmt.setLong(2, clusterId);
921+
int index = 2;
922+
if (zoneId != null) {
923+
pstmt.setLong(index, zoneId);
924+
index ++;
925+
}
926+
if (clusterId != null) {
927+
pstmt.setLong(index, clusterId);
920928
}
921929

922930
ResultSet rs = pstmt.executeQuery();

engine/storage/src/main/java/org/apache/cloudstack/storage/allocator/AbstractStoragePoolAllocator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public List<StoragePool> allocateToPool(DiskProfile dskCh, VirtualMachineProfile
9494

9595
protected List<StoragePool> reorderPoolsByCapacity(DeploymentPlan plan,
9696
List<StoragePool> pools) {
97+
Long zoneId = plan.getDataCenterId();
9798
Long clusterId = plan.getClusterId();
9899
short capacityType;
99100
if(pools != null && pools.size() != 0){
@@ -102,7 +103,7 @@ protected List<StoragePool> reorderPoolsByCapacity(DeploymentPlan plan,
102103
return null;
103104
}
104105

105-
List<Long> poolIdsByCapacity = capacityDao.orderHostsByFreeCapacity(clusterId, capacityType);
106+
List<Long> poolIdsByCapacity = capacityDao.orderHostsByFreeCapacity(zoneId, clusterId, capacityType);
106107
if (s_logger.isDebugEnabled()) {
107108
s_logger.debug("List of pools in descending order of free capacity: "+ poolIdsByCapacity);
108109
}

engine/storage/src/main/java/org/apache/cloudstack/storage/allocator/ZoneWideStoragePoolAllocator.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
3030
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
3131

32+
import com.cloud.capacity.Capacity;
33+
import com.cloud.capacity.dao.CapacityDao;
3234
import com.cloud.deploy.DeploymentPlan;
3335
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
3436
import com.cloud.hypervisor.Hypervisor.HypervisorType;
@@ -43,6 +45,8 @@ public class ZoneWideStoragePoolAllocator extends AbstractStoragePoolAllocator {
4345
private static final Logger LOGGER = Logger.getLogger(ZoneWideStoragePoolAllocator.class);
4446
@Inject
4547
private DataStoreManager dataStoreMgr;
48+
@Inject
49+
private CapacityDao capacityDao;
4650

4751

4852
@Override
@@ -110,6 +114,40 @@ private boolean canAddStoragePoolToAvoidSet(StoragePoolVO storagePoolVO) {
110114
return !ScopeType.ZONE.equals(storagePoolVO.getScope()) || !storagePoolVO.isManaged();
111115
}
112116

117+
118+
@Override
119+
protected List<StoragePool> reorderPoolsByCapacity(DeploymentPlan plan,
120+
List<StoragePool> pools) {
121+
Long zoneId = plan.getDataCenterId();
122+
short capacityType;
123+
if(pools != null && pools.size() != 0){
124+
capacityType = pools.get(0).getPoolType().isShared() ? Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED : Capacity.CAPACITY_TYPE_LOCAL_STORAGE;
125+
} else{
126+
return null;
127+
}
128+
129+
List<Long> poolIdsByCapacity = capacityDao.orderHostsByFreeCapacity(zoneId, null, capacityType);
130+
if (LOGGER.isDebugEnabled()) {
131+
LOGGER.debug("List of zone-wide storage pools in descending order of free capacity: "+ poolIdsByCapacity);
132+
}
133+
134+
//now filter the given list of Pools by this ordered list
135+
Map<Long, StoragePool> poolMap = new HashMap<>();
136+
for (StoragePool pool : pools) {
137+
poolMap.put(pool.getId(), pool);
138+
}
139+
List<Long> matchingPoolIds = new ArrayList<>(poolMap.keySet());
140+
141+
poolIdsByCapacity.retainAll(matchingPoolIds);
142+
143+
List<StoragePool> reorderedPools = new ArrayList<>();
144+
for(Long id: poolIdsByCapacity){
145+
reorderedPools.add(poolMap.get(id));
146+
}
147+
148+
return reorderedPools;
149+
}
150+
113151
@Override
114152
protected List<StoragePool> reorderPoolsByNumberOfVolumes(DeploymentPlan plan, List<StoragePool> pools, Account account) {
115153
if (account == null) {

server/src/main/java/com/cloud/agent/manager/allocator/impl/FirstFitAllocator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,15 @@ protected List<Host> allocateTo(DeploymentPlan plan, ServiceOffering offering, V
345345

346346
// Reorder hosts in the decreasing order of free capacity.
347347
private List<? extends Host> reorderHostsByCapacity(DeploymentPlan plan, List<? extends Host> hosts) {
348+
Long zoneId = plan.getDataCenterId();
348349
Long clusterId = plan.getClusterId();
349350
//Get capacity by which we should reorder
350351
String capacityTypeToOrder = _configDao.getValue(Config.HostCapacityTypeToOrderClusters.key());
351352
short capacityType = CapacityVO.CAPACITY_TYPE_CPU;
352353
if("RAM".equalsIgnoreCase(capacityTypeToOrder)){
353354
capacityType = CapacityVO.CAPACITY_TYPE_MEMORY;
354355
}
355-
List<Long> hostIdsByFreeCapacity = _capacityDao.orderHostsByFreeCapacity(clusterId, capacityType);
356+
List<Long> hostIdsByFreeCapacity = _capacityDao.orderHostsByFreeCapacity(zoneId, clusterId, capacityType);
356357
if (s_logger.isDebugEnabled()) {
357358
s_logger.debug("List of hosts in descending order of free capacity in the cluster: "+ hostIdsByFreeCapacity);
358359
}

0 commit comments

Comments
 (0)