You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@paimon.apache.org by "tsreaper (via GitHub)" <gi...@apache.org> on 2023/10/09 06:22:21 UTC

Re: [PR] [core] Support commit metrics [incubator-paimon]

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


##########
paimon-core/src/main/java/org/apache/paimon/operation/FileStoreCommitImpl.java:
##########
@@ -250,21 +271,64 @@ public void commit(ManifestCommittable committable, Map<String, String> properti
             // This optimization is mainly used to decrease the number of times we read from files.
             if (safeLatestSnapshotId != null) {
                 baseEntries.addAll(appendTableFiles);
-                noConflictsOrFail(latestSnapshot.commitUser(), baseEntries, compactTableFiles);
+                try {
+                    noConflictsOrFail(latestSnapshot.commitUser(), baseEntries, compactTableFiles);
+                } finally {
+                    long commitDuration = (System.nanoTime() - started) / 1_000_000;
+                    reportCommit(
+                            appendTableFiles,
+                            appendChangelog,
+                            Collections.emptyList(),
+                            Collections.emptyList(),
+                            commitDuration,
+                            generatedSnapshot,
+                            attempts + 1);
+                }

Review Comment:
   Don't wrap each `noConflictsOrFail` with `try...finally...`, just wrap the whole function with one `try...finally...` and report the metrics in `finally`.



##########
paimon-core/src/main/java/org/apache/paimon/metrics/commit/CommitStats.java:
##########
@@ -0,0 +1,209 @@
+/*
+ * 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.data.BinaryRow;
+import org.apache.paimon.manifest.FileKind;
+import org.apache.paimon.manifest.ManifestEntry;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/** Statistics for a commit. */
+public class CommitStats {
+    private final long duration;
+    private final int attempts;
+    private final long tableFilesAdded;
+    private final long tableFilesAppended;
+    private final long tableFilesDeleted;
+    private final long changelogFilesAppended;
+    private final long changelogFilesCompacted;
+    private final long changelogRecordsCompacted;
+
+    private final long deltaRecordsCompacted;
+    private final long changelogRecordsAppended;
+    private final long deltaRecordsAppended;
+    private final long tableFilesCompacted;
+    private final long generatedSnapshots;
+    private final long numPartitionsWritten;
+    private final long numBucketsWritten;
+
+    public CommitStats(
+            List<ManifestEntry> appendTableFiles,
+            List<ManifestEntry> appendChangelogFiles,
+            List<ManifestEntry> compactTableFiles,
+            List<ManifestEntry> compactChangelogFiles,
+            long commitDuration,
+            int generatedSnapshots,
+            int attempts) {
+        List<ManifestEntry> addedTableFiles = new ArrayList<>(appendTableFiles);
+        addedTableFiles.addAll(
+                compactTableFiles.stream()
+                        .filter(f -> FileKind.ADD.equals(f.kind()))
+                        .collect(Collectors.toList()));
+        List<ManifestEntry> deletedTableFiles =
+                compactTableFiles.stream()
+                        .filter(f -> FileKind.DELETE.equals(f.kind()))
+                        .collect(Collectors.toList());
+
+        this.tableFilesAdded = addedTableFiles.size();
+        this.tableFilesAppended = appendTableFiles.size();
+        this.tableFilesDeleted = deletedTableFiles.size();
+        this.tableFilesCompacted = compactTableFiles.size();
+        this.changelogFilesAppended = appendChangelogFiles.size();
+        this.changelogFilesCompacted = compactChangelogFiles.size();
+        this.numPartitionsWritten = numChangedPartitions(appendTableFiles, compactTableFiles);
+        this.numBucketsWritten = numChangedBuckets(appendTableFiles, compactTableFiles);
+        this.changelogRecordsCompacted = getRowCounts(compactChangelogFiles);
+        this.deltaRecordsCompacted = getRowCounts(compactTableFiles);
+        this.changelogRecordsAppended = getRowCounts(appendChangelogFiles);
+        this.deltaRecordsAppended = getRowCounts(appendTableFiles);
+        this.duration = commitDuration;
+        this.generatedSnapshots = generatedSnapshots;
+        this.attempts = attempts;
+    }
+
+    @VisibleForTesting
+    protected static long numChangedPartitions(List<ManifestEntry>... changes) {
+        return Arrays.stream(changes)
+                .flatMap(Collection::stream)
+                .map(ManifestEntry::partition)
+                .distinct()
+                .count();
+    }
+
+    @VisibleForTesting
+    protected static long numChangedBuckets(List<ManifestEntry>... changes) {
+        return changedPartBuckets(changes).values().stream()
+                .map(Set::size)
+                .reduce((a, b) -> a + b)
+                .orElseGet(() -> 0);

Review Comment:
   ```suggestion
                   .mapToLong(Set::size)
                   .sum();
   ```



##########
paimon-core/src/main/java/org/apache/paimon/table/sink/TableCommitImpl.java:
##########
@@ -261,6 +262,9 @@ public void close() throws Exception {
         }
         IOUtils.closeQuietly(lock);
         expireMainExecutor.shutdownNow();
+        if (commit instanceof FileStoreCommitImpl) {
+            ((FileStoreCommitImpl) commit).getCommitMetrics().getMetricGroup().close();
+        }

Review Comment:
   Add a `close` method to `FileStoreCommit` interface. Don't forget to check for the usages and close all `commit` object.



-- 
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-unsubscribe@paimon.apache.org

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