Skip to content

Commit f297977

Browse files
authored
Tablet server compaction metrics NPE fix (#5364)
* Resolves NPEs in tablet server compaction metrics
1 parent dacc3e9 commit f297977

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

server/tserver/src/main/java/org/apache/accumulo/tserver/metrics/CompactionExecutorsMetrics.java

+34-26
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,26 @@ public class CompactionExecutorsMetrics implements MetricsProducer {
5050
private MeterRegistry registry = null;
5151

5252
// public so it can be closed by outside callers
53-
public static class CeMetrics implements AutoCloseable {
54-
private AtomicInteger queued;
55-
private AtomicInteger running;
53+
public class CeMetrics implements AutoCloseable {
54+
private final AtomicInteger queued;
55+
private final AtomicInteger running;
5656

5757
private IntSupplier runningSupplier;
5858
private IntSupplier queuedSupplier;
5959

60+
private CeMetrics(CompactionExecutorId ceid) {
61+
if (registry != null) {
62+
this.queued = registry.gauge(METRICS_MAJC_QUEUED, Tags.of("id", ceid.canonical()),
63+
new AtomicInteger(0));
64+
this.running = registry.gauge(METRICS_MAJC_RUNNING, Tags.of("id", ceid.canonical()),
65+
new AtomicInteger(0));
66+
} else {
67+
// these vars have no effect on metrics in this case - just avoids NPEs
68+
this.queued = new AtomicInteger(0);
69+
this.running = new AtomicInteger(0);
70+
}
71+
}
72+
6073
@Override
6174
public void close() {
6275
runningSupplier = () -> 0;
@@ -66,9 +79,22 @@ public void close() {
6679
}
6780
}
6881

69-
private static class ExMetrics {
70-
AtomicInteger queued;
71-
AtomicInteger running;
82+
private class ExMetrics {
83+
private final AtomicInteger queued;
84+
private final AtomicInteger running;
85+
86+
private ExMetrics(CompactionExecutorId ceid) {
87+
if (registry != null) {
88+
this.queued = registry.gauge(METRICS_MAJC_QUEUED, Tags.of("id", ceid.canonical()),
89+
new AtomicInteger(0));
90+
this.running = registry.gauge(METRICS_MAJC_RUNNING, Tags.of("id", ceid.canonical()),
91+
new AtomicInteger(0));
92+
} else {
93+
// these vars have no effect on metrics in this case - just avoids NPEs
94+
this.queued = new AtomicInteger(0);
95+
this.running = new AtomicInteger(0);
96+
}
97+
}
7298
}
7399

74100
public CompactionExecutorsMetrics() {
@@ -89,16 +115,7 @@ public synchronized CeMetrics addExecutor(CompactionExecutorId ceid, IntSupplier
89115

90116
synchronized (ceMetricsMap) {
91117

92-
CeMetrics cem = ceMetricsMap.computeIfAbsent(ceid, id -> {
93-
CeMetrics m = new CeMetrics();
94-
if (registry != null) {
95-
m.queued = registry.gauge(METRICS_MAJC_QUEUED, Tags.of("id", ceid.canonical()),
96-
new AtomicInteger(0));
97-
m.running = registry.gauge(METRICS_MAJC_RUNNING, Tags.of("id", ceid.canonical()),
98-
new AtomicInteger(0));
99-
}
100-
return m;
101-
});
118+
CeMetrics cem = ceMetricsMap.computeIfAbsent(ceid, id -> new CeMetrics(ceid));
102119

103120
cem.runningSupplier = runningSupplier;
104121
cem.queuedSupplier = queuedSupplier;
@@ -120,16 +137,7 @@ public void update() {
120137
externalMetricsSupplier.get().forEach(ecm -> {
121138
seenIds.add(ecm.ceid);
122139

123-
ExMetrics exm = exCeMetricsMap.computeIfAbsent(ecm.ceid, id -> {
124-
ExMetrics m = new ExMetrics();
125-
if (registry != null) {
126-
m.queued = registry.gauge(METRICS_MAJC_QUEUED, Tags.of("id", ecm.ceid.canonical()),
127-
new AtomicInteger(0));
128-
m.running = registry.gauge(METRICS_MAJC_RUNNING, Tags.of("id", ecm.ceid.canonical()),
129-
new AtomicInteger(0));
130-
}
131-
return m;
132-
});
140+
ExMetrics exm = exCeMetricsMap.computeIfAbsent(ecm.ceid, id -> new ExMetrics(ecm.ceid));
133141

134142
exm.queued.set(ecm.queued);
135143
exm.running.set(ecm.running);

0 commit comments

Comments
 (0)