Skip to content

Commit 0a03e76

Browse files
committed
made old client instatiated on new client creation
1 parent 5a49f48 commit 0a03e76

File tree

4 files changed

+70
-71
lines changed

4 files changed

+70
-71
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
@@ -119,6 +119,7 @@ public class Client implements AutoCloseable {
119119

120120
private boolean useNewImplementation = false;
121121

122+
private ClickHouseClient oldClient = null;
122123

123124
private Client(Set<String> endpoints, Map<String,String> configuration, boolean useNewImplementation) {
124125
this.endpoints = endpoints;
@@ -136,6 +137,7 @@ private Client(Set<String> endpoints, Map<String,String> configuration, boolean
136137
this.httpClientHelper = new HttpAPIClientHelper(configuration);
137138
LOG.info("Using new http client implementation");
138139
} else {
140+
this.oldClient = ClientV1AdaptorHelper.createClient(configuration);
139141
LOG.info("Using old http client implementation");
140142
}
141143
}
@@ -165,6 +167,10 @@ public void close() {
165167
} catch (Exception e) {
166168
LOG.error("Failed to close shared operation executor", e);
167169
}
170+
171+
if (oldClient != null) {
172+
oldClient.close();
173+
}
168174
}
169175

170176
public static class Builder {
@@ -589,8 +595,14 @@ public boolean ping() {
589595
* @return true if the server is alive, false otherwise
590596
*/
591597
public boolean ping(long timeout) {
592-
try (ClickHouseClient client = ClientV1AdaptorHelper.createClient(configuration)) {
593-
return client.ping(getServerNode(), Math.toIntExact(timeout));
598+
if (useNewImplementation) {
599+
try (QueryResponse response = query("SELECT 1 FORMAT TabSeparated").get(timeout, TimeUnit.MILLISECONDS)) {
600+
return true;
601+
} catch (Exception e) {
602+
return false;
603+
}
604+
} else {
605+
return oldClient.ping(getServerNode(), Math.toIntExact(timeout));
594606
}
595607
}
596608

@@ -912,43 +924,41 @@ public CompletableFuture<InsertResponse> insert(String tableName,
912924
} else {
913925
CompletableFuture<InsertResponse> responseFuture = new CompletableFuture<>();
914926

915-
try (ClickHouseClient client = ClientV1AdaptorHelper.createClient(configuration)) {
916-
ClickHouseRequest.Mutation request = ClientV1AdaptorHelper
917-
.createMutationRequest(client.write(getServerNode()), tableName, settings, configuration).format(format);
927+
ClickHouseRequest.Mutation request = ClientV1AdaptorHelper
928+
.createMutationRequest(oldClient.write(getServerNode()), tableName, settings, configuration).format(format);
918929

919-
CompletableFuture<ClickHouseResponse> future = null;
920-
try (ClickHousePipedOutputStream stream = ClickHouseDataStreamFactory.getInstance().createPipedOutputStream(request.getConfig())) {
921-
future = request.data(stream.getInputStream()).execute();
930+
CompletableFuture<ClickHouseResponse> future = null;
931+
try (ClickHousePipedOutputStream stream = ClickHouseDataStreamFactory.getInstance().createPipedOutputStream(request.getConfig())) {
932+
future = request.data(stream.getInputStream()).execute();
922933

923-
//Copy the data from the input stream to the output stream
924-
byte[] buffer = new byte[settings.getInputStreamCopyBufferSize()];
925-
int bytesRead;
926-
while ((bytesRead = data.read(buffer)) != -1) {
927-
stream.write(buffer, 0, bytesRead);
928-
}
929-
} catch (IOException e) {
930-
responseFuture.completeExceptionally(new ClientException("Failed to write data to the output stream", e));
934+
//Copy the data from the input stream to the output stream
935+
byte[] buffer = new byte[settings.getInputStreamCopyBufferSize()];
936+
int bytesRead;
937+
while ((bytesRead = data.read(buffer)) != -1) {
938+
stream.write(buffer, 0, bytesRead);
931939
}
940+
} catch (IOException e) {
941+
responseFuture.completeExceptionally(new ClientException("Failed to write data to the output stream", e));
942+
}
932943

933-
if (!responseFuture.isCompletedExceptionally()) {
934-
try {
935-
int operationTimeout = getOperationTimeout();
936-
ClickHouseResponse clickHouseResponse;
937-
if (operationTimeout > 0) {
938-
clickHouseResponse = future.get(operationTimeout, TimeUnit.MILLISECONDS);
939-
} else {
940-
clickHouseResponse = future.get();
941-
}
942-
InsertResponse response = new InsertResponse(client, clickHouseResponse, clientStats);
943-
responseFuture.complete(response);
944-
} catch (ExecutionException e) {
945-
responseFuture.completeExceptionally(new ClientException("Failed to get insert response", e.getCause()));
946-
} catch (InterruptedException | TimeoutException e) {
947-
responseFuture.completeExceptionally(new ClientException("Operation has likely timed out.", e));
944+
if (!responseFuture.isCompletedExceptionally()) {
945+
try {
946+
int operationTimeout = getOperationTimeout();
947+
ClickHouseResponse clickHouseResponse;
948+
if (operationTimeout > 0) {
949+
clickHouseResponse = future.get(operationTimeout, TimeUnit.MILLISECONDS);
950+
} else {
951+
clickHouseResponse = future.get();
948952
}
953+
InsertResponse response = new InsertResponse(clickHouseResponse, clientStats);
954+
responseFuture.complete(response);
955+
} catch (ExecutionException e) {
956+
responseFuture.completeExceptionally(new ClientException("Failed to get insert response", e.getCause()));
957+
} catch (InterruptedException | TimeoutException e) {
958+
responseFuture.completeExceptionally(new ClientException("Operation has likely timed out.", e));
949959
}
950-
LOG.debug("Total insert (InputStream) time: {}", clientStats.getElapsedTime("insert"));
951960
}
961+
LOG.debug("Total insert (InputStream) time: {}", clientStats.getElapsedTime("insert"));
952962

953963
return responseFuture;
954964
}
@@ -1051,7 +1061,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
10511061
metrics.setQueryId(queryId);
10521062
metrics.operationComplete();
10531063

1054-
return new QueryResponse(httpResponse, finalSettings, metrics);
1064+
return new QueryResponse(httpResponse, finalSettings.getFormat(), metrics);
10551065
} catch (ClientException e) {
10561066
throw e;
10571067
} catch (Exception e) {
@@ -1062,8 +1072,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
10621072
}, sharedOperationExecutor);
10631073
return future;
10641074
} else {
1065-
ClickHouseClient client = ClientV1AdaptorHelper.createClient(configuration);
1066-
ClickHouseRequest<?> request = client.read(getServerNode());
1075+
ClickHouseRequest<?> request = oldClient.read(getServerNode());
10671076
request.options(SettingsConverter.toRequestOptions(settings.getAllSettings()));
10681077
request.settings(SettingsConverter.toRequestSettings(settings.getAllSettings(), queryParams));
10691078
request.option(ClickHouseClientOption.ASYNC, false); // we have own async handling
@@ -1084,7 +1093,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
10841093
clickHouseResponse = request.execute().get();
10851094
}
10861095

1087-
return new QueryResponse(client, clickHouseResponse, finalSettings, format, clientStats);
1096+
return new QueryResponse(clickHouseResponse, format, clientStats);
10881097
} catch (ClientException e) {
10891098
throw e;
10901099
} 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 & 20 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;
@@ -10,7 +9,6 @@
109
import com.clickhouse.data.ClickHouseFormat;
1110
import org.apache.hc.core5.http.ClassicHttpResponse;
1211

13-
import java.io.InputStream;
1412
import java.io.InputStream;
1513

1614
/**
@@ -30,34 +28,27 @@ public class QueryResponse implements AutoCloseable {
3028

3129
private final ClickHouseResponse clickHouseResponse;
3230
private final ClickHouseFormat format;
33-
private ClickHouseClient client;
34-
35-
private QuerySettings settings;
3631

3732
private OperationMetrics operationMetrics;
3833

3934
private ClassicHttpResponse httpResponse;
4035

4136
@Deprecated
42-
public QueryResponse(ClickHouseClient client, ClickHouseResponse clickHouseResponse,
43-
QuerySettings settings, ClickHouseFormat format,
37+
public QueryResponse(ClickHouseResponse clickHouseResponse, ClickHouseFormat format,
4438
ClientStatisticsHolder clientStatisticsHolder) {
45-
this.client = client;
4639
this.clickHouseResponse = clickHouseResponse;
4740
this.format = format;
48-
this.settings = settings;
4941
this.operationMetrics = new OperationMetrics(clientStatisticsHolder);
5042
this.operationMetrics.operationComplete();
5143
this.operationMetrics.setQueryId(clickHouseResponse.getSummary().getQueryId());
5244
ClientV1AdaptorHelper.setServerStats(clickHouseResponse.getSummary().getProgress(),
5345
this.operationMetrics);
5446
}
5547

56-
public QueryResponse(ClassicHttpResponse response, QuerySettings settings, OperationMetrics operationMetrics) {
48+
public QueryResponse(ClassicHttpResponse response, ClickHouseFormat format, OperationMetrics operationMetrics) {
5749
this.clickHouseResponse = null;
5850
this.httpResponse = response;
59-
this.format = settings.getFormat();
60-
this.settings = settings;
51+
this.format = format;
6152
this.operationMetrics = operationMetrics;
6253
}
6354

@@ -94,14 +85,6 @@ public void close() throws Exception {
9485
throw new ClientException("Failed to close response", e);
9586
}
9687
}
97-
98-
if (client !=null) {
99-
try {
100-
client.close();
101-
} catch (Exception e) {
102-
throw new ClientException("Failed to close client", e);
103-
}
104-
}
10588
}
10689

10790
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)