Sunday, August 30, 2009

Load Balancing - DNS - RoundRobin

Load Balancing Linux server :



What is a Cluster

cluster is a group of servers running a Web application simultaneously, appearing to the world as if it were a single server. To balance server load, the system distributes requests to different nodes within the server cluster, with the goal of optimizing system performance. This results in higher availability and scalability -- necessities in an enterprise, Web-based application.

High availability can be defined as redundancy. If one server cannot handle a request, can other servers in the cluster handle it? In a highly available system, if a single Web server fails, then another server takes over, as transparently as possible, to process the request.


Scalability is an application's ability to support a growing number of users. If it takes an application 10 milliseconds(ms) to respond to one request, how long does it take to respond to 10,000 concurrent requests? Infinite scalability would allow it to respond to those in 10 ms; in the real world, it's somwhere between 10 ms and a logjam. Scalability is a measure of a range of factors, including the number of simultaneous users a cluster can support and the time it takes to process a request.


Two Methods of load balancing :

1) DNS round robin
2) Hardware load balancers.

The DNS server generally contains a single IP address mapped to a particular site name. In our fictional example, our site www.loadbalancedsite.com maps to the IP address 203.24.23.3

To balance server loads using DNS, the DNS server maintains several different IP addresses for a site name. The multiple IP addresses represent the machines in the cluster, all of which map to the same single logical site name. Using our example, www.loadbalancedsite.com could be hosted on three machines in a cluster with the following IP addresses:

203.34.23.3
203.34.23.4
203.34.23.5

In this case, the DNS server contains the following mappings:

www.loadbalancedsite.com 203.34.23.3
www.loadbalancedsite.com 203.34.23.4
www.loadbalancedsite.com 203.34.23.5





Advantages :
Inexpensive and easy to set up. The system administrator only needs to make a few changes in the DNS server to support round robin, and many of the newer DNS servers already include support. It doesn't require any code change to the Web application; in fact, Web applications aren't aware of the load-balancing scheme in front of it.

Simplicity. It does not require any networking experts to set up or debug the system in case a problem arises.

Disadvantages :


1) No support for server affinity. Server affinity is a load-balancing system's ability to manage a user's requests, either to a specific server or any server, depending on whether session information is maintained on the server or at an underlying, database level.

2) No support for high availability

Tuesday, August 25, 2009

Serialization in java

Question: What are the uses of Serialization?

Answer: In some types of applications you have to write the code to serialize objects, but in many cases serialization is performed behind the scenes by various server-side containers.

These are some of the typical uses of serialization:

To persist data for future use.
To send data to a remote computer using such client/server Java technologies as RMI or socket programming.
To "flatten" an object into array of bytes in memory.
To exchange data between applets and servlets.
To store user session in Web applications .
To activate/passivate enterprise java beans.
To send objects between the servers in a cluster

Java Threads

Good overview:

http://www.cs.usfca.edu/~parrt/course/601/lectures/threads.html

Sunday, August 16, 2009

Volatile Variables in Java

What does volatile do?

This is probably best explained by comparing the effects that volatile and synchronized have on a method. volatile is a field modifier, while synchronized modifies code blocks and methods. So we can specify three variations of a simple accessor using those two keywords:

int i1; int geti1() {return i1;}
volatile int i2; int geti2() {return i2;}
int i3; synchronized int geti3() {return i3;}
geti1() accesses the value currently stored in i1 in the current thread. Threads can have local copies of variables, and the data does not have to be the same as the data held in other threads. In particular, another thread may have updated i1 in it's thread, but the value in the current thread could be different from that updated value. In fact Java has the idea of a "main" memory, and this is the memory that holds the current "correct" value for variables. Threads can have their own copy of data for variables, and the thread copy can be different from the "main" memory. So in fact, it is possible for the "main" memory to have a value of 1 for i1, for thread1 to have a value of 2 for i1 and for thread2 to have a value of 3 for i1 if thread1 and thread2 have both updated i1 but those updated value has not yet been propagated to "main" memory or other threads.

On the other hand, geti2() effectively accesses the value of i2 from "main" memory. A volatile variable is not allowed to have a local copy of a variable that is different from the value currently held in "main" memory. Effectively, a variable declared volatile must have it's data synchronized across all threads, so that whenever you access or update the variable in any thread, all other threads immediately see the same value. Of course, it is likely that volatile variables have a higher access and update overhead than "plain" variables, since the reason threads can have their own copy of data is for better efficiency.

Well if volatile already synchronizes data across threads, what is synchronized for? Well there are two differences. Firstly synchronized obtains and releases locks on monitors which can force only one thread at a time to execute a code block, if both threads use the same monitor (effectively the same object lock). That's the fairly well known aspect to synchronized. But synchronized also synchronizes memory. In fact synchronized synchronizes the whole of thread memory with "main" memory. So executing geti3() does the following:


