Skip to content

Commit aa7b910

Browse files
authored
Merge pull request #2497 from 5cmc/main
Ensure proper closure of `httpResponse` in error scenarios during retries
2 parents 539fd94 + 115bd12 commit aa7b910

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.io.InputStream;
5656
import java.io.OutputStream;
5757
import java.lang.reflect.InvocationTargetException;
58+
import java.net.MalformedURLException;
5859
import java.net.URL;
5960
import java.nio.charset.StandardCharsets;
6061
import java.time.Duration;
@@ -121,7 +122,7 @@ public class Client implements AutoCloseable {
121122
private final Map<String, Object> configuration;
122123

123124
private final Map<String, String> readOnlyConfig;
124-
125+
125126
private final POJOSerDe pojoSerDe;
126127

127128
private final ExecutorService sharedOperationExecutor;
@@ -291,7 +292,7 @@ public Builder() {
291292
*/
292293
public Builder addEndpoint(String endpoint) {
293294
try {
294-
URL endpointURL = new java.net.URL(endpoint);
295+
URL endpointURL = new URL(endpoint);
295296

296297
if (endpointURL.getProtocol().equalsIgnoreCase("https")) {
297298
addEndpoint(Protocol.HTTP, endpointURL.getHost(), endpointURL.getPort(), true);
@@ -300,7 +301,7 @@ public Builder addEndpoint(String endpoint) {
300301
} else {
301302
throw new IllegalArgumentException("Only HTTP and HTTPS protocols are supported");
302303
}
303-
} catch (java.net.MalformedURLException e) {
304+
} catch (MalformedURLException e) {
304305
throw new IllegalArgumentException("Endpoint should be a valid URL string, but was " + endpoint, e);
305306
}
306307
return this;
@@ -380,7 +381,7 @@ public Builder setAccessToken(String accessToken) {
380381

381382
/**
382383
* Makes client to use SSL Client Certificate to authenticate with server.
383-
* Client certificate should be set as well. {@link Client.Builder#setClientCertificate(String)}
384+
* Client certificate should be set as well. {@link Builder#setClientCertificate(String)}
384385
* @param useSSLAuthentication
385386
* @return
386387
*/
@@ -1583,8 +1584,9 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
15831584
Endpoint selectedEndpoint = getNextAliveNode();
15841585
RuntimeException lastException = null;
15851586
for (int i = 0; i <= retries; i++) {
1587+
ClassicHttpResponse httpResponse = null;
15861588
try {
1587-
ClassicHttpResponse httpResponse =
1589+
httpResponse =
15881590
httpClientHelper.executeRequest(selectedEndpoint, finalSettings.getAllSettings(), lz4Factory, output -> {
15891591
output.write(sqlQuery.getBytes(StandardCharsets.UTF_8));
15901592
output.close();
@@ -1614,6 +1616,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
16141616
return new QueryResponse(httpResponse, responseFormat, finalSettings, metrics);
16151617

16161618
} catch (Exception e) {
1619+
httpClientHelper.closeQuietly(httpResponse);
16171620
lastException = httpClientHelper.wrapException(String.format("Query request failed (Attempt: %s/%s - Duration: %s)",
16181621
(i + 1), (retries + 1), System.nanoTime() - startTime), e);
16191622
if (httpClientHelper.shouldRetry(e, finalSettings.getAllSettings())) {

client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,9 @@ public ClassicHttpResponse executeRequest(Endpoint server, Map<String, Object> r
428428

429429
HttpClientContext context = HttpClientContext.create();
430430

431+
ClassicHttpResponse httpResponse = null;
431432
try {
432-
ClassicHttpResponse httpResponse = httpClient.executeOpen(null, req, context);
433+
httpResponse = httpClient.executeOpen(null, req, context);
433434
boolean serverCompression = ClientConfigProperties.COMPRESS_SERVER_RESPONSE.getOrDefault(requestConfig);
434435
httpResponse.setEntity(wrapResponseEntity(httpResponse.getEntity(), httpResponse.getCode(), serverCompression, useHttpCompression, lz4Factory, requestConfig));
435436

@@ -448,14 +449,26 @@ public ClassicHttpResponse executeRequest(Endpoint server, Map<String, Object> r
448449
return httpResponse;
449450

450451
} catch (UnknownHostException e) {
452+
closeQuietly(httpResponse);
451453
LOG.warn("Host '{}' unknown", server.getBaseURL());
452454
throw e;
453455
} catch (ConnectException | NoRouteToHostException e) {
456+
closeQuietly(httpResponse);
454457
LOG.warn("Failed to connect to '{}': {}", server.getBaseURL(), e.getMessage());
455458
throw e;
456459
}
457460
}
458461

462+
public void closeQuietly(ClassicHttpResponse httpResponse) {
463+
if (httpResponse != null) {
464+
try {
465+
httpResponse.close();
466+
} catch (IOException e) {
467+
LOG.warn("Failed to close response");
468+
}
469+
}
470+
}
471+
459472
private static final ContentType CONTENT_TYPE = ContentType.create(ContentType.TEXT_PLAIN.getMimeType(), "UTF-8");
460473

461474
private void addHeaders(HttpPost req, Map<String, Object> requestConfig) {

0 commit comments

Comments
 (0)