From 2076f7e75a7af6c059e659607342034afdf1ea08 Mon Sep 17 00:00:00 2001 From: Matteo Mara Date: Thu, 23 May 2024 17:56:11 +0200 Subject: [PATCH] SONAR-22269 fix BulkIndexer profiler being stopped multiple times when log level is set to TRACE --- .../src/main/java/org/sonar/server/es/BulkIndexer.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/BulkIndexer.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/BulkIndexer.java index db10fae8450..dfdb12d3070 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/BulkIndexer.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/es/BulkIndexer.java @@ -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 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) { -- 2.39.5