Saturday, 25 August 2012

Thread Locals in Java

Thread Locals

Thread local  is a scope just as we have static scoped variables which belong to a class or instance scoped variables which belong to an Object. Thread local variables belong to a thread. Each thread would have its own thread local variable. So, threads can't modify each other's thread local variables. Thread variables are a sort of global variables which are restricted to a thread.

When do we use Thread Local variables ?

By default, data is shared between threads. You can refer to my previous post to get an idea about what is shared among threads. You can use Thread Local variables when you want each thread to have its own copy of something. One very important use cases of thread locals is when we have an object that is not thread safe, but we want to avoid synchronizing access to that object. It would be more clear from an example.

Suppose I have a requirement to use use Java Calendar object in my code. Since Java Calendar is not thread safe, we can either have Calendar object as an instance variable or have it as a class variable and provide synchronization to access it. The first method can't be used in most of the production codes because Calendar object creation is an expensive operation. And still if we have 2 threads of the same process accessing the variable, we need synchronization. The second method looks good since we would have only one object of Calendar class, but we would have to take care of synchronization. 

If we don't want to bother about synchronization, we can go for thread local variables in which case, each thread would be given its own local copy of the thread local variable. Another alternative to thread locals or synchronization is to make the variable a local variable. Local variables are always thread safe. But in our case, since Calendar object creation is an expensive operation, it is not recommended to use local variable for Calendar. Since each time the method is called, a Calendar object would be created which is an expensive operation, it would slow down the.

Another very important use case of Thread Local variables is when we want to associate state with a thread. Many frameworks use ThreadLocals to maintain some context related to the current thread. Web applications might store information about the current request and current session in thread local variables so that the application has easy access to them without passing them as parameters every time. Let me explain this with a scenario.

Lets say, we have a Servlet which calls  some methods. You have a requirement to generate a unique transaction ID for each request you receive and pass this transaction ID to the business methods for processing. One way is to generate a unique transaction ID each time the servlet receive a request and pass this trasaction ID to the methods which require it. But this doesn't look good, since passing of transaction ID to all methods which require it is redundant and unnecessary. Instead, we can use thread local variable to store the transaction ID. Every method which requires transaction ID can access it through the thread local variable. The servlet might be receiving many requests, but each request is processed in a separate thread. So, each transaction ID would be local to a thread and would be accessible all through the thread's execution which is what I mean when I say that Thread Local variables are global.

Usage of Thread Local variables in Java

Java provide a class named ThreadLocal by which you can set and get Thread Local variables.
Typically Thread Local variables are static fields in classes. The code below shows you how to create a Thread Local variable.

Problems with Thread Locals

Thread Locals also comes up with many problems and you have to be careful while using thread locals. Thread Locals can lead to classloading leaks. Thread Locals are very dangerous when it comes to long running applications and garbage collection. Let me explain this point a little bit.

If you use thread locals to store some object instance, there is a high risk that the object stored in thread local is never collected by garbage collector when your application runs inside WebLogic Server. This is because WebLogic server maintains a pool of working threads even when the class that created it is garbage collected. So, if you do not clean up when you are done, any references that it holds as part of the webapp deployment will remain in the heap and would never be garbage collected. This problem can be solved through the proper use of Weak References with Thread Locals. 

Sunday, 19 August 2012

Processes andThreads

In this post I would be discussing about processes and threads.

Process

A process is an instance of a program that is being executed. A process consumes the resources of an operating system. Since there are many processes running at a time, how does the OS manages its resources ? To manage processes, an operating system has a process table. A process table is a data structure which includes the following information:
  • Process ID
  • Process Owner
  • Process priority
  • Pointer to the executable code of the process
  • Parent Process
  • Environment variables
  • Process state
A process can have many threads of execution. By default, any running program has a single thread of execution. A process has a unique address space which is generally not shared with any other process, except during inter process communication, the operating system can relax this condition.

