Skip to content

Commit 0a73f51

Browse files
SadiJrSadiJr
andauthored
Externalize config to increment or not VM metrics in memory (#5351)
Co-authored-by: SadiJr <[email protected]>
1 parent 9163013 commit 0a73f51

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.apache.commons.collections.CollectionUtils;
5252
import org.apache.commons.collections.MapUtils;
5353
import org.apache.commons.lang.StringUtils;
54+
import org.apache.commons.lang3.BooleanUtils;
5455
import org.apache.log4j.Logger;
5556
import org.influxdb.BatchOptions;
5657
import org.influxdb.InfluxDB;
@@ -221,6 +222,9 @@ public String toString() {
221222
private static final ConfigKey<String> statsOutputUri = new ConfigKey<String>("Advanced", String.class, "stats.output.uri", "",
222223
"URI to send StatsCollector statistics to. The collector is defined on the URI scheme. Example: graphite://graphite-hostaddress:port or influxdb://influxdb-hostaddress/dbname. Note that the port is optional, if not added the default port for the respective collector (graphite or influxdb) will be used. Additionally, the database name '/dbname' is also optional; default db name is 'cloudstack'. You must create and configure the database if using influxdb.",
223224
true);
225+
private static final ConfigKey<Boolean> VM_STATS_INCREMENT_METRICS_IN_MEMORY = new ConfigKey<Boolean>("Advanced", Boolean.class, "vm.stats.increment.metrics.in.memory", "true",
226+
"When set to 'true', VM metrics(NetworkReadKBs, NetworkWriteKBs, DiskWriteKBs, DiskReadKBs, DiskReadIOs and DiskWriteIOs) that are collected from the hypervisor are summed and stored in memory. "
227+
+ "On the other hand, when set to 'false', the VM metrics API will just display the latest metrics collected.", true);
224228

225229
private static StatsCollector s_instance = null;
226230

@@ -1460,11 +1464,12 @@ private void prepareVmMetricsForGraphite(Map<Object, Object> metrics, VmStatsEnt
14601464
private void storeVirtualMachineStatsInMemory(VmStatsEntry statsForCurrentIteration) {
14611465
VmStatsEntry statsInMemory = (VmStatsEntry)_VmStats.get(statsForCurrentIteration.getVmId());
14621466

1463-
if (statsInMemory == null) {
1464-
//no stats exist for this vm, directly persist
1467+
boolean vmStatsIncrementMetrics = BooleanUtils.toBoolean(VM_STATS_INCREMENT_METRICS_IN_MEMORY.value());
1468+
if (statsInMemory == null || !vmStatsIncrementMetrics) {
14651469
_VmStats.put(statsForCurrentIteration.getVmId(), statsForCurrentIteration);
14661470
} else {
1467-
//update each field
1471+
s_logger.debug(String.format("Increment saved values of NetworkReadKBs, NetworkWriteKBs, DiskWriteKBs, DiskReadKBs, DiskReadIOs, DiskWriteIOs, with current metrics for VM with ID [%s]. "
1472+
+ "To change this process, check value of 'vm.stats.increment.metrics.in.memory' configuration.", statsForCurrentIteration.getVmId()));
14681473
statsInMemory.setCPUUtilization(statsForCurrentIteration.getCPUUtilization());
14691474
statsInMemory.setNumCPUs(statsForCurrentIteration.getNumCPUs());
14701475
statsInMemory.setNetworkReadKBs(statsInMemory.getNetworkReadKBs() + statsForCurrentIteration.getNetworkReadKBs());
@@ -1625,7 +1630,7 @@ public String getConfigComponentName() {
16251630

16261631
@Override
16271632
public ConfigKey<?>[] getConfigKeys() {
1628-
return new ConfigKey<?>[] {vmDiskStatsInterval, vmDiskStatsIntervalMin, vmNetworkStatsInterval, vmNetworkStatsIntervalMin, StatsTimeout, statsOutputUri};
1633+
return new ConfigKey<?>[] {vmDiskStatsInterval, vmDiskStatsIntervalMin, vmNetworkStatsInterval, vmNetworkStatsIntervalMin, StatsTimeout, statsOutputUri, VM_STATS_INCREMENT_METRICS_IN_MEMORY};
16291634
}
16301635

16311636
public double getImageStoreCapacityThreshold() {

0 commit comments

Comments
 (0)