@@ -143,6 +143,7 @@ public class Client implements AutoCloseable {
143143 // Server context
144144 private String serverVersion ;
145145 private Object metricsRegistry ;
146+ private int retries ;
146147
147148 private Client (Set <String > endpoints , Map <String ,String > configuration , boolean useNewImplementation ,
148149 ExecutorService sharedOperationExecutor , ColumnToMethodMatchingStrategy columnToMethodMatchingStrategy ) {
@@ -172,6 +173,9 @@ private Client(Set<String> endpoints, Map<String,String> configuration, boolean
172173 boolean initSslContext = getEndpoints ().stream ().anyMatch (s -> s .toLowerCase ().contains ("https://" ));
173174 this .httpClientHelper = new HttpAPIClientHelper (configuration , metricsRegistry , initSslContext );
174175 this .columnToMethodMatchingStrategy = columnToMethodMatchingStrategy ;
176+
177+ String retry = configuration .get (ClientConfigProperties .RETRY_ON_FAILURE .getKey ());
178+ this .retries = retry == null ? 0 : Integer .parseInt (retry );
175179 }
176180
177181 /**
@@ -1471,8 +1475,6 @@ public CompletableFuture<InsertResponse> insert(String tableName,
14711475 Supplier <InsertResponse > responseSupplier ;
14721476
14731477
1474- String retry = configuration .get (ClientConfigProperties .RETRY_ON_FAILURE .getKey ());
1475- final int maxRetries = retry == null ? 0 : Integer .parseInt (retry );
14761478 final int writeBufferSize = settings .getInputStreamCopyBufferSize () <= 0 ?
14771479 Integer .parseInt (configuration .getOrDefault (ClientConfigProperties .CLIENT_NETWORK_BUFFER_SIZE .getKey (), "8192" )) :
14781480 settings .getInputStreamCopyBufferSize ();
@@ -1491,7 +1493,7 @@ public CompletableFuture<InsertResponse> insert(String tableName,
14911493 ClickHouseNode selectedNode = getNextAliveNode ();
14921494
14931495 RuntimeException lastException = null ;
1494- for (int i = 0 ; i <= maxRetries ; i ++) {
1496+ for (int i = 0 ; i <= retries ; i ++) {
14951497 // Execute request
14961498 try (ClassicHttpResponse httpResponse =
14971499 httpClientHelper .executeRequest (selectedNode , finalSettings .getAllSettings (),
@@ -1517,7 +1519,7 @@ public CompletableFuture<InsertResponse> insert(String tableName,
15171519 return new InsertResponse (metrics );
15181520 } catch (Exception e ) {
15191521 lastException = httpClientHelper .wrapException (String .format ("Insert failed (Attempt: %s/%s - Duration: %s)" ,
1520- (i + 1 ), (maxRetries + 1 ), System .nanoTime () - startTime ), e );
1522+ (i + 1 ), (retries + 1 ), System .nanoTime () - startTime ), e );
15211523 if (httpClientHelper .shouldRetry (e , finalSettings .getAllSettings ())) {
15221524 LOG .warn ("Retrying." , e );
15231525 selectedNode = getNextAliveNode ();
@@ -1526,15 +1528,15 @@ public CompletableFuture<InsertResponse> insert(String tableName,
15261528 }
15271529 }
15281530
1529- if (i < maxRetries ) {
1531+ if (i < retries ) {
15301532 try {
15311533 writer .onRetry ();
15321534 } catch (IOException ioe ) {
15331535 throw new ClientException ("Failed to reset stream before next attempt" , ioe );
15341536 }
15351537 }
15361538 }
1537- throw new ClientException ("Insert request failed after attempts: " + (maxRetries + 1 ) + " - Duration: " + (System .nanoTime () - startTime ), lastException );
1539+ throw new ClientException ("Insert request failed after attempts: " + (retries + 1 ) + " - Duration: " + (System .nanoTime () - startTime ), lastException );
15381540 };
15391541
15401542 return runAsyncOperation (responseSupplier , settings .getAllSettings ());
@@ -1605,9 +1607,6 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
16051607
16061608 Supplier <QueryResponse > responseSupplier ;
16071609
1608- String retry = configuration .get (ClientConfigProperties .RETRY_ON_FAILURE .getKey ());
1609- final int maxRetries = retry == null ? 0 : Integer .parseInt (retry );
1610-
16111610 if (queryParams != null ) {
16121611 settings .setOption ("statement_params" , queryParams );
16131612 }
@@ -1617,7 +1616,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
16171616 // Selecting some node
16181617 ClickHouseNode selectedNode = getNextAliveNode ();
16191618 RuntimeException lastException = null ;
1620- for (int i = 0 ; i <= maxRetries ; i ++) {
1619+ for (int i = 0 ; i <= retries ; i ++) {
16211620 try {
16221621 ClassicHttpResponse httpResponse =
16231622 httpClientHelper .executeRequest (selectedNode , finalSettings .getAllSettings (), output -> {
@@ -1650,7 +1649,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
16501649
16511650 } catch (Exception e ) {
16521651 lastException = httpClientHelper .wrapException (String .format ("Query request failed (Attempt: %s/%s - Duration: %s)" ,
1653- (i + 1 ), (maxRetries + 1 ), System .nanoTime () - startTime ), e );
1652+ (i + 1 ), (retries + 1 ), System .nanoTime () - startTime ), e );
16541653 if (httpClientHelper .shouldRetry (e , finalSettings .getAllSettings ())) {
16551654 LOG .warn ("Retrying." , e );
16561655 selectedNode = getNextAliveNode ();
@@ -1660,7 +1659,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
16601659 }
16611660 }
16621661
1663- throw new ClientException ("Query request failed after attempts: " + (maxRetries + 1 ) + " - Duration: " + (System .nanoTime () - startTime ), lastException );
1662+ throw new ClientException ("Query request failed after attempts: " + (retries + 1 ) + " - Duration: " + (System .nanoTime () - startTime ), lastException );
16641663 };
16651664
16661665 return runAsyncOperation (responseSupplier , settings .getAllSettings ());
0 commit comments