leesf commented on a change in pull request #1106: [HUDI-209] Implement JMX 
metrics reporter
URL: https://github.com/apache/incubator-hudi/pull/1106#discussion_r360676268
 
 

 ##########
 File path: 
hudi-client/src/main/java/org/apache/hudi/metrics/JmxMetricsReporter.java
 ##########
 @@ -18,47 +18,68 @@
 
 package org.apache.hudi.metrics;
 
+import org.apache.hudi.client.utils.NetUtils;
 import org.apache.hudi.config.HoodieWriteConfig;
 import org.apache.hudi.exception.HoodieException;
 
+import com.codahale.metrics.MetricRegistry;
+import com.codahale.metrics.jmx.JmxReporter;
 import com.google.common.base.Preconditions;
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
 
+import javax.management.MBeanServer;
 import javax.management.remote.JMXConnectorServer;
 import javax.management.remote.JMXConnectorServerFactory;
 import javax.management.remote.JMXServiceURL;
 
 import java.io.Closeable;
 import java.lang.management.ManagementFactory;
 import java.rmi.registry.LocateRegistry;
+import java.util.Iterator;
 
 /**
  * Implementation of Jmx reporter, which used to report jmx metric.
  */
 public class JmxMetricsReporter extends MetricsReporter {
-
   private static final Logger LOG = 
LogManager.getLogger(JmxMetricsReporter.class);
   private final JMXConnectorServer connector;
+  private final JmxReporter reporter;
   private String host;
-  private int port;
 
-  public JmxMetricsReporter(HoodieWriteConfig config) {
+  public JmxMetricsReporter(HoodieWriteConfig config, MetricRegistry registry) 
{
     try {
       // Check the host and port here
       this.host = config.getJmxHost();
-      this.port = config.getJmxPort();
-      if (host == null || port == 0) {
+      String portsConfig = config.getJmxPorts();
+      if (host == null || portsConfig == null) {
         throw new RuntimeException(
             String.format("Jmx cannot be initialized with host[%s] and 
port[%s].",
-                host, port));
+                host, portsConfig));
+      }
+
+      Iterator<Integer> ports = NetUtils.getPortRangeFromString(portsConfig);
+      JMXConnectorServer successConnectorServer = null;
+      JmxReporter successReporter = null;
+      while (ports.hasNext() && successConnectorServer == null) {
+        int port = ports.next();
+        LocateRegistry.createRegistry(port);
+        String serviceUrl =
+            "service:jmx:rmi://" + host + ":" + port + "/jndi/rmi://" + host + 
":" + port
+                + "/jmxrmi";
+        JMXServiceURL url = new JMXServiceURL(serviceUrl);
+        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
+        successConnectorServer = JMXConnectorServerFactory
+            .newJMXConnectorServer(url, null, mBeanServer);
+        successReporter = 
JmxReporter.forRegistry(registry).registerWith(mBeanServer).build();
+      }
+      if (successConnectorServer == null || successReporter == null) {
+        throw new RuntimeException(
 
 Review comment:
   I think the `reporter` and `LocateRegistry.createRegistry` should be closed 
to release the resources, thus to say we need add `close` method in 
`MetrcsReporter`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to