Saturday, July 31, 2010

JBoss Performance Tuning

JBoss version EAP-4.3.0.GA_CP03
Configuration production 

Preface

This advice is primarily on how to tune and/or slim JBossAS. The two concepts are orthogonal in most cases. While reducing idle service threads through slimming won't have a large impact on performance, using less memory and resources may allow you to tune other performance aspects. Of course this does reduce startup time. Furthermore, as a general security concept -- remove services you don't use. We will separate the two categories: slimming and tuning. We start by using the production configuration and trimming from there.
Note for those concerned that this advice will make a technically non-J2EE-compliant instance of JBoss, as removing key J2EE services would cause JBoss to fail the TCK. Most performance tuning/administrative tasks done in real-world installations technically fall in this category.

  1. Tune the garbage collector

    • Set -Xms and -Xmx to the same value - This increase predictability by removing the most important sizing decision from the virtual machine.
    • Use server VM - The server JVM is better suited to longer running applications.
      To enable it simply set the -server option on the command line.
    • Turn off distributed gc - Set it to run every 30 minute at least
      -Dsun.rmi.dgc.client.gcInterval=1800000
      -Dsun.rmi.dgc.server.gcInterval=1800000
    • Turn on parallel gc - If you have multiple proessors you can do your garbage collection with multiple threads.
      Use the flag -XX:+UseParallelGC.
    • Don't choose an heap larger then 70% of your OS memory
    • Tune the Heap ratio - The heap ratio specifies how the amount of the total heap will be partitioned between the
      young and the tenured space. For example, setting -XX:NewRatio=3 means that the ratio between the
      young and tenured generation is 1:3
    • XX:+DisableExplicitGC turn's off explicit garbage collection from java code.
  2. Don't use Huge heaps, use a cluster More JVMs/smaller heaps can outperform fewer JVMs/Larger Heaps. So instead of huge heaps, use additional server nodes.
    Set up a JBoss cluster and balance work between nodes.

  3. For Disabling Development mode in JBoss Tomcat. Look for the code below in server/production/deploy/jboss-web.deployer/conf/web.xml


    • development - To disable on access checks for JSP pages compilation set this to false.
    • modificationTestInterval - If development has to be set to true for any reason (such as dynamic generation of JSPs), setting this to a high value will improve performance a lot.
    • checkInterval - If development is false and checkInterval is greater than zero, background compilations are enabled. checkInterval is the time in seconds between checks to see if a JSP page needs to be recompiled. Default is 0. 
    <servlet>
      <servlet-name>jsp</servlet-name>
      <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>

    Add the following parameters as required

    <init-param>
       <param-name>development</param-name>
       <param-value>false</param-value>
    </init-param>  

  4. Generic Database Connection Pool configuration Edit server/production/deploy/oracle-ds.xml
    <datasources>
       ....
    
    <!--pooling parameters-->
    <min-pool-size>5</min-pool-size>
    <max-pool-size>100</max-pool-size>
    <blocking-timeout-millis>5000</blocking-timeout-millis> 
    <idle-timeout-minutes>15</idle-timeout-minutes>
    <prepared-statement-cache-size>100</prepared-statement-cache-size>




    • : this is the number of prepared statements per connection to be kept open and reused in subsequent requests.
    • Disable the connection debugging
    • increase max size of pools to appropriate level

  5. Deployment Scanner - The deployment scanner scanning every 5 seconds eats up cycles especially on systems with a slow filesystem. Edit server/production/conf/jboss-service.xml. change the scan period to larger duration.
    <!-- An mbean for hot deployment/undeployment of archives. --> 
    <mbean code="org.jboss.deployment.scanner.URLDeploymentScanner" 
    name="jboss.deployment:type=DeploymentScanner,flavor=URL">
    ...
    
    
    <attribute name="ScanPeriod">5000</attribute>
    ...
    </mbean> 
  6. Lots of EJB requests ? switch to the PoolInvoker? open server/production/conf/standardjboss.xml and find the following fragment:

    <invoker-mbean>jboss:service=invoker,type=jrmp</invoker-mbean>

    On JBoss should find 4 occurrences of it: stateless-rmi-invoker, clustered-stateless-rmi-invoker, stateful-rmi-invoker,entity-rmi-invoker. Now replace this fragment for desired EJB types:

    <invoker-mbean>jboss:service=invoker,type=pooled</invoker-mbean>

  7. If you are using the Pooled Invoker then you may need to change the MaxPoolSize? attribute in pool configuration if required. Edit server/production/conf/jboss-service.xml

    <mbean code="org.jboss.invocation.pooled.server.PooledInvoker" 
    name="jboss:service=invoker,type=pooled"> 
    <attribute name="NumAcceptThreads">1</attribute>
    <attribute name="MaxPoolSize">300</attribute>
    <attribute name="ClientMaxPoolSize">300</attribute>
    <attribute name="SocketTimeout">60000</attribute>
    <attribute name="ServerBindAddress">${jboss.bind.address}</attribute>
    <attribute name="ServerBindPort">4445</attribute>
    <attribute name="ClientConnectAddress">${jboss.bind.address}</attribute>
    <attribute name="ClientConnectPort">0</attribute>
    <attribute name="EnableTcpNoDelay">false</attribute>
    <depends optional-attribute-name="TransactionManagerService">
    jboss:service=TransactionManager</depends>
    </mbean> 

  8. JBoss Logging

    * Enable the logging for file in production enviornment.
    * Disabled for console and the priority to be error.
  9. Tune the Operating System - Each operating system sets default tuning parameters differently. For Windows platforms, the default settings are usually sufficient. However, the UNIX and Linux operating systems usually need to be tuned appropriately.

    * Increase default socket send/receive buffer.
    * Optimize MTU.
    * Use Big Memory Pages.

