Skip to content

Add max bio size param - #205

Closed
siyengar wants to merge 1 commit into
netty:1.1.33from
siyengar:add_max_bio
Closed

siyengar wants to merge 1 commit into
netty:1.1.33from
siyengar:add_max_bio

Conversation

@siyengar

@siyengar siyengar commented Dec 6, 2016 •

Copy link
Copy Markdown

Add max bio size param

Motivation:

We observed that when the number of connections is very large
on a service, the machines started running out of memory.
On memory analysis the major consumer of memory was found
to be the bio allocation.

Modifications:

The default max bio size is 17K. This allows the parameter to be
tunable.

Result:

We've found this extremely useful for scaling large services. On
reducing the buffer size memory usage can be reduced signficantly. The
CPU usage goes up a bit when this is reduced so everyone find their
own sweet spot.

@normanmaurer

normanmaurer commented Dec 6, 2016 •

Copy link
Copy Markdown
Member

@normanmaurer normanmaurer self-assigned this Dec 6, 2016
@normanmaurer

Copy link
Copy Markdown
Member

@siyengar also do you use netty-tcnative as a standalone thing or with netty ?

Comment thread openssl-dynamic/src/main/c/ssl.c Outdated
// Make a BIO pair (network and internal) for the provided SSL * and return the network BIO
// Make a BIO pair (network and internal) for the provided SSL * with the default BIO size
// and return the network BIO
TCN_IMPLEMENT_CALL(jlong, SSL, makeNetworkBIO)(TCN_STDARGS,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just remove this method and always call makeNetworkBIO0

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do

Comment thread openssl-dynamic/src/main/c/ssl.c Outdated
return P2J(network_bio);
// Make a BIO pair (network and internal) for the provided SSL * with a max size and max size
// of max_bio_size and return the network BIO
TCN_IMPLEMENT_CALL(jlong, SSL, makeNetworkBIOWithSize)(TCN_STDARGS,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to makeNetworkBIO0

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

* @param maxBioSize The maximum size of the BIO.
* @return pointer to the Network BIO (BIO *)
*/
public static native long makeNetworkBIOWithSize(long ssl, int maxBioSize);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this method.

*
* @see #makeNetworkBIOWithSize(long, int)
*/
public static native long makeNetworkBIO(long ssl);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siyengar can you just rename this method to makeNetworkBIO0 and have it take maxBioSize . Then add static public methods to preserve old API:

Something like this:

public static long makeNetworkBIO(long ssl) {
    return makeNetworkBIO0(ssl, maxBioSize);
}

public static long makeNetworkBIO(long ssl, int maxBioSize) {
    return makeNetworkBIO0(ssl, maxBioSize);
}

private static native long makeNetworkBIO0(long ssl, int maxBioSize);

This way we can keep the changes / c code minimal. WDYT ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works great, will make this change

Comment thread openssl-dynamic/src/main/c/ssl.c Outdated
}

// max_bio_size = 0 is the default BIO size.
static jlong createNetworkBIO(JNIEnv *e, jlong ssl, jint max_bio_size) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see below (SSL.java) for comments :)

@siyengar siyengar Dec 6, 2016 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

@siyengar

siyengar commented Dec 6, 2016

Copy link
Copy Markdown
Author

@normanmaurer we do use netty-tcnative with netty, however we're still on an older version of netty so in some places we use netty-tcnative directly. The project is https://github.com/facebook/nifty which is thrift over netty.

will update the CLA and message

@normanmaurer

Copy link
Copy Markdown
Member

@siyengar I see... can you share what buffer size you usually use and if you are aware of a minimum that is required ?

@siyengar

siyengar commented Dec 6, 2016 •

Copy link
Copy Markdown
Author

@normanmaurer I'm still experimenting with the buffer size. After I found this problem, I first tried a buffer of 1K for read and write, and that reduced memory usage significantly, by > 3gigs since we have a large number connections on this service (and not a lot of free memory) in certain cases, however it increased CPU utilization by about 10% presumably because more ssl record encryption calls + syscalls calls need to be made for 1 application write. So we're still tuning this buffer w.r.t how much we can tolerate. Openssl and boringssl have no minimum size restriction on this buffer from reading their source.

@normanmaurer

Copy link
Copy Markdown
Member

@siyengar ok cool... let me know once you find the "sweet spot". About how many connections we are talking here on the box ?

Also say hi to @bmatheny and @andrewcox ;)

@siyengar

siyengar commented Dec 6, 2016

Copy link
Copy Markdown
Author

Ah ya, there are > 60K connections on average, which can spike sometimes, with a headroom of about 4Gigs. Openssl and boringssl do ephemeral allocations per write as well of 16K for read and write buffers (write_fragment_size), however after this change it doesn't seem to bite us much.

