use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project grpc-java by grpc.
the class Utils method getSocketOptions.
static InternalChannelz.SocketOptions getSocketOptions(Channel channel) {
ChannelConfig config = channel.config();
InternalChannelz.SocketOptions.Builder b = new InternalChannelz.SocketOptions.Builder();
// The API allows returning null but not sure if it can happen in practice.
// Let's be paranoid and do null checking just in case.
Integer lingerSeconds = config.getOption(SO_LINGER);
if (lingerSeconds != null) {
b.setSocketOptionLingerSeconds(lingerSeconds);
}
Integer timeoutMillis = config.getOption(SO_TIMEOUT);
if (timeoutMillis != null) {
// in java, SO_TIMEOUT only applies to receiving
b.setSocketOptionTimeoutMillis(timeoutMillis);
}
for (Map.Entry<ChannelOption<?>, Object> opt : config.getOptions().entrySet()) {
ChannelOption<?> key = opt.getKey();
// Constants are pooled, so there should only be one instance of each constant
if (key.equals(SO_LINGER) || key.equals(SO_TIMEOUT)) {
continue;
}
Object value = opt.getValue();
// zpencer: Can a netty option be null?
b.addOption(key.name(), String.valueOf(value));
}
NativeSocketOptions nativeOptions = NettySocketSupport.getNativeSocketOptions(channel);
if (nativeOptions != null) {
// may be null
b.setTcpInfo(nativeOptions.tcpInfo);
for (Map.Entry<String, String> entry : nativeOptions.otherInfo.entrySet()) {
b.addOption(entry.getKey(), entry.getValue());
}
}
return b.build();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project grpc-java by grpc.
the class NettyServer method start.
@Override
public void start(ServerListener serverListener) throws IOException {
listener = checkNotNull(serverListener, "serverListener");
final ServerBootstrap b = new ServerBootstrap();
b.option(ALLOCATOR, Utils.getByteBufAllocator(forceHeapBuffer));
b.childOption(ALLOCATOR, Utils.getByteBufAllocator(forceHeapBuffer));
b.group(bossExecutor, workerGroup);
b.channelFactory(channelFactory);
// For non-socket based channel, the option will be ignored.
b.childOption(SO_KEEPALIVE, true);
if (channelOptions != null) {
for (Map.Entry<ChannelOption<?>, ?> entry : channelOptions.entrySet()) {
@SuppressWarnings("unchecked") ChannelOption<Object> key = (ChannelOption<Object>) entry.getKey();
b.option(key, entry.getValue());
}
}
if (childChannelOptions != null) {
for (Map.Entry<ChannelOption<?>, ?> entry : childChannelOptions.entrySet()) {
@SuppressWarnings("unchecked") ChannelOption<Object> key = (ChannelOption<Object>) entry.getKey();
b.childOption(key, entry.getValue());
}
}
b.childHandler(new ChannelInitializer<Channel>() {
@Override
public void initChannel(Channel ch) {
ChannelPromise channelDone = ch.newPromise();
long maxConnectionAgeInNanos = NettyServer.this.maxConnectionAgeInNanos;
if (maxConnectionAgeInNanos != MAX_CONNECTION_AGE_NANOS_DISABLED) {
// apply a random jitter of +/-10% to max connection age
maxConnectionAgeInNanos = (long) ((.9D + Math.random() * .2D) * maxConnectionAgeInNanos);
}
NettyServerTransport transport = new NettyServerTransport(ch, channelDone, protocolNegotiator, streamTracerFactories, transportTracerFactory.create(), maxStreamsPerConnection, autoFlowControl, flowControlWindow, maxMessageSize, maxHeaderListSize, keepAliveTimeInNanos, keepAliveTimeoutInNanos, maxConnectionIdleInNanos, maxConnectionAgeInNanos, maxConnectionAgeGraceInNanos, permitKeepAliveWithoutCalls, permitKeepAliveTimeInNanos, eagAttributes);
ServerTransportListener transportListener;
// This is to order callbacks on the listener, not to guard access to channel.
synchronized (NettyServer.this) {
if (terminated) {
// Server already terminated.
ch.close();
return;
}
// `channel` shutdown can race with `ch` initialization, so this is only safe to increment
// inside the lock.
sharedResourceReferenceCounter.retain();
transportListener = listener.transportCreated(transport);
}
/**
* Releases the event loop if the channel is "done", possibly due to the channel closing.
*/
final class LoopReleaser implements ChannelFutureListener {
private boolean done;
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!done) {
done = true;
sharedResourceReferenceCounter.release();
}
}
}
transport.start(transportListener);
ChannelFutureListener loopReleaser = new LoopReleaser();
channelDone.addListener(loopReleaser);
ch.closeFuture().addListener(loopReleaser);
}
});
Future<Map<ChannelFuture, SocketAddress>> bindCallFuture = bossExecutor.submit(new Callable<Map<ChannelFuture, SocketAddress>>() {
@Override
public Map<ChannelFuture, SocketAddress> call() {
Map<ChannelFuture, SocketAddress> bindFutures = new HashMap<>();
for (SocketAddress address : addresses) {
ChannelFuture future = b.bind(address);
channelGroup.add(future.channel());
bindFutures.put(future, address);
}
return bindFutures;
}
});
Map<ChannelFuture, SocketAddress> channelFutures = bindCallFuture.awaitUninterruptibly().getNow();
if (!bindCallFuture.isSuccess()) {
channelGroup.close().awaitUninterruptibly();
throw new IOException(String.format("Failed to bind to addresses %s", addresses), bindCallFuture.cause());
}
final List<InternalInstrumented<SocketStats>> socketStats = new ArrayList<>();
for (Map.Entry<ChannelFuture, SocketAddress> entry : channelFutures.entrySet()) {
// We'd love to observe interruption, but if interrupted we will need to close the channel,
// which itself would need an await() to guarantee the port is not used when the method
// returns. See #6850
final ChannelFuture future = entry.getKey();
if (!future.awaitUninterruptibly().isSuccess()) {
channelGroup.close().awaitUninterruptibly();
throw new IOException(String.format("Failed to bind to address %s", entry.getValue()), future.cause());
}
final InternalInstrumented<SocketStats> listenSocketStats = new ListenSocket(future.channel());
channelz.addListenSocket(listenSocketStats);
socketStats.add(listenSocketStats);
future.channel().closeFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
channelz.removeListenSocket(listenSocketStats);
}
});
}
listenSocketStatsList = Collections.unmodifiableList(socketStats);
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project grpc-java by grpc.
the class NettyServerTest method startStop.
@Test
public void startStop() throws Exception {
InetSocketAddress addr = new InetSocketAddress(0);
class NoHandlerProtocolNegotiator implements ProtocolNegotiator {
boolean closed;
@Override
public ChannelHandler newHandler(GrpcHttp2ConnectionHandler handler) {
throw new UnsupportedOperationException();
}
@Override
public void close() {
closed = true;
}
@Override
public AsciiString scheme() {
return Utils.HTTP;
}
}
NoHandlerProtocolNegotiator protocolNegotiator = new NoHandlerProtocolNegotiator();
NettyServer ns = new NettyServer(Arrays.asList(addr), new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap<ChannelOption<?>, Object>(), new HashMap<ChannelOption<?>, Object>(), new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false, protocolNegotiator, Collections.<ServerStreamTracer.Factory>emptyList(), TransportTracer.getDefaultFactory(), // ignore
1, // ignore
false, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
true, // ignore
0, Attributes.EMPTY, channelz);
final SettableFuture<Void> serverShutdownCalled = SettableFuture.create();
ns.start(new ServerListener() {
@Override
public ServerTransportListener transportCreated(ServerTransport transport) {
return new NoopServerTransportListener();
}
@Override
public void serverShutdown() {
serverShutdownCalled.set(null);
}
});
// Check that we got an actual port.
assertThat(((InetSocketAddress) ns.getListenSocketAddress()).getPort()).isGreaterThan(0);
// Cleanup
ns.shutdown();
// serverShutdown() signals that resources are freed
serverShutdownCalled.get(1, TimeUnit.SECONDS);
assertThat(protocolNegotiator.closed).isTrue();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project grpc-java by grpc.
the class NettyServerTest method multiPortConnections.
@Test(timeout = 60000)
public void multiPortConnections() throws Exception {
InetSocketAddress addr1 = new InetSocketAddress(0);
InetSocketAddress addr2 = new InetSocketAddress(0);
final CountDownLatch allPortsConnectedCountDown = new CountDownLatch(2);
NettyServer ns = new NettyServer(Arrays.asList(addr1, addr2), new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap<ChannelOption<?>, Object>(), new HashMap<ChannelOption<?>, Object>(), new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false, ProtocolNegotiators.plaintext(), Collections.<ServerStreamTracer.Factory>emptyList(), TransportTracer.getDefaultFactory(), // ignore
1, // ignore
false, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
true, // ignore
0, Attributes.EMPTY, channelz);
final SettableFuture<Void> shutdownCompleted = SettableFuture.create();
ns.start(new ServerListener() {
@Override
public ServerTransportListener transportCreated(ServerTransport transport) {
allPortsConnectedCountDown.countDown();
return new NoopServerTransportListener();
}
@Override
public void serverShutdown() {
shutdownCompleted.set(null);
}
});
// SocketStats won't be available until the event loop task of adding SocketStats created by
// ns.start() complete. So submit a noop task and await until it's drained.
eventLoop.submit(new Runnable() {
@Override
public void run() {
}
}).await(5, TimeUnit.SECONDS);
List<SocketAddress> serverSockets = ns.getListenSocketAddresses();
assertEquals(2, serverSockets.size());
for (int i = 0; i < 2; i++) {
Socket socket = new Socket();
socket.connect(serverSockets.get(i), /* timeout= */
8000);
socket.close();
}
allPortsConnectedCountDown.await();
// Cleanup
ns.shutdown();
shutdownCompleted.get();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project grpc-java by grpc.
the class NettyServerTest method getPort_notStarted.
@Test
public void getPort_notStarted() {
InetSocketAddress addr = new InetSocketAddress(0);
List<InetSocketAddress> addresses = Collections.singletonList(addr);
NettyServer ns = new NettyServer(addresses, new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap<ChannelOption<?>, Object>(), new HashMap<ChannelOption<?>, Object>(), new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false, ProtocolNegotiators.plaintext(), Collections.<ServerStreamTracer.Factory>emptyList(), TransportTracer.getDefaultFactory(), // ignore
1, // ignore
false, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
1, // ignore
true, // ignore
0, Attributes.EMPTY, channelz);
assertThat(ns.getListenSocketAddress()).isEqualTo(addr);
assertThat(ns.getListenSocketAddresses()).isEqualTo(addresses);
}
Aggregations