I have the problem that when a query is run in parallel one of them returns empty results.
Initially I found that out when testing in the browser and pressing F5 / refresh 2 or more times very fast afteranother.
I could reproduce it also in Java code with a very simple query:
select meta(
beer-sample).id from
beer-sample`
My environment:
Couchbase Server 4.6. DP
Java SDK Client: 2.4.0 (downloaded yesterday)
(exact same behaviour with Java SDK Client 2.3.5)
Here is my simplistic test code:
public class Testcase {
private static DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS");
public static void main(String[] args) {
CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder()
// autoReleaseAfter is to avoid "The content of this Observable
// is already released." errors
// see
// https://www.couchbase.com/forums/t/n1ql-query-with-adhoc-false-query-runs-into-illegalstateexception-the-content-of-this-observable-is-already-released/11004/4?u=synesty
.autoreleaseAfter(50000).queryTimeout(10000).retryStrategy(FailFastRetryStrategy.INSTANCE)
.reconnectDelay(Delay.fixed(30, TimeUnit.SECONDS)).build();
CouchbaseCluster cluster = CouchbaseCluster.create(env, "127.0.0.1");
Bucket bucket = cluster.openBucket("beer-sample");
String q = "select meta(`beer-sample`).id from `beer-sample` ";
Map<String, String> values = new HashMap<String, String>();
JsonObject params = JsonObject.from(values);
N1qlParams n1qlParams = N1qlParams.build();
N1qlQuery query = N1qlQuery.parameterized(q, params, n1qlParams);
N1qlQueryResult r1 = bucket.query(query);
N1qlQueryResult r2 = bucket.query(query);
System.out.println(fmt.print(new DateTime()) + " - sync result 1: " + r1.allRows().size());
System.out.println(fmt.print(new DateTime()) + " - sync result 2: " + r2.allRows().size());
Observable.just(q, q)
.flatMap(q1 -> Observable.just(q1).subscribeOn(Schedulers.io()).map(q2 -> runQuery(bucket, q2)), 4)
.toBlocking().subscribe();
bucket.close();
env.shutdown();
}
private static Object runQuery(Bucket bucket, String q) {
Map<String, String> values = new HashMap<String, String>();
JsonObject params = JsonObject.from(values);
N1qlParams n1qlParams = N1qlParams.build();
N1qlQuery query = N1qlQuery.parameterized(q, params, n1qlParams);
N1qlQueryResult r = bucket.query(query);
int numRows = r.allRows().size();
System.out.println(fmt.print(new DateTime()) + " - async result: " + numRows);
if (numRows == 0) {
System.err.println("empty result. Errors: " + r.errors());
}
return null;
}
}
Now I run this test program a couple of times:
-
Run
2017-01-09 08:02:04.495 - sync result 1: 7303
2017-01-09 08:02:04.526 - sync result 2: 7303
2017-01-09 08:02:04.540 - async result: 0
empty result. Errors:
2017-01-09 08:02:05.129 - async result: 7303 -
Run
2017-01-09 08:03:17.166 - sync result 1: 7303
2017-01-09 08:03:17.201 - sync result 2: 7303
2017-01-09 08:03:17.222 - async result: 0
empty result. Errors:
2017-01-09 08:03:18.055 - async result: 7303
Always the same. The sync query executions are fine, but from the async queries only one returns results.
Notice that the errors list is also empty, so I don’t know what’s going on.
Notice that the results in one of the async results start jumping up and down.
When you debug it set a breakpoint in the runQuery method in the line:
int numRows = r.allRows().size();
You will notice two running threads you your debug stack and in one of them r.allRows() is empty.
Now I started to bring some random sleep in each thread into the mix by adding the following code in the beginning of the runQuery method:
try {
Thread.sleep(new Random().nextInt(1000));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Now it looks like this:
1. Run
2017-01-09 08:04:59.197 - sync result 1: 7303
2017-01-09 08:04:59.228 - sync result 2: 7303
2017-01-09 08:04:59.669 - async result: 1349
2017-01-09 08:05:00.341 - async result: 7303
2. Run
2017-01-09 08:05:21.288 - sync result 1: 7303
2017-01-09 08:05:21.322 - sync result 2: 7303
2017-01-09 08:05:22.115 - async result: 4061
2017-01-09 08:05:22.706 - async result: 7303
3. Run
2017-01-09 08:05:57.189 - sync result 1: 7303
2017-01-09 08:05:57.219 - sync result 2: 7303
2017-01-09 08:05:57.979 - async result: 3638
2017-01-09 08:05:58.569 - async result: 7303
Conclusion:
To me that indicates that the queries are blocking each other somehow.
Am I doing something wrong here?
Is this a known 4.6 DP problem?
Thanks
Christoph