Will definitely say hi :), we're all enjoying using "netty" with wangle very much as well.

@siyengar

siyengar commented Dec 6, 2016

Copy link
Copy Markdown
Author

made the requested changes. Thanks for the reviews @normanmaurer

@bmatheny

bmatheny commented Dec 6, 2016

Copy link
Copy Markdown

Careful @normanmaurer, I stalk the netty PR's ;) Come visit next time you're here!

@normanmaurer

Copy link
Copy Markdown
Member

@bmatheny lol... I will for sure... March it is ;)

@siyengar

siyengar commented Dec 6, 2016 •

Copy link
Copy Markdown
Author

looks like CI failed during cmake of boringssl stable which this diff shouldn't affect. Also it passes for me locally just fine, but then again I'm building on a mac

@normanmaurer normanmaurer reopened this Dec 6, 2016
@normanmaurer

Copy link
Copy Markdown
Member

@siyengar I installed a new ci server today and it seems like I missed to install ninja. Just installed it, let us see if it pass now :)

@siyengar

siyengar commented Dec 6, 2016

Copy link
Copy Markdown
Author

Thanks @normanmaurer, would love a release with this change if everything passes. I can then update open-source nifty to use this API rather than relying on internal changes.

@normanmaurer

Copy link
Copy Markdown
Member

@siyengar thats the plan at the end ...

* @return pointer to the Network BIO (BIO *)
*/
public static native long makeNetworkBIO(long ssl);
public static native long makeNetworkBIO0(long ssl, int maxBioSize);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siyengar this should be a private method.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

@siyengar

siyengar commented Dec 6, 2016

Copy link
Copy Markdown
Author

updated with changes.

*/
public static native void freeSSL(long ssl);


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siyengar just one last thing before I merge ... Can you remove this empty line as there is already one . Thanks!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

Comment thread openssl-dynamic/src/main/c/ssl.c Outdated
// Make a BIO pair (network and internal) for the provided SSL * and return the network BIO
TCN_IMPLEMENT_CALL(jlong, SSL, makeNetworkBIO)(TCN_STDARGS,
jlong ssl /* SSL * */) {
// Make a BIO pair (network and internal) for the provided SSL * with a max size and max size

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this reads a bit strange: with a max size and max size. Can you rework this comment.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

@siyengar

siyengar commented Dec 6, 2016

Copy link
Copy Markdown
Author

addressed comments.

@siyengar
siyengar force-pushed the add_max_bio branch 2 times, most recently from 75b6675 to 9ec1ef4 Compare December 6, 2016 21:08
Comment thread openssl-dynamic/src/main/c/ssl.c Outdated
}

if (BIO_new_bio_pair(&internal_bio, 0, &network_bio, 0) != 1) {
if (BIO_new_bio_pair(&internal_bio, max_bio_size, &network_bio, max_bio_size) != 1) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an implicit cast of signed -> unsigned here which may cause warnings, which may be treated as errors depending on environment, on some compilers. Can you be explicit about the type conversion?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great catch. will do an explicit conversion here.

Motivation:

We observed that when the number of connections is very large
on a service, the machines started running out of memory.
On memory analysis the major consumer of memory was found
to be the bio allocation.

Modifications:

The default max bio size is 17K. This allows the parameter to be
tunable.

Result:

We've found this extremely useful for scaling large services. On
reducing the buffer size memory usage can be reduced signficantly. The
CPU usage goes up a bit when this is reduced so everyone find their
own sweet spot.
@siyengar

siyengar commented Dec 7, 2016

Copy link
Copy Markdown
Author

I didn't add another check that size_t size > jint size and making that assumption of the arch here. Does netty-tcnative run on any archs where that is not true?

@normanmaurer

Copy link
Copy Markdown
Member

That should be fine

@normanmaurer

Copy link
Copy Markdown
Member

@Scottmitch satisfied as well?

@Scottmitch Scottmitch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm ... thanks @siyengar !

@Scottmitch Scottmitch added this to the 1.1.33.Fork24 milestone Dec 7, 2016
@Scottmitch

Copy link
Copy Markdown
Member

1.1.33 (e691680) master (3a3a1e5)

@Scottmitch Scottmitch closed this Dec 7, 2016
@siyengar

siyengar commented Dec 8, 2016

Copy link
Copy Markdown
Author

@normanmaurer do you have an estimate on when Fork24 will be released?

@normanmaurer

Copy link
Copy Markdown
Member

@siyengar tomorrow 👍

@siyengar

Copy link
Copy Markdown
Author

we finally ended up using a 2k buffer which seems to work fine.

We're now toying with the idea of deallocating and allocating the buffer after each write.

@Scottmitch

Copy link
Copy Markdown
Member

@siyengar - you may be interested in #216. I expect this can be used to reduce memory even further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants