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


##########
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 {
+        NoopObjectOperations helper = new NoopObjectOperations();
+        NativeS3RecoverableFsDataOutputStream stream =
+                new NativeS3RecoverableFsDataOutputStream(
+                        helper, KEY, UPLOAD_ID, tmpDir.toString(), 
MIN_PART_SIZE);
+
+        stream.write(new byte[1024], 0, 1024);
+
+        assertThat(stream.closeForCommit()).isNotNull();
+        assertThat(tempFiles(tmpDir))
+                .as("no temp file should remain after a successful commit 
upload")
+                .isEmpty();
+    }
+
+    /** {@code closeForCommit()} succeeds even when the temp file was already 
removed. */
+    @Test
+    void closeForCommitIsIdempotentWhenTempFileMissing(@TempDir Path tmpDir) 
throws IOException {
+        NoopObjectOperations helper = new NoopObjectOperations();
+        NativeS3RecoverableFsDataOutputStream stream =
+                new NativeS3RecoverableFsDataOutputStream(
+                        helper, KEY, UPLOAD_ID, tmpDir.toString(), 
MIN_PART_SIZE);
+
+        // No write() -> currentPartSize == 0 -> closeForCommit() takes the 
else (delete) branch.
+        List<File> files = tempFiles(tmpDir);
+        assertThat(files).hasSize(1);
+        Files.delete(files.get(0).toPath());
+
+        assertThat(stream.closeForCommit()).isNotNull();
+    }
+
+    /**
+     * The per-part cleanup in {@code uploadCurrentPart()} succeeds even when 
the temp file was
+     * already removed while the part was uploading.
+     */
+    @Test
+    void uploadPartCleanupIsIdempotentWhenTempFileRemovedDuringUpload(@TempDir 
Path tmpDir)
+            throws IOException {
+        TempFileRemovingOperations helper = new TempFileRemovingOperations();
+        NativeS3RecoverableFsDataOutputStream stream =
+                new NativeS3RecoverableFsDataOutputStream(
+                        helper, KEY, UPLOAD_ID, tmpDir.toString(), 
MIN_PART_SIZE);
+
+        // Reaching minPartSize flushes the part; the upload succeeds but the 
file is gone.
+        byte[] payload = new byte[(int) MIN_PART_SIZE];
+        stream.write(payload, 0, payload.length);
+
+        assertThat(stream.closeForCommit()).isNotNull();
+        assertThat(tempFiles(tmpDir)).as("no temp file should remain after 
commit").isEmpty();

Review Comment:
   Right. `InMemoryNativeS3Operations` counts `uploadPart()` calls now, so the 
test can assert the upload really happened (`uploadPartAttempts == 1`) and the 
file is gone afterwards. It also checks that `write()` rotated to a fresh temp 
file, that the part still gets committed, and that the commit cleans up the 
rotated file too.



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