Guosmilesmile commented on code in PR #13302:
URL: https://github.com/apache/iceberg/pull/13302#discussion_r2178857945


##########
flink/v2.0/flink/src/main/java/org/apache/iceberg/flink/maintenance/operator/OrphanFilesDetector.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.iceberg.flink.maintenance.operator;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.functions.OpenContext;
+import org.apache.flink.api.common.state.MapState;
+import org.apache.flink.api.common.state.MapStateDescriptor;
+import org.apache.flink.api.common.state.ValueState;
+import org.apache.flink.api.common.state.ValueStateDescriptor;
+import org.apache.flink.api.common.typeinfo.Types;
+import org.apache.flink.streaming.api.functions.co.KeyedCoProcessFunction;
+import org.apache.flink.util.Collector;
+import org.apache.iceberg.actions.DeleteOrphanFiles;
+import org.apache.iceberg.actions.FileURI;
+import org.apache.iceberg.exceptions.ValidationException;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A specialized co-process function that performs an anti-join between two 
streams of file URIs.
+ *
+ * <p>Emits every file that exists in the file system but is not referenced in 
the table metadata,
+ * which are considered orphan files. It also handles URI normalization using 
provided scheme and
+ * authority equivalence mappings.
+ */
+@Internal
+public class OrphanFilesDetector extends KeyedCoProcessFunction<String, 
String, String, String> {
+  private static final Logger LOG = 
LoggerFactory.getLogger(OrphanFilesDetector.class);
+
+  // Use MapState to dedupe the strings found in the table
+  private transient MapState<String, Boolean> foundInTable;
+  private transient ValueState<String> foundInFileSystem;
+  private transient ValueState<Boolean> hasUriError;
+  private final DeleteOrphanFiles.PrefixMismatchMode prefixMismatchMode;
+  private final Map<String, String> equalSchemes;
+  private final Map<String, String> equalAuthorities;
+
+  public OrphanFilesDetector(
+      DeleteOrphanFiles.PrefixMismatchMode prefixMismatchMode,
+      Map<String, String> equalSchemes,
+      Map<String, String> equalAuthorities) {
+    this.prefixMismatchMode = prefixMismatchMode;
+    this.equalSchemes = equalSchemes;
+    this.equalAuthorities = equalAuthorities;
+  }
+
+  @Override
+  public void open(OpenContext openContext) throws Exception {
+    super.open(openContext);
+    foundInTable =
+        getRuntimeContext()
+            .getMapState(
+                new MapStateDescriptor<>("antiJoinFoundInTable", Types.STRING, 
Types.BOOLEAN));
+    hasUriError =
+        getRuntimeContext().getState(new 
ValueStateDescriptor<>("antiJoinUriError", Types.BOOLEAN));
+    foundInFileSystem =
+        getRuntimeContext()
+            .getState(new ValueStateDescriptor<>("antiJoinFoundInFileSystem", 
Types.STRING));
+  }
+
+  @Override
+  public void processElement1(String value, Context context, Collector<String> 
collector)
+      throws Exception {
+    if (shouldSkipElement(value, context)) {
+      return;
+    }
+
+    if (!foundInTable.contains(value)) {
+      foundInTable.put(value, true);

Review Comment:
   The reason for initially using ValueState and later switching to MapState is 
as follows:
   
   1. The key is the URL with the schemes and authority removed, so it cannot 
be used directly as a key. Instead, the original URL needs to be stored.
   
   
https://github.com/apache/iceberg/blob/07126c5815a788de8cbe90bc8e9cb74304b0806f/flink/v2.0/flink/src/main/java/org/apache/iceberg/flink/maintenance/operator/FileUriKeySelector.java#L50-L53
   
   2. We think that in certain scenarios, such as when multiple authorities 
exist, there may be multiple pieces of data for foundInTable. Therefore, 
MapState is used to avoid accidental data deletion and deduplicate data.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to