[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 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
36

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ClickHouse/clickhouse-java のほかの issue

ClickHouse/clickhouse-java の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。