Java SO_timeout and Connection_Timeout issue - How to set the time that java client uses to connect with couchbase to be longer?

StackOverflow https://stackoverflow.com/questions/19674908

Question

I am using CouchbaseConnectionFactoryBuilder and HTTPnio to connect to my cluster that has 3 machines.

When I run the javacode it connects to the machine repeatedly and then performs some operation and then again disconnects and reconnects, authenticates etc., wasting a lot of time doing this.

How can I set the time for which the connection is there to be longer so that it does more operation when it connects and doesn't waste time reconnecting and authenticating after every 5 secs?

    public boolean init() throws DBException {      


    System.out.println("in INIT **************.");
    if (initialized) {
        return true;
    }
     HttpParams params = new SyncBasicHttpParams();
     params=params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 50000);
     params=params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 50000);

    /*
     Properties systemProperties = System.getProperties();
    systemProperties.put("net.spy.log.LoggerImpl", "net.spy.memcached.compat.log.SunLogger");
    System.setProperties(systemProperties);

    Logger logger = Logger.getLogger("com.couchbase.client");
    logger.setLevel(Level.FINEST);
    for(Handler h : logger.getParent().getHandlers()) {
        if(h instanceof ConsoleHandler){
            h.setLevel(Level.FINEST);
        }
    }
    */

    props = getProperties(); //used to set default configuration
    String url = props.getProperty(CONNECTION_URL, DEFAULT_PROP);
    String bucket = props.getProperty(CONNECTION_BUCKET, DEFAULT_PROP);
    String user = props.getProperty(CONNECTION_USER, DEFAULT_PROP);
    String passwd = props.getProperty(CONNECTION_PASSWD, DEFAULT_PROP);

    gson = new Gson();

    try {
        String urls[]= url.split(",");
        for(int i=0;i<urls.length;i++)
        uris.add(URI.create(urls[i]));

        CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder(); //Object to make connections to couchbase
        cfb.setOpTimeout(2000000); // wait up to 10 seconds for an operation to succeed
        //cfb.setOpQueueMaxBlockTime(5000); // wait up to 5 seconds when trying to enqueue an operation
        user_client = new CouchbaseClient(cfb.buildCouchbaseConnection(uris, "users", "")); //connection object for user bucket
        res_client = new CouchbaseClient(cfb.buildCouchbaseConnection(uris, "resources", ""));
        manip_client = new CouchbaseClient(cfb.buildCouchbaseConnection(uris, "manipulation", ""));
        photo_client = new CouchbaseClient(cfb.buildCouchbaseConnection(uris, "photos", ""));

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    initialized = true;
    return true;

}

public void cleanup(boolean warmup) {   //ends connection of thread with the DS
    System.out.println("shutdown coucebase client connection");
    user_client.shutdown(1, TimeUnit.SECONDS);
    res_client.shutdown(1, TimeUnit.SECONDS);
    manip_client.shutdown(1, TimeUnit.SECONDS);
    photo_client.shutdown(1, TimeUnit.SECONDS);
    initialized = false;
}

