Saturday, March 21, 2009

Protobuf - Data Interchange Format

It is a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage etc

Protocol Buffers are a way of encoding structured data in an efficient yet extensible format.Google uses Protocol Buffers for almost all of its internal RPC protocols and file formats.

You define how you want your data to be structured once, then you can use special generated
source code to easily write and read your structured data to and from a variety of data streams
and using a variety of languages

You specify how you want the information you're serializing to be structured by defining protocol
buffer message types in .proto files. Each protocol buffer message is a small logical record of
information, containing a series of name-value pairs.Once you've defined your messages, you run
the protocol buffer compiler for your application's language on your .proto file to generate data
access classes.

These data access classes can be in any of the languages such as C , Java ,Python etc.These provide simple accessors for each field as well as methods to serialize/parse the whole
structure to/from raw bytes. These generted classes can be shipped along with any client where
serialisation or deserialsation is required.

You can even update your data structure without breaking deployed programs that are compiled
against the "old" format.

Protocol buffers have many advantages over XML for serializing structured data.

Protocol buffers:
* are simpler
* are 3 to 10 times smaller
* are 20 to 100 times faster
* are less ambiguous
* generate data access classes that are easier to use programmatically

Ref : http://code.google.com/p/protobuf/

Java DataBases - Derby(Java DB) & HSQLDB

These are those two 100% Java databases, Derby and HSQLDB.

Apache Derby is developed as an open source project under the Apache 2.0 licence.
Derby was previously distributed as IBM Cloudscape.It is currently distributed as
Sun Java DB and comes packaged along with Java.

HSQLDB (Hyperthreaded Structured Query Language Database) is a relational database
management system written in Java.

Derby aims to be a fully functional relational database, and as such could be considered
as an alternative to "grown up" databases like Oracle and MySQL in some cases.

HSQL is more like a "toy database", providing a subset of the features of a fully
functional databases. But that subset of functionality may be all you need.


Benefits of HSQL over Derby.
- light weight (smaller libraries, less complex)
- supports in memory databases
- Simple to use

Benefits of Derby over HSQL
- much larger development community
- secured future (is part of JDK, is sponsored by IBM and Sun)
- More complete feature set (e.g. proper handling of transactions, encryption...)
- Scalability
- Performance with large Data Set

Memory Use

Derby is setup like a traditional database system where data is stored
on disk and a subset of that data is cached in a buffer cache or pool.

HSQL by default uses MEMORY tables (for standard CREATE TABLE
statements), this always stores the entire data in memory. Thus this is
fast, but obviously consumes memory. It is useful to look a process
sizes as well as absolute times when running benchmarks.

Ref:

http://hsqldb.org/
http://db.apache.org/derby/
http://developers.sun.com/javadb/

SQLite - LightWeight DB

SQLite is a software library that implements a self-contained, serverless,zero-configuration, transactional SQL database engine.It is opensource.

It is used by Firefox,Adobe,google,Microsoft etc

SQLite is different from most other SQL database engines in that its primary design goal is to be simple:

* Simple to administer
* Simple to operate
* Simple to embed in a larger program
* Simple to maintain and customize

It has Zero-Configuration, is Server Less ,Single Database File ,Stable Cross-Platform Database
File and gives easy user friendly APIs to write into and access from the database.

Situations Where SQLite Works Well

Embedded devices and applications
Websites
Replacement for ad hoc disk files
Low Concurrency

SQLite home page http://www.sqlite.org

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!