Skip to content

Commit d171582

Browse files
Fix false positives in Usage sanity checks of templates and network offerings (#8136)
Co-authored-by: João Jandre <48719461+JoaoJandre@users.noreply.github.com>
1 parent 3baa45b commit d171582

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

usage/src/main/java/com/cloud/usage/UsageSanityChecker.java

+25-4
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,19 @@ protected void checkMaxUsage() throws SQLException {
118118
}
119119
int aggregationHours = aggregationRange / 60;
120120

121-
addCheckCase("SELECT count(*) FROM `cloud_usage`.`cloud_usage` cu where usage_type not in (4,5) and raw_usage > "
122-
+ aggregationHours,
121+
addCheckCase("SELECT count(*) FROM `cloud_usage`.`cloud_usage` cu where usage_type not in (4,5,13) and raw_usage > " + aggregationHours,
123122
"usage records with raw_usage > " + aggregationHours,
124123
lastCheckId);
124+
125+
addCheckCase("SELECT count(*) " +
126+
" FROM ( SELECT cu.id, max(cu.raw_usage)/count(n.id) as avg_usage " +
127+
" FROM `cloud_usage`.`cloud_usage` AS cu " +
128+
" INNER JOIN cloud.nics AS n ON (n.instance_id = cu.vm_instance_id) " +
129+
" WHERE cu.usage_type = 13 AND ((n.created <= cu.end_date) AND (n.removed is null OR n.removed > cu.start_date)) " +
130+
" GROUP BY cu.id) as cu " +
131+
" WHERE cu.avg_usage > " + aggregationHours,
132+
"network offering usage records with raw_usage > " + aggregationHours,
133+
lastCheckId);
125134
}
126135

127136
private static int getAggregationRange(int aggregationRange, PreparedStatement pstmt) {
@@ -175,8 +184,17 @@ protected void checkVolumeUsage() {
175184
}
176185

177186
protected void checkTemplateISOUsage() {
178-
addCheckCase("select count(*) from cloud_usage.cloud_usage cu inner join cloud.template_zone_ref tzr where "
179-
+ "cu.usage_id = tzr.template_id and cu.zone_id = tzr.zone_id and cu.usage_type in (7,8) and cu.start_date > tzr.removed ",
187+
addCheckCase("SELECT count(*) " +
188+
" FROM cloud_usage.cloud_usage AS cu " +
189+
" INNER JOIN cloud.template_zone_ref AS c_tzr ON ( c_tzr.template_id = cu.usage_id " +
190+
" AND c_tzr.zone_id = cu.zone_id) " +
191+
" WHERE cu.usage_type in (7,8) " +
192+
" AND cu.start_date > c_tzr.removed " +
193+
" AND NOT EXISTS ( SELECT 1 " +
194+
" FROM cloud.template_zone_ref c_tzr_internal " +
195+
" WHERE c_tzr_internal.template_id = c_tzr.template_id " +
196+
" AND c_tzr_internal.zone_id = c_tzr.zone_id " +
197+
" AND c_tzr_internal.removed IS NULL) ",
180198
"template/ISO usage records which are created after it is removed",
181199
lastCheckId);
182200
}
@@ -196,6 +214,7 @@ protected void readLastCheckId(){
196214
String lastIdText = null;
197215
lastId = -1;
198216
if ((lastIdText = reader.readLine()) != null) {
217+
LOGGER.info("Read {} as lastId for Usage sanity checking.", lastIdText);
199218
lastId = Integer.parseInt(lastIdText);
200219
}
201220
} catch (Exception e) {
@@ -214,7 +233,9 @@ protected void readMaxId() throws SQLException {
214233
maxId = -1;
215234
if (rs.next() && (rs.getInt(1) > 0)) {
216235
maxId = rs.getInt(1);
236+
LOGGER.info("Read {} as maxId for Usage sanity checking.", maxId);
217237
if (maxId > lastId) {
238+
LOGGER.info("The max id {} is greater than the last id {}; adding id check to the query.", maxId, lastId);
218239
lastCheckId += " and cu.id <= ?";
219240
}
220241
}

0 commit comments

Comments
 (0)