Skip to content

Commit eba9839

Browse files
MINOR: Remove fetchQuotaMetrics and copyQuotaMetrics on close (#20394)
- Changes: Remove fetchQuotaMetrics and copyQuotaMetrics in RemoteLogManager on close from: #20342 (comment) Reviewers: Kamal Chandraprakash <[email protected]>
1 parent 511818e commit eba9839

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

storage/src/main/java/org/apache/kafka/server/log/remote/quota/RLMQuotaMetrics.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import java.util.concurrent.locks.ReentrantReadWriteLock;
2626

27-
public class RLMQuotaMetrics {
27+
public class RLMQuotaMetrics implements AutoCloseable {
2828

2929
private final SensorAccess sensorAccess;
3030
private final Metrics metrics;
@@ -51,4 +51,9 @@ public Sensor sensor() {
5151
String.format(descriptionFormat, "maximum")), new Max());
5252
});
5353
}
54+
55+
@Override
56+
public void close() {
57+
this.metrics.removeSensor(name);
58+
}
5459
}

storage/src/main/java/org/apache/kafka/server/log/remote/storage/RemoteLogManager.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ private void removeMetrics() {
314314
metricsGroup.removeMetric(REMOTE_LOG_MANAGER_TASKS_AVG_IDLE_PERCENT_METRIC);
315315
metricsGroup.removeMetric(REMOTE_LOG_READER_FETCH_RATE_AND_TIME_METRIC);
316316
remoteStorageReaderThreadPool.removeMetrics();
317+
Utils.closeQuietly(fetchQuotaMetrics, "fetchQuotaMetrics");
318+
Utils.closeQuietly(copyQuotaMetrics, "copyQuotaMetrics");
317319
}
318320

319321
// Visible for testing
@@ -2044,17 +2046,18 @@ public void close() {
20442046
followerThreadPool.close();
20452047
try {
20462048
shutdownAndAwaitTermination(remoteStorageReaderThreadPool, "RemoteStorageReaderThreadPool", 10, TimeUnit.SECONDS);
2049+
2050+
leaderCopyRLMTasks.clear();
2051+
leaderExpirationRLMTasks.clear();
2052+
followerRLMTasks.clear();
2053+
2054+
Utils.closeQuietly(indexCache, "RemoteIndexCache");
2055+
Utils.closeQuietly(remoteLogMetadataManagerPlugin, "remoteLogMetadataManagerPlugin");
2056+
Utils.closeQuietly(remoteStorageManagerPlugin, "remoteStorageManagerPlugin");
2057+
closed = true;
20472058
} finally {
20482059
removeMetrics();
20492060
}
2050-
leaderCopyRLMTasks.clear();
2051-
leaderExpirationRLMTasks.clear();
2052-
followerRLMTasks.clear();
2053-
2054-
Utils.closeQuietly(indexCache, "RemoteIndexCache");
2055-
Utils.closeQuietly(remoteLogMetadataManagerPlugin, "remoteLogMetadataManagerPlugin");
2056-
Utils.closeQuietly(remoteStorageManagerPlugin, "remoteStorageManagerPlugin");
2057-
closed = true;
20582061
}
20592062
}
20602063
}

storage/src/test/java/org/apache/kafka/server/log/remote/quota/RLMQuotaMetricsTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import static org.junit.jupiter.api.Assertions.assertEquals;
3030
import static org.junit.jupiter.api.Assertions.assertNotEquals;
31+
import static org.junit.jupiter.api.Assertions.assertNotNull;
32+
import static org.junit.jupiter.api.Assertions.assertNull;
3133

3234
public class RLMQuotaMetricsTest {
3335
private final MockTime time = new MockTime();
@@ -49,4 +51,22 @@ public void testNewSensorWhenExpired() {
4951
Sensor newSensor = rlmQuotaMetrics.sensor();
5052
assertNotEquals(sensor, newSensor);
5153
}
54+
55+
@Test
56+
public void testClose() {
57+
RLMQuotaMetrics quotaMetrics = new RLMQuotaMetrics(metrics, "metric", "group", "format", 5);
58+
59+
// Register the sensor
60+
quotaMetrics.sensor();
61+
var avg = metrics.metricName("metric" + "-avg", "group", String.format("format", "average"));
62+
63+
// Verify that metrics are created
64+
assertNotNull(metrics.metric(avg));
65+
66+
// Close the quotaMetrics instance
67+
quotaMetrics.close();
68+
69+
// After closing, the metrics should be removed
70+
assertNull(metrics.metric(avg));
71+
}
5272
}

0 commit comments

Comments
 (0)