Skip to content

Commit 8bc72bb

Browse files
Channyboyjhanders34
authored andcommitted
Over lay change to LegacymetricRegistryAdapter to not use ConcurrentLinkedQueue as it caused Memory Leak
1 parent 4ea5d11 commit 8bc72bb

File tree

2 files changed

+54
-17
lines changed

2 files changed

+54
-17
lines changed

dev/io.openliberty.io.smallrye.metrics/bnd.bnd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#*******************************************************************************
2-
# Copyright (c) 2022, 2023 IBM Corporation and others.
2+
# Copyright (c) 2022, 2024 IBM Corporation and others.
33
# All rights reserved. This program and the accompanying materials
44
# are made available under the terms of the Eclipse Public License 2.0
55
# which accompanies this distribution, and is available at

dev/io.openliberty.io.smallrye.metrics/src/io/smallrye/metrics/legacyapi/LegacyMetricRegistryAdapter.java

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1+
/*
2+
* Copyright 2020, 2024 Red Hat, Inc, and individual contributors
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+
* http://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+
/*
17+
* Liberty changes are enclosed by LIBERTY CHANGE START and LIBERTY CHANGE END
18+
*/
119
package io.smallrye.metrics.legacyapi;
220

321
import java.util.Collections;
422
import java.util.HashMap;
523
import java.util.Map;
624
import java.util.Map.Entry;
725
import java.util.Optional;
26+
import java.util.Set;
827
import java.util.SortedMap;
928
import java.util.SortedSet;
1029
import java.util.TreeMap;
1130
import java.util.TreeSet;
1231
import java.util.concurrent.ConcurrentHashMap;
13-
import java.util.concurrent.ConcurrentLinkedQueue;
1432
import java.util.function.Function;
1533
import java.util.function.Supplier;
1634
import java.util.function.ToDoubleFunction;
@@ -62,7 +80,12 @@ public class LegacyMetricRegistryAdapter implements MetricRegistry {
6280

6381
protected final ConcurrentHashMap<String, io.micrometer.core.instrument.Tag> applicationMPConfigAppNameTagCache;
6482

65-
protected final ConcurrentHashMap<String, ConcurrentLinkedQueue<MetricID>> applicationMap;
83+
// LIBERTY CHANGE START
84+
/*
85+
* Liberty change. Previously, the second param type of ConcurrentLinkedQueue lead to memory leak.
86+
*/
87+
protected final ConcurrentHashMap<String, Set<MetricID>> applicationMap;
88+
// LIBERTY CHANGE END
6689

6790
protected final ApplicationNameResolver appNameResolver;
6891

@@ -133,14 +156,21 @@ public void addNameToApplicationMap(MetricID metricID, String appName) {
133156
*/
134157
if (appName == null)
135158
return;
136-
ConcurrentLinkedQueue<MetricID> list = applicationMap.get(appName);
137-
if (list == null) {
138-
ConcurrentLinkedQueue<MetricID> newList = new ConcurrentLinkedQueue<MetricID>();
139-
list = applicationMap.putIfAbsent(appName, newList);
140-
if (list == null)
141-
list = newList;
159+
160+
// LIBERTY CHANGE START
161+
/*
162+
* Liberty change. Previously, the second param type of ConcurrentLinkedQueue lead to memory leak.
163+
*/
164+
165+
Set<MetricID> set = applicationMap.get(appName);
166+
if (set == null) {
167+
Set<MetricID> newSet = Collections.newSetFromMap(new ConcurrentHashMap<MetricID, Boolean>());
168+
set = applicationMap.putIfAbsent(appName, newSet);
169+
if (set == null)
170+
set = newSet;
142171
}
143-
list.add(metricID);
172+
set.add(metricID);
173+
// LIBERTY CHANGE END
144174
if (LOGGER.isLoggable(Level.FINER)) {
145175
LOGGER.logp(Level.FINER, CLASS_NAME, METHOD_NAME,
146176
String.format("Mapped MetricID [id= %s] to application \"%s\"", metricID, appName));
@@ -161,11 +191,14 @@ public void unRegisterApplicationMetrics(String appName) {
161191
if (appName == null) {
162192
return;
163193
}
164-
165-
ConcurrentLinkedQueue<MetricID> list = applicationMap.remove(appName);
166-
167-
if (list != null) {
168-
for (MetricID metricID : list) {
194+
// LIBERTY CHANGE START
195+
/*
196+
* Liberty change. Previously, the second Parameter type of ConcurrentLinkedQueue lead to memory leak.
197+
*/
198+
Set<MetricID> set = applicationMap.remove(appName);
199+
// LIBERTY CHANGE END
200+
if (set != null) {
201+
for (MetricID metricID : set) {
169202
remove(metricID);
170203
}
171204
}
@@ -175,7 +208,6 @@ public void unRegisterApplicationMetrics(String appName) {
175208
}
176209

177210
public LegacyMetricRegistryAdapter(String scope, MeterRegistry registry, ApplicationNameResolver appNameResolver) {
178-
179211
/*
180212
* Note: if ApplicationNameResolver is passed through as Java Reflection Proxy object,
181213
* can only be checked if its is "null".
@@ -194,7 +226,12 @@ public LegacyMetricRegistryAdapter(String scope, MeterRegistry registry, Applica
194226

195227
applicationMPConfigAppNameTagCache = new ConcurrentHashMap<String, io.micrometer.core.instrument.Tag>();
196228

197-
applicationMap = new ConcurrentHashMap<String, ConcurrentLinkedQueue<MetricID>>();
229+
// LIBERTY CHANGE START
230+
/*
231+
* Liberty change. Previously, the second param type of ConcurrentLinkedQueue lead to memory leak.
232+
*/
233+
applicationMap = new ConcurrentHashMap<String, Set<MetricID>>();
234+
// LIBERTY CHANGE END
198235

199236
defaultAppNameValue = resolveMPConfigDefaultAppNameTag();
200237

0 commit comments

Comments
 (0)