Skip to content

Commit 88a0a79

Browse files
committed
Fix slow test on java 8, 11, 17
1 parent 18a88d1 commit 88a0a79

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,16 @@ void doRecordDouble(double value, Attributes attributes, Context context) {
8181
}
8282
}
8383

84+
@SuppressWarnings("ThreadPriorityCheck")
8485
private DeltaAggregatorHandle<T> acquireHandleForRecord(Attributes attributes, Context context) {
8586
while (true) {
8687
DeltaAggregatorHandle<T> handle =
8788
getDeltaAggregatorHandle(this.aggregatorHolder, attributes, context);
8889
if (handle != null) {
8990
return handle;
9091
}
92+
// Holder or handle is locked for collection; yield to let the collector advance
93+
Thread.yield();
9194
}
9295
}
9396

sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SynchronousInstrumentStressTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData;
4242
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSumData;
4343
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
44-
import java.time.Duration;
4544
import java.util.ArrayList;
4645
import java.util.Arrays;
4746
import java.util.Collections;
@@ -65,7 +64,6 @@
6564
class SynchronousInstrumentStressTest {
6665

6766
private static final String INSTRUMENT_NAME = "instrument";
68-
private static final Duration ONE_MICROSECOND = Duration.ofNanos(1000);
6967
private static final List<Double> BUCKET_BOUNDARIES =
7068
ExplicitBucketHistogramUtils.DEFAULT_HISTOGRAM_BUCKET_BOUNDARIES;
7169
private static final double[] BUCKET_BOUNDARIES_ARR =
@@ -93,6 +91,7 @@ void stressTest(
9391
}
9492
}
9593

94+
@SuppressWarnings("ThreadPriorityCheck")
9695
private void stressTestOnce(
9796
AggregationTemporality aggregationTemporality,
9897
InstrumentType instrumentType,
@@ -130,15 +129,17 @@ private void stressTestOnce(
130129
int threadCount = 4;
131130
List<Thread> recordThreads = new ArrayList<>();
132131
CountDownLatch latch = new CountDownLatch(threadCount);
132+
CountDownLatch startSignal = new CountDownLatch(1);
133133
for (int i = 0; i < threadCount; i++) {
134134
recordThreads.add(
135135
new Thread(
136136
() -> {
137+
Uninterruptibles.awaitUninterruptibly(startSignal);
137138
for (Long measurement : measurements) {
138139
for (Attributes attr : attributes) {
139140
instrument.record(measurement, attr);
140141
}
141-
Uninterruptibles.sleepUninterruptibly(ONE_MICROSECOND);
142+
Thread.yield();
142143
}
143144
latch.countDown();
144145
}));
@@ -150,8 +151,9 @@ private void stressTestOnce(
150151
Thread collectThread =
151152
new Thread(
152153
() -> {
154+
Uninterruptibles.awaitUninterruptibly(startSignal);
153155
while (latch.getCount() != 0) {
154-
Uninterruptibles.sleepUninterruptibly(ONE_MICROSECOND);
156+
Thread.yield();
155157
collectedMetrics.addAll(
156158
reader.collectAllMetrics().stream()
157159
.map(SynchronousInstrumentStressTest::copy)
@@ -163,9 +165,10 @@ private void stressTestOnce(
163165
.collect(toList()));
164166
});
165167

166-
// Start all the threads
168+
// Start all the threads, then release the start signal so they begin simultaneously
167169
collectThread.start();
168170
recordThreads.forEach(Thread::start);
171+
startSignal.countDown();
169172

170173
// Wait for the collect thread to end, which collects until the record threads are done
171174
Uninterruptibles.joinUninterruptibly(collectThread);

0 commit comments

Comments
 (0)