NestDream commented on code in PR #28360:
URL: https://github.com/apache/flink/pull/28360#discussion_r3836778747


##########
flink-filesystems/flink-s3-fs-native/src/test/java/org/apache/flink/fs/s3native/writer/NativeS3RecoverableFsDataOutputStreamTest.java:
##########
@@ -0,0 +1,240 @@
+/*
+ * 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.s3native.writer;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * Tests for the local temp-file handling of {@link 
NativeS3RecoverableFsDataOutputStream} on the
+ * part-upload failure and commit paths.
+ */
+class NativeS3RecoverableFsDataOutputStreamTest {
+
+    private static final long MIN_PART_SIZE = 5L * 1024 * 1024; // 5 MB
+    private static final String KEY = "test/object";
+    private static final String UPLOAD_ID = "test-upload-id";
+
+    /** The two call sites that can hit a failing {@code uploadPart()}. */
+    enum UploadFailurePath {
+        WRITE,
+        CLOSE_FOR_COMMIT
+    }
+
+    /**
+     * When {@code uploadPart()} fails, whether flushed mid-stream from {@code 
write()} or at commit
+     * time from {@code closeForCommit()}, the temp file is intentionally 
retained so the upload
+     * exception propagates unmasked, and the subsequent {@code close()} 
reclaims it.
+     */
+    @ParameterizedTest
+    @EnumSource(UploadFailurePath.class)
+    void uploadPartFailureIsReclaimedByClose(UploadFailurePath path, @TempDir 
Path tmpDir)
+            throws IOException {
+        FailingUploadHelper helper = new FailingUploadHelper();
+        NativeS3RecoverableFsDataOutputStream stream =
+                new NativeS3RecoverableFsDataOutputStream(
+                        helper, KEY, UPLOAD_ID, tmpDir.toString(), 
MIN_PART_SIZE);
+
+        if (path == UploadFailurePath.WRITE) {
+            // Write >= minPartSize so write() flushes the part immediately, 
which fails.
+            byte[] payload = new byte[(int) MIN_PART_SIZE];
+            assertThatThrownBy(() -> stream.write(payload, 0, payload.length))
+                    .isInstanceOf(IOException.class);
+        } else {
+            // Write < minPartSize so the single pending part is uploaded only 
at commit time.
+            stream.write(new byte[1024], 0, 1024);
+            
assertThatThrownBy(stream::closeForCommit).isInstanceOf(IOException.class);
+        }
+
+        assertThat(tempFiles(tmpDir))
+                .as("temp file is reclaimed by close(), not eagerly")
+                .isNotEmpty();
+
+        stream.close();
+
+        assertThat(tempFiles(tmpDir)).as("no temp file should remain after 
close()").isEmpty();
+    }
+
+    /** The temp file for a successfully uploaded part is deleted on the 
normal commit path. */
+    @Test
+    void closeForCommitSuccessDeletesTempFile(@TempDir Path tmpDir) throws 
IOException {

Review Comment:
   Fair point, thanks🙂. Reworked the tests on top of the rebase: each new test 
first asserts the temp file is actually there, then does the action, then 
asserts the directory is empty (or holds a different file, for the rotation 
case).
   
   I also tried both of the ways you listed to break the old tests. Commenting 
out the `write()` in `setUp()` doesn't pass anymore, 
`closeForCommitDeletesTempFileOnSuccess` checks the part actually reached the 
upload and `partUploadDeletesAlreadyRemovedTempFile` checks `uploadPartAttempts 
== 1`. The foo-bar rename doesn't work either, the counting goes through the 
existing `countLocalFilesIn()` which counts everything in the directory, no 
`s3-part-` filter.
   
   Each of the three converted sites has its own failing test if you drop it: 
`uploadCurrentPart()` -> `closeForCommitDeletesTempFileOnSuccess`, the 
`closeForCommit()` else-branch -> `partUploadDeletesAlreadyRemovedTempFile`, 
`deleteTempFile()` -> `partUploadFailureLeavesTempFileForClose`.



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