From 8ee4d1a9d0ba1e05e1b5c227778fd26ca961ce09 Mon Sep 17 00:00:00 2001 From: GuojunLi Date: Thu, 28 Sep 2023 13:45:47 +0800 Subject: [PATCH] [core] Support remove metric groups from metric instance (#2062) * [core] Support remove metric groups from metric instance --- .../paimon/metrics/AbstractMetricGroup.java | 1 + .../org/apache/paimon/metrics/Metrics.java | 5 +++++ .../paimon/metrics/MetricGroupTest.java | 19 +++++++++++-------- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/paimon-core/src/main/java/org/apache/paimon/metrics/AbstractMetricGroup.java b/paimon-core/src/main/java/org/apache/paimon/metrics/AbstractMetricGroup.java index c4df417c9fc4..78f293a50e05 100644 --- a/paimon-core/src/main/java/org/apache/paimon/metrics/AbstractMetricGroup.java +++ b/paimon-core/src/main/java/org/apache/paimon/metrics/AbstractMetricGroup.java @@ -183,6 +183,7 @@ public void close() { if (!closed) { closed = true; metrics.clear(); + Metrics.getInstance().removeGroup(this); } } diff --git a/paimon-core/src/main/java/org/apache/paimon/metrics/Metrics.java b/paimon-core/src/main/java/org/apache/paimon/metrics/Metrics.java index 6161d528389c..6ef28749bb61 100644 --- a/paimon-core/src/main/java/org/apache/paimon/metrics/Metrics.java +++ b/paimon-core/src/main/java/org/apache/paimon/metrics/Metrics.java @@ -44,6 +44,11 @@ public void addGroup(AbstractMetricGroup group) { metricGroups.add(group); } + /** Remove a metric group. Called when closing the corresponding instances, like committer. */ + public void removeGroup(AbstractMetricGroup group) { + metricGroups.remove(group); + } + /** Get metric groups. */ public ConcurrentLinkedQueue getMetricGroups() { return metricGroups; diff --git a/paimon-core/src/test/java/org/apache/paimon/metrics/MetricGroupTest.java b/paimon-core/src/test/java/org/apache/paimon/metrics/MetricGroupTest.java index 054e1f458f58..24f903de9b1a 100644 --- a/paimon-core/src/test/java/org/apache/paimon/metrics/MetricGroupTest.java +++ b/paimon-core/src/test/java/org/apache/paimon/metrics/MetricGroupTest.java @@ -34,14 +34,7 @@ public void testGroupRegisterMetrics() { assertThat(group.isClosed()).isFalse(); // these will fail is the registration is propagated group.counter("testcounter"); - group.gauge( - "testgauge", - new Gauge() { - @Override - public Object getValue() { - return null; - } - }); + group.gauge("testgauge", () -> null); assertThat(group.getGroupName()).isEqualTo("commit"); assertThat(group.getAllTags().size()).isEqualTo(1); assertThat(group.getAllTags()) @@ -65,5 +58,15 @@ public void testTolerateMetricNameCollisions() { // return the old one with the metric name collision assertThat(group.counter(name)).isSameAs(counter1); + group.close(); + } + + @Test + public void testAddAndRemoveMetricGroups() { + AbstractMetricGroup metricGroup = + GenericMetricGroup.createGenericMetricGroup("myTable", "commit"); + assertThat(Metrics.getInstance().getMetricGroups()).containsExactly(metricGroup); + metricGroup.close(); + assertThat(Metrics.getInstance().getMetricGroups()).isEmpty(); } }