Threads

 A thread is a smallest unit of execution that can be scheduled by an OS. A thread is called the light weight process because thread creation can be 10-100 times faster than a process creation. This is because threads share address space unlike processes which do not share address space. Here I am talking about the threads of the same process. Threads of different processes, of course do not share address space. The main reason for having threads is that in many applications, many activities are going on at the same time. Some of these activities may block from time to time. By decomposing such an application into multiple threads, we increase performance. Threads yield no performance gain when all of them are CPU bound, but when there is substantial amount of I/O as well as computing. Having threads, allows the activities to overlap, this speeding up the application. Threads also allow parallel execution on a multiprocessor system. In this case, the programmer needs to be careful to avoid race condition.

A thread has the following information:
  • Thread ID
  • Program Counter
  • Register Set
  • Stack
So what does the thread share with other threads of the same process?
  • Code section
  • Data section
  • OS resources
Lets talk about the advantages of threads.

Thread Advantages:

  1. Thread creation and destruction is faster than process creation and destruction. 
  2. A thread has lower context switching overhead than a process. This is because a thread has a lesser context than a process because threads share address space. Remember here I am talking about thread of the same process.
  3. Information sharing between threads is easier and has less overhead because threads share address space. So, data produced by one thread is immediately available to all other threads of the same process.

Thread Disadvantages:

Since global variables are shared between threads, inadvertent modification of shared variables can be disastrous. It calls for concurrency control measure which have their own complications.

Types of Thread Implementations:

There are 3 types of thread implementations:
  1. User Level Threads
  2. Kernel Level Threads
  3. Hybrid implementation

User Level Threads:

The type of thread implementation puts the thread package entirely in user space. The kernel is not aware of threads. The kernel just knows that it is managing single threaded processes. Like an OS maintains a process table, a process maintains a thread table which does the same job as process table does for operating system. Each process has its own private thread table. In this implementation, when a thread wants to go to the blocked state, it notifies the run time system. The run time system saves the thread state in the thread table and looks for a ready thread in the thread table to run. We see that in case of user level thread implementation, we don't trap to the kernel in case of thread switching. This is at least an order of magnitude faster than trapping to the kernel in case of kernel level thread implementation.

The main problem with this type of thread implementation is that if by chance any user-level thread is blocked in the kernel, all threads of that process are blocked. Another problem with use-level threads is that we don't take advantage of multiprocessing since the kernel is not aware of any threads.

Kernel Level Threads:

In this type of thread implementation, any thread in a process would be mapped to a kernel level thread. Switching between threads in this case requires kernel mode switch which is expensive. When a thread blocks, the kernel, at its option, can run either another thread from the same process or a thread from a different process. With user level threads, the run time system keeps running threads from one process until the kernel takes the CPU away from it.

Saturday, 4 August 2012

Website Parsing

Today I would be writing about a website parser which I wrote. In this post, I would show you how to parse www.cricbuzz.com website. But the logic is nearly the same for other websites also. You can play around by changing the logic according to your needs. I have used Python in my code, so you need to know Python to follow this post.

So lets begin parsing cricbuzz site. First go to that website. Go to the page that you want to parse. Suppose I want to parse the ongoing England Vs South Africa test match. Go to the Full scorecard page of cricbuzz as shown below:


.
 If you are using Google Chrome browser, press Shift + Ctrl + J to go into the developer mode. You would see a new split window having some tabs as shown below:




Then click on the Network Tab:




Then click on scorecard.json which is highlighted in the above picture.



We see that this site uses JSON which is a light weight data interchange format to send the data. JSON is easy for machines to parse and generate. It is based on Javascript Programming Language. Now you can use your logic to parse the site. I will be using Python's Json package to parse the Json content. Lets start with the code. First import json package. We would need the URL of the JSON page to begin parsing, so get the url by right clicking on scorecard.json.  You can check that URL by pasting in your web browser. You should see a page like this :




We need to get the data from this URL to begin parsing. We can use urllib2 package for this task. The following statement would get the whole data in result string, where the URL is the copied URL:

result = json.load(urllib2.urlopen(URL))


The logic I have used is that if the score changes after 20 mins, it would send an email to the person. To handle the email part, we have to use smtp package.

So, here is the complete code:




The code I have used has very little practicality, but the idea was to make the concept clear. If the idea was clear, you can play around with the logic.  :)