Thursday, July 15, 2010

Debugging throug Ant

To add debug options to java command line:
 

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,
suspend=n,address=9009



If  the java process runs and terminates itself, change suspend=y, so you  can have a chance to attach the remote debugger.

To add the same debug options to ant build.xml java task:
<target name="run">
 <java fork="on"
       failonerror="true"
       classpath="xxx"
       classname="xxx">
     <jvmarg line="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,
suspend=y,address=9009" />
     <arg line="--arg1 arg2 --arg3 arg4"/>
  </java>
</target>

<arg> and <jvmarg> take either line or value attribute, but value attributes are treated as one single argument that may contain spaces. So in our case line attribute is used to specify multiple arguments separated by spaces. If value attribute was used instead of line attribute, the debug options will not take effect.

Friday, July 09, 2010

Little Observation about tinyurl

The tinyurl service takes a long URL string (which can be a site address or a document available online) and  return back a permanent shortcut pointing the original source url. For example

http://google.com generates  http://tinyurl.com/2tx
or

http://mail.yahoo.com generates  http://tinyurl.com/6dg
or 
http://tinyurl.com/5zv5vm
or 
http://gilby.com generated http://tinyurl.com/1 

 
Now the question is, how the service creates a new shortcut for all those uncountable number of available? What algorithm is working behind? Is it compressed URL form? Or is it saved/stored in database?


A little logical observation can reveal the little "secrets" working behind.

Currently the first "obvious fact" is that the shortcut has always a fixed length. In fact, the effective length looks as if it is exactly six (last 6 characters) but it has started from single digit  and now it is generating all urls with six digit  until threshold for it is reached then it will move to 7 digits.

The second "obvious fact" is that the shortcut consists of only small-caps letters and the digits 0-9.

The above observations quickly lead to the conclusion that the number of all possible configurations of the second part of the shortcut string is bounded above by 366 = (26 letters + 10 digits)6. This number equals 2176782336 and is close to int primitive data type in java
.
Thus all one has to do is to generate a class that creates a incremental unique alphanumeric number and store the original url reference in db and show it whenever that particular tinyurl is invoked.

Easy Logic,Go implement it.

Thursday, July 08, 2010

Enabling plain text copy in fckeditor

Recently I came across a very weird scenario where I was required to use fckeditor  instead of normal html text area to allow user to format text typed by him.
This all lead to another problem that user can copy from anywhere and paste it in the editor area and when which allows special formatting and external images styles and other html, which affected the content rendering functionality.
So to control the user inputs we need to allow only plain text inputs inspite what ever user have copied to clipboard from other souce using Ctrl+C keystrokes.

I was moving from pillar to post to get this plain text functionality working, looking for plugins and any proper solution in forums.

Finallybu doing minor changes to configuration file (thanks to fckeditor developer for allowing easy configurations) of fckeditor i am able to get what i want.
Open the orginal configuration file that comes with editor and look for code

FCKConfig.ForcePasteAsPlainText    = false;
FCKConfig.Keystrokes = [
    [ CTRL + 65 /*A*/, true ],
    [ CTRL + 67 /*C*/, true ],
    [ CTRL + 70 /*F*/, true ],
    [ CTRL + 83 /*S*/, true ],
    [ CTRL + 84 /*T*/, true ],
    [ CTRL + 88 /*X*/, true ],
    [ CTRL + 86 /*V*/, 'Paste' ],
    [ CTRL + 45 /*INS*/, true ],
    [ SHIFT + 45 /*INS*/, 'Paste' ],
    [ CTRL + 88 /*X*/, 'Cut' ],
    [ SHIFT + 46 /*DEL*/, 'Cut' ],
    [ CTRL + 90 /*Z*/, 'Undo' ],
    [ CTRL + 89 /*Y*/, 'Redo' ],
    [ CTRL + SHIFT + 90 /*Z*/, 'Redo' ],
    [ CTRL + 76 /*L*/, 'Link' ],
    [ CTRL + 66 /*B*/, 'Bold' ],
    [ CTRL + 73 /*I*/, 'Italic' ],
    [ CTRL + 85 /*U*/, 'Underline' ],
    [ CTRL + SHIFT + 83 /*S*/, 'Save' ],
    [ CTRL + ALT + 13 /*ENTER*/, 'FitWindow' ],
    [ SHIFT + 32 /*SPACE*/, 'Nbsp' ]
] ;

Now open your custom configuration file (or create one http://docs.cksource.com/FCKeditor_2.x/Developers_Guide/Configuration_File)

FCKConfig.ForcePasteAsPlainText    = true ;
FCKConfig.Keystrokes = [
    [ CTRL + 65 /*A*/, true ],
    [ CTRL + 67 /*C*/, true ],
    [ CTRL + 70 /*F*/, true ],
    [ CTRL + 83 /*S*/, true ],
    [ CTRL + 84 /*T*/, true ],
    [ CTRL + 88 /*X*/, true ],
    [ CTRL + 86 /*V*/, 'PasteText' ],
    [ CTRL + 45 /*INS*/, true ],
    [ SHIFT + 45 /*INS*/, 'PasteText' ],
    [ CTRL + 88 /*X*/, 'Cut' ],
    [ SHIFT + 46 /*DEL*/, 'Cut' ],
    [ CTRL + 90 /*Z*/, 'Undo' ],
    [ CTRL + 89 /*Y*/, 'Redo' ],
    [ CTRL + SHIFT + 90 /*Z*/, 'Redo' ],
    [ CTRL + 76 /*L*/, 'Link' ],
    [ CTRL + 66 /*B*/, 'Bold' ],
    [ CTRL + 73 /*I*/, 'Italic' ],
    [ CTRL + 85 /*U*/, 'Underline' ],
    [ CTRL + SHIFT + 83 /*S*/, 'Save' ],
    [ CTRL + ALT + 13 /*ENTER*/, 'FitWindow' ],
    [ SHIFT + 32 /*SPACE*/, 'Nbsp' ]
] ;