chia7712 commented on code in PR #20021:
URL: https://github.com/apache/kafka/pull/20021#discussion_r2162045295


##########
server/src/main/java/org/apache/kafka/server/metrics/NodeMetrics.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.server.metrics;
+
+import org.apache.kafka.common.MetricName;
+import org.apache.kafka.common.metrics.Metrics;
+import org.apache.kafka.controller.QuorumFeatures;
+import org.apache.kafka.metadata.VersionRange;
+import org.apache.kafka.server.common.Feature;
+import org.apache.kafka.server.common.MetadataVersion;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+public final class NodeMetrics implements AutoCloseable {
+    private static final String METRIC_GROUP_NAME = "node-metrics";
+    private static final String FEATURE_NAME_TAG = "feature-name";
+    private static final String MAXIMUM_SUPPORTED_LEVEL_NAME = 
"maximum-supported-level";
+    private static final String MINIMUM_SUPPORTED_LEVEL_NAME = 
"minimum-supported-level";
+
+    private final Metrics metrics;
+    private final Map<String, VersionRange> supportedFeatureRanges;
+
+    public NodeMetrics(Metrics metrics, boolean enableUnstableVersions) {
+        this.metrics = metrics;
+        this.supportedFeatureRanges = 
QuorumFeatures.defaultSupportedFeatureMap(enableUnstableVersions);
+        for (var featureName : Feature.PRODUCTION_FEATURE_NAMES) {
+            addSupportedLevelMetric(MAXIMUM_SUPPORTED_LEVEL_NAME, featureName);
+            addSupportedLevelMetric(MINIMUM_SUPPORTED_LEVEL_NAME, featureName);
+        }
+        addSupportedLevelMetric(
+            MAXIMUM_SUPPORTED_LEVEL_NAME,
+            MetadataVersion.FEATURE_NAME
+        );
+        addSupportedLevelMetric(
+            MINIMUM_SUPPORTED_LEVEL_NAME,
+            MetadataVersion.FEATURE_NAME
+        );
+    }
+
+    private void addSupportedLevelMetric(String metricName, String 
featureName) {
+        metrics.addMetric(
+            getFeatureNameTagMetricName(
+                metricName,
+                METRIC_GROUP_NAME,
+                featureName
+            ),
+            (config, now) -> {
+                if (metricName.equals(MAXIMUM_SUPPORTED_LEVEL_NAME)) {
+                    return supportedFeatureRanges.get(featureName).max();
+                } else {
+                    return supportedFeatureRanges.get(featureName).min();
+                }
+            }
+        );
+    }
+
+    @Override
+    public void close() {
+        for (var featureName : supportedFeatureRanges.keySet()) {
+            metrics.removeMetric(
+                getFeatureNameTagMetricName(
+                    MAXIMUM_SUPPORTED_LEVEL_NAME,
+                    METRIC_GROUP_NAME,
+                    featureName
+                )
+            );
+            metrics.removeMetric(
+                getFeatureNameTagMetricName(
+                    MINIMUM_SUPPORTED_LEVEL_NAME,
+                    METRIC_GROUP_NAME,
+                    featureName
+                )
+            );
+        }
+    }
+
+    private MetricName getFeatureNameTagMetricName(String name, String group, 
String featureName) {
+        LinkedHashMap<String, String> featureNameTag = new LinkedHashMap<>();
+        featureNameTag.put(FEATURE_NAME_TAG, featureName.replace(".", "-"));

Review Comment:
   it contains only one element, so it should be fine to use `Map.of`



##########
server/src/main/java/org/apache/kafka/server/metrics/NodeMetrics.java:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.server.metrics;
+
+import org.apache.kafka.common.MetricName;
+import org.apache.kafka.common.metrics.Metrics;
+import org.apache.kafka.controller.QuorumFeatures;
+import org.apache.kafka.metadata.VersionRange;
+import org.apache.kafka.server.common.Feature;
+import org.apache.kafka.server.common.MetadataVersion;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+public final class NodeMetrics implements AutoCloseable {
+    private static final String METRIC_GROUP_NAME = "node-metrics";
+    private static final String FEATURE_NAME_TAG = "feature-name";
+    private static final String MAXIMUM_SUPPORTED_LEVEL_NAME = 
"maximum-supported-level";
+    private static final String MINIMUM_SUPPORTED_LEVEL_NAME = 
"minimum-supported-level";
+
+    private final Metrics metrics;
+    private final Map<String, VersionRange> supportedFeatureRanges;
+
+    public NodeMetrics(Metrics metrics, boolean enableUnstableVersions) {
+        this.metrics = metrics;
+        this.supportedFeatureRanges = 
QuorumFeatures.defaultSupportedFeatureMap(enableUnstableVersions);

Review Comment:
   `QuorumFeatures.defaultSupportedFeatureMap` exclude the feature if the 
`enableUnstableVersions` is false and production version is zero. Hence, should 
we iterate `supportedFeatureRanges` instead of 
`Feature.PRODUCTION_FEATURE_NAMES` to avoid a possible NPE?



##########
metadata/src/main/java/org/apache/kafka/image/loader/metrics/MetadataLoaderMetrics.java:
##########
@@ -142,16 +185,44 @@ public long handleLoadSnapshotCount() {
         return this.handleLoadSnapshotCount.get();
     }
 
+    public void setFinalizedFeatureLevel(String featureName, short 
featureLevel) {
+        finalizedFeatureLevels.put(featureName, featureLevel);
+    }
+
+    public short finalizedFeatureLevel(String featureName) {

Review Comment:
   this could be private method



##########
metadata/src/main/java/org/apache/kafka/image/loader/metrics/MetadataLoaderMetrics.java:
##########
@@ -142,16 +185,44 @@ public long handleLoadSnapshotCount() {
         return this.handleLoadSnapshotCount.get();
     }
 
+    public void setFinalizedFeatureLevel(String featureName, short 
featureLevel) {
+        finalizedFeatureLevels.put(featureName, featureLevel);
+    }
+
+    public short finalizedFeatureLevel(String featureName) {
+        return finalizedFeatureLevels.getOrDefault(featureName, (short) 0);
+    }
+
     @Override
     public void close() {
         registry.ifPresent(r -> List.of(
             CURRENT_METADATA_VERSION,
             CURRENT_CONTROLLER_ID,
             HANDLE_LOAD_SNAPSHOT_COUNT
         ).forEach(r::removeMetric));
+        for (var featureName : finalizedFeatureLevels.keySet()) {
+            removeFinalizedFeatureLevelMetric(featureName);
+        }
     }
 
     private static MetricName getMetricName(String type, String name) {
         return KafkaYammerMetrics.getMetricName("kafka.server", type, name);
     }
+
+    private static MetricName getFeatureNameTagMetricName(String type, String 
name, String featureName) {
+        LinkedHashMap<String, String> featureNameTag = new LinkedHashMap<>();
+        featureNameTag.put(FEATURE_NAME_TAG, sanitizeFeatureName(featureName));
+        return KafkaYammerMetrics.getMetricName("kafka.server", type, name, 
featureNameTag);
+    }
+    
+    private static String sanitizeFeatureName(String featureName) {

Review Comment:
   Please consider adding comments to remind readers that the naming style is 
different to `NodeMetrics`, and this is expected.



##########
metadata/src/main/java/org/apache/kafka/image/loader/metrics/MetadataLoaderMetrics.java:
##########
@@ -142,16 +185,44 @@ public long handleLoadSnapshotCount() {
         return this.handleLoadSnapshotCount.get();
     }
 
+    public void setFinalizedFeatureLevel(String featureName, short 
featureLevel) {
+        finalizedFeatureLevels.put(featureName, featureLevel);
+    }
+
+    public short finalizedFeatureLevel(String featureName) {
+        return finalizedFeatureLevels.getOrDefault(featureName, (short) 0);
+    }
+
     @Override
     public void close() {
         registry.ifPresent(r -> List.of(
             CURRENT_METADATA_VERSION,
             CURRENT_CONTROLLER_ID,
             HANDLE_LOAD_SNAPSHOT_COUNT
         ).forEach(r::removeMetric));
+        for (var featureName : finalizedFeatureLevels.keySet()) {
+            removeFinalizedFeatureLevelMetric(featureName);
+        }
     }
 
     private static MetricName getMetricName(String type, String name) {
         return KafkaYammerMetrics.getMetricName("kafka.server", type, name);
     }
+
+    private static MetricName getFeatureNameTagMetricName(String type, String 
name, String featureName) {
+        LinkedHashMap<String, String> featureNameTag = new LinkedHashMap<>();
+        featureNameTag.put(FEATURE_NAME_TAG, sanitizeFeatureName(featureName));

Review Comment:
   ditto



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to