Skip to content

Commit 1ec1c9a

Browse files
committed
added check for the option to enable retry
1 parent 372322a commit 1ec1c9a

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

clickhouse-http-client/src/main/java/com/clickhouse/client/http/ApacheHttpConnectionImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,14 @@ protected ClickHouseHttpResponse post(ClickHouseConfig config, String sql, Click
251251
post.setEntity(postBody);
252252
CloseableHttpResponse response = null;
253253

254-
for (int attempt = 0; attempt < 2; attempt++) {
255-
log.debug("HTTP request attempt {}", attempt);
254+
int retryAttempts = config.getBoolOption(ClickHouseHttpOption.AHC_RETRY_ON_FAILURE) ? 2 : 1;
255+
for (int attempt = 0; attempt < retryAttempts; attempt++) {
256+
log.debug("HTTP request attempt " + attempt);
256257
try {
257258
response = client.execute(post);
258259
break;
259260
} catch (NoHttpResponseException e) {
260-
log.warn("HTTP request failed: ", e.getMessage());
261-
if (attempt > 0) {
261+
if ((retryAttempts - attempt - 1) == 0) {
262262
throw new ConnectException(e.getMessage());
263263
} else {
264264
continue;

clickhouse-http-client/src/main/java/com/clickhouse/client/http/config/ClickHouseHttpOption.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,7 @@ public enum ClickHouseHttpOption implements ClickHouseOption {
8080
* <li>{@code 503 Service Unavailable}</li>
8181
* </ul>
8282
*/
83-
AHC_RETRY_ON_FAILURE("ahc_retry_on_failure", true, "Whether to retry on failure with AsyncHttpClient."),
84-
85-
/**
86-
* Retry interval in milliseconds for AsyncHttpClient (if {@link #AHC_RETRY_ON_FAILURE} is enabled).
87-
*/
88-
AHC_RETRY_INTERVAL("ahc_retry_interval", 100, "Retry interval in milliseconds."),
83+
AHC_RETRY_ON_FAILURE("ahc_retry_on_failure", true, "Whether to retry on failure with AsyncHttpClient.")
8984
;
9085

9186
private final String key;

clickhouse-http-client/src/test/java/com/clickhouse/client/http/ApacheHttpConnectionImplTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,20 @@ public void testFailureWhileRequest() {
133133
.willReturn(WireMock.aResponse().withFault(Fault.EMPTY_RESPONSE)).build());
134134

135135
ClickHouseHttpClient httpClient = new ClickHouseHttpClient();
136-
ClickHouseConfig config = new ClickHouseConfig();
136+
Map<ClickHouseOption, Serializable> options = new HashMap<>();
137+
options.put(ClickHouseHttpOption.AHC_RETRY_ON_FAILURE, false);
138+
ClickHouseConfig config = new ClickHouseConfig(options);
137139
httpClient.init(config);
138140
ClickHouseRequest request = httpClient.read("http://localhost:9090/").query("SELECT 1");
139141

140142
try {
141143
httpClient.executeAndWait(request);
142144
} catch (ClickHouseException e) {
143145
Assert.assertEquals(e.getErrorCode(), ClickHouseException.ERROR_NETWORK);
146+
return;
144147
}
148+
149+
Assert.fail("Should throw exception");
145150
} finally {
146151
faultyServer.stop();
147152
}
@@ -153,12 +158,14 @@ public void testRetryOnFailure() {
153158
faultyServer.start();
154159
try {
155160
faultyServer.addStubMapping(WireMock.post(WireMock.anyUrl())
161+
.withRequestBody(WireMock.equalTo("SELECT 1"))
156162
.inScenario("Retry")
157163
.whenScenarioStateIs(Scenario.STARTED)
158164
.willReturn(WireMock.aResponse().withFault(Fault.EMPTY_RESPONSE))
159165
.willSetStateTo("Failed")
160166
.build());
161167
faultyServer.addStubMapping(WireMock.post(WireMock.anyUrl())
168+
.withRequestBody(WireMock.equalTo("SELECT 1"))
162169
.inScenario("Retry")
163170
.whenScenarioStateIs("Failed")
164171
.willReturn(WireMock.aResponse()

0 commit comments

Comments
 (0)