Skip to content

Connection Reset Error Occurs Occasionally with Client-v2 0.7.2 #2070

Open
@lelewolf

Description

Describe the bug

There is an issue where the ClickHouse client fails to execute a query, resulting in a “Connection reset” error. The request is being terminated unexpectedly.

Steps to reproduce

1.	Run the query on the ClickHouse client with a high load or specific network conditions.
2.	Observe that the connection resets with a SocketException: Connection reset error.
3.	The issue happens after the socket connection times out or gets interrupted.

Expected behaviour

The client should execute the query successfully without encountering a connection reset error, even with high traffic or under timeout conditions.

Code example

package com.opay.finder.analysis.config;

import com.clickhouse.client.api.Client;
import com.clickhouse.client.config.ClickHouseClientOption;
import com.clickhouse.client.config.ClickHouseHealthCheckMethod;
import java.time.temporal.ChronoUnit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lang
 * @description clickhouse-client配置类
 * @date 2024/10/24 10:58
 */
@Configuration
public class ClickHouseClientConfig {

  private final ClickHouseConfig clickHouseConfig;

  public ClickHouseClientConfig(ClickHouseConfig clickHouseConfig) {
    this.clickHouseConfig = clickHouseConfig;
  }

  @Bean
  public Client clickhouseClient() {
    return new Client.Builder()
        .addEndpoint(clickHouseConfig.getUrl())
        .setUsername(clickHouseConfig.getUsername())
        .setPassword(clickHouseConfig.getPassword())
        .setSocketTimeout(clickHouseConfig.getSocketTimeout(), ChronoUnit.HOURS)
        .setSocketKeepAlive(Boolean.TRUE)
        .setConnectTimeout(clickHouseConfig.getConnectionTimeout(), ChronoUnit.HOURS)
        .setConnectionTTL(clickHouseConfig.getConnectionTtl(), ChronoUnit.MINUTES)
        .setMaxConnections(clickHouseConfig.getMaxConnection())
        .enableConnectionPool(Boolean.TRUE)
        .setMaxRetries(3)
        .setOption(ClickHouseClientOption.ASYNC.getKey(), "false")
        .setOption(ClickHouseClientOption.AUTO_DISCOVERY.getKey(), "true")
//        .setSocketKeepAlive(true)
        .setOption(ClickHouseClientOption.LOAD_BALANCING_POLICY.getKey(), "roundRobin")
        .setOption(ClickHouseClientOption.HEALTH_CHECK_INTERVAL.getKey(), "60000")
        //这个研究一下,修改为获取系统当前负载的方式;默认是select 1
        .setOption(ClickHouseClientOption.HEALTH_CHECK_METHOD.getKey(), ClickHouseHealthCheckMethod.SELECT_ONE.name())
        .setConnectionRequestTimeout(clickHouseConfig.getConnectionRequestTimeout(), ChronoUnit.MINUTES)
        .build();
  }
}
### Error log
2025-01-07 07:10:43.143 opay-finder-web  [ForkJoinPool.commonPool-worker-129] DEBUG org.apache.hc.client5.http.wire [wire:106]- http-outgoing-399 << "[read] I/O error: Connection reset"
2025-01-07 07:10:43.144 opay-finder-web  [ForkJoinPool.commonPool-worker-129] DEBUG o.a.h.c.h.i.i.DefaultManagedHttpClientConnection [close:155]- http-outgoing-399 Close connection
2025-01-07 07:10:43.144 opay-finder-web  [ForkJoinPool.commonPool-worker-129] DEBUG o.a.h.c.h.i.c.InternalHttpClient [discardEndpoint:261]- ep-0000001120 endpoint closed
2025-01-07 07:10:43.144 opay-finder-web  [ForkJoinPool.commonPool-worker-129] DEBUG o.a.h.c.h.i.c.InternalHttpClient [discardEndpoint:265]- ep-0000001120 discarding endpoint
2025-01-07 07:10:43.144 opay-finder-web  [ForkJoinPool.commonPool-worker-129] DEBUG o.a.h.c.h.i.i.PoolingHttpClientConnectionManager [release:424]- ep-0000001120 releasing endpoint
2025-01-07 07:10:43.144 opay-finder-web  [ForkJoinPool.commonPool-worker-129] DEBUG o.a.h.c.h.i.i.PoolingHttpClientConnectionManager [release:455]- ep-0000001120 connection is not kept alive)
2025-01-07 07:10:43.144 opay-finder-web  [ForkJoinPool.commonPool-worker-129] DEBUG o.a.h.c.h.i.i.PoolingHttpClientConnectionManager [release:465]- ep-0000001120 connection released [route: {}->[http://10.166.16.117:8123]][total available: 1; route allocated: 3 of 15; total allocated: 3 of 15]
2025-01-07 07:10:43.145 opay-finder-web  [ForkJoinPool.commonPool-worker-129] ERROR c.o.f.a.b.i.QuerySchedulerServiceImpl [lambda$executeScheduledQuery$0:221]- 查询 [FX_PRO_10000009_FUNNEL_dF717CsZbAzhEql174CG9AUCYYSWXUWI_t00] 执行失败: Failed to execute request com.clickhouse.client.api.ClientException: Failed to execute request
	at com.clickhouse.client.api.internal.HttpAPIClientHelper.executeRequest(HttpAPIClientHelper.java:404)
	at com.clickhouse.client.api.Client.lambda$query$11(Client.java:1706)
	at com.clickhouse.client.api.Client.runAsyncOperation(Client.java:2116)
	at com.clickhouse.client.api.Client.query(Client.java:1782)
	at com.clickhouse.client.api.Client.query(Client.java:1647)
	at com.opay.finder.analysis.biz.impl.ClickHouseQueryServiceImpl.executeQuery(ClickHouseQueryServiceImpl.java:57)
	at com.opay.finder.analysis.biz.impl.ClickHouseQueryServiceImpl.executeSQLQuery(ClickHouseQueryServiceImpl.java:165)
	at com.opay.finder.analysis.biz.impl.ClickHouseQueryServiceImpl.executeSQL(ClickHouseQueryServiceImpl.java:125)
	at com.opay.finder.analysis.biz.impl.QuerySchedulerServiceImpl.lambda$executeScheduledQuery$0(QuerySchedulerServiceImpl.java:217)
	at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804)
	at java.base/java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1796)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:387)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1312)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1843)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1808)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:188)
