N1QL is failing with Timeout Exception

Hi,

I am trying this for a while but none of the solution is working for me. A small guidance will be very helpful.
Couchbase server version - 4.5.1-2844 Community Edition and SDK version - com.couchbase.client:java-client:2.3.5.

Exception received -
ERROR io.undertow.request.handleThrowable - UT005023: Exception handling request to /api/product/57
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.QueryTimeoutException: java.util.concurrent.TimeoutException; nested exception is java.lang.RuntimeException: java.util.concurrent.TimeoutException
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:110)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.SessionRestoringHandler.handleRequest(SessionRestoringHandler.java:119)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:211)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:809)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.dao.QueryTimeoutException: java.util.concurrent.TimeoutException; nested exception is java.lang.RuntimeException: java.util.concurrent.TimeoutException
at org.springframework.data.couchbase.core.CouchbaseExceptionTranslator.translateExceptionIfPossible(CouchbaseExceptionTranslator.java:122)
at org.springframework.data.couchbase.core.CouchbaseTemplate.execute(CouchbaseTemplate.java:539)
at org.springframework.data.couchbase.core.CouchbaseTemplate.queryN1QL(CouchbaseTemplate.java:493)
at org.springframework.data.couchbase.core.CouchbaseTemplate.findByN1QL(CouchbaseTemplate.java:428)
at org.springframework.data.couchbase.repository.query.AbstractN1qlBasedQuery.executeCollection(AbstractN1qlBasedQuery.java:155)
at org.springframework.data.couchbase.repository.query.AbstractN1qlBasedQuery.executeEntity(AbstractN1qlBasedQuery.java:161)
at org.springframework.data.couchbase.repository.query.AbstractN1qlBasedQuery.executeDependingOnType(AbstractN1qlBasedQuery.java:134)
at org.springframework.data.couchbase.repository.query.AbstractN1qlBasedQuery.execute(AbstractN1qlBasedQuery.java:105)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:483)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.couchbase.repository.support.ViewPostProcessor$ViewInterceptor.invoke(ViewPostProcessor.java:87)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy114.findByProductId(Unknown Source)
at com.services.ProductService.findInitByProductId(ProductService.java:200)
at com.controllers.ProductRestController.findProductById(ProductRestController.java:154)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
… 50 common frames omitted
Caused by: java.lang.RuntimeException: java.util.concurrent.TimeoutException
at com.couchbase.client.java.util.Blocking.blockForSingle(Blocking.java:73)
at com.couchbase.client.java.CouchbaseBucket.query(CouchbaseBucket.java:637)
at com.couchbase.client.java.CouchbaseBucket.query(CouchbaseBucket.java:572)
at org.springframework.data.couchbase.core.CouchbaseTemplate$8.doInBucket(CouchbaseTemplate.java:496)
at org.springframework.data.couchbase.core.CouchbaseTemplate$8.doInBucket(CouchbaseTemplate.java:493)
at org.springframework.data.couchbase.core.CouchbaseTemplate.execute(CouchbaseTemplate.java:536)
… 86 common frames omitted
Caused by: java.util.concurrent.TimeoutException: null
… 92 common frames omitted

What is query ? Did you executed query in query workbench and see. Did you created the right index.

Hi vsr1,

Yes same query which is being created internally by the SDK runs fine in the query workbench(ie in the couchbase server portal - under query tab), to be specific.

Query FYR -
SELECT META(ProductBucket).id AS _ID, META(ProductBucket).cas AS _CAS, ProductBucket.* FROM ProductBucket WHERE type = ‘PRODUCT’ and productId = $1

Besides, i tried with both @Query annotation and N1ql java file, with both it is failing. What I also observed that all the predefined methods in CrudRepository like findAll etc works perfectly fine but the custom defined queries dont work. Not sure why is the case?

Use the following index. If required increase timeout in SDK which might have set 75secs.

CRETE INDEX ix1 ON ProductBucket(productId) WHERE type = ‘PRODUCT’ ;

Still the same. Is it the case the no of queries having different where clauses will result in that many indexes.
If the above question’s answer is no, then i had created one common index by putting all the possible where clause variable in that index. But still I am getting timeout exception.

I would recommend read through https://blog.couchbase.com/create-right-index-get-right-performance/
If the problem is only through SDK, may be SDK experts may able to answer it.

Hi vsr1,

PFB the query which was formed in the code end but still it is throwing the timeout exception.

SELECT META(ProductBucket).id AS _ID, META(ProductBucket).cas AS _CAS, ProductBucket.* FROM ProductBucket USE INDEX (ix1) WHERE type = ‘PRODUCT’ and productId = ‘P70’

Here the same query was used to create the index as suggested by you, ie, based on ProductId.

Also, the findAll products in which we don’t give any customized query works perfectly fine, it is only for the customized queries that it fails with TimeoutException, so what i think it is more of an issue related to N1QL rather sdk. Please do let me know if I am wrong.

Thanks for the support till now. :slight_smile:

Run Above query using Query Workbench OR cbq Shell OR REST API and measure resultSize/executionTime . If that works N1QL side is fine.

Hi vsr1,

It works perfectly fine within 5ms. I ran it in the Couchbase query workbench. Here in the code i also increased the query timeout from 75 to 170.

Thanks

May be enable debug log in your SDK and checkout. I am not expert on Java SDK.

Let me try vsr1, few mins.

00:03:00.386 [cb-computations-4] DEBUG c.c.c.c.config.ConfigurationProvider - Received signal for outdated configuration.
00:03:00.426 [cb-computations-4] DEBUG c.c.c.c.config.ConfigurationProvider - New Bucket ProductBucket config proposed.
00:03:00.429 [cb-computations-4] DEBUG c.c.c.c.c.refresher.CarrierRefresher - Completed refreshing config for bucket "ProductBucket"
00:03:53.184 [cb-io-1-4] DEBUG c.c.c.c.e.AbstractGenericHandler - [couchbase/IP:8092][ViewEndpoint]: KeepAlive fired
00:00:56.430 [XNIO-2 task-1] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping - Looking up handler method for path /api/product/P70
00:00:56.430 [XNIO-2 task-1] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping - looking up handler for path: /api/product/P70
00:00:56.432 [XNIO-2 task-1] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping - Did not find handler method for [/api/product/P70]
00:00:56.432 [XNIO-2 task-1] DEBUG o.s.b.a.e.mvc.EndpointHandlerMapping - Looking up handler method for path /api/product/P70
00:00:56.440 [XNIO-2 task-1] DEBUG o.s.b.a.e.mvc.EndpointHandlerMapping - Did not find handler method for [/api/product/P70]
00:00:56.449 [XNIO-2 task-1] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean ‘productRestController'
00:00:56.466 [XNIO-2 task-1] INFO com.service.productRestController - Get Product : AP70
00:01:03.245 [cb-computations-4] DEBUG c.c.c.c.config.ConfigurationProvider - Received signal for outdated configuration.
00:01:03.245 [XNIO-2 task-1] INFO com.ProductService - Get Product by id : P70
00:01:11.141 [cb-computations-4] DEBUG c.c.c.c.config.ConfigurationProvider - Received signal for outdated configuration.
00:01:16.725 [cb-computations-1] DEBUG c.c.c.c.config.ConfigurationProvider - New Bucket ProductBucket config proposed.

These are the main logs in that long list. let me know if you need any specific log statement.

Hi vsr1,

Any updates?

Thanks