Sunday, 29 July 2012

Hedge Fund Vs Mutual Fund

I myself had this doubt for quite a long time: What is the difference between a Hedge Fund and a Mutual Fund. This month I started working at D.E. Shaw & Co and during the induction program, I was introduced to the financial concepts. This was different from what I has exposed myself since the last 4 years i.e. algorithms, operating systems, databases, compilers, processors, computation and blablabla.This was a slight shift from my regular track which is computer science, but I really enjoyed a lot. I explored a little more and got a clear understanding of the difference between a Hedge Fund and a Mutual Fund. In this post I would share my findings.

Hedge Fund

A hedge fund is an aggressively managed portfolio that uses leveraging to generate high returns. The word 'hedge' means to minimize or to reduce financial risk. But we see that hedge funds are mostly risky. They are so risky that only big investors invest in hedge funds. They are open to only some specific type of investors specified by regulators. These investors are big institutions such as pension funds and high net worth individuals. The goal of a hedge fund is to maximize returns and do achieve this goal, hedge fund uses leveraging. Now there is a very basic theory in finance which goes as: "Higher the returns, higher the risk involved". Since hedge funds give higher returns, they involve more risk. Then why are they called hedge funds since 'hedging' means to minimize or to reduce risk. Like mutual funds, hedge funds also pool money from investors and then manage them. But hedge funds uses leveraging to maximize gains and that make them risky. Let me explain the concept of hedging by the following example:

Suppose a hedge fund collects 100 $ from investors (I have taken this amount just for illustration. The actual amount that hedge fund invests runs into billions of US dollars). It takes 100 $ from a bank (This is what is leveraging). Now hedge fund has a pool of 200 $ which it invests. Now the bank that lends you 100 $ has a condition: No matter what you gain or loose, I should get 120 $ after say 6 months. Now lets examine 2 cases:
  1. Suppose after 6 months, hedge fund makes a profit of 40%. The value of the portfolio is 280 $. Hedge fund gives 120 $ to the bank and distributes 60 $ as profit among investors. Of course, it charges some fees also, but I have not included it in my computations. Hence the investors make a profit of 60%.  Now we see the power of leveraging. Though hedge made a profit of only 40%, it gave 60% profit to investors. This is possible due to leveraging.
  2. Suppose after 6 months hedge made a loss of 10%. The value of the portfolio is 180 $.  Hedge fund gives 120 $ to the bank and the value left in the portfolio is 60 $. Of course, it charges some fees also, but I have not included it in my computations. Hence the investors made a loss of 40%.  Now we see the risk of leveraging. Though hedge made a loss of only 10%, it resulted in 40% loss to investors. This is reason why hedge funds are risky.

Mutual Funds

A mutual fund is a type of professionally managed collective investment scheme that pools money from several investors to purchase securities. But unlike hedge funds, mutual funds are highly regulated. Mutual fund is open to common man unlike hedge funds. Due to this reason, mutual funds are highly regulated. They can't invest in any security, but only those which are approved by the regulators. Hence mutual funds are safer than hedge funds. Then again that basic finance theory comes into picture: "Lower the risk, lower the returns". Hence mutual funds don't generate as much returns as hedge funds.

Similarity between a mutual fund and a hedge fund

  1. The most important similarity between a hedge fund and a mutual fund is that they both pool money from investors. 
  2. One more similarity is that in a hedge fund also investors can withdraw their money as in a mutual fund

Difference between a Mutual fund and a Hedge Fund

In a nut shell, following are the differences between a Hedge Fund and a Mutual Fund:
  1. Hedge funds focus on absolute returns while mutual funds focus on relative returns.
  2. Hedge funds can invest in any asset class- stocks, bonds, sub prime mortgages, commodities, real estate. While mutual funds can only invest in a set of asset class. Mutual funds have to follow compliance framework set up by the regulator. Hence risky asset classes are debared from  investment.
  3. Hedge funds use leverage. Though mutual funds can also borrow to some extent but they are highly regulated.
  4. Hedge Fund can run concentrated portfolios. In case of Mutual Funds we have to protect investors' money and hence they always use diverse portfolios.
  5. Hedge Funds are meant for richer people to become more rich. Whereas even low worth individuals can invest in a mutual fund.
  6. Hedge Funds are unregulated whereas mutual funds are highly regulated.

Sunday, 22 July 2012

Money Markets

Money

Money is the current medium of exchange. I have seen that often people get confused with money. The money that is in our bank accounts is not real money. Money that is on account of the Central Bank of a country is the Real Money.  All other forms for example, the account balances with the commercial banks, even cash are promises to pay money, but not real money. Like individuals, banks and larger institutions also transact money amongst each other. In these transactions cash is not involved, but real money kept in accounts with the Central Bank of a country is involved.

Money Markets

Money market provides short term finance(for a period less than 1 year). The parties involved in Money Markets are Central Banks, Commercial Banks, FIs, Mutual Funds and Primary Dealers. One of the main differences between money markets and stock markets is that most money market securities trade in very high volume thus limiting accessing individual investors. The easiest way for an individual to access money market is through money market mutual funds. However, some money market instruments like Treasury bills may be purchased directly. Below we will have a look at major money market instruments:

Treasury Bills(T-Bills)

T-Bills are the most liquid money market securities. T-Bills are a way for the US government to raise money from the public. I am refering to the T-Bills issued by the US government, but many governments issue T-Bills in a similar fashion. Treasury Bills are issued through a competitive bidding process. The biggest reason that the T-Bills are so popular is that they one of the few money market instruments that are affordable by individual investors. Another important reason for their popularity is that they are considered to be the safest investment in the world because they are backed by US government. But this safety comes at a cost. They have very low returns. And this is the basic rule in finance: The higher the risk, the higher the return.

Certificate of Deposit

A certificate of deposit is a promissory note issued by a bank. It is a time deposit that restricts holders from withdrawing funds on demand. CDs are similar to saving accounts in that they are insured and hence virtually risk-free. They are different from saving accounts in that CDs have a specific and fixed term(often 1 month, 3 months, 6 months, 1 year). CDs generally give higher rate of return than Bank term deposit.

Commercial Paper

An unsecured, short-term debt instrument issued by a corporation, typically for the financing of accounts receivable, inventories and meeting short-term liabilities. Maturities on commercial paper rarely range any longer than 270 days. Commercial papers are not backed by collateral. Since these are not backed by collateral, only firms with high credit ratings from a recognized rating agency would be able to sell its commercials papers at a reasonable price.

Banker's Acceptance

BA is a promised future payment which is guaranteed by a bank and drawn on a deposit at a bank. A BA specifies the amount of money, date and the person to which the payment is due. Now the holder of the draft can sell it for cash to a buyer who is willing to wait until the matutity date of the funds in the deposit. BAs make the transaction between 2 parties who do not know each other to be more safe because they allow parties to substitute the banks's creditworthiness for that who owes the payment.

Eurodollars

Eurodollars are US dollar denominated deposits at banks outside United States and are thus not under the jurisdiction of Federal Reserve. These are called Eurodollars because most of the initially most of the US dollar reserves outside the United States were in Europe. Eurodollar market is relatively free of regulations and hence banks can operate at lower margins than their counterparts in United States.

Repo

A repurchase(repo) agreement can be seen as a short term swap between cash and securities. If a security holder wants to maintain his long-term position but needs cash for a short term period, he or she can enter into a repo contract whereby the securities are sold together with a binding agreement to repurchase them at a future date.. The effect is to provide the security holder with a short-term loan based on the collateral of the government securities he or she owns.

Friday, 1 June 2012

GC overhead limit exceeded error




Recently I was struggling with a very unusual error in one of the batches in production environment. The error read like:
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded

Here is the snapshot of the error when I ran it through command prompt:

Explored on this error and in this post I would like to highlight my findings:

This message means that for some reason the garbage collector is taking an excessive amount of time (by default 98% of all CPU time of the process) and recovers very little memory in each run (by default 2% of the heap). This effectively means that your program stops doing any progress and is busy running only the garbage collection at all time. To prevent your application from soaking up CPU time without getting anything done, the JVM throws this Error so that you have a chance of diagnosing the problem.
The rare cases where I've seen this happen is where some code was creating tons of temporary objects and tons of weakly-referenced objects in an already very memory-constrained environment. This feature is designed to prevent applications from running for an extended period of time while making little or no progress because the heap is too small. You can turn this off with the command line option -XX:-UseGCOverheadLimit
In my case the data was huge. We had deployed a batch in production which was implemented using stateful framework. But it had to be changed to the stateless code which took around a month. During that time that batch didn’t process any records. So the records had piled up which needs to be processed. When we deployed the stateless code, it gave this error. When I was running this batch process, I had allocated maximum of 1 GB memory to it. Then I removed this limit. Even then it gave this error. So, I had to turn off this feature to get rid of it by the command line option I had mentioned above.

Now the obvious question that comes to the mind is that what happens to the Java process in case of OutOfMemoryError.
And OutOfMemoryError is handled like any other exception:
·         If it is caught, then nothing more happens.
·         If it is not caught, then either the threads or the threads groups uncaught exception handler handles it. This pretty much always leads to the thread being stopped.
However there are two factors that are not really there in other exceptions:
·         OutOfMemoryError is an Error and not an Exception. This means that it's very unlikely to be caught anywhere: You should not try to catch an Error generally (with very few exceptions) and it's not usually done, so the chances of it being handled are rather low.
·         When an OutOfMemoryError happens and no object become eligible for GC because of that, then you'll still have little memory left and chances are that you'll run into the exact same problem again later on.
And if the thread this happens to is the only non-daemon thread (often, but not necessarily, that's the main thread, that executes the main method), then that thread getting killed results in the whole JVM shutting down (which is often perceived as "a crash").
So it will probably kill the thread, and if the memory-issue is not solved, then this can happen to more and more threads.

OutOfMemoryError should be considered unrecoverable and the behavior of the JVM after such an error has been raised is undefined, so there is no point in expending effort to handle it. Any operations done after this exception is thrown by the JVM will have undefined behavior. They may execute, but more likely they will just cause another error to be thrown.

Saturday, 26 May 2012

Mounting partitions in Linux through command line

In this post, I would add a partition in Linux through command line. Your first task should be to know which partitions are already mounted on your system and where they are mounted. So, run the following command which gives you this information:
$ sudo mount


If the partition which you wish to mount is shown, you can navigate to its mount directory. Lets say the mount directory of the partition you wish to mount is /mnt/partition1, then run the following command:
$ cd /mnt/partition1
And you be able to access its file system.

But if the partition you wish to mount is not shown by the command "sudo mount", you will have to mount it first. So, you would like to know the device identifier for the partition. The following command would help to do that:
$ sudo fdisk -l
it is a lowercase L. The identifier will look something like
/dev/sda1
Now you have to create a mount point(you can give any path for it). mount point is a directory (typically an empty one) in the currently accessible filesystem on which an additional filesystem is mounted (i.e., logically attached). The mount point becomes the root directory of the newly added filesystem, and that filesystem becomes accessible from that directory.
$ sudo mkdir /mnt/ankitpartition
I used the path /mnt/ankitpartition. You can use some other path.

Since you probably want it to be mounted all the time we’ll skip the mount command and go right into the fstab i.e. we would make this mount point default. The default mount points are the directories in which file systems will be mounted automatically when the computer is booted. Default mount points are listed in the file /etc/fstab.
So now we would make this partition default. First open fstab file in a text editor:
$ sudo nano /etc/fstab
You can use any other text editor, but I like nano.
Within the fstab you have to add a line which tells Linux to mount the partition. There should already be some entries which will give you a general guideline about how the line has to look like. Some examples:
mounting a FAT32 partition
/dev/sda2 /mnt/mypartition vfat umask=000,defaults 0 0
mounting a NTFS partition
/dev/sda2 /mnt/mypartition ntfs umask=000,defaults 0 0
To quit nano simply press ctrl + x. It will then ask you if you want to save the changes, press Y, and if you want to save it into the same fstab file, press Enter

And the last step would be:
$ sudo mount -a