Skip to content

Commit 68238cb

Browse files
committed
Use striped reentrent lock in sync metric stroage instead of striped AtomicLong
1 parent dd63134 commit 68238cb

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/state/DefaultSynchronousMetricStorage.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import java.util.Objects;
3131
import java.util.concurrent.ConcurrentHashMap;
3232
import java.util.concurrent.ConcurrentLinkedQueue;
33-
import java.util.concurrent.atomic.AtomicInteger;
33+
import java.util.concurrent.locks.ReentrantLock;
3434
import java.util.logging.Level;
3535
import java.util.logging.Logger;
3636
import javax.annotation.Nullable;
@@ -242,7 +242,6 @@ AggregatorHandle<T> maybeGetPooledAggregatorHandle() {
242242
private AggregatorHolder<T> getHolderForRecord() {
243243
AggregatorHolder<T> aggregatorHolder = this.aggregatorHolder;
244244
while (!aggregatorHolder.tryAcquireForRecord()) {
245-
aggregatorHolder.releaseForRecord();
246245
aggregatorHolder = this.aggregatorHolder;
247246
Thread.yield();
248247
}
@@ -369,44 +368,38 @@ private static class AggregatorHolder<T extends PointData> {
369368
// (AggregatorHolder), and so if a recording thread encounters an odd value,
370369
// all it needs to do is release the "read lock" it just obtained (decrementing by 2),
371370
// and then grab and record against the new current interval (AggregatorHolder).
372-
private final AtomicInteger[] activeRecordingThreads;
371+
private final ReentrantLock[] locks;
373372

374373
private AggregatorHolder() {
375374
this(new ConcurrentHashMap<>());
376375
}
377376

378377
private AggregatorHolder(ConcurrentHashMap<Attributes, AggregatorHandle<T>> aggregatorHandles) {
379378
this.aggregatorHandles = aggregatorHandles;
380-
activeRecordingThreads = new AtomicInteger[Runtime.getRuntime().availableProcessors()];
381-
for (int i = 0; i < activeRecordingThreads.length; i++) {
382-
activeRecordingThreads[i] = new AtomicInteger(0);
379+
locks = new ReentrantLock[Runtime.getRuntime().availableProcessors()];
380+
for (int i = 0; i < locks.length; i++) {
381+
locks[i] = new ReentrantLock();
383382
}
384383
}
385384

386385
private boolean tryAcquireForRecord() {
387-
return forThread().addAndGet(2) % 2 == 0;
386+
return forThread().tryLock();
388387
}
389388

390389
private void releaseForRecord() {
391-
forThread().addAndGet(-2);
390+
forThread().unlock();
392391
}
393392

394393
@SuppressWarnings("ThreadPriorityCheck")
395394
private void acquireForCollect() {
396-
for (int i = 0; i < activeRecordingThreads.length; i++) {
397-
activeRecordingThreads[i].addAndGet(1);
398-
}
399-
for (int i = 0; i < activeRecordingThreads.length; i++) {
400-
AtomicInteger val = activeRecordingThreads[i];
401-
while (val.get() > 1) {
402-
Thread.yield();
403-
}
395+
for (int i = 0; i < locks.length; i++) {
396+
locks[i].lock();
404397
}
405398
}
406399

407-
private AtomicInteger forThread() {
408-
int index = Math.abs((int) (Thread.currentThread().getId() % activeRecordingThreads.length));
409-
return activeRecordingThreads[index];
400+
private ReentrantLock forThread() {
401+
int index = Math.abs((int) (Thread.currentThread().getId() % locks.length));
402+
return locks[index];
410403
}
411404
}
412405

0 commit comments

Comments
 (0)