Skip to content

Commit 1f9a4b5

Browse files
committed
atomicreference+atomiclong
1 parent 1371683 commit 1f9a4b5

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import io.opentelemetry.sdk.resources.Resource;
2323
import java.util.Collection;
2424
import java.util.List;
25-
import java.util.concurrent.atomic.AtomicBoolean;
25+
import java.util.Objects;
26+
import java.util.concurrent.atomic.AtomicLong;
27+
import java.util.concurrent.atomic.AtomicReference;
2628
import java.util.function.Supplier;
2729
import javax.annotation.Nullable;
2830
import javax.annotation.concurrent.ThreadSafe;
@@ -113,8 +115,8 @@ public MetricData toMetricData(
113115
}
114116

115117
static final class Handle extends AggregatorHandle<DoublePointData, DoubleExemplarData> {
116-
private final AtomicBoolean set = new AtomicBoolean(false);
117-
private volatile double current = 0;
118+
private final AtomicReference<AtomicLong> current = new AtomicReference<>(null);
119+
private final AtomicLong valueBits = new AtomicLong();
118120

119121
// Only used when memoryMode is REUSABLE_DATA
120122
@Nullable private final MutableDoublePointData reusablePoint;
@@ -135,27 +137,22 @@ protected DoublePointData doAggregateThenMaybeReset(
135137
Attributes attributes,
136138
List<DoubleExemplarData> exemplars,
137139
boolean reset) {
138-
double currentLocal = current;
139-
if ((reset && !set.compareAndSet(true, false)) || (!reset && !set.get())) {
140-
throw new NullPointerException();
141-
}
142-
143-
DoublePointData output;
140+
AtomicLong valueBits =
141+
Objects.requireNonNull(reset ? this.current.getAndSet(null) : this.current.get());
142+
double value = Double.longBitsToDouble(valueBits.get());
144143
if (reusablePoint != null) {
145-
reusablePoint.set(startEpochNanos, epochNanos, attributes, currentLocal, exemplars);
146-
output = reusablePoint;
144+
reusablePoint.set(startEpochNanos, epochNanos, attributes, value, exemplars);
145+
return reusablePoint;
147146
} else {
148-
output =
149-
ImmutableDoublePointData.create(
150-
startEpochNanos, epochNanos, attributes, currentLocal, exemplars);
147+
return ImmutableDoublePointData.create(
148+
startEpochNanos, epochNanos, attributes, value, exemplars);
151149
}
152-
return output;
153150
}
154151

155152
@Override
156153
protected void doRecordDouble(double value) {
157-
current = value;
158-
set.set(true);
154+
valueBits.set(Double.doubleToLongBits(value));
155+
current.compareAndSet(null, valueBits);
159156
}
160157
}
161158
}

0 commit comments

Comments
 (0)