ivandika3 commented on code in PR #10941:
URL: https://github.com/apache/ozone/pull/10941#discussion_r4333653201
##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/server/http/TestHttpServer2Metrics.java:
##########
@@ -88,4 +97,37 @@ public void testMetrics() {
verify(recorder).addGauge(HttpServerThreadQueueWaitingTaskCount,
threadQueueWaitingTaskCount);
}
+
+ @Test
+ void testUnregisterWithMultipleMetricsSourceIds() throws Exception {
+ MetricsSystem metricsSystem =
+ DefaultMetricsSystem.initialize("TestHttpServer2Metrics");
+ Field field = metricsSystem.getClass().getDeclaredField("allSources");
Review Comment:
Thanks, updated.
##########
hadoop-ozone/mini-cluster/src/main/java/org/apache/hadoop/ozone/MetricsLeakAssertion.java:
##########
@@ -0,0 +1,241 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.ozone;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import org.apache.hadoop.metrics2.MetricsSource;
+import org.apache.hadoop.metrics2.MetricsSystem;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+
+/**
+ * Asserts that no metrics sources remain registered in the
+ * {@link DefaultMetricsSystem} after a mini cluster is shut down.
+ *
+ * <p>Hadoop's {@code MetricsSystemImpl} does not remove registered sources on
+ * {@code stop()} or {@code shutdown()}; a metrics class that forgets to call
+ * {@code unregisterSource(...)} leaks its registration silently. This helper
+ * inspects the private {@code allSources} map via reflection and fails the
+ * test if anything is still registered.
+ */
+public final class MetricsLeakAssertion {
+
+ private static final String ALL_SOURCES_FIELD = "allSources";
+
+ /**
+ * Metrics sources that are registered once per JVM (or once per service)
+ * and intentionally never unregistered, so they are expected to still be
+ * present after a mini cluster shuts down. These are JVM-level singletons
+ * with no per-service owner; do not add per-service metrics here.
+ *
+ * <p>For the matching rules see {@link #isExpectedLeftover(String)}.
+ */
+ private static final List<String> EXPECTED_LEFTOVER_SOURCES = Arrays.asList(
+ "JvmMetrics*", // per service, via HddsServerUtil.initializeMetrics
+ "JvmMetricsCpu",
Review Comment:
Thanks, updated.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]