Skip to content

Commit fcdf02d

Browse files
committed
Resolved changes between req cancellation and retries
1 parent e1ee917 commit fcdf02d

4 files changed

Lines changed: 30 additions & 14 deletions

File tree

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,11 +1688,15 @@ public CompletableFuture<InsertResponse> insert(String tableName,
16881688
} catch (Exception e) {
16891689
String msg = requestExMsg("Insert", (i + 1), durationSince(startTime).toMillis(), requestSettings.getQueryId());
16901690
lastException = httpClientHelper.wrapException(msg, e, requestSettings.getQueryId());
1691-
if (i < maxAttempts) {
1692-
LOG.warn("Retrying.", e);
1693-
selectedEndpoint = nodeSelector.getNextAliveNode(selectedEndpoint);
1691+
if (httpClientHelper.shouldRetry(e, requestSettings.getAllSettings())) {
1692+
if (i < maxAttempts) {
1693+
LOG.warn("Retrying.", e);
1694+
selectedEndpoint = nodeSelector.getNextAliveNode(selectedEndpoint);
1695+
} else {
1696+
nodeSelector.getNextAliveNode(selectedEndpoint);
1697+
}
16941698
} else {
1695-
nodeSelector.getNextAliveNode(selectedEndpoint);
1699+
throw lastException;
16961700
}
16971701
} finally {
16981702
// Insert completes once the request returns; the response exposes no stream to read afterwards,

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,6 @@ public TransportResponse executeRequest(TransportRequest transportRequest) throw
716716
throw new ClientMisconfigurationException("Proxy authentication required. Please check your proxy settings.");
717717
case HttpStatus.SC_BAD_GATEWAY:
718718
throw new ConnectException("Server returned '502 Bad gateway'. Check network and proxy settings.");
719-
} else if (httpResponse.getCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
720-
httpResponse.close();
721-
throw new ConnectException("Server returned '503 Service unavailable'.");
722719
case HttpStatus.SC_SERVICE_UNAVAILABLE:
723720
throw new ConnectException("Server returned '503 Service Unavailable'. Check network settings.");
724721
case HttpStatus.SC_BAD_REQUEST:

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
import com.clickhouse.client.api.metadata.TableSchema;
77
import com.clickhouse.client.api.serde.DataSerializationException;
88
import com.clickhouse.client.api.transport.Endpoint;
9+
import com.clickhouse.client.api.transport.internal.TransportRequest;
10+
import com.clickhouse.client.api.transport.internal.TransportResponse;
911
import com.clickhouse.data.ClickHouseColumn;
1012
import net.jpountz.lz4.LZ4Factory;
11-
import org.apache.hc.core5.http.ClassicHttpResponse;
1213
import org.apache.hc.core5.io.IOCallback;
1314
import org.mockito.Mockito;
1415
import org.testng.Assert;
@@ -20,6 +21,7 @@
2021
import java.lang.reflect.Field;
2122
import java.net.SocketException;
2223
import java.util.Collections;
24+
import java.util.Map;
2325

2426
public class InsertExceptionClassificationTest {
2527

@@ -96,17 +98,27 @@ private static String repeat(char ch, int count) {
9698

9799
private static final class CallbackHttpClientHelper extends HttpAPIClientHelper {
98100
private final OutputStream outputStream;
99-
101+
private IOCallback<OutputStream> writeCallback;
100102
private CallbackHttpClientHelper(OutputStream outputStream) {
101103
super(Collections.emptyMap(), null, false, LZ4Factory.fastestJavaInstance());
102104
this.outputStream = outputStream;
103105
}
104106

105107
@Override
106-
public ClassicHttpResponse executeRequest(Endpoint server, java.util.Map<String, Object> requestConfig,
107-
IOCallback<OutputStream> writeCallback) throws Exception {
108+
public TransportRequest createRequest(Endpoint server, Map<String, Object> requestConfig, String body) {
109+
return null;
110+
}
111+
112+
@Override
113+
public TransportRequest createRequest(Endpoint server, Map<String, Object> requestConfig, IOCallback<OutputStream> writeCallback) {
114+
this.writeCallback = writeCallback;
115+
return null;
116+
}
117+
118+
@Override
119+
public TransportResponse executeRequest(TransportRequest request) throws Exception {
108120
writeCallback.execute(outputStream);
109-
return Mockito.mock(ClassicHttpResponse.class);
121+
return Mockito.mock(TransportResponse.class);
110122
}
111123

112124
@Override

client-v2/src/test/java/com/clickhouse/client/api/internal/HttpAPIClientHelperTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.clickhouse.client.api.internal.HttpAPIClientHelper.CustomSSLConnectionFactory;
66
import com.clickhouse.client.api.transport.Endpoint;
77
import com.clickhouse.client.api.transport.HttpEndpoint;
8+
import com.clickhouse.client.api.transport.internal.TransportRequest;
89
import net.jpountz.lz4.LZ4Factory;
910
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
1011
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
@@ -247,7 +248,8 @@ public void testExecuteRequestThrowsConnectExceptionOn502() throws Exception {
247248

248249
Endpoint endpoint = new HttpEndpoint("localhost", 8123, false, "/");
249250
try {
250-
helper.executeRequest(endpoint, new HashMap<>(), "SELECT 1");
251+
TransportRequest req = helper.createRequest(endpoint, new HashMap<>(), "SELECT 1");
252+
helper.executeRequest(req).close();
251253
Assert.fail("Expected ConnectException to be thrown");
252254
} catch (ConnectException e) {
253255
// expected
@@ -275,7 +277,8 @@ public void testExecuteRequestThrowsConnectExceptionOn503() throws Exception {
275277

276278
Endpoint endpoint = new HttpEndpoint("localhost", 8123, false, "/");
277279
try {
278-
helper.executeRequest(endpoint, new HashMap<>(), "SELECT 1");
280+
TransportRequest req = helper.createRequest(endpoint, new HashMap<>(), "SELECT 1");
281+
helper.executeRequest(req).close();
279282
Assert.fail("Expected ConnectException to be thrown");
280283
} catch (ConnectException e) {
281284
// expected

0 commit comments

Comments
 (0)