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


##########
paimon-core/src/main/java/org/apache/paimon/metrics/commit/CommitMetrics.java:
##########
@@ -0,0 +1,246 @@
+/*
+ * 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.commit;
+
+import org.apache.paimon.annotation.VisibleForTesting;
+import org.apache.paimon.fs.FileIO;
+import org.apache.paimon.fs.FileStatus;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.io.DataFilePathFactory;
+import org.apache.paimon.metrics.Counter;
+import org.apache.paimon.metrics.DescriptiveStatisticsHistogram;
+import org.apache.paimon.metrics.Histogram;
+import org.apache.paimon.metrics.MetricGroup;
+import org.apache.paimon.metrics.groups.GenericMetricGroup;
+import org.apache.paimon.utils.FileStorePathFactory;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/** Metrics to measure a commit. */
+public class CommitMetrics {
+    private static Logger log = LoggerFactory.getLogger(CommitMetrics.class);
+    private static final int HISTOGRAM_WINDOW_SIZE = 10_000;
+    protected static final String GROUP_NAME = "commit";
+
+    private final MetricGroup genericMetricGroup;
+
+    private final FileStorePathFactory pathFactory;
+    private final FileIO fileIO;
+
+    private long initTableFilesCount = 0;
+    private long initChangelogFilesCount = 0;
+
+    public CommitMetrics(FileStorePathFactory pathFactory, FileIO fileIO) {
+        this.genericMetricGroup =
+                GenericMetricGroup.createGenericMetricGroup(
+                        pathFactory.root().getName(), GROUP_NAME);
+        this.pathFactory = pathFactory;
+        this.fileIO = fileIO;
+        initDataFilesCount();
+        registerGenericCommitMetrics();
+    }
+
+    @VisibleForTesting
+    void initDataFilesCount() {
+        try {
+            List<Path> dirs =
+                    Arrays.stream(fileIO.listStatus(pathFactory.root()))
+                            .map(f -> f.getPath())
+                            .collect(Collectors.toList());
+            boolean hasPartition = true;
+            for (Path dir : dirs) {
+                if (dir.getName().startsWith("bucket-")) {
+                    hasPartition = false;
+                    break;
+                }
+            }
+            if (hasPartition) {
+                List<Path> buckets = new ArrayList<>();
+                for (Path dir : dirs) {
+                    FileStatus[] fileStatuses = fileIO.listStatus(dir);
+                    buckets.addAll(
+                            Arrays.stream(fileStatuses)
+                                    .filter(
+                                            f ->
+                                                    f.isDir()
+                                                            && f.getPath()
+                                                                    .getName()
+                                                                    
.startsWith("bucket-"))
+                                    .map(f -> f.getPath())
+                                    .collect(Collectors.toList()));
+                }
+                for (Path bucket : buckets) {
+                    FileStatus[] fileStatuses = fileIO.listStatus(bucket);
+                    accFilesCount(fileStatuses);
+                }

Review Comment:
   For some filesystem, especially object store, listing directory is super 
slow. Never enumerate table files by walking through the directory tree 
structure. This code will also be slow if the user has accumulated hundreds of 
partitions.



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