Skip to content

Commit

Permalink
RATIS-1705. Fix metrics leak (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen01 authored Aug 3, 2023
1 parent 9039959 commit 95b51e5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ private void inc(String metricNamePrefix, Type t) {
types.get(t)
.computeIfAbsent(metricNamePrefix, prefix -> getRegistry().counter(prefix + t.getSuffix()))
.inc();
final Map<String, LongCounter> counters = types.get(t);
LongCounter c = counters.get(metricNamePrefix);
if (c == null) {
synchronized (counters) {
c = counters.computeIfAbsent(metricNamePrefix, prefix -> getRegistry().counter(prefix + t.getSuffix()));
}
}
c.inc();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class MetricRegistriesImpl extends MetricRegistries {
private final MetricRegistryFactoryImpl factory;

private final RefCountingMap<MetricRegistryInfo, RatisMetricRegistry> registries;
private final Object registerLock = new Object();

public MetricRegistriesImpl() {
this(new MetricRegistryFactoryImpl());
Expand All @@ -60,12 +61,17 @@ public MetricRegistriesImpl() {
public RatisMetricRegistry create(MetricRegistryInfo info) {
return registries.put(info, () -> {
if (reporterRegistrations.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("First MetricRegistry has been created without registering reporters. " +
"Hence registering JMX reporter by default.");
synchronized (registerLock) {
if (reporterRegistrations.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("First MetricRegistry has been created without registering reporters. " +
"Hence registering JMX reporter by default.");
}
enableJmxReporter();
}
}
enableJmxReporter();
}

RatisMetricRegistry registry = factory.create(info);
reporterRegistrations.forEach(reg -> reg.accept(registry));
return registry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class Dm3MetricRegistriesImpl extends MetricRegistries {

private final RefCountingMap<MetricRegistryInfo, RatisMetricRegistry> registries;

private final Object registerLock = new Object();

public Dm3MetricRegistriesImpl() {
this(new Dm3MetricRegistryFactoryImpl());
}
Expand All @@ -60,11 +62,13 @@ public Dm3MetricRegistriesImpl() {
public RatisMetricRegistry create(MetricRegistryInfo info) {
return registries.put(info, () -> {
if (reporterRegistrations.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("First MetricRegistry has been created without registering reporters. " +
"Hence registering JMX reporter by default.");
synchronized (registerLock) {
if (LOG.isDebugEnabled()) {
LOG.debug("First MetricRegistry has been created without registering reporters. " +
"Hence registering JMX reporter by default.");
}
enableJmxReporter();
}
enableJmxReporter();
}
RatisMetricRegistry registry = factory.create(info);
reporterRegistrations.forEach(reg -> reg.accept(registry));
Expand Down

0 comments on commit 95b51e5

Please sign in to comment.