This is an automated email from the ASF dual-hosted git repository.

lhotari pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 42e02a866df49147876a21996d45abaf4f2a7c42
Author: Vinkal <[email protected]>
AuthorDate: Mon Oct 27 14:37:56 2025 +0530

    [fix][test] 
BacklogQuotaManagerTest.backlogsAgeMetricsNoPreciseWithoutBacklogQuota handle 
empty /metrics scrape (#24887)
    
    Signed-off-by: Vinkal Chudgar <[email protected]>
    (cherry picked from commit ba8468eb5c5b686249aa1e39d2c8b6a1aacbea88)
---
 .../broker/service/BacklogQuotaManagerTest.java    | 45 ++++++++++++++++++----
 1 file changed, 37 insertions(+), 8 deletions(-)

diff --git 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BacklogQuotaManagerTest.java
 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BacklogQuotaManagerTest.java
index 46d06a71228..3c8dcfdc12f 100644
--- 
a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BacklogQuotaManagerTest.java
+++ 
b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BacklogQuotaManagerTest.java
@@ -95,6 +95,7 @@ public class BacklogQuotaManagerTest {
     private static final Logger log = 
LoggerFactory.getLogger(BacklogQuotaManagerTest.class);
 
     public static final String CLUSTER_NAME = "usc";
+    private static final String QUOTA_CHECK_COUNT = 
"pulsar_storage_backlog_quota_check_duration_seconds_count";
     PulsarService pulsar;
     ServiceConfiguration config;
 
@@ -934,11 +935,31 @@ public class BacklogQuotaManagerTest {
     }
 
     private void waitForQuotaCheckToRunTwice() {
-        final long initialQuotaCheckCount = getQuotaCheckCount();
+        final long[] baselineCount = new long[1];
+        final boolean[] baselineCaptured = new boolean[1];
+
         Awaitility.await()
                 .pollInterval(1, SECONDS)
                 .atMost(TIME_TO_CHECK_BACKLOG_QUOTA * 3, SECONDS)
-                .until(() -> getQuotaCheckCount() > initialQuotaCheckCount + 
1);
+                .until(() -> {
+                    final java.util.OptionalLong countOpt = 
getQuotaCheckCount();
+
+                    // If /metrics is not returning the metric yet, keep 
waiting.
+                    // Don't take the baseline until a successful scrape shows 
the metric.
+                    if (countOpt.isEmpty()) {
+                        return false;
+                    }
+
+                    // First successful scrape: capture baseline, then ask for 
two more checks.
+                    final long observedCount = countOpt.getAsLong();
+                    if (!baselineCaptured[0]) {
+                        baselineCount[0] = observedCount;
+                        baselineCaptured[0] = true;
+                        return false;
+                    }
+
+                    return observedCount > baselineCount[0] + 1;
+                });
     }
 
     /**
@@ -952,12 +973,20 @@ public class BacklogQuotaManagerTest {
             markDeletePosition -> markDeletePosition != null && 
!markDeletePosition.equals(previousMarkDeletePosition));
     }
 
-    private long getQuotaCheckCount() {
-        Metrics metrics = prometheusMetricsClient.getMetrics();
-        return (long) metrics.findByNameAndLabels(
-                        
"pulsar_storage_backlog_quota_check_duration_seconds_count",
-                        "cluster", CLUSTER_NAME)
-                .get(0).value;
+    private java.util.OptionalLong getQuotaCheckCount() {
+        try {
+            final Metrics metrics = prometheusMetricsClient.getMetrics();
+            final java.util.List<Metric> matches =
+                    metrics.findByNameAndLabels(QUOTA_CHECK_COUNT, "cluster", 
CLUSTER_NAME);
+            if (matches.isEmpty()) {
+                // No metric sample for this name and labels in this scrape.
+                return java.util.OptionalLong.empty();
+            }
+            return java.util.OptionalLong.of((long) matches.get(0).value);
+        } catch (Exception e) {
+            // Scrape failed or the metrics client threw, treat as not 
available now.
+            return java.util.OptionalLong.empty();
+        }
     }
 
     /**

Reply via email to