Skip to content

Commit d77b7be

Browse files
Fix concurrency issue with Exponential Histogram (#5749)
Synchronize writes to exponential histogram. An alternative would be to use explicit locks, but this made the code more complex and did not yield significant performance improvements over using a synchronized method. Also adds a concurrency test that reproduced the reported issue and verifies this fixes it. Fixes gh-5740
1 parent 7f5071f commit d77b7be

File tree

4 files changed

+103
-15
lines changed

4 files changed

+103
-15
lines changed

concurrency-tests/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dependencies {
88
implementation project(":micrometer-core")
99
// implementation("io.micrometer:micrometer-core:1.14.1")
1010
implementation project(":micrometer-registry-prometheus")
11+
implementation project(":micrometer-registry-otlp")
1112
// implementation("io.micrometer:micrometer-registry-prometheus:1.14.1")
1213
runtimeOnly(libs.logbackLatest)
1314
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2024 VMware, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micrometer.concurrencytests;
17+
18+
import static org.openjdk.jcstress.annotations.Expect.*;
19+
import static org.openjdk.jcstress.annotations.Expect.ACCEPTABLE;
20+
import static org.openjdk.jcstress.annotations.Expect.FORBIDDEN;
21+
22+
import org.openjdk.jcstress.annotations.*;
23+
import org.openjdk.jcstress.infra.results.*;
24+
import io.micrometer.registry.otlp.internal.Base2ExponentialHistogram;
25+
import io.micrometer.registry.otlp.internal.CumulativeBase2ExponentialHistogram;
26+
27+
public class Base2ExponentialHistogramConcurrencyTests {
28+
29+
@JCStressTest
30+
@Outcome(id = "null, 5, 2", expect = ACCEPTABLE, desc = "Read after all writes")
31+
@Outcome(id = "null, 20, 0", expect = ACCEPTABLE, desc = "Read before write")
32+
@Outcome(id = { "null, 20, 1" }, expect = ACCEPTABLE, desc = "Reading after single " + "write")
33+
@Outcome(
34+
id = { "class java.lang.ArrayIndexOutOfBoundsException, 20, 0",
35+
"class java.lang.ArrayIndexOutOfBoundsException, 20, 1" },
36+
expect = FORBIDDEN, desc = "Exception in recording thread")
37+
@Outcome(
38+
id = "class java.lang.ArrayIndexOutOfBoundsException, class java.lang.ArrayIndexOutOfBoundsException, null",
39+
expect = FORBIDDEN, desc = "Exception in both reading and writing threads")
40+
@Outcome(id = "null, class java.lang.ArrayIndexOutOfBoundsException, null", expect = FORBIDDEN,
41+
desc = "Exception in reading thread")
42+
@Outcome(expect = UNKNOWN)
43+
@State
44+
public static class RescalingAndConcurrentReading {
45+
46+
Base2ExponentialHistogram exponentialHistogram = new CumulativeBase2ExponentialHistogram(20, 40, 0, null);
47+
48+
@Actor
49+
public void actor1(LLL_Result r) {
50+
try {
51+
exponentialHistogram.recordDouble(2);
52+
}
53+
catch (Exception e) {
54+
r.r1 = e.getClass();
55+
}
56+
}
57+
58+
@Actor
59+
public void actor2(LLL_Result r) {
60+
try {
61+
exponentialHistogram.recordDouble(4);
62+
}
63+
catch (Exception e) {
64+
r.r1 = e.getClass();
65+
}
66+
}
67+
68+
@Actor
69+
public void actor3(LLL_Result r) {
70+
try {
71+
exponentialHistogram.takeSnapshot(2, 6, 4);
72+
r.r2 = exponentialHistogram.getLatestExponentialHistogramSnapshot().scale();
73+
r.r3 = exponentialHistogram.getLatestExponentialHistogramSnapshot()
74+
.positive()
75+
.bucketCounts()
76+
.stream()
77+
.mapToLong(Long::longValue)
78+
.sum();
79+
}
80+
catch (Exception e) {
81+
r.r2 = e.getClass();
82+
}
83+
}
84+
85+
}
86+
87+
}

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/internal/Base2ExponentialHistogram.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,16 @@ public void recordDouble(double value) {
167167
zeroCount.increment();
168168
return;
169169
}
170+
recordToHistogram(value);
171+
}
170172

173+
private synchronized void recordToHistogram(final double value) {
171174
int index = base2IndexProvider.getIndexForValue(value);
172175
if (!circularCountHolder.increment(index, 1)) {
173-
synchronized (this) {
174-
int downScaleFactor = getDownScaleFactor(index);
175-
downScale(downScaleFactor);
176-
index = base2IndexProvider.getIndexForValue(value);
177-
circularCountHolder.increment(index, 1);
178-
}
176+
int downScaleFactor = getDownScaleFactor(index);
177+
downScale(downScaleFactor);
178+
index = base2IndexProvider.getIndexForValue(value);
179+
circularCountHolder.increment(index, 1);
179180
}
180181
}
181182

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/internal/CircularCountHolder.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package io.micrometer.registry.otlp.internal;
1717

18-
import java.util.concurrent.atomic.AtomicLongArray;
18+
import java.util.Arrays;
1919

2020
/**
2121
* The CircularCountHolder is inspired from <a href=
@@ -25,7 +25,7 @@
2525
*/
2626
class CircularCountHolder {
2727

28-
private final AtomicLongArray counts;
28+
private final long[] counts;
2929

3030
private final int length;
3131

@@ -37,7 +37,7 @@ class CircularCountHolder {
3737

3838
CircularCountHolder(int size) {
3939
this.length = size;
40-
this.counts = new AtomicLongArray(size);
40+
this.counts = new long[size];
4141
this.baseIndex = Integer.MIN_VALUE;
4242
this.startIndex = Integer.MIN_VALUE;
4343
this.endIndex = Integer.MIN_VALUE;
@@ -52,7 +52,7 @@ int getEndIndex() {
5252
}
5353

5454
long getValueAtIndex(int index) {
55-
return counts.get(getRelativeIndex(index));
55+
return counts[getRelativeIndex(index)];
5656
}
5757

5858
boolean isEmpty() {
@@ -64,7 +64,7 @@ boolean increment(int index, long incrementBy) {
6464
this.baseIndex = index;
6565
this.startIndex = index;
6666
this.endIndex = index;
67-
this.counts.addAndGet(0, incrementBy);
67+
this.counts[0] = this.counts[0] + incrementBy;
6868
return true;
6969
}
7070

@@ -81,7 +81,8 @@ else if (index < startIndex) {
8181
startIndex = index;
8282
}
8383

84-
counts.addAndGet(getRelativeIndex(index), incrementBy);
84+
final int relativeIndex = getRelativeIndex(index);
85+
counts[relativeIndex] = counts[relativeIndex] + incrementBy;
8586
return true;
8687
}
8788

@@ -97,9 +98,7 @@ else if (result < 0) {
9798
}
9899

99100
void reset() {
100-
for (int i = 0; i < counts.length(); i++) {
101-
counts.set(i, 0);
102-
}
101+
Arrays.fill(counts, 0);
103102
this.baseIndex = Integer.MIN_VALUE;
104103
this.endIndex = Integer.MIN_VALUE;
105104
this.startIndex = Integer.MIN_VALUE;

0 commit comments

Comments
 (0)