Example usage for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup

List of usage examples for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup

Introduction

In this page you can find the example usage for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup.

Prototype

public NioEventLoopGroup(int nThreads, Executor executor) 

Source Link

Usage

From source file:com.caricah.iotracah.server.netty.ServerImpl.java

License:Apache License

/**
 * The @link configure method is responsible for starting the implementation server processes.
 * The implementation should return once the server has started this allows
 * the launcher to maintain the life of the application.
 *
 * @throws UnRetriableException// w  w w.j  a  va 2s  . co  m
 */
public void initiate() throws UnRetriableException {

    log.info(" configure : initiating the netty server.");

    try {

        int countOfAvailableProcessors = Runtime.getRuntime().availableProcessors() + 1;

        if (Epoll.isAvailable()) {
            bossEventLoopGroup = new EpollEventLoopGroup(2, getExecutorService());
            workerEventLoopGroup = new EpollEventLoopGroup(countOfAvailableProcessors, getExecutorService());

        } else {
            bossEventLoopGroup = new NioEventLoopGroup(2, getExecutorService());
            workerEventLoopGroup = new NioEventLoopGroup(countOfAvailableProcessors, getExecutorService());
        }

        //Initialize listener for TCP
        ServerBootstrap tcpBootstrap = new ServerBootstrap();
        tcpBootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
        tcpBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
        tcpBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);

        tcpBootstrap = tcpBootstrap.group(bossEventLoopGroup, workerEventLoopGroup);

        if (Epoll.isAvailable()) {
            tcpBootstrap = tcpBootstrap.channel(EpollServerSocketChannel.class);
        } else {
            tcpBootstrap = tcpBootstrap.channel(NioServerSocketChannel.class);
        }

        tcpBootstrap = tcpBootstrap.handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(getServerInitializer(this, getConnectionTimeout()));

        ChannelFuture tcpChannelFuture = tcpBootstrap.bind(getTcpPort()).sync();
        tcpChannel = tcpChannelFuture.channel();

        if (isSslEnabled()) {
            //Initialize listener for SSL
            ServerBootstrap sslBootstrap = new ServerBootstrap();
            sslBootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
            sslBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
            sslBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);

            sslBootstrap = sslBootstrap.group(bossEventLoopGroup, workerEventLoopGroup);

            if (Epoll.isAvailable()) {
                sslBootstrap = sslBootstrap.channel(EpollServerSocketChannel.class);
            } else {
                sslBootstrap = sslBootstrap.channel(NioServerSocketChannel.class);
            }

            sslBootstrap = sslBootstrap.handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(getServerInitializer(this, getConnectionTimeout(), getSslHandler()));

            ChannelFuture sslChannelFuture = sslBootstrap.bind(getSslPort()).sync();
            sslChannel = sslChannelFuture.channel();
        }

    } catch (InterruptedException e) {

        log.error(" configure : Initialization issues ", e);

        throw new UnRetriableException(e);

    }

}

From source file:com.cloudera.livy.client.local.driver.RemoteDriver.java

License:Apache License

private RemoteDriver(String[] args) throws Exception {
    this.activeJobs = Maps.newConcurrentMap();
    this.jcLock = new Object();
    this.shutdownLock = new Object();
    localTmpDir = Files.createTempDir();

    SparkConf conf = new SparkConf();
    String serverAddress = null;/*w ww .j  a v  a 2s .  co  m*/
    int serverPort = -1;
    for (int idx = 0; idx < args.length; idx += 2) {
        String key = args[idx];
        if (key.equals("--remote-host")) {
            serverAddress = getArg(args, idx);
        } else if (key.equals("--remote-port")) {
            serverPort = Integer.parseInt(getArg(args, idx));
        } else if (key.equals("--client-id")) {
            conf.set(LocalConf.SPARK_CONF_PREFIX + CLIENT_ID.key, getArg(args, idx));
        } else if (key.equals("--secret")) {
            conf.set(LocalConf.SPARK_CONF_PREFIX + CLIENT_SECRET.key, getArg(args, idx));
        } else if (key.equals("--conf")) {
            String[] val = getArg(args, idx).split("[=]", 2);
            conf.set(val[0], val[1]);
        } else {
            throw new IllegalArgumentException("Invalid command line: " + Joiner.on(" ").join(args));
        }
    }

    executor = Executors.newCachedThreadPool();

    LOG.info("Connecting to: {}:{}", serverAddress, serverPort);

    LocalConf livyConf = new LocalConf(null);
    for (Tuple2<String, String> e : conf.getAll()) {
        if (e._1().startsWith(LocalConf.SPARK_CONF_PREFIX)) {
            String key = e._1().substring(LocalConf.SPARK_CONF_PREFIX.length());
            livyConf.set(key, e._2());
            LOG.debug("Remote Driver config: {} = {}", key, e._2());
        }
    }

    String clientId = livyConf.get(CLIENT_ID);
    Preconditions.checkArgument(clientId != null, "No client ID provided.");
    String secret = livyConf.get(CLIENT_SECRET);
    Preconditions.checkArgument(secret != null, "No secret provided.");

    System.out.println("MAPCONF-->");
    System.out.println(livyConf);
    this.egroup = new NioEventLoopGroup(livyConf.getInt(RPC_MAX_THREADS),
            new ThreadFactoryBuilder().setNameFormat("Driver-RPC-Handler-%d").setDaemon(true).build());
    this.serializer = new Serializer();
    this.protocol = new DriverProtocol(this, jcLock);

    // The RPC library takes care of timing out this.
    this.clientRpc = Rpc.createClient(livyConf, egroup, serverAddress, serverPort, clientId, secret, protocol)
            .get();
    this.running = true;

    this.clientRpc.addListener(new Rpc.Listener() {
        @Override
        public void rpcClosed(Rpc rpc) {
            LOG.warn("Shutting down driver because RPC channel was closed.");
            shutdown(null);
        }
    });

    try {
        long t1 = System.currentTimeMillis();
        LOG.info("Starting Spark context at {}", t1);
        JavaSparkContext sc = new JavaSparkContext(conf);
        LOG.info("Spark context finished initialization in {}ms", System.currentTimeMillis() - t1);
        sc.sc().addSparkListener(new DriverSparkListener(this));
        synchronized (jcLock) {
            jc = new JobContextImpl(sc, localTmpDir);
            jcLock.notifyAll();
        }
    } catch (Exception e) {
        LOG.error("Failed to start SparkContext: " + e, e);
        shutdown(e);
        synchronized (jcLock) {
            jcLock.notifyAll();
        }
        throw e;
    }

    synchronized (jcLock) {
        for (JobWrapper<?> job : jobQueue) {
            job.submit(executor);
        }
        jobQueue.clear();
    }
}

From source file:com.cloudera.livy.client.local.LocalClient.java

License:Apache License

LocalClient(LocalClientFactory factory, LocalConf conf, ContextInfo ctx) throws IOException {
    this.ctx = ctx;
    this.factory = factory;
    this.conf = conf;
    this.jobs = Maps.newConcurrentMap();
    this.protocol = new ClientProtocol();
    this.eventLoopGroup = new NioEventLoopGroup(conf.getInt(RPC_MAX_THREADS), new ThreadFactoryBuilder()
            .setNameFormat("Client-RPC-Handler-" + ctx.getClientId() + "-%d").setDaemon(true).build());

    try {//w  w  w.  j  av a 2  s .  co  m
        this.driverRpc = Rpc.createClient(conf, eventLoopGroup, ctx.getRemoteAddress(), ctx.getRemotePort(),
                ctx.getClientId(), ctx.getSecret(), protocol).get();
    } catch (Throwable e) {
        ctx.dispose(true);
        throw Throwables.propagate(e);
    }

    driverRpc.addListener(new Rpc.Listener() {
        @Override
        public void rpcClosed(Rpc rpc) {
            if (isAlive) {
                LOG.warn("Client RPC channel closed unexpectedly.");
                isAlive = false;
            }
        }
    });

    isAlive = true;
    LOG.debug("Connected to context {} ({}).", ctx.getClientId(), driverRpc.getChannel());
}

From source file:com.cloudera.livy.client.local.rpc.RpcServer.java

License:Apache License

public RpcServer(LocalConf lconf) throws IOException, InterruptedException {
    this.config = lconf;
    this.group = new NioEventLoopGroup(this.config.getInt(RPC_MAX_THREADS),
            new ThreadFactoryBuilder().setNameFormat("RPC-Handler-%d").setDaemon(true).build());
    this.channel = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override//from   ww  w .  ja v a 2 s. c  om
                public void initChannel(SocketChannel ch) throws Exception {
                    SaslServerHandler saslHandler = new SaslServerHandler(config);
                    final Rpc newRpc = Rpc.createServer(saslHandler, config, ch, group);
                    saslHandler.rpc = newRpc;

                    Runnable cancelTask = new Runnable() {
                        @Override
                        public void run() {
                            LOG.warn("Timed out waiting for hello from client.");
                            newRpc.close();
                        }
                    };
                    saslHandler.cancelTask = group.schedule(cancelTask,
                            config.getTimeAsMs(RPC_CLIENT_HANDSHAKE_TIMEOUT), TimeUnit.MILLISECONDS);
                }
            }).option(ChannelOption.SO_BACKLOG, 1).option(ChannelOption.SO_REUSEADDR, true)
            .childOption(ChannelOption.SO_KEEPALIVE, true).bind(0).sync().channel();
    this.port = ((InetSocketAddress) channel.localAddress()).getPort();
    this.pendingClients = Maps.newConcurrentMap();

    String address = config.get(RPC_SERVER_ADDRESS);
    if (address == null) {
        address = config.findLocalAddress();
    }
    this.address = address;
}

From source file:com.cloudera.livy.rsc.rpc.RpcServer.java

License:Apache License

public RpcServer(RSCConf lconf) throws IOException, InterruptedException {
    this.config = lconf;
    this.group = new NioEventLoopGroup(this.config.getInt(RPC_MAX_THREADS),
            Utils.newDaemonThreadFactory("RPC-Handler-%d"));
    this.channel = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override/*from   www  .j  a  va 2s .c  o  m*/
                public void initChannel(SocketChannel ch) throws Exception {
                    SaslServerHandler saslHandler = new SaslServerHandler(config);
                    final Rpc newRpc = Rpc.createServer(saslHandler, config, ch, group);
                    saslHandler.rpc = newRpc;

                    Runnable cancelTask = new Runnable() {
                        @Override
                        public void run() {
                            LOG.warn("Timed out waiting for hello from client.");
                            newRpc.close();
                        }
                    };
                    saslHandler.cancelTask = group.schedule(cancelTask,
                            config.getTimeAsMs(RPC_CLIENT_HANDSHAKE_TIMEOUT), TimeUnit.MILLISECONDS);
                }
            }).option(ChannelOption.SO_BACKLOG, 1).option(ChannelOption.SO_REUSEADDR, true)
            .childOption(ChannelOption.SO_KEEPALIVE, true).bind(0).sync().channel();
    this.port = ((InetSocketAddress) channel.localAddress()).getPort();
    this.pendingClients = new ConcurrentHashMap<>();

    String address = config.get(RPC_SERVER_ADDRESS);
    if (address == null) {
        address = config.findLocalAddress();
    }
    this.address = address;
}

From source file:com.cloudera.livy.rsc.RSCClient.java

License:Apache License

