XComp commented on a change in pull request #18979:
URL: https://github.com/apache/flink/pull/18979#discussion_r822694090



##########
File path: 
flink-filesystems/flink-s3-fs-presto/src/main/java/org/apache/flink/fs/s3presto/FlinkS3PrestoFileSystem.java
##########
@@ -0,0 +1,164 @@
+/*
+ * 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.fs.s3presto;
+
+import org.apache.flink.core.fs.FileStatus;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.fs.s3.common.FlinkS3FileSystem;
+import org.apache.flink.fs.s3.common.writer.S3AccessHelper;
+import org.apache.flink.util.ExceptionUtils;
+
+import org.apache.hadoop.fs.FileSystem;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.nio.file.DirectoryNotEmptyException;
+import java.util.Optional;
+
+/**
+ * {@code FlinkS3PrestoFileSystem} provides custom recursive deletion 
functionality to work around a
+ * bug in the internally used Presto file system.
+ *
+ * <p>https://github.com/prestodb/presto/issues/17416
+ */
+public class FlinkS3PrestoFileSystem extends FlinkS3FileSystem {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(FlinkS3PrestoFileSystem.class);
+
+    /**
+     * Creates a {@code FlinkS3PrestoFileSystem} based on the given Hadoop S3 
file system. The given
+     * Hadoop file system object is expected to be initialized already.
+     *
+     * <p>This constructor additionally configures the entropy injection for 
the file system.
+     *
+     * @param hadoopS3FileSystem The Hadoop FileSystem that will be used under 
the hood.
+     * @param entropyInjectionKey The substring that will be replaced by 
entropy or removed.
+     * @param entropyLength The number of random alphanumeric characters to 
inject as entropy.
+     */
+    public FlinkS3PrestoFileSystem(
+            FileSystem hadoopS3FileSystem,
+            String localTmpDirectory,
+            @Nullable String entropyInjectionKey,
+            int entropyLength,
+            @Nullable S3AccessHelper s3UploadHelper,
+            long s3uploadPartSize,
+            int maxConcurrentUploadsPerStream) {
+        super(
+                hadoopS3FileSystem,
+                localTmpDirectory,
+                entropyInjectionKey,
+                entropyLength,
+                s3UploadHelper,
+                s3uploadPartSize,
+                maxConcurrentUploadsPerStream);
+    }
+
+    @Override
+    public boolean delete(Path path, boolean recursive) throws IOException {
+        if (recursive) {
+            deleteRecursively(path);
+        } else {
+            if (isDirectoryWithContent(path)) {
+                throw new DirectoryNotEmptyException(path.getPath());
+            }
+
+            deleteObject(path);
+        }
+
+        return true;
+    }
+
+    private void deleteRecursively(Path path) throws IOException {
+        final FileStatus[] containingFiles = listStatus(path);
+        if (containingFiles == null) {
+            LOG.warn(
+                    "No files could be retrieved even though the path was 
marked as existing. "
+                            + "This is an indication for a bug in the 
underlying FileSystem "
+                            + "implementation and should be reported. It won't 
affect the "
+                            + "processing of this run, though.");
+            return;
+        }
+
+        IOException exception = null;
+        for (FileStatus fileStatus : containingFiles) {
+            final Path childPath = fileStatus.getPath();
+
+            try {
+                if (fileStatus.isDir()) {
+                    deleteRecursively(childPath);
+                } else {
+                    deleteObject(childPath);
+                }
+            } catch (IOException e) {
+                exception = ExceptionUtils.firstOrSuppressed(e, exception);
+            }
+        }
+
+        if (exception != null) {
+            throw exception;
+        }
+
+        if (containingFiles.length == 0) {
+            // Presto doesn't hold placeholders for directories itself; 
therefore, we don't need to
+            // call deleteObject on the directory itself if there were objects 
with the prefix being
+            // deleted in the initial loop above. This saves us from doing 
some existence checks on
+            // an not-existing object (i.e. the now empty directory)
+            deleteObject(path);
+        }

Review comment:
       see discussion in the [thread 
below](https://github.com/apache/flink/pull/18979/files#r822528770)




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