Skip to content

Commit b9aad0f

Browse files
Prevent overflow on StatsCollector + add a few enhancements on code
1 parent 7d0fd9f commit b9aad0f

2 files changed

Lines changed: 13 additions & 20 deletions

File tree

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ public String toString() {
192192
private static final String DISK_WRITE_IOPS_FIELD = "disk_write_iops";
193193
private static final String DISK_WRITE_KBS_FIELD = "disk_write_kbs";
194194

195+
private static final int HOURLY_TIME = 60;
196+
private static final int DAILY_TIME = HOURLY_TIME * 24;
197+
private static final Long ONE_MINUTE_IN_MILLISCONDS = 60000L;
198+
195199
private static final String DEFAULT_DATABASE_NAME = "cloudstack";
196200
private static final String INFLUXDB_HOST_MEASUREMENT = "host_stats";
197201
private static final String INFLUXDB_VM_MEASUREMENT = "vm_stats";
@@ -328,11 +332,11 @@ public boolean start() {
328332
protected void init(Map<String, String> configs) {
329333
_executor = Executors.newScheduledThreadPool(6, new NamedThreadFactory("StatsCollector"));
330334

331-
hostStatsInterval = NumbersUtil.parseLong(configs.get("host.stats.interval"), 60000L);
332-
hostAndVmStatsInterval = NumbersUtil.parseLong(configs.get("vm.stats.interval"), 60000L);
333-
storageStatsInterval = NumbersUtil.parseLong(configs.get("storage.stats.interval"), 60000L);
334-
volumeStatsInterval = NumbersUtil.parseLong(configs.get("volume.stats.interval"), 600000L);
335-
autoScaleStatsInterval = NumbersUtil.parseLong(configs.get("autoscale.stats.interval"), 60000L);
335+
hostStatsInterval = NumbersUtil.parseLong(configs.get("host.stats.interval"), ONE_MINUTE_IN_MILLISCONDS);
336+
hostAndVmStatsInterval = NumbersUtil.parseLong(configs.get("vm.stats.interval"), ONE_MINUTE_IN_MILLISCONDS);
337+
storageStatsInterval = NumbersUtil.parseLong(configs.get("storage.stats.interval"), ONE_MINUTE_IN_MILLISCONDS);
338+
volumeStatsInterval = NumbersUtil.parseLong(configs.get("volume.stats.interval"), ONE_MINUTE_IN_MILLISCONDS);
339+
autoScaleStatsInterval = NumbersUtil.parseLong(configs.get("autoscale.stats.interval"), ONE_MINUTE_IN_MILLISCONDS);
336340

337341
String statsUri = statsOutputUri.value();
338342
if (StringUtils.isNotBlank(statsUri)) {
@@ -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,9 +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);
459459

460+
long period = _usageAggregationRange * ONE_MINUTE_IN_MILLISCONDS;
461+
_diskStatsUpdateExecutor.scheduleAtFixedRate(new VmDiskStatsUpdaterTask(), (endDate - System.currentTimeMillis()), period, TimeUnit.MILLISECONDS);
460462
}
461463

462464
/**
@@ -1209,7 +1211,7 @@ private String getAutoscaleAction(HashMap<Long, Double> avgCounter, long groupId
12091211
for (AutoScaleVmGroupPolicyMapVO asVmgPmap : listMap) {
12101212
AutoScalePolicyVO policyVO = _asPolicyDao.findById(asVmgPmap.getPolicyId());
12111213
if (policyVO != null) {
1212-
Integer quitetime = policyVO.getQuietTime();
1214+
int quitetime = policyVO.getQuietTime();
12131215
Date quitetimeDate = policyVO.getLastQuiteTime();
12141216
long last_quitetime = 0L;
12151217
if (quitetimeDate != null) {
@@ -1236,7 +1238,7 @@ private String getAutoscaleAction(HashMap<Long, Double> avgCounter, long groupId
12361238
if (counter_param.equals(counter_source.toString()))
12371239
break;
12381240
counter_count++;
1239-
} while (1 == 1);
1241+
} while (true);
12401242

12411243
Double sum = avgCounter.get(counter_count);
12421244
Double avg = sum / currentVM;
@@ -1526,9 +1528,6 @@ protected boolean isCurrentVmDiskStatsDifferentFromPrevious(VmDiskStatisticsVO p
15261528
boolean ioWriteDifferentFromPrevious = previousVmDiskStats.getCurrentIOWrite() != currentVmDiskStats.getCurrentIOWrite();
15271529
return bytesReadDifferentFromPrevious || bytesWriteDifferentFromPrevious || ioReadDifferentFromPrevious || ioWriteDifferentFromPrevious;
15281530
}
1529-
if (currentVmDiskStats == null) {
1530-
return false;
1531-
}
15321531
return true;
15331532
}
15341533

server/src/test/java/com/cloud/server/StatsCollectorTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,6 @@ public void isCurrentVmDiskStatsDifferentFromPreviousTestNull() {
159159
Assert.assertTrue(result);
160160
}
161161

162-
@Test
163-
public void isCurrentVmDiskStatsDifferentFromPreviousTestBothNull() {
164-
boolean result = statsCollector.isCurrentVmDiskStatsDifferentFromPrevious(null, null);
165-
Assert.assertFalse(result);
166-
}
167-
168162
@Test
169163
public void isCurrentVmDiskStatsDifferentFromPreviousTestDifferentIoWrite() {
170164
configureAndTestisCurrentVmDiskStatsDifferentFromPrevious(123l, 123l, 123l, 12l, true);

0 commit comments

Comments
 (0)