Thursday, January 12, 2012

HTTP load balancing using Apache and Mod_jk


Environment
J2EE Application deployed in JBOSS AS 5. Same such Jboss instances are clustered. If we need to load balance the HTTP requests across the various jboss instances the following steps can be followed. This post basically depicts the configurations done to setup a HTTP load balancer.

Software Packages Required

  • Apache 2.2.x
  • mod_jk 1.2.x

Apache server can be downloaded directly from Apache web site at http://httpd.apache.org/.

The mod_jk 1.2.x binary can be downloaded from http://www.apache.org/dist/jakarta/tomcat-connectors/jk/binaries/. Rename the downloaded file to mod_jk.so and copy it under APACHE_HOME/modules/.

Configure Apache to load mod_jk
Step1. Modify APACHE_HOME/conf/httpd.conf and add a single line at the end of the file:
# Include mod_jk's specific configuration file  
Include conf/mod-jk.conf
Step2. Create a new file named APACHE_HOME/conf/mod-jk.conf:
# Load mod_jk module
# Specify the filename of the mod_jk lib
LoadModule jk_module modules/mod_jk.so
 
# Where to find workers.properties
JkWorkersFile conf/workers.properties

# Where to put jk logs
JkLogFile logs/mod_jk.log
 
# Set the jk log level [debug/error/info]
JkLogLevel info 
 
# Select the log format
JkLogStampFormat  "[%a %b %d %H:%M:%S %Y]"
               
# Mount your applications
JkMount /application/* loadbalancer
 
Configure worker nodes in mod_jk
The mod_jk workers file conf/workers.properties should be configured. This file specifies where the different Servlet containers are located and how calls should be load-balanced across them. The configuration file contains one section for each target servlet container and one global section. For a two nodes setup, the file could look like this:

# Define list of workers that will be used
# for mapping requests
worker.list=loadbalancer,status

# Define Node1
# modify the host as your host IP or DNS name.
worker.node1.port=8009
worker.node1.host=192.168.2.50
worker.node1.type=ajp13
worker.node1.lbfactor=1
worker.node1.cachesize=10

# Define Node2
# modify the host as your host IP or DNS name.
worker.node2.port=8009
worker.node2.host= 192.168.2.52
worker.node2.type=ajp13
worker.node2.lbfactor=1
worker.node2.cachesize=10

# Load-balancing behaviour
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1,node2
worker.loadbalancer.sticky_session=1
#worker.list=loadbalancer


# Status worker for managing load balancer

worker.status.type=status

Sticky Session
The sticky_session property specifies the cluster behavior for HTTP sessions. If worker.loadbalancer.sticky_session=0, each request will be load balanced between node1 and node2; i.e., different requests for the same session will go to different servers. But when a user opens a session on one server, it is always necessary to always forward this user's requests to the same server, as long as that server is available. This is called a "sticky session", as the client is always using the same server he reached on his first request. To enable session stickiness, you need to set worker.loadbalancer.sticky_session to 1.

Lbfactor
The lbfactor attribute is the load-balancing factor for a specific worker. It is used to define the priority (or weight) a node should have over other nodes. The higher this number is for a given worker relative to the other workers, the more HTTP requests the worker will receive. This setting can be used to differentiate servers with different processing power.


Configuring JBoss to work with mod_jk

On each clustered JBoss node, we have to name the node according to the name specified in workers.properties. For instance, on JBoss instance node1, edit the JBOSS_HOME/server/all/deploy/jbossweb.sar/server.xml file Locate the <Engine> element and add an attribute jvmRoute:


<Engine name="jboss.web" defaultHost="localhost" jvmRoute="node1">
... ...
</Engine>

MySQL Server performance tuning



Tuning the following parameters in mysql configuration file were found very efficient. These below parameters should be configured in my.cnf(linux) and my.ini(windows)
 
table_cache
It changes the maximum number of files the server keeps open. Opening tables is expensive.
Each time MySQL accesses a table, it places it in the cache. If the system accesses many tables, it is faster to have these in the cache
table_cache is related to max_connections. For example, for 200 concurrent running connections, its optimal to have a table cache size of at least 200 * N, where N is the maximum number of tables per join in any of the queries which you execute. You must also reserve some extra file descriptors for temporary tables and files.


sort_buffer_size
It is the buffer memory used to process the sort queries. It has negative impact if increased too much with low volume of data. Optimum size is 2MB .


read_buffer_size
It is the buffer memory used to process the sort queries. It has negative impact if increased too much with low volume of data. Optimum size is 2MB .


thread_cache_size
Thread creation/destructions can be expensive, which happen at each connect/disconnect.


query_cache_size
Increasing query_cache_size improves the performance while executing frequent read queries.


thread_concurrency
No of threads executing at a time. It should be tuned based on the CPU the application is deployed.
optimum value=number of CPU's*2


innodb_flush_log_at_trx_commit
This determines the frequency of log flushing to disk.
If the value of innodb_flush_log_at_trx_commit is 0, the log buffer is written out to the log file once per second and the flush to disk operation is performed on the log file, but nothing is done at a transaction commit.
When the value is 1, the log buffer is written out to the log file at each transaction commit and the flush to disk operation is performed on the log file.
When the value is 2, the log buffer is written out to the file at each commit, but the flush to disk operation is not performed on it. However, the flushing on the log file takes place once per second also when the value is 2.


innodb_buffer_pool_size
The size in bytes of the memory buffer InnoDB uses to cache data and indexes of its tables. The default value is 8MB. The larger this value is set, the less disk I/O is needed to access data in tables. On a dedicated database server, it can be set up to 80% of the machine physical memory size.

Tuesday, January 10, 2012

Remove all .svn files

While checkout from SVN repositary the repositary information is present in the directory .svn. While delivering project/source code we have to remove such .svn directories.

Instead of removing each .svn folder manually execute this command to delete all .svn folders recursively.

find ./ -name ".svn" | xargs rm -Rf





Monday, January 9, 2012

Screenshot using C# .NET

  •  Create a window application using MS Visual Studio. 
  • Add a Picture box and a Button.



Source Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging ;
using System.Text;
using System.Windows.Forms;

namespace screen_shot
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int scrwidth = Screen.GetBounds(new Point(0, 0)).Width;
            int scrheight = Screen.GetBounds(new Point(0, 0)).Height;
            Bitmap bmp = new Bitmap(scrwidth, scrheight);
            Graphics gfx = Graphics.FromImage((Image)bmp);
            gfx.CopyFromScreen(0,0,0,0,new Size(scrwidth,scrheight));
            bmp.Save(@"c:\test.jpg",ImageFormat.Jpeg);
        }
    }
}

Read From Excel file - Java

Here I have used JXL API to read Excel file. It can be downloaded from JXL API download

Source Code

import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcel
{
    private String inputFile;


    public void setInputFile(String inputFile)
    {
        this.inputFile = inputFile;
    }
    public void read() throws IOException
    {
        File inputWorkbook = new File(inputFile);
        Workbook w;
        try
        {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet sheet = w.getSheet(0);


            for (int j = 0; j < sheet.getRows(); j++)
            {
                for (int i = 0; i < sheet.getColumns(); i++)
                {
                    Cell cell = sheet.getCell(i, j);
                    System.out.print(cell.getContents()+"\t|\t");
                   
                    // here you can use sql Queries/other code to do insertion of what ever fields you want
                   
                }
                System.out.println();
            }
        } catch (BiffException e)
        {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws IOException
    {
        ReadExcel test = new ReadExcel();
        test.setInputFile("c:\\namelist.xls");
        test.read();
    }
}


SQL Injection

SQL Injection is a web attack to sneak into a website.