tsreaper commented on code in PR #1620:
URL: https://github.com/apache/incubator-paimon/pull/1620#discussion_r1271662115


##########
paimon-core/src/main/java/org/apache/paimon/metrics/AbstractMetricGroup.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.paimon.metrics;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Contains key functionality for adding metrics and carries metrics.
+ *
+ * <p>A MetricGroup can be {@link #close() closed}. Upon closing, the group 
de-register all metrics.
+ *
+ * <p>The {@link #close()} method and {@link #addMetric(String, Metric)} 
method should never be
+ * invoked by multiple threads at the same time, {@link #addMetric(String, 
Metric)} and {@link
+ * #getMetrics()} have multi-threads problems, like the reporter is reading 
the metrics map and the
+ * group is adding metrics to the map at the same time.
+ */
+public abstract class AbstractMetricGroup implements MetricGroup {
+    protected static final Logger LOG = 
LoggerFactory.getLogger(MetricGroup.class);
+
+    // ------------------------------------------------------------------------
+
+    /** The map containing all tags and their associated values, lazily 
computed. */
+    protected Map<String, String> tags;
+
+    /** Flag indicating whether this group has been closed. */
+    private boolean closed = false;
+
+    private final ConcurrentHashMap<String, Metric> metrics = new 
ConcurrentHashMap<>();
+
+    // ------------------------------------------------------------------------
+
+    public AbstractMetricGroup(@Nullable Map<String, String> tags) {
+        this.tags = tags;
+        Metrics.getInstance().addGroup(this);
+    }
+
+    @Override
+    public Map<String, String> getAllTags() {
+        return tags;
+    }
+
+    /**
+     * Returns the fully qualified metric name using the configured delimiter 
for the reporter with
+     * the given index, for example {@code "myTable.bucket-1.metricName"}.
+     *
+     * @param metricName metric name
+     * @param delimiter delimiter to use
+     * @return fully qualified metric name
+     */
+    public String getMetricIdentifier(String metricName, String delimiter) {
+        return String.join(delimiter, getGroupName(), metricName);
+    }
+
+    /**
+     * Creates and registers a new {@link org.apache.paimon.metrics.Counter} 
or return the existing
+     * {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @return the created or existing counter
+     */
+    public Counter counter(String name) {
+        return counter(name, new SimpleCounter());
+    }
+
+    /**
+     * Registers a {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @param counter counter to register
+     * @param <C> counter type
+     * @return the given counter
+     */
+    public <C extends Counter> C counter(String name, C counter) {
+        Metric metric = addMetric(name, counter);
+        return (C) metric;
+    }
+
+    /**
+     * Registers a new {@link org.apache.paimon.metrics.Gauge}.
+     *
+     * @param name name of the gauge
+     * @param gauge gauge to register
+     * @param <T> return type of the gauge
+     * @return the given gauge
+     */
+    public <T, G extends Gauge<T>> G gauge(String name, G gauge) {
+        Metric metric = addMetric(name, gauge);
+        return (G) metric;

Review Comment:
   ```suggestion
           return (G) addMetric(name, gauge);
   ```



##########
paimon-core/src/main/java/org/apache/paimon/metrics/AbstractMetricGroup.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.paimon.metrics;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Contains key functionality for adding metrics and carries metrics.
+ *
+ * <p>A MetricGroup can be {@link #close() closed}. Upon closing, the group 
de-register all metrics.
+ *
+ * <p>The {@link #close()} method and {@link #addMetric(String, Metric)} 
method should never be
+ * invoked by multiple threads at the same time, {@link #addMetric(String, 
Metric)} and {@link
+ * #getMetrics()} have multi-threads problems, like the reporter is reading 
the metrics map and the
+ * group is adding metrics to the map at the same time.
+ */
+public abstract class AbstractMetricGroup implements MetricGroup {
+    protected static final Logger LOG = 
LoggerFactory.getLogger(MetricGroup.class);
+
+    // ------------------------------------------------------------------------
+
+    /** The map containing all tags and their associated values, lazily 
computed. */
+    protected Map<String, String> tags;
+
+    /** Flag indicating whether this group has been closed. */
+    private boolean closed = false;
+
+    private final ConcurrentHashMap<String, Metric> metrics = new 
ConcurrentHashMap<>();
+
+    // ------------------------------------------------------------------------
+
+    public AbstractMetricGroup(@Nullable Map<String, String> tags) {
+        this.tags = tags;
+        Metrics.getInstance().addGroup(this);
+    }
+
+    @Override
+    public Map<String, String> getAllTags() {
+        return tags;
+    }
+
+    /**
+     * Returns the fully qualified metric name using the configured delimiter 
for the reporter with
+     * the given index, for example {@code "myTable.bucket-1.metricName"}.
+     *
+     * @param metricName metric name
+     * @param delimiter delimiter to use
+     * @return fully qualified metric name
+     */
+    public String getMetricIdentifier(String metricName, String delimiter) {
+        return String.join(delimiter, getGroupName(), metricName);
+    }
+
+    /**
+     * Creates and registers a new {@link org.apache.paimon.metrics.Counter} 
or return the existing
+     * {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @return the created or existing counter
+     */
+    public Counter counter(String name) {
+        return counter(name, new SimpleCounter());
+    }
+
+    /**
+     * Registers a {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @param counter counter to register
+     * @param <C> counter type
+     * @return the given counter
+     */
+    public <C extends Counter> C counter(String name, C counter) {
+        Metric metric = addMetric(name, counter);
+        return (C) metric;

Review Comment:
   ```suggestion
           return (C) addMetric(name, counter);
   ```



##########
paimon-core/src/test/java/org/apache/paimon/metrics/MetricGroupTest.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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.paimon.metrics;
+
+import org.apache.paimon.metrics.groups.GenericMetricGroup;
+
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/** Tests for the {@link MetricGroup}. */
+public class MetricGroupTest {
+    @Test
+    public void closedGroupDoesNotRegisterMetrics() {
+        GenericMetricGroup group = 
GenericMetricGroup.createGenericMetricGroup("myTable", "commit");
+        assertFalse(group.isClosed());
+
+        group.close();
+        assertTrue(group.isClosed());
+
+        // these will fail is the registration is propagated
+        group.counter("testcounter");
+        group.gauge(
+                "testgauge",
+                new Gauge<Object>() {
+                    @Override
+                    public Object getValue() {
+                        return null;
+                    }
+                });
+        assertThat(group.getMetrics().size()).isEqualTo(0);
+    }
+
+    @Test
+    public void tolerateMetricNameCollisions() {
+        final String name = "abctestname";
+        GenericMetricGroup group = 
GenericMetricGroup.createGenericMetricGroup("myTable", "commit");
+
+        Counter counter1 = group.counter(name);
+
+        // return the old one with the metric name collision
+        assertThat(group.counter(name)).isEqualTo(counter1);

Review Comment:
   Use `isSameAs` to check for the exact same object.
   
   ```suggestion
           // return the old one with the metric name collision
           assertThat(group.counter(name)).isSameAs(counter1);
   ```



##########
paimon-core/src/main/java/org/apache/paimon/metrics/AbstractMetricGroup.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.paimon.metrics;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Contains key functionality for adding metrics and carries metrics.
+ *
+ * <p>A MetricGroup can be {@link #close() closed}. Upon closing, the group 
de-register all metrics.
+ *
+ * <p>The {@link #close()} method and {@link #addMetric(String, Metric)} 
method should never be
+ * invoked by multiple threads at the same time, {@link #addMetric(String, 
Metric)} and {@link
+ * #getMetrics()} have multi-threads problems, like the reporter is reading 
the metrics map and the
+ * group is adding metrics to the map at the same time.
+ */
+public abstract class AbstractMetricGroup implements MetricGroup {
+    protected static final Logger LOG = 
LoggerFactory.getLogger(MetricGroup.class);
+
+    // ------------------------------------------------------------------------
+
+    /** The map containing all tags and their associated values, lazily 
computed. */
+    protected Map<String, String> tags;
+
+    /** Flag indicating whether this group has been closed. */
+    private boolean closed = false;
+
+    private final ConcurrentHashMap<String, Metric> metrics = new 
ConcurrentHashMap<>();
+
+    // ------------------------------------------------------------------------
+
+    public AbstractMetricGroup(@Nullable Map<String, String> tags) {
+        this.tags = tags;
+        Metrics.getInstance().addGroup(this);
+    }
+
+    @Override
+    public Map<String, String> getAllTags() {
+        return tags;
+    }
+
+    /**
+     * Returns the fully qualified metric name using the configured delimiter 
for the reporter with
+     * the given index, for example {@code "myTable.bucket-1.metricName"}.
+     *
+     * @param metricName metric name
+     * @param delimiter delimiter to use
+     * @return fully qualified metric name
+     */
+    public String getMetricIdentifier(String metricName, String delimiter) {
+        return String.join(delimiter, getGroupName(), metricName);
+    }
+
+    /**
+     * Creates and registers a new {@link org.apache.paimon.metrics.Counter} 
or return the existing
+     * {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @return the created or existing counter
+     */
+    public Counter counter(String name) {
+        return counter(name, new SimpleCounter());
+    }
+
+    /**
+     * Registers a {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @param counter counter to register
+     * @param <C> counter type
+     * @return the given counter
+     */
+    public <C extends Counter> C counter(String name, C counter) {
+        Metric metric = addMetric(name, counter);
+        return (C) metric;
+    }
+
+    /**
+     * Registers a new {@link org.apache.paimon.metrics.Gauge}.
+     *
+     * @param name name of the gauge
+     * @param gauge gauge to register
+     * @param <T> return type of the gauge
+     * @return the given gauge
+     */
+    public <T, G extends Gauge<T>> G gauge(String name, G gauge) {
+        Metric metric = addMetric(name, gauge);
+        return (G) metric;
+    }
+
+    /**
+     * Registers a new {@link Histogram} with Paimon.
+     *
+     * @param name name of the histogram
+     * @param histogram histogram to register
+     * @param <H> histogram type
+     * @return the registered histogram
+     */
+    public <H extends Histogram> H histogram(String name, H histogram) {
+        Metric metric = addMetric(name, histogram);
+        return (H) metric;
+    }
+
+    /**
+     * Adds the given metric to the group and registers it at the registry, if 
the group is not yet
+     * closed, and if no metric with the same name has been registered before.
+     *
+     * @param metricName the name to register the metric under
+     * @param metric the metric to register
+     */
+    protected Metric addMetric(String metricName, Metric metric) {
+        if (metric == null) {
+            LOG.warn(
+                    "Ignoring attempted registration of a metric due to being 
null for name {}.",
+                    metricName);
+            return null;
+        }
+        // add the metric only if the group is still open
+        if (!isClosed()) {
+            switch (metric.getMetricType()) {
+                case COUNTER:
+                case GAUGE:
+                case HISTOGRAM:
+                    // immediately put without a 'contains' check to optimize 
the common case
+                    // (no
+                    // collision)
+                    // collisions are resolved later
+                    Metric prior = metrics.put(metricName, metric);
+
+                    // check for collisions with other metric names
+                    if (prior != null) {
+                        // we had a collision. put back the original value
+                        metrics.put(metricName, prior);
+
+                        // we warn here, rather than failing, because metrics 
are tools that
+                        // should not
+                        // fail the
+                        // program when used incorrectly

Review Comment:
   ```suggestion
                           // we warn here, rather than failing, because 
metrics are tools that
                           // should not fail the program when used incorrectly
   ```



##########
paimon-core/src/test/java/org/apache/paimon/metrics/MetricGroupTest.java:
##########
@@ -0,0 +1,59 @@
+/*
+ * 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.paimon.metrics;
+
+import org.apache.paimon.metrics.groups.GenericMetricGroup;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/** Tests for the {@link MetricGroup}. */
+public class MetricGroupTest {
+    @Test
+    public void closedGroupDoesNotRegisterMetrics() {
+        GenericMetricGroup group = new GenericMetricGroup("testgroup");
+        assertFalse(group.isClosed());
+
+        group.close();
+        assertTrue(group.isClosed());
+
+        // these will fail is the registration is propagated
+        group.counter("testcounter");
+        group.gauge(
+                "testgauge",
+                new Gauge<Object>() {
+                    @Override
+                    public Object getValue() {
+                        return null;
+                    }
+                });

Review Comment:
   I'm wondering why we need this test. Under what circumstances will we add a 
metric after the metric group is closed?



##########
paimon-core/src/main/java/org/apache/paimon/metrics/AbstractMetricGroup.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.paimon.metrics;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Contains key functionality for adding metrics and carries metrics.
+ *
+ * <p>A MetricGroup can be {@link #close() closed}. Upon closing, the group 
de-register all metrics.
+ *
+ * <p>The {@link #close()} method and {@link #addMetric(String, Metric)} 
method should never be
+ * invoked by multiple threads at the same time, {@link #addMetric(String, 
Metric)} and {@link
+ * #getMetrics()} have multi-threads problems, like the reporter is reading 
the metrics map and the
+ * group is adding metrics to the map at the same time.
+ */
+public abstract class AbstractMetricGroup implements MetricGroup {
+    protected static final Logger LOG = 
LoggerFactory.getLogger(MetricGroup.class);
+
+    // ------------------------------------------------------------------------
+
+    /** The map containing all tags and their associated values, lazily 
computed. */
+    protected Map<String, String> tags;
+
+    /** Flag indicating whether this group has been closed. */
+    private boolean closed = false;
+
+    private final ConcurrentHashMap<String, Metric> metrics = new 
ConcurrentHashMap<>();
+
+    // ------------------------------------------------------------------------
+
+    public AbstractMetricGroup(@Nullable Map<String, String> tags) {
+        this.tags = tags;
+        Metrics.getInstance().addGroup(this);
+    }
+
+    @Override
+    public Map<String, String> getAllTags() {
+        return tags;
+    }
+
+    /**
+     * Returns the fully qualified metric name using the configured delimiter 
for the reporter with
+     * the given index, for example {@code "myTable.bucket-1.metricName"}.
+     *
+     * @param metricName metric name
+     * @param delimiter delimiter to use
+     * @return fully qualified metric name
+     */
+    public String getMetricIdentifier(String metricName, String delimiter) {
+        return String.join(delimiter, getGroupName(), metricName);
+    }
+
+    /**
+     * Creates and registers a new {@link org.apache.paimon.metrics.Counter} 
or return the existing
+     * {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @return the created or existing counter
+     */
+    public Counter counter(String name) {
+        return counter(name, new SimpleCounter());
+    }
+
+    /**
+     * Registers a {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @param counter counter to register
+     * @param <C> counter type
+     * @return the given counter
+     */
+    public <C extends Counter> C counter(String name, C counter) {
+        Metric metric = addMetric(name, counter);
+        return (C) metric;
+    }
+
+    /**
+     * Registers a new {@link org.apache.paimon.metrics.Gauge}.
+     *
+     * @param name name of the gauge
+     * @param gauge gauge to register
+     * @param <T> return type of the gauge
+     * @return the given gauge
+     */
+    public <T, G extends Gauge<T>> G gauge(String name, G gauge) {
+        Metric metric = addMetric(name, gauge);
+        return (G) metric;
+    }
+
+    /**
+     * Registers a new {@link Histogram} with Paimon.
+     *
+     * @param name name of the histogram
+     * @param histogram histogram to register
+     * @param <H> histogram type
+     * @return the registered histogram
+     */
+    public <H extends Histogram> H histogram(String name, H histogram) {
+        Metric metric = addMetric(name, histogram);
+        return (H) metric;

Review Comment:
   ```suggestion
           return (H) addMetric(name, histogram);
   ```



##########
paimon-core/src/main/java/org/apache/paimon/metrics/Metrics.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.paimon.metrics;
+
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+/** Core of Paimon metrics system. */
+public class Metrics {
+    private static volatile Metrics instance = new Metrics();
+
+    /**
+     * The metrics groups. All the commit & compaction & scan metric groups 
are collected in this
+     * group container, there is no need to distinguish the groups by group 
name for reporters.
+     */
+    private final ConcurrentLinkedQueue<MetricGroup> metricGroups = new 
ConcurrentLinkedQueue<>();
+
+    private Metrics() {}
+
+    public static Metrics getInstance() {
+        return instance;
+    }
+
+    /**
+     * Add a metric group. Which is called by metrics instances, like commit / 
compaction metrics
+     * instances.
+     */
+    public synchronized void addGroup(AbstractMetricGroup group) {
+        metricGroups.add(group);
+    }
+
+    /** Get metric groups. */
+    public synchronized ConcurrentLinkedQueue<MetricGroup> getMetricGroups() {
+        return metricGroups;
+    }

Review Comment:
   Why `synchronized`? You already used `ConcurrentLinkedQueue`.



##########
paimon-core/src/main/java/org/apache/paimon/metrics/MetricGroup.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.paimon.metrics;
+
+import org.apache.paimon.annotation.Public;
+
+import java.util.Map;
+
+/**
+ * A MetricGroup is a named container for {@link Metric Metrics} and further 
metric subgroups.
+ *
+ * <p>Instances of this class can be used to register new metrics with Paimon.
+ */
+@Public
+public interface MetricGroup {
+
+    /**
+     * Creates and registers a new {@link org.apache.paimon.metrics.Counter} 
with Paimon.
+     *
+     * @param name name of the counter
+     * @return the created counter
+     */
+    Counter counter(String name);
+
+    /**
+     * Registers a {@link org.apache.paimon.metrics.Counter} with Paimon.
+     *
+     * @param name name of the counter
+     * @param counter counter to register
+     * @param <C> counter type
+     * @return the given counter
+     */
+    <C extends Counter> C counter(String name, C counter);
+
+    /**
+     * Registers a new {@link org.apache.paimon.metrics.Gauge} with Paimon.
+     *
+     * @param name name of the gauge
+     * @param gauge gauge to register
+     * @param <T> return type of the gauge
+     * @return the given gauge
+     */
+    <T, G extends Gauge<T>> G gauge(String name, G gauge);
+
+    /**
+     * Registers a new {@link Histogram} with Paimon.
+     *
+     * @param name name of the histogram
+     * @param histogram histogram to register
+     * @param <H> histogram type
+     * @return the registered histogram
+     */
+    <H extends Histogram> H histogram(String name, H histogram);
+
+    /**
+     * Returns the fully qualified metric name, for example {@code 
"myTable.bucket-1.metricName"}.
+     *
+     * @param metricName metric name
+     * @return fully qualified metric name
+     */
+    String getMetricIdentifier(String metricName, String delimiter);
+
+    /** Returns a map of all variables and their associated value. */
+    Map<String, String> getAllTags();
+
+    /**
+     * Returns the name for this group, meaning what kind of entity it 
represents, for example
+     * "bucket".
+     */
+    String getGroupName();

Review Comment:
   Same as above.



##########
paimon-core/src/main/java/org/apache/paimon/metrics/AbstractMetricGroup.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.paimon.metrics;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Contains key functionality for adding metrics and carries metrics.
+ *
+ * <p>A MetricGroup can be {@link #close() closed}. Upon closing, the group 
de-register all metrics.
+ *
+ * <p>The {@link #close()} method and {@link #addMetric(String, Metric)} 
method should never be
+ * invoked by multiple threads at the same time, {@link #addMetric(String, 
Metric)} and {@link
+ * #getMetrics()} have multi-threads problems, like the reporter is reading 
the metrics map and the
+ * group is adding metrics to the map at the same time.
+ */
+public abstract class AbstractMetricGroup implements MetricGroup {
+    protected static final Logger LOG = 
LoggerFactory.getLogger(MetricGroup.class);
+
+    // ------------------------------------------------------------------------
+
+    /** The map containing all tags and their associated values, lazily 
computed. */
+    protected Map<String, String> tags;
+
+    /** Flag indicating whether this group has been closed. */
+    private boolean closed = false;
+
+    private final ConcurrentHashMap<String, Metric> metrics = new 
ConcurrentHashMap<>();
+
+    // ------------------------------------------------------------------------
+
+    public AbstractMetricGroup(@Nullable Map<String, String> tags) {
+        this.tags = tags;
+        Metrics.getInstance().addGroup(this);
+    }
+
+    @Override
+    public Map<String, String> getAllTags() {
+        return tags;
+    }
+
+    /**
+     * Returns the fully qualified metric name using the configured delimiter 
for the reporter with
+     * the given index, for example {@code "myTable.bucket-1.metricName"}.
+     *
+     * @param metricName metric name
+     * @param delimiter delimiter to use
+     * @return fully qualified metric name
+     */
+    public String getMetricIdentifier(String metricName, String delimiter) {
+        return String.join(delimiter, getGroupName(), metricName);
+    }
+
+    /**
+     * Creates and registers a new {@link org.apache.paimon.metrics.Counter} 
or return the existing
+     * {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @return the created or existing counter
+     */
+    public Counter counter(String name) {
+        return counter(name, new SimpleCounter());
+    }
+
+    /**
+     * Registers a {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @param counter counter to register
+     * @param <C> counter type
+     * @return the given counter
+     */
+    public <C extends Counter> C counter(String name, C counter) {
+        Metric metric = addMetric(name, counter);
+        return (C) metric;
+    }
+
+    /**
+     * Registers a new {@link org.apache.paimon.metrics.Gauge}.
+     *
+     * @param name name of the gauge
+     * @param gauge gauge to register
+     * @param <T> return type of the gauge
+     * @return the given gauge
+     */
+    public <T, G extends Gauge<T>> G gauge(String name, G gauge) {
+        Metric metric = addMetric(name, gauge);
+        return (G) metric;
+    }
+
+    /**
+     * Registers a new {@link Histogram} with Paimon.
+     *
+     * @param name name of the histogram
+     * @param histogram histogram to register
+     * @param <H> histogram type
+     * @return the registered histogram
+     */
+    public <H extends Histogram> H histogram(String name, H histogram) {
+        Metric metric = addMetric(name, histogram);
+        return (H) metric;
+    }
+
+    /**
+     * Adds the given metric to the group and registers it at the registry, if 
the group is not yet
+     * closed, and if no metric with the same name has been registered before.
+     *
+     * @param metricName the name to register the metric under
+     * @param metric the metric to register
+     */
+    protected Metric addMetric(String metricName, Metric metric) {
+        if (metric == null) {
+            LOG.warn(
+                    "Ignoring attempted registration of a metric due to being 
null for name {}.",
+                    metricName);
+            return null;
+        }
+        // add the metric only if the group is still open
+        if (!isClosed()) {
+            switch (metric.getMetricType()) {
+                case COUNTER:
+                case GAUGE:
+                case HISTOGRAM:
+                    // immediately put without a 'contains' check to optimize 
the common case
+                    // (no
+                    // collision)
+                    // collisions are resolved later
+                    Metric prior = metrics.put(metricName, metric);

Review Comment:
   ```suggestion
                       // immediately put without a 'contains' check to 
optimize the common case
                       // (no collision), collisions are resolved later
                       Metric prior = metrics.put(metricName, metric);
   ```



##########
paimon-core/src/main/java/org/apache/paimon/metrics/AbstractMetricGroup.java:
##########
@@ -0,0 +1,201 @@
+/*
+ * 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.paimon.metrics;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Contains key functionality for adding metrics and carries metrics.
+ *
+ * <p>A MetricGroup can be {@link #close() closed}. Upon closing, the group 
de-register all metrics.
+ *
+ * <p>The {@link #close()} method and {@link #addMetric(String, Metric)} 
method should never be
+ * invoked by multiple threads at the same time, {@link #addMetric(String, 
Metric)} and {@link
+ * #getMetrics()} have multi-threads problems, like the reporter is reading 
the metrics map and the
+ * group is adding metrics to the map at the same time.
+ */
+public abstract class AbstractMetricGroup implements MetricGroup {
+    protected static final Logger LOG = 
LoggerFactory.getLogger(MetricGroup.class);
+
+    // ------------------------------------------------------------------------
+
+    /** The map containing all tags and their associated values, lazily 
computed. */
+    protected Map<String, String> tags;
+
+    /** Flag indicating whether this group has been closed. */
+    private boolean closed = false;
+
+    private final ConcurrentHashMap<String, Metric> metrics = new 
ConcurrentHashMap<>();
+
+    // ------------------------------------------------------------------------
+
+    public AbstractMetricGroup(@Nullable Map<String, String> tags) {
+        this.tags = tags;
+        Metrics.getInstance().addGroup(this);
+    }
+
+    @Override
+    public Map<String, String> getAllTags() {
+        return tags;
+    }
+
+    /**
+     * Returns the fully qualified metric name using the configured delimiter 
for the reporter with
+     * the given index, for example {@code "myTable.bucket-1.metricName"}.
+     *
+     * @param metricName metric name
+     * @param delimiter delimiter to use
+     * @return fully qualified metric name
+     */
+    public String getMetricIdentifier(String metricName, String delimiter) {
+        return String.join(delimiter, getGroupName(), metricName);
+    }
+
+    /**
+     * Creates and registers a new {@link org.apache.paimon.metrics.Counter} 
or return the existing
+     * {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @return the created or existing counter
+     */
+    public Counter counter(String name) {
+        return counter(name, new SimpleCounter());
+    }
+
+    /**
+     * Registers a {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @param counter counter to register
+     * @param <C> counter type
+     * @return the given counter
+     */
+    public <C extends Counter> C counter(String name, C counter) {
+        Metric metric = addMetric(name, counter);
+        return (C) metric;
+    }
+
+    /**
+     * Registers a new {@link org.apache.paimon.metrics.Gauge}.
+     *
+     * @param name name of the gauge
+     * @param gauge gauge to register
+     * @param <T> return type of the gauge
+     * @return the given gauge
+     */
+    public <T, G extends Gauge<T>> G gauge(String name, G gauge) {
+        Metric metric = addMetric(name, gauge);
+        return (G) metric;
+    }
+
+    /**
+     * Registers a new {@link Histogram} with Paimon.
+     *
+     * @param name name of the histogram
+     * @param histogram histogram to register
+     * @param <H> histogram type
+     * @return the registered histogram
+     */
+    public <H extends Histogram> H histogram(String name, H histogram) {
+        Metric metric = addMetric(name, histogram);
+        return (H) metric;
+    }
+
+    /**
+     * Adds the given metric to the group and registers it at the registry, if 
the group is not yet
+     * closed, and if no metric with the same name has been registered before.
+     *
+     * @param metricName the name to register the metric under
+     * @param metric the metric to register
+     */
+    protected Metric addMetric(String metricName, Metric metric) {
+        if (metric == null) {
+            LOG.warn(
+                    "Ignoring attempted registration of a metric due to being 
null for name {}.",
+                    metricName);
+            return null;
+        }
+        // add the metric only if the group is still open
+        if (!isClosed()) {
+            switch (metric.getMetricType()) {
+                case COUNTER:
+                case GAUGE:
+                case HISTOGRAM:
+                    // immediately put without a 'contains' check to optimize 
the common case
+                    // (no
+                    // collision)
+                    // collisions are resolved later
+                    Metric prior = metrics.put(metricName, metric);
+
+                    // check for collisions with other metric names
+                    if (prior != null) {
+                        // we had a collision. put back the original value
+                        metrics.put(metricName, prior);
+
+                        // we warn here, rather than failing, because metrics 
are tools that
+                        // should not
+                        // fail the
+                        // program when used incorrectly
+                        LOG.warn(
+                                "Name collision: Group already contains a 
Metric with the name '"
+                                        + metricName
+                                        + "'. The new added Metric will not be 
reported.");
+                    }
+                    break;
+                default:
+                    LOG.warn(
+                            "Cannot add unknown metric type {}. This indicates 
that the paimon "
+                                    + "does not support this metric type.",
+                            metric.getClass().getName());
+            }
+        }
+        return metrics.get(metricName);
+    }
+
+    @Override
+    public Map<String, Metric> getMetrics() {
+        return Collections.unmodifiableMap(metrics);
+    }
+
+    /**
+     * Returns the name for this group, meaning what kind of entity it 
represents, for example
+     * "bucket".
+     *
+     * @return logical name for this group
+     */
+    public abstract String getGroupName();

Review Comment:
   For example "bucket"? Bucket is just a tag for the metrics (at least for 
now). Shouldn't it be "compact" or "commit"?



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