From d171582163440447b8091acff7efb93ea27d5c2b Mon Sep 17 00:00:00 2001 From: Fabricio Duarte Date: Mon, 26 Feb 2024 17:14:24 -0300 Subject: [PATCH] Fix false positives in Usage sanity checks of templates and network offerings (#8136) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: João Jandre <48719461+JoaoJandre@users.noreply.github.com> --- .../com/cloud/usage/UsageSanityChecker.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java b/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java index 933f818d657..d5dee9b00bc 100644 --- a/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java +++ b/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java @@ -118,10 +118,19 @@ public class UsageSanityChecker { } int aggregationHours = aggregationRange / 60; - addCheckCase("SELECT count(*) FROM `cloud_usage`.`cloud_usage` cu where usage_type not in (4,5) and raw_usage > " - + aggregationHours, + addCheckCase("SELECT count(*) FROM `cloud_usage`.`cloud_usage` cu where usage_type not in (4,5,13) and raw_usage > " + aggregationHours, "usage records with raw_usage > " + aggregationHours, lastCheckId); + + addCheckCase("SELECT count(*) " + + " FROM ( SELECT cu.id, max(cu.raw_usage)/count(n.id) as avg_usage " + + " FROM `cloud_usage`.`cloud_usage` AS cu " + + " INNER JOIN cloud.nics AS n ON (n.instance_id = cu.vm_instance_id) " + + " WHERE cu.usage_type = 13 AND ((n.created <= cu.end_date) AND (n.removed is null OR n.removed > cu.start_date)) " + + " GROUP BY cu.id) as cu " + + " WHERE cu.avg_usage > " + aggregationHours, + "network offering usage records with raw_usage > " + aggregationHours, + lastCheckId); } private static int getAggregationRange(int aggregationRange, PreparedStatement pstmt) { @@ -175,8 +184,17 @@ public class UsageSanityChecker { } protected void checkTemplateISOUsage() { - addCheckCase("select count(*) from cloud_usage.cloud_usage cu inner join cloud.template_zone_ref tzr where " - + "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 ", + addCheckCase("SELECT count(*) " + + " FROM cloud_usage.cloud_usage AS cu " + + " INNER JOIN cloud.template_zone_ref AS c_tzr ON ( c_tzr.template_id = cu.usage_id " + + " AND c_tzr.zone_id = cu.zone_id) " + + " WHERE cu.usage_type in (7,8) " + + " AND cu.start_date > c_tzr.removed " + + " AND NOT EXISTS ( SELECT 1 " + + " FROM cloud.template_zone_ref c_tzr_internal " + + " WHERE c_tzr_internal.template_id = c_tzr.template_id " + + " AND c_tzr_internal.zone_id = c_tzr.zone_id " + + " AND c_tzr_internal.removed IS NULL) ", "template/ISO usage records which are created after it is removed", lastCheckId); } @@ -196,6 +214,7 @@ public class UsageSanityChecker { String lastIdText = null; lastId = -1; if ((lastIdText = reader.readLine()) != null) { + LOGGER.info("Read {} as lastId for Usage sanity checking.", lastIdText); lastId = Integer.parseInt(lastIdText); } } catch (Exception e) { @@ -214,7 +233,9 @@ public class UsageSanityChecker { maxId = -1; if (rs.next() && (rs.getInt(1) > 0)) { maxId = rs.getInt(1); + LOGGER.info("Read {} as maxId for Usage sanity checking.", maxId); if (maxId > lastId) { + LOGGER.info("The max id {} is greater than the last id {}; adding id check to the query.", maxId, lastId); lastCheckId += " and cu.id <= ?"; } }