RSCClient(RSCConf conf, Promise<ContextInfo> ctx) throws IOException {
    this.conf = conf;
    this.jobs = new ConcurrentHashMap<>();
    this.protocol = new ClientProtocol();
    this.driverRpc = ImmediateEventExecutor.INSTANCE.newPromise();
    this.executorGroupId = EXECUTOR_GROUP_ID.incrementAndGet();
    this.eventLoopGroup = new NioEventLoopGroup(conf.getInt(RPC_MAX_THREADS),
            Utils.newDaemonThreadFactory("RSCClient-" + executorGroupId + "-%d"));

    Utils.addListener(ctx, new FutureListener<ContextInfo>() {
        @Override//from   ww  w. j a  v a  2s. com
        public void onSuccess(ContextInfo info) throws Exception {
            connectToContext(info);
        }

        @Override
        public void onFailure(Throwable error) {
            connectionError(error);
        }
    });

    isAlive = true;
}

From source file:com.couchbase.client.core.env.DefaultCoreEnvironment.java

License:Apache License

protected DefaultCoreEnvironment(final Builder builder) {
    if (++instanceCounter > MAX_ALLOWED_INSTANCES) {
        LOGGER.warn("More than " + MAX_ALLOWED_INSTANCES + " Couchbase Environments found (" + instanceCounter
                + "), this can have severe impact on performance and stability. Reuse environments!");
    }//from   w w w .  java2 s.  co m
    dcpEnabled = booleanPropertyOr("dcpEnabled", builder.dcpEnabled);
    sslEnabled = booleanPropertyOr("sslEnabled", builder.sslEnabled);
    sslKeystoreFile = stringPropertyOr("sslKeystoreFile", builder.sslKeystoreFile);
    sslKeystorePassword = stringPropertyOr("sslKeystorePassword", builder.sslKeystorePassword);
    bootstrapHttpEnabled = booleanPropertyOr("bootstrapHttpEnabled", builder.bootstrapHttpEnabled);
    bootstrapHttpDirectPort = intPropertyOr("bootstrapHttpDirectPort", builder.bootstrapHttpDirectPort);
    bootstrapHttpSslPort = intPropertyOr("bootstrapHttpSslPort", builder.bootstrapHttpSslPort);
    bootstrapCarrierEnabled = booleanPropertyOr("bootstrapCarrierEnabled", builder.bootstrapCarrierEnabled);
    bootstrapCarrierDirectPort = intPropertyOr("bootstrapCarrierDirectPort",
            builder.bootstrapCarrierDirectPort);
    bootstrapCarrierSslPort = intPropertyOr("bootstrapCarrierSslPort", builder.bootstrapCarrierSslPort);
    int ioPoolSize = intPropertyOr("ioPoolSize", builder.ioPoolSize);
    int computationPoolSize = intPropertyOr("computationPoolSize", builder.computationPoolSize);
    responseBufferSize = intPropertyOr("responseBufferSize", builder.responseBufferSize);
    requestBufferSize = intPropertyOr("requestBufferSize", builder.requestBufferSize);
    dcpConnectionBufferSize = intPropertyOr("dcpConnectionBufferSize", builder.dcpConnectionBufferSize);
    dcpConnectionBufferAckThreshold = doublePropertyOr("dcpConnectionBufferAckThreshold",
            builder.dcpConnectionBufferAckThreshold);
    dcpConnectionName = stringPropertyOr("dcpConnectionName", builder.dcpConnectionName);
    kvServiceEndpoints = intPropertyOr("kvEndpoints", builder.kvEndpoints);
    viewServiceEndpoints = intPropertyOr("viewEndpoints", builder.viewEndpoints);
    queryServiceEndpoints = intPropertyOr("queryEndpoints", builder.queryEndpoints);
    searchServiceEndpoints = intPropertyOr("searchEndpoints", builder.searchEndpoints);
    packageNameAndVersion = stringPropertyOr("packageNameAndVersion", builder.packageNameAndVersion);
    userAgent = stringPropertyOr("userAgent", builder.userAgent);
    observeIntervalDelay = builder.observeIntervalDelay;
    reconnectDelay = builder.reconnectDelay;
    retryDelay = builder.retryDelay;
    retryStrategy = builder.retryStrategy;
    maxRequestLifetime = longPropertyOr("maxRequestLifetime", builder.maxRequestLifetime);
    keepAliveInterval = longPropertyOr("keepAliveInterval", builder.keepAliveInterval);
    autoreleaseAfter = longPropertyOr("autoreleaseAfter", builder.autoreleaseAfter);
    bufferPoolingEnabled = booleanPropertyOr("bufferPoolingEnabled", builder.bufferPoolingEnabled);
    tcpNodelayEnabled = booleanPropertyOr("tcpNodelayEnabled", builder.tcpNodelayEnabled);
    mutationTokensEnabled = booleanPropertyOr("mutationTokensEnabled", builder.mutationTokensEnabled);
    socketConnectTimeout = intPropertyOr("socketConnectTimeout", builder.socketConnectTimeout);
    callbacksOnIoPool = booleanPropertyOr("callbacksOnIoPool", builder.callbacksOnIoPool);
    disconnectTimeout = longPropertyOr("disconnectTimeout", builder.disconnectTimeout);
    sslKeystore = builder.sslKeystore;

    if (ioPoolSize < MIN_POOL_SIZE) {
        LOGGER.info("ioPoolSize is less than {} ({}), setting to: {}", MIN_POOL_SIZE, ioPoolSize,
                MIN_POOL_SIZE);
        this.ioPoolSize = MIN_POOL_SIZE;
    } else {
        this.ioPoolSize = ioPoolSize;
    }

    if (computationPoolSize < MIN_POOL_SIZE) {
        LOGGER.info("computationPoolSize is less than {} ({}), setting to: {}", MIN_POOL_SIZE,
                computationPoolSize, MIN_POOL_SIZE);
        this.computationPoolSize = MIN_POOL_SIZE;
    } else {
        this.computationPoolSize = computationPoolSize;
    }

    if (builder.ioPool == null) {
        this.ioPool = new NioEventLoopGroup(ioPoolSize(), new DefaultThreadFactory("cb-io", true));
        this.ioPoolShutdownHook = new IoPoolShutdownHook(this.ioPool);
    } else {
        this.ioPool = builder.ioPool;
        this.ioPoolShutdownHook = builder.ioPoolShutdownHook == null ? new NoOpShutdownHook()
                : builder.ioPoolShutdownHook;
    }

    if (!(this.ioPoolShutdownHook instanceof NoOpShutdownHook)) {
        this.nettyShutdownHook = new NettyShutdownHook();
    } else {
        this.nettyShutdownHook = this.ioPoolShutdownHook;
    }

    if (builder.scheduler == null) {
        CoreScheduler managed = new CoreScheduler(computationPoolSize());
        this.coreScheduler = managed;
        this.coreSchedulerShutdownHook = managed;
    } else {
        this.coreScheduler = builder.scheduler;
        this.coreSchedulerShutdownHook = builder.schedulerShutdownHook == null ? new NoOpShutdownHook()
                : builder.schedulerShutdownHook;
    }
    this.eventBus = builder.eventBus == null ? new DefaultEventBus(coreScheduler) : builder.eventBus;
    this.runtimeMetricsCollector = new RuntimeMetricsCollector(eventBus, coreScheduler,
            builder.runtimeMetricsCollectorConfig == null ? DefaultMetricsCollectorConfig.create()
                    : builder.runtimeMetricsCollectorConfig);
    this.networkLatencyMetricsCollector = new NetworkLatencyMetricsCollector(eventBus, coreScheduler,
            builder.networkLatencyMetricsCollectorConfig == null ? DefaultLatencyMetricsCollectorConfig.create()
                    : builder.networkLatencyMetricsCollectorConfig);

    if (builder.defaultMetricsLoggingConsumer != null) {
        metricsCollectorSubscription = eventBus.get().filter(new Func1<CouchbaseEvent, Boolean>() {
            @Override
            public Boolean call(CouchbaseEvent evt) {
                return evt.type().equals(EventType.METRIC);
            }
        }).subscribe(builder.defaultMetricsLoggingConsumer);
    } else {
        metricsCollectorSubscription = null;
    }

    if (builder.requestBufferWaitStrategy == null) {
        requestBufferWaitStrategy = new WaitStrategyFactory() {
            @Override
            public WaitStrategy newWaitStrategy() {
                return new BlockingWaitStrategy();
            }
        };
    } else {
        requestBufferWaitStrategy = builder.requestBufferWaitStrategy;
    }
}

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

License:Apache License

/**
 * Return a new instance of {@link EventLoopGroup}.
 * <p/>//from w ww  .  j  ava2 s  .  c o  m
 * Returns an instance of {@link io.netty.channel.epoll.EpollEventLoopGroup} if {@link #isEpollAvailable() epoll is available},
 * or an instance of {@link NioEventLoopGroup} otherwise.
 *
 * @param factory the {@link ThreadFactory} instance to use to create the new instance of {@link EventLoopGroup}
 * @return a new instance of {@link EventLoopGroup}
 */
public static EventLoopGroup newEventLoopGroupInstance(ThreadFactory factory) {
    if (isEpollAvailable()) {
        try {
            return EPOLL_EVENT_LOOP_GROUP_CONSTRUCTOR.newInstance(0, factory);
        } catch (Exception e) {
            throw Throwables.propagate(e); // should not happen
        }
    } else {
        return new NioEventLoopGroup(0, factory);
    }
}

From source file:com.digitalpetri.opcua.stack.core.Stack.java

License:Apache License

/**
 * @return a shared {@link NioEventLoopGroup}.
 *///from  w w w.j a v a2s .  c  o m
public static synchronized NioEventLoopGroup sharedEventLoop() {
    if (EVENT_LOOP == null) {
        ThreadFactory threadFactory = new ThreadFactory() {
            private final AtomicLong threadNumber = new AtomicLong(0L);

            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r, "ua-netty-event-loop-" + threadNumber.getAndIncrement());
                thread.setDaemon(true);
                return thread;
            }
        };

        EVENT_LOOP = new NioEventLoopGroup(0, threadFactory);
    }

    return EVENT_LOOP;
}

From source file:com.dinstone.jrpc.transport.netty4.NettyAcceptance.java

License:Apache License

@Override
public Acceptance bind() {
    bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("N4A-Boss"));
    workGroup = new NioEventLoopGroup(transportConfig.getNioProcessorCount(),
            new DefaultThreadFactory("N4A-Work"));

    ServerBootstrap boot = new ServerBootstrap().group(bossGroup, workGroup);
    boot.channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override//ww  w  .  j a  va  2  s. c  om
        public void initChannel(SocketChannel ch) throws Exception {
            TransportProtocolDecoder decoder = new TransportProtocolDecoder();
            decoder.setMaxObjectSize(transportConfig.getMaxSize());
            TransportProtocolEncoder encoder = new TransportProtocolEncoder();
            encoder.setMaxObjectSize(transportConfig.getMaxSize());
            ch.pipeline().addLast("TransportProtocolDecoder", decoder);
            ch.pipeline().addLast("TransportProtocolEncoder", encoder);

            int intervalSeconds = transportConfig.getHeartbeatIntervalSeconds();
            ch.pipeline().addLast("IdleStateHandler", new IdleStateHandler(intervalSeconds * 2, 0, 0));
            ch.pipeline().addLast("NettyServerHandler", new NettyServerHandler());
        }
    });
    boot.option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_BACKLOG, 128);
    boot.childOption(ChannelOption.SO_RCVBUF, 16 * 1024).childOption(ChannelOption.SO_SNDBUF, 16 * 1024)
            .childOption(ChannelOption.TCP_NODELAY, true);

    try {
        boot.bind(serviceAddress).sync();

        int processorCount = transportConfig.getBusinessProcessorCount();
        if (processorCount > 0) {
            NamedThreadFactory threadFactory = new NamedThreadFactory("N4A-BusinessProcessor");
            executorService = Executors.newFixedThreadPool(processorCount, threadFactory);
        }
    } catch (Exception e) {
        throw new RuntimeException("can't bind service on " + serviceAddress, e);
    }
    LOG.info("netty acceptance bind on {}", serviceAddress);

    return this;
}