[client-v2] An Error thrown while an insert writes its body shuts down the connection pool, so every later request of the Client fails

未關閉
#3,453 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
73/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
java

研究方向

Start with HttpAPIClientHelper.createRequest in HttpAPIClientHelper.java, where the insert body write callback is passed to an EntityTemplate; the issue identifies this as the fix location. Check how the callback behaves when it throws an Error, then reproduce with the provided program against a local ClickHouse server. Done means the insert still propagates the Error, while a later request and ping() on the same Client succeed.

由索引模型根據 Issue 內容生成。

描述

bug client connectivity

Description

When an Error (not an Exception) is thrown while client-v2 writes the body of an insert, the Client stays unusable: each later request fails with IllegalStateException: Connection pool shut down, and ping() returns false. Only a new Client works again.

The program below shows it on three entry points:

  • insert(table, DataStreamWriter, format, settings): the writer throws AssertionError;
  • insert(table, InputStream, format): read() of the stream throws ExceptionInInitializerError;
  • insert(table, List<POJO>): the serialization throws StackOverflowError for a Map nested 10,000 levels deep in a Dynamic column.

A RuntimeException from the same writer fails only that insert. An Error that the caller throws while it reads a query response does not shut down the pool.

Root cause. Apache HttpClient 5 closes its connection manager when an Error passes through MainClientExec.execute (httpclient5 5.6.4, MainClientExec.java:185-187: connectionManager.close(CloseMode.IMMEDIATE)), and client-v2 writes the insert body inside that call: HttpAPIClientHelper.createRequest (HttpAPIClientHelper.java:903) gives the write callback to an EntityTemplate.

The fix is a wrapper of the write callback in HttpAPIClientHelper.createRequest. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related: #3377 removes one trigger (a Dynamic value that contains itself, #3177), not this effect.

Steps to reproduce
  1. Start a ClickHouse server on localhost:8123.
  2. Run the program of "Code Example" with client-v2 from main (java -cp <client-v2 classpath> Repro.java).
  3. Each case makes one insert that fails, then runs SELECT 1 and ping() on the same Client.
Error Log or Exception StackTrace
writer throws AssertionError: insert threw java.lang.AssertionError
writer throws AssertionError: next request FAILED: java.lang.IllegalStateException: Connection pool shut down, ping() = false
InputStream.read throws ExceptionInInitializerError: insert threw java.lang.ExceptionInInitializerError
InputStream.read throws ExceptionInInitializerError: next request FAILED: java.lang.IllegalStateException: Connection pool shut down, ping() = false
POJO with a Map nested 10000 levels deep: insert threw java.lang.StackOverflowError
POJO with a Map nested 10000 levels deep: next request FAILED: java.lang.IllegalStateException: Connection pool shut down, ping() = false
contrast: writer throws IllegalStateException: insert threw com.clickhouse.client.api.ClickHouseException
contrast: writer throws IllegalStateException: next request OK, ping() = true

The Error passes through MainClientExec:

java.lang.AssertionError: boom
	...
	at com.clickhouse.client.api.Client.lambda$insert$2(Client.java:1878)
	at org.apache.hc.core5.http.io.entity.EntityTemplate.writeTo(EntityTemplate.java:123)
	...
	at org.apache.hc.client5.http.impl.classic.MainClientExec.execute(MainClientExec.java:120)
	...
	at com.clickhouse.client.api.internal.HttpAPIClientHelper.doExecuteRequest(HttpAPIClientHelper.java:837)

The next request:

com.clickhouse.client.api.ClientException: Failed to get query response
	at com.clickhouse.client.api.Client.queryAll(Client.java:2252)
Caused by: com.clickhouse.client.api.ClickHouseException: Query request failed (attempt: 1, duration: 0ms, queryId: null)
	at com.clickhouse.client.api.internal.HttpAPIClientHelper.wrapException(HttpAPIClientHelper.java:1250)
