Samrat002 commented on code in PR #27187: URL: https://github.com/apache/flink/pull/27187#discussion_r2919964481
########## flink-filesystems/flink-s3-fs-native/src/main/java/org/apache/flink/fs/s3native/NativeS3OutputStream.java: ########## @@ -0,0 +1,149 @@ +/* + * 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; + +import org.apache.flink.core.fs.FSDataOutputStream; + +import software.amazon.awssdk.core.sync.RequestBody; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.PutObjectRequest; + +import javax.annotation.Nullable; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.util.UUID; + +public class NativeS3OutputStream extends FSDataOutputStream { + + private final S3Client s3Client; + private final String bucketName; + private final String key; + private final File tmpFile; + private final FileOutputStream localStream; + private final S3EncryptionConfig encryptionConfig; + + private long position; + private volatile boolean closed; + + public NativeS3OutputStream( + S3Client s3Client, String bucketName, String key, String localTmpDir) + throws IOException { + this(s3Client, bucketName, key, localTmpDir, null); + } + + public NativeS3OutputStream( + S3Client s3Client, + String bucketName, + String key, + String localTmpDir, + @Nullable S3EncryptionConfig encryptionConfig) + throws IOException { + this.s3Client = s3Client; + this.bucketName = bucketName; + this.key = key; + this.encryptionConfig = + encryptionConfig != null ? encryptionConfig : S3EncryptionConfig.none(); + + File tmpDir = new File(localTmpDir); + if (!tmpDir.exists()) { + tmpDir.mkdirs(); + } + + this.tmpFile = new File(tmpDir, "s3-upload-" + UUID.randomUUID()); + this.localStream = new FileOutputStream(tmpFile); + this.position = 0; + this.closed = false; + } + + @Override + public long getPos() throws IOException { + return position; + } + + @Override + public void write(int b) throws IOException { + if (closed) { + throw new IOException("Stream is closed"); + } + localStream.write(b); + position++; + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + if (closed) { + throw new IOException("Stream is closed"); + } + if (b == null) { + throw new NullPointerException(); + } + if (off < 0 || len < 0 || len > b.length - off) { + throw new IndexOutOfBoundsException(); + } + + localStream.write(b, off, len); + position += len; + } + + @Override + public void flush() throws IOException { + if (!closed) { + localStream.flush(); + } + } + + @Override + public void sync() throws IOException { Review Comment: > I think it sounds good to me. We were also discussing options for not using temp files. Since we are using SDK V2, is there any reason not to stream directly https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/best-practices-s3-uploads.html? While AWS SDK v2 does support streaming, there are several hard constraints where the temp file approach is suitable. Below are my 2 cent why 1. We don't know the data size upfront. Flink's RecoverableWriter is used for unbounded streams. we can't tell if we're writing 1MB or 10GB when we start. The AWS SDK v2 streaming guide assumes you have a known Content-Length before uploading. If you don't, it buffers in memory anyway to compute it. So we haven't solved the problem, just hiding it. 2. Memory safety is the real problem we're solving. TaskManagers have fixed heap memory shared across multiple tasks. A single unbounded byte stream can eat up GBs of heap and kill the entire TaskManager. By writing to disk first, we let the OS handle it via swap if needed. Checkpoint state can be 100s of MB to GBs; we can't bet that heap will absorb it. 3. Multipart uploads still need buffering. S3 requires 5MB minimum parts. Direct streaming without local buffering just pushes the problem to the SDK—it still needs to collect 5MB chunks before uploading each part. 4. Exactly-once recovery requires staging. We write to a temp file first, then upload on success. If the job fails mid-upload, we delete the temp file and restart. Direct streaming breaks this two-phase commit model. -- 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]
