Skip to content

Commit

Permalink
Merge pull request #1701 from ClickHouse/add-query-id-v2
Browse files Browse the repository at this point in the history
Adding query_id to V2 client
  • Loading branch information
chernser authored Jun 25, 2024
2 parents 6539f9c + 9873a5d commit 1b0a40e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,12 @@ public long getServerTime() {
public long getResultRows() {
return operationMetrics.getMetric(ServerMetrics.RESULT_ROWS).getLong();
}

/**
* Alias for {@link OperationMetrics#getQueryId()}
* @return number of returned bytes
*/
public String getQueryId() {
return operationMetrics.getQueryId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public interface Metric {
* @return value of the metric as a long
*/
long getLong();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
public class OperationMetrics {

public Map<String, Metric> metrics = new HashMap<>();
private String queryId;

private final ClientStatisticsHolder clientStatistics;

Expand All @@ -31,6 +32,10 @@ public Metric getMetric(ClientMetrics metric) {
return metrics.get(metric.getKey());
}

public String getQueryId() {
return queryId;
}

public void operationComplete(ClickHouseResponseSummary serverStats) {
for (Map.Entry<String, StopWatch> sw : clientStatistics.getStopWatches().entrySet()) {
sw.getValue().stop();
Expand All @@ -43,11 +48,13 @@ public void operationComplete(ClickHouseResponseSummary serverStats) {
metrics.put(ServerMetrics.NUM_BYTES_WRITTEN.getKey(), new Gauge(serverStats.getWrittenBytes()));
metrics.put(ServerMetrics.RESULT_ROWS.getKey(), new Gauge(serverStats.getResultRows()));
metrics.put(ServerMetrics.ELAPSED_TIME.getKey(), new Gauge(serverStats.getElapsedTime()));
this.queryId = serverStats.getQueryId();
}

@Override
public String toString() {
return "OperationStatistics{" +
"\"queryId\"=\"" + queryId + "\", " +
"\"metrics\"=" + metrics +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,12 @@ public long getServerTime() {
public long getResultRows() {
return operationMetrics.getMetric(ServerMetrics.RESULT_ROWS).getLong();
}

/**
* Alias for {@link OperationMetrics#getQueryId()}
* @return query id of the request
*/
public String getQueryId() {
return operationMetrics.getQueryId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,20 @@ private void createTable(String tableQuery) throws ClickHouseException {
}
}

private void dropTable(String tableName) throws ClickHouseException {
try (ClickHouseClient client = ClickHouseClient.builder().config(new ClickHouseConfig())
.nodeSelector(ClickHouseNodeSelector.of(ClickHouseProtocol.HTTP))
.build()) {
String tableQuery = "DROP TABLE IF EXISTS " + tableName;
client.read(getServer(ClickHouseProtocol.HTTP)).query(tableQuery).executeAndWait().close();
}
}

@Test(groups = { "integration" }, enabled = true)
public void insertSimplePOJOs() throws Exception {
String tableName = "simple_pojo_table";
String createSQL = SamplePOJO.generateTableCreateSQL(tableName);
String uuid = UUID.randomUUID().toString();
System.out.println(createSQL);
createTable(createSQL);
client.register(SamplePOJO.class, client.getTableSchema(tableName, "default"));
Expand All @@ -65,12 +75,16 @@ public void insertSimplePOJOs() throws Exception {
for (int i = 0; i < 1000; i++) {
simplePOJOs.add(new SamplePOJO());
}
settings.setQueryId(uuid);
InsertResponse response = client.insert(tableName, simplePOJOs, settings).get(30, TimeUnit.SECONDS);

OperationMetrics metrics = response.getMetrics();
assertEquals(simplePOJOs.size(), metrics.getMetric(ServerMetrics.NUM_ROWS_WRITTEN).getLong());
assertEquals(simplePOJOs.size(), response.getWrittenRows());
assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0);
assertTrue(metrics.getMetric(ClientMetrics.OP_SERIALIZATION).getLong() > 0);
assertEquals(metrics.getQueryId(), uuid);
assertEquals(response.getQueryId(), uuid);
dropTable(tableName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,9 @@ public void testDataTypes(List<String> columns, List<Supplier<String>> valueGene
public void testQueryMetrics() throws Exception {
prepareDataSet(DATASET_TABLE, DATASET_COLUMNS, DATASET_VALUE_GENERATORS, 10);

String uuid = UUID.randomUUID().toString();
QuerySettings settings = new QuerySettings()
.setFormat(ClickHouseFormat.TabSeparated);
.setFormat(ClickHouseFormat.TabSeparated).setQueryId(uuid);

QueryResponse response = client.query("SELECT * FROM " + DATASET_TABLE + " LIMIT 3", settings).get();

Expand Down Expand Up @@ -947,6 +948,8 @@ public void testQueryMetrics() throws Exception {
Assert.assertEquals(metrics.getMetric(ServerMetrics.RESULT_ROWS).getLong(), rowsToInsert);
Assert.assertEquals(response.getReadRows(), rowsToInsert);
Assert.assertTrue(metrics.getMetric(ClientMetrics.OP_DURATION).getLong() > 0);
Assert.assertEquals(metrics.getQueryId(), uuid);
Assert.assertEquals(response.getQueryId(), uuid);

}

Expand Down

0 comments on commit 1b0a40e

Please sign in to comment.