Copilot commented on code in PR #29890:
URL: https://github.com/apache/flink/pull/29890#discussion_r4579143919
##########
flink-filesystems/flink-s3-fs-native/src/main/java/org/apache/flink/fs/s3native/NativeS3FileSystem.java:
##########
@@ -553,6 +571,7 @@ public CompletableFuture<Void> closeAsync() {
}
LOG.info("Starting async close of Native S3 FileSystem for bucket:
{}", bucketName);
+ uploadThreadPool.shutdown();
Review Comment:
shutdown() only rejects new submissions; it does not wait for running
uploads. The following future immediately closes clientProvider (including the
synchronous S3 client used by these workers), and closeAsync() can report
completion while upload workers still hold local files and execute against that
closed client. Please include bounded executor termination in the asynchronous
close sequence before closing the client, with an explicit cancellation/cleanup
path if the timeout expires.
##########
flink-filesystems/flink-s3-fs-native/src/main/java/org/apache/flink/fs/s3native/writer/NativeS3RecoverableFsDataOutputStream.java:
##########
@@ -186,35 +207,97 @@ public void write(byte[] b, int off, int len) throws
IOException {
@Override
public void flush() throws IOException {
- if (closed) {
- throw new IOException("Stream is closed");
- }
+ checkUsable();
currentOutputStream.flush();
}
@Override
public void sync() throws IOException {
+ checkUsable();
+ persist();
+ }
+
+ private void checkUsable() throws IOException {
if (closed) {
throw new IOException("Stream is closed");
}
- persist();
+ if (uploadFailure != null) {
+ throw new IOException(
+ "A part upload of this stream failed. The stream cannot be
used anymore and "
+ + "must be recovered from the last checkpoint.",
+ uploadFailure);
+ }
}
private void uploadCurrentPart() throws IOException {
currentOutputStream.close();
- // Do not delete the temp file if uploadPart fails: propagate the
original exception
- // unmasked and let the cleanup path (close() or the closeForCommit()
failure handler)
- // delete it. nextPartNumber is only advanced on success.
- NativeS3ObjectOperations.UploadPartResult result =
- s3AccessHelper.uploadPart(
- key, uploadId, nextPartNumber, currentTempFile,
currentPartSize);
-
+ final PendingPartUpload upload =
+ new PendingPartUpload(nextPartNumber, currentTempFile,
currentPartSize);
+ pendingUploads.addLast(upload);
nextPartNumber++;
- completedParts.add(new PartETag(result.getPartNumber(),
result.getETag()));
numBytesInParts += currentPartSize;
+ currentOutputStream = null;
+ currentFileStream = null;
+ currentTempFile = null;
+ currentPartSize = 0;
- Files.deleteIfExists(currentTempFile.toPath());
+ try {
+ uploadExecutor.execute(() -> uploadPart(upload));
+ } catch (RuntimeException e) {
+ upload.future.completeExceptionally(e);
+ }
+ completeFinishedUploads();
+ }
+
+ private void uploadPart(PendingPartUpload upload) {
+ try {
+ final NativeS3ObjectOperations.UploadPartResult result =
+ s3AccessHelper.uploadPart(
+ key, uploadId, upload.partNumber, upload.file,
upload.length);
+ Files.deleteIfExists(upload.file.toPath());
+ upload.future.complete(new PartETag(result.getPartNumber(),
result.getETag()));
+ } catch (Throwable t) {
+ upload.future.completeExceptionally(t);
Review Comment:
If part 2 fails while part 1 is still running, `uploadFailure` stays unset
because failures are only consumed in part order, so subsequent writes keep
accepting data and submitting uploads. Failed uploads release their permits but
retain their temp files, allowing failed files to accumulate beyond the
per-stream concurrency limit. Publish the first failure thread-safely from both
upload and submission failure paths, and check it before accepting or
submitting more data independently of ordered ETag collection.
##########
flink-filesystems/flink-s3-fs-native/src/main/java/org/apache/flink/fs/s3native/writer/NativeS3RecoverableFsDataOutputStream.java:
##########
@@ -327,11 +416,30 @@ private void releaseLocalResources() throws IOException {
collected = ExceptionUtils.firstOrSuppressed(e, collected);
}
}
+ for (PendingPartUpload upload : pendingUploads) {
+ if (upload.future.isDone()) {
+ try {
+ deleteTempFile(upload.file);
+ } catch (IOException e) {
+ collected = ExceptionUtils.firstOrSuppressed(e, collected);
+ }
+ } else {
+ upload.future.whenComplete((ignored, error) ->
deleteTempFileQuietly(upload.file));
Review Comment:
An upload left running by `close()` can finish after recovery has reused the
same upload ID and part number, overwriting the recovered part and invalidating
its recorded ETag. Ensure requests from the previous attempt have finished, or
isolate attempts, before reusing those parts; cancelling only the future does
not stop an S3 request. Add a recovery test that delays an old post-checkpoint
upload until after the resumed attempt uploads different content, and validate
content-dependent ETags.
##########
flink-filesystems/flink-s3-fs-native/src/main/java/org/apache/flink/fs/s3native/writer/NativeS3RecoverableFsDataOutputStream.java:
##########
@@ -186,35 +207,97 @@ public void write(byte[] b, int off, int len) throws
IOException {
@Override
public void flush() throws IOException {
- if (closed) {
- throw new IOException("Stream is closed");
- }
+ checkUsable();
currentOutputStream.flush();
}
@Override
public void sync() throws IOException {
+ checkUsable();
+ persist();
+ }
+
+ private void checkUsable() throws IOException {
if (closed) {
throw new IOException("Stream is closed");
}
- persist();
+ if (uploadFailure != null) {
+ throw new IOException(
+ "A part upload of this stream failed. The stream cannot be
used anymore and "
+ + "must be recovered from the last checkpoint.",
+ uploadFailure);
+ }
}
private void uploadCurrentPart() throws IOException {
currentOutputStream.close();
- // Do not delete the temp file if uploadPart fails: propagate the
original exception
- // unmasked and let the cleanup path (close() or the closeForCommit()
failure handler)
- // delete it. nextPartNumber is only advanced on success.
- NativeS3ObjectOperations.UploadPartResult result =
- s3AccessHelper.uploadPart(
- key, uploadId, nextPartNumber, currentTempFile,
currentPartSize);
-
+ final PendingPartUpload upload =
+ new PendingPartUpload(nextPartNumber, currentTempFile,
currentPartSize);
+ pendingUploads.addLast(upload);
nextPartNumber++;
- completedParts.add(new PartETag(result.getPartNumber(),
result.getETag()));
numBytesInParts += currentPartSize;
+ currentOutputStream = null;
+ currentFileStream = null;
+ currentTempFile = null;
+ currentPartSize = 0;
- Files.deleteIfExists(currentTempFile.toPath());
+ try {
+ uploadExecutor.execute(() -> uploadPart(upload));
Review Comment:
`BackPressuringExecutor.execute()` can block after the buffer fields are
cleared, while another thread calls `close()`. When submission resumes, either
`write()` overload can create a new buffer on the closed stream, leaking an
open file because later `close()` calls skip cleanup. Coordinate buffer handoff
and rotation with `close()` without blocking cancellation, recheck the closed
state before allocation, and test closing a stream while a write is
back-pressured.
--
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]