Skip to content

Commit 1dde255

Browse files
authored
Use synchronized instead of reentrant lock in explicit bucket histogram (open-telemetry#6309)
1 parent bc5eb9e commit 1dde255

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ uses [google-java-format](https://github.com/google/google-java-format) library:
138138
* Adding `toString()` overrides on classes is encouraged, but we only use `toString()` to provide
139139
debugging assistance. The implementations of all `toString()` methods should be considered to be
140140
unstable unless explicitly documented otherwise.
141+
* Avoid synchronizing using a class's intrinsic lock. Instead, synchronize on a dedicated lock object. E.g:
142+
```java
143+
private final Object lock = new Object();
144+
145+
public void doSomething() {
146+
synchronized (lock) { ... }
147+
}
148+
```
141149

142150
If you notice any practice being applied in the project consistently that isn't listed here, please
143151
consider a pull request to add it.

sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/internal/aggregator/DoubleExplicitBucketHistogramAggregator.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.Collection;
2727
import java.util.Collections;
2828
import java.util.List;
29-
import java.util.concurrent.locks.ReentrantLock;
3029
import java.util.function.Supplier;
3130
import javax.annotation.Nullable;
3231

@@ -95,6 +94,8 @@ static final class Handle extends AggregatorHandle<HistogramPointData, DoubleExe
9594
// read-only
9695
private final double[] boundaries;
9796

97+
private final Object lock = new Object();
98+
9899
@GuardedBy("lock")
99100
private double sum;
100101

@@ -110,8 +111,6 @@ static final class Handle extends AggregatorHandle<HistogramPointData, DoubleExe
110111
@GuardedBy("lock")
111112
private final long[] counts;
112113

113-
private final ReentrantLock lock = new ReentrantLock();
114-
115114
// Used only when MemoryMode = REUSABLE_DATA
116115
@Nullable private MutableHistogramPointData reusablePoint;
117116

@@ -140,8 +139,7 @@ protected HistogramPointData doAggregateThenMaybeReset(
140139
Attributes attributes,
141140
List<DoubleExemplarData> exemplars,
142141
boolean reset) {
143-
lock.lock();
144-
try {
142+
synchronized (lock) {
145143
HistogramPointData pointData;
146144
if (reusablePoint == null) {
147145
pointData =
@@ -180,24 +178,19 @@ protected HistogramPointData doAggregateThenMaybeReset(
180178
Arrays.fill(this.counts, 0);
181179
}
182180
return pointData;
183-
} finally {
184-
lock.unlock();
185181
}
186182
}
187183

188184
@Override
189185
protected void doRecordDouble(double value) {
190186
int bucketIndex = ExplicitBucketHistogramUtils.findBucketIndex(this.boundaries, value);
191187

192-
lock.lock();
193-
try {
188+
synchronized (lock) {
194189
this.sum += value;
195190
this.min = Math.min(this.min, value);
196191
this.max = Math.max(this.max, value);
197192
this.count++;
198193
this.counts[bucketIndex]++;
199-
} finally {
200-
lock.unlock();
201194
}
202195
}
203196

0 commit comments

Comments
 (0)