Skip to content

Commit 8a3c8ae

Browse files
Address review feedback on the weighted allocation algorithm
vm_instance.update_time is a TIMESTAMP column, so there is no date that reliably means "never" - anything past 2038 is out of range. Counting with no cut-off used Long.MAX_VALUE, which is roughly year 292 million. The query now leaves the timestamp out entirely in that case rather than binding an impossible one. Also catch Exception rather than Throwable in the new query, and put the config key array one entry per line. Signed-off-by: Brad House <bhouse@nexthop.ai>
1 parent 6a02514 commit 8a3c8ae

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
155155
private static final String COUNT_VMS_BASED_ON_VGPU_TYPES2 =
156156
"GROUP BY gpu_card.name, vgpu_profile.name";
157157

158-
private static final String COUNT_VMS_BY_HOST = "SELECT host.id, COUNT(vm.id), SUM(IF(vm.update_time > ?, 1, 0)) " +
158+
// %s is the "changed state recently" test, or a constant 0 when no cut-off is given. It is not
159+
// a bound parameter because there is no timestamp that reliably means "never" - update_time is
160+
// a TIMESTAMP column, so anything past 2038 is out of range.
161+
private static final String COUNT_VMS_BY_HOST = "SELECT host.id, COUNT(vm.id), SUM(IF(%s, 1, 0)) " +
159162
"FROM `cloud`.`host` host LEFT JOIN `cloud`.`vm_instance` vm " +
160163
"ON vm.host_id = host.id AND vm.state IN ('Running', 'Starting', 'Stopping', 'Migrating') " +
161164
"AND vm.removed IS NULL WHERE host.type = 'Routing' AND host.removed IS NULL AND host.data_center_id = ? ";
@@ -807,7 +810,7 @@ public Pair<List<Long>, Map<Long, Double>> listPodIdsInZoneByVmCount(long dataCe
807810
public Map<Long, Pair<Long, Long>> countVmsByHost(long dcId, Long podId, Long clusterId, Date changedStateAfter) {
808811
TransactionLegacy txn = TransactionLegacy.currentTxn();
809812
Map<Long, Pair<Long, Long>> result = new HashMap<>();
810-
String sql = COUNT_VMS_BY_HOST;
813+
String sql = String.format(COUNT_VMS_BY_HOST, changedStateAfter != null ? "vm.update_time > ?" : "0");
811814
if (podId != null) {
812815
sql = sql + " AND host.pod_id = ? ";
813816
}
@@ -818,9 +821,9 @@ public Map<Long, Pair<Long, Long>> countVmsByHost(long dcId, Long podId, Long cl
818821
try {
819822
PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql);
820823
int index = 1;
821-
// a cut-off in the future counts nothing as recent, which is what a null asks for
822-
long cutOff = changedStateAfter != null ? changedStateAfter.getTime() : Long.MAX_VALUE;
823-
pstmt.setTimestamp(index++, new Timestamp(cutOff));
824+
if (changedStateAfter != null) {
825+
pstmt.setTimestamp(index++, new Timestamp(changedStateAfter.getTime()));
826+
}
824827
pstmt.setLong(index++, dcId);
825828
if (podId != null) {
826829
pstmt.setLong(index++, podId);
@@ -835,7 +838,7 @@ public Map<Long, Pair<Long, Long>> countVmsByHost(long dcId, Long podId, Long cl
835838
return result;
836839
} catch (SQLException e) {
837840
throw new CloudRuntimeException("DB Exception on: " + sql, e);
838-
} catch (Throwable e) {
841+
} catch (Exception e) {
839842
throw new CloudRuntimeException("Caught: " + sql, e);
840843
}
841844
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,19 @@ public String getConfigComponentName() {
400400

401401
@Override
402402
public ConfigKey<?>[] getConfigKeys() {
403-
return new ConfigKey<?>[] {HostScoringWeights.CpuAllocatedWeight, HostScoringWeights.CpuUsedWeight, HostScoringWeights.MemoryAllocatedWeight, HostScoringWeights.MemoryUsedWeight,
404-
VmCountWeight, RecentStartWeight, DominantResourceWeight, RecentStartWindow, ExpectedVmsPerHost,
405-
CpuUtilisationThreshold, MemoryUtilisationThreshold, SelectionSpread};
403+
return new ConfigKey<?>[] {
404+
HostScoringWeights.CpuAllocatedWeight,
405+
HostScoringWeights.CpuUsedWeight,
406+
HostScoringWeights.MemoryAllocatedWeight,
407+
HostScoringWeights.MemoryUsedWeight,
408+
VmCountWeight,
409+
RecentStartWeight,
410+
DominantResourceWeight,
411+
RecentStartWindow,
412+
ExpectedVmsPerHost,
413+
CpuUtilisationThreshold,
414+
MemoryUtilisationThreshold,
415+
SelectionSpread
416+
};
406417
}
407418
}

0 commit comments

Comments
 (0)