Skip to content

Commit 334864f

Browse files
Prevent overflow and null pointers + small code enhancements
1 parent 8c078b8 commit 334864f

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ public String toString() {
177177
*/
178178
private static final int INFLUXDB_DEFAULT_PORT = 8086;
179179

180+
private static final int HOURLY_TIME = 60;
181+
private static final int DAILY_TIME = HOURLY_TIME * 24;
182+
private static final int ONE_MINUTE_IN_MILLISCONDS = 60 * 1000;
183+
180184
private static final String UUID_TAG = "uuid";
181185

182186
private static final String TOTAL_MEMORY_KBS_FIELD = "total_memory_kb";
@@ -427,8 +431,6 @@ protected void init(Map<String, String> configs) {
427431
Calendar cal = Calendar.getInstance(usageTimezone);
428432
cal.setTime(new Date());
429433
long endDate = 0;
430-
int HOURLY_TIME = 60;
431-
final int DAILY_TIME = 60 * 24;
432434
if (_usageAggregationRange == DAILY_TIME) {
433435
cal.set(Calendar.HOUR_OF_DAY, 0);
434436
cal.set(Calendar.MINUTE, 0);
@@ -454,8 +456,9 @@ protected void init(Map<String, String> configs) {
454456
s_logger.warn("Usage stats job aggregation range is to small, using the minimum value of " + UsageUtils.USAGE_AGGREGATION_RANGE_MIN);
455457
_usageAggregationRange = UsageUtils.USAGE_AGGREGATION_RANGE_MIN;
456458
}
457-
_diskStatsUpdateExecutor.scheduleAtFixedRate(new VmDiskStatsUpdaterTask(), (endDate - System.currentTimeMillis()), (_usageAggregationRange * 60 * 1000),
458-
TimeUnit.MILLISECONDS);
459+
460+
long period = _usageAggregationRange * ONE_MINUTE_IN_MILLISCONDS;
461+
_diskStatsUpdateExecutor.scheduleAtFixedRate(new VmDiskStatsUpdaterTask(), (endDate - System.currentTimeMillis()), period, TimeUnit.MILLISECONDS);
459462

460463
}
461464

@@ -1209,7 +1212,7 @@ private String getAutoscaleAction(HashMap<Long, Double> avgCounter, long groupId
12091212
for (AutoScaleVmGroupPolicyMapVO asVmgPmap : listMap) {
12101213
AutoScalePolicyVO policyVO = _asPolicyDao.findById(asVmgPmap.getPolicyId());
12111214
if (policyVO != null) {
1212-
Integer quitetime = policyVO.getQuietTime();
1215+
int quitetime = policyVO.getQuietTime();
12131216
Date quitetimeDate = policyVO.getLastQuiteTime();
12141217
long last_quitetime = 0L;
12151218
if (quitetimeDate != null) {
@@ -1227,7 +1230,7 @@ private String getAutoscaleAction(HashMap<Long, Double> avgCounter, long groupId
12271230
// check whole conditions of this policy
12281231
for (ConditionVO conditionVO : lstConditions) {
12291232
long thresholdValue = conditionVO.getThreshold();
1230-
Double thresholdPercent = (double)thresholdValue / 100;
1233+
double thresholdPercent = (double)thresholdValue / 100;
12311234
CounterVO counterVO = _asCounterDao.findById(conditionVO.getCounterid());
12321235
long counter_count = 1;
12331236
do {
@@ -1236,16 +1239,16 @@ private String getAutoscaleAction(HashMap<Long, Double> avgCounter, long groupId
12361239
if (counter_param.equals(counter_source.toString()))
12371240
break;
12381241
counter_count++;
1239-
} while (1 == 1);
1242+
} while (true);
12401243

1241-
Double sum = avgCounter.get(counter_count);
1242-
Double avg = sum / currentVM;
1244+
double sum = avgCounter.get(counter_count);
1245+
double avg = sum / currentVM;
12431246
Operator op = conditionVO.getRelationalOperator();
1244-
boolean bConditionCheck = ((op == com.cloud.network.as.Condition.Operator.EQ) && (thresholdPercent.equals(avg)))
1245-
|| ((op == com.cloud.network.as.Condition.Operator.GE) && (avg.doubleValue() >= thresholdPercent.doubleValue()))
1246-
|| ((op == com.cloud.network.as.Condition.Operator.GT) && (avg.doubleValue() > thresholdPercent.doubleValue()))
1247-
|| ((op == com.cloud.network.as.Condition.Operator.LE) && (avg.doubleValue() <= thresholdPercent.doubleValue()))
1248-
|| ((op == com.cloud.network.as.Condition.Operator.LT) && (avg.doubleValue() < thresholdPercent.doubleValue()));
1247+
boolean bConditionCheck = ((op == com.cloud.network.as.Condition.Operator.EQ) && thresholdPercent == avg)
1248+
|| ((op == com.cloud.network.as.Condition.Operator.GE) && avg >= thresholdPercent)
1249+
|| ((op == com.cloud.network.as.Condition.Operator.GT) && avg > thresholdPercent)
1250+
|| ((op == com.cloud.network.as.Condition.Operator.LE) && avg <= thresholdPercent)
1251+
|| ((op == com.cloud.network.as.Condition.Operator.LT) && avg < thresholdPercent);
12491252

12501253
if (!bConditionCheck) {
12511254
bValid = false;
@@ -1533,7 +1536,7 @@ protected void writeBatches(InfluxDB influxDbConnection, String dbName, List<Poi
15331536
* The considered disk stats are the following: bytes read, bytes write, IO read, and IO write.
15341537
*/
15351538
protected boolean isCurrentVmDiskStatsDifferentFromPrevious(VmDiskStatisticsVO previousVmDiskStats, VmDiskStatisticsVO currentVmDiskStats) {
1536-
if (previousVmDiskStats != null) {
1539+
if (previousVmDiskStats != null && currentVmDiskStats != null) {
15371540
boolean bytesReadDifferentFromPrevious = previousVmDiskStats.getCurrentBytesRead() != currentVmDiskStats.getCurrentBytesRead();
15381541
boolean bytesWriteDifferentFromPrevious = previousVmDiskStats.getCurrentBytesWrite() != currentVmDiskStats.getCurrentBytesWrite();
15391542
boolean ioReadDifferentFromPrevious = previousVmDiskStats.getCurrentIORead() != currentVmDiskStats.getCurrentIORead();

0 commit comments

Comments
 (0)