NFS
> Stands for Network File System
> Client Server Mechanism
> NFS to be hosted by one single server machine (not suitable for scaling)
> NFS works on TCP/IP layer.
Mounting NFS
> User boots up the machine and NFS is mounted as file system
> OS is required to do same file system calls and NFS mount will handle it
Interaction between client and server
> Client makes a request for a file and translated to NFS
> Client uses TCP/IP to communicate
> Reqeusted file is locked and sent back to client
> Client caches the copy of the file (or part of the file)
> The lock is a time base entity. Say for 5 mins lock will stay alive for client.
> Meanwhile accessing the file client can release the lock, can request lock on the file (to restart the time quota), on go down (NFS will time out and releases the lock)
Cache Consistency or Coherence of files
> As mentioned above the files are stored in the local cache (temporal locality)
> Now if client requires to use the same file, a request goes back to NFS checking if the cache is consistent with file on the server (caching the same copy)
> If the copy is updated then new version of file is fetched else client can use the cached copy of file
How to cache consistency is not 100% in NFS
> Client has already cached a file
> Client requests the file and cache is consistent with NFS copy
> While client is using the cached copy of file another client (client0) requests a lock on that file to NFS
> NFS allocates lock to client0.
> Client0 updates the file while client is reading the file
> So there are chances that client may read a stale version
For more information check NFS @ Wiki.
Saturday, December 25, 2010
Friday, December 24, 2010
Tree traversals
Preparing for interviews always go through different tree traversals. Wikipedia has an excellent article over tree traversal.
The tree traversals are:
> InOrder (Sorted Output)
> PreOrder (Root first)
> PostOrder [Depth First Search](children first)
> LevelOrder(Top Level First)
InOrder Code:
PreOrder Code:
PostOrder Code (Depth First Search):
LevelOrder Code :
Except Level Order Traversal remaining of the traversal methods can be rewritten using stacks. Remember you might need to push and pop elements in stack rather than calling the function again. Also sometimes visited flag is required to make sure function is not repeating the same values again.
Have a great fight !!
The tree traversals are:
> InOrder (Sorted Output)
> PreOrder (Root first)
> PostOrder [Depth First Search](children first)
> LevelOrder(Top Level First)
InOrder Code:
void InOrder(Node root){
if(root.left != null) InOrder(root.left);
print (root.value);
if(root.right != null) InOrder(root.right);
}
PreOrder Code:
void PreOrder(Node root){
print(root.value);
if(root.left != null) PreOrder(root.left);
if(root.right != null) PreOrder(root.right);
}
PostOrder Code (Depth First Search):
void PostOrder(Node root){
if(root.left != null) PostOrder(root.left);
if(root.right != null) PosOrder(root.right);
print(root.value);
}
LevelOrder Code :
void LevelOrder(Node root){
// use queue to get level order traversal
// Queue provides two functions. 1. head() 2.add()
Queue q = new Queue();
q.add(root);
while(!q.isEmpty()){
root = q.head();
if(root.left != null) q.add(root.left);
if(root.right != null) q.add(root.right)l
print(roor.value);
}
}
Except Level Order Traversal remaining of the traversal methods can be rewritten using stacks. Remember you might need to push and pop elements in stack rather than calling the function again. Also sometimes visited flag is required to make sure function is not repeating the same values again.
Have a great fight !!
How to switch between remote host and localhost while SSHing?
How to switch between localhost and remotehost while sshing?
localhost$ssh -l username remotehost.com
-put your password here
remotehost$
-now you are connected to remote host
remotehost$~
-press enter, now remote host goes to background and localhost shows up
localhost$
-to open the session to remote host
localhost$jobs
[1]+ Stopped ssh -l username remotehost.com
-to go back use fg command
localhost$fg %1
ssh -l username remotehost.com
remotehost$
-now you are back to remotehost session
localhost$ssh -l username remotehost.com
-put your password here
remotehost$
-now you are connected to remote host
remotehost$~
-press enter, now remote host goes to background and localhost shows up
localhost$
-to open the session to remote host
localhost$jobs
[1]+ Stopped ssh -l username remotehost.com
-to go back use fg command
localhost$fg %1
ssh -l username remotehost.com
remotehost$
-now you are back to remotehost session
Wednesday, December 22, 2010
Installing Apache MySql Server and Apache on Ubuntu
Ubuntu gives beautiful functionality of installing Apapche http web server, MySql Server and PHP. It is sudo apt-get install.
To start before installation just update OSes package index.
$ sudo apt-get update
I was trying to install apache and it works fine. Here is what I did.
$ sudo apt-get install apache2
Any one have ever seen which lang Apache Web Server is written?
It is C. It made me worried because it does not have garbage collection.
Might not work if too much load is generated and resources are getting free is required.
Anyway lets get back to installation. To check Apache has been install properly to to web browser and go to http://localhost/
The text itself is self explanatory.
Now to install php :
$ sudo apt-get install php5 libapache2-mod-php5
As libapache2 has been installed to take affect restart Apache.
$ sudo /etc/init.d/restart
OK. So now php is up and running. How?
$ sudo gedit /var/www/phpinfo.php
Type in
It you see page which is not showing any error, PHP works. NICE !!
Now its time for installing MySql :
$ sudo apt-get install mysql-server mysql-client
This should finish the install. Remember the passwords you are installing here.
Now its time to check whether login and MySql is working fine.
$ mysql -u root -p
Here root is the user created. MySql service will ask for the password after pressing enter. And then you will see
> mysql
> exit
Now some more installation for PHP
$ sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin
Now this installation may or may not ask you to select Yes No type pop up box.
I got some but in this changing world if you don't get them, don't worry just move on.
If you do get such Yes No Pop ups then select by reading the text.
Next thing to do is to restart Apache2.
$ sudo /etc/init.d/restart
This is good enough for right now. If you run into any issue just Google it.
Have great day / night ahead.
To start before installation just update OSes package index.
$ sudo apt-get update
I was trying to install apache and it works fine. Here is what I did.
$ sudo apt-get install apache2
Any one have ever seen which lang Apache Web Server is written?
It is C. It made me worried because it does not have garbage collection.
Might not work if too much load is generated and resources are getting free is required.
Anyway lets get back to installation. To check Apache has been install properly to to web browser and go to http://localhost/
The text itself is self explanatory.
Now to install php :
$ sudo apt-get install php5 libapache2-mod-php5
As libapache2 has been installed to take affect restart Apache.
$ sudo /etc/init.d/restart
OK. So now php is up and running. How?
$ sudo gedit /var/www/phpinfo.php
Type in
It you see page which is not showing any error, PHP works. NICE !!
Now its time for installing MySql :
$ sudo apt-get install mysql-server mysql-client
This should finish the install. Remember the passwords you are installing here.
Now its time to check whether login and MySql is working fine.
$ mysql -u root -p
Here root is the user created. MySql service will ask for the password after pressing enter. And then you will see
> mysql
> exit
Now some more installation for PHP
$ sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin
Now this installation may or may not ask you to select Yes No type pop up box.
I got some but in this changing world if you don't get them, don't worry just move on.
If you do get such Yes No Pop ups then select by reading the text.
Next thing to do is to restart Apache2.
$ sudo /etc/init.d/restart
This is good enough for right now. If you run into any issue just Google it.
Have great day / night ahead.
Sunday, October 31, 2010
SSH to Virtual machine running as a service
Running virtual box as a service has many advantages however accessibility is limited. Ssh is very famous way to access virtual machines running ssh server.In this blog I will show how to setup ssh on Ubuntu as Guest running over Windows as a host and accessing Ubuntu resources through Windows machine using ssh.
On ubuntu:
$sudo apt-get install ssh
#this will prompt you sshserver. Say yes and install ssh / open-ssh server
$ifconfig
#This command will print your current IP configuration
#As virtual machine in virutal box runs as NAT usually, no static ip is attached to it.
#To ssh we will need a static IP attached to virtualbox. Lets do that.
$sudo vim /etc/network/interfaces
#This will open the file containing network configuration. Comment out existing text and write this:
iface eth0 inet static
address 192.168.10.50
netmask 255.255.255.0
gateway 192.168.1.254
#Now restart your virtual machine
$sudo shutdown -r now
Now on windows machine start putty and ssh to 192.168.10.50.
And this is it if you know your login userid and password for ubuntu.
References:
1) A bog which I dont remember anymore
2) VirtualBox
3) Ubuntu
On ubuntu:
$sudo apt-get install ssh
#this will prompt you sshserver. Say yes and install ssh / open-ssh server
$ifconfig
#This command will print your current IP configuration
#As virtual machine in virutal box runs as NAT usually, no static ip is attached to it.
#To ssh we will need a static IP attached to virtualbox. Lets do that.
$sudo vim /etc/network/interfaces
#This will open the file containing network configuration. Comment out existing text and write this:
iface eth0 inet static
address 192.168.10.50
netmask 255.255.255.0
gateway 192.168.1.254
#Now restart your virtual machine
$sudo shutdown -r now
Now on windows machine start putty and ssh to 192.168.10.50.
And this is it if you know your login userid and password for ubuntu.
References:
1) A bog which I dont remember anymore
2) VirtualBox
3) Ubuntu
Running Virtual Machine as Service (virtual box)
Oracle VM Virtual Box is a good utility to try different OSes and keep you host machine clutter free from development installations. Also makes sense to take a regular back up using rsync (file synchronization utility).
This blog post describes shortly how to run virtual box as a service.
To run virtual box as a service we have to start it from command line first and then add the batch / script to scheduled task (on windows) or a cron job ( on unix/linux).
Here are the steps:
1) go to c:\Program Files\Oracle\VirtualBox
2) execute "start /b /wait /low VBoxHeadless -startvm "virtual machine name"
3) press enter
This is it. Now you just need to schedule this execution and now you can run virtual machine in virtual box as a service.
To access virtual machine you can use ssh / remote rdp.
Reference:
http://forums.virtualbox.org/viewtopic.php?f=6&t=1887&start=15
http://www.virtualbox.org
This blog post describes shortly how to run virtual box as a service.
To run virtual box as a service we have to start it from command line first and then add the batch / script to scheduled task (on windows) or a cron job ( on unix/linux).
Here are the steps:
1) go to c:\Program Files\Oracle\VirtualBox
2) execute "start /b /wait /low VBoxHeadless -startvm "virtual machine name"
3) press enter
This is it. Now you just need to schedule this execution and now you can run virtual machine in virtual box as a service.
To access virtual machine you can use ssh / remote rdp.
Reference:
http://forums.virtualbox.org/viewtopic.php?f=6&t=1887&start=15
http://www.virtualbox.org
Sunday, October 24, 2010
Optimizing JVM : tips and tricks
Looking at todays fast paced world performance management and optimization is necessary to stay competent. As working on Java currently, JVM optimization is my current research topic.
JVM works certainly good buy by all means can be improved. I read some suggestions here and will summarize them quickly.
Using JAVA_OPTS -Xmx and -Xms
- If JVM is invoking garbage collection too frequently, meaning that your app heap space is not enough. JVM is using garbage collection to free up some space for your app. Result is drastically reduced performance. -Xmx option can be used to increase the heap space.
- Use -Xms switch to enable heap space equal to maximum allocated memory (all your RAM).
- If garbage collection is using too much time of your CPU, use -Xincgc to garbage collect in phases rather than all in one shot.
JVM works certainly good buy by all means can be improved. I read some suggestions here and will summarize them quickly.
Using JAVA_OPTS -Xmx and -Xms
- If JVM is invoking garbage collection too frequently, meaning that your app heap space is not enough. JVM is using garbage collection to free up some space for your app. Result is drastically reduced performance. -Xmx option can be used to increase the heap space.
- Use -Xms switch to enable heap space equal to maximum allocated memory (all your RAM).
- If garbage collection is using too much time of your CPU, use -Xincgc to garbage collect in phases rather than all in one shot.
Benchmarking suite for java
Java Grande, an initiative to promote Java for grande applications. Grande applications are apps which require lots of memory, bandwidth and processing power.
For more information visit JavaG Benchmarking
For more information visit JavaG Benchmarking
Saturday, October 23, 2010
Optimize query or filter data on App side
Some days ago I ran into problem where complexity of query became prominent. The query used to return 60000 rows after filters and has to compare them against "IN" block of query which again containing around 60000 keys in them. You can see it is an N^2 complexity.
So the first solution I ran into was to pass 60000 keys to the database. As Java being my (so called sophasticated) language that I am using, I have to use jdbc connectors. As jdbc connectors has limitation of passing only 1000 parameters, it is my problem to tackle with. At this point I was still hoping 60000^2 would be pretty fast on DB side. Coming back to parameters issue, I first used Query.setParameterList(Collection), but failed miserable.
Moving forward and identifying problem decided not to use Query.setParameterList(Collection). However still hoping 60000^2 would work once I can pass the parameters. Now to hack the jdbc a bit, I requested the query string first with a patters in it. The patter would be replaced by 60000 keys which is generated by java program. After replacing the string,query sent to database to do the comparison and I waited ........ Did not come back. ALAS.
An interesting discussion with my colleague suggested that processing data on java would be a better idea. I fastened my seat belts for this experiment. I did the same query but this time just returned all the rows to app side. App was comparing the results, a bit slow but much much faster than SQL query.
Sharing in the benefits of those who believe in getting things done .....
How to use setParameterList
JAVA Link
So the first solution I ran into was to pass 60000 keys to the database. As Java being my (so called sophasticated) language that I am using, I have to use jdbc connectors. As jdbc connectors has limitation of passing only 1000 parameters, it is my problem to tackle with. At this point I was still hoping 60000^2 would be pretty fast on DB side. Coming back to parameters issue, I first used Query.setParameterList(Collection), but failed miserable.
Moving forward and identifying problem decided not to use Query.setParameterList(Collection). However still hoping 60000^2 would work once I can pass the parameters. Now to hack the jdbc a bit, I requested the query string first with a patters in it. The patter would be replaced by 60000 keys which is generated by java program. After replacing the string,query sent to database to do the comparison and I waited ........ Did not come back. ALAS.
An interesting discussion with my colleague suggested that processing data on java would be a better idea. I fastened my seat belts for this experiment. I did the same query but this time just returned all the rows to app side. App was comparing the results, a bit slow but much much faster than SQL query.
Sharing in the benefits of those who believe in getting things done .....
How to use setParameterList
JAVA Link
java.text.SimpleDateFormat
DateFormat dateFormat = new SimpleDateFormat("");
For 20th October, 2010 13:40:19.333, that is 20th October, 1:40 PM and 19 seconds with 333 seconds, the Java mapping would be.
yyyy = 2010
yy = 10 (instead of 2010 only 10 would be printed. Don't forget y2k problem)
MM - 10 (10th month of the georgean calendar)
dd - 20 (20th day of October month)
HH/hh - 01 (should print hour part of date format)
mm - 40 (minute part of the date part)
ss - 10 (seconds of the date part)
SSS - 333 (milliseconds part of the date format)
Usage:
Date d = new Date();
DateFormat datefromat = new SimpleDateFormat("yyyyMMdd HH:mm:ss SSS");
String dateAsString = dateformat.format(d);
System.out.println("The date in text format is:" + dateAsString);
Imports:
java.util.Date
java.text.SimpleDateFormat
For 20th October, 2010 13:40:19.333, that is 20th October, 1:40 PM and 19 seconds with 333 seconds, the Java mapping would be.
yyyy = 2010
yy = 10 (instead of 2010 only 10 would be printed. Don't forget y2k problem)
MM - 10 (10th month of the georgean calendar)
dd - 20 (20th day of October month)
HH/hh - 01 (should print hour part of date format)
mm - 40 (minute part of the date part)
ss - 10 (seconds of the date part)
SSS - 333 (milliseconds part of the date format)
Usage:
Date d = new Date();
DateFormat datefromat = new SimpleDateFormat("yyyyMMdd HH:mm:ss SSS");
String dateAsString = dateformat.format(d);
System.out.println("The date in text format is:" + dateAsString);
Imports:
java.util.Date
java.text.SimpleDateFormat
Subscribe to:
Posts (Atom)