hachikuji commented on a change in pull request #11131:
URL: https://github.com/apache/kafka/pull/11131#discussion_r676942838



##########
File path: 
metadata/src/test/java/org/apache/kafka/controller/QuorumControllerMetricsTest.java
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.kafka.controller;
+
+import com.yammer.metrics.core.MetricsRegistry;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class QuorumControllerMetricsTest {
+    @Test
+    public void testKafkaControllerMetricNames() {
+        String expectedGroup = "kafka.controller";
+        String expectedType = "KafkaController";
+        Set<String> expectedMetricNames = new HashSet<>(Arrays.asList(
+            "ActiveControllerCount",
+            "GlobalTopicCount",
+            "GlobalPartitionCount",
+            "OfflinePartitionCount",
+            "PreferredReplicaImbalanceCount"));
+        Set<String> missingMetrics = 
getMissingMetricNames(expectedMetricNames, expectedGroup, expectedType);
+        assertEquals(Collections.emptySet(), missingMetrics, "Expected metrics 
did not exist");
+    }
+
+    @Test
+    public void testControllerEventManagerMetricNames() {
+        String expectedGroup = "kafka.controller";
+        String expectedType = "ControllerEventManager";
+        Set<String> expectedMetricNames = new HashSet<>(Arrays.asList(
+            "EventQueueTimeMs",
+            "EventQueueProcessingTimeMs"));
+        Set<String> missingMetrics = 
getMissingMetricNames(expectedMetricNames, expectedGroup, expectedType);
+        assertEquals(Collections.emptySet(), missingMetrics, "Expected metrics 
did not exist");
+    }
+
+    private static Set<String> getMissingMetricNames(
+        Set<String> expectedMetricNames, String expectedGroup, String 
expectedType) {
+        MetricsRegistry registry = new MetricsRegistry();
+        new QuorumControllerMetrics(registry); // populates the registry
+        Set<String> foundMetricNames = 
expectedMetricNames.stream().filter(expectedMetricName ->
+            registry.allMetrics().keySet().stream().anyMatch(metricName -> {
+                if (metricName.getGroup().equals(expectedGroup) && 
metricName.getType().equals(expectedType)
+                    && metricName.getScope() == null && 
metricName.getName().equals(expectedMetricName)) {
+                    // It has to exist AND the MBean name has to be correct;
+                    // fail right here if the MBean name doesn't match
+                    String expectedMBeanPrefix = expectedGroup + ":type=" + 
expectedType + ",name=";
+                    assertEquals(expectedMBeanPrefix + expectedMetricName, 
metricName.getMBeanName());
+                    return true; // the expected metric name exists and the 
associated MBean name matches
+                } else {
+                    return false; // this one didn't match
+                }
+            })).collect(Collectors.toSet());
+        if (foundMetricNames.size() == expectedMetricNames.size()) {

Review comment:
       nit: is the size check sufficient? Any harm skipping this check and 
following the other branch in all cases?

##########
File path: 
metadata/src/main/java/org/apache/kafka/controller/QuorumControllerMetrics.java
##########
@@ -22,22 +22,21 @@
 import com.yammer.metrics.core.MetricName;
 import com.yammer.metrics.core.MetricsRegistry;
 
-
 public final class QuorumControllerMetrics implements ControllerMetrics {
-    private final static MetricName ACTIVE_CONTROLLER_COUNT = new MetricName(
-        "kafka.controller", "KafkaController", "ActiveControllerCount", null);
-    private final static MetricName EVENT_QUEUE_TIME_MS = new MetricName(
-        "kafka.controller", "ControllerEventManager", "EventQueueTimeMs", 
null);
-    private final static MetricName EVENT_QUEUE_PROCESSING_TIME_MS = new 
MetricName(
-        "kafka.controller", "ControllerEventManager", 
"EventQueueProcessingTimeMs", null);
-    private final static MetricName GLOBAL_TOPIC_COUNT = new MetricName(
-        "kafka.controller", "KafkaController", "GlobalTopicCount", null);
-    private final static MetricName GLOBAL_PARTITION_COUNT = new MetricName(
-        "kafka.controller", "KafkaController", "GlobalPartitionCount", null);
-    private final static MetricName OFFLINE_PARTITION_COUNT = new MetricName(
-        "kafka.controller", "KafkaController", "OfflinePartitionCount", null);
-    private final static MetricName PREFERRED_REPLICA_IMBALANCE_COUNT = new 
MetricName(
-        "kafka.controller", "KafkaController", 
"PreferredReplicaImbalanceCount", null);
+    private final static MetricName ACTIVE_CONTROLLER_COUNT = getMetricName(
+        "kafka.controller", "KafkaController", "ActiveControllerCount");
+    private final static MetricName EVENT_QUEUE_TIME_MS = getMetricName(
+        "kafka.controller", "ControllerEventManager", "EventQueueTimeMs");
+    private final static MetricName EVENT_QUEUE_PROCESSING_TIME_MS = 
getMetricName(
+        "kafka.controller", "ControllerEventManager", 
"EventQueueProcessingTimeMs");
+    private final static MetricName GLOBAL_TOPIC_COUNT = getMetricName(
+        "kafka.controller", "KafkaController", "GlobalTopicCount");
+    private final static MetricName GLOBAL_PARTITION_COUNT = getMetricName(
+        "kafka.controller", "KafkaController", "GlobalPartitionCount");

Review comment:
       nit: since the context is already controller metrics, do we need to pass 
through `kafka.controller` or could this be implicit?

##########
File path: 
metadata/src/test/java/org/apache/kafka/controller/QuorumControllerMetricsTest.java
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.kafka.controller;
+
+import com.yammer.metrics.core.MetricsRegistry;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class QuorumControllerMetricsTest {
+    @Test
+    public void testKafkaControllerMetricNames() {
+        String expectedGroup = "kafka.controller";
+        String expectedType = "KafkaController";
+        Set<String> expectedMetricNames = new HashSet<>(Arrays.asList(

Review comment:
       nit: could use Utils.mkSet

##########
File path: 
metadata/src/main/java/org/apache/kafka/controller/QuorumControllerMetrics.java
##########
@@ -22,22 +22,21 @@
 import com.yammer.metrics.core.MetricName;
 import com.yammer.metrics.core.MetricsRegistry;
 
-
 public final class QuorumControllerMetrics implements ControllerMetrics {
-    private final static MetricName ACTIVE_CONTROLLER_COUNT = new MetricName(
-        "kafka.controller", "KafkaController", "ActiveControllerCount", null);
-    private final static MetricName EVENT_QUEUE_TIME_MS = new MetricName(
-        "kafka.controller", "ControllerEventManager", "EventQueueTimeMs", 
null);
-    private final static MetricName EVENT_QUEUE_PROCESSING_TIME_MS = new 
MetricName(
-        "kafka.controller", "ControllerEventManager", 
"EventQueueProcessingTimeMs", null);
-    private final static MetricName GLOBAL_TOPIC_COUNT = new MetricName(
-        "kafka.controller", "KafkaController", "GlobalTopicCount", null);
-    private final static MetricName GLOBAL_PARTITION_COUNT = new MetricName(
-        "kafka.controller", "KafkaController", "GlobalPartitionCount", null);
-    private final static MetricName OFFLINE_PARTITION_COUNT = new MetricName(
-        "kafka.controller", "KafkaController", "OfflinePartitionCount", null);
-    private final static MetricName PREFERRED_REPLICA_IMBALANCE_COUNT = new 
MetricName(
-        "kafka.controller", "KafkaController", 
"PreferredReplicaImbalanceCount", null);
+    private final static MetricName ACTIVE_CONTROLLER_COUNT = getMetricName(
+        "kafka.controller", "KafkaController", "ActiveControllerCount");

Review comment:
       Can we introduce some constants for the metric names? They could be used 
in the test case as well as de-registration (which is a gap at the moment).




-- 
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]


Reply via email to