From bd4dee4b3859b0799e705f1ff865ce3e5124ec31 Mon Sep 17 00:00:00 2001 From: Fabricio Duarte Date: Thu, 28 Sep 2023 14:54:08 -0300 Subject: [PATCH 1/3] Rewrite usage sanity checks for template and network --- .../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 35dd31e9434f..f9ada5dadda4 100644 --- a/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java +++ b/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java @@ -117,10 +117,19 @@ protected void checkMaxUsage() throws SQLException { } 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) { @@ -174,8 +183,17 @@ protected void checkVolumeUsage() { } 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); } @@ -195,6 +213,7 @@ protected void readLastCheckId(){ String lastIdText = null; lastId = -1; if ((lastIdText = reader.readLine()) != null) { + s_logger.info(String.format("Read [%s] as lastId for Usage sanity checking.", lastIdText)); lastId = Integer.parseInt(lastIdText); } } catch (Exception e) { @@ -213,7 +232,9 @@ protected void readMaxId() throws SQLException { maxId = -1; if (rs.next() && (rs.getInt(1) > 0)) { maxId = rs.getInt(1); + s_logger.info(String.format("Read [%s] as maxId for Usage sanity checking.", maxId)); if (maxId > lastId) { + s_logger.info(String.format("The max id [%s] is greater than the last id [%s]; adding id check to the query.", maxId, lastId)); lastCheckId += " and cu.id <= ?"; } } From 6c39d81ee281f2cc2627944f3d2e49380b13d67f Mon Sep 17 00:00:00 2001 From: Fabricio Duarte Date: Thu, 8 Feb 2024 12:35:10 -0300 Subject: [PATCH 2/3] Apply Joao's suggestions 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> --- usage/src/main/java/com/cloud/usage/UsageSanityChecker.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java b/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java index f9ada5dadda4..f5dfc303da7d 100644 --- a/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java +++ b/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java @@ -213,7 +213,7 @@ protected void readLastCheckId(){ String lastIdText = null; lastId = -1; if ((lastIdText = reader.readLine()) != null) { - s_logger.info(String.format("Read [%s] as lastId for Usage sanity checking.", lastIdText)); + logger.info("Read {} as lastId for Usage sanity checking.", lastIdText); lastId = Integer.parseInt(lastIdText); } } catch (Exception e) { @@ -232,9 +232,9 @@ protected void readMaxId() throws SQLException { maxId = -1; if (rs.next() && (rs.getInt(1) > 0)) { maxId = rs.getInt(1); - s_logger.info(String.format("Read [%s] as maxId for Usage sanity checking.", maxId)); + logger.info("Read {} as maxId for Usage sanity checking.", maxId); if (maxId > lastId) { - s_logger.info(String.format("The max id [%s] is greater than the last id [%s]; adding id check to the query.", 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 <= ?"; } } From 234be6482d49b8476a2a0906f8040ad86f557959 Mon Sep 17 00:00:00 2001 From: Fabricio Duarte Date: Thu, 8 Feb 2024 13:36:58 -0300 Subject: [PATCH 3/3] Fix logger usages --- usage/src/main/java/com/cloud/usage/UsageSanityChecker.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java b/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java index e597253f58d4..d5dee9b00bc0 100644 --- a/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java +++ b/usage/src/main/java/com/cloud/usage/UsageSanityChecker.java @@ -214,7 +214,7 @@ protected void readLastCheckId(){ String lastIdText = null; lastId = -1; if ((lastIdText = reader.readLine()) != null) { - logger.info("Read {} as lastId for Usage sanity checking.", lastIdText); + LOGGER.info("Read {} as lastId for Usage sanity checking.", lastIdText); lastId = Integer.parseInt(lastIdText); } } catch (Exception e) { @@ -233,9 +233,9 @@ protected void readMaxId() throws SQLException { maxId = -1; if (rs.next() && (rs.getInt(1) > 0)) { maxId = rs.getInt(1); - logger.info("Read {} as maxId for Usage sanity checking.", maxId); + 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); + LOGGER.info("The max id {} is greater than the last id {}; adding id check to the query.", maxId, lastId); lastCheckId += " and cu.id <= ?"; } }