Friday, October 16, 2009

25 Horses

You have to organize a racing event to find out top 3 horses. You have a 5 lane track and you DO NOT have stop watch.

Find out the minimum number of races in which you can find the winners.
-> Seven races
Best answer was given by Mandeep Singh (8) and then by Amritpal Singh (10)

Friday, October 9, 2009

Move Coins


Just tell me how long you took to solve this :)

Friday, October 2, 2009

Find roof’s height

You entered in room and noticed that there is nothing but a bulb hanging from the ceiling. You noticed that the bulb is exactly touching your head and it is in the center of roof (where diagonals meet.)

So, what is roof’s height? BTW, you just came from office. So, go ahead and take some assumptions

Solution: -
The only assumption I will take is "I have a watch" & actually I do :) Now, given that bulb is hanging from the ceiling, I can make it swing like a "Pendulum". Then I can find out the length of the string T = 3.14 SQRT (L/32) . Finally, I will add 5'10 to L and that the roof's height :)

So, do you want more puzzles ??

Give your comments -

Friday, August 7, 2009

Dependency Injection

Dependency Injection (DI), Inversion of Control (IoC) sounds scary as compared to what they are. These two terms are used interchangeably in IT industry. Basically, IoC is a principle and DI is a design pattern which follows this principle. In this pattern (DI), container takes the responsibility to inject the appropriate resources to each component it manages. Lets take it step by step with example. If we look at the below implementation of CallService, we notice that it is tightly coupled with the class HomePhone.


public class CallService {
Callable callMaker = new HomePhone();

public void callClient(int clientId){
// we do not care how to make a call, but we will need callMaker
callMaker.makeCall();
}
// other methods to exposed goes here.
}

interface Callable{
void makeCall();
}

class HomePhone implements Callable{
public void makeCall() {
System.out.println("Calling from HomePhone ...");
}
}

class MobilePhone implements Callable{
public void makeCall() {
System.out.println("Calling from MobilePhone ...");
}
}


Problem is if we want to make a call from MobilePhone, we will have to write another service or will have to update CallService and recompile. BTW, who writes the code like above these days. Atleast we'll write our CallService like this.


public class CallService {
Callable callMaker = null;

public CallService(Callable callMaker){
this.callMaker = callMaker;
}
public void callClient(int clientId){
// we do not care how to make a call, but we will need callMaker
callMaker.makeCall();
}
// other methods to exposed goes here.
}


In this case what we are doing is "injecting" the dependency "Callable" to the component "CallService" via constructor. So, this is one form of dependency injection. But in this case I am asking my CallService client to instantiate a Callable object to be used. Other way is to provide a setter method for callMaker. What if the user of CallService forgets to instantiate the CallMaker before using it ? famus NullPointerException, remember ? There are few light weight containers which takes the responsiblity of injecting appropriate resources before you can use the component [Spring container.] Now questions is what exactly container does ? lets look at the below code which acts (sort of) as a container and manages given components based upon configuration file.


