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

pan3793 pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/hadoop.git


The following commit(s) were added to refs/heads/trunk by this push:
     new dc99f88d076 HADOOP-19920. Fix flaky test TestPrometheusMetricsSink 
(#8549)
dc99f88d076 is described below

commit dc99f88d076346e5a7d959131640040adf7c3837
Author: Cheng Pan <[email protected]>
AuthorDate: Tue Jun 16 19:32:57 2026 +0800

    HADOOP-19920. Fix flaky test TestPrometheusMetricsSink (#8549)
    
    Reviewed-by: Aaron Fabbri <[email protected]>
    Reviewed-by: Shilun Fan <[email protected]>
    Signed-off-by: Cheng Pan <[email protected]>
---
 .../metrics2/sink/TestPrometheusMetricsSink.java   | 77 +++++++++++-----------
 1 file changed, 40 insertions(+), 37 deletions(-)

diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/TestPrometheusMetricsSink.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/TestPrometheusMetricsSink.java
index 5abdff6c142..90e612001ff 100644
--- 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/TestPrometheusMetricsSink.java
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/TestPrometheusMetricsSink.java
@@ -35,7 +35,11 @@
 import org.apache.hadoop.metrics2.lib.Interns;
 import org.apache.hadoop.metrics2.lib.MutableCounterLong;
 
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.assertj.core.api.Assertions.assertThat;
@@ -48,12 +52,37 @@
  */
 public class TestPrometheusMetricsSink {
 
+  private static final Logger LOG =
+      LoggerFactory.getLogger(TestPrometheusMetricsSink.class);
+
+  /**
+   * Shared {@code DefaultMetricsSystem} singleton, set up fresh per test.
+   * Each test does exactly one balanced {@code init}/{@code shutdown} so the
+   * system (and the JVM-global source-name registry it owns) is fully reset
+   * between tests. This keeps tests isolated even when an assertion fails
+   * before the test body finishes, avoiding flaky failures such as
+   * "Metrics source TestMetrics already exists!" on later tests or reruns.
+   */
+  private MetricsSystem metrics;
+
+  @BeforeEach
+  public void setUp() {
+    metrics = DefaultMetricsSystem.instance();
+    metrics.init("test");
+  }
+
+  @AfterEach
+  public void tearDown() {
+    // DefaultMetricsSystem.shutdown() shuts down the underlying metrics system
+    // and, once its refCount reaches 0, clears the JVM-global source-name and
+    // MBean-name registries. Running it in @AfterEach guarantees cleanup even
+    // when a test assertion fails before reaching the end of the test body.
+    DefaultMetricsSystem.shutdown();
+  }
+
   @Test
   public void testPublish() throws IOException {
     //GIVEN
-    MetricsSystem metrics = DefaultMetricsSystem.instance();
-
-    metrics.init("test");
     PrometheusMetricsSink sink = new PrometheusMetricsSink();
     metrics.register("Prometheus", "Prometheus", sink);
     TestMetrics testMetrics = metrics
@@ -69,14 +98,10 @@ public void testPublish() throws IOException {
     writer.flush();
 
     //THEN
-    String writtenMetrics = stream.toString(UTF_8.name());
-    System.out.println(writtenMetrics);
+    String writtenMetrics = stream.toString(UTF_8);
+    LOG.debug(writtenMetrics);
     
assertTrue(writtenMetrics.contains("test_metrics_num_bucket_create_fails{context=\"dfs\""),
         "The expected metric line is missing from prometheus metrics output");
-
-    metrics.unregisterSource("TestMetrics");
-    metrics.stop();
-    metrics.shutdown();
   }
 
   /**
@@ -86,9 +111,6 @@ public void testPublish() throws IOException {
   @Test
   public void testPublishMultiple() throws IOException {
     //GIVEN
-    MetricsSystem metrics = DefaultMetricsSystem.instance();
-
-    metrics.init("test");
     PrometheusMetricsSink sink = new PrometheusMetricsSink();
     metrics.register("Prometheus", "Prometheus", sink);
     TestMetrics testMetrics1 = metrics
@@ -107,19 +129,14 @@ public void testPublishMultiple() throws IOException {
     writer.flush();
 
     //THEN
-    String writtenMetrics = stream.toString(UTF_8.name());
-    System.out.println(writtenMetrics);
+    String writtenMetrics = stream.toString(UTF_8);
+    LOG.debug(writtenMetrics);
     assertTrue(writtenMetrics.contains(
         
"test_metrics_num_bucket_create_fails{context=\"dfs\",testtag=\"testTagValue1\""),
         "The expected first metric line is missing from prometheus metrics 
output");
     assertTrue(writtenMetrics.contains(
         
"test_metrics_num_bucket_create_fails{context=\"dfs\",testtag=\"testTagValue2\""),
         "The expected second metric line is missing from prometheus metrics 
output");
-
-    metrics.unregisterSource("TestMetrics1");
-    metrics.unregisterSource("TestMetrics2");
-    metrics.stop();
-    metrics.shutdown();
   }
 
   /**
@@ -128,9 +145,6 @@ public void testPublishMultiple() throws IOException {
   @Test
   public void testPublishFlush() throws IOException {
     //GIVEN
-    MetricsSystem metrics = DefaultMetricsSystem.instance();
-
-    metrics.init("test");
     PrometheusMetricsSink sink = new PrometheusMetricsSink();
     metrics.register("Prometheus", "Prometheus", sink);
     TestMetrics testMetrics = metrics
@@ -154,18 +168,14 @@ public void testPublishFlush() throws IOException {
     writer.flush();
 
     //THEN
-    String writtenMetrics = stream.toString(UTF_8.name());
-    System.out.println(writtenMetrics);
+    String writtenMetrics = stream.toString(UTF_8);
+    LOG.debug(writtenMetrics);
     assertFalse(writtenMetrics.contains(
         
"test_metrics_num_bucket_create_fails{context=\"dfs\",testtag=\"testTagValue1\""),
         "The first metric should not exist after flushing");
     assertTrue(writtenMetrics.contains(
         
"test_metrics_num_bucket_create_fails{context=\"dfs\",testtag=\"testTagValue2\""),
         "The expected metric line is missing from prometheus metrics output");
-
-    metrics.unregisterSource("TestMetrics");
-    metrics.stop();
-    metrics.shutdown();
   }
 
   @Test
@@ -223,10 +233,6 @@ public void testNamingWhitespaces() {
    */
   @Test
   public void testTopMetricsPublish() throws IOException {
-    MetricsSystem metrics = DefaultMetricsSystem.instance();
-
-    metrics.init("test");
-
     //GIVEN
     PrometheusMetricsSink sink = new PrometheusMetricsSink();
 
@@ -248,8 +254,8 @@ public void testTopMetricsPublish() throws IOException {
     writer.flush();
 
     //THEN
-    String writtenMetrics = stream.toString(UTF_8.name());
-    System.out.println(writtenMetrics);
+    String writtenMetrics = stream.toString(UTF_8);
+    LOG.debug(writtenMetrics);
 
     assertThat(writtenMetrics)
         .contains(
@@ -260,9 +266,6 @@ public void testTopMetricsPublish() throws IOException {
             "nn_top_user_op_counts_window_ms_1500000_count{")
         .contains(
             "op=\"rename\",user=\"hadoop/[email protected]\"");
-
-    metrics.stop();
-    metrics.shutdown();
   }
 
   /**


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to