Stephen0421 commented on code in PR #9207:
URL: https://github.com/apache/paimon/pull/9207#discussion_r3795106125


##########
paimon-core/src/main/java/org/apache/paimon/operation/ManagedBlobOrphanFilesClean.java:
##########
@@ -0,0 +1,266 @@
+/*
+ * 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.operation;
+
+import org.apache.paimon.Snapshot;
+import org.apache.paimon.blob.ManagedBlobReachabilityCollector;
+import org.apache.paimon.blob.ManagedBlobReachabilityCollector.Result;
+import org.apache.paimon.blob.ManagedBlobReferenceFile;
+import org.apache.paimon.blob.ManagedBlobReferenceFile.Reference;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.io.DataFilePathFactory;
+import org.apache.paimon.manifest.FileKind;
+import org.apache.paimon.manifest.ManifestEntry;
+import org.apache.paimon.manifest.ManifestFile;
+import org.apache.paimon.manifest.ManifestFileMeta;
+import org.apache.paimon.manifest.ManifestList;
+import org.apache.paimon.table.FileStoreTable;
+import org.apache.paimon.utils.DataFilePathFactories;
+
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.function.Consumer;
+
+/**
+ * Cleans unreferenced primary-key {@code .managed.blob} packs.
+ *
+ * <p>Unlike {@link OrphanFilesClean}, this cleaner only lists and deletes 
managed BLOB packs. Pack
+ * reachability is collected from live {@link FileKind#ADD} data-file {@code 
.blobref} sidecars.
+ * Missing manifest lists or unreadable sidecars on a still-existing data file 
abort pack deletion
+ * for the rest of the run.
+ *
+ * <p>Used packs are collected twice. If the snapshot topology or the 
used-pack set changes between
+ * those collections, this run deletes nothing. That shrinks the race with 
compaction reuse; it is
+ * not a commit lease.
+ */
+public abstract class ManagedBlobOrphanFilesClean extends OrphanFilesClean {
+
+    /**
+     * Marker emitted into the used-pack set when a {@code .blobref} sidecar 
or a required manifest
+     * cannot be trusted. Callers must skip deleting every {@code 
.managed.blob} pack.
+     */
+    public static final String SKIP_MANAGED_BLOB_GC = 
"__paimon_skip_managed_blob_gc__";
+
+    public ManagedBlobOrphanFilesClean(FileStoreTable table, long 
olderThanMillis, boolean dryRun) {
+        super(table, olderThanMillis, dryRun);
+    }
+
+    /**
+     * Join key for a managed pack. Identity is {@code storageRootId + 
relativePath}, reconstructed
+     * by {@link Reference#toPath()}. The FileIO scheme is omitted so a 
wrapper such as test {@code
+     * traceable:} still matches listed {@code file:} paths at the same 
location.
+     */
+    public static String packIdentity(Path packPath) {
+        URI uri = packPath.toUri();
+        String authority = uri.getAuthority();
+        String path = uri.getPath();
+        if (authority == null || authority.isEmpty()) {
+            return path;
+        }
+        return authority + path;
+    }
+
+    public static String packIdentity(Reference reference) {
+        return packIdentity(reference.toPath());
+    }
+
+    /**
+     * Sorted {@code branch:snapshotId} pairs over every valid branch. Used to 
abort pack GC when
+     * the snapshot set changes between the two used-pack collections.
+     */
+    protected List<String> snapshotTopology() throws IOException {
+        List<String> topology = new ArrayList<>();
+        for (String branch : validBranches()) {
+            for (Snapshot snapshot : safelyGetAllSnapshots(branch)) {
+                topology.add(branch + ":" + snapshot.id());
+            }
+        }
+        Collections.sort(topology);
+        return topology;
+    }
+
+    /**
+     * Collects used pack identities from every valid branch. Subclasses may 
override to
+     * parallelize.
+     */
+    protected Set<String> collectUsedPacks() throws IOException {
+        Set<String> used = new HashSet<>();
+        for (String branch : validBranches()) {
+            for (Snapshot snapshot : safelyGetAllSnapshots(branch)) {

Review Comment:
   Fixed.
   
   Each mark pass now performs global deduplication before reading immutable 
metadata:
   
   - manifest lists are deduplicated by `(branch, manifestListName)`;
   - manifests are deduplicated by `(branch, manifestName)`;
   - resolved `.blobref` sidecars are represented as serializable 
`SidecarWorkItem`s and deduplicated by their canonical sidecar identity.
   
   The two mark passes use independent deduplication state, so the concurrency 
check remains unchanged.
   
   Flink and Spark tests now construct the same sidecar referenced by different 
manifests routed to different partitions, and verify that every manifest list, 
manifest, and sidecar is read exactly once per pass.



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