Getting Query Timeout exception on Java Client 2.0.1 SNAPSHOT

Exception in thread “main” java.lang.RuntimeException: java.util.concurrent.TimeoutException
at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:481)
at rx.observables.BlockingObservable.single(BlockingObservable.java:348)
at com.couchbase.client.java.CouchbaseBucket.query(CouchbaseBucket.java:612)
at com.couchbase.client.java.CouchbaseBucket.query(CouchbaseBucket.java:574)
at com.amadeus.alf.TestCouchBase.createClient(TestCouchBase.java:31)
at com.amadeus.alf.TestCouchBase.main(TestCouchBase.java:15)
Caused by: java.util.concurrent.TimeoutException
at rx.internal.operators.OperatorTimeoutBase$TimeoutSubscriber.onTimeout(OperatorTimeoutBase.java:169)
at rx.internal.operators.OperatorTimeout$1$1.call(OperatorTimeout.java:42)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:43)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

Java Code : CouchbaseCluster cluster = CouchbaseCluster.fromConnectionString(“http://bdtv0600.os.amadeus.net:8091”);//CouchbaseCluster.create(“bdtv0601.os.amadeus.net”);
//System.out.println(cluster);
Bucket bucket = cluster.openBucket(“devalf”, “”);
//System.out.println(bucket);
System.out.println(“Start the query”);
QueryResult rs = bucket.query(“FROM devalf.phase”);
System.out.println(rs);
System.out.println(“Query call finsished”);
//System.out.println(rs);
List rows = rs.allRows();
for(QueryRow qr : rows){
System.out.println(qr.toString());
}
bucket.close();
cluster.disconnect();

Hi stalin,

Since you don’t write a sentence with a question mark I’m just making some assumptions what you are trying to achieve.

Finally, if you are a paying customer you can get addition support through the usual channels as well.

One more note: 2.0.1 GA has already been released, so no need to use SNAPSHOT anymore!

Hi Daschl,

Thanks a lot for your reply. Earlier I wasn’t aware that I have to configure the N1QL server. Now I downloaded the N1QL server on my local and then executed the below command.

  1. cbq-engine -couchbase http://bdtv0600.os.amadeus.net:8091/
  2. cbq-engine=http://localhost:8093/

After executing above two commands I am able to query my Couchbase server from coomand prompt and used to get Data based on query.

But my problem starts when I try to use the Java code to fetch the data.

    System.setProperty("com.couchbase.client.queryEnabled", "true");
    CouchbaseCluster cluster = CouchbaseCluster.create("http://localhost:8093");
    Bucket bucket = cluster.openBucket("devalf", "");
    System.out.println("Start the query");
    QueryResult rs = bucket.query("FROM devalf.phase");
    System.out.println(rs);
    System.out.println("Query call finsished");
    List<QueryRow> rows = rs.allRows();
    for(QueryRow qr : rows){
        System.out.println(qr.toString());
    }
    bucket.close();
    cluster.disconnect();

http://localhost:8093” is the url of my query server which I installed locally and configured to look at my couchbase server by running below command from CMD.

cbq-engine -couchbase http://bdtv0600.os.amadeus.net:8091

My question is I am not able to connect and getting below exception

ARNING: [null][KeyValueEndpoint]: Could not connect to endpoint, retrying with delay 2ms:
java.net.ConnectException: Connection refused: no further information: localhost/127.0.0.1:11210
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:739)
at com.couchbase.client.deps.io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:208)
at com.couchbase.client.deps.io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:287)
at com.couchbase.client.deps.io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:528)
at com.couchbase.client.deps.io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
at com.couchbase.client.deps.io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
at com.couchbase.client.deps.io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
at com.couchbase.client.deps.io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
at com.couchbase.client.deps.io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
at java.lang.Thread.run(Thread.java:744)

Hi,

