Skip to content

Commit 979386d

Browse files
committed
log capacity alongwith host/storagaepool ids
1 parent e12fd49 commit 979386d

File tree

5 files changed

+49
-14
lines changed

5 files changed

+49
-14
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
@@ -64,5 +64,5 @@ List<SummedCapacity> listCapacitiesGroupedByLevelAndType(Integer capacityType, L
6464

6565
float findClusterConsumption(Long clusterId, short capacityType, long computeRequested);
6666

67-
List<Long> orderHostsByFreeCapacity(Long zoneId, Long clusterId, short capacityType);
67+
Pair<List<Long>, Map<Long, Double>> orderHostsByFreeCapacity(Long zoneId, Long clusterId, short capacityType);
6868
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,10 +1028,11 @@ public Pair<List<Long>, Map<Long, Double>> orderClustersByAggregateCapacity(long
10281028
}
10291029

10301030
@Override
1031-
public List<Long> orderHostsByFreeCapacity(Long zoneId, Long clusterId, short capacityTypeForOrdering){
1031+
public Pair<List<Long>, Map<Long, Double>> orderHostsByFreeCapacity(Long zoneId, Long clusterId, short capacityTypeForOrdering){
10321032
TransactionLegacy txn = TransactionLegacy.currentTxn();
10331033
PreparedStatement pstmt = null;
1034-
List<Long> result = new ArrayList<Long>();
1034+
List<Long> result = new ArrayList<>();
1035+
Map<Long, Double> hostCapacityMap = new HashMap<>();
10351036
StringBuilder sql = new StringBuilder(ORDER_HOSTS_BY_FREE_CAPACITY_PART1);
10361037
if (zoneId != null) {
10371038
sql.append(" AND data_center_id = ?");
@@ -1054,9 +1055,11 @@ public List<Long> orderHostsByFreeCapacity(Long zoneId, Long clusterId, short ca
10541055

10551056
ResultSet rs = pstmt.executeQuery();
10561057
while (rs.next()) {
1057-
result.add(rs.getLong(1));
1058+
Long hostId = rs.getLong(1);
1059+
result.add(hostId);
1060+
hostCapacityMap.put(hostId, rs.getDouble(2));
10581061
}
1059-
return result;
1062+
return new Pair<>(result, hostCapacityMap);
10601063
} catch (SQLException e) {
10611064
throw new CloudRuntimeException("DB Exception on: " + sql, e);
10621065
} catch (Throwable e) {

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,22 @@
5757
import javax.naming.ConfigurationException;
5858
import java.math.BigDecimal;
5959
import java.security.SecureRandom;
60+
import java.text.DecimalFormat;
6061
import java.util.ArrayList;
6162
import java.util.Arrays;
6263
import java.util.Collections;
64+
import java.util.Comparator;
6365
import java.util.HashMap;
66+
import java.util.LinkedHashMap;
6467
import java.util.List;
6568
import java.util.Map;
69+
import java.util.stream.Collectors;
6670

6771
public abstract class AbstractStoragePoolAllocator extends AdapterBase implements StoragePoolAllocator {
6872

6973
protected BigDecimal storageOverprovisioningFactor = new BigDecimal(1);
7074
protected long extraBytesPerVolume = 0;
75+
static DecimalFormat decimalFormat = new DecimalFormat("#.##");
7176
@Inject protected DataStoreManager dataStoreMgr;
7277
@Inject protected PrimaryDataStoreDao storagePoolDao;
7378
@Inject protected VolumeDao volumeDao;
@@ -137,12 +142,16 @@ protected List<StoragePool> reorderPoolsByCapacity(DeploymentPlan plan, List<Sto
137142
capacityType, storagePool.getName(), storagePool.getUuid(), storageType
138143
));
139144

140-
List<Long> poolIdsByCapacity = capacityDao.orderHostsByFreeCapacity(zoneId, clusterId, capacityType);
145+
Pair<List<Long>, Map<Long, Double>> result = capacityDao.orderHostsByFreeCapacity(zoneId, clusterId, capacityType);
146+
List<Long> poolIdsByCapacity = result.first();
147+
Map<Long, String> sortedHostByCapacity = result.second().entrySet()
148+
.stream()
149+
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
150+
.collect(Collectors.toMap(Map.Entry::getKey, entry -> decimalFormat.format(entry.getValue() * 100) + "%", (e1, e2) -> e1, LinkedHashMap::new));
151+
logger.debug("List of pools in descending order of hostId: [{}] available capacity (percentage): {}",
152+
poolIdsByCapacity, sortedHostByCapacity);
141153

142-
logger.debug(String.format("List of pools in descending order of available capacity [%s].", poolIdsByCapacity));
143-
144-
145-
//now filter the given list of Pools by this ordered list
154+
// now filter the given list of Pools by this ordered list
146155
Map<Long, StoragePool> poolMap = new HashMap<>();
147156
for (StoragePool pool : pools) {
148157
poolMap.put(pool.getId(), pool);

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Arrays;
21+
import java.util.Comparator;
2122
import java.util.HashMap;
23+
import java.util.LinkedHashMap;
2224
import java.util.List;
2325
import java.util.Map;
26+
import java.util.stream.Collectors;
2427

2528
import javax.inject.Inject;
2629

30+
import com.cloud.utils.Pair;
2731
import org.springframework.stereotype.Component;
2832

2933
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
@@ -122,9 +126,16 @@ protected List<StoragePool> reorderPoolsByCapacity(DeploymentPlan plan,
122126
return null;
123127
}
124128

125-
List<Long> poolIdsByCapacity = capacityDao.orderHostsByFreeCapacity(zoneId, null, capacityType);
129+
Pair<List<Long>, Map<Long, Double>> result = capacityDao.orderHostsByFreeCapacity(zoneId, null, capacityType);
130+
List<Long> poolIdsByCapacity = result.first();
131+
Map<Long, String> sortedHostByCapacity = result.second().entrySet()
132+
.stream()
133+
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
134+
.collect(Collectors.toMap(Map.Entry::getKey, entry -> decimalFormat.format(entry.getValue() * 100) + "%",
135+
(e1, e2) -> e1, LinkedHashMap::new));
126136
if (logger.isDebugEnabled()) {
127-
logger.debug("List of zone-wide storage pools in descending order of free capacity: "+ poolIdsByCapacity);
137+
logger.debug("List of zone-wide storage pools: [{}] in descending order of free capacity (percentage): {}",
138+
poolIdsByCapacity, sortedHostByCapacity);
128139
}
129140

130141
//now filter the given list of Pools by this ordered list

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
// under the License.
1717
package com.cloud.agent.manager.allocator.impl;
1818

19+
import java.text.DecimalFormat;
1920
import java.util.ArrayList;
2021
import java.util.Collections;
22+
import java.util.Comparator;
2123
import java.util.HashMap;
24+
import java.util.LinkedHashMap;
2225
import java.util.List;
2326
import java.util.Map;
27+
import java.util.stream.Collectors;
2428

2529
import javax.inject.Inject;
2630
import javax.naming.ConfigurationException;
@@ -98,6 +102,7 @@ public class FirstFitAllocator extends AdapterBase implements HostAllocator {
98102
UserVmDetailsDao _userVmDetailsDao;
99103

100104
boolean _checkHvm = true;
105+
static DecimalFormat decimalFormat = new DecimalFormat("#.##");
101106

102107
@Override
103108
public List<Host> allocateTo(VirtualMachineProfile vmProfile, DeploymentPlan plan, Type type, ExcludeList avoid, int returnUpTo) {
@@ -373,9 +378,16 @@ private List<? extends Host> reorderHostsByCapacity(DeploymentPlan plan, List<?
373378
if("RAM".equalsIgnoreCase(capacityTypeToOrder)){
374379
capacityType = CapacityVO.CAPACITY_TYPE_MEMORY;
375380
}
376-
List<Long> hostIdsByFreeCapacity = _capacityDao.orderHostsByFreeCapacity(zoneId, clusterId, capacityType);
381+
Pair<List<Long>, Map<Long, Double>> result = _capacityDao.orderHostsByFreeCapacity(zoneId, clusterId, capacityType);
382+
List<Long> hostIdsByFreeCapacity = result.first();
383+
Map<Long, String> sortedHostByCapacity = result.second().entrySet()
384+
.stream()
385+
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
386+
.collect(Collectors.toMap(Map.Entry::getKey, entry -> decimalFormat.format(entry.getValue() * 100) + "%",
387+
(e1, e2) -> e1, LinkedHashMap::new));
377388
if (logger.isDebugEnabled()) {
378-
logger.debug("List of hosts in descending order of free capacity in the cluster: "+ hostIdsByFreeCapacity);
389+
logger.debug("List of hosts: [{}] in descending order of free capacity (percentage) in the cluster: {}",
390+
hostIdsByFreeCapacity, sortedHostByCapacity);
379391
}
380392

381393
//now filter the given list of Hosts by this ordered list

0 commit comments

Comments
 (0)