Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -24,7 +24,7 @@

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class RLMQuotaMetrics {
public class RLMQuotaMetrics implements AutoCloseable {

private final SensorAccess sensorAccess;
private final Metrics metrics;
Expand All @@ -51,4 +51,9 @@ public Sensor sensor() {
String.format(descriptionFormat, "maximum")), new Max());
});
}

@Override
public void close() {
this.metrics.removeSensor(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ private void removeMetrics() {
metricsGroup.removeMetric(REMOTE_LOG_MANAGER_TASKS_AVG_IDLE_PERCENT_METRIC);
metricsGroup.removeMetric(REMOTE_LOG_READER_FETCH_RATE_AND_TIME_METRIC);
remoteStorageReaderThreadPool.removeMetrics();
Utils.closeQuietly(fetchQuotaMetrics, "fetchQuotaMetrics");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jim0987795064 Could you please add unit test for it? RemoteLogManagerTest would be a good place.

Utils.closeQuietly(copyQuotaMetrics, "copyQuotaMetrics");
}

// Visible for testing
Expand Down Expand Up @@ -2044,17 +2046,18 @@ public void close() {
followerThreadPool.close();
try {
shutdownAndAwaitTermination(remoteStorageReaderThreadPool, "RemoteStorageReaderThreadPool", 10, TimeUnit.SECONDS);

leaderCopyRLMTasks.clear();
leaderExpirationRLMTasks.clear();
followerRLMTasks.clear();

Utils.closeQuietly(indexCache, "RemoteIndexCache");
Utils.closeQuietly(remoteLogMetadataManagerPlugin, "remoteLogMetadataManagerPlugin");
Utils.closeQuietly(remoteStorageManagerPlugin, "remoteStorageManagerPlugin");
closed = true;
} finally {
removeMetrics();
}
leaderCopyRLMTasks.clear();
leaderExpirationRLMTasks.clear();
followerRLMTasks.clear();

Utils.closeQuietly(indexCache, "RemoteIndexCache");
Utils.closeQuietly(remoteLogMetadataManagerPlugin, "remoteLogMetadataManagerPlugin");
Utils.closeQuietly(remoteStorageManagerPlugin, "remoteStorageManagerPlugin");
closed = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

public class RLMQuotaMetricsTest {
private final MockTime time = new MockTime();
Expand All @@ -49,4 +51,22 @@ public void testNewSensorWhenExpired() {
Sensor newSensor = rlmQuotaMetrics.sensor();
assertNotEquals(sensor, newSensor);
}

@Test
public void testClose() {
RLMQuotaMetrics quotaMetrics = new RLMQuotaMetrics(metrics, "metric", "group", "format", 5);

// Register the sensor
quotaMetrics.sensor();
var avg = metrics.metricName("metric" + "-avg", "group", String.format("format", "average"));

// Verify that metrics are created
assertNotNull(metrics.metric(avg));

// Close the quotaMetrics instance
quotaMetrics.close();

// After closing, the metrics should be removed
assertNull(metrics.metric(avg));
}
}