Skip to content

Commit

Permalink
Merge pull request #2104 from ClickHouse/adding-network-debugging
Browse files Browse the repository at this point in the history
Adding some duration timers
Paultagoras authored Jan 22, 2025

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
2 parents c0d9634 + ad99723 commit 0992132
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions client-v2/src/main/java/com/clickhouse/client/api/Client.java
Original file line number Diff line number Diff line change
@@ -1164,10 +1164,11 @@ public boolean ping() {
* @return true if the server is alive, false otherwise
*/
public boolean ping(long timeout) {
long startTime = System.nanoTime();
try (QueryResponse response = query("SELECT 1 FORMAT TabSeparated").get(timeout, TimeUnit.MILLISECONDS)) {
return true;
} catch (Exception e) {
LOG.debug("Failed to connect to the server", e);
LOG.debug("Failed to connect to the server (Duration: {})", System.nanoTime() - startTime, e);
return false;
}
}
@@ -1284,7 +1285,6 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data)
* @return {@code CompletableFuture<InsertResponse>} - a promise to insert response
*/
public CompletableFuture<InsertResponse> insert(String tableName, List<?> data, InsertSettings settings) {

if (data == null || data.isEmpty()) {
throw new IllegalArgumentException("Data cannot be empty");
}
@@ -1325,6 +1325,7 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data,
settings.setOption(ClientConfigProperties.INPUT_OUTPUT_FORMAT.getKey(), format.name());
final InsertSettings finalSettings = settings;
Supplier<InsertResponse> supplier = () -> {
long startTime = System.nanoTime();
// Selecting some node
ClickHouseNode selectedNode = getNextAliveNode();

@@ -1355,7 +1356,7 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data,

// Check response
if (httpResponse.getCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
LOG.warn("Failed to get response. Server returned {}. Retrying.", httpResponse.getCode());
LOG.warn("Failed to get response. Server returned {}. Retrying. (Duration: {})", httpResponse.getCode(), System.nanoTime() - startTime);
selectedNode = getNextAliveNode();
continue;
}
@@ -1369,7 +1370,8 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data,
metrics.setQueryId(queryId);
return new InsertResponse(metrics);
} catch (Exception e) {
lastException = httpClientHelper.wrapException("Query request failed (Attempt " + (i + 1) + "/" + (maxRetries + 1) + ")", e);
lastException = httpClientHelper.wrapException(String.format("Query request failed (Attempt: %s/%s - Duration: %s)",
(i + 1), (maxRetries + 1), System.nanoTime() - startTime), e);
if (httpClientHelper.shouldRetry(e, finalSettings.getAllSettings())) {
LOG.warn("Retrying.", e);
selectedNode = getNextAliveNode();
@@ -1378,7 +1380,7 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data,
}
}
}
throw new ClientException("Insert request failed after attempts: " + (maxRetries + 1), lastException);
throw new ClientException("Insert request failed after attempts: " + (maxRetries + 1) + " - Duration: " + (System.nanoTime() - startTime), lastException);
};

return runAsyncOperation(supplier, settings.getAllSettings());
@@ -1483,6 +1485,7 @@ public CompletableFuture<InsertResponse> insert(String tableName,
final String sqlStmt = "INSERT INTO " + tableName + " FORMAT " + format.name();
finalSettings.serverSetting(ClickHouseHttpProto.QPARAM_QUERY_STMT, sqlStmt);
responseSupplier = () -> {
long startTime = System.nanoTime();
// Selecting some node
ClickHouseNode selectedNode = getNextAliveNode();

@@ -1499,7 +1502,7 @@ public CompletableFuture<InsertResponse> insert(String tableName,

// Check response
if (httpResponse.getCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
LOG.warn("Failed to get response. Server returned {}. Retrying.", httpResponse.getCode());
LOG.warn("Failed to get response. Server returned {}. Retrying. (Duration: {})", System.nanoTime() - startTime, httpResponse.getCode());
selectedNode = getNextAliveNode();
continue;
}
@@ -1512,7 +1515,8 @@ public CompletableFuture<InsertResponse> insert(String tableName,
metrics.setQueryId(queryId);
return new InsertResponse(metrics);
} catch (Exception e) {
lastException = httpClientHelper.wrapException("Query request failed (Attempt " + (i + 1) + "/" + (maxRetries + 1) + ")", e);
lastException = httpClientHelper.wrapException(String.format("Insert failed (Attempt: %s/%s - Duration: %s)",
(i + 1), (maxRetries + 1), System.nanoTime() - startTime), e);
if (httpClientHelper.shouldRetry(e, finalSettings.getAllSettings())) {
LOG.warn("Retrying.", e);
selectedNode = getNextAliveNode();
@@ -1529,7 +1533,7 @@ public CompletableFuture<InsertResponse> insert(String tableName,
}
}
}
throw new ClientException("Insert request failed after attempts: " + (maxRetries + 1), lastException);
throw new ClientException("Insert request failed after attempts: " + (maxRetries + 1) + " - Duration: " + (System.nanoTime() - startTime), lastException);
};

return runAsyncOperation(responseSupplier, settings.getAllSettings());
@@ -1608,6 +1612,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
}
final QuerySettings finalSettings = settings;
responseSupplier = () -> {
long startTime = System.nanoTime();
// Selecting some node
ClickHouseNode selectedNode = getNextAliveNode();
RuntimeException lastException = null;
@@ -1621,7 +1626,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec

// Check response
if (httpResponse.getCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
LOG.warn("Failed to get response. Server returned {}. Retrying.", httpResponse.getCode());
LOG.warn("Failed to get response. Server returned {}. Retrying. (Duration: {})", System.nanoTime() - startTime, httpResponse.getCode());
selectedNode = getNextAliveNode();
continue;
}
@@ -1638,7 +1643,8 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
return new QueryResponse(httpResponse, finalSettings.getFormat(), finalSettings, metrics);

} catch (Exception e) {
lastException = httpClientHelper.wrapException("Query request failed (Attempt " + (i + 1) + "/" + (maxRetries + 1) + ")", e);
lastException = httpClientHelper.wrapException(String.format("Query request failed (Attempt: %s/%s - Duration: %s)",
(i + 1), (maxRetries + 1), System.nanoTime() - startTime), e);
if (httpClientHelper.shouldRetry(e, finalSettings.getAllSettings())) {
LOG.warn("Retrying.", e);
selectedNode = getNextAliveNode();
@@ -1648,7 +1654,7 @@ public CompletableFuture<QueryResponse> query(String sqlQuery, Map<String, Objec
}
}

throw new ClientException("Query request failed after attempts: " + (maxRetries + 1), lastException);
throw new ClientException("Query request failed after attempts: " + (maxRetries + 1) + " - Duration: " + (System.nanoTime() - startTime), lastException);
};

return runAsyncOperation(responseSupplier, settings.getAllSettings());

0 comments on commit 0992132

Please sign in to comment.