Skip to content

Commit 6e6b6b7

Browse files
authored
Merge pull request #1914 from ClickHouse/v2_fix_metrics_npe
[client-v2] Added documentation for OperationMetrics#completeOperation().
2 parents ddeee30 + 0e41e23 commit 6e6b6b7

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

client-v2/src/main/java/com/clickhouse/client/api/metrics/OperationMetrics.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public String getQueryId() {
3535
return queryId;
3636
}
3737

38+
/**
39+
* Complete counting metrics on operation and stop all stopwatches.
40+
* Multiple calls may have side effects.
41+
* Note: should not be called by user code, except when created by user code.
42+
*/
3843
public void operationComplete() {
3944
for (Map.Entry<String, StopWatch> sw : clientStatistics.getStopWatches().entrySet()) {
4045
sw.getValue().stop();

client-v2/src/test/java/com/clickhouse/client/query/QueryTests.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,13 +1248,13 @@ public void testQueryMetrics() throws Exception {
12481248
.waitEndOfQuery(true)
12491249
.setQueryId(uuid);
12501250

1251-
QueryResponse response = client.query("SELECT * FROM " + DATASET_TABLE + " LIMIT 3", settings).get();
1251+
try (QueryResponse response = client.query("SELECT * FROM " + DATASET_TABLE + " LIMIT 3", settings).get()) {
1252+
// Stats should be available after the query is done
1253+
OperationMetrics metrics = response.getMetrics();
12521254

1253-
// Stats should be available after the query is done
1254-
OperationMetrics metrics = response.getMetrics();
1255-
1256-
Assert.assertEquals(metrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), 10); // 10 rows in the table
1257-
Assert.assertEquals(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(), 3);
1255+
Assert.assertEquals(metrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), 10); // 10 rows in the table
1256+
Assert.assertEquals(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(), 3);
1257+
}
12581258

12591259
StringBuilder insertStmtBuilder = new StringBuilder();
12601260
insertStmtBuilder.append("INSERT INTO default.").append(DATASET_TABLE).append(" VALUES ");
@@ -1265,17 +1265,26 @@ public void testQueryMetrics() throws Exception {
12651265
insertStmtBuilder.setLength(insertStmtBuilder.length() - 2);
12661266
insertStmtBuilder.append("), ");
12671267
}
1268-
response = client.query(insertStmtBuilder.toString(), settings).get();
1268+
try (QueryResponse response = client.query(insertStmtBuilder.toString(), settings).get()) {
12691269

1270-
metrics = response.getMetrics();
1270+
OperationMetrics metrics = response.getMetrics();
12711271

1272-
Assert.assertEquals(metrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), rowsToInsert); // 10 rows in the table
1273-
Assert.assertEquals(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(), rowsToInsert);
1274-
Assert.assertEquals(response.getReadRows(), rowsToInsert);
1275-
Assert.assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0);
1276-
Assert.assertEquals(metrics.getQueryId(), uuid);
1277-
Assert.assertEquals(response.getQueryId(), uuid);
1272+
Assert.assertEquals(metrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), rowsToInsert); // 10 rows in the table
1273+
Assert.assertEquals(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(), rowsToInsert);
1274+
Assert.assertEquals(response.getReadRows(), rowsToInsert);
1275+
Assert.assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0);
1276+
Assert.assertEquals(metrics.getQueryId(), uuid);
1277+
Assert.assertEquals(response.getQueryId(), uuid);
1278+
}
12781279

1280+
try (QueryResponse response = client.query("SELECT number FROM system.numbers LIMIT 30", settings).get()) {
1281+
// Stats should be available after the query is done
1282+
OperationMetrics metrics = response.getMetrics();
1283+
1284+
Assert.assertEquals(metrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), 30);
1285+
Assert.assertTrue(metrics.getMetric(ServerMetrics.ELAPSED_TIME).getLong() > 0);
1286+
Assert.assertTrue(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong() > 0);
1287+
}
12791288
}
12801289

12811290
private final static List<String> DATASET_COLUMNS = Arrays.asList(

0 commit comments

Comments
 (0)