This is an automated email from the ASF dual-hosted git repository.

bbeaudreault pushed a commit to branch branch-2.5
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2.5 by this push:
     new 9ab35a0ef6e HBASE-28327 Add remove(String key, Metric metric) method 
to MetricRegistry interface (#5647)
9ab35a0ef6e is described below

commit 9ab35a0ef6e6052a4a36ee1d8f1fc4a273131997
Author: eab148 <[email protected]>
AuthorDate: Thu Jan 25 08:17:25 2024 -0500

    HBASE-28327 Add remove(String key, Metric metric) method to MetricRegistry 
interface (#5647)
    
    Co-authored-by: Evie Boland <[email protected]>
    Signed-off-by: Bryan Beaudreault <[email protected]>
---
 .../apache/hadoop/hbase/metrics/MetricRegistry.java   |  8 ++++++++
 .../hadoop/hbase/metrics/impl/MetricRegistryImpl.java |  5 +++++
 .../hbase/metrics/impl/TestMetricRegistryImpl.java    | 19 +++++++++++++++++++
 3 files changed, 32 insertions(+)

diff --git 
a/hbase-metrics-api/src/main/java/org/apache/hadoop/hbase/metrics/MetricRegistry.java
 
b/hbase-metrics-api/src/main/java/org/apache/hadoop/hbase/metrics/MetricRegistry.java
index b70526e1c5a..96f4b313d79 100644
--- 
a/hbase-metrics-api/src/main/java/org/apache/hadoop/hbase/metrics/MetricRegistry.java
+++ 
b/hbase-metrics-api/src/main/java/org/apache/hadoop/hbase/metrics/MetricRegistry.java
@@ -96,6 +96,14 @@ public interface MetricRegistry extends MetricSet {
    */
   boolean remove(String name);
 
+  /**
+   * Removes the metric with the given name only if it is registered to the 
provided metric.
+   * @param name   the name of the metric
+   * @param metric the metric expected to be registered to the given name
+   * @return true if the metric is removed.
+   */
+  boolean remove(String name, Metric metric);
+
   /**
    * Return the MetricRegistryInfo object for this registry.
    * @return MetricRegistryInfo describing the registry.
diff --git 
a/hbase-metrics/src/main/java/org/apache/hadoop/hbase/metrics/impl/MetricRegistryImpl.java
 
b/hbase-metrics/src/main/java/org/apache/hadoop/hbase/metrics/impl/MetricRegistryImpl.java
index 0ecd707b481..10e579b6737 100644
--- 
a/hbase-metrics/src/main/java/org/apache/hadoop/hbase/metrics/impl/MetricRegistryImpl.java
+++ 
b/hbase-metrics/src/main/java/org/apache/hadoop/hbase/metrics/impl/MetricRegistryImpl.java
@@ -114,6 +114,11 @@ public class MetricRegistryImpl implements MetricRegistry {
     return metrics.remove(name) != null;
   }
 
+  @Override
+  public boolean remove(String name, Metric metric) {
+    return metrics.remove(name, metric);
+  }
+
   @Override
   public MetricRegistryInfo getMetricRegistryInfo() {
     return info;
diff --git 
a/hbase-metrics/src/test/java/org/apache/hadoop/hbase/metrics/impl/TestMetricRegistryImpl.java
 
b/hbase-metrics/src/test/java/org/apache/hadoop/hbase/metrics/impl/TestMetricRegistryImpl.java
index 56b3f0d6a9e..8cae0636120 100644
--- 
a/hbase-metrics/src/test/java/org/apache/hadoop/hbase/metrics/impl/TestMetricRegistryImpl.java
+++ 
b/hbase-metrics/src/test/java/org/apache/hadoop/hbase/metrics/impl/TestMetricRegistryImpl.java
@@ -18,6 +18,7 @@
 package org.apache.hadoop.hbase.metrics.impl;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
@@ -141,4 +142,22 @@ public class TestMetricRegistryImpl {
     assertEquals(gauge, metrics.get("mygauge"));
     assertEquals(timer, metrics.get("mytimer"));
   }
+
+  @Test
+  public void testRemove() {
+    CounterImpl counter1 = new CounterImpl();
+    CounterImpl counter2 = new CounterImpl();
+    registry.register("mycounter", counter1);
+
+    boolean removed = registry.remove("mycounter", counter2);
+    Optional<Metric> metric = registry.get("mycounter");
+    assertFalse(removed);
+    assertTrue(metric.isPresent());
+    assertEquals(metric.get(), counter1);
+
+    removed = registry.remove("mycounter");
+    metric = registry.get("mycounter");
+    assertTrue(removed);
+    assertFalse(metric.isPresent());
+  }
 }

Reply via email to