This is an automated email from the ASF dual-hosted git repository. penghui pushed a commit to branch branch-2.8 in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit 2e808b4d53edeea363ccca6a8a149a62c46ed813 Author: chenlin <[email protected]> AuthorDate: Wed Jan 12 11:10:52 2022 +0800 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 (cherry picked from commit 2a7515f9593a76b294bfe2835621a0ab8a904957) --- .../broker/loadbalance/impl/GenericBrokerHostUsageImpl.java | 4 ++-- .../pulsar/broker/loadbalance/impl/LinuxBrokerHostUsageImpl.java | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) 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 @@ public class GenericBrokerHostUsageImpl implements BrokerHostUsage { 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 @@ public class LinuxBrokerHostUsageImpl implements BrokerHostUsage { // 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 @@ public class LinuxBrokerHostUsageImpl implements BrokerHostUsage { 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) {
