Skip to content

Commit e4028e9

Browse files
committed
Merge branch '1.14.x'
2 parents 9d650ca + b2d8277 commit e4028e9

File tree

5 files changed

+144
-20
lines changed

5 files changed

+144
-20
lines changed

Diff for: benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/compare/CompareOTLPHistograms.java

+40-5
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,26 @@
4545
*/
4646
@Fork(1)
4747
@Measurement(iterations = 2)
48-
@Warmup(iterations = 2)
49-
@BenchmarkMode(Mode.AverageTime)
48+
@Warmup(iterations = 3)
49+
@BenchmarkMode(Mode.SampleTime)
5050
@OutputTimeUnit(TimeUnit.NANOSECONDS)
51-
@Threads(16)
51+
@Threads(2)
5252
public class CompareOTLPHistograms {
5353

54+
// disable publishing since we are only benchmarking recording
55+
static OtlpConfig disabledConfig = new OtlpConfig() {
56+
57+
@Override
58+
public boolean enabled() {
59+
return false;
60+
}
61+
62+
@Override
63+
public String get(String key) {
64+
return "";
65+
}
66+
};
67+
5468
@State(Scope.Thread)
5569
public static class Data {
5670

@@ -82,7 +96,7 @@ public static class DistributionsWithoutHistogramCumulative {
8296

8397
@Setup(Level.Iteration)
8498
public void setup() {
85-
registry = new OtlpMeterRegistry();
99+
registry = new OtlpMeterRegistry(disabledConfig, Clock.SYSTEM);
86100
distributionSummary = DistributionSummary.builder("ds").register(registry);
87101
timer = Timer.builder("timer").register(registry);
88102
}
@@ -99,6 +113,11 @@ public static class DistributionsWithoutHistogramDelta {
99113

100114
OtlpConfig otlpConfig = new OtlpConfig() {
101115

116+
@Override
117+
public boolean enabled() {
118+
return false;
119+
}
120+
102121
@Override
103122
public AggregationTemporality aggregationTemporality() {
104123
return AggregationTemporality.DELTA;
@@ -142,7 +161,7 @@ public static class ExplicitBucketHistogramCumulative {
142161

143162
@Setup(Level.Iteration)
144163
public void setup() {
145-
registry = new OtlpMeterRegistry();
164+
registry = new OtlpMeterRegistry(disabledConfig, Clock.SYSTEM);
146165
distributionSummary = DistributionSummary.builder("ds").publishPercentileHistogram().register(registry);
147166
timer = Timer.builder("timer").publishPercentileHistogram().register(registry);
148167
}
@@ -159,6 +178,11 @@ public static class ExplicitBucketHistogramDelta {
159178

160179
OtlpConfig otlpConfig = new OtlpConfig() {
161180

181+
@Override
182+
public boolean enabled() {
183+
return false;
184+
}
185+
162186
@Override
163187
public AggregationTemporality aggregationTemporality() {
164188
return AggregationTemporality.DELTA;
@@ -197,6 +221,12 @@ public static class ExponentialHistogramCumulative {
197221
MeterRegistry registry;
198222

199223
OtlpConfig otlpConfig = new OtlpConfig() {
224+
225+
@Override
226+
public boolean enabled() {
227+
return false;
228+
}
229+
200230
@Override
201231
public HistogramFlavor histogramFlavor() {
202232
return HistogramFlavor.BASE2_EXPONENTIAL_BUCKET_HISTOGRAM;
@@ -234,6 +264,11 @@ public static class ExponentialHistogramDelta {
234264

235265
OtlpConfig otlpConfig = new OtlpConfig() {
236266

267+
@Override
268+
public boolean enabled() {
269+
return false;
270+
}
271+
237272
@Override
238273
public AggregationTemporality aggregationTemporality() {
239274
return AggregationTemporality.DELTA;

Diff for: concurrency-tests/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ dependencies {
99
// implementation("io.micrometer:micrometer-core:1.14.1")
1010
implementation project(":micrometer-registry-prometheus")
1111
// implementation("io.micrometer:micrometer-registry-prometheus:1.14.1")
12+
implementation project(":micrometer-registry-otlp")
13+
1214
runtimeOnly(libs.logbackLatest)
1315
}
1416

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+
}

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

+7-6
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

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

+8-9
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)