class Container {
private static Container INSTANCE = new Container();
public static Container getInstance() {
return INSTANCE;
}
Map components;

private Container() {
components = new HashMap();
try {
Properties properties = new Properties();
properties.load(new FileInputStream("testPackage/other/properties/container.properties"));
for (Map.Entry entry : properties.entrySet()){
process((String)entry.getKey(), (String)entry.getValue());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void process(String key, String value) throws Exception{
String[] arr = key.split("\\.");
if (arr.length == 1){
Object component = Class.forName(value).newInstance();
components.put(key, component);
}else{
Object component = components.get(arr[0]); // CallService
Object toBeSet = components.get(value); // CallMaker
PropertyUtils.setProperty(component, arr[1], toBeSet); //CallService.setCallMaker(...)
}
}
public Object getComponent(String key){
return components.get(key);
}

}

class CallServiceClient {
public static void main(String[] args){
Container container = Container.getInstance();
CallService service = (CallService)container.getComponent("callservice");
service.callClient(5);
}
}

container.properties
#Define callMaker
callMaker=testPackage.other.HomePhone

#Define callService
callService=testPackage.other.CallService
#inject callMaker to callService
callService.callMaker=callMaker
# end

This is a typical example of how a container manager different components and their dependencies.

Sunday, July 12, 2009

Synchronization is easy ?

Sometimes its hard to find bugs when it comes to multi-threading environment as few programers forget fundamental concepts of writing a synchronized block. If we talk about synchronized block I can create it in three different ways.

1. Using Object synchronization [acquire lock on "this"]
2. Using Class synchronization [acquire lock on "MyClass.class"]
3. Using "another" Object for synchronization [acquired lock on "different" object]

Lets see if you can find the bug in the below code. HINT: Read the above 3 points carefully.


/*
* CountManager
*/
public class CountManager {
static int count = 0;

public static synchronized void incrementFew(int n) {
for (int i = 0; i < n; i++) {
count++;
log("Static : " + count);
try {
Thread.sleep(200);
} catch (Exception e) {
// doesn't matter.
}
}
}

public synchronized void addFew(int n) {
for (int i = 0; i < n; i++) {
count++;
log("Non-Static : " + count);
try {
Thread.sleep(200);
} catch (Exception e) {
// doesn't matter.
}
}
}

/*
* CountMangagerClient, needs run()
*/
static abstract class CountManagerClient implements Runnable {
CountManager s;

public CountManagerClient(CountManager s) {
this.s = s;
}
}

private static void log(String str) {
System.out.println(str);
}

/* main */
public static void main(String[] args) {
CountManager s = new CountManager();
Thread t1 = new Thread(new CountManagerClient(s) {
public void run() {
s.addFew(5); // no change in client code
System.out.println("Result = " + CountManager.count);
}
});
Thread t2 = new Thread(new CountManagerClient(s) {
public void run() {
CountManager.incrementFew(5); // no change in client code
System.out.println("Result 2 = " + CountManager.count);
}
});
t1.start();
t2.start();
}
}


OUTPUT
Non-Static : 1
Static : 2
Non-Static : 4
Static : 4
Static : 6
Non-Static : 6
Static : 7
Non-Static : 8
Non-Static : 10
Static : 10
Result = 10
Result 2 = 10

If you were able to find the bug, you really know synchronization. Anyways , the problem here is we are trying to synchronize the code block by acquiring the locks from two differet objects AKA "this" and "MyClass.class"

So, what is the solution? its simple; either use Object synchronization or Class synchronization. But what if the code is already being used by other clients which uses both? humm !! then introduce another object wihch is common to both of these code blocks and acquire lock on that object. This way you will not have to change the client code. Here is how you will do that:



/*
* CountManager
* @Threadsafe
*/
public class CountManager {
static int count = 0;
/* Object to get lock from */
private static final Object obj = new Object();

public static void incrementFew(int n) {
synchronized (obj) { /* get lock from Object 'obj' instead of 'MyClass.class'*/
for (int i = 0; i < n; i++) {
count++;
log("Static : " + count);
try {
Thread.sleep(200);
} catch (Exception e) {
// doesn't matter.
}
}
}
}

public void addFew(int n) {
synchronized (obj) { /* get lock from Object 'obj' instead of 'this'*/
for (int i = 0; i < n; i++) {
count++;
log("Non-Static : " + count);
try {
Thread.sleep(200);
} catch (Exception e) {
// doesn't matter.
}
}
}
}

/*
* CountMangagerClient, needs run()
*/
static abstract class CountManagerClient implements Runnable {
CountManager s;

public CountManagerClient(CountManager s) {
this.s = s;
}
}

private static void log(String str) {
System.out.println(str);
}

public static void main(String[] args) {
CountManager s = new CountManager();
Thread t1 = new Thread(new CountManagerClient(s) {
public void run() {
s.addFew(5); // no change in client code
System.out.println("Result = " + CountManager.count);
}
});
Thread t2 = new Thread(new CountManagerClient(s) {
public void run() {
CountManager.incrementFew(5); // no change in client code
System.out.println("Result 2 = " + CountManager.count);
}
});
t1.start();
t2.start();
}
}


OUTPUT:
Static : 1
Static : 2
Static : 3
Static : 4
Static : 5
Non-Static : 6
Result 2 = 6
Non-Static : 7
Non-Static : 8
Non-Static : 9
Non-Static : 10
Result = 10

Thursday, July 9, 2009

Odd one out !!

Problem: In an array of integers, all but one occur even number of times. Find the one which occur odd number of times.

First solution:
Use Hashmap like data structure Map to hold the Integer in the given array & the corresponding count.
performance : O(n) + O(n) = O(n)
Problem : extra space & data structure

Second solution:
Sort the array & start skipping the pair e.g. [2,2,3,3,9,9,11,11,11,12,12,34,34] : 11 cannot be skipped as it’s not a pair.
performance: O (nlog(n)) + O(n)
problem: bad performance

Trick solution:
We know X XOR X = 0, therefore start with 0 as result and XOR all the elements with result.
performace : O(n)
problem: none, let me know if you see any


public static void main(String[] args){
int[] arr = {2,3,6,9,20,2,4,6,9,20,3};
int result = 0;
for(int i: arr){
result ^= i;
}
System.out.println(result); //prints 4
}




Saturday, June 27, 2009

Load Balancing

I would say load balancing is a two step process; within the application [EJB & JMS] and @ web tier.

Load balancing @ Web Tier: Improve the response time for the web users.
Most(rather all) of the order capturing systems needs better response time for their user and this cannot be achieved by running an application on single server. Few months back we had to deal with the same problem and we used Big IP F5 external load balancer to get around this.

Things we need to consider & we considered while using external load balancer:

1. Sticky Session: Most of the web-applications maintain some session state; either its as simple as user name or as complex as shopping cart. It’s important that the load balancer should forward the request to the same server every time the request is coming from the same user.

2. Health Monitor: It’s important that the load balancer should not forward the request to the failed or crashed servers. Also, it should start dispatching the request to server which comes back [It can actually be achieved by some kind of ping protocol.]

3. Load Balancing Algorithms: We used simple Round-Robin [with ratio configuration], but other algorithms like load based also exists, which dispatch the request to the server with lesser load than others.

All the above can be achieved by external load balancer, but in case a server fails we need to transfer the user sessions to other available servers. I am going to pass this problem for now [we'll talk about it in next post] as its a high availability problem not a load balancing one.

Load Balancing within the application [EJB & JMS]: Improve the processing within application.

Consider a scenario when your application is running a scheduled process & has to process n (n is a very big number) events. To achieve parallelism within the J2EE applications we have to utilize JMS or Work Manager API's. Even if a scheduler picks up n numbers of orders to process and to process them efficiently put them to JMS queue, all the messages will be processed via the listeners local to that queue.

To achieve true parallelism when application is deployed in the cluster, JMS queues need to be remote to the listeners. So, to utilize all the servers within the cluster we created a separate "messaging cluster" to hold some of the JMS queues which will be remote to the listeners deployed in the application cluster. I soon realized that the orders which need immediate processing should be deployed per server [performance reasons: messages are processed quicker via local listeners.]



Another benefit for this architecture is high availability of messages in JMS queue. In case one of the nodes in a cluster fails, the JMS queue, being deployed in a cluster, does not loose messages. I will save high availability discussion for the next post.















EJB Load balancing : Typically, orders are processed in an asynchronous way [SOA architecture] e.g. orders captured by an order capturing system (OCS) is sent to a MQ which is then picked up by some kind of Order fulfillment system (OFS) for further processing. Between these two layers namely OCS & OFS there has to be an integration layer like ESB / WebMethods etc. Typically, integration layer picks up the message from MQ, do some transformations, add transit logs to DB and route the message to an appropriate service. Let's consider that the fulfillment system is running in a cluster & an EJB named FulfillmentService is deployed in that cluster. So, with the given scenario the load balancing opportunity is only with the integration layer which can choose EJB Skelton to process the request. Almost all the application containers expose the API's to achieve this via an EJB client; I have only tested this with Websphere 6.1 ND.

ADVICE: If you are using IBM Websphere, you better be using IBM JRE @ client side as well.

While writing a client for an EJB deployed in a cluster, we just have to provide the cluster aware initial context. The first call to EJB is intercepted by some kind of interceptor know as Location Service Daemon which gives the client enough information about the cluster environment in which the EJB is deployed. From this point, client becomes cluster aware client and takes the load balancing responsibilities as well.



//Context ctx = getInitialContext("iiop://FA-LT5824.us.farwaha.com:38014");
// for cluster
Context ctx = getInitialContext("corbaloc::FA-LT5824.us.farwaha.com:9811,:MA-LT5184.us.farwaha.com:9811");

if (ctx == null) {
System.out.println("Context received is null");
System.exit(0);
}

//Object obj = ctx.lookup("FulfillmentService");

//cluster aware lookup
Object obj = ctx.lookup("cell/clusters/ClusterEJB/FulfillmentService");
IFulfillmentServiceBeanHome home = (IFulfillmentServiceBeanHome) PortableRemoteObject.narrow(
obj, IFulfillmentServiceBeanHome.class);
IFulfillmentService bean = home.create();

bean.importOrder("testXML"); // just test



Associated distributed problems:
1. Concurrency: If there are two updates being handled by the two differed EJB's at the same time, which one should be persisted? Always use optimistic locking [check the version before saving.] Yes, Hibernate provides this facility but sometimes its over kill [may be I will write another post on optimized concurrency control.]

2. Race Condition: If there are two updates UD1 & UD2 and due to asynchronous processing UD2 is processed before UD1 is received. What will happen when system receives UD1? This is a typical sequence problem, so it’s a good idea to always assign a version id to all the updates & make sure the system never accepts the version lower than what it currently holds [This is only true when the system is accepting full updates, in partial update scenarios system may will loose important updates.]


** I have Tested this architecture only on IBM Websphere ND 6.1.17. I believe that basic concepts remain same and its much easier to achieve the same with BEA Weblogic or JBOSS App container.

Saturday, June 20, 2009

Distributed Cache

Every application requires some level of caching, either its finite value (values which remain same for the application) cache or cache which holds stateful objects like HTTP Session objects. If the application is running in the same JVM, there is no problem*(don’t miss the *) but then we have a single point of failure.

Problems in caching: I would rather say important decisions which are mostly taken wrong.
1. What data to cache - should business entities / transactional data be cached?
2. How to clear the cache? Should soft references be used? How to remove the cached items which are not used?

So, running the same application in a clustered environment makes the above decision making process even harder.

There are different solutions to solve the above problem like JCache which takes care of distributing and synchronizing the objects in cache. Now a new project Infinispan [JBoss] expose the same interface as JCache [JSR-107] but takes a one step further to provide highly available data grid platform. So, what’s the difference? The difference is how data is being cached. In a typical caching systems the data is just replicated to provide high availability & therefore total memory available remains the same even after running the application in cluster [If we replicate the same object @ N servers, we need N * sizeOf(Object) memory in heap.] On the other hand data grid solutions increase the total effective heap size by replicating the objects in any given K servers [ K < N & K > 0]. So, what does it mean? It means that I can choose which objects I want to replicate in the cloud and how many times.

According to me data grid solutions make more sence. If you read the project introduction of Infinispan carefully, JBoss is going to concentrate on it and stop supporting JCache soon.

The purpose of this post is to lay down the background of what I will be blogging about in the coming few post. In the next post, we'll talk about J2EE Clustering, associated issues and its solutions.

Monday, May 18, 2009

Type Safe Classes - 2

In my previous post about Type Safe Classes, we can add two additional public API's



The above two API's look very decent and type safe and there is some hard to find problems here. Lets take this by example; In the below example, we can create a stack of animals and add any Type of animal to the stack. But unfortunately with our published APIs, we can add a Dog to animals [Stack] but cannot add List dogs to same stack using pushAll API, its not allowed (But this is what we want to achive, isn't it.)



Also, there is a similar problem with popAll API as well. Consider the below example, Also we know that its acceptable to collect animal object (any Type) in an Object, but we cannot collect all the animals in List<Object> at once using the above mentioned popAll() API



This wont work either because popAll API only take Collection<E> that means Collection<Animal> in our case. Note that Collection<Animal> is not same as Collection<Object> & similarly Iterable<Animal&tt; is not same as Iterable<Dog>. Question is, if the below code works [we can push any Type of E in the stack]



Below code should have also worked [see below code], for all the Types of E, but it doesn't because thought Animal & Dog has covarient type relationship [the referenced object's type parameter is a subclass], List<Animal> & List<Dog> defines "invariant type relationship", [List<Dog> is not a subtype of List<Animal>]



So, Java 1.5 generics also provide us with wild cards, which is what we have to use here. We have to define a "covarient type relationship" using wild cards.



Our same code which was not working before, will work because we converted our invarient type relationship to covarient type.



One more interesting thing is PECS principle [producer extends, consumer super], what it means is; when we only want to read or use the elements in a collection, we use extends keyword. e.g. pushAll(Iterable<? extends E> elements), we can just read the elements but cannot add anything in elements because the input is producer not a consumer. Even if we had pushAnimal(List<? extends Animal> animals) API, we cannot add any elements, except null, in the collection. So, if we want to add elements in the collection as well, we should use a consumer like popAll(Collection<? super E> collection.

I guess PECS rule is the most tricker of all the rules we have to follow while making our code generic.

Type Safe Classes

We always try to use type safe collections where ever possible, for example

What would it take to implement a type safe Collection like Stack ? Below is the implementation of type safe Stack class


Why I have used "@SuppressWarnings("unchecked")", and still claiming it to be type safe ? First, its type safe for the client using this Stack, its pushes and pops element of type E. Second, we know what we are doing, we are typecasting Object[] to E[]; hence a "@SuppressWarnings("unchecked")."

Friday, May 15, 2009

Groovy & Java

Closure(s) Groovy Closures, loosely speaking are method pointers. Also, Groovy is a 2nd standard language for JVM. So, does it mean we have function pointers in Java / JVM ? No, if you read about Closures carefully enough, they are actually objects. Let’s see if we can do something close to what Groovy can do in Java.

Below is a sample code written in Groovy script


def list = []
for (i in 1..5)
list.add(i);
/*I am treating doThis as a Closure, I could have written 'Closure doThis' as well */
def doSomething(toMe, doThis){
toMe.each(doThis)
}

doSomething(list, {print it + " "})
print("\n")
doSomething(list, {print it * 5 + " "})
print("\n")
list.each{print it + " "}

Below are the results

1 2 3 4 5
5 10 15 20 25
1 2 3 4 5


In the above example, "doThis" can be anything (its loosly typed, feature of dynamic language) but I am using it as a Closure. Method named "doSomething" simply applies the "doThis" closure to "toMe" variable. yea, I know its painful when on one side you say "Following generics, it’s good for you and your code's health" and on the other side "Look @ the really cool features of Groovy, you don’t even need to declare Type".. WTF. Anyways, the point is, what will it take to do the same thing in Java, Groovy style.

Lets first declare something like closure :)


Also, lets implement our own version of List (just to include groovy stype "each" method)



Now, let’s see our code in Action


Below are the results: as expected

1 2 3 4 5
5 10 15 20 25
10 20 30 40 50
1 2 3 4 5


Then only thing interesting in MyGroovy class is below code:


In Groovy list.each() method takes a closure, which is internally just an Object. So, in our implementation of List, we are also passing instance of Closure :). BTW, I do not know how Groovy implements this concept, but this technique is a good candidate. Also, given that SpringSource owns Groovy & Grails, we might see Groovy getting even more popular for web application development. According to me, it is going to be maintenance nightmare if that happens.

Thursday, May 14, 2009

ConcurrentModificationException

While iterating over the List, or anything which implements iteratable, we cannot modify the contents of that particular collection. Not a surprise, but today while implementing iteratable (@ work) I din't know how to achieve this. BTW, why do I need to throw this exception ?? do I care ?? should I not just use object locking ?? I guess all of us know the reason, object state in multithreaded applications & performance. Anyways, I think below implementation looks decent enough: