I would like to understand Tomcat's thread model for BIO and NIO connectors. I am referencing the official Tomcat 7 documentaion for connectors which can be found here. Based on it, this is what I have in doubt:
Please note: All discussion with respect to tomcat 7.
The Apache Tomcat's threadpool (is it called the connector Threadpool? ) has a number of threads ( 200 by default ). Now this means that at a particular time, 200 people can communicate with my web application. Now, taking a scenario when a particular connects with my application.
The number of threads in the pool depends on the parameters you've set for the connector in your conf/server. xml file. By default, Tomcat sets maxThreads to 200, which represents the maximum number of threads allowed to run at any given time.
By default, Tomcat allocates a single thread to perform deployment and management of applications. When multiple applications are handled by a single Tomcat instance, this can cause the startup time to increase considerably, as each application is started in sequence by the management thread.
<Service name="Catalina"> <Connector port="8443"/> <Engine> <Host name="yourhostname">
acceptorThread(s) : This is a single or at the most 2 threads (as mentioned in the doc) which is responsible only for accepting in-coming connections. This can be configured using acceptorThreadCount, and it's suggested that more than two can be used for a multi-cpu machine -
why is this ?
Accepting connections is a very low cost operation so it just doesn't make sense to dedicate more than one thread to this task.
Does this imply that the number of simultaneous open connections scales with the number of cpus versus the number of open file descriptors allowed on the server system ?
No it doesn't because it's a very low cost operation in terms of CPU. As for file descriptors, each accepted connection is going to use a file descriptor, so the maxim number of the connections the server can accept is limited by the number of available file descriptors.
maxConnections(s) :
What is the relation between this setting and acceptCount and the number of open file descriptors on the system.
maxConnections cannot be higher the number of open file descriptors on the system. Keep in mind that other processes use file descriptors as well, so may want to be conservative with maxConnections in regards to the available file descriptors, let's say maxConnections < file descriptors / 2.
Why is the default value for this so much higher for the NIO connector ( 10000 ) than for the BIO ( = maxThreads ) ?
This is because in NIO a single handles all IO, and in BIO the server need to create/use a separate thread-per connection.
acceptCount : This is the queue for requests when all request processing threads are busy.
When requests are put into this queue, is a file descriptor assigned to it as well ?
Yes, that's correct the connection request is accepted but the server not yet ready to being serving requests, so the connection is placed in the queue. This is done to prevent TCP/stack timing out connection requests which may look like a server down from the client point of view. In other words, server says 'I'm here and will process your request once I have resources to do it'.
Or is it only when a request is being actively processed, does it consume a file descriptor ?
Nope.
Hope this helps.
Regards,
Slava Imeshev
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With