【问题标题】:Timeout waiting for connection from pool for S3 upload等待来自池的连接以进行 S3 上传超时
【发布时间】:2018-11-29 12:36:30
【问题描述】:

我正在尝试从 7 台机器上传大量文件。 在每台机器上,我运行 6 个线程上传到 S3 。 当我从一台机器上运行上传时它运行良好,但是当我在 7 台机器上运行时它开始失败。

我在其他机器上遇到错误。

错误 - AmazonClientException com.amazonaws.SdkClientException:无法执行 HTTP 请求:等待来自池的连接超时

我上传到 S3 的小文件总数 = 1659328

每个线程中的记录数 = 276554

所以我必须关闭 TransferManager 吗?如果是,那么我应该如何关闭它?我的应用程序多线程。当我调用tm.shutdownNow(); 时,其他线程将无法使用它。

这是我要上传到 S3 的代码。

AWSCredentials credential = new ProfileCredentialsProvider("skjfffkjg-Prod-ServiceUser").getCredentials();
        AmazonS3Client s3Client = (AmazonS3Client) AmazonS3ClientBuilder.standard().withRegion("us-east-1")
                .withCredentials(new AWSStaticCredentialsProvider(credential)).withForceGlobalBucketAccessEnabled(true)
                .build();

        s3Client.getClientConfiguration().setMaxConnections(100);

上传到S3方法

public void uploadToToS3() {
        _logger.info("Number of record to be processed in current thread: : " + records.size());


        TransferManager tm = new TransferManager(s3Client);

        MultipleFileUpload upload = tm.uploadFileList(bucketName, "", new File(fileLocation), records);

        if (upload.isDone() == false) {
            System.out.println("Transfer: " + upload.getDescription());
            System.out.println("  - State: " + upload.getState());
            System.out.println("  - Progress: " + upload.getProgress().getBytesTransferred());
        }
        try {
            upload.waitForCompletion();
        } catch (AmazonServiceException e1) {
            _logger.error("AmazonServiceException " + e1.toString());
        } catch (AmazonClientException e1) {
            _logger.error("AmazonClientException " + e1.toString());
        } catch (InterruptedException e1) {
            _logger.error("InterruptedException " + e1.toString());
        }
        System.out.println("Is Upload completed Successfully ="+upload.isDone());

        for (File file : records) {
            try {
                Files.delete(FileSystems.getDefault().getPath(file.getAbsolutePath()));
            } catch (IOException e) {
                _logger.error("IOException in file delete: " + e.toString());
                System.exit(1);
                _logger.error("IOException: " + e.toString());
            }
        }

        _logger.info("Calling Transfer manager shutdown");
        // tm.shutdownNow();
    }

我必须关闭任何东西才能顺利上传吗?

【问题讨论】:

  • 您是否验证了是否可以使用具有相同凭据的 CLI 上传?另外,请粘贴完整的堆栈跟踪。
  • @titogeo 是的凭据是正确的..我检查过..它在一台机器上工作..
  • @titogeo 只是一个问题..在我的情况下,我有关闭 Transfer Manger 吗..
  • 你好。你找到解决办法了吗?
  • 注意:我在从 AWS 检索单个文件时收到此消息。文件也不是很大。

标签: java amazon-web-services amazon-s3


【解决方案1】:

当您拥有 S3 对象时,您需要中止并关闭它 - 当您中止打开的连接或关闭文件阅读器等时。

因此,您需要确保您的对象请求已正确关闭。 顺便说一句,增加最大连接数可能不是解决此问题的最佳解决方案。

有一个允许中止 S3 操作的 AWS API。我认为您需要添加“finally”块以控制异常期间的上传。

这是我将使用的 sn-p:

public void uploadToToS3() {
        _logger.info("Number of record to be processed in current thread: : " + records.size());
    TransferManager tm = new TransferManager(s3Client);

    MultipleFileUpload upload = tm.uploadFileList(bucketName, "", new File(fileLocation), records);

    if (upload.isDone() == false) {
        System.out.println("Transfer: " + upload.getDescription());
        System.out.println("  - State: " + upload.getState());
        System.out.println("  - Progress: " + upload.getProgress().getBytesTransferred());
    }
    try {
        upload.waitForCompletion();
    } catch (AmazonServiceException e1) {
        _logger.error("AmazonServiceException " + e1.toString());
    } catch (AmazonClientException e1) {
        _logger.error("AmazonClientException " + e1.toString());
    } catch (InterruptedException e1) {
        _logger.error("InterruptedException " + e1.toString());
    } 

    // ************ THIS IS THE MODIFICATION ************ 
    finally {
        upload.getSubTransfers().forEach(s -> s.abort());
    }
    // *************************************************** 

    System.out.println("Is Upload completed Successfully ="+upload.isDone());
    for (File file : records) {
        try {
            Files.delete(FileSystems.getDefault().getPath(file.getAbsolutePath()));
        } catch (IOException e) {
            _logger.error("IOException in file delete: " + e.toString());
            System.exit(1);
            _logger.error("IOException: " + e.toString());
        }
    }

    _logger.info("Calling Transfer manager shutdown");
    // tm.shutdownNow();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 2022-09-25
    • 1970-01-01
    相关资源
    最近更新 更多