Friday, January 3, 2014

Full State Replication among WSO2 Carbon 4.2.X server nodes

The task of replicating the state among nodes of a cluster is not an easy task. More concern over state replication could define limitations in the scalability of the cluster. Previously in a cluster of WSO2 Carbon server nodes state replication has been done using given below methodologies.

1.)  Full state replication with a shared database.
2.)  Full state replication with the dissemination of state among nodes without any external persistence method.
3.)  Use of Sticky sessions.

The methods 1 and 3 would anyway be possible with external implementations whereas for the method 2  there should be some kind of group communication method. Refer the article at http://wso2.com/library/articles/clustering-landscape-wso2-stack/ for further information.

In WSO2 carbon releases prior to 4.2.X Tribes has been used as the group communication framework which is supported by the Axis2 clustering. But when Carbon shifted from Axis2 clustering to Hazelcast based clustering Tribes has also been dropped as the group communication framework. Axis2 context based clustering has been dropped since it was not a widely used technology and had many limitations.

Therefor in order to do a full state replication with Hazelcast clustering, in latest 4.2.x products it is recommended to use distributed caching with JCache. JCache is the Java caching standard developed by JSR107 and is included in Java EE 7. Users should write the application code using JCache, wherever they require sharing state across members in a cluster.

The given below is a simple Axis service which uses JCache to replicate state. This service can be deployed in a simple two node clustered setup of WSO2 Application Server 5.2.0 and test. Please refer my blog post at How to cluster WSO2 4.2.X products with latest Hazelcast clustering to setup a simple clustered setup of two AS nodes.

package org.wso2.appserver.sample.replication;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;

public class StateFullCarService {
    CacheManager cacheManager =   Caching.getCacheManagerFactory().getCacheManager("CarCacheManagerTest");
    Cache cache = cacheManager.getCache("CarCache");

    public String setCarInfo(String carName, String registrationNumber){
           if(carName!=null && registrationNumber!=null){
                   cache.put(registrationNumber, carName);
                   return "The Car "+carName+" is registered with "+registrationNumber;
           }else{
                    return "Fill all inputs !!!";
           }
    }

    public String getCarInfo(String registrationNumber){
           if(cache.get(registrationNumber)!=null){
                  return cache.get(registrationNumber);
           }else{
                   return "Invalid Registration Number !!!";
           }
    }
}

Download the StateFullCarService.aar from [1] and upload to both the Application Server nodes and follow the given below steps.

1.) On Application Server node1 use "Try this Service" to invoke the service.

2.) Do setCarInfo operation by providing a car name and a registration number.

3.) Now do getCarInfo operation and try to retrieve the car name by providing the previously entered registration number.

4.) Now go to the management console of Application Server node2 (open on another browser) and use "Try this Service" for the same service.

5.) Now do getCarInfo operation and try to retrieve the car name by providing the previously entered registration number at Application Server node 1. You will get same result you got in step 4.

6.) Now you can do vise-versa in the two Application Servers and try out.


[1] - https://drive.google.com/file/d/0B6bebiliFKjddVBMaDFnSEwzbzA/edit?usp=sharing

No comments:

Post a Comment