jolshan commented on code in PR #14387:
URL: https://github.com/apache/kafka/pull/14387#discussion_r1384116373


##########
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/metrics/GroupCoordinatorMetricsShard.java:
##########
@@ -0,0 +1,325 @@
+/*
+ * 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.coordinator.group.metrics;
+
+import com.yammer.metrics.core.MetricName;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.metrics.Sensor;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.coordinator.group.consumer.ConsumerGroup;
+import org.apache.kafka.coordinator.group.generic.GenericGroupState;
+import org.apache.kafka.timeline.SnapshotRegistry;
+import org.apache.kafka.timeline.TimelineLong;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicLong;
+
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_CONSUMER_GROUPS;
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_CONSUMER_GROUPS_ASSIGNING;
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_CONSUMER_GROUPS_DEAD;
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_CONSUMER_GROUPS_EMPTY;
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_CONSUMER_GROUPS_RECONCILING;
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_CONSUMER_GROUPS_STABLE;
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_GENERIC_GROUPS;
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_GENERIC_GROUPS_COMPLETING_REBALANCE;
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_GENERIC_GROUPS_DEAD;
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_GENERIC_GROUPS_EMPTY;
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_GENERIC_GROUPS_PREPARING_REBALANCE;
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_GENERIC_GROUPS_STABLE;
+import static 
org.apache.kafka.coordinator.group.metrics.GroupCoordinatorMetrics.NUM_OFFSETS;
+
+/**
+ * This class is mapped to a single {@link 
org.apache.kafka.coordinator.group.GroupCoordinatorShard}. It will
+ * record all metrics that the shard handles with respect to {@link 
org.apache.kafka.coordinator.group.OffsetMetadataManager}
+ * and {@link org.apache.kafka.coordinator.group.GroupMetadataManager} 
operations.
+ *
+ * Local gauges will be recorded in this class which will be gathered by 
{@link GroupCoordinatorMetrics} to
+ * report.
+ */
+public class GroupCoordinatorMetricsShard implements CoordinatorMetricsShard {
+
+    /**
+     * This class represents a gauge counter for this shard. The TimelineLong 
object represents a gauge backed by
+     * the snapshot registry. Once we commit to a certain offset in the 
snapshot registry, we write the given
+     * TimelineLong's value to the AtomicLong. This AtomicLong represents the 
actual gauge counter that is queried
+     * when reporting the value to {@link GroupCoordinatorMetrics}.
+     */
+    private static class TimelineGaugeCounter {
+
+        final TimelineLong timelineLong;
+
+        final AtomicLong atomicLong;
+
+        public TimelineGaugeCounter(TimelineLong timelineLong, AtomicLong 
atomicLong) {
+            this.timelineLong = timelineLong;
+            this.atomicLong = atomicLong;
+        }
+    }
+
+    /**
+     * Local timeline gauge counters keyed by the metric name.
+     */
+    private final Map<String, TimelineGaugeCounter> localGauges;
+
+    /**
+     * All sensors keyed by the sensor name. A Sensor object is shared across 
all metrics shards.
+     */
+    private final Map<String, Sensor> globalSensors;
+
+    /**
+     * Global gauge counters keyed by the metric name. The same counter is 
shared across all metrics shards.
+     */
+    private final Map<String, AtomicLong> globalGauges;
+
+    /**
+     * The topic partition.
+     */
+    private final TopicPartition topicPartition;
+
+    public GroupCoordinatorMetricsShard(
+        SnapshotRegistry snapshotRegistry,
+        Map<String, Sensor> globalSensors,
+        Map<String, AtomicLong> globalGauges,
+        TopicPartition topicPartition
+    ) {
+        Objects.requireNonNull(snapshotRegistry);
+        TimelineLong numOffsetsTimeline = new TimelineLong(snapshotRegistry);
+        TimelineLong numGenericGroupsTimeline = new 
TimelineLong(snapshotRegistry);
+        TimelineLong numConsumerGroupsTimeline = new 
TimelineLong(snapshotRegistry);
+        TimelineLong numConsumerGroupsEmptyTimeline = new 
TimelineLong(snapshotRegistry);
+        TimelineLong numConsumerGroupsAssigningTimeline = new 
TimelineLong(snapshotRegistry);
+        TimelineLong numConsumerGroupsReconcilingTimeline = new 
TimelineLong(snapshotRegistry);
+        TimelineLong numConsumerGroupsStableTimeline = new 
TimelineLong(snapshotRegistry);
+        TimelineLong numConsumerGroupsDeadTimeline = new 
TimelineLong(snapshotRegistry);
+
+        this.localGauges = Collections.unmodifiableMap(Utils.mkMap(
+            Utils.mkEntry(NUM_OFFSETS.getName(),
+                new TimelineGaugeCounter(numOffsetsTimeline, new 
AtomicLong(0))),
+            Utils.mkEntry(NUM_GENERIC_GROUPS.getName(),
+                new TimelineGaugeCounter(numGenericGroupsTimeline, new 
AtomicLong(0))),
+            Utils.mkEntry(NUM_CONSUMER_GROUPS.getName(),
+                new TimelineGaugeCounter(numConsumerGroupsTimeline, new 
AtomicLong(0))),
+            Utils.mkEntry(NUM_CONSUMER_GROUPS_EMPTY.getName(),
+                new TimelineGaugeCounter(numConsumerGroupsEmptyTimeline, new 
AtomicLong(0))),
+            Utils.mkEntry(NUM_CONSUMER_GROUPS_ASSIGNING.getName(),
+                new TimelineGaugeCounter(numConsumerGroupsAssigningTimeline, 
new AtomicLong(0))),
+            Utils.mkEntry(NUM_CONSUMER_GROUPS_RECONCILING.getName(),
+                new TimelineGaugeCounter(numConsumerGroupsReconcilingTimeline, 
new AtomicLong(0))),
+            Utils.mkEntry(NUM_CONSUMER_GROUPS_STABLE.getName(),
+                new TimelineGaugeCounter(numConsumerGroupsStableTimeline, new 
AtomicLong(0))),
+            Utils.mkEntry(NUM_CONSUMER_GROUPS_DEAD.getName(),
+                new TimelineGaugeCounter(numConsumerGroupsDeadTimeline, new 
AtomicLong(0)))
+        ));
+
+        this.globalSensors = Objects.requireNonNull(globalSensors);
+        this.globalGauges = Objects.requireNonNull(globalGauges);
+        this.topicPartition = Objects.requireNonNull(topicPartition);
+    }
+
+    @Override
+    public void incrementGlobalGauge(MetricName metricName) {
+        AtomicLong gaugeCounter = globalGauges.get(metricName.getName());
+        if (gaugeCounter != null) {

Review Comment:
   Yeah. Not sure if it is needed, but I guess I'm just used to seeing logging 
or something when we do null checks. Maybe it's not worth it (or should be very 
high level logging)



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