Sunday, March 15, 2009

Struts - Accessing Value Stack in interceptors

Values which are set in action classes can be accessed and modified in interceptors
using the struts value stack object

Code in interceptor

ValueStack valueStack = actionInvocation.getStack();

Object object = valueStack.findValue(objectName);

if (object instanceof List) {
......

can access here directly and modify it which will be reflected in the viewing JSP

}

This method of accessing of modifying the Struts value stack will have wide applications.

Tomcat Connectors

Typically Communication protocol between apache and tomcat is AJP

Apache used mod_jk to speak AJP(Apache Jserv Protocol) to tomcat .

AJP is a binary packet based protocol - hence difficult to debug and limitation on packet size


  • AJP on tomcat side

Java Connector(BIO) - Blocking IO , one thread per connection

APR Connector - Allows more connections , allows polling

Java NIO Connector



  • Tomcats Http Connectors - can replace AJP specific features ,more options , more control ,Finely tuned,easy to debug since its text based


Nio , APR Connectors, BIO - fastest when not using persistent connections

  • mod_proxy_http - for HTTP - native protocol level support for authentication and SSL


  • HTTP Connector -

o.a.coyote.http11.Http11Protocol (BIO)

o.a.coyote.http11.Http11AprProtocol (APR)

o.a.coyote.http11.Http11NioProtocol (NIO)

HTTP/1.1 aliased to Http11 and Http11Apr depending on PATH or LD_LIBRARY_PATH settings


  • AJP Connector -

org.apache.jk.server.JkCoyoteHandler

org.apache.coyote.ajp.AjpAprProtocol

AJP/1.3 aliased to the two above
Depending on PATH or LD_LIBRARY_PATH settings


  • Use BIO if

Stability is the highest priority APR and NIO are more recent
Most content is dynamic
Keep alive is not a determining factor

protocol=“org.apache.coyote.http11.Http11Protocol”
protocol=“HTTP/1.1”

  • Use APR if

SSL is terminated at Tomcat
Platforms are Linux or Windows
Keep alive is important
Lots of static content
Using Comet feature

protocol=“org.apache.coyote.http11.Http11AprProtocol”

  • Use NIO if

Compiling APR is not an option
SSL is terminated at Tomcat
Keep alive is important
Lots of static content
Using Comet features

protocol=“org.apache.coyote.http11.Http11NioProtocol”


  • requirement connectors in preference order

Stability --> Bio, APR/NIO

SSL --> APR,NIO,BIO

Low Concurrency --> BIO,APR,NIO

High Concurreny with keepAlive --> BIO,APR,NIO

Hig Concurrency with no keep alive -->APR,NIO,BIO


  • The native APR connector is unstable on Solaris

Performance Optimization for Tomcat

1. Clear the webapp directory in tomcat ( unnecessary loading of example and non essential applications)

2. Remove Duplicate Logging in Tomcat which comes by default
.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler can be removed as logging is a costly operation

3. Using Asynchronous logging .
Tomcat by default using synchronous logging ,we can use log4j to change the default behavior

Synchronous logging (logging.properties) Can become a bottleneck
We don’t want disk IO to become the limiting factor


4. The following should be considered for choosing the connectors

BIO stablity, static content offloaded into other server only dynamic content is present , keep alive non essential

APR ssl terminating at tomcat (because of openssl library it uses ),keep alive imp , also servers static content


NIO ssl terminating at tomcat, keepalive imp, static content
(keep alive becomes because SSL connection is costly )

check out separate blog on Tomcat Connectors ...

5. Best practice

Maintain a separate setenv.bat file which will be automatically called by tomcat 6 , which has custom settings(also Vm params).
This helps in easy migration

Use catalina.properties for server.xml substitution variables



6. Avoid using the log() methods in the Servlet API

7. Apache log4j

– log4j.properties in /WEB-INF/classes
– log4j-x.y.z.jar in /WEB-INF/lib

no seperate initializations required


8 . Have overflow protection for logs using cronolog

