Search in sources :

Example 71 with NioEventLoopGroup

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

the class NetworkChannelTest method testCloseServer.

@Test
public void testCloseServer() throws Exception {
    try (NettyChannelAcceptor server = new NettyChannelAcceptor("localhost", NetworkUtils.assignFirstFreePort(), true)) {
        server.setEnableJVMNetwork(false);
        server.setEnableRealNetwork(true);
        server.setAcceptor((Channel channel) -> {
            channel.setMessagesReceiver(new ChannelEventListener() {
            });
            return (ServerSideConnection) () -> new Random().nextLong();
        });
        server.start();
        ExecutorService executor = Executors.newCachedThreadPool();
        AtomicBoolean closeNotificationReceived = new AtomicBoolean();
        try (Channel client = NettyConnector.connect(server.getHost(), server.getPort(), true, 0, 0, new ChannelEventListener() {

            @Override
            public void channelClosed(Channel channel) {
                System.out.println("client channelClosed");
                closeNotificationReceived.set(true);
            }
        }, executor, new NioEventLoopGroup(10, executor))) {
            // closing the server should eventually close the client
            server.close();
            TestUtils.waitForCondition(() -> closeNotificationReceived.get(), NOOP, 100);
            assertTrue(closeNotificationReceived.get());
        } finally {
            executor.shutdown();
        }
    }
}
Also used : ChannelEventListener(herddb.network.ChannelEventListener) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) Channel(herddb.network.Channel) ExecutorService(java.util.concurrent.ExecutorService) ServerSideConnection(herddb.network.ServerSideConnection) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 72 with NioEventLoopGroup

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

the class WebSocketClient method handhshake.

/**
 * @return true if the handshake is done properly.
 * @throws URISyntaxException throws if there is an error in the URI syntax.
 * @throws InterruptedException throws if the connecting the server is interrupted.
 */
public boolean handhshake() throws InterruptedException, URISyntaxException, SSLException {
    boolean isDone = false;
    URI uri = new URI(url);
    String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();
    final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
    final int port;
    if (uri.getPort() == -1) {
        if ("ws".equalsIgnoreCase(scheme)) {
            port = 80;
        } else if ("wss".equalsIgnoreCase(scheme)) {
            port = 443;
        } else {
            port = -1;
        }
    } else {
        port = uri.getPort();
    }
    if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {
        logger.error("Only WS(S) is supported.");
        return false;
    }
    final boolean ssl = "wss".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }
    group = new NioEventLoopGroup();
    DefaultHttpHeaders httpHeaders = new DefaultHttpHeaders();
    headers.entrySet().forEach(header -> {
        httpHeaders.add(header.getKey(), header.getValue());
    });
    try {
        // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
        // If you change it to V00, ping is not supported and remember to change
        // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
        handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, true, httpHeaders));
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc(), host, port));
                }
                p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), WebSocketClientCompressionHandler.INSTANCE, handler);
            }
        });
        channel = b.connect(uri.getHost(), port).sync().channel();
        isDone = handler.handshakeFuture().sync().isSuccess();
    } catch (Exception e) {
        logger.error("Handshake unsuccessful : " + e.getMessage(), e);
        return false;
    }
    logger.info("WebSocket Handshake successful : " + isDone);
    return isDone;
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) URI(java.net.URI) ChannelPipeline(io.netty.channel.ChannelPipeline) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 73 with NioEventLoopGroup

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

the class BookieClientTest method testGetBookieInfo.

@Test
public void testGetBookieInfo() throws IOException, InterruptedException {
    BookieSocketAddress addr = bs.getLocalAddress();
    BookieClient bc = new BookieClient(new ClientConfiguration(), new NioEventLoopGroup(), executor, scheduler, NullStatsLogger.INSTANCE);
    long flags = BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE | BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE;
    class CallbackObj {

        int rc;

        long requested;

        long freeDiskSpace, totalDiskCapacity;

        CountDownLatch latch = new CountDownLatch(1);

        CallbackObj(long requested) {
            this.requested = requested;
            this.rc = 0;
            this.freeDiskSpace = 0L;
            this.totalDiskCapacity = 0L;
        }
    }
    CallbackObj obj = new CallbackObj(flags);
    bc.getBookieInfo(addr, flags, new GetBookieInfoCallback() {

        @Override
        public void getBookieInfoComplete(int rc, BookieInfo bInfo, Object ctx) {
            CallbackObj obj = (CallbackObj) ctx;
            obj.rc = rc;
            if (rc == Code.OK) {
                if ((obj.requested & BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE) != 0) {
                    obj.freeDiskSpace = bInfo.getFreeDiskSpace();
                }
                if ((obj.requested & BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE) != 0) {
                    obj.totalDiskCapacity = bInfo.getTotalDiskSpace();
                }
            }
            obj.latch.countDown();
        }
    }, obj);
    obj.latch.await();
    System.out.println("Return code: " + obj.rc + "FreeDiskSpace: " + obj.freeDiskSpace + " TotalCapacity: " + obj.totalDiskCapacity);
    assertTrue("GetBookieInfo failed with error " + obj.rc, obj.rc == Code.OK);
    assertTrue("GetBookieInfo failed with error " + obj.rc, obj.freeDiskSpace <= obj.totalDiskCapacity);
    assertTrue("GetBookieInfo failed with error " + obj.rc, obj.totalDiskCapacity > 0);
}
Also used : GetBookieInfoCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GetBookieInfoCallback) BookieClient(org.apache.bookkeeper.proto.BookieClient) BookieInfo(org.apache.bookkeeper.client.BookieInfoReader.BookieInfo) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 74 with NioEventLoopGroup

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

