Monday, November 25, 2013

How to add sleep interval or delay between 2 mediators -WSO2 ESB

You can add thread.sleep by using script mediator. See following synapse configuration block. You can add it to between your operations.

<script language="js">java.lang.Thread.sleep(5000);</script>


However having Thread.sleep() has an impact when the ESB is working in very high concurrency levels. In ESB a thread pool is used to do the processing, if all the threads in this thread pool get blocked at the same time due to Thread.sleep() calls, then we have a problem. 

Wednesday, November 20, 2013

How to create multipart file upload API in WSO2 API Manager

I tried this scenario in my local machine with multi part file upload sample[1]. Please refer my instructions below.

01. Deployed file uploading war file into webserver(for this example i used WSO2  Application server 5.2.0).
02. Create API from publisher UI in API manager by pointing to file upload web service, and then publish API. Here you don't have to add any other parameter to synapse configuration.
03. Create SoapUI project to send requests(attached here).
04. Then added file attachment and oauth authentication header and send request to API manager's API.

I was able to send large(4.0 MB) files without any issue. I have attached fileupload-sample.zip file to this JIRA and it contains all artefacts i used for this test(file upload war file, SoapUI project and API definition).


[1]https://svn.wso2.org/repos/wso2/trunk/commons/qa/qa-artifacts/app-server/as-5.1.0/content_type/

fileupload-sample.zip

Tuesday, November 19, 2013

How Clustering and caching effect for WSO2 API Manager 1.2.0 distributed deployment

So here is a small description about caching and clustering configurations and parameters we used in WSO2 API manager. Lets see how clustering and caching effects for API manager.  1.2.0.

Gateway
=======
Effect of tribes clustering := Clustering must be enabled for gateways and all should clustered to single clustering domain, otherwise throttling replication will not happen.
Effect of cache enable:= There is no effect on caching if we don't use validation information cache at gateway.
Effect of synchronizeAll parameter in axis2.xml:= This doesn't have any effect on throttling information replication. Throttling information replication happens through axis2 context(according to throttle code) and there is no effect on this parameter. So we can set this as false for gateway nodes.

Key manager
===========
For key manager clustering should be enabled as we send cluster messages to invalidate cache. And we need to enable replicate cache for API key manager nodes as oauth2 cache is a replicated cache. Also we checked synch or asynch mode of replicate cache and didn't noticed any issues in the system.
SynchronizeAll parameter in axis2.xml doesn't have any effect for key manager nodes.

Enable async replicated cache mode can be done by setting sync property in cache.xml as false. According to infinity span documents, if Infinispan is configured in async mode, the cache will become inconsistent when the same key is modified by two peers at the same time. But inside our code we do not update cache elements ,instead of that we will invalidate old objects. So both asynch and synch should work fine with API manager.

In addition to that we have introduced complete new caching mechanism from API manager 1.5.0 onwards. Now we have JSR 107 (JCache) based caching implementation which has been implemented on top of Hazelcast. All components which were using the older infinispan & ehcache based implementations now changed to this new implementation.

Monday, November 11, 2013

How to fix issue in subscription page of the WSO2 API Manager due to missed API( ERROR - UserAwareAPIConsumer Failed to get API from : /apimgt/applicationdata/provider/)

Sometimes you may not able to view subscriptions in the Store of the WSO2 API Manager due to missing API from registry. When you visit subscription page
(https://192.168.1.4:9443/store/site/pages/subscriptions.jag) you might get following error.

[2013-11-11 21:14:58,904] ERROR - UserAwareAPIConsumer Failed to get API from : /apimgt/applicationdata/provider/admin/ddd/1.0.0/api
org.wso2.carbon.registry.core.exceptions.ResourceNotFoundException: Resource does not exist at path /_system/governance/apimgt/applicationdata/provider/admin/ddd/1.0.0/api
    at org.wso2.carbon.registry.core.jdbc.EmbeddedRegistry.get(EmbeddedRegistry.java:532)
    at org.wso2.carbon.registry.core.caching.CacheBackedRegistry.get(CacheBackedRegistry.java:166)
    at org.wso2.carbon.registry.core.session.UserRegistry.get(UserRegistry.java:524)
    at org.wso2.carbon.apimgt.impl.AbstractAPIManager.getAPI(AbstractAPIManager.java:292)
    at org.wso2.carbon.apimgt.hostobjects.APIStoreHostObject.addAPIObj(APIStoreHostObject.java:1289)
    at org.wso2.carbon.apimgt.hostobjects.APIStoreHostObject.jsFunction_getAllSubscriptions(APIStoreHostObject.java:1278)


The reason for this issue is api resource is not there in provided path(/_system/governance/apimgt/applicationdata/provider/admin/ddd/1.0.0/api ). Here, subscriptions are run time data that stored in api manager db and API are meta data stored in registry. This error log says application is having API which is not there in registry.

This can happen due to multiple reasons.
01. Deleting registry API resource without deleting it from publisher UI(then it will delete registry data and entries in API manager tables).
02. change governance space after create subscriptions().

For this issue you can do followings.
If governance space changed you can mount to old space again. Then missed API will appear in given path.
You can create missed (API ddd/1.0.0) again in the system. for this delete API from api manager tables and create API from publisher UI.
You can delete subscriptions associated with missed API(ddd/1.0.0). For this you need to delete all associated entries from api manager tables carefully.

To fix this issue please run following db queries against you API manager database. Its always recommend to run these queries on test environment and then apply to production servers.

Here first we need to get API_ID associated with problematic API. To get that please run following command
SELECT API_ID FROM AM_API where API_NAME = 'ddd' and API_VERSION = '1.0.0'

Then we need to delete all subscriptions associated with that API. Let say we got 3 as the result of above query. Run following query to delete all associated subscriptions.
DELETE FROM AM_SUBSCRIPTION where API_ID = '3'

Then you are done  :)

