grpc-java icon indicating copy to clipboard operation
grpc-java copied to clipboard

DNS resolution expiration

Open pbassiner opened this issue 10 years ago • 17 comments

It looks like the channel is somehow caching the IP resolved through DNS but never refreshing it. We are using grpc in AWS and since ELB's IPs change over time, we've run into issues and found out grpc clients are trying to connect to an old IP.

Code snippet:

private[this] val channel = NettyChannelBuilder.forAddress(httpHost, httpPort).negotiationType(NegotiationType.PLAINTEXT).build();
  private[this] val blockingStub = CrudGrpc.newBlockingStub(channel)
  private[this] def getStub = blockingStub.withDeadlineAfter(timeout.toNanos, TimeUnit.NANOSECONDS)

override def getEntity(id: String): Entity = {
  getStub.getEntity(id)
}

Is our assumption correct? Is there any issue in how are we using the client library? Would it be a bad practice (performance-wise at least) to create a new channel in each request?

Thanks in advance

pbassiner avatar Feb 22 '16 10:02 pbassiner

This is a deficiency of DnsNameResolver, not a fault with how you are using the library. To to get an idea of what would work for you, would refreshing every 15-30 minutes work for you? That would be easy to add in the short-term.

ejona86 avatar Feb 23 '16 17:02 ejona86

In my opinion, the proper solution would be to make it configurable by channel, so each client can set it up according to its needs (like connection or request timeouts in an http client). Is it reasonable?

pbassiner avatar Feb 23 '16 18:02 pbassiner

In my mind the proper solution is to use the DNS TTL... but that's not quite feasible. Instead we could do a DNS query every time and rely on the platform's caching (like networkaddress.cache.ttl). But that will take a little bit of interface rework. I don't think you should need to configure the refresh period. So I really am suggesting the fixed rate refreshing as only a short-term solution. We could maybe have a short-term way to configure it though that goes away when we implement a better solution.

I've not yet discussed with the other implementations how they approach the problem though.

ejona86 avatar Feb 23 '16 19:02 ejona86

Well, in our case, that wouldn't make the trick. We are using AWS's ELBs which have a 60 seconds TTL for their DNS records.

I agree on the proposal for the long-term solution. Relying on the DNS TTL seems the way to go (although being able to override the setting would also make sense anyway).

pbassiner avatar Feb 23 '16 20:02 pbassiner

We use NettyChannelBuilder to create a ManagedChannelImpl. This ManagedChannelImpl keeps a reference to its NettyChannelBuilder.NettyTransportFactory, which is used every time the channel needs to open a new Transport (for example, if the server has changed its IP, the Transport will be closed and a new Transport will be requested).

Every time the NettyChannelBuilder.NettyTransportFactory is used to get a new transport, it'll reuse the original SocketAddress. This SocketAddress has the original IP resolved, so it'll never try to resolve it again.

Maybe a possible fix would be to "save" the original host/port, and use it instead of the already resolved SocketAddress every time a new Transport needs to be open/created?

I'm making some assumptions here that maybe are wrong and I'm not sure about the implications of this change, so WDYT?

aserrallerios avatar Feb 24 '16 15:02 aserrallerios

Instead of doing DNS resolution periodically, we will do it when ever the channel has failed to connect all addresses. We believe that's enough, and are yet to find a use case that would make periodic resolution necessary.

Because we use InetAddress.getAllByName() for DNS resolution, which is subject to the JVM DNS cache, users still need to configure JVM to adjust the cache TTL if the default doesn't suffice.

zhangkun83 avatar Mar 24 '16 20:03 zhangkun83

I have a second thought. Refreshing solely on connection failures may not suffice.

Consider an idle channel, it may not maintain a live connection, thus no connection failures to trigger refreshing. When we make an RPC on that channel, the addresses may be stale and there are two possible outcomes:

  • Best case: old addresses are no longer connectable, thus we gets connection failures which triggers re-resolution, and eventually we will connect to new addresses. The application may see some additional latency on the initial requests.
  • Worst case: old addresses are still serving gRPC or other HTTP/2 services. We won't get connection failures. Instead, RPCs may fail. It won't trigger re-resolution and we can't go out of this state.

I think we still need periodic re-resolution. @ejona86 @ctiller WDYT?

zhangkun83 avatar Mar 24 '16 22:03 zhangkun83

Don't trigger the resolution until you need to connect?

On Thu, Mar 24, 2016, 3:02 PM Kun Zhang [email protected] wrote:

I have a second thought and only refreshing on connection failures may not suffice.

Consider an idle channel, it may not maintain a live connection, thus no connection failures to trigger refreshing. When we make an RPC on that channel, the addresses may be stale and there are two possible outcomes:

  • Best case: old addresses are no longer connectable, thus we gets connection failures which triggers re-resolution, and eventually we will connect to new addresses. The application may see some additional latency on the initial requests.
  • Worst case: old addresses are still serving gRPC or other HTTP/2 services. We won't get connection failures. Instead, RPCs may fail. It won't trigger re-resolution and we can't go out of this state.

I think we still need periodic re-resolution. @ejona86 https://github.com/ejona86 @ctiller https://github.com/ctiller WDYT?

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/grpc/grpc-java/issues/1463#issuecomment-201043930

ctiller avatar Mar 25 '16 05:03 ctiller

A few questions to consider:

  1. When to trigger the resolution
    • 1a: After disconnection/failure
    • 1b: Before connection
  2. Invalidate old addresses before re-resolution?
    • 2a: invalidate
    • 2b: do not invalidate

@ctiller prefers 1b+2a, because it provides the best address freshness, with the caveat that it may add some latency to requests that trigger new connections, even if the addresses haven't changed.

At this moment we are doing 1a+2b, with a higher chance of connecting to old addresses, which shouldn't be a big concern. We may want to change it to 1b+2a, but it's not trivial. It will also have interesting interaction with the idle-mode (#1276)

zhangkun83 avatar Mar 25 '16 22:03 zhangkun83

I also encountered such problem, using Python as server, Java as client and appoint NettyChannelBuilder as the ManagedChannel builder, deploy in AWS with ELB, but IP changes over time. if I change ManagedChannel builder to ManagedChannelBuilder, the grpc-java version is 0.13.1, ManagedChannelBuilder using io.grpc.okhttp.OkHttpChannelProvider as provider seems to work fine.

Any suggestion on this?

zhongql avatar May 05 '16 05:05 zhongql

@zhongql 0.14.0 has just been released and it includes #1591, which should solve your issue.

zhangkun83 avatar May 05 '16 16:05 zhangkun83

@zhangkun83 thank you ! I will update version 0.14.0 and try again !

zhongql avatar May 07 '16 16:05 zhongql

quick ping. was there a resolution on periodic refreshing?

saturnism avatar Jun 22 '17 16:06 saturnism

@zhongql just tried with OkHttpChannelBuilder and I can confirm that it works now. I used 1.7.0 Release.

kgoralski avatar Nov 22 '17 08:11 kgoralski

We trigger a re-resolution when a connection goes IDLE or TRANSIENT_FAILURE. This triggers on normal connection shutdown and after failed connection attempts. But we don't poll, and the way the refresh happens you may have to wait one connection attempt more than necessary (which may only be 1 second) for it to begin working.

ejona86 avatar Nov 27 '17 23:11 ejona86

@zhangkun83, I think this is fully addressed. Are you aware of anything missing?

ejona86 avatar Aug 09 '18 22:08 ejona86

Yes I think we have addressed the original issue. The periodic refreshing is discussed in #2514

zhangkun83 avatar Aug 09 '18 23:08 zhangkun83