you’re getting a few things wrong here.

  1. You need to run cbq-engine on every node of the cluster (so if only on localhost, then just on localhost is fine).

  2. That’s wrong: CouchbaseCluster cluster = CouchbaseCluster.create(“http://localhost:8093”);
    Just use CouchbaseCluster.create(“localhost”); because it will try to find the query engine on 8093 out of the box.

  3. Not sure if this query is going to work. Try something like “SELECT * FROM devalf”

  4. You need to make sure that a primary index is created at least, so you want to execute (http://docs.couchbase.com/prebuilt/n1ql/n1ql-dp3/#create-primary-index.html)
    CREATE PRIMARY INDEX ON devalf;

So your code should look more like:

System.setProperty("com.couchbase.client.queryEnabled", "true");
CouchbaseCluster cluster = CouchbaseCluster.create("localhost");
Bucket bucket = cluster.openBucket("devalf", "");
System.out.println("Start the query");
QueryResult rs = bucket.query("SELECT * FROM devalf");

One note: in the future, once N1QL is packaged into the server you dont even need to run cbq-engine manually or use queryEnabled. You just create the cluster, open the bucket and you’re good to go.

Hi Daschl,

Thanks a lot for your quick reply.
Currently I installed Couchbase community server 2.2.0 version in my local system and then ran the cbq-engine in my local system.

After the written the

System.setProperty(“com.couchbase.client.queryEnabled”, “true”);
CouchbaseCluster cluster = CouchbaseCluster.create(“localhost”);
Bucket bucket = cluster.openBucket(“devalf”, “”);
System.out.println(“Start the query”);
QueryResult rs = bucket.query(“SELECT * FROM devalf”);

But still in the logs I could see below information

INFO: CoreEnvironment: {sslEnabled=false, sslKeystoreFile=‘null’, sslKeystorePassword=‘null’, queryEnabled=false, queryPort=8093, bootstrapHttpEnabled=true, bootstrapCarrierEnabled=true, bootstrapHttpDirectPort=8091, bootstrapHttpSslPort=18091, bootstrapCarrierDirectPort=11210, bootstrapCarrierSslPort=11207, ioPoolSize=8, computationPoolSize=8, responseBufferSize=16384, requestBufferSize=16384, kvServiceEndpoints=1, viewServiceEndpoints=1, queryServiceEndpoints=1, ioPool=NioEventLoopGroup, coreScheduler=CoreScheduler, packageNameAndVersion=couchbase-java-client/2.0.1 (git: 2.0.1)}

queryEnabled=false ?

Also getting the above mentioned TimeOut Exception.
Is this exception related to queryEnabled=false ? if yes then what I need to do to solve the issue.

yes that makes sense, I just copied and didn’t check the flag in your code.

It actually is:

System.setProperty("com.couchbase.queryEnabled", "true"); (without the client thing).

Note that there is a much better option, you can do that manually through a builder:

    CouchbaseEnvironment env = DefaultCouchbaseEnvironment
        .builder()
        .queryEnabled(true)
        .build();
    Cluster cluster = CouchbaseCluster.create(env, "localhost");

HI Daschl,

Thanks for your help. Now I am able to connect and query the bucket.
I have one more query.
Previously I used to use CouchbaseClient API to save any data to the Couchbase server by add(key,json).

Can you please send me the piece of code through which I can save and update json to the Couchbase by using CouchbaseCluster API

1 Like

Hi @stalin_12,

So the “add” equivalent is “insert” on the bucket method. See: http://docs.couchbase.com/developer/java-2.0/documents-creating.html

And then you can decide what type of document to store. The easiest is the JsonDocument, which allows you to store JSON through a JsonObject, but if you already have your own JSON handling in place you can use the RawJsonDocument to read and write raw JSON data.

So either

bucket.insert(JsonDocument.create("id", JsonObject.create().put("foo", "bar)));

or

bucket.insert(RawJsonDocument.create("id", "{\"foo\": \"bar\"}"));

make sure to check the API docs for possible errors that can happen, especially the DocumentAlreadyExistsException.

Hi daschl,

While firing query to couchbase by using N1QL , can we pass the values to the queries dynamically by using Java SDK 2.0.1?

Like we do it in JDBC.

statement.setString(“ID”);

Hi daschl,

When I am creating Couchbase Cluser object, shall i give one server name of couchbase or all the server names which are in cluster?

Ex : Lets say I have a couchbase configuration where 5 nodes are there in a cluster
node1, node2, node3, node4, node5.

And I have started Query Engine on the all the nodes.

So my code should have

CouchbaseCluster cluster = CouchbaseCluster.create(node1);

or

CouchbaseCluster cluster = CouchbaseCluster.create(node1, node2, node3, node4, node5);

?

Hi,

the getting started guide (http://docs.couchbase.com/developer/java-2.0/hello-couchbase.html) states:

You do not need to pass in all nodes of the cluster, just a few seed nodes so that the client is able to establish initial contact.

So if you have a 5 node cluster, I recommend passing in any 3 that you see fit. Remember it will be removed anyways and the full one is taken, it’s really just for initial contact.

If in my cluster 5 nodes are there and I am just using 3 nodes for getting connection, in that case the data I have saved will be stored in all the 5 nodes or will be stored in 3 nodes which I had given for connection.

Okay, maybe let me rephrase it so that it gets clearer:

The list you pass in when you call CouchbaseCluster.create(…); is just to make an initial contact with the cluster. Once we have an initial contact, your list (regardless of what you passed in) will be thrown away and a fresh config list from the cluster will be used.

So even if you have a 50 node cluster and you only pass in 2 nodes here, all will be fine. You can also pass in only one and that will work too, but if this node happens to be down at the time of connect your application server will not be able to establish the contact. This is why we recommend in to pass in 2-3 nodes to the list so if one is down it can try the next.

Got it. Thanks a lot for a wonderful answer.

No problem! Let us know if you need anything else.

Hi,
Seems like .queryEnabled(true) is not longer necessary in Couchbase server >=4.5
I cannot find the method in the DefaultCouchbaseEnvironment Class for the new java smart client?
Am I right? (I have the timeout issue as well).
Cheers,
Ivan

Hi @1vand1ng0,

Yes queryEnabled was deprecated and taken away in SDK version 2.3.0. This is not related to the server version, as long as you have query service setup, it should work. Can you elaborate more on the timeout, does the query always times out and which client version are you using?

1 Like

Sorry. It was our fault. A sysadmin was redirecting a port to the wrong place. Kind of happens when the developer cannot see directly the deployment environment.
Thanks anyways,
I <3 couchbase cause the forums are very active. KUDOS.

Could you please elaborate what was wrong configuration made by sysadmin?.. Cause I’m facing timeout on basic select query (select * from bucket use keys ‘key’). Strange thing is query times out after around 10 k iterations and all the subsequent query iterations fail. I’m using java client 2.2.8. We have 5 nodes in the cluster with two nodes running query and index service.

@thenamespraveen I wish I knew exactly. He would not tell exactly cause they messed up, but they did tell me it was a port redirection issue. I am using java client 2.4.4
Mine is a development environment, only one node. All services running in the same node (they wont give me more nodes, cause they don’t understand horizontally scaling).
I wish I could help you more.