Tuesday, November 5, 2013

How to use AuthenticationAdmin service to authenticate user against WSO2 carbon server

In WSO2 products we use client server architecture within our servers. Any functionality available to you via WSO2 Carbon management console can also be invoked via a web service call. To invoke an admin service you have to authenticate user first, then get the session cookie and invoke the services you want. For this you need to invoke AuthenticationAdmin service. Here is a sample java code to invoke AuthenticationAdmin service and get session cookie. While running sample you can change username and password and see how authentication works.


package wso2.test.com;

import org.apache.axis2.AxisFault;
import org.apache.axis2.transport.http.HTTPConstants;
import org.wso2.carbon.authenticator.stub.AuthenticationAdminStub;
import org.wso2.carbon.authenticator.stub.LoginAuthenticationExceptionException;
import org.wso2.carbon.authenticator.stub.LogoutAuthenticationExceptionException;
import org.apache.axis2.context.ServiceContext;

import java.rmi.RemoteException;

public class AdminServiceClient {

    public static void main(String[] args)
            throws RemoteException, LoginAuthenticationExceptionException,
            LogoutAuthenticationExceptionException {
        System.setProperty("javax.net.ssl.trustStore", "/repository/resources/security/wso2carbon.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
        System.setProperty("javax.net.ssl.trustStoreType", "JKS");
        String backEndUrl = "https://localhost:9443";
        LoginAdminServiceClient login = new LoginAdminServiceClient(backEndUrl);
        String session = login.authenticate("admin", "admin");
    }
}

class LoginAdminServiceClient {
    private final String serviceName = "AuthenticationAdmin";
    private AuthenticationAdminStub authenticationAdminStub;
    private String endPoint;

    public LoginAdminServiceClient(String backEndUrl) throws AxisFault {
        this.endPoint = backEndUrl + "/services/" + serviceName;
        authenticationAdminStub = new AuthenticationAdminStub(endPoint);
    }

    public String authenticate(String userName, String password) throws RemoteException,
            LoginAuthenticationExceptionException {

        String sessionCookie = null;

        if (authenticationAdminStub.login(userName, password, "localhost")) {
            System.out.println("Login Successful");

            ServiceContext serviceContext = authenticationAdminStub.
                    _getServiceClient().getLastOperationContext().getServiceContext();
            sessionCookie = (String) serviceContext.getProperty(HTTPConstants.COOKIE_STRING);
            System.out.println(sessionCookie);
        }
        System.out.println("Login Unsuccessful");
        return sessionCookie;
    }

    public void logOut() throws RemoteException, LogoutAuthenticationExceptionException {
        authenticationAdminStub.logout();
    }
}

Recommended system requirement for WSO2 carbon 4.2.0 based products

Here are list of recommended hardware configurations for WSO2 carbon 4.2.0 based products. With following specifications we can get maximum output of servers.

Physical : 3GHz Dual-core Xeon/Opteron (or latest), 4 GB RAM 
(minimum : 2 GB for JVM and 2GB for the OS, 10GB free disk space 
(minimum) disk based on the expected storage requirements 
(calculate by considering the file uploads and the backup policies) . 

Virtual Machine : 2 compute units minimum (each unit having 1.0-1.2 
GHz Opteron/Xeon processor) 4 GB RAM 10GB free disk space. 
One cpu unit for OS and one for JVM. (e.g if 3 Carbon instances 
running require VM of 4 compute units 8 GB RAM 30 GB free space) 

EC2 : M1-Medium instance to run one Carbon instance. (e.g if 3 Carbon 
instances EC2 Large instance)

Friday, November 1, 2013

How to limit number of connections to cassandra server - WSO2 BAM2

We might need to limit number of connections to cassandra server due to some fine tuning purposes. Here we will see how we can do that.

For this we can use rpc_max_threads parameter. Description about rpc_max_threads as follows.
"Cassandra uses one thread-per-client for remote procedure calls. For a large number of client connections, this can cause excessive memory usage for the thread stack. Connection pooling on the client side is highly recommended. Setting a maximum thread pool size acts as a safeguard against misbehaved clients. If the maximum is reached, Cassandra will block additional connections until a client disconnects."


So we can use this parameter to limit number of active connections.

So you need to edit repository/conf/etc/cassandra.yaml file and edit  rpc_max_threads property with desired value.