Overflow protection (logging.properties) Size based rotation using JVM logger

www.cronolog.org



9. In highly concurrent environments turn off KeepAlive

If its on , the socket stays connected and waits for the given time interval


10. Tuning Parameters


maxThreadsTypical
Maximum nr of concurrentrequests
For BIO, max nr of open/active connections


maxKeepAliveRequests
Typical values 1, 100-200
Represents the number of requests Tomcat will handle on a TCP connection
Set to 1 disables keep alive
connectionTimeout/keepAliveTimeout controls the timeout in between requests



maxKeepAliveRequests
Set to 1 if Very high concurrency

acceptCount

Represents nr of additional connections the OS should accept on your behalf
Useful when Tomcat can’t accept connections fast enough


connectionTimeout

Represents the SO_TIMEOUT value
Essentially, max time between TCP packets during a blocking read or write
Critical to a stable system
Also used for keep alive timeout



11. Enable caching for Static conetnt , will help in faster access from disk

Configured in element
•40MB cache (default 10MB)
•cache revalidation every 60 seconds (default 5 seconds)
•caching enabled (default true)



12. GC Settings –JDK 1.5 and 1.6
-XX:+UseConcMarkSweepGC
-XX:+CMSIncrementalMode
-XX:+CMSIncrementalPacing
-XX:CMSIncrementalDutyCycleMin=0
-XX:CMSIncrementalDutyCycle=10
-XX:+UseParNewGC
-XX:+CMSPermGenSweepingEnabled
-XX:MaxGCPauseMillis=250
-XX:MaxGCMinorPauseMillis=100


13.

stops unauthorized shutdown of servers

Performance Optimization in Apache

1. Useful modules which could be used

mod_dbd -> Connection Pooling for other modules

mod_filter -> Dynamic chaing of filters, filters inserted based on request headers and response

mod_dumpio --> Dumps all IO to error log - in case of SSL dumping is done right before encryption and after decryption

mod_log_forensic --> helps in tracking request response , resulting in 2 lines of log for each request

mod_info - helps in giving info about all modules installed


2. MOD_CACHE

Enabling can improve the performance by reducing IO calls for fetching the content (since all static content are served from apache)

mod_cache --> mod_disk_cache /mod_mem_cache - determines cache implementataion

3. Rewrite rules

a. Since Rewrite rules and regular expression matching are expensive can be replaced with Proxy pass where ever possible

Example
RewriteRule ^/ws/(.*) http://localhost:8080/ws/$1 [P,L] with
ProxyPass ^/ws/ http://localhost:8080/ws/$1 [P,L]

b.Smart order of rewrite rules

The order of rewrite rules needs to be arranged in such a way that the maximum occurring instances should be available at the top , since the entire list is scanned trough for every request

Example

RewriteRule ^/(.*\.jsp) http://localhost:8080/$1 [P]
RewriteRule ^/(.*\.do) http://localhost:8080/$1 [P]

Could be replaced with

RewriteRule ^/(.*\.do) http://localhost:8080/$1 [P]
RewriteRule ^/(.*\.jsp) http://localhost:8080/$1 [P]

Since the maximum number of requests we expect will be .do requests. This will improve performance because there will be only few jsp requests and most of the times execution can stop in the above line.


4. Also other parameters for Operating systems and GC tuning will enhance the performance


5. Graceful shutdown - handle existing request , time limit can be specified in case of long time
consuming request with property GracefulShutdownTime




6. Prefork MPM- Stable but not scalable - one process per request

Worker MPM(Preferred) - more scalable , thread safeness of libs critical - one thread per request


7. Useful Parametres

Controling Idle pool - MinspareServers /MaxspareServers

MinSpareThreads/MaxSpareThreads

ThreadsPerChild

MaxRequestsPeeChild


8. KeepAlives - prevents connections (socket open close for each request)


9.Avoid DNS Lookups


10. On the Fly Compression - deflate - bandwidth reduction and faster response times

 
Free Domain Names @ .co.nr!