MartijnVisser commented on code in PR #29890:
URL: https://github.com/apache/flink/pull/29890#discussion_r4679395905
##########
flink-filesystems/flink-s3-fs-native/src/main/java/org/apache/flink/fs/s3native/NativeS3FileSystem.java:
##########
@@ -543,7 +552,37 @@ public RecoverableWriter createRecoverableWriter() throws
IOException {
throw new UnsupportedOperationException("Recoverable writer not
available");
}
return NativeS3RecoverableWriter.writer(
- s3AccessHelper, localTmpDir, s3uploadPartSize,
maxConcurrentUploadsPerStream);
+ s3AccessHelper,
+ uploadThreadPool,
+ localTmpDir,
+ s3uploadPartSize,
+ maxConcurrentUploadsPerStream);
+ }
+
+ /**
+ * Creates the pool for the part uploads of the recoverable writer. Its
threads are started by
+ * the task threads that write to the streams and would inherit their
context class loader,
+ * which is the user code class loader of the job. The pool lives as long
as the file system,
+ * which outlives the job, so its threads use the class loader of the file
system instead.
+ * Otherwise they would keep the class loader of a finished job reachable.
+ */
+ @VisibleForTesting
+ static ExecutorService createUploadThreadPool() {
+ final ThreadFactory threadFactory =
+ new ExecutorThreadFactory(
+ "s3-native-part-upload",
+ (thread, error) ->
+ LOG.error(
+ "Uncaught exception in S3 part upload
thread {}",
+ thread.getName(),
+ error));
+ final ClassLoader classLoader =
NativeS3FileSystem.class.getClassLoader();
+ return Executors.newCachedThreadPool(
Review Comment:
Uploads can now take every connection, so other requests to the same bucket
wait: checkpoint writes took up to 39 s, against 9 s on master. I think the
pool needs headroom, at half it stayed at 10 s.
##########
flink-filesystems/flink-s3-fs-native/src/main/java/org/apache/flink/fs/s3native/writer/NativeS3RecoverableFsDataOutputStream.java:
##########
@@ -180,41 +209,153 @@ public void write(byte[] b, int off, int len) throws
IOException {
if (currentPartSize >= minPartSize) {
uploadCurrentPart();
- createNewTempFile();
+ startNewPart();
}
}
@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();
+ final IOException failure = uploadFailure.get();
+ if (failure != null) {
+ // Rethrown as it is, so that the task handles a fatal error or an
OutOfMemoryError
+ // of an upload thread like one of its own thread, e.g. by
terminating the JVM.
+ ExceptionUtils.rethrowIfFatalErrorOrOOM(failure.getCause());
+ throw new IOException(
+ "A part upload of this stream failed. The stream cannot be
used anymore and "
+ + "must be recovered from the last checkpoint.",
+ failure);
+ }
}
private void uploadCurrentPart() throws IOException {
- currentOutputStream.close();
+ final PendingPartUpload upload;
+ lock();
+ try {
+ // close() may have run concurrently and released the buffer.
+ checkUsable();
+ currentOutputStream.close();
+
+ upload = new PendingPartUpload(nextPartNumber, currentTempFile,
currentPartSize);
+ pendingUploads.addLast(upload);
+ nextPartNumber++;
+ numBytesInParts += currentPartSize;
+ currentOutputStream = null;
+ currentFileStream = null;
+ currentTempFile = null;
+ currentPartSize = 0;
+ } finally {
+ unlock();
+ }
- // 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);
+ // Blocks while the maximum number of uploads is in flight. Called
from write(), this does
+ // not hold the lock, so close() is not blocked by back-pressure.
+ try {
+ uploadExecutor.execute(() -> uploadPart(upload));
+ } catch (Throwable t) {
+ // E.g. a RejectedExecutionException once the file system is
closed, or an
+ // OutOfMemoryError if no thread can be created.
+ final IOException failure = failUpload(upload, t);
+ ExceptionUtils.rethrowIfFatalErrorOrOOM(t);
+ throw failure;
+ }
+ completeFinishedUploads();
+ }
- nextPartNumber++;
- completedParts.add(new PartETag(result.getPartNumber(),
result.getETag()));
- numBytesInParts += currentPartSize;
+ private void startNewPart() throws IOException {
+ lock();
+ try {
+ // close() may have run while uploadCurrentPart() waited for a
free upload slot.
+ checkUsable();
+ createNewTempFile();
+ } finally {
+ unlock();
+ }
+ }
- Files.deleteIfExists(currentTempFile.toPath());
+ private void uploadPart(PendingPartUpload upload) {
+ try {
+ if (closed) {
Review Comment:
`close()` also waits for this stream's parts that are still queued behind
other streams, which `uploadPart` skips anyway. That took 54 s here; completing
unstarted parts in `close()` itself brought it to 9 s.
--
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]