From 3b695dc67632ec4ee40ed06ddc5b6d5c57ced6af Mon Sep 17 00:00:00 2001 From: "wangjiahua.wjh" Date: Mon, 15 Jun 2026 11:45:45 +0800 Subject: [PATCH] [ISSUE #10515] Fix TransactionMetricsFlushService busy spin bug --- .../transaction/TransactionMetricsFlushService.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/broker/src/main/java/org/apache/rocketmq/broker/transaction/TransactionMetricsFlushService.java b/broker/src/main/java/org/apache/rocketmq/broker/transaction/TransactionMetricsFlushService.java index 948f9fbc8ed..369828e79e1 100644 --- a/broker/src/main/java/org/apache/rocketmq/broker/transaction/TransactionMetricsFlushService.java +++ b/broker/src/main/java/org/apache/rocketmq/broker/transaction/TransactionMetricsFlushService.java @@ -41,10 +41,15 @@ public void run() { long start = System.currentTimeMillis(); while (!this.isStopped()) { try { - if (System.currentTimeMillis() - start > brokerController.getBrokerConfig().getTransactionMetricFlushInterval()) { + // Bug fix: original code only called waitForRunning inside the if-branch, + // so on every iteration where the interval hadn't elapsed yet the loop + // spun without yielding (~170 CPU samples in JFR on idle broker). + // Now we always wait, then check whether enough time has passed to persist. + long interval = brokerController.getBrokerConfig().getTransactionMetricFlushInterval(); + this.waitForRunning(interval); + if (System.currentTimeMillis() - start > interval) { start = System.currentTimeMillis(); brokerController.getTransactionalMessageService().getTransactionMetrics().persist(); - waitForRunning(brokerController.getBrokerConfig().getTransactionMetricFlushInterval()); } } catch (Throwable e) { log.error("Error occurred in " + getServiceName(), e);