Search in sources :

Example 76 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project bookkeeper by apache.

the class TestPerChannelBookieClient method testConnectRace.

/**
 * Test race scenario found in {@link https://issues.apache.org/jira/browse/BOOKKEEPER-5}
 * where multiple clients try to connect a channel simultaneously. If not synchronised
 * correctly, this causes the netty channel to get orphaned.
 */
@Test
public void testConnectRace() throws Exception {
    GenericCallback<PerChannelBookieClient> nullop = new GenericCallback<PerChannelBookieClient>() {

        @Override
        public void operationComplete(int rc, PerChannelBookieClient pcbc) {
        // do nothing, we don't care about doing anything with the connection,
        // we just want to trigger it connecting.
        }
    };
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    OrderedExecutor executor = getOrderedSafeExecutor();
    BookieSocketAddress addr = getBookie(0);
    for (int i = 0; i < 100; i++) {
        PerChannelBookieClient client = new PerChannelBookieClient(executor, eventLoopGroup, addr, authProvider, extRegistry);
        for (int j = i; j < 10; j++) {
            client.connectIfNeededAndDoOp(nullop);
        }
        client.close();
    }
    eventLoopGroup.shutdownGracefully();
    executor.shutdown();
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) OrderedExecutor(org.apache.bookkeeper.common.util.OrderedExecutor) GenericCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 77 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project bookkeeper by apache.

the class TestPerChannelBookieClient method testRequestCompletesAfterDisconnectRace.

/**
 * Test that requests are completed even if the channel is disconnected
 * {@link https://issues.apache.org/jira/browse/BOOKKEEPER-668}.
 */
