jjenkov / java-nio-server

A Java NIO Server using non-blocking IO all the way through.
Apache License 2.0
926 stars 461 forks source link

The application dies under high load #13

Open fanifieiev opened 4 years ago

fanifieiev commented 4 years ago

I did some testing using 'ab' tool. So, ./ab -n 100000 -c 10000 -s 120 http://localhost:9999/ makes the application stop with stack trace: Socket accepted: java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:9999 remote=/0:0:0:0:0:0:0:1:64822] Socket accepted: java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:9999 remote=/0:0:0:0:0:0:0:1:64823] Socket accepted: java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:9999 remote=/0:0:0:0:0:0:0:1:64824] Socket accepted: java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:9999 remote=/0:0:0:0:0:0:0:1:64825] Socket accepted: java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:9999 remote=/0:0:0:0:0:0:0:1:64826] Socket accepted: java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:9999 remote=/0:0:0:0:0:0:0:1:64827] Exception in thread "Thread-0" java.lang.IllegalStateException: Queue full at java.util.AbstractQueue.add(AbstractQueue.java:98) at java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:312) at com.jenkov.nioserver.SocketAccepter.run(SocketAccepter.java:44) at java.lang.Thread.run(Thread.java:748)

jjenkov commented 4 years ago

That looks to me like a too small capacity is used for the ArrayBlockingQueue between the accepting thread and the connection managing thread. Increase capacity of that queue, and the problem will be mitigated. Or, use a blocking "put" method on the queue, so that the accepting thread is blocked until there is space in the queue for another connection.

By the way, this is not a problem of "high load" - but a problem of a high number of connections being opened (and accepted) within a very short time.