Search in sources :

Example 1 with ExecutionHandlerContext

use of org.apache.geode.redis.internal.ExecutionHandlerContext in project geode by apache.

the class GeodeRedisServer method startRedisServer.

/**
   * Helper method to start the server listening for connections. The server is bound to the port
   * specified by {@link GeodeRedisServer#serverPort}
   * 
   * @throws IOException
   * @throws InterruptedException
   */
private void startRedisServer() throws IOException, InterruptedException {
    ThreadFactory selectorThreadFactory = new ThreadFactory() {

        private final AtomicInteger counter = new AtomicInteger();

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("GeodeRedisServer-SelectorThread-" + counter.incrementAndGet());
            t.setDaemon(true);
            return t;
        }
    };
    ThreadFactory workerThreadFactory = new ThreadFactory() {

        private final AtomicInteger counter = new AtomicInteger();

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("GeodeRedisServer-WorkerThread-" + counter.incrementAndGet());
            return t;
        }
    };
    bossGroup = null;
    workerGroup = null;
    Class<? extends ServerChannel> socketClass = null;
    if (singleThreadPerConnection) {
        bossGroup = new OioEventLoopGroup(Integer.MAX_VALUE, selectorThreadFactory);
        workerGroup = new OioEventLoopGroup(Integer.MAX_VALUE, workerThreadFactory);
        socketClass = OioServerSocketChannel.class;
    } else {
        bossGroup = new NioEventLoopGroup(this.numSelectorThreads, selectorThreadFactory);
        workerGroup = new NioEventLoopGroup(this.numWorkerThreads, workerThreadFactory);
        socketClass = NioServerSocketChannel.class;
    }
    InternalDistributedSystem system = (InternalDistributedSystem) cache.getDistributedSystem();
    String pwd = system.getConfig().getRedisPassword();
    final byte[] pwdB = Coder.stringToBytes(pwd);
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(socketClass).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            if (logger.fineEnabled())
                logger.fine("GeodeRedisServer-Connection established with " + ch.remoteAddress());
            ChannelPipeline p = ch.pipeline();
            p.addLast(ByteToCommandDecoder.class.getSimpleName(), new ByteToCommandDecoder());
            p.addLast(ExecutionHandlerContext.class.getSimpleName(), new ExecutionHandlerContext(ch, cache, regionCache, GeodeRedisServer.this, pwdB));
        }
    }).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_RCVBUF, getBufferSize()).childOption(ChannelOption.SO_KEEPALIVE, true).childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, GeodeRedisServer.connectTimeoutMillis).childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    // Bind and start to accept incoming connections.
    ChannelFuture f = b.bind(new InetSocketAddress(getBindAddress(), serverPort)).sync();
    if (this.logger.infoEnabled()) {
        String logMessage = "GeodeRedisServer started {" + getBindAddress() + ":" + serverPort + "}, Selector threads: " + this.numSelectorThreads;
        if (this.singleThreadPerConnection)
            logMessage += ", One worker thread per connection";
        else
            logMessage += ", Worker threads: " + this.numWorkerThreads;
        this.logger.info(logMessage);
    }
    this.serverChannel = f.channel();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ThreadFactory(java.util.concurrent.ThreadFactory) OioServerSocketChannel(io.netty.channel.socket.oio.OioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) ByteToCommandDecoder(org.apache.geode.redis.internal.ByteToCommandDecoder) InetSocketAddress(java.net.InetSocketAddress) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutionHandlerContext(org.apache.geode.redis.internal.ExecutionHandlerContext) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 OioEventLoopGroup (io.netty.channel.oio.OioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 OioServerSocketChannel (io.netty.channel.socket.oio.OioServerSocketChannel)1 InetSocketAddress (java.net.InetSocketAddress)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)1 ByteToCommandDecoder (org.apache.geode.redis.internal.ByteToCommandDecoder)1 ExecutionHandlerContext (org.apache.geode.redis.internal.ExecutionHandlerContext)1