2013-10-29 23:13:55.491 INFO com.couchbase.client.ViewConnection:  Added SANKET-PC to connect queue
2013-10-29 23:13:55.495 INFO com.couchbase.client.ViewConnection:  Added awesome to connect queue
2013-10-29 23:13:55.497 INFO com.couchbase.client.ViewConnection:  Added gaurang-PC1 to connect queue
2013-10-29 23:13:55.497 INFO com.couchbase.client.CouchbaseClient:  viewmode property isn't defined. Setting viewmode to production mode
2013-10-29 23:13:55.850 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=/192.168.0.11:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 23:13:55.851 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=/192.168.0.12:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 23:13:55.879 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=/192.168.0.20:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 23:14:00.386 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@2430c1a0
2013-10-29 23:14:00.387 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@66b55df1
2013-10-29 23:14:00.387 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@1308c2db
2013-10-29 23:14:00.390 INFO net.spy.memcached.auth.AuthThread:  Authenticated to SANKET-PC/192.168.0.20:11210
2013-10-29 23:14:00.391 INFO net.spy.memcached.auth.AuthThread:  Authenticated to awesome/192.168.0.11:11210
2013-10-29 23:14:00.391 INFO net.spy.memcached.auth.AuthThread:  Authenticated to gaurang-PC1/192.168.0.12:11210
2013-10-29 23:14:04.892 INFO com.couchbase.client.ViewConnection:  Added SANKET-PC to connect queue
2013-10-29 23:14:04.898 INFO com.couchbase.client.ViewConnection:  Added awesome to connect queue
2013-10-29 23:14:04.901 INFO com.couchbase.client.ViewConnection:  Added gaurang-PC1 to connect queue
2013-10-29 23:14:04.903 INFO com.couchbase.client.CouchbaseClient:  viewmode property isn't defined. Setting viewmode to production mode
2013-10-29 23:14:05.206 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=/192.168.0.11:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 23:14:05.207 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=/192.168.0.12:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 23:14:05.208 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=/192.168.0.20:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 23:14:10.012 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@3fe40c9f
2013-10-29 23:14:10.013 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@14f8bb7e
2013-10-29 23:14:10.014 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@201cc181
2013-10-29 23:14:10.016 INFO com.couchbase.client.ViewConnection:  Added awesome to connect queue
2013-10-29 23:14:10.017 INFO net.spy.memcached.auth.AuthThread:  Authenticated to gaurang-PC1/192.168.0.12:11210
2013-10-29 23:14:10.027 INFO net.spy.memcached.auth.AuthThread:  Authenticated to awesome/192.168.0.11:11210
2013-10-29 23:14:10.027 INFO com.couchbase.client.ViewConnection:  Added gaurang-PC1 to connect queue
2013-10-29 23:14:10.027 INFO net.spy.memcached.auth.AuthThread:  Authenticated to SANKET-PC/192.168.0.20:11210
2013-10-29 23:14:14.535 INFO com.couchbase.client.ViewConnection:  Added SANKET-PC to connect queue
2013-10-29 23:14:14.536 INFO com.couchbase.client.CouchbaseClient:  viewmode property isn't defined. 

--
Does some opeartions and again reconnect authenticates wastes a lot of time .. How to increase this time so I can do more oprn before reconnecting

2013-10-29 22:58:48.132 INFO com.couchbase.client.ViewConnection:  Added SANKET-PC to connect queue
2013-10-29 22:58:48.133 INFO com.couchbase.client.ViewConnection:  Added gaurang-PC1 to connect queue
2013-10-29 22:58:48.134 INFO com.couchbase.client.CouchbaseClient:  viewmode property isn't defined. Setting viewmode to production mode
2013-10-29 22:58:48.941 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=/192.168.0.11:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 22:58:48.941 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=/192.168.0.12:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 22:58:48.942 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=/192.168.0.20:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 22:58:53.451 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@34c945ea
2013-10-29 22:58:53.451 INFO com.couchbase.client.ViewConnection:  Added gaurang-PC1 to connect queue
2013-10-29 22:58:53.451 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@a0d20b9
2013-10-29 22:58:53.454 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@107dd383
2013-10-29 22:58:53.456 INFO net.spy.memcached.auth.AuthThread:  Authenticated to gaurang-PC1/192.168.0.12:11210
2013-10-29 22:58:53.456 INFO com.couchbase.client.ViewConnection:  Added awesome to connect queue
2013-10-29 22:58:53.459 INFO net.spy.memcached.auth.AuthThread:  Authenticated to awesome/192.168.0.11:11210
2013-10-29 22:58:53.459 INFO net.spy.memcached.auth.AuthThread:  Authenticated to SANKET-PC/192.168.0.20:11210
2013-10-29 22:58:57.961 INFO com.couchbase.client.ViewConnection:  Added SANKET-PC to connect queue
2013-10-29 22:58:57.962 INFO com.couchbase.client.CouchbaseClient:  viewmode property isn't defined. Setting viewmode to production mode
2013-10-29 22:58:58.251 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=/192.168.0.11:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 22:58:58.252 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=/192.168.0.12:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 22:58:58.252 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=/192.168.0.20:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2013-10-29 22:59:02.754 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@f9da0cd
2013-10-29 22:59:02.755 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@35a7fabd
2013-10-29 22:59:02.755 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@3027f656
2013-10-29 22:59:02.757 INFO net.spy.memcached.auth.AuthThread:  Authenticated to gaurang-PC1/192.168.0.12:11210
2013-10-29 22:59:02.757 INFO net.spy.memcached.auth.AuthThread:  Authenticated to SANKET-PC/192.168.0.20:11210
2013-10-29 22:59:02.758 INFO net.spy.memcached.auth.AuthThread:  Authenticated to awesome/192.168.0.11:11210
Was it helpful?

Solution

this is easy to answer. Create the CouchbaseClient object once (in a singleton) and then reuse it across requests. This is documented best practice and makes sense, because object creation with all the stuff that is done in the background is expensive.

Also, make sure to not treat a bucket like a relational db table. All data for one application should go into one bucket, then you also - by design - only need one CouchbaseClient object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top