'InfluxDBIOException: java.net.SocketTimeoutException: timeout

I'm using Influx Db java wrapper and adding data in influx db for every 5 seconds with a retention policy of 30 days.

When I'm trying to query the data by sending multiple requests, the timeout exception is seen. Once I clear the db data , the response is received properly.

org.influxdb.InfluxDBIOException: java.net.SocketTimeoutException: timeout
    at org.influxdb.impl.InfluxDBImpl.execute(InfluxDBImpl.java:587)
    at org.influxdb.impl.InfluxDBImpl.query(InfluxDBImpl.java:425)
    **at services.SampleService.getTotalCount(SampleService.kt:45)**
    at sun.reflect.GeneratedMethodAccessor83.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at ninja.params.ControllerMethodInvoker.invoke(ControllerMethodInvoker.java:76)
    at ninja.FilterChainEnd.next(FilterChainEnd.java:45)
    at filters.PermissionFilter.filter(PermissionFilter.kt:30)
    at ninja.FilterChainImpl.next(FilterChainImpl.java:36)
    at filters.JwtFilter.filter(JwtFilter.kt:40)
    at ninja.FilterChainImpl.next(FilterChainImpl.java:36)
    at filters.CorsFilter.filter(CorsFilter.kt:10)
    at ninja.FilterChainImpl.next(FilterChainImpl.java:36)
    at ninja.NinjaDefault.onRouteRequest(NinjaDefault.java:102)
    at ninja.servlet.NinjaServletDispatcher.service(NinjaServletDispatcher.java:86)
    at com.google.inject.servlet.ServletDefinition.doServiceImpl(ServletDefinition.java:286)
    at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:276)
    at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:181)
    at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
    at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:85)
    at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:120)
    at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:135)
    at ninja.servlet.NinjaServletFilter.doFilter(NinjaServletFilter.java:106)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1759)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:582)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:512)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:134)
    at org.eclipse.jetty.server.Server.handle(Server.java:534)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:283)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:93)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(ExecuteProduceConsume.java:303)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(ExecuteProduceConsume.java:148)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:136)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketTimeoutException: timeout
    at okio.Okio$4.newTimeoutException(Okio.java:232)
    at okio.AsyncTimeout.exit(AsyncTimeout.java:285)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
    at okio.RealBufferedSource.request(RealBufferedSource.java:68)
    at okio.RealBufferedSource.require(RealBufferedSource.java:61)
    at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.java:304)
    at okhttp3.internal.http.Http1xStream$ChunkedSource.readChunkSize(Http1xStream.java:441)
    at okhttp3.internal.http.Http1xStream$ChunkedSource.read(Http1xStream.java:422)
    at okio.RealBufferedSource.read(RealBufferedSource.java:47)
    at okio.RealBufferedSource.request(RealBufferedSource.java:68)
    at okio.RealBufferedSource.require(RealBufferedSource.java:61)
    at okio.GzipSource.consumeHeader(GzipSource.java:114)
    at okio.GzipSource.read(GzipSource.java:73)
    at okio.RealBufferedSource.read(RealBufferedSource.java:47)
    at okio.ForwardingSource.read(ForwardingSource.java:35)
    at retrofit2.OkHttpCall$ExceptionCatchingRequestBody$1.read(OkHttpCall.java:279)
    at okio.RealBufferedSource.request(RealBufferedSource.java:68)
    at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:418)
    at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:402)
    at retrofit2.converter.moshi.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:39)
    at retrofit2.converter.moshi.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:25)
    at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:117)
    at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
    at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
    at org.influxdb.impl.InfluxDBImpl.execute(InfluxDBImpl.java:579)
    ... 46 more
Caused by: java.net.SocketException: Socket closed
    at java.net.SocketInputStream.read(SocketInputStream.java:204)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at okio.Okio$2.read(Okio.java:140)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)

The exception cause points to this line val results = influxDB.query(q)

val q = Query(
            "select sum(firstCount) as totalFirstCount, sum(secondCount) as totalSecondCount" +
                " ,sum(thirdCount) as totalThirdCount, sum(fourthCount) as totalFourthCount " +
                "from counters where time > now() - "1h" group by field1 , field2",
            "dbname")

All the field values are of Long Type and have values like 2721976000.

Below is response from explain statement of the above query.

EXPRESSION: sum(firstCount::integer)
NUMBER OF SHARDS: 123
NUMBER OF SERIES: 26568
CACHED VALUES: 105408
NUMBER OF FILES: 16200
NUMBER OF BLOCKS: 16200
SIZE OF BLOCKS: 76693173

EXPRESSION: sum(secondCount::integer)
NUMBER OF SHARDS: 123
NUMBER OF SERIES: 26568
CACHED VALUES: 105408
NUMBER OF FILES: 16200
NUMBER OF BLOCKS: 16200
SIZE OF BLOCKS: 86133364

EXPRESSION: sum(thirdCount::integer)
NUMBER OF SHARDS: 123
NUMBER OF SERIES: 26568
CACHED VALUES: 105408
NUMBER OF FILES: 16200
NUMBER OF BLOCKS: 16200
SIZE OF BLOCKS: 61967770

EXPRESSION: sum(fourthCount::integer)
NUMBER OF SHARDS: 123
NUMBER OF SERIES: 26568
CACHED VALUES: 105408
NUMBER OF FILES: 16200
NUMBER OF BLOCKS: 16200
SIZE OF BLOCKS: 61967754

I could not find the solution to solve the exception , in the java console logs the response is received properly. Any pointers whether to look at query optimization or cause of exception would be helpful.



Solution 1:[1]

I changed the default timeout of OKHttpClient and the exceptions were not thrown within the particular duration. Missed it earlier as I had passed a different instance of DB.

val client = OkHttpClient.Builder()
                .connectTimeout(1, TimeUnit.MINUTES)
                .readTimeout(1, TimeUnit.MINUTES)
                .writeTimeout(1, TimeUnit.MINUTES)
                .retryOnConnectionFailure(true)
val influxConnection = InfluxDBFactory.connect("http://localhost:8086", client)

Solution 2:[2]

I translate @Nirmal 's answer to java

   OkHttpClient.Builder client = new OkHttpClient.Builder()
            .connectTimeout(1, TimeUnit.MINUTES)
            .readTimeout(1, TimeUnit.MINUTES)
            .writeTimeout(1, TimeUnit.MINUTES)
            .retryOnConnectionFailure(true);
   InfluxDB influxdb = InfluxDBFactory.connect(influxUrl, client);

Solution 3:[3]

A simpler approach with newer clients (I testet 6.0): You can add to the url ?readTimeout=1m&connectTimeout=1m&writeTimeout=30s The default is 10s. See JavaDoc of com.influxdb.client.InfluxDBClientFactory#create(java.lang.String, char[], java.lang.String)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nirmal
Solution 2 Kai
Solution 3 niels