Package org.apache.zookeeper.server
Class NIOServerCnxnFactory
- java.lang.Object
-
- org.apache.zookeeper.server.ServerCnxnFactory
-
- org.apache.zookeeper.server.NIOServerCnxnFactory
-
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 Summary
Fields Modifier and Type Field Description protected int
maxClientCnxns
protected WorkerService
workerPool
static java.lang.String
ZOOKEEPER_NIO_DIRECT_BUFFER_BYTES
Default: 64kBstatic 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.static java.lang.String
ZOOKEEPER_NIO_NUM_WORKER_THREADS
Default: 2 * numCoresstatic java.lang.String
ZOOKEEPER_NIO_SESSIONLESS_CNXN_TIMEOUT
Default sessionless connection timeout in ms: 10000 (10s)static java.lang.String
ZOOKEEPER_NIO_SHUTDOWN_TIMEOUT
Default worker pool shutdown timeout in ms: 5000 (5s)-
Fields inherited from class org.apache.zookeeper.server.ServerCnxnFactory
cnxns, login, saslServerCallbackHandler, secure, zkServer, ZOOKEEPER_SERVER_CNXN_FACTORY
-
-
Constructor Summary
Constructors Constructor Description 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).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addSession(long sessionId, NIOServerCnxn cnxn)
void
closeAll()
clear all the connections in the selectorboolean
closeSession(long sessionId)
void
configure(java.net.InetSocketAddress addr, int maxcc, boolean secure)
protected NIOServerCnxn
createConnection(java.nio.channels.SocketChannel sock, java.nio.channels.SelectionKey sk, org.apache.zookeeper.server.NIOServerCnxnFactory.SelectorThread selectorThread)
void
dumpConnections(java.io.PrintWriter pwriter)
java.lang.Iterable<java.util.Map<java.lang.String,java.lang.Object>>
getAllConnectionInfo(boolean brief)
java.lang.Iterable<ServerCnxn>
getConnections()
static java.nio.ByteBuffer
getDirectBuffer()
java.net.InetSocketAddress
getLocalAddress()
int
getLocalPort()
int
getMaxClientCnxnsPerHost()
Maximum number of connections allowed from particular host (ip)void
join()
void
reconfigure(java.net.InetSocketAddress addr)
boolean
removeCnxn(NIOServerCnxn cnxn)
De-registers the connection from the various mappings maintained by the factory.void
resetAllConnectionStats()
void
setMaxClientCnxnsPerHost(int max)
Maximum number of connections allowed from particular host (ip)void
shutdown()
void
start()
void
startup(ZooKeeperServer zks, boolean startServer)
void
stop()
void
touchCnxn(NIOServerCnxn cnxn)
Add or update cnxn in our cnxnExpiryQueue-
Methods inherited from class org.apache.zookeeper.server.ServerCnxnFactory
configure, configureSaslLogin, createFactory, createFactory, createFactory, getNumAliveConnections, getZooKeeperServer, isSecure, registerConnection, setZooKeeperServer, startup, unregisterConnection
-
-
-
-
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
-
workerPool
protected WorkerService workerPool
-
-
Method Detail
-
getDirectBuffer
public static java.nio.ByteBuffer getDirectBuffer()
-
configure
public void configure(java.net.InetSocketAddress addr, int maxcc, boolean secure) throws java.io.IOException
- Specified by:
configure
in classServerCnxnFactory
- Throws:
java.io.IOException
-
reconfigure
public void reconfigure(java.net.InetSocketAddress addr)
- Specified by:
reconfigure
in classServerCnxnFactory
-
getMaxClientCnxnsPerHost
public int getMaxClientCnxnsPerHost()
Maximum number of connections allowed from particular host (ip)- Specified by:
getMaxClientCnxnsPerHost
in classServerCnxnFactory
-
setMaxClientCnxnsPerHost
public void setMaxClientCnxnsPerHost(int max)
Maximum number of connections allowed from particular host (ip)- Specified by:
setMaxClientCnxnsPerHost
in classServerCnxnFactory
-
start
public void start()
- Specified by:
start
in classServerCnxnFactory
-
startup
public void startup(ZooKeeperServer zks, boolean startServer) throws java.io.IOException, java.lang.InterruptedException
- Specified by:
startup
in classServerCnxnFactory
- Throws:
java.io.IOException
java.lang.InterruptedException
-
getLocalAddress
public java.net.InetSocketAddress getLocalAddress()
- Specified by:
getLocalAddress
in classServerCnxnFactory
-
getLocalPort
public int getLocalPort()
- Specified by:
getLocalPort
in classServerCnxnFactory
-
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, org.apache.zookeeper.server.NIOServerCnxnFactory.SelectorThread selectorThread) throws java.io.IOException
- Throws:
java.io.IOException
-
closeAll
public void closeAll()
clear all the connections in the selector- Specified by:
closeAll
in classServerCnxnFactory
-
stop
public void stop()
-
shutdown
public void shutdown()
- Specified by:
shutdown
in classServerCnxnFactory
-
addSession
public void addSession(long sessionId, NIOServerCnxn cnxn)
-
closeSession
public boolean closeSession(long sessionId)
- Specified by:
closeSession
in classServerCnxnFactory
- Returns:
- true if the cnxn that contains the sessionId exists in this ServerCnxnFactory and it's closed. Otherwise false.
-
join
public void join() throws java.lang.InterruptedException
- Specified by:
join
in classServerCnxnFactory
- Throws:
java.lang.InterruptedException
-
getConnections
public java.lang.Iterable<ServerCnxn> getConnections()
- Specified by:
getConnections
in classServerCnxnFactory
-
dumpConnections
public void dumpConnections(java.io.PrintWriter pwriter)
-
resetAllConnectionStats
public void resetAllConnectionStats()
- Specified by:
resetAllConnectionStats
in classServerCnxnFactory
-
getAllConnectionInfo
public java.lang.Iterable<java.util.Map<java.lang.String,java.lang.Object>> getAllConnectionInfo(boolean brief)
- Specified by:
getAllConnectionInfo
in classServerCnxnFactory
-
-