Grpc java interceptor
Please answer these questions before submitting your issue.
What version of gRPC are you using? 1.0.3
No way in grpc java to insert custom logic before retry in case of error from server.
Our grpc server can come in a random port.Lets say server go down our application infra should retry and before retry should open up channel on new server port.
As we have several client applications I need to insert in common place rather than asking client to handle retry.
I believe gRPC retry support was added more recently than 1.0.3, and does what you are describing: https://github.com/grpc/proposal/blob/master/A6-client-retries.md#where-retries-occur
A channel should be load balanced across backends, so if a rpc fails then subsequent attempts should go to different hosts (as dictated by the load balancing policy).
Hi zpencer thanks for the reply. However I didn't understand regarding load balancing part. Today we have created interceptor over grpc infra (not the interceptor provided by grpc)and we catch system unavailable exception. After this we reinitialize the channel by getting new server port,try to invoke the operation again. Problem I am facing is above approach works fine for blocking stub but in case of future and asyn stubs I cannot catch since execution happens in another thread. Ex my interceptor will get listenable future and pass it to client applications.when client does get on this future he gets the exception ,we don't want every client to add retry logic instead our backend application should be able to get this exception and retry. In addition I have no control on port of server,it can come at any random port. The only way I know which port server comes up is asking our internal registry which server updates after coming up.How can load balancing help in this case.
@Kavyamanjanatha
If you can upgrade grpc lib from v1.0.3 to v1.12.0, you can take advantage of the first class retry support https://github.com/grpc/proposal/blob/master/A6-client-retries.md
You can implement your own NameResolver.Factory and NameResolver, which resolves your target URI into the correct ip:port based on your internal registry, plus a service config that defines your retry policy. You need let your name resolver to call
NameResolver.Listener.onAddresses(List<EquivalentAddressGroup> servers, Attributes config)
where servers is the updated address(es) with correct port, and config may contain the key GrpcAttributes.NAME_RESOLVER_SERVICE_CONFIG with the value of service config in json string that defines your retry policy.
You need call
channelBuilder.nameResolverFactory(nameResolverFactoryImpl).enableRetry()
// at this version, you should also disable stats and tracing
.setStatsEnabled(false).setTracingEnabled(false);
to create a channel. Some APIs are internal at the version through
Hi @dapengzhang0, I want to try this new retry feature. Now I am using grpc v1.15.0, but .setStatsEnabled(false) and .setTracingEnabled(false) are not public methods of ManagedChannelBuilder. how should I disable stats and tracing, and enable retry?
@yuzheng21 .setStatsEnabled(false) and .setTracingEnabled(false) are not public methods because we don't have users who need them be public yet. Would you please file an issue/send a PR to make them public? Thanks.
sure, thanks! @dapengzhang0 and one more question, is there any document about how to write a service config file? I see one used for testing here https://github.com/grpc/grpc-java/blob/master/core/src/test/resources/io/grpc/internal/test_retry_service_config.json
+ @carl-mastrangelo
yuzheng21: is there any document about how to write a service config file?
@dapengzhang0 instead of making setStatsEnabled and setTracingEnabled public at AbstractManagedChannelImplBuilder directly, I've made them public at InProcessChannelBuilder, which can makes it a lot easier to play with and test retry feature.
There is no documented way to use service config currently, sorry. There is some contention over how the feature should work at the moment.
ic, thanks! @carl-mastrangelo
This is quite old and seems resolved. Closing.
I'll note that channelBuilder.enableRetry() handles disabling stats/tracing and channelBuilder.defaultServiceConfig() makes it semi-easy to provide service config. There is also a hedging example which is similar to enabling retries.