luoyuxia commented on code in PR #21257:
URL: https://github.com/apache/flink/pull/21257#discussion_r1058733349


##########
flink-connectors/flink-connector-files/src/main/java/org/apache/flink/connector/file/table/CompactFileUtils.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * 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.flink.connector.file.table;
+
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.connector.file.table.stream.compact.CompactContext;
+import org.apache.flink.connector.file.table.stream.compact.CompactReader;
+import org.apache.flink.connector.file.table.stream.compact.CompactWriter;
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.util.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/** Utils for compacting files. */
+public class CompactFileUtils {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(CompactFileUtils.class);
+
+    public static final String UNCOMPACTED_PREFIX = ".uncompacted-";
+
+    public static final String COMPACTED_PREFIX = "compacted-";
+
+    /** The function interface for move single file. */
+    @FunctionalInterface
+    public interface SingleFileMvFunction<T, U, R> {
+        R apply(T t, U u) throws IOException;
+    }
+
+    /**
+     * Do Compaction: - Target file exists, do nothing. - Can do compaction: - 
Single file, do
+     * atomic renaming, there are optimizations for FileSystem. - Multiple 
file, do reading and
+     * writing.
+     */
+    public static <T> Path doCompact(
+            FileSystem fileSystem,
+            String partition,
+            List<Path> paths,
+            Configuration config,
+            SingleFileMvFunction<Path, Path, Boolean> singleFileMvFunc,
+            CompactReader.Factory<T> readerFactory,
+            CompactWriter.Factory<T> writerFactory)
+            throws IOException {
+        if (paths.size() == 0) {
+            return null;
+        }
+
+        Map<Path, Long> inputMap = new HashMap<>();
+        for (Path path : paths) {
+            inputMap.put(path, fileSystem.getFileStatus(path).getLen());
+        }
+
+        Path target = createCompactedFile(paths);
+        if (fileSystem.exists(target)) {
+            return target;

Review Comment:
   A bit of more explanation in here:
   if the compact unit only contains single file, we will do nothing and make 
the downstream operator commit(rename) the file.
   
   If the compact unit contains multiple files, we will read these files and 
write them to a target path, but the target path is different for different 
attempt.
   
   So, in these operator, speculative execution won't make  any difference.
   



-- 
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-unsubscr...@flink.apache.org

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

Reply via email to