sureshanaparti commented on code in PR #6825:
URL: https://github.com/apache/cloudstack/pull/6825#discussion_r994025177


##########
usage/src/main/java/com/cloud/usage/UsageSanityChecker.java:
##########
@@ -120,6 +123,21 @@ protected void checkMaxUsage() throws SQLException {
                 lastCheckId);
     }
 
+    private static int getAggregationRange(int aggregationRange, 
PreparedStatement pstmt) {
+        try(ResultSet rs = pstmt.executeQuery();) {
+           if (rs.next()) {
+                aggregationRange = rs.getInt(1);
+            } else {
+               if (s_logger.isDebugEnabled()) {
+                   s_logger.debug("Failed to retrieve aggregation range. Using 
default : " + aggregationRange);
+               }
+            }
+        }catch (SQLException e) {

Review Comment:
   ```suggestion
           } catch (SQLException e) {
   ```



##########
usage/src/main/java/com/cloud/usage/UsageSanityChecker.java:
##########
@@ -170,14 +188,21 @@ protected void checkSnapshotUsage() {
     }
 
     protected void readLastCheckId(){
+        if (s_logger.isDebugEnabled()) {
+            s_logger.debug("reading last checked id for sanity check");
+        }
         try(BufferedReader reader = new BufferedReader(new 
FileReader(lastCheckFile));) {
             String lastIdText = null;
             lastId = -1;
-            if ((reader != null) && (lastIdText = reader.readLine()) != null) {
+            if ((lastIdText = reader.readLine()) != null) {
                 lastId = Integer.parseInt(lastIdText);
             }
         } catch (Exception e) {
-            s_logger.error("readLastCheckId:Exception:"+e.getMessage(),e);
+            String msg = String.format("error reading the LastCheckId reason: 
%s", e.getMessage());

Review Comment:
   ```suggestion
               String msg = String.format("Error reading the LastCheckId 
reason: %s", e.getMessage());
   ```



##########
usage/src/main/java/com/cloud/usage/UsageSanityChecker.java:
##########
@@ -170,14 +188,21 @@ protected void checkSnapshotUsage() {
     }
 
     protected void readLastCheckId(){
+        if (s_logger.isDebugEnabled()) {
+            s_logger.debug("reading last checked id for sanity check");
+        }
         try(BufferedReader reader = new BufferedReader(new 
FileReader(lastCheckFile));) {
             String lastIdText = null;
             lastId = -1;
-            if ((reader != null) && (lastIdText = reader.readLine()) != null) {
+            if ((lastIdText = reader.readLine()) != null) {
                 lastId = Integer.parseInt(lastIdText);
             }
         } catch (Exception e) {
-            s_logger.error("readLastCheckId:Exception:"+e.getMessage(),e);
+            String msg = String.format("error reading the LastCheckId reason: 
%s", e.getMessage());
+            s_logger.error(msg);
+            s_logger.debug(msg, e);
+        } finally {
+            s_logger.info(String.format("using %d as last checked id to start 
from in sanity check", lastId));

Review Comment:
   ```suggestion
               s_logger.info(String.format("Using %d as last checked id to 
start from in sanity check", lastId));
   ```
   
   other logs messages if applicable



##########
usage/src/main/java/com/cloud/usage/UsageSanityChecker.java:
##########
@@ -120,6 +123,21 @@ protected void checkMaxUsage() throws SQLException {
                 lastCheckId);
     }
 
+    private static int getAggregationRange(int aggregationRange, 
PreparedStatement pstmt) {
+        try(ResultSet rs = pstmt.executeQuery();) {
+           if (rs.next()) {
+                aggregationRange = rs.getInt(1);
+            } else {
+               if (s_logger.isDebugEnabled()) {
+                   s_logger.debug("Failed to retrieve aggregation range. Using 
default : " + aggregationRange);
+               }
+            }
+        }catch (SQLException e) {
+            throwPreparedStatementExcecutionException("retrieval aggregate 
value is failing", pstmt.toString(), e);

Review Comment:
   ```suggestion
               throwPreparedStatementExcecutionException("Retrieval aggregate 
value is failing", pstmt.toString(), e);
   ```



##########
usage/src/main/java/com/cloud/usage/UsageSanityChecker.java:
##########
@@ -170,14 +188,21 @@ protected void checkSnapshotUsage() {
     }
 
     protected void readLastCheckId(){
+        if (s_logger.isDebugEnabled()) {
+            s_logger.debug("reading last checked id for sanity check");

Review Comment:
   ```suggestion
               s_logger.debug("Reading last checked id for sanity check");
   ```



##########
usage/src/main/java/com/cloud/usage/UsageSanityChecker.java:
##########
@@ -69,48 +69,51 @@ protected boolean checkItemCountByPstmt(CheckCase 
checkCase) throws SQLException
         /*
          * Check for item usage records which are created after it is removed
          */
-        try (PreparedStatement pstmt = 
conn.prepareStatement(checkCase.sqlTemplate)) {
-            if(checkCase.checkId) {
-                pstmt.setInt(1, lastId);
-                pstmt.setInt(2, maxId);
-            }
-            try(ResultSet rs = pstmt.executeQuery();) {
-                if (rs.next() && (rs.getInt(1) > 0)) {
-                    errors.append(String.format("Error: Found %s %s\n", 
rs.getInt(1), checkCase.itemName));
-                    checkOk = false;
+        try (PreparedStatement pstmt = 
conn.prepareStatement(checkCase.getSqlTemplate())) {
+            if(checkCase.isCheckId()) {
+                if (lastId > 0) {
+                    pstmt.setInt(1, lastId);
+                }
+                if (maxId > lastId) {
+                    pstmt.setInt(2, maxId);
                 }
-            }catch (Exception e)
-            {
-                
s_logger.error("checkItemCountByPstmt:Exception:"+e.getMessage());
-                throw new 
CloudRuntimeException("checkItemCountByPstmt:Exception:"+e.getMessage(),e);
             }
+            checkOk = isCheckOkForPstmt(checkCase, checkOk, pstmt);
         }
         catch (Exception e)
         {
-            s_logger.error("checkItemCountByPstmt:Exception:"+e.getMessage());
-            throw new 
CloudRuntimeException("checkItemCountByPstmt:Exception:"+e.getMessage(),e);
+            throwPreparedStatementExcecutionException("preparing statement", 
checkCase.getSqlTemplate(), e);
+        }
+        return checkOk;
+    }
+
+    private boolean isCheckOkForPstmt(CheckCase checkCase, boolean checkOk, 
PreparedStatement pstmt) {
+        try(ResultSet rs = pstmt.executeQuery();) {
+            if (rs.next() && (rs.getInt(1) > 0)) {
+                errors.append(String.format("Error: Found %s %s%n", 
rs.getInt(1), checkCase.getItemName()));
+                checkOk = false;
+            }
+        }catch (Exception e)
+        {
+            throwPreparedStatementExcecutionException("check is failing", 
pstmt.toString(), e);
         }
         return checkOk;
     }
 
+    private static void throwPreparedStatementExcecutionException(String 
msgPrefix, String stmt, Exception e) {
+        String msg = String.format("%s for prepared statement \"%s\" reason: 
%s", msgPrefix, stmt, e.getMessage());
+        s_logger.error(msg);
+        throw new CloudRuntimeException(msg, e);
+    }
+
     protected void checkMaxUsage() throws SQLException {
         int aggregationRange = DEFAULT_AGGREGATION_RANGE;
-        try (PreparedStatement pstmt = conn.prepareStatement(
-                "SELECT value FROM `cloud`.`configuration` where name = 
'usage.stats.job.aggregation.range'");)
+        String sql = "SELECT value FROM `cloud`.`configuration` where name = 
'usage.stats.job.aggregation.range'";

Review Comment:
   ```suggestion
           String sql = "SELECT value FROM `cloud`.`configuration` WHERE name = 
'usage.stats.job.aggregation.range'";
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to