@Test
public void testRequestCompletesAfterDisconnectRace() throws Exception {
    ServerConfiguration conf = killBookie(0);
    Bookie delayBookie = new Bookie(conf) {

        @Override
        public ByteBuf readEntry(long ledgerId, long entryId) throws IOException, NoLedgerException {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                throw new IOException("Interrupted waiting", ie);
            }
            return super.readEntry(ledgerId, entryId);
        }
    };
    bsConfs.add(conf);
    bs.add(startBookie(conf, delayBookie));
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    final OrderedExecutor executor = getOrderedSafeExecutor();
    BookieSocketAddress addr = getBookie(0);
    final PerChannelBookieClient client = new PerChannelBookieClient(executor, eventLoopGroup, addr, authProvider, extRegistry);
    final CountDownLatch completion = new CountDownLatch(1);
    final ReadEntryCallback cb = new ReadEntryCallback() {

        @Override
        public void readEntryComplete(int rc, long ledgerId, long entryId, ByteBuf buffer, Object ctx) {
            completion.countDown();
        }
    };
    client.connectIfNeededAndDoOp(new GenericCallback<PerChannelBookieClient>() {

        @Override
        public void operationComplete(final int rc, PerChannelBookieClient pcbc) {
            if (rc != BKException.Code.OK) {
                executor.executeOrdered(1, new SafeRunnable() {

                    @Override
                    public void safeRun() {
                        cb.readEntryComplete(rc, 1, 1, null, null);
                    }
                });
                return;
            }
            client.readEntry(1, 1, cb, null, BookieProtocol.FLAG_DO_FENCING, "00000111112222233333".getBytes());
        }
    });
    Thread.sleep(1000);
    client.disconnect();
    client.close();
    assertTrue("Request should have completed", completion.await(5, TimeUnit.SECONDS));
    eventLoopGroup.shutdownGracefully();
    executor.shutdown();
}
Also used : ReadEntryCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.ReadEntryCallback) ServerConfiguration(org.apache.bookkeeper.conf.ServerConfiguration) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) IOException(java.io.IOException) OrderedExecutor(org.apache.bookkeeper.common.util.OrderedExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bookie(org.apache.bookkeeper.bookie.Bookie) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 78 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project bookkeeper by apache.

the class BenchBookie method main.

/**
 * @param args
 * @throws InterruptedException
 */
public static void main(String[] args) throws InterruptedException, ParseException, IOException, BKException, KeeperException {
    Options options = new Options();
    options.addOption("host", true, "Hostname or IP of bookie to benchmark");
    options.addOption("port", true, "Port of bookie to benchmark (default 3181)");
    options.addOption("zookeeper", true, "Zookeeper ensemble, (default \"localhost:2181\")");
    options.addOption("size", true, "Size of message to send, in bytes (default 1024)");
    options.addOption("warmupCount", true, "Number of messages in warmup phase (default 999)");
    options.addOption("latencyCount", true, "Number of messages in latency phase (default 5000)");
    options.addOption("throughputCount", true, "Number of messages in throughput phase (default 50000)");
    options.addOption("help", false, "This message");
    CommandLineParser parser = new PosixParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption("help") || !cmd.hasOption("host")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("BenchBookie <options>", options);
        System.exit(-1);
    }
    String addr = cmd.getOptionValue("host");
    int port = Integer.parseInt(cmd.getOptionValue("port", "3181"));
    int size = Integer.parseInt(cmd.getOptionValue("size", "1024"));
    String servers = cmd.getOptionValue("zookeeper", "localhost:2181");
    int warmUpCount = Integer.parseInt(cmd.getOptionValue("warmupCount", "999"));
    int latencyCount = Integer.parseInt(cmd.getOptionValue("latencyCount", "5000"));
    int throughputCount = Integer.parseInt(cmd.getOptionValue("throughputCount", "50000"));
    EventLoopGroup eventLoop;
    if (SystemUtils.IS_OS_LINUX) {
        try {
            eventLoop = new EpollEventLoopGroup();
        } catch (Throwable t) {
            LOG.warn("Could not use Netty Epoll event loop for benchmark {}", t.getMessage());
            eventLoop = new NioEventLoopGroup();
        }
    } else {
        eventLoop = new NioEventLoopGroup();
    }
    OrderedExecutor executor = OrderedExecutor.newBuilder().name("BenchBookieClientScheduler").numThreads(1).build();
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new DefaultThreadFactory("BookKeeperClientScheduler"));
    ClientConfiguration conf = new ClientConfiguration();
    BookieClient bc = new BookieClient(conf, eventLoop, executor, scheduler, NullStatsLogger.INSTANCE);
    LatencyCallback lc = new LatencyCallback();
    ThroughputCallback tc = new ThroughputCallback();
    long ledger = getValidLedgerId(servers);
    for (long entry = 0; entry < warmUpCount; entry++) {
        ByteBuf toSend = Unpooled.buffer(size);
        toSend.resetReaderIndex();
        toSend.resetWriterIndex();
        toSend.writeLong(ledger);
        toSend.writeLong(entry);
        toSend.writerIndex(toSend.capacity());
        bc.addEntry(new BookieSocketAddress(addr, port), ledger, new byte[20], entry, ByteBufList.get(toSend), tc, null, BookieProtocol.FLAG_NONE);
    }
    LOG.info("Waiting for warmup");
    tc.waitFor(warmUpCount);
    ledger = getValidLedgerId(servers);
    LOG.info("Benchmarking latency");
    long startTime = System.nanoTime();
    for (long entry = 0; entry < latencyCount; entry++) {
        ByteBuf toSend = Unpooled.buffer(size);
        toSend.resetReaderIndex();
        toSend.resetWriterIndex();
        toSend.writeLong(ledger);
        toSend.writeLong(entry);
        toSend.writerIndex(toSend.capacity());
        lc.resetComplete();
        bc.addEntry(new BookieSocketAddress(addr, port), ledger, new byte[20], entry, ByteBufList.get(toSend), lc, null, BookieProtocol.FLAG_NONE);
        lc.waitForComplete();
    }
    long endTime = System.nanoTime();
    LOG.info("Latency: " + (((double) (endTime - startTime)) / ((double) latencyCount)) / 1000000.0);
    ledger = getValidLedgerId(servers);
    LOG.info("Benchmarking throughput");
    startTime = System.currentTimeMillis();
    tc = new ThroughputCallback();
    for (long entry = 0; entry < throughputCount; entry++) {
        ByteBuf toSend = Unpooled.buffer(size);
        toSend.resetReaderIndex();
        toSend.resetWriterIndex();
        toSend.writeLong(ledger);
        toSend.writeLong(entry);
        toSend.writerIndex(toSend.capacity());
        bc.addEntry(new BookieSocketAddress(addr, port), ledger, new byte[20], entry, ByteBufList.get(toSend), tc, null, BookieProtocol.FLAG_NONE);
    }
    tc.waitFor(throughputCount);
    endTime = System.currentTimeMillis();
    LOG.info("Throughput: " + ((long) throughputCount) * 1000 / (endTime - startTime));
    bc.close();
    scheduler.shutdown();
    eventLoop.shutdownGracefully();
    executor.shutdown();
}
Also used : Options(org.apache.commons.cli.Options) BookieClient(org.apache.bookkeeper.proto.BookieClient) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PosixParser(org.apache.commons.cli.PosixParser) OrderedExecutor(org.apache.bookkeeper.common.util.OrderedExecutor) ByteBuf(io.netty.buffer.ByteBuf) HelpFormatter(org.apache.commons.cli.HelpFormatter) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) CommandLine(org.apache.commons.cli.CommandLine) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) CommandLineParser(org.apache.commons.cli.CommandLineParser) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration)

