Reputation: 1155
CODE-1
new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),WORKER_SIZE)
CODE-2
OrderedMemoryAwareThreadPoolExecutor executor = new OrderedMemoryAwareThreadPoolExecutor(48, 0, 0, 1, TimeUnit.SECONDS);
pipeline.addLast("executor", new ExecutionHandler(executor));
If IO worker thread pool size (default is 2*count of cpu) can be set from CODE-1, what is the purpose of adding executer (a thread pool) to pipeline in CODE-2 ?
IO operations are done from worker threads. Does that mean, a client with slow connection or bad network keeps IO worker thread busy until data is completely sent ? If so, increasing WORKER_SIZE would help me prevent latencies ?
Upvotes: 1
Views: 2721
Reputation: 2486
Slow Connections does not affect Netty threads in NIO normally (check the update note).
Some points about Netty server internal threads
by default there will be only one Boss Thread per server port, and it will accept connection and handover the connection to worker thread.
to be precise: WORKER_SIZE is the maximum number of NioWorker runnables a server can have. for example If the server has only one connection, then there will be 1 worker thread. If number of connections are increasing and it can not be assigned to next worker (active connections > WORKER_SIZE), then connections will be assigned to a worker in a round robin fashion.
If IO worker thread pool size (default is 2*count of cpu) can be set from CODE-1, what is the purpose of adding executer (a thread pool) to pipeline in CODE-2 ?
If your upstream tasks are blocking then you should execute them in a separate thread pool using a execution handler. Otherwise Nio read/write will not work on time (latency?). I think having a execution handler will help to reduce the latency than setting big value to WORKER_SIZE.
IO operations are done from worker threads. Does that mean, a client with slow connection or bad network keeps IO worker thread busy until data is completely sent ? If so, increasing WORKER_SIZE would help me prevent latencies ?
Generally speaking, increasing the WORKER_SIZE >= number of cpu * 2 does not help because, NIO is non blocking and If I am not mistaken, its CPU intensive.For CPU intensive task CPU * 2 number of threads are chosen mostly.
Update:
NioWorker runs a loop with selector.select(500ms) to receive OP_READ, selector.select with timeout a blocking call and if most of the connections are slow, performance may reduce?. You can reduce the timeout in org.jboss.netty.channel.socket.nio.SelectorUtil.java and test.
Upvotes: 2
Reputation: 1931
The thread pool[s] you are adding in CODE-1 are for the boss threads, and worker threads. The boss threads accept connections and pass it on to worker thread to handle.
The executor you add in CODE-2 is for handling the messages read by the worker threads.
Slow connections will not affect performance since you are using a non-blocking architecture (NIO) - which is set in Netty to not block (it could if it wanted to)
Upvotes: 0