]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-22269 fix BulkIndexer profiler being stopped multiple times when log level...
authorMatteo Mara <matteo.mara@sonarsource.com>
Thu, 23 May 2024 15:56:11 +0000 (17:56 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 19 Jun 2024 20:02:43 +0000 (20:02 +0000)
server/sonar-server-common/src/main/java/org/sonar/server/es/BulkIndexer.java

index db10fae84506cbc4c34e095dcd40d545b6a6559e..dfdb12d307048937c9fc97f1ee8b5d9e2417b674 100644 (file)
@@ -28,6 +28,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 import javax.annotation.Nullable;
 import org.elasticsearch.action.DocWriteRequest;
@@ -213,11 +214,14 @@ public class BulkIndexer {
   }
 
   private final class BulkProcessorListener implements Listener {
-    private final Profiler profiler = Profiler.createIfTrace(EsClient.LOGGER);
+    // a map containing per each request the associated profiler
+    private final Map<BulkRequest, Profiler> profilerByRequest = new ConcurrentHashMap<>();
 
     @Override
     public void beforeBulk(long executionId, BulkRequest request) {
+      final Profiler profiler = Profiler.createIfTrace(EsClient.LOGGER);
       profiler.start();
+      profilerByRequest.put(request, profiler);
     }
 
     @Override
@@ -242,9 +246,11 @@ public class BulkIndexer {
     }
 
     private void stopProfiler(BulkRequest request) {
-      if (profiler.isTraceEnabled()) {
+      final Profiler profiler = profilerByRequest.get(request);
+      if (Objects.nonNull(profiler) && profiler.isTraceEnabled()) {
         profiler.stopTrace(toString(request));
       }
+      profilerByRequest.remove(request);
     }
 
     private String toString(BulkRequest bulkRequest) {