Skip to content
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

Request connection on startup #377

Merged
merged 16 commits into from Jul 15, 2020

Conversation

genuss
Copy link
Contributor

@genuss genuss commented Jul 6, 2020

Solves #376

Looks like I was a bit too optimistic about this task. It's not that easy to check that connection was established in GRPC. It doesn't even have such thing as "connection timeout" becase GRPC internally handles connections and retries on failures.

I came out with this solution:

  1. Use separate method to check connection on startup if grpc-spring-boot-starter was configured to do so.
  2. As far as I understand ConnectivityState state-machine it should work like this:
IDLE -> CONNECTING -> READY                 for happy path
IDLE -> CONNECTING -> TRANSIENT_FAILURE     for connection failure
  1. So my method initiates connection via channel.getState(true), registers two callbacks and awaits on CountDownLatch for some period of time.
  2. If ConnectivityState is in wrong status or timeout has passed then we throw exception which stops context from starting.

Well any feedback will be highly appreciated. If you suppose design is OK, I'll make connection timeout configurable too.

@ST-DDT ST-DDT added the enhancement A feature request or improvement label Jul 6, 2020
@ST-DDT ST-DDT linked an issue Jul 6, 2020 that may be closed by this pull request
@ST-DDT
Copy link
Collaborator

ST-DDT commented Jul 6, 2020

If you suppose design is OK, I'll make connection timeout configurable too.

Maybe we could change the immediateConnect to a immediateConnectTimeout duration and define it as "do it if > 0"?

@genuss
Copy link
Contributor Author

genuss commented Jul 8, 2020

Maybe we could change the immediateConnect to a immediateConnectTimeout duration and define it as "do it if > 0"?

That's a great idea. Applied it.

@genuss genuss marked this pull request as ready for review July 8, 2020 14:51
@ST-DDT
Copy link
Collaborator

ST-DDT commented Jul 8, 2020

Could you please add a test for this feature?
If possible for both code paths:

  • Connect success -> app starts
  • Connect fails -> app fails to start

I can help you with that, if necessary.

@ST-DDT ST-DDT self-assigned this Jul 8, 2020
@ST-DDT ST-DDT added this to the 2.10.0 milestone Jul 8, 2020
@genuss
Copy link
Contributor Author

genuss commented Jul 8, 2020

I created three tests and arranged them in one file. I think it’s ok to group them so.

  • When connection is not requested, channel must be in IDLE state
  • When connection is requested and successful channel must be in READY state when user-code interacts with a channel
  • When connection is requested but was unsuccessful, context fails to start. That is the tricky one.

@genuss
Copy link
Contributor Author

genuss commented Jul 8, 2020

Do you have some ideas why build fails? I can't see issues.

@ST-DDT
Copy link
Collaborator

ST-DDT commented Jul 8, 2020

ImmediateConnectTest$ImmediateConnectEnabledAndSuccessfulTest > testFailingCall() FAILED
    java.lang.IllegalStateException: Failed to create channel: test
        at net.devh.boot.grpc.client.inject.GrpcClientBeanPostProcessor.processInjectionPoint(GrpcClientBeanPostProcessor.java:126)
		[...]
	Caused by:
        java.lang.IllegalStateException: Can't connect to channel test
            at net.devh.boot.grpc.client.channelfactory.AbstractChannelFactory.connectOnStartup(AbstractChannelFactory.java:172)
            at net.devh.boot.grpc.client.channelfactory.AbstractChannelFactory.newManagedChannel(AbstractChannelFactory.java:140)

@ST-DDT
Copy link
Collaborator

ST-DDT commented Jul 9, 2020

I cannot reproduce the error at my place either. But I assume, that the server might be a bit slow starting up.
Could you do me a favor and change the check to wait for ready? If that does work, we at least know, that it is related to the startup speed.

As an alternative you can also try to uncomment:

// failFast = true
// showStandardStreams = true

in the main build.gradle.

@genuss
Copy link
Contributor Author

genuss commented Jul 11, 2020

This issue with tests was the following. A channel can move to READY state too fast (looks like CI on github is), however in my assumption was wrong in the first place. So state diagram can be:

IDLE -> CONNECTING -> READY                 for happy path
READY                                       for super-happy path
IDLE -> CONNECTING -> TRANSIENT_FAILURE     for connection failure

And I applied your suggestion to wait for ready state. When I actually made it, I understood you're right. The code looks much better now 👍 .

import io.grpc.ConnectivityState;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.*;
Copy link
Collaborator

Choose a reason for hiding this comment

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

No star imports please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Collaborator

@ST-DDT ST-DDT left a comment

Choose a reason for hiding this comment

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

Functionality wise this is already good to go 👍 . Just some minor changes left.

Copy link
Collaborator

@ST-DDT ST-DDT left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks for addressing all the small issues this fast.

@ST-DDT ST-DDT requested a review from yidongnan July 15, 2020 09:15
@yidongnan yidongnan merged commit ea7c20e into grpc-ecosystem:master Jul 15, 2020
@genuss genuss deleted the feature/connect-on-start branch July 15, 2020 11:21
@genuss
Copy link
Contributor Author

genuss commented Jul 15, 2020

Thanks guys! Looking forward to release 🚀

@ST-DDT
Copy link
Collaborator

ST-DDT commented Aug 1, 2020

FYI: I try to finish the remaining issues within the next two weeks and do a release afterwards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature request or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request connection on startup
3 participants