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



##########
File path: 
flink-filesystems/flink-s3-fs-presto/src/main/java/org/apache/flink/fs/s3presto/FlinkS3PrestoFileSystem.java
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.flink.util.Preconditions;
+
+import org.apache.hadoop.fs.FileSystem;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+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 {
+
+    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 {
+            deleteObject(path);
+        }
+
+        return true;
+    }
+
+    private void deleteRecursively(Path path) throws IOException {
+        final FileStatus[] containingFiles =
+                Preconditions.checkNotNull(
+                        listStatus(path),
+                        "Hadoop FileSystem.listStatus should never return null 
based on its contract.");
+
+        if (containingFiles.length == 0) {
+            deleteObject(path);
+            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);
+            }
+        }
+
+        // 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)
+        if (exception != null) {
+            throw exception;
+        }
+    }
+
+    /**
+     * Deletes the object referenced by the passed {@code path}.
+     *
+     * @param path The path referring to the object that shall be deleted.
+     * @throws IOException if an error occurred while deleting the file other 
than the {@code path}
+     *     referring to a non-empty directory.
+     */
+    private void deleteObject(Path path) throws IOException {
+        if (!exists(path)) {

Review comment:
       Please document that this method exists because the Presto FS does not 
allow us to differentiate between deleting a file that doesn't exist (which 
we'd be fine with) and some random error.




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