S3 TransferManager hangs forever
Hi all,
When downloading a "directory" from s3 with 10 objects, each object can be up to 10GB, the TransferManager hangs indefinitely. This problem happens sporadically, but I'll try to make it reproducible. AWS SDK Version: 1.10.2 Java: Oracle java 1.8.0_31 OS: CentOS 6.5
Here's a snippet using TransferManager and downloadDirectory that wil eventually hang if left running long enough
public static void main(String [] args) {
String bucket = "mybucket";
String key = "dataDir";
File dest = new File("/tmp/data123");
DefaultAWSCredentialsProviderChain credentialProviderChain = new DefaultAWSCredentialsProviderChain();
TransferManager tx = new TransferManager(
credentialProviderChain.getCredentials());
while(true) {
boolean success = false;
Transfer transfer;
while ( !success ) {
try {
//Keep trying until we success
if (dest.exists()) {
dest.delete();
}
transfer = tx.downloadDirectory(bucket, key, dest);
transfer.waitForCompletion();
success = true;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
It might take a day or two running this snippet, but eventually it will kick in
To fix the issue, I replaced waitForCompletion() by own ProgressListener that counts number of bytes transfered and if within a minute where the transfer state is still IN_PROGRESS and the number of bytes transfered remained constant then an exception is thrown.
Doing a jstack on the hanging process, showed that hanging is happening at latch.wait in MultipleFileTransferStateChangeListener.java exactly at the pointed line.
Hey Moataz, thanks for the report! Taking a look now.
I haven't been able to reproduce this yet, but that sounds like it's expected. Almost certainly some kind of race condition. I'll keep it running and see if I get a hit.
I see two main possibilities:
- TransferManager.downloadDirectory is somehow synchronously calling through to MFTSCL.transferStateChanged before calling latch.countDown. A pretty classic deadlock: the calling thread is blocked waiting for a call to countDown that's never coming. Is downloadDirectory on the stack trace you're seeing for the blocked thread? If so, can you post it here? I don't see any obvious way that a synchronous call to transferStateChanged could be happening, but the relevant code is very complicated so I wouldn't be 100% surprised.
- TransferManager.downloadDirectory is exiting without calling latch.countDown, presumably via an exception? You're catching the exception and looping around, but any individual downloads that were started before the exception was thrown aren't cancelled, and will eventually try to call MFTSCL.transferStateChanged and hang waiting for a countDown that's never coming. Eventually all of the background threads allocated to the TransferManager are tied up in this manner, and submitting a new downloadDirectory request just queues 10 downloads that are stuck waiting for an available thread. Do you see any output from that
e.printStackTrace()line?
Hi David,
Thanks for the reply
As far as I remember besides the hanging process, I was getting InterruptExceptions from time to time, that's why I built in those retries
I'll try to reproduce the issue and attach the jstack/stacktrace Do you need the request IDs as well?
Cheers, Moataz
Just a quick (no)update. The process has been running successfully for the past 3 days or so. I'll try to get the information needed as soon as it hangs
Hm, downloadDirectory shouldn't be throwing InterruptedException. waitForCompletion can (though I'm not sure what would be interrupting your main thread?), but that shouldn't cause anything to hang as far as I can see. I'd love to take a look at a stack trace if you can get one. Request ID's shouldn't be necessary, this seems like a purely client-side issue.
Any updates on the stack trace ?
Hi manikandanrs
The issue is not happening anymore on a recent version of the sdk
Thanks. Glad the issue is resolved.
I see the same issue on aws-java-sdk-s3:1.11.327 with Java 11.0.2+9-LTS.
All threads wait for the countdownLatch forever. Was there a fix done for this or it just stopped happening?
I am using this to initializer the transferManager:
transferManagerPool = Executors.newFixedThreadPool(10);
int timeoutConnection = 500_000;
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setMaxErrorRetry(3);
clientConfiguration.setConnectionTimeout(timeoutConnection);
clientConfiguration.setSocketTimeout(timeoutConnection);
AmazonS3 s3client = AmazonS3ClientBuilder.standard()
.withClientConfiguration(clientConfiguration)
.build();
transferManager = TransferManagerBuilder.standard().withS3Client(s3client)
.withExecutorFactory(() -> transferManagerPool)
.build();
the stack for all the 10 threads is this:
"pool-1-thread-10" #139 prio=5 os_prio=0 cpu=34656.51ms elapsed=140733.58s tid=0x00007f2284010000 nid=0x15472 waiting on condition [0x00007f223a06d000]
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park([email protected]/Native Method)
- parking to wait for <0x00007f286b5c7d80> (a java.util.concurrent.CountDownLatch$Sync)
at java.util.concurrent.locks.LockSupport.park([email protected]/LockSupport.java:194)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt([email protected]/AbstractQueuedSynchronizer.java:885)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly([email protected]/AbstractQueuedSynchronizer.java:1039)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly([email protected]/AbstractQueuedSynchronizer.java:1345)
at java.util.concurrent.CountDownLatch.await([email protected]/CountDownLatch.java:232)
at com.amazonaws.services.s3.transfer.MultipleFileTransferStateChangeListener.transferStateChanged(MultipleFileTransferStateChangeListener.java:40)
at com.amazonaws.services.s3.transfer.internal.AbstractTransfer.setState(AbstractTransfer.java:165)
at com.amazonaws.services.s3.transfer.internal.DownloadImpl.setState(DownloadImpl.java:164)
at com.amazonaws.services.s3.transfer.DownloadCallable.call(DownloadCallable.java:129)
at com.amazonaws.services.s3.transfer.DownloadCallable.call(DownloadCallable.java:53)
at java.util.concurrent.FutureTask.run([email protected]/FutureTask.java:264)
at java.util.concurrent.ThreadPoolExecutor.runWorker([email protected]/ThreadPoolExecutor.java:1128)
at java.util.concurrent.ThreadPoolExecutor$Worker.run([email protected]/ThreadPoolExecutor.java:628)
at java.lang.Thread.run([email protected]/Thread.java:834)
Edit: There was a a failure that happened before that may have caused this:
com.amazonaws.SdkClientException: Unable to execute HTTP request: The target server failed to respond
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleRetryableException(AmazonHttpClient.java:1116)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1066)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:743)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:717)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:667)
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4365)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4312)
at com.amazonaws.services.s3.AmazonS3Client.getObjectMetadata(AmazonS3Client.java:1270)
at com.amazonaws.services.s3.transfer.TransferManager.doDownload(TransferManager.java:1050)
at com.amazonaws.services.s3.transfer.TransferManager.downloadDirectory(TransferManager.java:1284)
at com.amazonaws.services.s3.transfer.TransferManager.downloadDirectory(TransferManager.java:1152)