Java 2.2+ SDK long N1QL queries execution

Sometimes I need to execute large N1QL update statement, which updates most of the bucket’s data, without any return data. Such types of queries usually executes minutes, which is much more than casual queryTime of client setting.
Questions I have:

  1. What happens with SDK, when query operates longer than timeout, but at some moment of time it’ll finally succeed?
  2. Does this lock any threads from connection pool of client SDK?
  3. Am I able somehow to execute such type of queries on custom threadpool or with custom timeout not creating another CouchbaseCluster instance?

Hi @Rabajaba

To answer your questions:

  1. The client using the SDK will receive a RuntimeException which wraps a TimeoutException (so runtimeEx.getCause() instanceOf TimeoutException). That is when you use the default blocking API. The async() API receives the TimeoutException directly. However, it doesn’t mean that the operation won’t complete on the server side, just that the SDK doesn’t know if it was justing taking a long time, or it was unresponsive.
  2. This doesn’t lock any thread after the timeout occurred, as the SDK is asynchronous in nature (using Netty for I/O)
  3. Yes, all operations have an overload that allows you to customize the timeout (which is the simplest solution to your problem, IMO).

That covers everything, except one small thing:
how to overload client timeout for N1QL query?
bucket.async().query() doesn’t have such
N1QLParams has only serverSideTimeout(long timeout, TimeUnit unit), which javadoc I can’t really undestand:
what if timeout happens for DML statement? it’ll rollback? what if it would be SELECT?

ah forgot to mention that for the async() API you have to chain in the timeout(...) operator yourself, there’s no timeout by default, unlike in the blocking API…

there’s no rollback in N1QL, but maybe @geraldss can shed more light on what happens in these cases with the server side timeout?

There is no rollback.

This means that with server side timeout only a part of DML statement will be executed?
I would recommend to update javadoc on SDK, to provide more details on it’s behaviour.

This is documented in the N1QL docs. It is not specific to Java. You can search for “atomicity” in the N1QL docs.

You may also have to increase the node’s index scan timeout. It can’t be set in the client SDK.