Skip to content

Commit 43a4b97

Browse files
authored
Polish gh-4857 (#5368)
1 parent 71dcae5 commit 43a4b97

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

concurrency-tests/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies {
1111
}
1212

1313
jcstress {
14-
jcstressDependency 'org.openjdk.jcstress:jcstress-core:0.16'
14+
libs.jcstressCore
1515

1616
verbose = true
1717
}

micrometer-core/src/main/java/io/micrometer/core/instrument/MeterRegistry.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public abstract class MeterRegistry {
103103

104104
/**
105105
* write/remove guarded by meterMapLock, read in
106-
* {@link this#getOrCreateMeter(DistributionStatisticConfig, BiFunction, Id,
107-
* Function)} is unguarded
106+
* {@link #getOrCreateMeter(DistributionStatisticConfig, BiFunction, Id, Function)} is
107+
* unguarded
108108
*/
109109
private final Map<Id, Meter> preFilterIdToMeterMap = new HashMap<>();
110110

@@ -755,15 +755,16 @@ public Meter remove(Meter.Id mappedId) {
755755
if (meterMap.containsKey(mappedId)) {
756756
synchronized (meterMapLock) {
757757
final Meter removedMeter = meterMap.remove(mappedId);
758-
Iterator<Map.Entry<Id, Meter>> iterator = preFilterIdToMeterMap.entrySet().iterator();
759-
while (iterator.hasNext()) {
760-
Map.Entry<Id, Meter> nextEntry = iterator.next();
761-
if (nextEntry.getValue().equals(removedMeter)) {
762-
stalePreFilterIds.remove(nextEntry.getKey());
763-
iterator.remove();
764-
}
765-
}
766758
if (removedMeter != null) {
759+
Iterator<Map.Entry<Id, Meter>> iterator = preFilterIdToMeterMap.entrySet().iterator();
760+
while (iterator.hasNext()) {
761+
Map.Entry<Id, Meter> nextEntry = iterator.next();
762+
if (nextEntry.getValue().equals(removedMeter)) {
763+
stalePreFilterIds.remove(nextEntry.getKey());
764+
iterator.remove();
765+
}
766+
}
767+
767768
Set<Id> synthetics = syntheticAssociations.remove(mappedId);
768769
if (synthetics != null) {
769770
for (Id synthetic : synthetics) {

micrometer-core/src/test/java/io/micrometer/core/instrument/MeterRegistryTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ void differentPreFilterIdsMapToSameIdWithStaleId() {
272272

273273
assertThat(c1).isSameAs(c2);
274274
assertThat(registry.remove(c1)).isSameAs(c2);
275-
assertThat(registry.remove(c2)).isNull();
276275
assertThat(registry.getMeters()).isEmpty();
277276
}
278277

@@ -314,6 +313,7 @@ void removingStaleMeterRemovesItFromAllInternalState() {
314313
Counter c1 = registry.counter("counter");
315314
// make c1 marked as stale
316315
registry.config().commonTags("common", "tag");
316+
assertThat(registry._getStalePreFilterIds()).hasSize(1);
317317

318318
registry.remove(c1.getId());
319319
assertThat(registry.getMeters()).isEmpty();
@@ -338,6 +338,7 @@ void unchangedStaleMeterShouldBeUnmarked() {
338338
Counter c1 = registry.counter("counter");
339339
// make c1 stale
340340
registry.config().meterFilter(MeterFilter.ignoreTags("abc"));
341+
assertThat(registry._getStalePreFilterIds()).hasSize(1);
341342
// this should cause c1 (== c2) to be unmarked as stale
342343
Counter c2 = registry.counter("counter");
343344

@@ -347,7 +348,7 @@ void unchangedStaleMeterShouldBeUnmarked() {
347348
assertThat(registry._getPreFilterIdToMeterMap()).hasSize(1);
348349
assertThat(registry._getStalePreFilterIds())
349350
.describedAs("If the meter-filter doesn't alter the meter creation, meters are never unmarked "
350-
+ "from staleness and we end-up paying the additional cost everytime")
351+
+ "from staleness and we end up paying the additional cost every time")
351352
.isEmpty();
352353
}
353354

0 commit comments

Comments
 (0)