From 8926439e800ed9f07c2c91caa87b44aae6149bfb Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Mon, 23 Oct 2023 12:55:31 -0500 Subject: [PATCH] Use ReentrantReadWriteLock --- .../DefaultSynchronousMetricStorage.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/DefaultSynchronousMetricStorage.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/DefaultSynchronousMetricStorage.java index cf43245637f..eae1b6ce856 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/DefaultSynchronousMetricStorage.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/DefaultSynchronousMetricStorage.java @@ -26,7 +26,9 @@ import java.util.Queue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.locks.StampedLock; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.logging.Level; import java.util.logging.Logger; @@ -47,7 +49,9 @@ public final class DefaultSynchronousMetricStorage aggregator; - private final StampedLock sl = new StampedLock(); + private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); + private final Lock readLock = readWriteLock.readLock(); + private final Lock writeLock = readWriteLock.writeLock(); private ConcurrentHashMap> aggregatorHandles = new ConcurrentHashMap<>(); private final AttributesProcessor attributesProcessor; @@ -85,12 +89,12 @@ Queue> getAggregatorHandlePool() { @Override public void recordLong(long value, Attributes attributes, Context context) { - long stamp = sl.readLock(); + readLock.lock(); try { AggregatorHandle handle = getAggregatorHandle(attributes, context); handle.recordLong(value, attributes, context); } finally { - sl.unlockRead(stamp); + readLock.unlock(); } } @@ -106,12 +110,12 @@ public void recordDouble(double value, Attributes attributes, Context context) { + ". Dropping measurement."); return; } - long stamp = sl.readLock(); + readLock.lock(); try { AggregatorHandle handle = getAggregatorHandle(attributes, context); handle.recordDouble(value, attributes, context); } finally { - sl.unlockRead(stamp); + readLock.unlock(); } } @@ -160,12 +164,12 @@ public MetricData collect( ConcurrentHashMap> aggregatorHandles; if (reset) { - long stamp = sl.writeLock(); + writeLock.lock(); try { aggregatorHandles = this.aggregatorHandles; this.aggregatorHandles = new ConcurrentHashMap<>(); } finally { - sl.unlockWrite(stamp); + writeLock.unlock(); } } else { aggregatorHandles = this.aggregatorHandles;