Samrat002 commented on code in PR #27187:
URL: https://github.com/apache/flink/pull/27187#discussion_r2919837705


##########
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);

Review Comment:
   The choice is intentional for memory safety on unbounded writes.
   
   `NativeS3OutputStream` is returned by `FileSystem.create()`. The caller 
controls how much data flows through. Checkpoint state files, bulk copy 
targets, and sink output can be hundreds of MBs to GBs. A 
`ByteArrayOutputStream` would hold all of that on JVM heap, risking OOM in 
TaskManagers that already operate under tight managed memory budgets.
   
   A temp file lets the OS handle the buffering only the page cache is in 
memory, and the kernel can evict it under pressure. The JVM footprint stays 
constant regardless of write volume.
   
   The pre-existing `S3RecoverableFsDataOutputStream` in flink-s3-fs-base 
follows the same pattern — it uses RefCountedBufferingFileStream (a temp file) 
rather than in-memory buffers, for the same reason.



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