From 8e711cf82bfb6c541012d0fec8ddcd3224b3e218 Mon Sep 17 00:00:00 2001 From: Kushagra Pratap Singh Date: Sat, 28 Nov 2026 14:36:35 +0530 Subject: [PATCH] [client-v2] Measure only the row writing in OP_SERIALIZATION --- .../java/com/clickhouse/client/api/Client.java | 5 ++++- .../client/api/internal/StopWatch.java | 13 ++++++++++++- .../observability/MetricsRecorderUnitTest.java | 18 ++++++++++++++++++ history/latest/3198.md | 4 ++++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 history/latest/3198.md diff --git a/client-v2/src/main/java/com/clickhouse/client/api/Client.java b/client-v2/src/main/java/com/clickhouse/client/api/Client.java index f7dc49519e..6add6ff126 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/Client.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/Client.java @@ -1585,7 +1585,7 @@ public CompletableFuture insert(String tableName, List data, // the duration reported for a successful operation, so that both outcomes measure the same work. final long operationStartNanos = System.nanoTime(); globalClientStats.get(operationId).start(ClientMetrics.OP_DURATION); - globalClientStats.get(operationId).start(ClientMetrics.OP_SERIALIZATION); + final ClientStatisticsHolder insertStats = globalClientStats.get(operationId); boolean hasDefaults = this.tableSchemaHasDefaults.get(tableName); @@ -1632,6 +1632,8 @@ public CompletableFuture insert(String tableName, List data, // Execute request TransportRequest transportRequest = httpClientHelper.createRequest(selectedEndpoint, requestSettings.getAllSettings(), out -> { + // each attempt writes the body again, so the metric is that of the last one + insertStats.start(ClientMetrics.OP_SERIALIZATION); out.write("INSERT INTO ".getBytes()); out.write(tableName.getBytes()); out.write(" \n FORMAT ".getBytes()); @@ -1648,6 +1650,7 @@ public CompletableFuture insert(String tableName, List data, } } out.close(); + insertStats.stop(ClientMetrics.OP_SERIALIZATION).freeze(); }); registerTransportReq(queryId, transportRequest); diff --git a/client-v2/src/main/java/com/clickhouse/client/api/internal/StopWatch.java b/client-v2/src/main/java/com/clickhouse/client/api/internal/StopWatch.java index 0772861418..8d8ce13bb3 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/internal/StopWatch.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/internal/StopWatch.java @@ -8,6 +8,7 @@ public class StopWatch implements Metric { long elapsedNanoTime = 0; long startNanoTime; + private boolean frozen; public StopWatch() { // do nothing @@ -19,10 +20,20 @@ public StopWatch(long startNanoTime) { public void start() { startNanoTime = System.nanoTime(); + frozen = false; } public void stop() { - elapsedNanoTime = System.nanoTime() - startNanoTime; + if (!frozen) { + elapsedNanoTime = System.nanoTime() - startNanoTime; + } + } + + /** + * Keeps the current measurement: later {@link #stop()} calls are ignored until the next {@link #start()}. + */ + public void freeze() { + frozen = true; } /** diff --git a/client-v2/src/test/java/com/clickhouse/client/api/observability/MetricsRecorderUnitTest.java b/client-v2/src/test/java/com/clickhouse/client/api/observability/MetricsRecorderUnitTest.java index 34ff471632..6dcab9d13c 100644 --- a/client-v2/src/test/java/com/clickhouse/client/api/observability/MetricsRecorderUnitTest.java +++ b/client-v2/src/test/java/com/clickhouse/client/api/observability/MetricsRecorderUnitTest.java @@ -96,6 +96,24 @@ public void testInsertReportsTableAndSerializationDuration() throws Exception { Assert.assertEquals(serialization.getAttribute(MetricAttribute.DB_COLLECTION_NAME), "target_table"); } + @Test + public void testInsertSerializationDurationExcludesWaitingForTheServer() throws Exception { + mockServer.stubFor(WireMock.post(WireMock.anyUrl()) + .willReturn(WireMock.aResponse().withStatus(200).withFixedDelay(2000))); + + try (Client client = newClientBuilder().addEndpoint(mockEndpoint()).build()) { + client.register(ValuePojo.class, new TableSchema("target_table", null, "", + Collections.singletonList(ClickHouseColumn.of("value", "String")))); + client.insert("target_table", Collections.singletonList(new ValuePojo("a"))).get(10, TimeUnit.SECONDS) + .close(); + } + + double duration = recorder.getOnlyMetric(MetricName.OPERATION_DURATION).getValue(); + double serialization = recorder.getOnlyMetric(MetricName.OPERATION_SERIALIZATION_DURATION).getValue(); + Assert.assertTrue(duration >= 2, "the server delay is part of the operation: " + duration); + Assert.assertTrue(serialization < 1, "the server delay is not serialization: " + serialization); + } + @Test public void testQueryWithoutSerializationStepReportsNoSerializationDuration() throws Exception { // contrast case: the client measures the serialization step of an insert only diff --git a/history/latest/3198.md b/history/latest/3198.md new file mode 100644 index 0000000000..8b8ea627b7 --- /dev/null +++ b/history/latest/3198.md @@ -0,0 +1,4 @@ +- **[client-v2]** `ClientMetrics.OP_SERIALIZATION` of a POJO insert measured the whole operation, including the + network round trip and the server time, so it always matched `OP_DURATION`. It now measures writing the rows into + the request body, for the last attempt when the insert is retried. + (https://github.com/ClickHouse/clickhouse-java/issues/3198)