Caused by: java.lang.IllegalStateException: Connection pool shut down
	at org.apache.hc.core5.pool.LaxConnPool.lease(LaxConnPool.java:163)
	at org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager.lease(PoolingHttpClientConnectionManager.java:366)
Expected Behaviour

Only the insert that throws fails, as with a RuntimeException: the caller gets the Error, and the next request on the same Client runs.

Code Example
Reproduction program (Java, 63 lines)
import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.insert.InsertSettings;
import com.clickhouse.data.ClickHouseFormat;

import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class Repro {
    public static class Row {
        private int id; private Object v;
        public Row() {}
        public Row(int id, Object v) { this.id = id; this.v = v; }
        public int getId() { return id; } public void setId(int id) { this.id = id; }
        public Object getV() { return v; } public void setV(Object v) { this.v = v; }
    }

    static Client newClient() {
        return new Client.Builder().addEndpoint("http://localhost:8123")
                .setUsername("default").setPassword("").build();
    }

    static void run(String label, Consumer<Client> failingInsert) {
        try (Client client = newClient()) {
            try {
                failingInsert.accept(client);
            } catch (Throwable t) {
                System.out.println(label + ": insert threw " + t.getClass().getName());
            }
            try {
                client.queryAll("SELECT 1");
                System.out.println(label + ": next request OK, ping() = " + client.ping());
            } catch (Exception e) {
                Throwable root = e; while (root.getCause() != null) root = root.getCause();
                System.out.println(label + ": next request FAILED: " + root + ", ping() = " + client.ping());
            }
        }
    }

    public static void main(String[] args) {
        try (Client client = newClient()) {
            client.queryAll("CREATE OR REPLACE TABLE t_s (id Int32) ENGINE = Memory");
            client.queryAll("CREATE OR REPLACE TABLE t_d (id Int32, v Dynamic) ENGINE = MergeTree ORDER BY id");
        }
        run("writer throws AssertionError", c -> c.insert("t_s",
                out -> { throw new AssertionError("boom"); },
                ClickHouseFormat.RowBinary, new InsertSettings()).join());
        run("InputStream.read throws ExceptionInInitializerError", c -> c.insert("t_s", new InputStream() {
                    @Override public int read() { throw new ExceptionInInitializerError("boom"); }
                }, ClickHouseFormat.RowBinary).join());
        run("POJO with a Map nested 10000 levels deep", c -> {
            c.register(Row.class, c.getTableSchema("t_d"));
            Object v = "leaf";
            for (int i = 0; i < 10_000; i++) { Map<String, Object> m = new HashMap<>(); m.put("k", v); v = m; }
            c.insert("t_d", Collections.singletonList(new Row(1, v))).join();
        });
        run("contrast: writer throws IllegalStateException", c -> c.insert("t_s",
                out -> { throw new IllegalStateException("boom"); },
                ClickHouseFormat.RowBinary, new InsertSettings()).join());
    }
}
Configuration
Client Configuration
new Client.Builder().addEndpoint("http://localhost:8123").setUsername("default").setPassword("").build()
Environment
  • Cloud
  • Client version: client-v2 from main 8480ca001 (0.12.0-rc1-SNAPSHOT), httpclient5 5.6.4
  • Language version: Java 17.0.21
  • OS: Linux
ClickHouse Server
  • ClickHouse Server version: 26.11.2.4
  • ClickHouse Server non-default settings, if any: none
  • CREATE TABLE statements for tables involved: in the program
  • Sample data for all these tables: none

Classification: impact: Stability · area: Connectivity (connection pool)


Found by automated analysis (Polyglot AI) while working on #3177. Verified against a live server.

主要語言
Java
星號
1.6k
分支
657
平均合併
2 天 6 小時
30 天內合併 PR
36

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

ClickHouse/clickhouse-java 的其他 Issue

查看 ClickHouse/clickhouse-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。