Caused by: java.net.SocketException: Connection reset
	at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:318)
	at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:346)
	at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:796)
	at java.base/java.net.Socket$SocketInputStream.read(Socket.java:1099)
	at org.apache.hc.client5.http.impl.io.LoggingInputStream.read(LoggingInputStream.java:83)
	at org.apache.hc.core5.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:149)
	at org.apache.hc.core5.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:280)
	at org.apache.hc.core5.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:250)
	at org.apache.hc.core5.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:56)
	at org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:331)
	at org.apache.hc.core5.http.impl.io.HttpRequestExecutor.execute(HttpRequestExecutor.java:193)
	at org.apache.hc.client5.http.impl.classic.InternalExecRuntime.lambda$execute$0(InternalExecRuntime.java:236)
	at org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager$InternalConnectionEndpoint.execute(PoolingHttpClientConnectionManager.java:791)
	at org.apache.hc.client5.http.impl.classic.InternalExecRuntime.execute(InternalExecRuntime.java:233)
	at org.apache.hc.client5.http.impl.classic.MainClientExec.execute(MainClientExec.java:121)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.ConnectExec.execute(ConnectExec.java:199)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.ProtocolExec.execute(ProtocolExec.java:192)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.ContentCompressionExec.execute(ContentCompressionExec.java:150)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.HttpRequestRetryExec.execute(HttpRequestRetryExec.java:113)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.RedirectExec.execute(RedirectExec.java:110)
	at org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
	at org.apache.hc.client5.http.impl.classic.InternalHttpClient.doExecute(InternalHttpClient.java:174)
	at org.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:87)
	at org.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:55)
	at org.apache.hc.client5.http.classic.HttpClient.executeOpen(HttpClient.java:183)
	at com.clickhouse.client.api.internal.HttpAPIClientHelper.executeRequest(HttpAPIClientHelper.java:377)
	... 15 common frames omitted

Configuration

Environment

  • Client version: clent-v2 0.7.2
  • Language version: Java version “21.0.5” (LTS, 2024-10-15)
  • OS: CentOS Linux 7 (Core)

ClickHouse server

  • ClickHouse Server version: version 23.3.2.1
  • ClickHouse Server non-default settings, if any:
  • CREATE TABLE statements for tables involved:
  • Sample data for all these tables, use clickhouse-obfuscator if necessary

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions