Samrat002 commented on code in PR #27187: URL: https://github.com/apache/flink/pull/27187#discussion_r2809004625
########## flink-filesystems/flink-s3-fs-native/src/main/java/org/apache/flink/fs/s3native/NativeS3BulkCopyHelper.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.annotation.Internal; +import org.apache.flink.core.fs.ICloseableRegistry; +import org.apache.flink.core.fs.PathsCopyingFileSystem; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import software.amazon.awssdk.transfer.s3.S3TransferManager; +import software.amazon.awssdk.transfer.s3.model.CompletedCopy; +import software.amazon.awssdk.transfer.s3.model.DownloadFileRequest; +import software.amazon.awssdk.transfer.s3.model.FileDownload; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +@Internal +public class NativeS3BulkCopyHelper { + + private static final Logger LOG = LoggerFactory.getLogger(NativeS3BulkCopyHelper.class); + + private final S3TransferManager transferManager; + private final int maxConcurrentCopies; + + public NativeS3BulkCopyHelper(S3TransferManager transferManager, int maxConcurrentCopies) { + this.transferManager = transferManager; + this.maxConcurrentCopies = maxConcurrentCopies; + } + + public void copyFiles( + List<PathsCopyingFileSystem.CopyRequest> requests, ICloseableRegistry closeableRegistry) + throws IOException { + + if (requests.isEmpty()) { + return; + } + + LOG.info("Starting bulk copy of {} files using S3TransferManager", requests.size()); + + List<CompletableFuture<CompletedCopy>> copyFutures = new ArrayList<>(); + + for (int i = 0; i < requests.size(); i++) { + PathsCopyingFileSystem.CopyRequest request = requests.get(i); + String sourceUri = request.getSource().toUri().toString(); + if (sourceUri.startsWith("s3://") || sourceUri.startsWith("s3a://")) { + copyS3ToLocal(request, copyFutures); + } else { + throw new UnsupportedOperationException( + "Only S3 to local copies are currently supported: " + sourceUri); + } + + if (copyFutures.size() >= maxConcurrentCopies || i == requests.size() - 1) { + waitForCopies(copyFutures); + copyFutures.clear(); + } + } + + LOG.info("Completed bulk copy of {} files", requests.size()); + } + + private void copyS3ToLocal( + PathsCopyingFileSystem.CopyRequest request, + List<CompletableFuture<CompletedCopy>> copyFutures) + throws IOException { + + String sourceUri = request.getSource().toUri().toString(); + String bucket = extractBucket(sourceUri); + String key = extractKey(sourceUri); + File destFile = new File(request.getDestination().getPath()); + + Files.createDirectories(destFile.getParentFile().toPath()); + + DownloadFileRequest downloadRequest = + DownloadFileRequest.builder() + .getObjectRequest(req -> req.bucket(bucket).key(key)) + .destination(destFile.toPath()) + .build(); + + FileDownload download = transferManager.downloadFile(downloadRequest); + + CompletableFuture<CompletedCopy> future = + download.completionFuture() + .thenApply( + completed -> { + LOG.debug("Successfully copied {} to {}", sourceUri, destFile); + return null; + }); + + copyFutures.add(future); + } + + private void waitForCopies(List<CompletableFuture<CompletedCopy>> futures) throws IOException { Review Comment: Both are handled by design, at the appropriate layers: > Do we need any cleanup on failures? Per the PathsCopyingFileSystem.copyFiles contract ([here](https://nightlies.apache.org/flink/flink-docs-release-2.0-preview1/api/java/org/apache/flink/core/fs/PathsCopyingFileSystem.html)), the caller is responsible for cleanup: ```In case of an exception some files might have been already copied fully or partially. Caller should clean this up.``` This is intentional — the caller (state restore logic) knows the destination directory structure and can delete the entire restore directory on failure. Having the bulk copy helper attempt partial cleanup would be fragile (which files succeeded? which are partial?) and duplicates responsibility that the caller already handles. > How do we handle bulk copy retry? 1. S3 TransferManager retries transient HTTP errors (5xx, throttling, connection resets) internally using the SDK's configurable retry policy (default: 3 retries with exponential backoff) 2. Flink task restart — if copyFiles throws after the SDK retries are exhausted, the task fails and Flink's restart strategy re-triggers the entire state restore, which calls copyFiles again with a fresh set of requests Adding retry at the NativeS3BulkCopyHelper level would be a redundant middle layer between these two. let me know if you think otherwise -- 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]
