Skip to content

Commit 098e598

Browse files
tianliuliuApache9
authored andcommitted
HBASE-27440 fix table HistogramMetrics leak in table metrics map (#4838)
Signed-off-by: Duo Zhang <[email protected]> (cherry picked from commit d187f69)
1 parent 1c2813b commit 098e598

2 files changed

Lines changed: 90 additions & 1 deletion

File tree

hbase-hadoop2-compat/src/main/java/org/apache/hadoop/metrics2/lib/DynamicMetricsRegistry.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.metrics2.lib;
1919

20+
import com.google.errorprone.annotations.RestrictedApi;
2021
import java.util.Collection;
2122
import java.util.concurrent.ConcurrentMap;
2223
import org.apache.hadoop.hbase.metrics.Interns;
@@ -391,8 +392,9 @@ public void removeMetric(String name) {
391392

392393
public void removeHistogramMetrics(String baseName) {
393394
for (String suffix : histogramSuffixes) {
394-
removeMetric(baseName + suffix);
395+
helper.removeObjectName(baseName + suffix);
395396
}
397+
metricsMap.remove(baseName);
396398
}
397399

398400
/**
@@ -535,4 +537,10 @@ public void clearMetrics() {
535537
}
536538
metricsMap.clear();
537539
}
540+
541+
@RestrictedApi(explanation = "Should only be called in TestMetricsTableMetricsMap", link = "",
542+
allowedOnPath = ".*/(DynamicMetricsRegistry|TestMetricsTableMetricsMap).java")
543+
public ConcurrentMap<String, MutableMetric> getMetricsMap() {
544+
return metricsMap;
545+
}
538546
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.regionserver;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import org.apache.hadoop.conf.Configuration;
24+
import org.apache.hadoop.hbase.HBaseClassTestRule;
25+
import org.apache.hadoop.hbase.testclassification.RegionServerTests;
26+
import org.apache.hadoop.hbase.testclassification.SmallTests;
27+
import org.junit.Before;
28+
import org.junit.ClassRule;
29+
import org.junit.Test;
30+
import org.junit.experimental.categories.Category;
31+
32+
@Category({ RegionServerTests.class, SmallTests.class })
33+
public class TestMetricsTableMetricsMap {
34+
35+
@ClassRule
36+
public static final HBaseClassTestRule CLASS_RULE =
37+
HBaseClassTestRule.forClass(TestMetricsTableMetricsMap.class);
38+
39+
private String tableName = "testTableMetricsMap";
40+
41+
private MetricsTableWrapperStub tableWrapper;
42+
private MetricsTable mt;
43+
private MetricsRegionServerWrapper rsWrapper;
44+
private MetricsRegionServer rsm;
45+
private MetricsTableAggregateSourceImpl agg;
46+
47+
@Before
48+
public void setUp() {
49+
Configuration conf = new Configuration();
50+
51+
tableWrapper = new MetricsTableWrapperStub(tableName);
52+
mt = new MetricsTable(tableWrapper);
53+
rsWrapper = new MetricsRegionServerWrapperStub();
54+
55+
rsm = new MetricsRegionServer(rsWrapper, conf, mt);
56+
MetricsTableAggregateSource tableSourceAgg = mt.getTableSourceAgg();
57+
if (tableSourceAgg instanceof MetricsTableAggregateSourceImpl) {
58+
agg = (MetricsTableAggregateSourceImpl) tableSourceAgg;
59+
} else {
60+
throw new RuntimeException(
61+
"tableSourceAgg should be the instance of MetricsTableAggregateSourceImpl");
62+
}
63+
}
64+
65+
@Test
66+
public void testMetricsMap() throws InterruptedException {
67+
// do major compaction
68+
rsm.updateCompaction(tableName, true, 100, 200, 300, 400, 500);
69+
70+
int metricsMapSize = agg.getMetricsRegistry().getMetricsMap().size();
71+
assertTrue("table metrics added then metricsMapSize should larger than 0", metricsMapSize > 0);
72+
73+
// just for metrics update
74+
Thread.sleep(1000);
75+
// delete table all metrics
76+
agg.deleteTableSource(tableName);
77+
78+
metricsMapSize = agg.getMetricsRegistry().getMetricsMap().size();
79+
assertEquals("table metrics all deleted then metricsSize should be 0", 0, metricsMapSize);
80+
}
81+
}

0 commit comments

Comments
 (0)