Search in sources :

Example 81 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project pinot by linkedin.

the class NettyCloseChannelTest method testCloseServerChannel.

@Test
public /**
   * Client sends a request. Server closes the channel ( scenario: as it is shutting down)
   */
void testCloseServerChannel() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    Timer timer = new HashedWheelTimer();
    int port = 9089;
    MyRequestHandler handler = new MyRequestHandler("empty", null);
    MyRequestHandlerFactory handlerFactory = new MyRequestHandlerFactory(handler);
    NettyTCPServer serverConn = new NettyCloseTCPServer(port, handlerFactory);
    Thread serverThread = new Thread(serverConn, "ServerMain");
    serverThread.start();
    Thread.sleep(1000);
    ServerInstance server = new ServerInstance("localhost", port);
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    NettyTCPClientConnection clientConn = new NettyTCPClientConnection(server, eventLoopGroup, timer, metric);
    LOGGER.info("About to connect the client !!");
    boolean connected = clientConn.connect();
    LOGGER.info("Client connected !!");
    Assert.assertTrue(connected, "connected");
    Thread.sleep(1000);
    String request = "dummy request";
    LOGGER.info("Sending the request !!");
    ResponseFuture serverRespFuture = clientConn.sendRequest(Unpooled.wrappedBuffer(request.getBytes()), 1L, 5000L);
    ByteBuf serverResp = serverRespFuture.getOne();
    clientConn.close();
    serverConn.shutdownGracefully();
    Assert.assertNull(serverResp);
    Assert.assertFalse(serverRespFuture.isCancelled(), "Is Cancelled");
    Assert.assertTrue(serverRespFuture.getError() != null, "Got Exception");
    LOGGER.trace("metrics: {}", metric);
}
Also used : NettyClientMetrics(com.linkedin.pinot.transport.metrics.NettyClientMetrics) HashedWheelTimer(io.netty.util.HashedWheelTimer) ResponseFuture(com.linkedin.pinot.transport.netty.NettyClientConnection.ResponseFuture) ByteBuf(io.netty.buffer.ByteBuf) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) ServerInstance(com.linkedin.pinot.common.response.ServerInstance) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.testng.annotations.Test)

Example 82 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project pinot by linkedin.

the class NettySingleConnectionIntegrationTest method testValidatePool.

/*
   * WARNING: This test has potential failures due to timing.
   */
@Test
public void testValidatePool() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    Timer timer = new HashedWheelTimer();
    MyServer server = new MyServer();
    Thread.sleep(1000);
    // used as a key to pool. Can be anything.
    final String serverName = "SomeServer";
    ServerInstance serverInstance = server.getServerInstance();
    MetricsRegistry metricsRegistry = new MetricsRegistry();
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    PooledNettyClientResourceManager resourceManager = new PooledNettyClientResourceManager(eventLoopGroup, new HashedWheelTimer(), metric);
    ExecutorService executorService = Executors.newCachedThreadPool();
    ScheduledExecutorService timeoutExecutor = new ScheduledThreadPoolExecutor(5);
    AsyncPoolResourceManagerAdapter<ServerInstance, NettyClientConnection> rmAdapter = new AsyncPoolResourceManagerAdapter<ServerInstance, NettyClientConnection>(serverInstance, resourceManager, executorService, metricsRegistry);
    AsyncPool pool = new AsyncPoolImpl<NettyClientConnection>(serverName, rmAdapter, /*maxSize=*/
    5, /*idleTimeoutMs=*/
    100000, timeoutExecutor, executorService, /*maxWaiters=*/
    10, AsyncPoolImpl.Strategy.LRU, /*minSize=*/
    2, metricsRegistry);
    pool.start();
    Callback<NoneType> callback;
    callback = new Callback<NoneType>() {

        @Override
        public void onSuccess(NoneType arg0) {
        }

        @Override
        public void onError(Throwable arg0) {
            Assert.fail("Shutdown error");
        }
    };
    boolean serverShutdown = false;
    try {
        PoolStats stats;
        /* Validate with no connection in pool */
        // Give the pool enough time to create connections (in this case, 2 connections minSize)
        Thread.sleep(3000);
        //      System.out.println("Validating with no used objects in the pool");
        pool.validate(false);
        // System.out.println(stats);
        stats = pool.getStats();
        Assert.assertEquals(2, stats.getPoolSize());
        Assert.assertEquals(0, stats.getTotalBadDestroyed());
        /* checkout one connection, it should not destroy anything */
        AsyncResponseFuture<ServerInstance, NettyClientConnection> future = new AsyncResponseFuture<ServerInstance, NettyClientConnection>(serverInstance, "Future for " + serverName);
        Cancellable cancellable = pool.get(future);
        future.setCancellable(cancellable);
        NettyClientConnection conn = future.getOne();
        // System.out.println(stats);
        stats = pool.getStats();
        //      System.out.println("Validating with one used object in the pool");
        pool.validate(false);
        Assert.assertEquals(2, stats.getPoolSize());
        Assert.assertEquals(0, stats.getTotalBadDestroyed());
        Assert.assertEquals(1, stats.getCheckedOut());
        // Now stop the server, so that the checked out connection is invalidated.
        server.shutdown();
        serverShutdown = true;
        ;
        // Wait for the client channel to be closed.
        Thread.sleep(2000);
        pool.validate(false);
        // Wait for the callback into AsyncPoolImpl after the destroy thread completes destroying the connection
        Thread.sleep(5000);
        //      System.out.println("Validating with one used object in the pool, after server shutdown");
        // System.out.println(stats);
        stats = pool.getStats();
        Assert.assertEquals(2, stats.getPoolSize());
        Assert.assertEquals(1, stats.getTotalBadDestroyed());
        Assert.assertEquals(1, stats.getCheckedOut());
    } finally {
        server.shutdown();
        pool.shutdown(callback);
        executorService.shutdown();
        timeoutExecutor.shutdown();
    }
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Cancellable(com.linkedin.pinot.transport.common.Cancellable) ServerInstance(com.linkedin.pinot.common.response.ServerInstance) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) MetricsRegistry(com.yammer.metrics.core.MetricsRegistry) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NettyClientMetrics(com.linkedin.pinot.transport.metrics.NettyClientMetrics) AsyncPoolResourceManagerAdapter(com.linkedin.pinot.transport.pool.AsyncPoolResourceManagerAdapter) NoneType(com.linkedin.pinot.transport.common.NoneType) HashedWheelTimer(io.netty.util.HashedWheelTimer) AsyncResponseFuture(com.linkedin.pinot.transport.common.AsyncResponseFuture) PoolStats(com.linkedin.pinot.transport.metrics.PoolStats) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) AsyncPoolImpl(com.linkedin.pinot.transport.pool.AsyncPoolImpl) AsyncPool(com.linkedin.pinot.transport.pool.AsyncPool) Test(org.testng.annotations.Test)

Example 83 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project pinot by linkedin.

the class NettySingleConnectionIntegrationTest method testSingleSmallRequestResponse.

@Test
public /**
   * Test Single small request response
   * @throws Exception
   */
void testSingleSmallRequestResponse() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    Timer timer = new HashedWheelTimer();
    MyServer server = new MyServer();
    Thread.sleep(1000);
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    NettyTCPClientConnection clientConn = new NettyTCPClientConnection(server.getServerInstance(), eventLoopGroup, timer, metric);
    try {
        LOGGER.info("About to connect the client !!");
        boolean connected = clientConn.connect();
        LOGGER.info("Client connected !!");
        Assert.assertTrue(connected, "connected");
        Thread.sleep(1000);
        String request = "dummy request";
        LOGGER.info("Sending the request !!");
        ResponseFuture serverRespFuture = clientConn.sendRequest(Unpooled.wrappedBuffer(request.getBytes()), 1L, 5000L);
        LOGGER.info("Request  sent !!");
        ByteBuf serverResp = serverRespFuture.getOne();
        byte[] b2 = new byte[serverResp.readableBytes()];
        serverResp.readBytes(b2);
        String gotResponse = new String(b2);
        Assert.assertEquals(gotResponse, server.getResponseStr(), "Response Check at client");
        Assert.assertEquals(server.getHandler().getRequest(), request, "Request Check at server");
    //      System.out.println(metric);
    } finally {
        if (null != clientConn) {
            clientConn.close();
        }
        server.shutdown();
    }
}
Also used : NettyClientMetrics(com.linkedin.pinot.transport.metrics.NettyClientMetrics) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) HashedWheelTimer(io.netty.util.HashedWheelTimer) ResponseFuture(com.linkedin.pinot.transport.netty.NettyClientConnection.ResponseFuture) AsyncResponseFuture(com.linkedin.pinot.transport.common.AsyncResponseFuture) ByteBuf(io.netty.buffer.ByteBuf) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.testng.annotations.Test)

Example 84 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project pinot by linkedin.

the class NettySingleConnectionIntegrationTest method test10KSmallRequestResponses.

@Test
public /**
   * Send 10K small sized request in sequence. Verify each request and response.
   * @throws Exception
   */
void test10KSmallRequestResponses() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    MyServer server = new MyServer(null);
    Thread.sleep(1000);
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    NettyTCPClientConnection clientConn = new NettyTCPClientConnection(server.getServerInstance(), eventLoopGroup, new HashedWheelTimer(), metric);
    try {
        LOGGER.info("About to connect the client !!");
        boolean connected = clientConn.connect();
        LOGGER.info("Client connected !!");
        Assert.assertTrue(connected, "connected");
        Thread.sleep(1000);
        for (int i = 0; i < 10000; i++) {
            String request = "dummy request :" + i;
            String response = "dummy response :" + i;
            server.getHandler().setResponse(response);
            LOGGER.info("Sending the request (" + request + ")");
            ResponseFuture serverRespFuture = clientConn.sendRequest(Unpooled.wrappedBuffer(request.getBytes()), 1L, 5000L);
            LOGGER.info("Request  sent !!");
            ByteBuf serverResp = serverRespFuture.getOne();
            if (null == serverResp) {
                LOGGER.error("Got unexpected error while trying to get response.", serverRespFuture.getError());
            }
            byte[] b2 = new byte[serverResp.readableBytes()];
            serverResp.readBytes(b2);
            String gotResponse = new String(b2);
            Assert.assertEquals(gotResponse, response, "Response Check at client");
            Assert.assertEquals(server.getHandler().getRequest(), request, "Request Check at server");
        }
    } finally {
        if (null != clientConn) {
            clientConn.close();
        }
        server.shutdown();
    }
}
Also used : NettyClientMetrics(com.linkedin.pinot.transport.metrics.NettyClientMetrics) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) HashedWheelTimer(io.netty.util.HashedWheelTimer) ResponseFuture(com.linkedin.pinot.transport.netty.NettyClientConnection.ResponseFuture) AsyncResponseFuture(com.linkedin.pinot.transport.common.AsyncResponseFuture) ByteBuf(io.netty.buffer.ByteBuf) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.testng.annotations.Test)

Example 85 with EventLoopGroup

use of io.netty.channel.EventLoopGroup in project pinot by linkedin.

the class NettySingleConnectionIntegrationTest method testCancelOutstandingRequest.

@Test
public void testCancelOutstandingRequest() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    CountDownLatch latch = new CountDownLatch(1);
    MyServer server = new MyServer();
    Thread.sleep(1000);
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    NettyTCPClientConnection clientConn = new NettyTCPClientConnection(server.getServerInstance(), eventLoopGroup, new HashedWheelTimer(), metric);
    LOGGER.info("About to connect the client !!");
    boolean connected = clientConn.connect();
    LOGGER.info("Client connected !!");
    Assert.assertTrue(connected, "connected");
    Thread.sleep(1000);
    String request = "dummy request";
    LOGGER.info("Sending the request !!");
    ResponseFuture serverRespFuture = clientConn.sendRequest(Unpooled.wrappedBuffer(request.getBytes()), 1L, 5000L);
    serverRespFuture.cancel(false);
    latch.countDown();
    ByteBuf serverResp = serverRespFuture.getOne();
    Assert.assertNull(serverResp);
    Assert.assertTrue(serverRespFuture.isCancelled(), "Is Cancelled");
    clientConn.close();
    server.shutdown();
}
Also used : NettyClientMetrics(com.linkedin.pinot.transport.metrics.NettyClientMetrics) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) HashedWheelTimer(io.netty.util.HashedWheelTimer) ResponseFuture(com.linkedin.pinot.transport.netty.NettyClientConnection.ResponseFuture) AsyncResponseFuture(com.linkedin.pinot.transport.common.AsyncResponseFuture) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.testng.annotations.Test)

Aggregations

EventLoopGroup (io.netty.channel.EventLoopGroup)134 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)92 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)64 Channel (io.netty.channel.Channel)64 Bootstrap (io.netty.bootstrap.Bootstrap)61 Test (org.junit.Test)36 ChannelFuture (io.netty.channel.ChannelFuture)35 SslContext (io.netty.handler.ssl.SslContext)35 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)31 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)28 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)27 LoggingHandler (io.netty.handler.logging.LoggingHandler)26 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)25 SocketChannel (io.netty.channel.socket.SocketChannel)22 ChannelPipeline (io.netty.channel.ChannelPipeline)20 LocalAddress (io.netty.channel.local.LocalAddress)18 LocalChannel (io.netty.channel.local.LocalChannel)18 LocalServerChannel (io.netty.channel.local.LocalServerChannel)18 InetSocketAddress (java.net.InetSocketAddress)18 ByteBuf (io.netty.buffer.ByteBuf)17