Skip to content

Conversation

@jiangtaoli2016
Copy link
Contributor

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.

@jiangtaoli2016 jiangtaoli2016 added the kokoro:force-run Add this label to a PR to tell Kokoro to re-run all tests. Not generally necessary label Jan 4, 2019
@kokoro-team kokoro-team removed the kokoro:force-run Add this label to a PR to tell Kokoro to re-run all tests. Not generally necessary label Jan 4, 2019
@jiangtaoli2016
Copy link
Contributor Author

#4755

final ObjectPool<Channel> handshakerChannelPool) {
final class ServerAltsProtocolNegotiator extends AltsProtocolNegotiator {

private Channel handshakerChannel = null;
Copy link
Contributor

@carl-mastrangelo carl-mastrangelo Jan 4, 2019

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.

Copy link
Contributor Author

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);
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

@srini100
Copy link
Contributor

srini100 commented Jan 7, 2019

/cc

public void close() {
logger.finest("ALTS Client ProtocolNegotiator Closed");
// TODO(jiangtaoli2016): release resources
handshakerChannelPool.returnObject(handshakerChannel);
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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);
Copy link
Member

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);
Copy link
Member

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();
Copy link
Member

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());
Copy link
Member

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.

Copy link
Contributor Author

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;
Copy link
Member

Choose a reason for hiding this comment

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

final

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor Author

@jiangtaoli2016 jiangtaoli2016 left a 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());
Copy link
Contributor Author

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@jiangtaoli2016 jiangtaoli2016 force-pushed the release_handshaker_channel branch from 65013af to 5fbd5ed Compare January 11, 2019 21:06
@jiangtaoli2016
Copy link
Contributor Author

Thanks Eric and Carl for helpful discussion and suggestions! It is much nicer than the original implementation.

@jiangtaoli2016 jiangtaoli2016 merged commit 4d90b37 into grpc:master Jan 11, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Apr 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants