Class NIOServerCnxnFactory

  • Direct Known Subclasses:
    ControllableConnectionFactory

    public class NIOServerCnxnFactory
    extends ServerCnxnFactory
    NIOServerCnxnFactory implements a multi-threaded ServerCnxnFactory using NIO non-blocking socket calls. Communication between threads is handled via queues. - 1 accept thread, which accepts new connections and assigns to a selector thread - 1-N selector threads, each of which selects on 1/N of the connections. The reason the factory supports more than one selector thread is that with large numbers of connections, select() itself can become a performance bottleneck. - 0-M socket I/O worker threads, which perform basic socket reads and writes. If configured with 0 worker threads, the selector threads do the socket I/O directly. - 1 connection expiration thread, which closes idle connections; this is necessary to expire connections on which no session is established. Typical (default) thread counts are: on a 32 core machine, 1 accept thread, 1 connection expiration thread, 4 selector threads, and 64 worker threads.
    • Field Detail

      • ZOOKEEPER_NIO_SESSIONLESS_CNXN_TIMEOUT

        public static final java.lang.String ZOOKEEPER_NIO_SESSIONLESS_CNXN_TIMEOUT
        Default sessionless connection timeout in ms: 10000 (10s)
        See Also:
        Constant Field Values
      • ZOOKEEPER_NIO_NUM_SELECTOR_THREADS

        public static final java.lang.String ZOOKEEPER_NIO_NUM_SELECTOR_THREADS
        With 500 connections to an observer with watchers firing on each, is unable to exceed 1GigE rates with only 1 selector. Defaults to using 2 selector threads with 8 cores and 4 with 32 cores. Expressed as sqrt(numCores/2). Must have at least 1 selector thread.
        See Also:
        Constant Field Values
      • ZOOKEEPER_NIO_NUM_WORKER_THREADS

        public static final java.lang.String ZOOKEEPER_NIO_NUM_WORKER_THREADS
        Default: 2 * numCores
        See Also:
        Constant Field Values
      • ZOOKEEPER_NIO_DIRECT_BUFFER_BYTES

        public static final java.lang.String ZOOKEEPER_NIO_DIRECT_BUFFER_BYTES
        Default: 64kB
        See Also:
        Constant Field Values
      • ZOOKEEPER_NIO_SHUTDOWN_TIMEOUT

        public static final java.lang.String ZOOKEEPER_NIO_SHUTDOWN_TIMEOUT
        Default worker pool shutdown timeout in ms: 5000 (5s)
        See Also:
        Constant Field Values
      • maxClientCnxns

        protected int maxClientCnxns
    • Constructor Detail

      • NIOServerCnxnFactory

        public NIOServerCnxnFactory()
        Construct a new server connection factory which will accept an unlimited number of concurrent connections from each client (up to the file descriptor limits of the operating system). startup(zks) must be called subsequently.
    • Method Detail

      • getDirectBuffer

        public static java.nio.ByteBuffer getDirectBuffer()
      • configure

        public void configure​(java.net.InetSocketAddress addr,
                              int maxcc,
                              int backlog,
                              boolean secure)
                       throws java.io.IOException
        Specified by:
        configure in class ServerCnxnFactory
        Throws:
        java.io.IOException
      • setMaxClientCnxnsPerHost

        public void setMaxClientCnxnsPerHost​(int max)
        Maximum number of connections allowed from particular host (ip)
        Specified by:
        setMaxClientCnxnsPerHost in class ServerCnxnFactory
      • startup

        public void startup​(ZooKeeperServer zks,
                            boolean startServer)
                     throws java.io.IOException,
                            java.lang.InterruptedException
        Specified by:
        startup in class ServerCnxnFactory
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • removeCnxn

        public boolean removeCnxn​(NIOServerCnxn cnxn)
        De-registers the connection from the various mappings maintained by the factory.
      • touchCnxn

        public void touchCnxn​(NIOServerCnxn cnxn)
        Add or update cnxn in our cnxnExpiryQueue
        Parameters:
        cnxn -
      • createConnection

        protected NIOServerCnxn createConnection​(java.nio.channels.SocketChannel sock,
                                                 java.nio.channels.SelectionKey sk,
                                                 NIOServerCnxnFactory.SelectorThread selectorThread)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • stop

        public void stop()
      • join

        public void join()
                  throws java.lang.InterruptedException
        Specified by:
        join in class ServerCnxnFactory
        Throws:
        java.lang.InterruptedException
      • dumpConnections

        public void dumpConnections​(java.io.PrintWriter pwriter)
      • getAllConnectionInfo

        public java.lang.Iterable<java.util.Map<java.lang.String,​java.lang.Object>> getAllConnectionInfo​(boolean brief)
        Specified by:
        getAllConnectionInfo in class ServerCnxnFactory