From 83447fc550efaf4705be740b868962b08d3b35f2 Mon Sep 17 00:00:00 2001 From: NipunaMadhushan Date: Fri, 21 Jun 2024 02:42:01 +0530 Subject: [PATCH 1/5] Fix concurrent issue in observability --- .../nativeimpl/OpenTracerBallerinaWrapper.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java b/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java index 5293e1d4d4b7..3dd7d3d9e440 100644 --- a/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java +++ b/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java @@ -43,7 +43,7 @@ public class OpenTracerBallerinaWrapper { private static OpenTracerBallerinaWrapper instance = new OpenTracerBallerinaWrapper(); private TracersStore tracerStore; private final boolean enabled; - private Map observerContextList = new HashMap<>(); + private ConcurrentMap observerContextList = new ConcurrentHashMap<>(); private AtomicLong spanId = new AtomicLong(); private static final int SYSTEM_TRACE_INDICATOR = -1; @@ -126,7 +126,10 @@ public boolean finishSpan(Strand strand, long spanId) { if (!enabled) { return false; } - ObserverContext observerContext = observerContextList.get(spanId); + ObserverContext observerContext = null; + if (observerContextList.containsKey(spanId)) { + observerContext = observerContextList.get(spanId); + } if (observerContext != null) { if (observerContext.isSystemSpan()) { ObserveUtils.setObserverContextToCurrentFrame(strand, observerContext.getParent()); @@ -153,7 +156,10 @@ public boolean addTag(String tagKey, String tagValue, long spanId, Strand strand if (!enabled) { return false; } - ObserverContext observerContext = observerContextList.get(spanId); + ObserverContext observerContext = null; + if (observerContextList.containsKey(spanId)) { + observerContext = observerContextList.get(spanId); + } if (spanId == -1) { Optional observer = ObserveUtils.getObserverContextOfCurrentFrame(strand); if (observer.isPresent()) { From dee14f75be2172fc08876584fdb24d0053846c0f Mon Sep 17 00:00:00 2001 From: NipunaMadhushan Date: Mon, 24 Jun 2024 10:17:09 +0530 Subject: [PATCH 2/5] Address review suggestions --- .../observe/nativeimpl/OpenTracerBallerinaWrapper.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java b/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java index 3dd7d3d9e440..dd75e18f16a9 100644 --- a/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java +++ b/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java @@ -126,10 +126,7 @@ public boolean finishSpan(Strand strand, long spanId) { if (!enabled) { return false; } - ObserverContext observerContext = null; - if (observerContextList.containsKey(spanId)) { - observerContext = observerContextList.get(spanId); - } + ObserverContext observerContext = observerContextList.getOrDefault(spanId, null); if (observerContext != null) { if (observerContext.isSystemSpan()) { ObserveUtils.setObserverContextToCurrentFrame(strand, observerContext.getParent()); @@ -156,10 +153,7 @@ public boolean addTag(String tagKey, String tagValue, long spanId, Strand strand if (!enabled) { return false; } - ObserverContext observerContext = null; - if (observerContextList.containsKey(spanId)) { - observerContext = observerContextList.get(spanId); - } + ObserverContext observerContext = observerContextList.getOrDefault(spanId, null); if (spanId == -1) { Optional observer = ObserveUtils.getObserverContextOfCurrentFrame(strand); if (observer.isPresent()) { From 8d283322d0aa3840351961d81c5c228ee83e2a50 Mon Sep 17 00:00:00 2001 From: NipunaMadhushan Date: Mon, 24 Jun 2024 10:33:52 +0530 Subject: [PATCH 3/5] Address review suggestions --- .../observe/nativeimpl/OpenTracerBallerinaWrapper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java b/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java index dd75e18f16a9..5f2de6059d9e 100644 --- a/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java +++ b/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java @@ -126,7 +126,7 @@ public boolean finishSpan(Strand strand, long spanId) { if (!enabled) { return false; } - ObserverContext observerContext = observerContextList.getOrDefault(spanId, null); + ObserverContext observerContext = observerContextList.get(spanId); if (observerContext != null) { if (observerContext.isSystemSpan()) { ObserveUtils.setObserverContextToCurrentFrame(strand, observerContext.getParent()); @@ -153,7 +153,7 @@ public boolean addTag(String tagKey, String tagValue, long spanId, Strand strand if (!enabled) { return false; } - ObserverContext observerContext = observerContextList.getOrDefault(spanId, null); + ObserverContext observerContext = observerContextList.get(spanId); if (spanId == -1) { Optional observer = ObserveUtils.getObserverContextOfCurrentFrame(strand); if (observer.isPresent()) { From d7e9965e3d5c7227594bedc1ca92d6eebde5883d Mon Sep 17 00:00:00 2001 From: NipunaMadhushan Date: Wed, 26 Jun 2024 11:36:13 +0530 Subject: [PATCH 4/5] Fix compilation error --- .../observe/nativeimpl/OpenTracerBallerinaWrapper.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java b/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java index 5f2de6059d9e..db62ec285ce0 100644 --- a/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java +++ b/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java @@ -27,9 +27,9 @@ import org.ballerinalang.jvm.observability.tracer.TracersStore; import org.ballerinalang.jvm.scheduling.Strand; -import java.util.HashMap; -import java.util.Map; import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicLong; import static org.ballerinalang.jvm.observability.ObservabilityConstants.CONFIG_TRACING_ENABLED; @@ -139,7 +139,7 @@ public boolean finishSpan(Strand strand, long spanId) { return false; } } - + /** * Method to add tags to an existing span. * From 12c26d32b330951912751f364865b35cbc94afcb Mon Sep 17 00:00:00 2001 From: NipunaMadhushan Date: Wed, 26 Jun 2024 11:40:42 +0530 Subject: [PATCH 5/5] Fix compilation error --- .../observe/nativeimpl/OpenTracerBallerinaWrapper.java | 1 + 1 file changed, 1 insertion(+) diff --git a/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java b/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java index db62ec285ce0..99974e37fdf1 100644 --- a/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java +++ b/observelib/observe/src/main/java/org/ballerinalang/observe/nativeimpl/OpenTracerBallerinaWrapper.java @@ -27,6 +27,7 @@ import org.ballerinalang.jvm.observability.tracer.TracersStore; import org.ballerinalang.jvm.scheduling.Strand; +import java.util.Map; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap;