ClickHouse / clickhouse-java

ClickHouse Java Clients & JDBC Driver
https://clickhouse.com
Apache License 2.0
1.45k stars 537 forks source link

client-V2 com.clickhouse.client.api.metrics.Metric.getLong() #1900

Open lelewolf opened 3 weeks ago

lelewolf commented 3 weeks ago

Describe the bug

After executing an SQL query, attempting to retrieve ServerMetrics.ELAPSED_TIME or ServerMetrics.RESULT_ROWS from OperationMetrics results in a NullPointerException. This error indicates that metrics.getMetric() is returning null, likely because the OperationMetrics instance did not record the relevant metrics correctly.

Steps to reproduce

  1. Configure the ClickHouse client and run the following code.
  2. Call the executeSQL method, passing in an SQL query and queryId.
  3. Observe the logs to see if the error message appears: Cannot invoke "com.clickhouse.client.api.metrics.Metric.getLong()" because the return value of "com.clickhouse.client.api.metrics.OperationMetrics.getMetric(com.clickhouse.client.api.metrics.ServerMetrics)" is null.

Expected behaviour

Code example

public QueryResult executeSQL(String sql, String queryId) throws Exception {
    QuerySettings settings = createQuerySettings(queryId);
    log.info("开始执行查询,queryId:{},sql:{},settings:{}", queryId, sql,
        JSON.toJSONString(settings));
    try {
      QueryResponse response = executeSQLQuery(sql, settings);
      OperationMetrics metrics = response.getMetrics();
      metrics.operationComplete();
      log.info("SQL 查询执行时间: {} 毫秒, 查询查询结果条数: {}", metrics.getMetric(ServerMetrics.ELAPSED_TIME), metrics.getMetric(ServerMetrics.RESULT_ROWS));
      log.info("SQL 查询结果: {}", JSON.toJSONString(metrics));
      return buildQueryResult(jsonResult,metrics);
    } catch (InterruptedException | ExecutionException e) {
      log.error("SQL执行失败: {}", sql, e);
      throw new SQLException("Error executing SQL: " + sql, e);
    }
  }

Error log

Cannot invoke "com.clickhouse.client.api.metrics.Metric.getLong()" because the return value of "com.clickhouse.client.api.metrics.OperationMetrics.getMetric(com.clickhouse.client.api.metrics.ServerMetrics)" is null

Configuration

Environment

ClickHouse server

chernser commented 3 weeks ago

@lelewolf Thank you for reporting! We will look into it.

chernser commented 2 weeks ago

Good day, @lelewolf !

I've not reproduced the issue. Here is my PR with test https://github.com/ClickHouse/clickhouse-java/pull/1914 Few things to mention:

Thanks!