1.The thread acquires the lock on the monitor for object this (assuming the monitor is unlocked, otherwise the thread waits until the monitor is unlocked).
2.The thread memory flushes all its variables, i.e. it has all of its variables effectively read from "main" memory (JVMs can use dirty sets to optimize this so that only "dirty" variables are flushed, but conceptually this is the same. See section 17.9 of the Java language specification).
3.The code block is executed (in this case setting the return value to the current value of i3, which may have just been reset from "main" memory).
4.(Any changes to variables would normally now be written out to "main" memory, but for geti3() we have no changes.)
5.The thread releases the lock on the monitor for object this.
So where volatile only synchronizes the value of one variable between thread memory and "main" memory, synchronized synchronizes the value of all variables between thread memory and "main" memory, and locks and releases a monitor to boot. Clearly synchronized is likely to have more overhead than volatile.

Source : http://www.javaperformancetuning.com/news/qotm030.shtml

Monday, August 10, 2009

Designing Thread Safe Classes in Java

http://www.artima.com/designtechniques/threadsafety.html

Sunday, December 28, 2008

Basic Unix Commands

What is Shell..?

The shell is perhaps the most important program on the Unix system, from the end-user's standpoint.
The shell is your interface with the Unix system, the middleman between you and the kernel.

CONCEPT: The shell is a type of program called an interpreter. An interpreter operates in a simple loop: It accepts a command, interprets the command, executes the command, and then waits for another command. The shell displays a "prompt," to notify you that it is ready to accept your
command.

The shell is a program that the Unix kernel runs for you. A program is referred to as a process while the kernel is running it. The kernel can run the same shell program (or any other program) simultaneously for many users on a Unix system, and each running copy of the program is a separate process.

The basic form of a Unix command is: commandname [-options] [arguments]

ls -l /tmp

-l = Long list of options


ls -lR /lib/l*

-R causes ls to operate recursively, moving down directory trees

ls -m /etc/i*g

-m causes output to be streamed into a single line.

-l Shows you huge amounts of information (permissions, owners, size, and when last modified.)

-r Reverses the order of how the files are displayed.

-t Shows you the files in modification time.

