List of usage examples for io.netty.channel DefaultEventLoopGroup DefaultEventLoopGroup
public DefaultEventLoopGroup(ThreadFactory threadFactory)
From source file:com.linecorp.armeria.client.EventLoopSchedulerTest.java
License:Apache License
@Test public void stressTest() { final EventLoopGroup group = new DefaultEventLoopGroup(1024); final EventLoopScheduler s = new EventLoopScheduler(group); final List<Entry> acquiredEntries = new ArrayList<>(); stressTest(s, acquiredEntries, 0.8); stressTest(s, acquiredEntries, 0.5); stressTest(s, acquiredEntries, 0.2); // Release all acquired entries to make sure activeRequests are all 0. acquiredEntries.forEach(Entry::release); final List<Entry> entries = s.entries(endpoint); for (Entry e : entries) { assertThat(e.activeRequests()).withFailMessage("All entries must have 0 activeRequests.").isZero(); }//from ww w. j a va2 s. co m assertThat(entries.get(0).id()).isZero(); }
From source file:herddb.network.netty.NettyChannelAcceptor.java
License:Apache License
public void start() throws Exception { if (ssl) {/* www.j av a 2 s .c o m*/ if (sslCertFile == null) { LOGGER.log(Level.SEVERE, "start SSL with self-signed auto-generated certificate"); if (sslCiphers != null) { LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers); } SelfSignedCertificate ssc = new SelfSignedCertificate(); try { sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).ciphers(sslCiphers) .build(); } finally { ssc.delete(); } } else { LOGGER.log(Level.SEVERE, "start SSL with certificate " + sslCertFile.getAbsolutePath() + " chain file " + sslCertChainFile.getAbsolutePath()); if (sslCiphers != null) { LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers); } sslCtx = SslContextBuilder.forServer(sslCertChainFile, sslCertFile, sslCertPassword) .ciphers(sslCiphers).build(); } } if (callbackThreads == 0) { callbackExecutorQueue = new SynchronousQueue<Runnable>(); callbackExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, callbackExecutorQueue, threadFactory); } else { callbackExecutorQueue = new LinkedBlockingQueue<Runnable>(); callbackExecutor = new ThreadPoolExecutor(callbackThreads, callbackThreads, 0L, TimeUnit.MILLISECONDS, callbackExecutorQueue, threadFactory); } statsLogger.registerGauge("callbacksqueue", new Gauge<Integer>() { @Override public Integer getDefaultValue() { return 0; } @Override public Integer getSample() { return callbackExecutorQueue.size(); } }); InetSocketAddress address = new InetSocketAddress(host, port); LOGGER.log(Level.SEVERE, "Starting HerdDB network server at {0}:{1}", new Object[] { host, port + "" }); if (address.isUnresolved()) { throw new IOException("Bind address " + host + ":" + port + " cannot be resolved"); } ChannelInitializer<io.netty.channel.Channel> channelInitialized = new ChannelInitializer<io.netty.channel.Channel>() { @Override public void initChannel(io.netty.channel.Channel ch) throws Exception { NettyChannel session = new NettyChannel("unnamed", ch, callbackExecutor); if (acceptor != null) { acceptor.createConnection(session); } // ch.pipeline().addLast(new LoggingHandler()); // Add SSL handler first to encrypt and decrypt everything. if (ssl) { ch.pipeline().addLast(sslCtx.newHandler(ch.alloc())); } ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4)); ch.pipeline().addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); // ch.pipeline().addLast("messagedecoder", new ProtocolMessageDecoder()); ch.pipeline().addLast(new ServerInboundMessageHandler(session)); } }; if (enableRealNetwork) { if (NetworkUtils.isEnableEpoolNative()) { bossGroup = new EpollEventLoopGroup(workerThreads); workerGroup = new EpollEventLoopGroup(workerThreads); LOGGER.log(Level.FINE, "Using netty-native-epoll network type"); } else { bossGroup = new NioEventLoopGroup(workerThreads); workerGroup = new NioEventLoopGroup(workerThreads); LOGGER.log(Level.FINE, "Using nio network type"); } ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NetworkUtils.isEnableEpoolNative() ? EpollServerSocketChannel.class : NioServerSocketChannel.class) .childHandler(channelInitialized).option(ChannelOption.SO_BACKLOG, 128); ChannelFuture f = b.bind(address).sync(); this.channel = f.channel(); } if (enableJVMNetwork) { localBossGroup = new DefaultEventLoopGroup(workerThreads); localWorkerGroup = new DefaultEventLoopGroup(workerThreads); ServerBootstrap b_local = new ServerBootstrap(); b_local.group(localBossGroup, localWorkerGroup).channel(LocalServerChannel.class) .childHandler(channelInitialized); String hostAddress = NetworkUtils.getAddress(address); LocalServerRegistry.registerLocalServer(hostAddress, port, ssl); ChannelFuture local_f = b_local.bind(new LocalAddress(hostAddress + ":" + port + ":" + ssl)).sync(); this.local_channel = local_f.channel(); } }
From source file:io.grpc.netty.ProtocolNegotiatorsTest.java
License:Apache License
@Test public void httpProxy_completes() throws Exception { DefaultEventLoopGroup elg = new DefaultEventLoopGroup(1); // ProxyHandler is incompatible with EmbeddedChannel because when channelRegistered() is called // the channel is already active. LocalAddress proxy = new LocalAddress("httpProxy_completes"); SocketAddress host = InetSocketAddress.createUnresolved("specialHost", 314); ChannelInboundHandler mockHandler = mock(ChannelInboundHandler.class); Channel serverChannel = new ServerBootstrap().group(elg).channel(LocalServerChannel.class) .childHandler(mockHandler).bind(proxy).sync().channel(); ProtocolNegotiator nego = ProtocolNegotiators.httpProxy(proxy, null, null, ProtocolNegotiators.plaintext()); // normally NettyClientTransport will add WBAEH which kick start the ProtocolNegotiation, // mocking the behavior using KickStartHandler. ChannelHandler handler = new KickStartHandler( nego.newHandler(FakeGrpcHttp2ConnectionHandler.noopHandler())); Channel channel = new Bootstrap().group(elg).channel(LocalChannel.class).handler(handler).register().sync() .channel();//from ww w . j a va 2 s. co m pipeline = channel.pipeline(); // Wait for initialization to complete channel.eventLoop().submit(NOOP_RUNNABLE).sync(); channel.connect(host).sync(); serverChannel.close(); ArgumentCaptor<ChannelHandlerContext> contextCaptor = ArgumentCaptor.forClass(ChannelHandlerContext.class); Mockito.verify(mockHandler).channelActive(contextCaptor.capture()); ChannelHandlerContext serverContext = contextCaptor.getValue(); final String golden = "isThisThingOn?"; ChannelFuture negotiationFuture = channel.writeAndFlush(bb(golden, channel)); // Wait for sending initial request to complete channel.eventLoop().submit(NOOP_RUNNABLE).sync(); ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class); Mockito.verify(mockHandler).channelRead(ArgumentMatchers.<ChannelHandlerContext>any(), objectCaptor.capture()); ByteBuf b = (ByteBuf) objectCaptor.getValue(); String request = b.toString(UTF_8); b.release(); assertTrue("No trailing newline: " + request, request.endsWith("\r\n\r\n")); assertTrue("No CONNECT: " + request, request.startsWith("CONNECT specialHost:314 ")); assertTrue("No host header: " + request, request.contains("host: specialHost:314")); assertFalse(negotiationFuture.isDone()); serverContext.writeAndFlush(bb("HTTP/1.1 200 OK\r\n\r\n", serverContext.channel())).sync(); negotiationFuture.sync(); channel.eventLoop().submit(NOOP_RUNNABLE).sync(); objectCaptor = ArgumentCaptor.forClass(Object.class); Mockito.verify(mockHandler, times(2)).channelRead(ArgumentMatchers.<ChannelHandlerContext>any(), objectCaptor.capture()); b = (ByteBuf) objectCaptor.getAllValues().get(1); // If we were using the real grpcHandler, this would have been the HTTP/2 preface String preface = b.toString(UTF_8); b.release(); assertEquals(golden, preface); channel.close(); }
From source file:io.grpc.netty.ProtocolNegotiatorsTest.java
License:Apache License
@Test public void httpProxy_500() throws Exception { DefaultEventLoopGroup elg = new DefaultEventLoopGroup(1); // ProxyHandler is incompatible with EmbeddedChannel because when channelRegistered() is called // the channel is already active. LocalAddress proxy = new LocalAddress("httpProxy_500"); SocketAddress host = InetSocketAddress.createUnresolved("specialHost", 314); ChannelInboundHandler mockHandler = mock(ChannelInboundHandler.class); Channel serverChannel = new ServerBootstrap().group(elg).channel(LocalServerChannel.class) .childHandler(mockHandler).bind(proxy).sync().channel(); ProtocolNegotiator nego = ProtocolNegotiators.httpProxy(proxy, null, null, ProtocolNegotiators.plaintext()); // normally NettyClientTransport will add WBAEH which kick start the ProtocolNegotiation, // mocking the behavior using KickStartHandler. ChannelHandler handler = new KickStartHandler( nego.newHandler(FakeGrpcHttp2ConnectionHandler.noopHandler())); Channel channel = new Bootstrap().group(elg).channel(LocalChannel.class).handler(handler).register().sync() .channel();// w w w. j a v a2s . c o m pipeline = channel.pipeline(); // Wait for initialization to complete channel.eventLoop().submit(NOOP_RUNNABLE).sync(); channel.connect(host).sync(); serverChannel.close(); ArgumentCaptor<ChannelHandlerContext> contextCaptor = ArgumentCaptor.forClass(ChannelHandlerContext.class); Mockito.verify(mockHandler).channelActive(contextCaptor.capture()); ChannelHandlerContext serverContext = contextCaptor.getValue(); final String golden = "isThisThingOn?"; ChannelFuture negotiationFuture = channel.writeAndFlush(bb(golden, channel)); // Wait for sending initial request to complete channel.eventLoop().submit(NOOP_RUNNABLE).sync(); ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class); Mockito.verify(mockHandler).channelRead(any(ChannelHandlerContext.class), objectCaptor.capture()); ByteBuf request = (ByteBuf) objectCaptor.getValue(); request.release(); assertFalse(negotiationFuture.isDone()); String response = "HTTP/1.1 500 OMG\r\nContent-Length: 4\r\n\r\noops"; serverContext.writeAndFlush(bb(response, serverContext.channel())).sync(); thrown.expect(ProxyConnectException.class); try { negotiationFuture.sync(); } finally { channel.close(); } }
From source file:io.grpc.netty.ProtocolNegotiatorsTest.java
License:Apache License
@Test public void waitUntilActiveHandler_firesNegotiation() throws Exception { EventLoopGroup elg = new DefaultEventLoopGroup(1); SocketAddress addr = new LocalAddress("addr"); final AtomicReference<Object> event = new AtomicReference<>(); ChannelHandler next = new ChannelInboundHandlerAdapter() { @Override/* w w w . j a v a 2 s. co m*/ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { event.set(evt); ctx.close(); } }; Channel s = new ServerBootstrap().childHandler(new ChannelInboundHandlerAdapter()).group(elg) .channel(LocalServerChannel.class).bind(addr).sync().channel(); Channel c = new Bootstrap().handler(new WaitUntilActiveHandler(next)).channel(LocalChannel.class) .group(group).connect(addr).sync().channel(); c.pipeline().fireUserEventTriggered(ProtocolNegotiationEvent.DEFAULT); SocketAddress localAddr = c.localAddress(); ProtocolNegotiationEvent expectedEvent = ProtocolNegotiationEvent.DEFAULT .withAttributes(Attributes.newBuilder().set(Grpc.TRANSPORT_ATTR_LOCAL_ADDR, localAddr) .set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, addr) .set(GrpcAttributes.ATTR_SECURITY_LEVEL, SecurityLevel.NONE).build()); c.closeFuture().sync(); assertThat(event.get()).isInstanceOf(ProtocolNegotiationEvent.class); ProtocolNegotiationEvent actual = (ProtocolNegotiationEvent) event.get(); assertThat(actual).isEqualTo(expectedEvent); s.close(); elg.shutdownGracefully(); }
From source file:org.apache.bookkeeper.proto.BookieNettyServer.java
License:Apache License
BookieNettyServer(ServerConfiguration conf, RequestProcessor processor, ByteBufAllocator allocator) throws IOException, KeeperException, InterruptedException, BookieException { this.allocator = allocator; this.maxFrameSize = conf.getNettyMaxFrameSizeBytes(); this.conf = conf; this.requestProcessor = processor; this.authProviderFactory = AuthProviderFactoryFactory.newBookieAuthProviderFactory(conf); if (!conf.isDisableServerSocketBind()) { this.eventLoopGroup = EventLoopUtil.getServerEventLoopGroup(conf, new DefaultThreadFactory("bookie-io")); allChannels = new CleanupChannelGroup(eventLoopGroup); } else {//from ww w . j ava2 s . c om this.eventLoopGroup = null; } if (conf.isEnableLocalTransport()) { jvmEventLoopGroup = new DefaultEventLoopGroup(conf.getServerNumIOThreads()) { @Override protected EventLoop newChild(Executor executor, Object... args) throws Exception { return new DefaultEventLoop(this, executor) { @Override protected Queue<Runnable> newTaskQueue(int maxPendingTasks) { if (conf.isBusyWaitEnabled()) { return new BlockingMpscQueue<>(Math.min(maxPendingTasks, 10_000)); } else { return super.newTaskQueue(maxPendingTasks); } } }; } }; // Enable CPU affinity on IO threads if (conf.isBusyWaitEnabled()) { for (int i = 0; i < conf.getServerNumIOThreads(); i++) { jvmEventLoopGroup.next().submit(() -> { try { CpuAffinity.acquireCore(); } catch (Throwable t) { LOG.warn("Failed to acquire CPU core for thread {}", Thread.currentThread().getName(), t.getMessage(), t); } }); } } allChannels = new CleanupChannelGroup(jvmEventLoopGroup); } else { jvmEventLoopGroup = null; } bookieAddress = Bookie.getBookieAddress(conf); if (conf.getListeningInterface() == null) { bindAddress = new InetSocketAddress(conf.getBookiePort()); } else { bindAddress = bookieAddress.getSocketAddress(); } listenOn(bindAddress, bookieAddress); }