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

szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 9077cb83d1 HDDS-7473. Ratis integration for support of remove 
registeration (#4112)
9077cb83d1 is described below

commit 9077cb83d1db925e9ad6d973d82dde204f18b413
Author: Sumit Agrawal <[email protected]>
AuthorDate: Wed Dec 21 11:46:12 2022 +0530

    HDDS-7473. Ratis integration for support of remove registeration (#4112)
---
 .../apache/hadoop/ozone/HddsDatanodeService.java   |  7 +--
 .../hdds/server/http/RatisDropwizardExports.java   | 59 ++++++++++++++++++----
 .../hdds/scm/server/StorageContainerManager.java   |  9 ++--
 .../org/apache/hadoop/ozone/om/OzoneManager.java   |  6 ++-
 4 files changed, 63 insertions(+), 18 deletions(-)

diff --git 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/HddsDatanodeService.java
 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/HddsDatanodeService.java
index 2de1bde537..f9d39f9445 100644
--- 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/HddsDatanodeService.java
+++ 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/HddsDatanodeService.java
@@ -110,6 +110,7 @@ public class HddsDatanodeService extends GenericCli 
implements ServicePlugin {
   private final AtomicBoolean isStopped = new AtomicBoolean(false);
   private final Map<String, RatisDropwizardExports> ratisMetricsMap =
       new ConcurrentHashMap<>();
+  private List<RatisDropwizardExports.MetricReporter> ratisReporterList = null;
   private DNMXBeanImpl serviceRuntimeInfo =
       new DNMXBeanImpl(HddsVersionInfo.HDDS_VERSION_INFO) { };
   private ObjectName dnInfoBeanName;
@@ -212,8 +213,8 @@ public class HddsDatanodeService extends GenericCli 
implements ServicePlugin {
   public void start() {
     serviceRuntimeInfo.setStartTime();
 
-    RatisDropwizardExports.
-        registerRatisMetricReporters(ratisMetricsMap, () -> isStopped.get());
+    ratisReporterList = RatisDropwizardExports
+        .registerRatisMetricReporters(ratisMetricsMap, () -> isStopped.get());
 
     OzoneConfiguration.activate();
     HddsServerUtil.initializeMetrics(conf, "HddsDatanode");
@@ -589,7 +590,7 @@ public class HddsDatanodeService extends GenericCli 
implements ServicePlugin {
       } catch (Exception ex) {
         LOG.error("Datanode CRL store stop failed", ex);
       }
-      RatisDropwizardExports.clear(ratisMetricsMap);
+      RatisDropwizardExports.clear(ratisMetricsMap, ratisReporterList);
     }
   }
 
diff --git 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/http/RatisDropwizardExports.java
 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/http/RatisDropwizardExports.java
index 50d840a7ad..834c7d80a5 100644
--- 
a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/http/RatisDropwizardExports.java
+++ 
b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/http/RatisDropwizardExports.java
@@ -22,7 +22,10 @@ import io.prometheus.client.Collector;
 import io.prometheus.client.CollectorRegistry;
 import io.prometheus.client.dropwizard.DropwizardExports;
 import io.prometheus.client.dropwizard.samplebuilder.DefaultSampleBuilder;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.function.BooleanSupplier;
+import java.util.function.Consumer;
 import org.apache.ratis.metrics.MetricRegistries;
 import org.apache.ratis.metrics.MetricsReporting;
 import org.apache.ratis.metrics.RatisMetricRegistry;
@@ -43,21 +46,29 @@ public class RatisDropwizardExports extends 
DropwizardExports {
     super(registry, new RatisNameRewriteSampleBuilder());
   }
 
-  public static void registerRatisMetricReporters(
+  public static List<MetricReporter> registerRatisMetricReporters(
       Map<String, RatisDropwizardExports> ratisMetricsMap,
       BooleanSupplier checkStopped) {
     //All the Ratis metrics (registered from now) will be published via JMX and
     //via the prometheus exporter (used by the /prom servlet
-    MetricRegistries.global()
-        .addReporterRegistration(MetricsReporting.jmxReporter(),
-            MetricsReporting.stopJmxReporter());
-    MetricRegistries.global().addReporterRegistration(
-        r1 -> registerDropwizard(r1, ratisMetricsMap, checkStopped),
-        r2 -> deregisterDropwizard(r2, ratisMetricsMap));
+    List<MetricReporter> ratisReporterList = new ArrayList<>();
+    ratisReporterList.add(new MetricReporter(MetricsReporting.jmxReporter(),
+        MetricsReporting.stopJmxReporter()));
+    Consumer<RatisMetricRegistry> reporter
+        = r1 -> registerDropwizard(r1, ratisMetricsMap, checkStopped);
+    Consumer<RatisMetricRegistry> stopper
+        = r2 -> deregisterDropwizard(r2, ratisMetricsMap);
+    ratisReporterList.add(new MetricReporter(reporter, stopper));
+    
+    for (MetricReporter metricReporter : ratisReporterList) {
+      metricReporter.addToGlobalRegistration();
+    }
+    return ratisReporterList;
   }
 
-  public static void clear(Map<String, RatisDropwizardExports>
-                               ratisMetricsMap) {
+  public static void clear(
+      Map<String, RatisDropwizardExports> ratisMetricsMap,
+      List<MetricReporter> ratisReporterList) {
     ratisMetricsMap.entrySet().stream().forEach(e -> {
       // remove and deregister from registry only one registered
       // as unregistered element if performed unregister again will
@@ -67,6 +78,13 @@ public class RatisDropwizardExports extends 
DropwizardExports {
         CollectorRegistry.defaultRegistry.unregister(c);
       }
     });
+    
+    if (null != ratisReporterList) {
+      for (MetricReporter metricReporter : ratisReporterList) {
+        metricReporter.removeFromGlobalRegistration();
+      }
+    }
+    
     MetricRegistries.global().clear();
   }
 
@@ -94,4 +112,27 @@ public class RatisDropwizardExports extends 
DropwizardExports {
       CollectorRegistry.defaultRegistry.unregister(c);
     }
   }
+
+  /**
+   * class for keeping track of reporters and add/remove to registry.
+   * 
+   */
+  public static class MetricReporter {
+    private final Consumer<RatisMetricRegistry> reporter;
+    private final Consumer<RatisMetricRegistry> stopper;
+
+    MetricReporter(Consumer<RatisMetricRegistry> reporter,
+                        Consumer<RatisMetricRegistry> stopper) {
+      this.reporter = reporter;
+      this.stopper = stopper;
+    }
+
+    void addToGlobalRegistration() {
+      MetricRegistries.global().addReporterRegistration(reporter, stopper);
+    }
+
+    void removeFromGlobalRegistration() {
+      MetricRegistries.global().removeReporterRegistration(reporter, stopper);
+    }
+  }
 }
\ No newline at end of file
diff --git 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/StorageContainerManager.java
 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/StorageContainerManager.java
index e053076488..5def8ba48a 100644
--- 
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/StorageContainerManager.java
+++ 
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/server/StorageContainerManager.java
@@ -267,6 +267,7 @@ public final class StorageContainerManager extends 
ServiceRuntimeInfoImpl
   private MetricsSystem ms;
   private final Map<String, RatisDropwizardExports> ratisMetricsMap =
       new ConcurrentHashMap<>();
+  private List<RatisDropwizardExports.MetricReporter> ratisReporterList = null;
   private String primaryScmNodeId;
 
   /**
@@ -284,7 +285,7 @@ public final class StorageContainerManager extends 
ServiceRuntimeInfoImpl
   // container replicas.
   private ContainerReplicaPendingOps containerReplicaPendingOps;
   private final AtomicBoolean isStopped = new AtomicBoolean(false);
-  
+
   /**
    * Creates a new StorageContainerManager. Configuration will be
    * updated with information on the actual listening addresses used
@@ -593,8 +594,8 @@ public final class StorageContainerManager extends 
ServiceRuntimeInfoImpl
       clusterMap = new NetworkTopologyImpl(conf);
     }
     // This needs to be done before initializing Ratis.
-    RatisDropwizardExports.registerRatisMetricReporters(ratisMetricsMap,
-        () -> isStopped.get());
+    ratisReporterList = RatisDropwizardExports
+        .registerRatisMetricReporters(ratisMetricsMap, () -> isStopped.get());
     if (configurator.getSCMHAManager() != null) {
       scmHAManager = configurator.getSCMHAManager();
     } else {
@@ -1620,7 +1621,7 @@ public final class StorageContainerManager extends 
ServiceRuntimeInfoImpl
 
     scmSafeModeManager.stop();
     serviceManager.stop();
-    RatisDropwizardExports.clear(ratisMetricsMap);
+    RatisDropwizardExports.clear(ratisMetricsMap, ratisReporterList);
 
     try {
       LOG.info("Stopping SCM MetadataStore.");
diff --git 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java
 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java
index d6755e03f0..ed3dc058b1 100644
--- 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java
+++ 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OzoneManager.java
@@ -282,6 +282,7 @@ import static 
org.apache.hadoop.ozone.om.ratis.OzoneManagerRatisServer.getRaftGr
 import static 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerInterServiceProtocolProtos.OzoneManagerInterService;
 import static 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OzoneManagerService;
 import static 
org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.PrepareStatusResponse.PrepareStatus;
+
 import org.apache.ratis.proto.RaftProtos.RaftPeerRole;
 import org.apache.ratis.protocol.RaftGroupId;
 import org.apache.ratis.protocol.RaftPeer;
@@ -379,6 +380,7 @@ public final class OzoneManager extends 
ServiceRuntimeInfoImpl
   private final RatisSnapshotInfo omRatisSnapshotInfo;
   private final Map<String, RatisDropwizardExports> ratisMetricsMap =
       new ConcurrentHashMap<>();
+  private List<RatisDropwizardExports.MetricReporter> ratisReporterList = null;
 
   private KeyProviderCryptoExtension kmsProvider = null;
   private static String keyProviderUriKeyName =
@@ -1968,7 +1970,7 @@ public final class OzoneManager extends 
ServiceRuntimeInfoImpl
     if (isRatisEnabled) {
       if (omRatisServer == null) {
         // This needs to be done before initializing Ratis.
-        RatisDropwizardExports.
+        ratisReporterList = RatisDropwizardExports.
             registerRatisMetricReporters(ratisMetricsMap, () -> isStopped());
         omRatisServer = OzoneManagerRatisServer.newOMRatisServer(
             configuration, this, omNodeDetails, peerNodesMap,
@@ -2082,7 +2084,7 @@ public final class OzoneManager extends 
ServiceRuntimeInfoImpl
         omSnapshotProvider.stop();
       }
       OMPerformanceMetrics.unregister();
-      RatisDropwizardExports.clear(ratisMetricsMap);
+      RatisDropwizardExports.clear(ratisMetricsMap, ratisReporterList);
       scmClient.close();
     } catch (Exception e) {
       LOG.error("OzoneManager stop failed.", e);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to