the class TestPerChannelBookieClient method testConnectCloseRace.

/**
 * Test that a race does not exist between connection completion
 * and client closure. If a race does exist, this test will simply
 * hang at releaseExternalResources() as it is uninterruptible.
 * This specific race was found in
 * {@link https://issues.apache.org/jira/browse/BOOKKEEPER-485}.
 */
@Test
public void testConnectCloseRace() throws Exception {
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    OrderedExecutor executor = getOrderedSafeExecutor();
    BookieSocketAddress addr = getBookie(0);
    for (int i = 0; i < 1000; i++) {
        PerChannelBookieClient client = new PerChannelBookieClient(executor, eventLoopGroup, addr, authProvider, extRegistry);
        client.connectIfNeededAndDoOp(new GenericCallback<PerChannelBookieClient>() {

            @Override
            public void operationComplete(int rc, PerChannelBookieClient client) {
            // do nothing, we don't care about doing anything with the connection,
            // we just want to trigger it connecting.
            }
        });
        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) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 75 with NioEventLoopGroup

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

the class TestPerChannelBookieClient method testDisconnectRace.

/**
 * Test that all resources are freed if connections and disconnections
 * are interleaved randomly.
 *
 * {@link https://issues.apache.org/jira/browse/BOOKKEEPER-620}
 */
@Test
public void testDisconnectRace() throws Exception {
    final GenericCallback<PerChannelBookieClient> nullop = new GenericCallback<PerChannelBookieClient>() {

        @Override
        public void operationComplete(int rc, PerChannelBookieClient client) {
        // do nothing, we don't care about doing anything with the connection,
        // we just want to trigger it connecting.
        }
    };
    final int iterations = 100000;
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    OrderedExecutor executor = getOrderedSafeExecutor();
    BookieSocketAddress addr = getBookie(0);
    final PerChannelBookieClient client = new PerChannelBookieClient(executor, eventLoopGroup, addr, authProvider, extRegistry);
    final AtomicBoolean shouldFail = new AtomicBoolean(false);
    final AtomicBoolean running = new AtomicBoolean(true);
    final CountDownLatch disconnectRunning = new CountDownLatch(1);
    Thread connectThread = new Thread() {

        public void run() {
            try {
                if (!disconnectRunning.await(10, TimeUnit.SECONDS)) {
                    LOG.error("Disconnect thread never started");
                    shouldFail.set(true);
                }
            } catch (InterruptedException ie) {
                LOG.error("Connect thread interrupted", ie);
                Thread.currentThread().interrupt();
                running.set(false);
            }
            for (int i = 0; i < iterations && running.get(); i++) {
                client.connectIfNeededAndDoOp(nullop);
            }
            running.set(false);
        }
    };
    Thread disconnectThread = new Thread() {

        public void run() {
            disconnectRunning.countDown();
            while (running.get()) {
                client.disconnect();
            }
        }
    };
    Thread checkThread = new Thread() {

        public void run() {
            ConnectionState state;
            Channel channel;
            while (running.get()) {
                synchronized (client) {
                    state = client.state;
                    channel = client.channel;
                    if ((state == ConnectionState.CONNECTED && (channel == null || !channel.isActive())) || (state != ConnectionState.CONNECTED && channel != null && channel.isActive())) {
                        LOG.error("State({}) and channel({}) inconsistent " + channel, state, channel == null ? null : channel.isActive());
                        shouldFail.set(true);
                        running.set(false);
                    }
                }
            }
        }
    };
    connectThread.start();
    disconnectThread.start();
    checkThread.start();
    connectThread.join();
    disconnectThread.join();
    checkThread.join();
    assertFalse("Failure in threads, check logs", shouldFail.get());
    client.close();
    eventLoopGroup.shutdownGracefully();
    executor.shutdown();
}
Also used : Channel(io.netty.channel.Channel) OrderedExecutor(org.apache.bookkeeper.common.util.OrderedExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) ConnectionState(org.apache.bookkeeper.proto.PerChannelBookieClient.ConnectionState) GenericCallback(org.apache.bookkeeper.proto.BookkeeperInternalCallbacks.GenericCallback) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

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