Skip to content

Commit 8f400f4

Browse files
committed
Merge branch 'main' into feat_clientv2_timezone
2 parents 6d2feaa + 301d75f commit 8f400f4

File tree

4 files changed

+70
-68
lines changed

4 files changed

+70
-68
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public class Client implements AutoCloseable {
122122

123123
private boolean useNewImplementation = false;
124124

125+
private ClickHouseClient oldClient = null;
125126

126127
private Client(Set<String> endpoints, Map<String,String> configuration, boolean useNewImplementation) {
127128
this.endpoints = endpoints;
@@ -139,6 +140,7 @@ private Client(Set<String> endpoints, Map<String,String> configuration, boolean
139140
this.httpClientHelper = new HttpAPIClientHelper(configuration);
140141
LOG.info("Using new http client implementation");
141142
} else {
143+
this.oldClient = ClientV1AdaptorHelper.createClient(configuration);
142144
LOG.info("Using old http client implementation");
143145
}
144146
}
@@ -168,6 +170,10 @@ public void close() {
168170
} catch (Exception e) {
169171
LOG.error("Failed to close shared operation executor", e);
170172
}
173+
174+
if (oldClient != null) {
175+
oldClient.close();
176+
}
171177
}
172178

173179
public static class Builder {
@@ -663,8 +669,14 @@ public boolean ping() {
663669
* @return true if the server is alive, false otherwise
664670
*/
665671
public boolean ping(long timeout) {
666-
try (ClickHouseClient client = ClientV1AdaptorHelper.createClient(configuration)) {
667-
return client.ping(getServerNode(), Math.toIntExact(timeout));
672+
if (useNewImplementation) {
673+
try (QueryResponse response = query("SELECT 1 FORMAT TabSeparated").get(timeout, TimeUnit.MILLISECONDS)) {
674+
return true;
675+
} catch (Exception e) {
676+
return false;
677+
}
678+
} else {
679+
return oldClient.ping(getServerNode(), Math.toIntExact(timeout));
668680
}
669681
}
670682

@@ -986,43 +998,41 @@ public CompletableFuture<InsertResponse> insert(String tableName,
986998
} else {
987999
CompletableFuture<InsertResponse> responseFuture = new CompletableFuture<>();
9881000

989-
try (ClickHouseClient client = ClientV1AdaptorHelper.createClient(configuration)) {
990-
ClickHouseRequest.Mutation request = ClientV1AdaptorHelper
991-
.createMutationRequest(client.write(getServerNode()), tableName, settings, configuration).format(format);
1001+
ClickHouseRequest.Mutation request = ClientV1AdaptorHelper
1002+
.createMutationRequest(oldClient.write(getServerNode()), tableName, settings, configuration).format(format);
9921003

993-
CompletableFuture<ClickHouseResponse> future = null;
994-
try (ClickHousePipedOutputStream stream = ClickHouseDataStreamFactory.getInstance().createPipedOutputStream(request.getConfig())) {
995-
future = request.data(stream.getInputStream()).execute();
1004+
CompletableFuture<ClickHouseResponse> future = null;
1005+
try (ClickHousePipedOutputStream stream = ClickHouseDataStreamFactory.getInstance().createPipedOutputStream(request.getConfig())) {
1006+
future = request.data(stream.getInputStream()).execute();
9961007

997-
//Copy the data from the input stream to the output stream
998-
byte[] buffer = new byte[settings.getInputStreamCopyBufferSize()];
999-
int bytesRead;
1000-
while ((bytesRead = data.read(buffer)) != -1) {
1001-
stream.write(buffer, 0, bytesRead);
1002-
}
1003-
} catch (IOException e) {
1004-
responseFuture.completeExceptionally(new ClientException("Failed to write data to the output stream", e));
1008+
//Copy the data from the input stream to the output stream
1009+
byte[] buffer = new byte[settings.getInputStreamCopyBufferSize()];
1010+
int bytesRead;
1011+
while ((bytesRead = data.read(buffer)) != -1) {
1012+
stream.write(buffer, 0, bytesRead);
10051013
}
1014+
} catch (IOException e) {
1015+
responseFuture.completeExceptionally(new ClientException("Failed to write data to the output stream", e));
1016+
}
10061017

1007-
if (!responseFuture.isCompletedExceptionally()) {
1008-
try {
1009-
int operationTimeout = getOperationTimeout();
1010-
ClickHouseResponse clickHouseResponse;
1011-
if (operationTimeout > 0) {
1012-
clickHouseResponse = future.get(operationTimeout, TimeUnit.MILLISECONDS);
1013-
} else {
1014-
clickHouseResponse = future.get();
1015-
}
1016-
InsertResponse response = new InsertResponse(client, clickHouseResponse, clientStats);
1017-
responseFuture.complete(response);
1018-
} catch (ExecutionException e) {
1019-
responseFuture.completeExceptionally(new ClientException("Failed to get insert response", e.getCause()));
1020-
} catch (InterruptedException | TimeoutException e) {
1021-
responseFuture.completeExceptionally(new ClientException("Operation has likely timed out.", e));
1018+
if (!responseFuture.isCompletedExceptionally()) {
1019+
try {
1020+
int operationTimeout = getOperationTimeout();
1021+
ClickHouseResponse clickHouseResponse;
1022+
if (operationTimeout > 0) {
1023+
clickHouseResponse = future.get(operationTimeout, TimeUnit.MILLISECONDS);
1024+
} else {
1025+
clickHouseResponse = future.get();
10221026
}
1027+
InsertResponse response = new InsertResponse(clickHouseResponse, clientStats);
1028+
responseFuture.complete(response);
1029+
} catch (ExecutionException e) {
1030+
responseFuture.completeExceptionally(new ClientException("Failed to get insert response", e.getCause()));
1031+
} catch (InterruptedException | TimeoutException e) {
1032+
responseFuture.completeExceptionally(new ClientException("Operation has likely timed out.", e));
10231033
}
1024-
LOG.debug("Total insert (InputStream) time: {}", clientStats.getElapsedTime("insert"));
10251034
}
1035+
LOG.debug("Total insert (InputStream) time: {}", clientStats.getElapsedTime("insert"));
10261036

10271037
return responseFuture;
10281038
}
@@ -1127,7 +1137,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
11271137
metrics.setQueryId(queryId);
11281138
metrics.operationComplete();
11291139

1130-
return new QueryResponse(httpResponse, finalSettings, metrics);
1140+
return new QueryResponse(httpResponse, finalSettings.getFormat(), metrics);
11311141
} catch (ClientException e) {
11321142
throw e;
11331143
} catch (Exception e) {
@@ -1138,8 +1148,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
11381148
}, sharedOperationExecutor);
11391149
return future;
11401150
} else {
1141-
ClickHouseClient client = ClientV1AdaptorHelper.createClient(configuration);
1142-
ClickHouseRequest<?> request = client.read(getServerNode());
1151+
ClickHouseRequest<?> request = oldClient.read(getServerNode());
11431152
request.options(SettingsConverter.toRequestOptions(settings.getAllSettings()));
11441153
request.settings(SettingsConverter.toRequestSettings(settings.getAllSettings(), queryParams));
11451154
request.option(ClickHouseClientOption.ASYNC, false); // we have own async handling
@@ -1160,7 +1169,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
11601169
clickHouseResponse = request.execute().get();
11611170
}
11621171

1163-
return new QueryResponse(client, clickHouseResponse, finalSettings, format, clientStats);
1172+
return new QueryResponse(clickHouseResponse, format, clientStats);
11641173
} catch (ClientException e) {
11651174
throw e;
11661175
} catch (Exception e) {

client-v2/src/main/java/com/clickhouse/client/api/insert/InsertResponse.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@
66
import com.clickhouse.client.api.internal.ClientV1AdaptorHelper;
77
import com.clickhouse.client.api.metrics.OperationMetrics;
88
import com.clickhouse.client.api.metrics.ServerMetrics;
9-
import org.apache.hc.core5.http.ClassicHttpResponse;
109

1110
public class InsertResponse implements AutoCloseable {
1211
private final ClickHouseResponse responseRef;
13-
private final ClickHouseClient client;
14-
1512
private OperationMetrics operationMetrics;
1613

17-
public InsertResponse(ClickHouseClient client, ClickHouseResponse responseRef,
14+
public InsertResponse(ClickHouseResponse responseRef,
1815
ClientStatisticsHolder clientStatisticsHolder) {
1916
this.responseRef = responseRef;
20-
this.client = client;
2117
this.operationMetrics = new OperationMetrics(clientStatisticsHolder);
2218
this.operationMetrics.operationComplete();
2319
this.operationMetrics.setQueryId(responseRef.getSummary().getQueryId());
@@ -26,22 +22,13 @@ public InsertResponse(ClickHouseClient client, ClickHouseResponse responseRef,
2622

2723
public InsertResponse(OperationMetrics metrics) {
2824
this.responseRef = null;
29-
this.client = null;
3025
this.operationMetrics = metrics;
3126
}
3227

3328
@Override
3429
public void close() {
3530
if (responseRef != null) {
36-
try {
37-
responseRef.close();
38-
} finally {
39-
client.close();
40-
}
41-
}
42-
43-
if (client != null) {
44-
client.close();
31+
responseRef.close();
4532
}
4633
}
4734

client-v2/src/main/java/com/clickhouse/client/api/query/QueryResponse.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.clickhouse.client.api.query;
22

3-
import com.clickhouse.client.ClickHouseClient;
43
import com.clickhouse.client.ClickHouseResponse;
54
import com.clickhouse.client.api.ClientException;
65
import com.clickhouse.client.api.internal.ClientStatisticsHolder;
@@ -32,7 +31,6 @@ public class QueryResponse implements AutoCloseable {
3231

3332
private final ClickHouseResponse clickHouseResponse;
3433
private final ClickHouseFormat format;
35-
private ClickHouseClient client;
3634

3735
private QuerySettings settings;
3836

@@ -41,13 +39,10 @@ public class QueryResponse implements AutoCloseable {
4139
private ClassicHttpResponse httpResponse;
4240

4341
@Deprecated
44-
public QueryResponse(ClickHouseClient client, ClickHouseResponse clickHouseResponse,
45-
QuerySettings settings, ClickHouseFormat format,
42+
public QueryResponse(ClickHouseResponse clickHouseResponse, ClickHouseFormat format,
4643
ClientStatisticsHolder clientStatisticsHolder) {
47-
this.client = client;
4844
this.clickHouseResponse = clickHouseResponse;
4945
this.format = format;
50-
this.settings = settings;
5146
this.operationMetrics = new OperationMetrics(clientStatisticsHolder);
5247
this.operationMetrics.operationComplete();
5348
this.operationMetrics.setQueryId(clickHouseResponse.getSummary().getQueryId());
@@ -56,11 +51,10 @@ public QueryResponse(ClickHouseClient client, ClickHouseResponse clickHouseRespo
5651
settings.setOption("server_timezone", clickHouseResponse.getTimeZone());
5752
}
5853

59-
public QueryResponse(ClassicHttpResponse response, QuerySettings settings, OperationMetrics operationMetrics) {
54+
public QueryResponse(ClassicHttpResponse response, ClickHouseFormat format, OperationMetrics operationMetrics) {
6055
this.clickHouseResponse = null;
6156
this.httpResponse = response;
62-
this.format = settings.getFormat();
63-
this.settings = settings;
57+
this.format = format;
6458
this.operationMetrics = operationMetrics;
6559

6660
Header tzHeader = response.getFirstHeader(ClickHouseHttpProto.HEADER_TIMEZONE);
@@ -106,14 +100,6 @@ public void close() throws Exception {
106100
throw new ClientException("Failed to close response", e);
107101
}
108102
}
109-
110-
if (client !=null) {
111-
try {
112-
client.close();
113-
} catch (Exception e) {
114-
throw new ClientException("Failed to close client", e);
115-
}
116-
}
117103
}
118104

119105
public ClickHouseFormat getFormat() {

client-v2/src/test/java/com/clickhouse/client/ClientTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,25 @@ public void testRawSettings() {
115115
}
116116
}
117117

118+
@Test
119+
public void testPing() {
120+
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
121+
try (Client client = new Client.Builder()
122+
.addEndpoint(node.toUri().toString())
123+
.setUsername("default")
124+
.setPassword("")
125+
.useNewImplementation(System.getProperty("client.tests.useNewImplementation", "false").equals("true"))
126+
.build()) {
127+
Assert.assertTrue(client.ping());
128+
}
118129

130+
try (Client client = new Client.Builder()
131+
.addEndpoint("http://localhost:12345")
132+
.setUsername("default")
133+
.setPassword("")
134+
.useNewImplementation(System.getProperty("client.tests.useNewImplementation", "false").equals("true"))
135+
.build()) {
136+
Assert.assertFalse(client.ping(TimeUnit.SECONDS.toMillis(20)));
137+
}
138+
}
119139
}

0 commit comments

Comments
 (0)