platinumhamburg commented on code in PR #3404:
URL: https://github.com/apache/fluss/pull/3404#discussion_r3418475864


##########
fluss-flink/fluss-flink-common/src/main/java/org/apache/fluss/flink/action/orphan/job/ScopeEnumeratorFunction.java:
##########
@@ -0,0 +1,535 @@
+/*
+ * 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.fluss.flink.action.orphan.job;
+
+import org.apache.fluss.annotation.Internal;
+import org.apache.fluss.client.Connection;
+import org.apache.fluss.client.ConnectionFactory;
+import org.apache.fluss.client.admin.Admin;
+import org.apache.fluss.config.ConfigOptions;
+import org.apache.fluss.config.Configuration;
+import org.apache.fluss.flink.action.orphan.OrphanCleanUtils;
+import org.apache.fluss.flink.action.orphan.RpcErrorClassifier;
+import org.apache.fluss.flink.action.orphan.audit.AuditLogger;
+import org.apache.fluss.flink.action.orphan.build.ActiveRefsFetcher;
+import org.apache.fluss.flink.action.orphan.build.KvActiveRefsFetchResult;
+import org.apache.fluss.flink.action.orphan.build.LogActiveRefsFetchResult;
+import org.apache.fluss.flink.action.orphan.build.MaxKnownIdsTracker;
+import org.apache.fluss.flink.action.orphan.config.OrphanCleanConfig;
+import org.apache.fluss.flink.action.orphan.rule.OrphanDirDetector;
+import org.apache.fluss.fs.FileStatus;
+import org.apache.fluss.fs.FileSystem;
+import org.apache.fluss.fs.FsPath;
+import org.apache.fluss.metadata.PartitionInfo;
+import org.apache.fluss.metadata.TableBucket;
+import org.apache.fluss.metadata.TableInfo;
+import org.apache.fluss.metadata.TablePath;
+import org.apache.fluss.utils.FlussPaths;
+
+import org.apache.flink.streaming.api.functions.ProcessFunction;
+import org.apache.flink.util.Collector;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.function.Predicate;
+
+import static 
org.apache.fluss.flink.action.orphan.OrphanCleanUtils.enumerateBuckets;
+import static 
org.apache.fluss.flink.action.orphan.OrphanCleanUtils.fetchClusterConfigMap;
+import static 
org.apache.fluss.flink.action.orphan.OrphanCleanUtils.getFileSystemIfExists;
+import static 
org.apache.fluss.flink.action.orphan.OrphanCleanUtils.listStatuses;
+import static 
org.apache.fluss.flink.action.orphan.OrphanCleanUtils.normalizeRoot;
+import static 
org.apache.fluss.flink.action.orphan.OrphanCleanUtils.physicalPath;
+import static 
org.apache.fluss.flink.action.orphan.OrphanCleanUtils.remoteSubDir;
+import static 
org.apache.fluss.flink.action.orphan.OrphanCleanUtils.resolveClusterRemoteDataDir;
+import static 
org.apache.fluss.flink.action.orphan.OrphanCleanUtils.resolveClusterRemoteDataDirs;
+import static 
org.apache.fluss.flink.action.orphan.OrphanCleanUtils.resolveRemoteDataDir;
+
+/**
+ * Stage 1 of the orphan files cleanup job. Runs at parallelism=1 and 
concentrates all coordinator
+ * RPC interaction in a single subtask.
+ *
+ * <p>For each live bucket, emits a {@link BucketCleanTask} containing the FS 
paths and manifest
+ * locations needed for Stage 2 to execute cleanup without coordinator access. 
For each detected
+ * orphan directory, emits an {@link OrphanDirCleanTask}.
+ */
+@Internal
+public final class ScopeEnumeratorFunction extends ProcessFunction<Integer, 
CleanTask> {
+
+    private static final long serialVersionUID = 1L;
+    private static final Logger LOG = 
LoggerFactory.getLogger(ScopeEnumeratorFunction.class);
+    private static final String[] TOP_LEVEL_DIRS = {
+        FlussPaths.REMOTE_LOG_DIR_NAME, FlussPaths.REMOTE_KV_DIR_NAME
+    };
+
+    private final OrphanCleanConfig config;
+
+    public ScopeEnumeratorFunction(OrphanCleanConfig config) {
+        this.config = config;
+    }
+
+    @Override
+    public void processElement(Integer trigger, Context ctx, 
Collector<CleanTask> out)
+            throws Exception {
+        if (!config.extraConfigs().isEmpty()) {
+            
FileSystem.initialize(Configuration.fromMap(config.extraConfigs()), null);
+        }
+
+        Configuration flussConfig = new Configuration();
+        flussConfig.setString(ConfigOptions.BOOTSTRAP_SERVERS.key(), 
config.bootstrapServer());
+
+        try (Connection connection = 
ConnectionFactory.createConnection(flussConfig);
+                Admin admin = connection.getAdmin()) {
+            AuditLogger audit = new AuditLogger();
+            audit.logCutoff(config.olderThanMillis());
+
+            ActiveRefsFetcher fetcher = new ActiveRefsFetcher(admin, 3);
+            MaxKnownIdsTracker tracker = new MaxKnownIdsTracker();
+            Map<String, String> clusterConfigMap = 
fetchClusterConfigMap(admin);
+            String clusterRemoteDataDir = 
resolveClusterRemoteDataDir(clusterConfigMap);
+            List<String> clusterRoots =
+                    
normalizeRoots(resolveClusterRemoteDataDirs(clusterConfigMap));
+
+            Map<String, DbScanState> dbStates = enumerateActiveScope(admin, 
audit, tracker);
+
+            for (DbScanState dbState : dbStates.values()) {

Review Comment:
   @wuchong  Thanks for pointing this out. I agree that the current scope 
enumeration is driven by active databases, so files under a database directory 
that no longer exists in metadata are not covered.
   
   However, database-level cleanup has a different safety model from 
table/partition cleanup. Table and partition directories include stable IDs in 
their path names, so we can apply an ID-based guard such as “not in active IDs 
and no greater than the last-known max ID”. Database directories, however, only 
carry the database name and do not have a database ID in the remote path.
   
   Because of that, an orphan database directory cannot be safely inferred from 
the filesystem alone. If a database is deleted and later recreated with the 
same name, the remote path is indistinguishable from the old one. Without an 
additional mechanism such as a database ID, deletion marker/tombstone, 
coordinator-side metadata, or a DB-level lock/epoch, cleaning a deleted 
database directory would risk deleting data for a newly recreated database.
   
   So I agree this is a known coverage gap and we can create an issue to track 
it. However, I think database-level orphan cleanup is almost impossible to 
implement safely based only on filesystem paths before introducing a database 
ID or an equivalent ownership/epoch mechanism. I would treat the issue as a 
long-term target to document the limitation, required prerequisites, and 
possible future design, rather than something we can directly address in this 
PR or as a near-term follow-up.



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