Saturday, November 29, 2008

What should India do about Mumbai attacks?

During his preliminary interrogation, Qasad believed to have reportedly told police, "I have done right and I have no regrets." He has also admitted to being a member of the Lashkar.


This what Qasad said who was captured by NSG in Mumbai. Should India listen to this and keep quite? I say NO... Make him regret. Whatever means necessary.

If I was given control, I would make him regret by making his loved one loose their life in front of his eyes and set an example for the future. Make people think about the consequences if at all they decide to do something like what happened in Mumbai.

For those who say I am cold, I would say bring it on.

Friday, November 28, 2008

Who is responsible for Mumbai attack?

US expert on South Asia dubs Mumbai attacks as India's "domestic issue"


Here is what he said:

This is a domestic issue. India's domestic problems and long tensions between local communities were at the root of the rise of terrorism in the country


How come when the attack is in US, it is global terrorism and when the attack is on other countries (ofcourse other than the so called friends of US) it is domestic issue.

This guy failed to check that the attackers were not from India, but everyone of them were from Pakistan. More over, Pakistan reverted its decision to send the head of ISI to India to share the information on this terrorist attack.

Mr/Ms. Christine Fair, get your facts right before making any statements.

Read more about it here.

Wah bhai wah!

Friday, November 14, 2008

Parallel assignment

Today I was working on some problem (off course in Erlang). What I am trying to achieve was to execute a function in parallel and assign result of function to a variable.


Func = fun(Arg) ->
Parent = self(),
Pid = spawn( fun() -> timer:sleep(1000), Parent ! {self(), Arg + 2} end ),
receive {Pid, Val} -> Val
end,
RetVal = Func(),
SomeOtherValue = 10,
% rest of the code
io:format( "~p~n", [RetVal] ),
% more code here


In the above code, calling Func() block the execution until receive loop within Func is done receiving the data from the spawned process. SomeOtherValue is assigned only after the execution of Func.

What I want is not that. I want the execution to continue even if the execution of Func is not finished. I want to block only when I am trying to use RetVal variable.

Currently I am acheiving this by waiting for the response from the spawned process only when I need the data from the process, i.e., same as blocking when the variable is accessed.


Func = fun(Arg) ->
Parent = self(),
Pid = spawn( fun() -> timer:sleep(1000), Parent ! {self(), Arg + 2} end ),
end,
Pid = Func(),
SomeOtherValue = 10,
% rest of the code
RetVal = receive {self(), Val} -> Val end,
io:format( "~p~n", [RetVal] ),
% more code here


Even though it is a trivial code change, I would like if the parallel assignment is allowed and block when I try to use it (if necessary).

Thursday, November 13, 2008

Erlang and Garbage collection

In my new project, we did the whole coding in erlang. Yaws is our front-end. When running a load test, we observed that memory usage goes high and ultimately crash the erlang virtual machine. We did a thorough analysis of our code to find any possible memory leak (not possible as erlang uses garbage collection) and also studied the code for any possible recursion instead of tail recursion. We did not find any. It was pretty puzzling. Then I did find one strange thing. This is all about erlang garbage collection works.



In turns out that Erlang's garbage collection works per process. Each process has its own heap and stack. If a process is a long running one and does lot of things for each call, memory gets accumulated and will get released very slowly. When Erlang virtual machine is busy doing things, garbage collection thread gets lower priority and may never get called. Even if it is called, it may not do the whole memory garbage collection. Thus, memory keeps on growing.


In our code, we were running a gen server. This gen server was doing lot of work in the same process. We were running several hundreds of these gen_servers (monitored by a supervisor). Child specs for the supervisor was created dynamically depending on the initial configuration. In nutshell, it was doin the following




-module(foo_server)
-behaviour(gen_server)

% Not outlining all methods exported here, only the required ones

execute(Params) ->
gen_server:call({run, Params}).

handle_call({run, Params}, _From, State) ->
some_module:execute(Params). % some_module:execute does most of the work here.


Note that here some_module:execute/1 runs in the same process as gen_server. Idea here was that gen_server is monitored by supervisor and we get free process monitoring. But since same process gets called over and over again, memory accumulates. Running load test was increasing memory usage.



Now Erlang process comes to rescue. Because of functional aspect, it got pretty easy to change the code to run some_module:execute/1 in a different process and return the result to the caller via process message passing. I did the following changes


-module(foo_server)

execute(Params) ->
MySelf = self(),
spawn( fun() -> Result = some_module:execute(Params), MySelf ! {MySelf, Result} end),
receive
{MySelf, R} -> R;
after 1000 -> {error, timeout}
end.


Eureka! Running the same load test did not result in increased memory usage. Memory was pretty stable even after running the load test for more than an hour.


Monday, November 10, 2008

URL Shortcut in Firefox







Firefox has this amazingly useful but hidden feature of adding shortcut for a bookmark. This is not just restricted to search based urls. Here is how to do this.

1. Bookmark your favorite URL
2. Go to Bookmarks -> Organize Bookmarks... menu
3. Choose the bookmark from (1)
4. In the bottom panel, click on "More"
5. Enter shortcut word or letter in "Keyword" text box
6. Close "Organize Bookmarks" window
7. In the address bar, type the word or letter from (5)
8. Viola!
9. Enjoy the time saved in taking the mouse pointer to Bookmarks menu and selecting the bookmark :-)

Monday, November 3, 2008

Java is like writing story.

Here is a code to get md5 signature for a text


public static String MD5(String text)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md;
md = MessageDigest.getInstance("MD5");
byte[] md5hash = new byte[32];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
md5hash = md.digest();
return convertToHex(md5hash);
}

private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}




Here is code in perl


use Digest::MD5 qw(md5_hex);
my $digest = md5_hex($text);


This is 20+ lines vs. 3 lines. Java seems to make everything over-object-oriented. Why do I need to getInstance(), update and then call digest() to just get the md5 of the string?

Book Promotion