aws-sdk-java icon indicating copy to clipboard operation
aws-sdk-java copied to clipboard

High CPU usage in fillBuffer

Open kennethjor opened this issue 10 years ago • 9 comments

Hi guys. I've been using the AWS SDK in an application to communicate with DynamoDB, Kinesis, SQS, SNS (in order of load) and CPU sampling via VisualVM has revealed about 70-80% of the total CPU time is spent in org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(). I don't use Apache HTTP Components for any other part of the application, so this can only come from the AWS SDK. I'm primarily using the async APIs wherever possible, except for DynamoDBMapper for which I've created a custom API, very similar to the async classes in the SDK itself. Is this something you've seen before and do you have any tips for dealing with it?

In reading the source code, I see the async APIs are implemented using thread pools which just wait for synchronous requests to complete. In trying to combat the problem, I've spent some time retrofitting the SDK to run on top of Netty rather than Apache HTTP. This essentially makes the API "asynchronous-first" and allows it to make use of the NIO libraries, leading to less CPU under heavy load.

So far the results have been good, though too early to give out hard numbers. Going forward I would like to make my efforts available to the public somehow. What would you suggest as the best route to approach this? Should I simply create a fork and start hacking away? Do you have any internal development going in this direction? I know you guys make use of a lot of auto-generated code, and I'd rather not switch all of it over by hand.

Thanks for a nicely well engineered SDK.

kennethjor avatar Dec 10 '15 16:12 kennethjor

Hey Kenneth,

Thanks for reaching out to us! The request for async I/O in the SDK has come up quite a bit recently and is definitely something we have on our radar. Other customers have reported similar performance bottlenecks in high RPS situations. That said, if you can share more about your runtime environment and client configuration, including what version of the SDK you're using, whether you're overriding the underlying HTTP client version, request and socket timeouts, max connections, etc, that would be helpful. Also, what type of requests you're making specifically when you see these CPU spikes? Do you see the same issue when only making calls to a single AWS service? I'd like to try to reproduce what you're seeing to see if there are any issues on our end or if there's any room for optimization.

Outside of that, it's awesome that you've gone ahead and retrofitted the SDK over Netty and we would certainly love to hear about your approach and any results as you get them. As you mentioned, the vast majority of our client code is generated so any long term solution we develop would have to be incorporated into our code generation logic. Creating a fork of any clients you're working on and periodically merging in changes as bug fixes and service updates are added would probably be the best way to experiment with these changes in the short term. Longer term this is definitely something we want to officially support but as of yet we do not have a timeframe on when this will occur. Any data you can provide, both to show the performance pain you are seeing with the current client and benefit you're seeing on the Netty implementation, would help make the case to prioritize the work.

breedloj avatar Dec 15 '15 01:12 breedloj

Hi Jonathan,

Thank you for your reply. My application primarily uses DynamoDB and Kinesis, both of which exhibit this behaviour. I'm not talking about spikes though, I'm consistent seeing the majority of the CPU go to the fillBuffer() method. I have also verified that it is indeed the DynamoDB and Kinesis threads. The request rates are in the 3-4k per second per server for both DDB and Kinesis, and I'd very much like to increase this, hence why effort is being put towards switching out the HTTP layer.

Using hte stock SDK, I'm using the following basic ClientConfiguration:

new ClientConfiguration()
    .withConnectionTTL(600000)
    .withConnectionTimeout(1000)
    .withGzip(false)
    .withMaxConnections(64)
    .withMaxErrorRetry(10)
    .withReaper(false)
    .withTcpKeepAlive(true)
    .withRetryPolicy(new RetryPolicy(
        PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION,
        RetryPolicy.BackoffStrategy.NO_DELAY,
        config.getMaxErrorRetry(),
        true
    ));

For DDB it's modified slightly:

config
    .withRetryPolicy(PredefinedRetryPolicies.NO_RETRY_POLICY)
    .withMaxConnections(config.dynamoDb.maxConnections)
    .withMaxErrorRetry(config.dynamoDb.maxRetry)
    .withProtocol(Protocol.HTTP);

Both DDB and Kinesis also have an externally configured maxConnections value of 128 which is brought in to override the default. Besides this everything is pretty standard. The requests performed on DDB are primarily simple GetItem and PutItem, with reads in the majority. Kinesis is all PutRecord requests.

I'm still debugging the Netty system, trying to make it works properly. Though I'm very happy to hear you'd be interested in evaluating something like this for the future. Once I make it work properly I'll share some numbers and perhaps I can look at forking the SDK and we can see what you guys think.

Thank you.

kennethjor avatar Dec 15 '15 03:12 kennethjor

@kennethjor Hi! I was wondering if I could take a look at your netty aws sdk implementation.

lukiano avatar May 13 '16 04:05 lukiano

Closing this for now but we'd love to hear about any subsequent findings you've made. Feel free to re-open or create a new issue.

breedloj avatar Jul 06 '16 21:07 breedloj

BTW the 1.11 version of the Kinesis client uses a binary protocol that performs significantly better.

shorea avatar Jul 06 '16 21:07 shorea

@breedloj @shorea do either of you have the CBOR format working with the latest versions of the KCL and the Java SDK?

On amazon-kinesis-client 1.6.3 and AWS Java SDK 1.11.14 we're getting:

java.lang.NoClassDefFoundError: com/amazonaws/util/json/JSONObject

because of Lease.java.

We have an open support ticket, but they told us that it will be fixed in the next version of KCL... and that was over a month ago. Just wondering if you have a recommended way to use the CBOR format that actually works.

bcarl avatar Jul 07 '16 20:07 bcarl

The new version of KCL that was just updated fixes this

https://github.com/awslabs/amazon-kinesis-client/commit/dd14bac4e3f26ac9ea95c0a6f3ed72b56d69b4e1

manikandanrs avatar Jul 07 '16 20:07 manikandanrs

@manikandanrs 🎉 🎉 🎉 Thanks!

bcarl avatar Jul 07 '16 21:07 bcarl

We just launched a Developer Preview of AWS SDK for Java 2.0 that adds support for this feature request. Try it out and let us know what you think!

https://github.com/aws/aws-sdk-java-v2 http://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/welcome.html https://aws.amazon.com/blogs/developer/aws-sdk-for-java-2-0-developer-preview/

shorea avatar Jun 28 '17 20:06 shorea