ClickHouse / ClickHouse/clickhouse-java

Allow configuring HTTP redirect strategy for Apache HTTP Client

Đang mở
#2,776 3 bình luận 0 reaction 0 người được giao Xem trên GitHub
client-api-v2 enhancement
Ngôn ngữ chính
Java
Star
1.6k
Fork
636
Merge trung bình
2 ngày 23 giờ
Pull request đã merge (30 ngày)
29

Mô tả

Description:

Currently, the Client.Builder does not expose any way to configure the redirect strategy for the underlying Apache HTTP Client 5. This causes issues in environments where ClickHouse cluster infrastructure uses HTTP redirects (e.g., load balancers, reverse proxies, or cluster coordinators that redirect requests to the appropriate shard/replica).

Root cause:

Apache HTTP Client 5 does not follow redirects for POST requests by default. Since the ClickHouse Java client uses POST for all operations (queries, inserts, commands), any HTTP redirect response (301, 302, 303, 307, 308) from the server-side infrastructure will result in a failed request instead of being transparently followed.

The relevant code is in HttpAPIClientHelper.createHttpClient() where HttpClientBuilder is used without setting a redirect strategy:
```
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
// No .setRedirectStrategy(...) call
return clientBuilder.build();
Expected behavior:
```

Client.Builder should provide a way to configure redirect following behavior, for example:

```
Client client = new Client.Builder()
.addEndpoint("http://my-cluster:8123")
.setUsername("user")
.setPassword("password")
.followRedirects(true) // follow redirects for POST requests
.build();
```
Or alternatively allow passing a custom RedirectStrategy implementation:

```
Client client = new Client.Builder()
.addEndpoint("http://my-cluster:8123")
.setUsername("user")
.setPassword("password")
.setRedirectStrategy(new DefaultRedirectStrategy())
.build();
```
Workaround:

Currently the only workaround is to patch the internal httpClient field inside HttpAPIClientHelper via reflection, which is fragile and not suitable for production use:

```
Field helperField = client.getClass().getDeclaredField("httpClientHelper");
helperField.setAccessible(true);
Object helper = helperField.get(client);

Field httpClientField = helper.getClass().getDeclaredField("httpClient");
httpClientField.setAccessible(true);
httpClientField.set(helper, HttpClientBuilder.create()
.setRedirectStrategy(new DefaultRedirectStrategy())
.build());
```

Environment:

clickhouse-java version: 0.9.4
Apache HTTP Client 5 (HC5)
ClickHouse server behind a load balancer / reverse proxy with HTTP redirects enabled

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu trong HttpAPIClientHelper.createHttpClient(), nơi HttpClientBuilder được tạo mà không có chiến lược chuyển hướng, và theo dõi cách Client.Builder xây dựng helper. Xem xét API followRedirects hoặc setRedirectStrategy được đề xuất và xác định nó sẽ ảnh hưởng như thế nào đến các chuyển hướng POST. Hoàn tất có nghĩa là builder cung cấp cấu hình đã chọn và các request có thể theo các response chuyển hướng được hỗ trợ mà không cần reflection.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
java
Lĩnh vực
api, backend
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
55/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.