Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAFKA-18337: KafkaProducer Memory Leak in JmxReporter Class #18332

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class JmxReporter implements MetricsReporter {
private String prefix;
private final Map<String, KafkaMbean> mbeans = new HashMap<>();
private Predicate<String> mbeanPredicate = s -> true;
private boolean closed = false;

public JmxReporter() {
this.prefix = "";
Expand Down Expand Up @@ -191,6 +192,7 @@ static String getMBeanName(String prefix, MetricName metricName) {

public void close() {
synchronized (LOCK) {
closed = true;
for (KafkaMbean mbean : this.mbeans.values())
unregister(mbean);
}
Expand All @@ -207,6 +209,12 @@ private void unregister(KafkaMbean mbean) {
}

private void reregister(KafkaMbean mbean) {
// avoid re-registering after being closed, which could lead to memory leaks
// See KAFKA-18337 for more detail.
if (closed) {
log.warn("JmxReporter has been closed!, cannot re-registering mbean {}", mbean);
tmw090906 marked this conversation as resolved.
Show resolved Hide resolved
return;
}
unregister(mbean);
try {
ManagementFactory.getPlatformMBeanServer().registerMBean(mbean, mbean.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.metrics.stats.Avg;
import org.apache.kafka.common.metrics.stats.CumulativeSum;
import org.apache.kafka.common.metrics.stats.Max;
import org.apache.kafka.common.utils.Time;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -177,4 +178,44 @@ public void testJmxPrefix() throws Exception {
metrics.close();
}
}

@Test
public void testClose() throws Exception {
Metrics metrics = new Metrics();
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
JmxReporter reporter = new JmxReporter();
try {
metrics.addReporter(reporter);

assertFalse(server.isRegistered(new ObjectName(":type=grp1")));

Sensor sensor = metrics.sensor("kafka.requests");
sensor.add(metrics.metricName("pack.bean1.avg", "grp1"), new Avg());
sensor.add(metrics.metricName("pack.bean1.max", "grp1"), new Max());
sensor.add(metrics.metricName("pack.bean2.total", "grp2"), new CumulativeSum());

assertTrue(server.isRegistered(new ObjectName(":type=grp1")));
assertEquals(Double.NaN, server.getAttribute(new ObjectName(":type=grp1"), "pack.bean1.avg"));
assertEquals(Double.NaN, server.getAttribute(new ObjectName(":type=grp1"), "pack.bean1.max"));
assertTrue(server.isRegistered(new ObjectName(":type=grp2")));
assertEquals(0.0, server.getAttribute(new ObjectName(":type=grp2"), "pack.bean2.total"));

MetricName metricName = metrics.metricName("pack.bean1.avg", "grp1");
String mBeanName = JmxReporter.getMBeanName("", metricName);
assertTrue(reporter.containsMbean(mBeanName));

assertTrue(server.isRegistered(new ObjectName(":type=grp1")));
assertTrue(server.isRegistered(new ObjectName(":type=grp2")));
} finally {
metrics.close();
}

assertFalse(server.isRegistered(new ObjectName(":type=grp1")));
assertFalse(server.isRegistered(new ObjectName(":type=grp2")));

MetricName metricName = metrics.metricName("pack.bean1.avg", "grp1");
metrics.removeMetric(metricName);
assertFalse(server.isRegistered(new ObjectName(":type=grp1")));
assertFalse(server.isRegistered(new ObjectName(":type=grp2")));
}
}
Loading