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


##########
paimon-core/src/main/java/org/apache/paimon/metrics/AbstractMetricGroup.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * 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.HashMap;
+import java.util.Map;
+
+/**
+ * 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 won't be invoked by
+ * multiple threads at the same time, {@link #addMetric(String, Metric)} and 
{@link #getMetrics()}
+ * have multi-threads problems, so they should be synchronized by the object 
lock.
+ */
+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 Map<String, Metric> metrics = new HashMap<>();
+
+    private final String table;
+
+    // ------------------------------------------------------------------------
+
+    public AbstractMetricGroup(String table, @Nullable Map<String, String> 
tags) {
+        this.table = table;
+        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, table, metricName);
+    }
+
+    /**
+     * Creates and registers a new {@link org.apache.paimon.metrics.Counter}.
+     *
+     * @param name name of the counter
+     * @return the created 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) {
+        addMetric(name, counter);
+        return counter;
+    }
+
+    /**
+     * 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) {
+        addMetric(name, gauge);
+        return 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
+     */
+    public <H extends Histogram> H histogram(String name, H histogram) {
+        addMetric(name, histogram);
+        return histogram;
+    }
+
+    /**
+     * 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 void addMetric(String metricName, Metric metric) {
+        if (metric == null) {
+            LOG.warn(
+                    "Ignoring attempted registration of a metric due to being 
null for name {}.",
+                    metricName);
+            return;
+        }
+        // add the metric only if the group is still open
+        synchronized (this) {
+            if (!isClosed()) {
+                switch (metric.getMetricType()) {
+                    case COUNTER:
+                    case GAUGE:
+                    case HISTOGRAM:
+                        metrics.put(metricName, metric);
+                        break;
+                    default:
+                        LOG.warn(
+                                "Cannot add unknown metric type {}. This 
indicates that the paimon "
+                                        + "does not support this metric type.",
+                                metric.getClass().getName());
+                }
+            }
+        }
+    }
+
+    @Override
+    public Map<String, Metric> getMetrics() {
+        Map<String, Metric> metricsMap = new HashMap<>();
+        synchronized (this) {
+            metrics.forEach((k, v) -> metricsMap.put(k, v));
+        }

Review Comment:
   I think `ConcurrentHashMap` won't guarantee the sequence of iterator and 
`put` operation right? Or there is no need to guarantee the sequence of 
iterator and the `put` operation you think?



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