InfluxDBIOException:java.net.SocketTimeoutException:超时

Posted

技术标签:

【中文标题】InfluxDBIOException:java.net.SocketTimeoutException:超时【英文标题】:InfluxDBIOException: java.net.SocketTimeoutException: timeout 【发布时间】:2018-11-28 01:37:00 【问题描述】:

我正在使用 Influx Db java 包装器,每 5 秒在 influx db 中添加数据,保留期为 30 天。

当我尝试通过发送多个请求来查询数据时,会看到超时异常。 一旦我清除了数据库数据,就会正确接收到响应。

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)

异常原因指向这一行 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")

所有的字段值都是 Long 类型,并且有类似 2721976000 的值。

以下是上述查询的解释语句的响应。

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

我找不到解决异常的解决方案,在 java 控制台日志中,响应被正确接收。是否查看查询优化或异常原因的任何指针都会有所帮助。

【问题讨论】:

【参考方案1】:

我更改了 OKHttpClient 的默认超时时间,并且在特定持续时间内没有抛出异常。之前错过了,因为我通过了不同的 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)

【讨论】:

【参考方案2】:

我将 @Nirmal 的答案翻译成 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);

【讨论】:

以上是关于InfluxDBIOException:java.net.SocketTimeoutException:超时的主要内容,如果未能解决你的问题,请参考以下文章

Java Math

Java 布尔运算

java [Java] Java常用代码#java

Java - 35 Java 实例

Java While 循环

Java 字符串