-
Notifications
You must be signed in to change notification settings - Fork 4k
ALTS: release handshaker channel if no longer needed #5210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ALTS: release handshaker channel if no longer needed #5210
Conversation
| final ObjectPool<Channel> handshakerChannelPool) { | ||
| final class ServerAltsProtocolNegotiator extends AltsProtocolNegotiator { | ||
|
|
||
| private Channel handshakerChannel = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/ = null;/;/
it's implicit in java.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
|
|
||
| /** Creates a new handshaker. */ | ||
| TsiHandshaker newHandshaker(@Nullable String authority); | ||
| TsiHandshaker newHandshaker(@Nullable Channel handshakerChannel, @Nullable String authority); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fear this may have internal repercussions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not an issue. I have done it before. Let me prepare an import patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks wrong. The reason authority is passed is because it could be different every time. This channel is also specific to one handshaker implementation.
Instead, you should pass some object when creating the TsiHandshakerFactory. Problably an ObjectPool.
|
/cc |
| public void close() { | ||
| logger.finest("ALTS Client ProtocolNegotiator Closed"); | ||
| // TODO(jiangtaoli2016): release resources | ||
| handshakerChannelPool.returnObject(handshakerChannel); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could release the object before all the Handlers have finished using it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't return the channel here, where shall we return the channel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking we would return it here and in the handshakers.
However, I checked and this close is called on termination, which means it is guaranteed no handshakes are still occurring. So there's nothing needed to be changed here.
|
|
||
| /** Creates a new handshaker. */ | ||
| TsiHandshaker newHandshaker(@Nullable String authority); | ||
| TsiHandshaker newHandshaker(@Nullable Channel handshakerChannel, @Nullable String authority); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks wrong. The reason authority is passed is because it could be different every time. This channel is also specific to one handshaker implementation.
Instead, you should pass some object when creating the TsiHandshakerFactory. Problably an ObjectPool.
| public void close() { | ||
| logger.finest("ALTS Client ProtocolNegotiator Closed"); | ||
| // TODO(jiangtaoli2016): release resources | ||
| handshakerChannelPool.returnObject(handshakerChannel); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking we would return it here and in the handshakers.
However, I checked and this close is called on termination, which means it is guaranteed no handshakes are still occurring. So there's nothing needed to be changed here.
| public Handler newHandler(GrpcHttp2ConnectionHandler grpcHandler) { | ||
| TsiHandshaker handshaker = handshakerFactory.newHandshaker(grpcHandler.getAuthority()); | ||
| if (handshakerChannel == null) { | ||
| handshakerChannel = handshakerChannelPool.getObject(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not thread-safe.
| public void close() { | ||
| logger.finest("ALTS Server ProtocolNegotiator Closed"); | ||
| // TODO(jiangtaoli2016): release resources | ||
| handshakerChannelPool.returnObject(lazyHandshakerChannel.get()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This creates it if it wasn't used. If that's being done there's no point in creating it lazily.
Add a close/shutdown/release to LazyChannel? It would be a synchronized method and release the object only if it was created. That also means you wouldn't need to pass ObjectPool to AltsProtocolNegotiator which assumed it is the same instance as LazyChannel is using.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion! done.
|
|
||
| /** Channel created from a channel pool lazily. */ | ||
| public static class LazyChannel { | ||
| private ObjectPool<Channel> channelPool; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
final
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
jiangtaoli2016
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code revised.
| public void close() { | ||
| logger.finest("ALTS Server ProtocolNegotiator Closed"); | ||
| // TODO(jiangtaoli2016): release resources | ||
| handshakerChannelPool.returnObject(lazyHandshakerChannel.get()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion! done.
|
|
||
| /** Channel created from a channel pool lazily. */ | ||
| public static class LazyChannel { | ||
| private ObjectPool<Channel> channelPool; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
65013af to
5fbd5ed
Compare
|
Thanks Eric and Carl for helpful discussion and suggestions! It is much nicer than the original implementation. |
In ALTS and Google Default Channel, we create a shared channel to handshaker service lazily. This PR releases the channel when ProtocolNegotiator close() is called.