-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Protect against concurrent reads/writes to Context keyvalues (#5739)
Using a ConcurrentHashMap for the Context keyvalues (low and high) avoids the issues with concurrent reads and writes. Related benchmarks and concurrency tests were added as well. See gh-4356 --------- Co-authored-by: Jonatan Ivanov <jonatan.ivanov@gmail.com>
- Loading branch information
1 parent
4a44430
commit 4f534a7
Showing
6 changed files
with
171 additions
and
10 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
...chmarks-core/src/jmh/java/io/micrometer/benchmark/core/ObservationKeyValuesBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2024 VMware, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.micrometer.benchmark.core; | ||
|
||
import io.micrometer.common.KeyValues; | ||
import io.micrometer.observation.Observation; | ||
import io.micrometer.observation.ObservationRegistry; | ||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@State(Scope.Benchmark) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
@Threads(4) | ||
public class ObservationKeyValuesBenchmark { | ||
|
||
private static final KeyValues KEY_VALUES = KeyValues.of("key1", "value1", "key2", "value2", "key3", "value3", | ||
"key4", "value4", "key5", "value5"); | ||
|
||
private final ObservationRegistry registry = ObservationRegistry.create(); | ||
|
||
private final Observation.Context context = new TestContext(); | ||
|
||
private final Observation observation = Observation.createNotStarted("jmh", () -> context, registry); | ||
|
||
@Benchmark | ||
@Group("contended") | ||
@GroupThreads(1) | ||
public Observation contendedWrite() { | ||
return write(); | ||
} | ||
|
||
@Benchmark | ||
@Group("contended") | ||
@GroupThreads(1) | ||
public KeyValues contendedRead() { | ||
return read(); | ||
} | ||
|
||
@Benchmark | ||
@Threads(1) | ||
public Observation uncontendedWrite() { | ||
return write(); | ||
} | ||
|
||
@Benchmark | ||
@Threads(1) | ||
public KeyValues uncontendedRead() { | ||
return read(); | ||
} | ||
|
||
private Observation write() { | ||
return observation.lowCardinalityKeyValues(KEY_VALUES); | ||
} | ||
|
||
private KeyValues read() { | ||
return observation.getContext().getLowCardinalityKeyValues(); | ||
} | ||
|
||
public static void main(String[] args) throws RunnerException { | ||
Options options = new OptionsBuilder().include(ObservationKeyValuesBenchmark.class.getSimpleName()) | ||
.warmupIterations(3) | ||
.measurementIterations(5) | ||
.mode(Mode.SampleTime) | ||
.forks(1) | ||
.build(); | ||
|
||
new Runner(options).run(); | ||
} | ||
|
||
static class TestContext extends Observation.Context { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...s/src/jcstress/java/io/micrometer/concurrencytests/ObservationContextConcurrencyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2024 VMware, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.micrometer.concurrencytests; | ||
|
||
import io.micrometer.common.KeyValue; | ||
import io.micrometer.observation.Observation; | ||
import org.openjdk.jcstress.annotations.Actor; | ||
import org.openjdk.jcstress.annotations.JCStressTest; | ||
import org.openjdk.jcstress.annotations.Outcome; | ||
import org.openjdk.jcstress.annotations.State; | ||
import org.openjdk.jcstress.infra.results.LL_Result; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.openjdk.jcstress.annotations.Expect.ACCEPTABLE; | ||
import static org.openjdk.jcstress.annotations.Expect.FORBIDDEN; | ||
|
||
public class ObservationContextConcurrencyTest { | ||
|
||
@JCStressTest | ||
@State | ||
@Outcome(id = "No exception, No exception", expect = ACCEPTABLE) | ||
@Outcome(expect = FORBIDDEN) | ||
public static class ConsistentKeyValues { | ||
|
||
private final Observation.Context context = new TestContext(); | ||
|
||
private final String uuid = UUID.randomUUID().toString(); | ||
|
||
@Actor | ||
public void read(LL_Result r) { | ||
try { | ||
context.getHighCardinalityKeyValues(); | ||
r.r1 = "No exception"; | ||
} | ||
catch (Exception e) { | ||
r.r1 = e.getClass(); | ||
} | ||
} | ||
|
||
@Actor | ||
public void write(LL_Result r) { | ||
try { | ||
context.addHighCardinalityKeyValue(KeyValue.of(uuid, uuid)); | ||
r.r2 = "No exception"; | ||
} | ||
catch (Exception e) { | ||
r.r2 = e.getClass(); | ||
} | ||
} | ||
|
||
} | ||
|
||
static class TestContext extends Observation.Context { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters