Ouch!

Yesterday one of the developers that I work with lost all his work that he has done for the past two months! Apparently what happened was that the poor bagger did not commit anything to CVS for the past two months. So yesterday for some mysterious reason he has overridden his local changes with the outdated copy of the source code from CVS and tried to recompile everything. I don’t know why he did it.

I think he was almost crying telling me about the amount of work that was lost. During the conversation, it became obvious to me that for his luck, he had somewhere all the classes JARed as a backup (from before he has overridden the changes).

When I heard it, I said “Dude… you can decompile the classes! You can get the source code back” …

Ohhh… the look on his face after I said it – priceless …

Marshalling VS Serialization

The two terms are very very similar, and often treated the same. Quite few responds on various forums contradict each other, which made me a bit confused. So to make it clear for my self I decided to summarize what i understood from the resources I have read on the Internet:

Serialization:

  1. Technique of converting a state of an object (or object instance in different words) into a byte stream.
  2. To send object over the network, it needs to be serialized first, thus converted into to a sequence of bytes.

Marshalling:

  1. Technique of encoding an object to send it over the network and decoding it on the other end. Which is stub and skeleton mechanism, where stub is the local client and skeleton is the remote server.
  2. Besides the object state, marshalling also records codebases.
  3. Marshalling is used for sending objects through sockets. Off course objects must be serialized serialized before sending over the network.
  4. Marshalling is a way to create byte stream so that the copy of the original object can be recreated on the other side. When object is “unmarshalled” on the other side, object’s class definitions are loaded.
  5. Marshalling is a form of serialization, thats why I think some people treat teh two the same.

Thats it… I think… Did I miss something?

JBoss Clustering – HASingleton Service

Have you ever dealt with clustered singleton service? How to determine which cluster node is the master? Well, if I am the current node, I can simply ask whether I am the master or not. But what if I already know that the current node is not the master, and I want to determine which node among other nodes in the cluster is the master?

First I would like to give brief summary about HASingleton service (HA stands for High Availability).

Summary:
HASingleton service, is a service that is deployed on every node in a cluster, but runs only on one node, while the other nodes remain passive. The node that the service runs on, is the master node.

How does JBoss selects the master node?
Well the first node in a cluster will become master node. If existing master node will leave the cluster as a result of a shutdown for example, another node is selected as master from the remaining nodes.

Master node can control which tasks will get executed, and how many times. HASingletons also have the ability to share a memory state across clustered partition. Something like caching …

Solution:
Lets assume that I have a service bean that extends from HASingletonSupport class. HASingletonSupport in its turn extends from HAServiceMBeanSupport
and implements two interfaces: HASingletonMBean and HASingleton. All of them give me those wonderful APIs that can tell me whether the current node is the master or not, what the status of my cluster, how many nodes etc. etc.

public class MyHAService extends HASingletonSupport implements
        MyHAServiceMBean {

private static Logger logger =
		Logger.getLogger(MyHAService.class);

 public void startService() throws Exception {
        logger.info(" *** STARTED MY SINGLETON SERVICE *** ");
        super.startService();
 }

 public void stopService() throws Exception {
        logger.info(" *** STOPPED MY SINGLETON SERVICE *** ");
        super.stopService();
 }

public boolean isMasterNode() {
        return super.isMasterNode();
}

public void startSingleton() {
        logger.info(" *** CURRENT NODE IP:"
                + this.getPartition().getClusterNode()
			.getIpAddress().getHostAddress() +
			" ELECTED AS A MASTER NODE *** ");
}

public void stopSingleton() {
        logger.info(" *** CURRENT NODE IP:"
                + this.getPartition().getClusterNode()
			.getIpAddress().getHostAddress()
                + " STOPPED ACTING AS A MASTER NODE *** ");
}

public void partitionTopologyChanged(List newReplicants, int newViewID) {
        logger.info(" *** TOPOLOGY CHANGE STARTING *** ");
        super.partitionTopologyChanged(newReplicants, newViewID);

   }
}

startSingleton() – invoked when the current node elected as a master.
stopSingleton() – invoked when the current node stops acting as a master.
partitionTopologyChanged() – invoked when new node joins or leaves the cluster.

As i mentioned before, I have the ability to know whether the current node is the master node, by calling isMasterNode(). The method will return true if the node is master and false if its not.

In case I already know that the current node is not the master, I can ask the clustered partition (the cluster) which node is the master. For example I can request the current view of my cluster.

The implementation can be similar to the method below which you can have inside your service bean:

private String getMasterSocket() {

        HAPartition partition = this.getPartition();

        if (partition != null) {

            if (partition.getCurrentView() != null) {
                return partition.getCurrentView().get(0).toString();

            } else {
                return null;
            }
        } else {
            return null;
        }
    }

The method above will return me a string contains port and ip of the master node, for example:

192.168.62.12:1099

The HAPartition service maintains across cluster a registry of nodes in a view order. Now, keep in mind that an order of the nodes in the view, does not necessary reflect the order nodes have joined the cluster.

So the first node in the view as you can see beloew, will be the master node.
Simple as that.

return partition.getCurrentView().get(0).toString();

Please note:
Method getPartition() may return null, if super.startService() hasnt been called. Have a look at implementation of HAServiceMBeanSupport and my other post JBoss Clustering – How many nodes in the cluster?.

Multiple Return Statements

Yesterday I had a thought in my mind (which is good already to have one) – how many return statements a method should have?

Whats the difference if you have method that looks like that:

public int calculateSum(int a, int b, int c)  {
	int result = -1;
	if (a % b == c)  {
	   result = c;
	}
	else if ((a + b - c) > (a - c))  {
	   result = a;
	}

	return result;
}

or the same method but looks like that:

public int calculateSum(int a, int b, int c)  {
	if (a % b == c)  {
	   return c;
	}
	else if ((a + b - c) > (a - c))  {
	   return a;
	}

	return -1;
}

I always thought (at least this is how i was taught), that having one return statement makes code look more elegant, and i should try to avoid having multiple return statements. When i asked my colleague what he thinks about this, he said that at run time, method with multiple returns will execute faster and the code looks clearer. That sounded logical enough, and after doing some more investigation i have to say that i tend to agree.

Although some may say that having multiple return statements in a method can create a confusion, and it will be hard to see all the exit places. But if this is the case, i would say that maybe the method needs to be re-factored and simplified?