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

szetszwo pushed a commit to branch branch-2
in repository https://gitbox.apache.org/repos/asf/ratis.git


The following commit(s) were added to refs/heads/branch-2 by this push:
     new 0e89ce788 RATIS-1741. Add a removeReporterRegistration method. (#779)
0e89ce788 is described below

commit 0e89ce78803be1e5cc4ecc597adb97c891c95c0a
Author: Sumit Agrawal <[email protected]>
AuthorDate: Fri Nov 11 13:25:23 2022 +0530

    RATIS-1741. Add a removeReporterRegistration method. (#779)
---
 .../org/apache/ratis/metrics/MetricRegistries.java | 11 +++++++++
 .../ratis/metrics/impl/MetricRegistriesImpl.java   |  7 ++++++
 .../ratis/metrics/TestMetricRegistriesLoader.java  | 26 ++++++++++++++++++++++
 3 files changed, 44 insertions(+)

diff --git 
a/ratis-metrics/src/main/java/org/apache/ratis/metrics/MetricRegistries.java 
b/ratis-metrics/src/main/java/org/apache/ratis/metrics/MetricRegistries.java
index 536cdbf56..99bd14af7 100644
--- a/ratis-metrics/src/main/java/org/apache/ratis/metrics/MetricRegistries.java
+++ b/ratis-metrics/src/main/java/org/apache/ratis/metrics/MetricRegistries.java
@@ -93,10 +93,21 @@ public abstract class MetricRegistries {
    * Add hook to register reporter for the metricRegistry.
    *
    * @param reporterRegistration Consumer to create the reporter for the 
registry.
+   * @param stopReporter Consumer to stop the reporter for the registry.
    */
   public abstract void addReporterRegistration(Consumer<RatisMetricRegistry> 
reporterRegistration,
       Consumer<RatisMetricRegistry> stopReporter);
 
+  /**
+   * Remove hook of reporter for the metricRegistry.
+   *
+   * @param reporterRegistration Consumer to create the reporter for the 
registry.
+   * @param stopReporter Consumer to stop the reporter for the registry.
+   */
+  public void removeReporterRegistration(Consumer<RatisMetricRegistry> 
reporterRegistration,
+      Consumer< RatisMetricRegistry> stopReporter) {
+  }
+
   /**
    * Enable jmx reporter for the metricRegistry.
    */
diff --git 
a/ratis-metrics/src/main/java/org/apache/ratis/metrics/impl/MetricRegistriesImpl.java
 
b/ratis-metrics/src/main/java/org/apache/ratis/metrics/impl/MetricRegistriesImpl.java
index cbe8d7bf9..b3fa8ce53 100644
--- 
a/ratis-metrics/src/main/java/org/apache/ratis/metrics/impl/MetricRegistriesImpl.java
+++ 
b/ratis-metrics/src/main/java/org/apache/ratis/metrics/impl/MetricRegistriesImpl.java
@@ -115,6 +115,13 @@ public class MetricRegistriesImpl extends MetricRegistries 
{
     this.stopReporters.add(stopReporter);
   }
 
+  @Override
+  public void removeReporterRegistration(Consumer<RatisMetricRegistry> 
reporterRegistration,
+      Consumer<RatisMetricRegistry> stopReporter) {
+    this.reporterRegistrations.remove(reporterRegistration);
+    this.stopReporters.remove(stopReporter);
+  }
+
   @Override
   public void enableJmxReporter() {
     addReporterRegistration(
diff --git 
a/ratis-metrics/src/test/java/org/apache/ratis/metrics/TestMetricRegistriesLoader.java
 
b/ratis-metrics/src/test/java/org/apache/ratis/metrics/TestMetricRegistriesLoader.java
index 8429ded13..e5759bb4e 100644
--- 
a/ratis-metrics/src/test/java/org/apache/ratis/metrics/TestMetricRegistriesLoader.java
+++ 
b/ratis-metrics/src/test/java/org/apache/ratis/metrics/TestMetricRegistriesLoader.java
@@ -21,7 +21,10 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
 import static org.mockito.Mockito.mock;
 
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Consumer;
 import org.apache.ratis.thirdparty.com.google.common.collect.Lists;
+import org.junit.Assert;
 import org.junit.Test;
 
 
@@ -51,4 +54,27 @@ public class TestMetricRegistriesLoader {
     assertNotEquals(loader2, instance);
     assertNotEquals(loader3, instance);
   }
+
+  @Test
+  public void testAddRemoveReporter() {
+    final AtomicLong cntr = new AtomicLong(0L);
+    final MetricRegistries r = MetricRegistries.global();
+    Consumer<RatisMetricRegistry> reporter = v-> cntr.incrementAndGet();
+    Consumer<RatisMetricRegistry> stopReporter = v-> cntr.incrementAndGet();
+    r.addReporterRegistration(reporter, stopReporter);
+
+    // check if add and remove of metric do reporting counter increase
+    MetricRegistryInfo info = new MetricRegistryInfo("t1", "t1", "t1", "t1");
+    r.create(info);
+    Assert.assertTrue(cntr.get() == 1);
+    r.remove(info);
+    Assert.assertTrue(cntr.get() == 2);
+
+    // after removal, add and remove of metric must not do any increase
+    r.removeReporterRegistration(reporter, stopReporter);
+    r.create(info);
+    Assert.assertTrue(cntr.get() == 2);
+    r.remove(info);
+    Assert.assertTrue(cntr.get() == 2);
+  }
 }

Reply via email to