masteryhx commented on code in PR #19598:
URL: https://github.com/apache/flink/pull/19598#discussion_r861893623


##########
flink-state-backends/flink-statebackend-changelog/src/main/java/org/apache/flink/state/changelog/ChangelogMetricGroup.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.flink.state.changelog;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.metrics.Counter;
+import org.apache.flink.metrics.Gauge;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.runtime.metrics.groups.ProxyMetricGroup;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.locks.ReentrantLock;
+
+/** Metrics related to the Changelog State Backend. */
+class ChangelogMetricGroup extends ProxyMetricGroup<MetricGroup> {
+    private static final Logger LOG = 
LoggerFactory.getLogger(ChangelogMetricGroup.class);
+
+    private static final String PREFIX = "Changelog";
+
+    @VisibleForTesting
+    static final String NUMBER_OF_TOTAL_MATERIALIZATION = PREFIX + 
".numberOfTotalMaterialization";
+
+    @VisibleForTesting
+    static final String NUMBER_OF_IN_PROGRESS_MATERIALIZATION =
+            PREFIX + ".numberOfInProgressMaterialization";
+
+    @VisibleForTesting
+    static final String NUMBER_OF_COMPLETED_MATERIALIZATION =
+            PREFIX + ".numberOfCompletedMaterialization";
+
+    @VisibleForTesting
+    static final String NUMBER_OF_FAILED_MATERIALIZATION =
+            PREFIX + ".numberOfFailedMaterialization";
+
+    @VisibleForTesting
+    static final String LATEST_FULL_SIZE_OF_MATERIALIZATION =
+            PREFIX + ".lastFullSizeOfMaterialization";
+
+    @VisibleForTesting
+    static final String LATEST_INC_SIZE_OF_MATERIALIZATION =
+            PREFIX + ".lastIncSizeOfMaterialization";
+
+    private final Counter totalMaterializationCounter;
+    private final Counter inProgressMaterializationCounter;
+    private final Counter completedMaterializationCounter;
+    private final Counter failedMaterializationCounter;
+    private final UpdatableGauge<Long> lastFullSizeOfMaterializationGauge;
+    private final UpdatableGauge<Long> lastIncSizeOfMaterializationGauge;
+
+    private final ReentrantLock metricsReadWriteLock = new ReentrantLock();
+
+    ChangelogMetricGroup(MetricGroup parentMetricGroup) {
+        super(parentMetricGroup);
+        this.totalMaterializationCounter = 
counter(NUMBER_OF_TOTAL_MATERIALIZATION);
+        this.inProgressMaterializationCounter = 
counter(NUMBER_OF_IN_PROGRESS_MATERIALIZATION);
+        this.completedMaterializationCounter = 
counter(NUMBER_OF_COMPLETED_MATERIALIZATION);
+        this.failedMaterializationCounter = 
counter(NUMBER_OF_FAILED_MATERIALIZATION);
+        this.lastFullSizeOfMaterializationGauge =
+                gauge(LATEST_FULL_SIZE_OF_MATERIALIZATION, new 
SimpleUpdatableGauge<>());
+        this.lastIncSizeOfMaterializationGauge =
+                gauge(LATEST_INC_SIZE_OF_MATERIALIZATION, new 
SimpleUpdatableGauge<>());
+    }
+
+    void reportPendingMaterialization() {
+        metricsReadWriteLock.lock();
+        try {
+            inProgressMaterializationCounter.inc();
+            totalMaterializationCounter.inc();
+        } finally {
+            metricsReadWriteLock.unlock();
+        }
+    }
+
+    void reportCompletedMaterialization(
+            long fullSizeOfMaterialization, long incSizeOfMaterialization) {
+        metricsReadWriteLock.lock();
+        try {
+            completedMaterializationCounter.inc();
+            if (canDecrementOfInProgressMaterializationNumber()) {
+                inProgressMaterializationCounter.dec();
+            }
+            
lastFullSizeOfMaterializationGauge.updateValue(fullSizeOfMaterialization);
+            
lastIncSizeOfMaterializationGauge.updateValue(incSizeOfMaterialization);
+        } finally {
+            metricsReadWriteLock.unlock();
+        }
+    }
+
+    void reportFailedMaterialization() {
+        metricsReadWriteLock.lock();
+        try {
+            failedMaterializationCounter.inc();
+            if (canDecrementOfInProgressMaterializationNumber()) {
+                inProgressMaterializationCounter.dec();
+            }
+        } finally {
+            metricsReadWriteLock.unlock();
+        }
+    }
+
+    private boolean canDecrementOfInProgressMaterializationNumber() {
+        boolean decrementLeadsToNegativeNumber =
+                inProgressMaterializationCounter.getCount() - 1 < 0;
+        if (decrementLeadsToNegativeNumber) {
+            String errorMessage =
+                    "Incremented the completed number of materialization "
+                            + "without incrementing the in progress 
checkpoints before.";
+            LOG.warn(errorMessage);
+        }
+        return !decrementLeadsToNegativeNumber;
+    }
+
+    /** A gauge supports to update the metric value. */
+    private interface UpdatableGauge<T> extends Gauge<T> {
+        void updateValue(T newValue);

Review Comment:
   I have removed it and maintain some variables in the metric group.



-- 
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: issues-unsubscr...@flink.apache.org

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

Reply via email to