Sunday, February 15, 2015

Configuring Tomcat,Connection Pooling,JVM , MySql and Monitoring Application Server in Production

Tomcat Server Configuration
Connector
There are two types of connector available AJP Connectors and http connector. AJP Connectors is for load balancing if cluster is required  with apache server configuration .This AJP  configuration is for very large system which runs lots of tomcat clusters .
Used Connector: Http Connector. This time we are using http connector.
Protoc0l
org.apache.coyote.http11.Http11Protocol - blocking Java connector(For large load it is not suitable)
org.apache.coyote.http11.Http11NioProtocol - non blocking Java connector(less threads more connection  NIO  is used, suitable for large load )
org.apache.coyote.http11.Http11AprProtocol - the APR/native connector-Does not use java implementation .Use platform  native interface using  JNI .High performance but required more configuration .
Configuration Used
Used Protocol: org.apache.coyote.http11.Http11NioProtocol
Why used: Default of max 200 threads and minimum of 10 threads simultaneously can accept default of 10000 connections.
According to mathematics use by tomcat.
Tomcat for 200 threads creates 10000 connections by default.
For one connection =200 /10000
For N no of connection=200/10000 Multiply N
So depending on the no of user increasing we will need to change its value for no of connection and no of threads.
Connection Timeout: Used 20000 sec(equivalent to 4 min)
-1 to indicate no (i.e. infinite) timeout.



DB configuration   Parameter:
The min and max size  of connection pooling should be less than My SQL  max no of connection.
Configuration Used
MY SQL max no of connection:500
Connection pooling min size connection :50
Connection Pooling Max size connection:150
Connection Timeout: 0(It means it does not time out).
Connection validation :Enabled. If connection has remained idle for 28690 sec.Then connection is validated after that  time. The term validation mean connection if it has reached idle for 8 hours then MYSQL does not accept that connection after it has reached 8 hours. So, new connection has to be recreated from the pool to replace that expired connection. This feature is controlled by following property
prop key="hibernate.c3p0.idle_test_period">28690</prop><prop key="hibernate.c3p0.preferredTestQuery">select 1</prop>

maxIdleTime: 1800sec.It is configured with following property
<prop key="hibernate.c3p0.timeout">1800</prop>
If 150 is the max pool connection and 50 is the minimum pool  connection. At some instance suppose 100 users concurrently hit the system. Then there is always minimum of 50 connections  available in pool .But, to serve 100 users more connection is required. So, pool created more 50 connections. Suppose all 100 users completed successfully. Now there is 100 connections already created in pool. So, if this property is configured then after 1800 sec it will remove all connections that exceeded  minimum connection .So, it mean after 1800 sec  50 connection will be removed and as always 50 connections are always available in pool.





As, no of user increases connection pooling  min size and max size need to be increased .But the value should be less than MY SQL  max no of connection it allows .Otherwise it throws exception .
Connection pooling  min size  < MY SQL max no of connection
Connection Pooling Max size < MY SQL max no of connection
JVM Configuration Parameter
Based  on java -XX:+PrintCommandLineFlags -XX:+PrintGCDetails –version command and VisaulGC plugin
Heap space and Perm Germ Space.
-Xms3g –Xmx3gm -XX:PermSize=1g -XX:MaxPermSize=1g -XX:+DisableExplicitGC
Normal Consideration is that
Max heap = Min heap(Equal keeps low garbage collector pausing )
Max Perm =Min perm((Equal keeps low garbage collector pausing )


As Perm Germ also is also sub divided into following
PSYoungGen (Survivor 0 area shown in visualvm  with Visual GC Plugin   )
Eden space ((Survivor 1 area shown in visualvm  with Visual GC Plugin   )
ParOldGen
The sum of PSYoungGen+ Eden space + ParOldGen =Heap Space

First Garbage collection starts from PSYoungGen .Survived object on PSYoungGen  is moved to Eden space  .Similarly in Eden space also garbage collection goes on .Again the object that has survived in Eden space  is again moved to ParOldGen  .So,at last in ParOldGen full garbage collection goes on if there is no more space available .










Garbage Collector:
Java provides 3 types of algorithm selection to application listed as follows .
The G1 Garbage Collector
 The Concurrent Mark Sweep (CMS) Collector
 The Parallel GC

Java Garbage collection is designed for two category of application need. What is much important to application  whether  Responsiveness  or Throughput. Depending on need of application algorithm can be switched.
Responsiveness
Throughput
Note: This  is the most advance option for tuning .We can consider this option  later on for garbage collector selection  .

MySQL Configuration:
wait_timeout
connect_timeout
default command timeout

Configuration  on tomcat
<Executor name="easyThreadPool"
            namePrefix="easy-"
            maxThreads="300"
            minSpareThreads="50"
         
  />

<Connector port="80" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="240000" redirectPort="443" executor="easyThreadPool" />

    <Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="240000" executor="easyThreadPool"
               SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS"
  keystoreFile="path/to/keystroke.jks " keystorePass="somepassword"
  />
Configuration  on setclasspath.sh
export JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms4536m -Xmx4536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=856m -XX:+DisableExplicitGC -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.rmi.port=9998 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=192.168.0.1"

Configuration on  sql
wait_timeout:120
max_allowed_packet:5511729

Configuration used on connection Pooling
 <prop key="hibernate.c3p0.acquire_increment">25</prop>
<prop key="hibernate.c3p0.idle_test_period">100</prop>
<prop key="hibernate.c3p0.max_size">250</prop>
<prop key="hibernate.c3p0.max_statements">100</prop>
<prop key="hibernate.c3p0.min_size">50</prop>
<prop key="hibernate.c3p0.preferredTestQuery">select 1</prop>
<prop key="hibernate.c3p0.testConnectionOnCheckout">true</prop>

Configuration used for Transaction
<property name="defaultTimeout" value="400" />



Monitoring From Local System using visualvm on GCE
Jstat configuration is already done on GCE.So,the server administation has to follow some more steps
Step 1: sudo ./jstatd -J-Djava.security.policy=jstatd.all.policy -J-Djava.rmi.server.hostname=192.168.0.1
Step 2: Run  "sudo netstat -nlp | grep jstatd"
Step 3:Open port for Jstat.
Step 4:Connect from local visualvm with IP and port
Step 5:Enable JMX on tomcat by adding following in setENV
-Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.rmi.port=9998 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=192.168.0.1"

No comments:

Post a Comment