@@ -1083,7 +1083,7 @@ public CompletableFuture<InsertResponse> insert(String tableName, List<?> data,
10831083 }
10841084
10851085
1086- String operationId = startOperation ();
1086+ String operationId = registerOperationMetrics ();
10871087 settings .setOperationId (operationId );
10881088 if (useNewImplementation ) {
10891089 globalClientStats .get (operationId ).start (ClientMetrics .OP_DURATION );
@@ -1225,13 +1225,18 @@ public CompletableFuture<InsertResponse> insert(String tableName,
12251225 InputStream data ,
12261226 ClickHouseFormat format ,
12271227 InsertSettings settings ) {
1228+
12281229 String operationId = (String ) settings .getOperationId ();
1229- if (operationId == null ) {
1230- operationId = startOperation ();
1231- settings .setOperationId (operationId );
1230+ ClientStatisticsHolder clientStats = null ;
1231+ if (operationId != null ) {
1232+ clientStats = globalClientStats .remove (operationId );
1233+ }
1234+
1235+ if (clientStats == null ) {
1236+ clientStats = new ClientStatisticsHolder ();
12321237 }
1233- ClientStatisticsHolder clientStats = globalClientStats .remove (operationId );
12341238 clientStats .start (ClientMetrics .OP_DURATION );
1239+ final ClientStatisticsHolder finalClientStats = clientStats ;
12351240
12361241 Supplier <InsertResponse > responseSupplier ;
12371242 if (useNewImplementation ) {
@@ -1280,7 +1285,7 @@ public CompletableFuture<InsertResponse> insert(String tableName,
12801285 continue ;
12811286 }
12821287
1283- OperationMetrics metrics = new OperationMetrics (clientStats );
1288+ OperationMetrics metrics = new OperationMetrics (finalClientStats );
12841289 String summary = HttpAPIClientHelper .getHeaderVal (httpResponse .getFirstHeader (ClickHouseHttpProto .HEADER_SRV_SUMMARY ), "{}" );
12851290 ProcessParser .parseSummary (summary , metrics );
12861291 String queryId = HttpAPIClientHelper .getHeaderVal (httpResponse .getFirstHeader (ClickHouseHttpProto .HEADER_QUERY_ID ), finalSettings .getQueryId (), String ::valueOf );
@@ -1333,7 +1338,7 @@ public CompletableFuture<InsertResponse> insert(String tableName,
13331338 } else {
13341339 clickHouseResponse = future .get ();
13351340 }
1336- InsertResponse response = new InsertResponse (clickHouseResponse , clientStats );
1341+ InsertResponse response = new InsertResponse (clickHouseResponse , finalClientStats );
13371342 return response ;
13381343 } catch (ExecutionException e ) {
13391344 throw new ClientException ("Failed to get insert response" , e .getCause ());
@@ -1762,7 +1767,7 @@ public ClickHouseBinaryFormatReader newBinaryFormatReader(QueryResponse response
17621767 return newBinaryFormatReader (response , null );
17631768 }
17641769
1765- private String startOperation () {
1770+ private String registerOperationMetrics () {
17661771 String operationId = UUID .randomUUID ().toString ();
17671772 globalClientStats .put (operationId , new ClientStatisticsHolder ());
17681773 return operationId ;
0 commit comments