Skip to content

Commit 0992132

Browse files
authored
Merge pull request #2104 from ClickHouse/adding-network-debugging
Adding some duration timers
2 parents c0d9634 + ad99723 commit 0992132

File tree

1 file changed

+17
-11
lines changed
  • client-v2/src/main/java/com/clickhouse/client/api

1 file changed

+17
-11
lines changed

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

+17-11
Original file line numberDiff line numberDiff line change
@@ -1164,10 +1164,11 @@ public boolean ping() {
11641164
* @return true if the server is alive, false otherwise
11651165
*/
11661166
public boolean ping(long timeout) {
1167+
long startTime = System.nanoTime();
11671168
try (QueryResponse response = query("SELECT 1 FORMAT TabSeparated").get(timeout, TimeUnit.MILLISECONDS)) {
11681169
return true;
11691170
} catch (Exception e) {
1170-
LOG.debug("Failed to connect to the server", e);
1171+
LOG.debug("Failed to connect to the server (Duration: {})", System.nanoTime() - startTime, e);
11711172
return false;
11721173
}
11731174
}
@@ -1284,7 +1285,6 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data)
12841285
* @return {@code CompletableFuture<InsertResponse>} - a promise to insert response
12851286
*/
12861287
public CompletableFuture<InsertResponse> insert(String tableName, List<?> data, InsertSettings settings) {
1287-
12881288
if (data == null || data.isEmpty()) {
12891289
throw new IllegalArgumentException("Data cannot be empty");
12901290
}
@@ -1325,6 +1325,7 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data,
13251325
settings.setOption(ClientConfigProperties.INPUT_OUTPUT_FORMAT.getKey(), format.name());
13261326
final InsertSettings finalSettings = settings;
13271327
Supplier<InsertResponse> supplier = () -> {
1328+
long startTime = System.nanoTime();
13281329
// Selecting some node
13291330
ClickHouseNode selectedNode = getNextAliveNode();
13301331

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

13561357
// Check response
13571358
if (httpResponse.getCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
1358-
LOG.warn("Failed to get response. Server returned {}. Retrying.", httpResponse.getCode());
1359+
LOG.warn("Failed to get response. Server returned {}. Retrying. (Duration: {})", httpResponse.getCode(), System.nanoTime() - startTime);
13591360
selectedNode = getNextAliveNode();
13601361
continue;
13611362
}
@@ -1369,7 +1370,8 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data,
13691370
metrics.setQueryId(queryId);
13701371
return new InsertResponse(metrics);
13711372
} catch (Exception e) {
1372-
lastException = httpClientHelper.wrapException("Query request failed (Attempt " + (i + 1) + "/" + (maxRetries + 1) + ")", e);
1373+
lastException = httpClientHelper.wrapException(String.format("Query request failed (Attempt: %s/%s - Duration: %s)",
1374+
(i + 1), (maxRetries + 1), System.nanoTime() - startTime), e);
13731375
if (httpClientHelper.shouldRetry(e, finalSettings.getAllSettings())) {
13741376
LOG.warn("Retrying.", e);
13751377
selectedNode = getNextAliveNode();
@@ -1378,7 +1380,7 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data,
13781380
}
13791381
}
13801382
}
1381-
throw new ClientException("Insert request failed after attempts: " + (maxRetries + 1), lastException);
1383+
throw new ClientException("Insert request failed after attempts: " + (maxRetries + 1) + " - Duration: " + (System.nanoTime() - startTime), lastException);
13821384
};
13831385

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

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

15001503
// Check response
15011504
if (httpResponse.getCode() == HttpStatus.SC_SERVICE_UNAVAILABLE) {
1502-
LOG.warn("Failed to get response. Server returned {}. Retrying.", httpResponse.getCode());
1505+
LOG.warn("Failed to get response. Server returned {}. Retrying. (Duration: {})", System.nanoTime() - startTime, httpResponse.getCode());
15031506
selectedNode = getNextAliveNode();
15041507
continue;
15051508
}
@@ -1512,7 +1515,8 @@ public CompletableFuture<InsertResponse> insert(String tableName,
15121515
metrics.setQueryId(queryId);
15131516
return new InsertResponse(metrics);
15141517
} catch (Exception e) {
1515-
lastException = httpClientHelper.wrapException("Query request failed (Attempt " + (i + 1) + "/" + (maxRetries + 1) + ")", e);
1518+
lastException = httpClientHelper.wrapException(String.format("Insert failed (Attempt: %s/%s - Duration: %s)",
1519+
(i + 1), (maxRetries + 1), System.nanoTime() - startTime), e);
15161520
if (httpClientHelper.shouldRetry(e, finalSettings.getAllSettings())) {
15171521
LOG.warn("Retrying.", e);
15181522
selectedNode = getNextAliveNode();
@@ -1529,7 +1533,7 @@ public CompletableFuture<InsertResponse> insert(String tableName,
15291533
}
15301534
}
15311535
}
1532-
throw new ClientException("Insert request failed after attempts: " + (maxRetries + 1), lastException);
1536+
throw new ClientException("Insert request failed after attempts: " + (maxRetries + 1) + " - Duration: " + (System.nanoTime() - startTime), lastException);
15331537
};
15341538

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

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

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

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

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

0 commit comments

Comments
 (0)