Special Characters in Unix: `,~,!,$,%,^, & *, |, \,/, {,},[,],",',;

your file names cannot start with any of the spl characters above.


Getting Help in Unix:

man

whatis gives you a brief description of the command (wont specify the options)

File Permissions:

ls -l /etc/passwd
-rw-r--r-- 1 root sys 41002 Apr 17 12:05 /etc/passwd

first - (is a - if its a normal file, d, if its a directory and s if its a special file ex: device file)

next 3: rw- : permissons of the owner

next 3: r-- : Permissions of the group

next 3: r-- : permissions of others


To set file permissions, you may use to the "rwx" notation to specify the type of permissions, and the
"ugo" notation to specify those the permissions apply to.

To define the kind of change you want to make to the permissions, use the plus sign (+) to add a permission, the minus sign (-) to remove a permission, and the equal sign (=) to set a permission directly.

chmod g=rw- ~/.shrc (here you specify / set all permissions)
to change the file permissions on the file .shrc, in your home directory. Specifically, you are
specifying group read access and write access, with no execute access.

u : owner
g : group
o : others
a : all

chmod a-x socktest.pl (revoke execute permissions for all on a file)
$ ls -l socktest.pl
-rw-r--r-- 1 nick users 1874 Jan 19 10:23 socktest.pl

chmod 755
You might have encountered things like chmod 755 somefile and of course you will be wondering what this is. The thing is, that you can change the entire permission pattern of a file in one go using one number like the one in this example. Every mode has a corresponding code number, and as we shall see there is a very simple way to figure out what number corresponds to any mode.

Triplet for u: rwx => 4 + 2 + 1 = 7
Triplet for g: r-x => 4 + 0 + 1 = 5
Tripler for o: r-x => 4 + 0 + 1 = 5
Which makes : 755

So, 755 is a terse way to say 'I don't mind if other people read or run this file, but only I should be able to modify it' and 777 means 'everyone has full access to this file'

pwd : Print Working Directory

file is a standard Unix program for determining the type of data contained in a computer file.

The Unix file command allows you to determine whether an unknown file is in text format, suitable for direct viewing

file /bin/sh


The cat command
The cat command concatenates files and sends them to the screen. You can specify one or more files as arguments. Cat makes no attempt to format the text in any way, and long output may scroll off the screen before you can read it.

Output scrolls off of the screen

The tilde character (~) is Unix shorthand for your home directory


The more command
The more command displays a text file, one screenful at a time. You can scroll forward a line at a time by pressing the return key, or a screenful at a time by pressing the spacebar. You can quit at any time by pressing the q key.

head -15 /etc/rc
to see the first fifteen lines of the /etc/rc file.


tail /etc/rc
to see the last ten lines of the file /etc/rc. Because we did not specify the number of lines as an
option, the tail command defaulted to ten lines.

cp ~/.profile ~/pcopy
makes a copy of your .profile file, and stores it in a file called "pcopy" in your home directory.

mv ~/pcopy ~/qcopy
takes the pcopy file you created in the cp exercise, and renames it "qcopy".

The rm command is used for removing files and directories. The syntax of the rm command is rm
filename. You may include many filenames on the command line.

rm ~/.shrccopy

The Unix mkdir command is used to make directories. The basic syntax is mkdir directory-name.
If you do not specify the place where you want the directory created (by giving a path as part of the
directory name), the shell assumes that you want the new directory placed within the current working
directory.

mkdir ~/foo

The Unix rmdir command removes a directory from the filesystem tree. The rmdir command does not work unless the directory to be removed is completely empty.


The rm command, used with the -r option can also be used to remove directories. The rm -r command will first remove the contents of the directory, and then remove the directory itself.

Redirecting Input and Output


< = Input redirection
> Output redirection to file
>> output redirection to file (append)
2> error redirection


ex: more < /etc/passwd

Use standard input redirection to send the contents of the file /etc/passwd to the morecommand

Using the "less-than" sign with a file name like this:
< file1 in a shell command instructs the shell to read input from a file called "file1" instead of from the keyboard.

To see the first ten lines of the /etc/passwd file, the command:
head /etc/passwd
will work just the same as the command:
head < /etc/passwd


ls /tmp > ~/ls.out

ls /etc >> myls


sort < /etc/passwd > foo 2> err


Pipe:


CONCEPT: Unix allows you to connect processes, by letting the standard output of one process feed into the standard input of another process. That mechanism is called a pipe. Connecting simple processes in a pipeline allows you to perform complex tasks without writing complex programs.

ls -l /etc | more

How could you use head and tail in a pipeline to display lines 25 through 75 of a file?
ANSWER: The command
cat file | head -75 | tail -50


The grep utility recognizes a variety of patterns, and the pattern specification syntax was taken from
the vi editor. Here are some of the characters you can use to build grep expressions:
> The caret (^) matches the beginning of a line.
> The dollar sign ($) matches the end of a line.
> The period (.) matches any single character.
> The asterisk (*) matches zero or more occurrences of the previous character.
> The expression [a-b] matches any characters that are lexically between a and b.


grep 'jon' /etc/passwd
grep '^jon' /etc/passwd

ls -l /tmp | grep 'root'

PS:
Unix provides a utility called ps (process status) for viewing the status of all the unfinished jobs that
have been submitted to the kernel. The ps command has a number of options to control which
processes are displayed, and how the output is formatted.


ps -ef
to see a complete listing of all the processes currently scheduled. The -e option causes ps to include
all processes (including ones that do not belong to you), and the -f option causes ps to give a long
listing. The long listing includes the process owner, the process ID, the ID of the parent process,
processor utilization, the time of submission, the process's terminal, the total time for the process,
and the command that started the process.


To kill a process, you must first find its process ID number using the ps command. Some processes
refuse to die easily, and you can use the "-9" option to force termination of the job.
EXAMPLE: To force termination of a job whose process ID is 111, enter the command
kill -9 111

Executing a job in background


sort < foo > bar &


The commands related to job control will apply to
the current job, unless directed to do otherwise. You may refer to jobs by job ID by using the percent
sign. Thus, job 1 is referred to as %1, job 2 is %2, and so forth.

To place a foreground job to background, use bg command

Monday, October 13, 2008

Reflection in Java

Creating a object using reflection in Java:

Class parserClass=Class.forName(parser);
Object newParser=parserClass.newInstance();
Class classParams[]=new Class[1]; //To set type of parameter
Object arglist[]=new Object[1]; //To set the actual value

classParams[0] = String.class;
arglist[0] = tradeType;
String methodName="setLoadType";

Method meth = parserClass.getMethod(methodName, classParams);

meth.invoke(newParser, arglist); //Set setTradeType=tradeType

return (DefaultFileParser)newParser;


StringArrayConvertor in Java

public Object convert(Class type,
Object value)
Deprecated.
Convert the specified input object into an output object of the specified type.
If the value is already of type String[] then it is simply returned unaltered.
If the value is of type int[], then a String[] is returned where each element in the string array is the result of calling Integer.toString on the corresponding element of the int array. This was added as a result of bugzilla request #18297 though there is not complete agreement that this feature should have been added.
In all other cases, this method calls toString on the input object, then assumes the result is a comma-separated list of values. The values are split apart into the individual items and returned as the elements of an array. See class AbstractArrayConverter for the exact input formats supported.
Converting Strng Array to String in Java
public static String arrayToString(String[] a, String separator) {
StringBuffer result = new StringBuffer();
if (a.length lt 0) {
result.append(a[0]);
for (int i=1; i lt a.length; i++) {
result.append(separator);
result.append(a[i]);
}
}
return result.toString();
}