Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,7 @@ public CompletableFuture<InsertResponse> 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);
Expand Down Expand Up @@ -1632,6 +1632,8 @@ public CompletableFuture<InsertResponse> 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());
Expand All @@ -1648,6 +1650,7 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data,
}
}
out.close();
insertStats.stop(ClientMetrics.OP_SERIALIZATION).freeze();
});

registerTransportReq(queryId, transportRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class StopWatch implements Metric {

long elapsedNanoTime = 0;
long startNanoTime;
private boolean frozen;

public StopWatch() {
// do nothing
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions history/latest/3198.md
Original file line number Diff line number Diff line change
@@ -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)