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+ */
119package io .smallrye .metrics .legacyapi ;
220
321import java .util .Collections ;
422import java .util .HashMap ;
523import java .util .Map ;
624import java .util .Map .Entry ;
725import java .util .Optional ;
26+ import java .util .Set ;
827import java .util .SortedMap ;
928import java .util .SortedSet ;
1029import java .util .TreeMap ;
1130import java .util .TreeSet ;
1231import java .util .concurrent .ConcurrentHashMap ;
13- import java .util .concurrent .ConcurrentLinkedQueue ;
1432import java .util .function .Function ;
1533import java .util .function .Supplier ;
1634import 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