Skip to content

Commit b895051

Browse files
lucas-a-martinsLucas Martins
authored andcommitted
Fix Stats Collector to not divide by zero (apache#10492)
* Set loadHistory value to zero when interval is zero to not throw arithmatic exception * Change loadHistory value to -1 and fix maxsize bug --------- Co-authored-by: Lucas Martins <[email protected]>
1 parent 8b96d95 commit b895051

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

server/src/main/java/com/cloud/server/StatsCollector.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -738,25 +738,25 @@ class DbCollector extends AbstractStatsCollector {
738738
protected void runInContext() {
739739
logger.debug(String.format("%s is running...", this.getClass().getSimpleName()));
740740

741-
try {
742-
long lastUptime = (dbStats.containsKey(uptime) ? (Long) dbStats.get(uptime) : 0);
743-
long lastQueries = (dbStats.containsKey(queries) ? (Long) dbStats.get(queries) : 0);
744-
getDynamicDataFromDB();
745-
long interval = (Long) dbStats.get(uptime) - lastUptime;
746-
long activity = (Long) dbStats.get(queries) - lastQueries;
747-
loadHistory.add(0, Double.valueOf(activity / interval));
748-
int maxsize = DATABASE_SERVER_LOAD_HISTORY_RETENTION_NUMBER.value();
749-
while (loadHistory.size() > maxsize) {
750-
loadHistory.remove(maxsize - 1);
751-
}
752-
} catch (Throwable e) {
753-
// pokemon catch to make sure the thread stays running
754-
logger.error("db statistics collection failed due to " + e.getLocalizedMessage());
755-
if (logger.isDebugEnabled()) {
756-
logger.debug("db statistics collection failed.", e);
757-
}
758-
}
759-
}
741+
try {
742+
long lastUptime = (dbStats.containsKey(uptime) ? (Long) dbStats.get(uptime) : 0);
743+
long lastQueries = (dbStats.containsKey(queries) ? (Long) dbStats.get(queries) : 0);
744+
getDynamicDataFromDB();
745+
long interval = (Long) dbStats.get(uptime) - lastUptime;
746+
long activity = (Long) dbStats.get(queries) - lastQueries;
747+
loadHistory.add(0, interval == 0 ? -1 : Double.valueOf(activity / interval));
748+
int maxsize = DATABASE_SERVER_LOAD_HISTORY_RETENTION_NUMBER.value();
749+
while (loadHistory.size() > maxsize) {
750+
loadHistory.remove(maxsize);
751+
}
752+
} catch (Throwable e) {
753+
// pokemon catch to make sure the thread stays running
754+
logger.error("db statistics collection failed due to " + e.getLocalizedMessage());
755+
if (logger.isDebugEnabled()) {
756+
logger.debug("db statistics collection failed.", e);
757+
}
758+
}
759+
}
760760

761761
private void getDynamicDataFromDB() {
762762
Map<String, String> stats = DbUtil.getDbInfo("STATUS", queries, uptime);

0 commit comments

Comments
 (0)