gaborgsomogyi commented on code in PR #28360: URL: https://github.com/apache/flink/pull/28360#discussion_r3811822233
########## 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. + */ Review Comment: The function names are correct so such comments are not adding anything so we can remove this and all similar in this file ########## 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: I'm not satisfied with this test and this applies to other similar pattern too. The reason is that the test claims that `closeForCommit` deletes files. What I've already highlighted in other place we we must ensure that. The actual stand is that - if I comment out file writing - if the writer writes files like `foo-bar-...` and nobody deletes it then the test still pass. TLDR: when we say that files are deleted then - we must ensure that there are such files - action - we must ensure files deleted ########## 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(); + } + + /** + * {@code close()} succeeds even when the temp file vanishes after being observed to exist: the + * injected tail file reports a stale {@code exists()}, pinning the check-then-act window. + */ + @Test + void closeIsIdempotentWhenTempFileMissing(@TempDir Path tmpDir) throws IOException { + File realFile = new File(tmpDir.toFile(), "s3-part-recovered-tail"); + assertThat(realFile.createNewFile()).isTrue(); + File staleExistsFile = + new File(realFile.getPath()) { + @Override + public boolean exists() { + return true; + } + }; + NativeS3RecoverableFsDataOutputStream stream = + new NativeS3RecoverableFsDataOutputStream( + new NoopObjectOperations(), + KEY, + UPLOAD_ID, + tmpDir.toString(), + MIN_PART_SIZE, + Collections.emptyList(), + 0L, + staleExistsFile); + + Files.delete(realFile.toPath()); Review Comment: Here we're fine because delete is explicit enough. Here the assertion is that delete not thrown ########## 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: Same precondition check. IIUC here the diff is that file must not exist. I see the test uses `TempFileRemovingOperations` but `delete` inside `uploadPart` may or may not be called. ########## 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 Review Comment: I think this has a special meaning and there is already a define for this so duplication is meaningless, right? -- 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]