Example 79 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project apiRecord by tobecoder2015.

the class NettyHttpProxyServer method start.

public void start(int port) {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    // ChannelInboundHandlerAdapter
    try {
        init();
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast("httpCodec", new HttpServerCodec());
                ch.pipeline().addLast(new ReadTimeoutHandler(10));
                ch.pipeline().addLast(new WriteTimeoutHandler(10));
                ch.pipeline().addLast("serverHandle", new HttpProxyServerHandle(proxyInterceptFactory.build()));
            }
        });
        ChannelFuture f = b.bind(port).sync();
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) WriteTimeoutHandler(io.netty.handler.timeout.WriteTimeoutHandler) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) HttpProxyServerHandle(com.wing.apirecord.core.handler.HttpProxyServerHandle) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 80 with NioEventLoopGroup

use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project apiRecord by tobecoder2015.

the class NettyHttpProxyServer method init.

private void init() throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    Method method = HttpResponseStatus.class.getDeclaredMethod("newStatus", int.class, String.class);
    method.setAccessible(true);
    SUCCESS = (HttpResponseStatus) method.invoke(null, 200, "Connection established");
    clientSslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    issuer = CertUtil.getSubject(classLoader.getResourceAsStream("ca.crt"));
    // CA私钥和公钥用于给动态生成的网站SSL证书签证
    caPriKey = CertUtil.loadPriKey(classLoader.getResourceAsStream("ca_private.pem"));
    caPubKey = CertUtil.loadPubKey(classLoader.getResourceAsStream("ca_public.der"));
    // 生产一对随机公私钥用于网站SSL证书动态创建
    KeyPair keyPair = CertUtil.genKeyPair();
    serverPriKey = keyPair.getPrivate();
    serverPubKey = keyPair.getPublic();
    proxyGroup = new NioEventLoopGroup();
    if (proxyInterceptFactory == null) {
        proxyInterceptFactory = new DefaultInterceptFactory();
    }
}
Also used : DefaultInterceptFactory(com.wing.apirecord.core.intercept.DefaultInterceptFactory) KeyPair(java.security.KeyPair) Method(java.lang.reflect.Method) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Aggregations

NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)521 EventLoopGroup (io.netty.channel.EventLoopGroup)256 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)226 Bootstrap (io.netty.bootstrap.Bootstrap)184 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)178 SocketChannel (io.netty.channel.socket.SocketChannel)169 ChannelFuture (io.netty.channel.ChannelFuture)157 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)157 Channel (io.netty.channel.Channel)138 InetSocketAddress (java.net.InetSocketAddress)118 LoggingHandler (io.netty.handler.logging.LoggingHandler)87 ChannelPipeline (io.netty.channel.ChannelPipeline)84 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)73 SslContext (io.netty.handler.ssl.SslContext)64 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)49 IOException (java.io.IOException)49 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)47 ByteBuf (io.netty.buffer.ByteBuf)44 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)43 ChannelInitializer (io.netty.channel.ChannelInitializer)41