• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19517

22 Oct 2024 07:17PM UTC coverage: 84.679% (-0.004%) from 84.683%
#19517

push

github

web-flow
netty: Avoid TCP_USER_TIMEOUT warning when not using epoll (#11564)

In NettyClientTransport, the TCP_USER_TIMEOUT attribute can be set only
if the channel is of the AbstractEpollStreamChannel.

Fixes #11517

33876 of 40005 relevant lines covered (84.68%)

0.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.67
/../netty/src/main/java/io/grpc/netty/NettyClientTransport.java
1
/*
2
 * Copyright 2014 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.netty;
18

19
import static io.grpc.internal.GrpcUtil.KEEPALIVE_TIME_NANOS_DISABLED;
20
import static io.netty.channel.ChannelOption.ALLOCATOR;
21
import static io.netty.channel.ChannelOption.SO_KEEPALIVE;
22

23
import com.google.common.annotations.VisibleForTesting;
24
import com.google.common.base.MoreObjects;
25
import com.google.common.base.Preconditions;
26
import com.google.common.base.Ticker;
27
import com.google.common.util.concurrent.ListenableFuture;
28
import com.google.common.util.concurrent.SettableFuture;
29
import io.grpc.Attributes;
30
import io.grpc.CallOptions;
31
import io.grpc.ChannelLogger;
32
import io.grpc.ClientStreamTracer;
33
import io.grpc.InternalChannelz.SocketStats;
34
import io.grpc.InternalLogId;
35
import io.grpc.Metadata;
36
import io.grpc.MethodDescriptor;
37
import io.grpc.Status;
38
import io.grpc.internal.ClientStream;
39
import io.grpc.internal.ConnectionClientTransport;
40
import io.grpc.internal.FailingClientStream;
41
import io.grpc.internal.GrpcUtil;
42
import io.grpc.internal.Http2Ping;
43
import io.grpc.internal.KeepAliveManager;
44
import io.grpc.internal.KeepAliveManager.ClientKeepAlivePinger;
45
import io.grpc.internal.StatsTraceContext;
46
import io.grpc.internal.TransportTracer;
47
import io.grpc.netty.NettyChannelBuilder.LocalSocketPicker;
48
import io.netty.bootstrap.Bootstrap;
49
import io.netty.channel.Channel;
50
import io.netty.channel.ChannelFactory;
51
import io.netty.channel.ChannelFuture;
52
import io.netty.channel.ChannelFutureListener;
53
import io.netty.channel.ChannelHandler;
54
import io.netty.channel.ChannelOption;
55
import io.netty.channel.EventLoop;
56
import io.netty.channel.EventLoopGroup;
57
import io.netty.handler.codec.http2.StreamBufferingEncoder.Http2ChannelClosedException;
58
import io.netty.util.AsciiString;
59
import io.netty.util.concurrent.Future;
60
import io.netty.util.concurrent.GenericFutureListener;
61
import java.net.SocketAddress;
62
import java.nio.channels.ClosedChannelException;
63
import java.util.Map;
64
import java.util.concurrent.Executor;
65
import java.util.concurrent.TimeUnit;
66
import javax.annotation.Nullable;
67

68
/**
69
 * A Netty-based {@link ConnectionClientTransport} implementation.
70
 */
71
class NettyClientTransport implements ConnectionClientTransport {
1✔
72

73
  private final InternalLogId logId;
74
  private final Map<ChannelOption<?>, ?> channelOptions;
75
  private final SocketAddress remoteAddress;
76
  private final ChannelFactory<? extends Channel> channelFactory;
77
  private final EventLoopGroup group;
78
  private final ProtocolNegotiator negotiator;
79
  private final String authorityString;
80
  private final AsciiString authority;
81
  private final AsciiString userAgent;
82
  private final boolean autoFlowControl;
83
  private final int flowControlWindow;
84
  private final int maxMessageSize;
85
  private final int maxHeaderListSize;
86
  private KeepAliveManager keepAliveManager;
87
  private final long keepAliveTimeNanos;
88
  private final long keepAliveTimeoutNanos;
89
  private final boolean keepAliveWithoutCalls;
90
  private final AsciiString negotiationScheme;
91
  private final Runnable tooManyPingsRunnable;
92
  private NettyClientHandler handler;
93
  // We should not send on the channel until negotiation completes. This is a hard requirement
94
  // by SslHandler but is appropriate for HTTP/1.1 Upgrade as well.
95
  private Channel channel;
96
  /** If {@link #start} has been called, non-{@code null} if channel is {@code null}. */
97
  private Status statusExplainingWhyTheChannelIsNull;
98
  /** Since not thread-safe, may only be used from event loop. */
99
  private ClientTransportLifecycleManager lifecycleManager;
100
  /** Since not thread-safe, may only be used from event loop. */
101
  private final TransportTracer transportTracer;
102
  private final Attributes eagAttributes;
103
  private final LocalSocketPicker localSocketPicker;
104
  private final ChannelLogger channelLogger;
105
  private final boolean useGetForSafeMethods;
106
  private final Ticker ticker;
107

108
  NettyClientTransport(
109
      SocketAddress address, ChannelFactory<? extends Channel> channelFactory,
110
      Map<ChannelOption<?>, ?> channelOptions, EventLoopGroup group,
111
      ProtocolNegotiator negotiator, boolean autoFlowControl, int flowControlWindow,
112
      int maxMessageSize, int maxHeaderListSize,
113
      long keepAliveTimeNanos, long keepAliveTimeoutNanos,
114
      boolean keepAliveWithoutCalls, String authority, @Nullable String userAgent,
115
      Runnable tooManyPingsRunnable, TransportTracer transportTracer, Attributes eagAttributes,
116
      LocalSocketPicker localSocketPicker, ChannelLogger channelLogger,
117
      boolean useGetForSafeMethods, Ticker ticker) {
1✔
118

119
    this.negotiator = Preconditions.checkNotNull(negotiator, "negotiator");
1✔
120
    this.negotiationScheme = this.negotiator.scheme();
1✔
121
    this.remoteAddress = Preconditions.checkNotNull(address, "address");
1✔
122
    this.group = Preconditions.checkNotNull(group, "group");
1✔
123
    this.channelFactory = channelFactory;
1✔
124
    this.channelOptions = Preconditions.checkNotNull(channelOptions, "channelOptions");
1✔
125
    this.autoFlowControl = autoFlowControl;
1✔
126
    this.flowControlWindow = flowControlWindow;
1✔
127
    this.maxMessageSize = maxMessageSize;
1✔
128
    this.maxHeaderListSize = maxHeaderListSize;
1✔
129
    this.keepAliveTimeNanos = keepAliveTimeNanos;
1✔
130
    this.keepAliveTimeoutNanos = keepAliveTimeoutNanos;
1✔
131
    this.keepAliveWithoutCalls = keepAliveWithoutCalls;
1✔
132
    this.authorityString = authority;
1✔
133
    this.authority = new AsciiString(authority);
1✔
134
    this.userAgent = new AsciiString(GrpcUtil.getGrpcUserAgent("netty", userAgent));
1✔
135
    this.tooManyPingsRunnable =
1✔
136
        Preconditions.checkNotNull(tooManyPingsRunnable, "tooManyPingsRunnable");
1✔
137
    this.transportTracer = Preconditions.checkNotNull(transportTracer, "transportTracer");
1✔
138
    this.eagAttributes = Preconditions.checkNotNull(eagAttributes, "eagAttributes");
1✔
139
    this.localSocketPicker = Preconditions.checkNotNull(localSocketPicker, "localSocketPicker");
1✔
140
    this.logId = InternalLogId.allocate(getClass(), remoteAddress.toString());
1✔
141
    this.channelLogger = Preconditions.checkNotNull(channelLogger, "channelLogger");
1✔
142
    this.useGetForSafeMethods = useGetForSafeMethods;
1✔
143
    this.ticker = Preconditions.checkNotNull(ticker, "ticker");
1✔
144
  }
1✔
145

146
  @Override
147
  public void ping(final PingCallback callback, final Executor executor) {
148
    if (channel == null) {
1✔
149
      executor.execute(new Runnable() {
1✔
150
        @Override
151
        public void run() {
152
          callback.onFailure(statusExplainingWhyTheChannelIsNull.asException());
1✔
153
        }
1✔
154
      });
155
      return;
1✔
156
    }
157
    // The promise and listener always succeed in NettyClientHandler. So this listener handles the
158
    // error case, when the channel is closed and the NettyClientHandler no longer in the pipeline.
159
    ChannelFutureListener failureListener = new ChannelFutureListener() {
1✔
160
      @Override
161
      public void operationComplete(ChannelFuture future) throws Exception {
162
        if (!future.isSuccess()) {
1✔
163
          Status s = statusFromFailedFuture(future);
1✔
164
          Http2Ping.notifyFailed(callback, executor, s.asException());
1✔
165
        }
166
      }
1✔
167
    };
168
    // Write the command requesting the ping
169
    handler.getWriteQueue().enqueue(new SendPingCommand(callback, executor), true)
1✔
170
        .addListener(failureListener);
1✔
171
  }
1✔
172

173
  @Override
174
  public ClientStream newStream(
175
      MethodDescriptor<?, ?> method, Metadata headers, CallOptions callOptions,
176
      ClientStreamTracer[] tracers) {
177
    Preconditions.checkNotNull(method, "method");
1✔
178
    Preconditions.checkNotNull(headers, "headers");
1✔
179
    if (channel == null) {
1✔
180
      return new FailingClientStream(statusExplainingWhyTheChannelIsNull, tracers);
1✔
181
    }
182
    StatsTraceContext statsTraceCtx =
1✔
183
        StatsTraceContext.newClientContext(tracers, getAttributes(), headers);
1✔
184
    return new NettyClientStream(
1✔
185
        new NettyClientStream.TransportState(
186
            handler,
187
            channel.eventLoop(),
1✔
188
            maxMessageSize,
189
            statsTraceCtx,
190
            transportTracer,
191
            method.getFullMethodName(),
1✔
192
            callOptions) {
1✔
193
          @Override
194
          protected Status statusFromFailedFuture(ChannelFuture f) {
195
            return NettyClientTransport.this.statusFromFailedFuture(f);
1✔
196
          }
197
        },
198
        method,
199
        headers,
200
        channel,
201
        authority,
202
        negotiationScheme,
203
        userAgent,
204
        statsTraceCtx,
205
        transportTracer,
206
        callOptions,
207
        useGetForSafeMethods);
208
  }
209

210
  @SuppressWarnings("unchecked")
211
  @Override
212
  public Runnable start(Listener transportListener) {
213
    lifecycleManager = new ClientTransportLifecycleManager(
1✔
214
        Preconditions.checkNotNull(transportListener, "listener"));
1✔
215
    EventLoop eventLoop = group.next();
1✔
216
    if (keepAliveTimeNanos != KEEPALIVE_TIME_NANOS_DISABLED) {
1✔
217
      keepAliveManager = new KeepAliveManager(
1✔
218
          new ClientKeepAlivePinger(this), eventLoop, keepAliveTimeNanos, keepAliveTimeoutNanos,
219
          keepAliveWithoutCalls);
220
    }
221

222
    handler = NettyClientHandler.newHandler(
1✔
223
        lifecycleManager,
224
        keepAliveManager,
225
        autoFlowControl,
226
        flowControlWindow,
227
        maxHeaderListSize,
228
        GrpcUtil.STOPWATCH_SUPPLIER,
229
        tooManyPingsRunnable,
230
        transportTracer,
231
        eagAttributes,
232
        authorityString,
233
        channelLogger,
234
        ticker);
235

236
    ChannelHandler negotiationHandler = negotiator.newHandler(handler);
1✔
237

238
    Bootstrap b = new Bootstrap();
1✔
239
    b.option(ALLOCATOR, Utils.getByteBufAllocator(false));
1✔
240
    b.group(eventLoop);
1✔
241
    b.channelFactory(channelFactory);
1✔
242
    // For non-socket based channel, the option will be ignored.
243
    b.option(SO_KEEPALIVE, true);
1✔
244
    for (Map.Entry<ChannelOption<?>, ?> entry : channelOptions.entrySet()) {
1✔
245
      // Every entry in the map is obtained from
246
      // NettyChannelBuilder#withOption(ChannelOption<T> option, T value)
247
      // so it is safe to pass the key-value pair to b.option().
248
      b.option((ChannelOption<Object>) entry.getKey(), entry.getValue());
1✔
249
    }
1✔
250

251
    ChannelHandler bufferingHandler = new WriteBufferingAndExceptionHandler(negotiationHandler);
1✔
252

253
    /*
254
     * We don't use a ChannelInitializer in the client bootstrap because its "initChannel" method
255
     * is executed in the event loop and we need this handler to be in the pipeline immediately so
256
     * that it may begin buffering writes.
257
     */
258
    b.handler(bufferingHandler);
1✔
259
    ChannelFuture regFuture = b.register();
1✔
260
    if (regFuture.isDone() && !regFuture.isSuccess()) {
1✔
261
      channel = null;
1✔
262
      // Initialization has failed badly. All new streams should be made to fail.
263
      Throwable t = regFuture.cause();
1✔
264
      if (t == null) {
1✔
265
        t = new IllegalStateException("Channel is null, but future doesn't have a cause");
×
266
      }
267
      statusExplainingWhyTheChannelIsNull = Utils.statusFromThrowable(t);
1✔
268
      // Use a Runnable since lifecycleManager calls transportListener
269
      return new Runnable() {
1✔
270
        @Override
271
        public void run() {
272
          // NOTICE: we not are calling lifecycleManager from the event loop. But there isn't really
273
          // an event loop in this case, so nothing should be accessing the lifecycleManager. We
274
          // could use GlobalEventExecutor (which is what regFuture would use for notifying
275
          // listeners in this case), but avoiding on-demand thread creation in an error case seems
276
          // a good idea and is probably clearer threading.
277
          lifecycleManager.notifyTerminated(statusExplainingWhyTheChannelIsNull);
1✔
278
        }
1✔
279
      };
280
    }
281
    channel = regFuture.channel();
1✔
282
    // For non-epoll based channel, the option will be ignored.
283
    try {
284
      if (keepAliveTimeNanos != KEEPALIVE_TIME_NANOS_DISABLED
1✔
285
              && Class.forName("io.netty.channel.epoll.AbstractEpollChannel").isInstance(channel)) {
1✔
286
        ChannelOption<Integer> tcpUserTimeout = Utils.maybeGetTcpUserTimeoutOption();
1✔
287
        if (tcpUserTimeout != null) {
1✔
288
          int tcpUserTimeoutMs = (int) TimeUnit.NANOSECONDS.toMillis(keepAliveTimeoutNanos);
1✔
289
          channel.config().setOption(tcpUserTimeout, tcpUserTimeoutMs);
1✔
290
        }
291
      }
292
    } catch (ClassNotFoundException ignored) {
×
293
      // JVM did not load AbstractEpollChannel, so the current channel will not be of epoll type,
294
      // so there is no need to set TCP_USER_TIMEOUT
295
    }
1✔
296
    // Start the write queue as soon as the channel is constructed
297
    handler.startWriteQueue(channel);
1✔
298
    // This write will have no effect, yet it will only complete once the negotiationHandler
299
    // flushes any pending writes. We need it to be staged *before* the `connect` so that
300
    // the channel can't have been closed yet, removing all handlers. This write will sit in the
301
    // AbstractBufferingHandler's buffer, and will either be flushed on a successful connection,
302
    // or failed if the connection fails.
303
    channel.writeAndFlush(NettyClientHandler.NOOP_MESSAGE).addListener(new ChannelFutureListener() {
1✔
304
      @Override
305
      public void operationComplete(ChannelFuture future) throws Exception {
306
        if (!future.isSuccess()) {
1✔
307
          // Need to notify of this failure, because NettyClientHandler may not have been added to
308
          // the pipeline before the error occurred.
309
          lifecycleManager.notifyTerminated(Utils.statusFromThrowable(future.cause()));
1✔
310
        }
311
      }
1✔
312
    });
313
    // Start the connection operation to the server.
314
    SocketAddress localAddress =
1✔
315
        localSocketPicker.createSocketAddress(remoteAddress, eagAttributes);
1✔
316
    if (localAddress != null) {
1✔
317
      channel.connect(remoteAddress, localAddress);
×
318
    } else {
319
      channel.connect(remoteAddress);
1✔
320
    }
321

322
    if (keepAliveManager != null) {
1✔
323
      keepAliveManager.onTransportStarted();
1✔
324
    }
325

326
    return null;
1✔
327
  }
328

329
  @Override
330
  public void shutdown(Status reason) {
331
    // start() could have failed
332
    if (channel == null) {
1✔
333
      return;
1✔
334
    }
335
    // Notifying of termination is automatically done when the channel closes.
336
    if (channel.isOpen()) {
1✔
337
      handler.getWriteQueue().enqueue(new GracefulCloseCommand(reason), true);
1✔
338
    }
339
  }
1✔
340

341
  @Override
342
  public void shutdownNow(final Status reason) {
343
    // Notifying of termination is automatically done when the channel closes.
344
    if (channel != null && channel.isOpen()) {
1✔
345
      handler.getWriteQueue().enqueue(new Runnable() {
1✔
346
        @Override
347
        public void run() {
348
          lifecycleManager.notifyShutdown(reason);
1✔
349
          channel.write(new ForcefulCloseCommand(reason));
1✔
350
        }
1✔
351
      }, true);
352
    }
353
  }
1✔
354

355
  @Override
356
  public String toString() {
357
    return MoreObjects.toStringHelper(this)
1✔
358
        .add("logId", logId.getId())
1✔
359
        .add("remoteAddress", remoteAddress)
1✔
360
        .add("channel", channel)
1✔
361
        .toString();
1✔
362
  }
363

364
  @Override
365
  public InternalLogId getLogId() {
366
    return logId;
1✔
367
  }
368

369
  @Override
370
  public Attributes getAttributes() {
371
    return handler.getAttributes();
1✔
372
  }
373

374
  @Override
375
  public ListenableFuture<SocketStats> getStats() {
376
    final SettableFuture<SocketStats> result = SettableFuture.create();
1✔
377
    if (channel.eventLoop().inEventLoop()) {
1✔
378
      // This is necessary, otherwise we will block forever if we get the future from inside
379
      // the event loop.
380
      result.set(getStatsHelper(channel));
×
381
      return result;
×
382
    }
383
    channel.eventLoop().submit(
1✔
384
        new Runnable() {
1✔
385
          @Override
386
          public void run() {
387
            result.set(getStatsHelper(channel));
1✔
388
          }
1✔
389
        })
390
        .addListener(
1✔
391
            new GenericFutureListener<Future<Object>>() {
1✔
392
              @Override
393
              public void operationComplete(Future<Object> future) throws Exception {
394
                if (!future.isSuccess()) {
1✔
395
                  result.setException(future.cause());
×
396
                }
397
              }
1✔
398
            });
399
    return result;
1✔
400
  }
401

402
  private SocketStats getStatsHelper(Channel ch) {
403
    assert ch.eventLoop().inEventLoop();
1✔
404
    return new SocketStats(
1✔
405
        transportTracer.getStats(),
1✔
406
        channel.localAddress(),
1✔
407
        channel.remoteAddress(),
1✔
408
        Utils.getSocketOptions(ch),
1✔
409
        handler == null ? null : handler.getSecurityInfo());
1✔
410
  }
411

412
  @VisibleForTesting
413
  Channel channel() {
414
    return channel;
1✔
415
  }
416

417
  @VisibleForTesting
418
  KeepAliveManager keepAliveManager() {
419
    return keepAliveManager;
1✔
420
  }
421

422
  /**
423
   * Convert ChannelFuture.cause() to a Status, taking into account that all handlers are removed
424
   * from the pipeline when the channel is closed. Since handlers are removed, you may get an
425
   * unhelpful exception like ClosedChannelException.
426
   *
427
   * <p>This method must only be called on the event loop.
428
   */
429
  private Status statusFromFailedFuture(ChannelFuture f) {
430
    Throwable t = f.cause();
1✔
431
    if (t instanceof ClosedChannelException
1✔
432
        // Exception thrown by the StreamBufferingEncoder if the channel is closed while there
433
        // are still streams buffered. This exception is not helpful. Replace it by the real
434
        // cause of the shutdown (if available).
435
        || t instanceof Http2ChannelClosedException) {
436
      Status shutdownStatus = lifecycleManager.getShutdownStatus();
1✔
437
      if (shutdownStatus == null) {
1✔
438
        return Status.UNKNOWN.withDescription("Channel closed but for unknown reason")
×
439
            .withCause(new ClosedChannelException().initCause(t));
×
440
      }
441
      return shutdownStatus;
1✔
442
    }
443
    return Utils.statusFromThrowable(t);
1✔
444
  }
445
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc