Example usage for io.netty.util HashedWheelTimer HashedWheelTimer

List of usage examples for io.netty.util HashedWheelTimer HashedWheelTimer

Introduction

In this page you can find the example usage for io.netty.util HashedWheelTimer HashedWheelTimer.

Prototype

public HashedWheelTimer() 

Source Link

Document

Creates a new timer with the default thread factory ( Executors#defaultThreadFactory() ), default tick duration, and default number of ticks per wheel.

Usage

From source file:com.datastax.driver.core.NettyOptionsTest.java

License:Apache License

private void should_invoke_netty_options_hooks(int hosts, int coreConnections) throws Exception {
    NettyOptions nettyOptions = mock(NettyOptions.class, CALLS_REAL_METHODS.get());
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    Timer timer = new HashedWheelTimer();
    doReturn(eventLoopGroup).when(nettyOptions).eventLoopGroup(any(ThreadFactory.class));
    doReturn(timer).when(nettyOptions).timer(any(ThreadFactory.class));
    final ChannelHandler handler = mock(ChannelHandler.class);
    doAnswer(new Answer() {
        @Override//from   w  w  w. ja v  a  2  s  .  c om
        public Object answer(InvocationOnMock invocation) throws Throwable {
            SocketChannel channel = (SocketChannel) invocation.getArguments()[0];
            channel.pipeline().addLast("test-handler", handler);
            return null;
        }
    }).when(nettyOptions).afterChannelInitialized(any(SocketChannel.class));
    Cluster cluster = register(Cluster.builder().addContactPoints(getContactPoints().get(0))
            .withPort(ccm().getBinaryPort()).withPoolingOptions(new PoolingOptions()
                    .setConnectionsPerHost(HostDistance.LOCAL, coreConnections, coreConnections))
            .withNettyOptions(nettyOptions).build());
    // when
    cluster.connect();// force session creation to populate pools

    int expectedNumberOfCalls = TestUtils.numberOfLocalCoreConnections(cluster) * hosts + 1;
    // If the driver supports a more recent protocol version than C*, the negotiation at startup
    // will open 1 extra connection.
    if (!ProtocolVersion.NEWEST_SUPPORTED.isSupportedBy(TestUtils.findHost(cluster, 1)))
        expectedNumberOfCalls += 1;

    cluster.close();
    // then
    verify(nettyOptions, times(1)).eventLoopGroup(any(ThreadFactory.class));
    verify(nettyOptions, times(1)).channelClass();
    verify(nettyOptions, times(1)).timer(any(ThreadFactory.class));
    // per-connection hooks will be called coreConnections * hosts + 1 times:
    // the extra call is for the control connection
    verify(nettyOptions, times(expectedNumberOfCalls)).afterBootstrapInitialized(any(Bootstrap.class));
    verify(nettyOptions, times(expectedNumberOfCalls)).afterChannelInitialized(any(SocketChannel.class));
    verify(handler, times(expectedNumberOfCalls)).handlerAdded(any(ChannelHandlerContext.class));
    verify(handler, times(expectedNumberOfCalls)).handlerRemoved(any(ChannelHandlerContext.class));
    verify(nettyOptions, times(1)).onClusterClose(eventLoopGroup);
    verify(nettyOptions, times(1)).onClusterClose(timer);
    verifyNoMoreInteractions(nettyOptions);
}

From source file:com.github.spapageo.jannel.client.JannelClient.java

License:Open Source License

public JannelClient(final EventLoopGroup eventLoopGroup, final Class<? extends Channel> channelClass,
        final EventExecutorGroup eventExecutors) {
    this(new Bootstrap().group(eventLoopGroup).channel(channelClass), eventExecutors,
            new ChannelHandlerProvider(), new DefaultTranscoder(new TranscoderHelper()),
            new HashedWheelTimer());
}

From source file:com.king.platform.net.http.integration.ConnectionPool.java

License:Apache License

private void createHttpClient(boolean useConnectionPool) {
    HashedWheelTimer cleanupTimer = new HashedWheelTimer();
    SystemTimeProvider timeProvider = new SystemTimeProvider();

    ChannelPool pool = new NoChannelPool();
    if (useConnectionPool) {
        pool = new PoolingChannelPool(cleanupTimer, timeProvider, 15, mock(MetricCallback.class));
    }//from  w w w  .j av a2  s . com

    NettyHttpClientBuilder nettyHttpClientBuilder = new NettyHttpClientBuilder().setNioThreads(2)
            .setHttpCallbackExecutorThreads(2).setRootEventBus(rootEventBus);

    nettyHttpClientBuilder.setChannelPool(pool);

    httpClient = nettyHttpClientBuilder.createHttpClient();

    httpClient.setConf(ConfKeys.IDLE_TIMEOUT_MILLIS, 0);
    httpClient.setConf(ConfKeys.TOTAL_REQUEST_TIMEOUT_MILLIS, 0);

    httpClient.start();
}

From source file:com.king.platform.net.http.integration.TestingHttpClientFactory.java

License:Apache License

public TestingHttpClientFactory useChannelPool() {
    HashedWheelTimer cleanupTimer = new HashedWheelTimer();
    SystemTimeProvider timeProvider = new SystemTimeProvider();
    nettyHttpClientBuilder/*from  w ww  . j  av  a2 s.  c  om*/
            .setChannelPool(new PoolingChannelPool(cleanupTimer, timeProvider, 15000, new MetricCallback() {
                @Override
                public void onClosedConnectionTo(String host) {

                }

                @Override
                public void onCreatedConnectionTo(String host) {

                }

                @Override
                public void onReusedConnectionTo(String host) {

                }

                @Override
                public void onError(String host, RecordedTimeStamps timeStampRecorder) {

                }

                @Override
                public void onCompletedRequest(String host, RecordedTimeStamps recordedTimeStamps) {

                }

                @Override
                public void onCreatedServerPool(String host) {

                }

                @Override
                public void onRemovedServerPool(String host) {

                }

                @Override
                public void onServerPoolClosedConnection(String host, int poolSize) {

                }

                @Override
                public void onServerPoolAddedConnection(String host, int poolSize) {

                }
            }));
    return this;

}

From source file:com.king.platform.net.http.netty.NettyHttpClientBuilder.java

License:Apache License

public NettyHttpClient createHttpClient() {
    if (httpCallbackExecutor == null) {
        if (httpCallbackExecutorThreads == 0) {
            httpCallbackExecutorThreads = 2;
        }/*from  ww  w  .  j  av a2  s  .  com*/
        httpCallbackExecutor = Executors.newFixedThreadPool(httpCallbackExecutorThreads,
                newThreadFactory("HttpClient-HttpCallback"));
    }

    if (httpExecuteExecutor == null) {
        if (httpExecuteExecutorThreads == 0) {
            httpExecuteExecutorThreads = 2;
        }
        httpExecuteExecutor = Executors.newFixedThreadPool(httpExecuteExecutorThreads,
                newThreadFactory("HttpClient-Executor"));
    }

    if (nioThreadFactory == null) {
        this.nioThreadFactory = newThreadFactory("HttpClient nio event loop");
    }

    if (cleanupTimer == null) {
        cleanupTimer = new HashedWheelTimer();
    }

    if (timeProvider == null) {
        timeProvider = new SystemTimeProvider();
    }

    if (rootEventBus == null) {
        rootEventBus = new DefaultEventBus();
    }

    if (metricCallback == null) {
        metricCallback = EMPTY_METRIC_CALLBACK;
    } else {
        new MetricCollector().wireMetricCallbackOnEventBus(metricCallback, rootEventBus);
    }

    if (channelPool == null) {
        channelPool = new PoolingChannelPool(cleanupTimer, timeProvider, 30, metricCallback);
    }

    if (executionBackPressure == null) {
        executionBackPressure = new NoBackPressure();
    }

    return new NettyHttpClient(nioThreads, nioThreadFactory, httpCallbackExecutor, httpExecuteExecutor,
            cleanupTimer, timeProvider, executionBackPressure, rootEventBus, channelPool);
}

From source file:com.kradac.karview.netty.EchoServer.java

public EchoServer(int port, int timeout) {
    this.port = port;
    this.timeout = timeout;
    this.t = new HashedWheelTimer();
}

From source file:com.linkedin.pinot.broker.broker.BrokerServerBuilder.java

License:Apache License

public void buildNetwork() throws ConfigurationException {
    // build transport
    Configuration transportConfigs = _config.subset(TRANSPORT_CONFIG_PREFIX);
    TransportClientConf conf = new TransportClientConf();
    conf.init(transportConfigs);//from   w  w w  .j  a va  2 s  . co m

    _registry = new MetricsRegistry();
    MetricsHelper.initializeMetrics(_config.subset(METRICS_CONFIG_PREFIX));
    MetricsHelper.registerMetricsRegistry(_registry);
    _brokerMetrics = new BrokerMetrics(_registry);
    _brokerMetrics.initializeGlobalMeters();
    _state.set(State.INIT);
    _eventLoopGroup = new NioEventLoopGroup();
    /**
     * Some of the client metrics uses histogram which is doing synchronous operation.
     * These are fixed overhead per request/response.
     * TODO: Measure the overhead of this.
     */
    final NettyClientMetrics clientMetrics = new NettyClientMetrics(_registry, "client_");

    // Setup Netty Connection Pool
    _resourceManager = new PooledNettyClientResourceManager(_eventLoopGroup, new HashedWheelTimer(),
            clientMetrics);
    _poolTimeoutExecutor = new ScheduledThreadPoolExecutor(50);
    // _requestSenderPool = MoreExecutors.sameThreadExecutor();
    final ConnectionPoolConfig cfg = conf.getConnPool();

    _requestSenderPool = Executors.newCachedThreadPool();

    ConnectionPoolConfig connPoolCfg = conf.getConnPool();

    _connPool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>(
            connPoolCfg.getMinConnectionsPerServer(), connPoolCfg.getMaxConnectionsPerServer(),
            connPoolCfg.getIdleTimeoutMs(), connPoolCfg.getMaxBacklogPerServer(), _resourceManager,
            _poolTimeoutExecutor, _requestSenderPool, _registry);
    // MoreExecutors.sameThreadExecutor(), _registry);
    _resourceManager.setPool(_connPool);

    // Setup Routing Table
    if (conf.getRoutingMode() == RoutingMode.CONFIG) {
        final CfgBasedRouting rt = new CfgBasedRouting();
        rt.init(conf.getCfgBasedRouting());
        _routingTable = rt;
    } else {
        // Helix based routing is already initialized.
    }

    // Setup ScatterGather
    _scatterGather = new ScatterGatherImpl(_connPool, _requestSenderPool);

    // Setup Broker Request Handler
    long brokerTimeOut = DEFAULT_BROKER_TIME_OUT;
    if (_config.containsKey(BROKER_TIME_OUT_CONFIG)) {
        try {
            brokerTimeOut = _config.getLong(BROKER_TIME_OUT_CONFIG);
        } catch (Exception e) {
            LOGGER.warn("Caught exception while reading broker timeout from config, using default value", e);
        }
    }
    LOGGER.info("Broker timeout is - " + brokerTimeOut + " ms");

    _requestHandler = new BrokerRequestHandler(_routingTable, _timeBoundaryService, _scatterGather,
            new DefaultReduceService(), _brokerMetrics, brokerTimeOut);

    //TODO: Start Broker Server : Code goes here. Broker Server part should use request handler to submit requests

    LOGGER.info("Network initialized !!");
}

From source file:com.linkedin.pinot.transport.netty.NettyCloseChannelTest.java

License:Apache License

@Test
/**/*from  ww  w. j  av a  2s.c o m*/
 * Client sends a request. Before Server generates the response, the client closes the channel (scenario:as the client is shutting down)
 */
public void testCloseClientChannel() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    Timer timer = new HashedWheelTimer();
    String response = "dummy response";
    int port = 9089;
    CountDownLatch latch = new CountDownLatch(1);
    MyRequestHandler handler = new MyRequestHandler(response, latch);
    MyRequestHandlerFactory handlerFactory = new MyRequestHandlerFactory(handler);
    NettyTCPServer serverConn = new NettyTCPServer(port, handlerFactory, null);
    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);
    //Close the client
    clientConn.close();
    latch.countDown();
    ByteBuf serverResp = serverRespFuture.getOne();
    clientConn.close();
    serverConn.shutdownGracefully();
    Assert.assertNull(serverResp);
    Assert.assertFalse(serverRespFuture.isCancelled(), "Is Cancelled");
    Assert.assertTrue(serverRespFuture.getError() != null, "Got Exception");
    serverConn.shutdownGracefully();
    System.out.println(metric);
}

From source file:com.linkedin.pinot.transport.netty.NettyCloseChannelTest.java

License:Apache License

@Test
/**// ww  w .j  a va2 s. c o  m
 * Client sends a request. Server closes the channel ( scenario: as it is shutting down)
 */
public 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");
    System.out.println(metric);
}

From source file:com.linkedin.pinot.transport.netty.NettySingleConnectionIntegrationTest.java

License:Apache License

@Test
/**//from w  w  w . j  ava  2 s. c  om
 * Test Single small request response
 * @throws Exception
 */
public void testSingleSmallRequestResponse() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    Timer timer = new HashedWheelTimer();

    String response = "dummy response";
    int port = 9089;
    MyRequestHandler handler = new MyRequestHandler(response, null);
    MyRequestHandlerFactory handlerFactory = new MyRequestHandlerFactory(handler);
    NettyTCPServer serverConn = new NettyTCPServer(port, handlerFactory, null);
    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);
    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, response, "Response Check at client");
        Assert.assertEquals(handler.getRequest(), request, "Request Check at server");
        System.out.println(metric);
    } finally {
        if (null != clientConn) {
            clientConn.close();
        }

        if (null != serverConn) {
            serverConn.shutdownGracefully();
        }
    }
}