Fix bug :Infinity value for CPU or Bandwidth usage (#13609)
* fix bug :Infinity value for CPU or Bandwidth usage
* use executorService.schedule replace to executorService.schedule
* 1.use scheduleAtFixedDelay instead of scheduleAtFixedRate;
2.returns when elapsedSeconds <= 0 is true, skip this round of calculateBrokerHostUsage;
* 1.move the early return conditional block to before getTotalCpuUsage;
* 1.use scheduleWithFixedDelay instead of scheduleAtFixedRate in the class GenericBrokerHostUsageImpl;
* update log content: add a space in log
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/GenericBrokerHostUsageImpl.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/GenericBrokerHostUsageImpl.java
index 72d36ab..1405e8e 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/GenericBrokerHostUsageImpl.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/GenericBrokerHostUsageImpl.java
@@ -54,9 +54,9 @@
this.totalCpuLimit = getTotalCpuLimit();
// Call now to initialize values before the constructor returns
calculateBrokerHostUsage();
- executorService.scheduleAtFixedRate(catchingAndLoggingThrowables(this::checkCpuLoad), CPU_CHECK_MILLIS,
+ executorService.scheduleWithFixedDelay(catchingAndLoggingThrowables(this::checkCpuLoad), CPU_CHECK_MILLIS,
CPU_CHECK_MILLIS, TimeUnit.MILLISECONDS);
- executorService.scheduleAtFixedRate(catchingAndLoggingThrowables(this::doCalculateBrokerHostUsage),
+ executorService.scheduleWithFixedDelay(catchingAndLoggingThrowables(this::doCalculateBrokerHostUsage),
hostUsageCheckIntervalMin,
hostUsageCheckIntervalMin, TimeUnit.MINUTES);
}
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java
index ccbe8e3..5bc7bf9 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java
@@ -87,7 +87,7 @@
// Call now to initialize values before the constructor returns
calculateBrokerHostUsage();
- executorService.scheduleAtFixedRate(catchingAndLoggingThrowables(this::calculateBrokerHostUsage),
+ executorService.scheduleWithFixedDelay(catchingAndLoggingThrowables(this::calculateBrokerHostUsage),
hostUsageCheckIntervalMin,
hostUsageCheckIntervalMin, TimeUnit.MINUTES);
}
@@ -105,9 +105,14 @@
double totalNicUsageRx = getTotalNicUsageRxKb(nics);
double totalCpuLimit = getTotalCpuLimit();
- SystemResourceUsage usage = new SystemResourceUsage();
long now = System.currentTimeMillis();
double elapsedSeconds = (now - lastCollection) / 1000d;
+ if (elapsedSeconds <= 0) {
+ log.warn("elapsedSeconds {} is not expected, skip this round of calculateBrokerHostUsage", elapsedSeconds);
+ return;
+ }
+
+ SystemResourceUsage usage = new SystemResourceUsage();
double cpuUsage = getTotalCpuUsage(elapsedSeconds);
if (lastCollection == 0L) {