All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hudi.metrics.JmxMetricsReporter Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hudi.metrics;

import org.apache.hudi.config.metrics.HoodieMetricsConfig;
import org.apache.hudi.exception.HoodieException;

import com.codahale.metrics.MetricRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.MBeanServer;

import java.lang.management.ManagementFactory;
import java.rmi.server.ExportException;
import java.util.Objects;
import java.util.stream.IntStream;

/**
 * Implementation of Jmx reporter, which used to report jmx metric.
 */
public class JmxMetricsReporter extends MetricsReporter {

  private static final Logger LOG = LoggerFactory.getLogger(JmxMetricsReporter.class);

  private final MetricRegistry registry;
  private JmxReporterServer jmxReporterServer;

  public JmxMetricsReporter(HoodieMetricsConfig config, MetricRegistry registry) {
    try {
      this.registry = registry;
      // Check the host and port here
      String host = config.getJmxHost();
      String portsConfig = config.getJmxPort();
      if (host == null || portsConfig == null) {
        throw new HoodieException(
            String.format("Jmx cannot be initialized with host[%s] and port[%s].",
                host, portsConfig));
      }
      int[] ports = getPortRangeFromString(portsConfig);
      initializeJmxReporterServer(host, ports);

      boolean successfullyStartedServer = isServerCreated();
      if (!successfullyStartedServer) {
        throw new HoodieException(
            "Could not start JMX server on any configured port. Ports: " + portsConfig
                + ". Maybe require port range for multiple hoodie tables");
      }
      LOG.info("Configured JMXReporter with {port:" + portsConfig + "}");
    } catch (Exception e) {
      String msg = "Jmx initialize failed: ";
      LOG.error(msg, e);
      throw new HoodieException(msg, e);
    }
  }

  private boolean isServerCreated() {
    return jmxReporterServer != null;
  }

  private void initializeJmxReporterServer(String host, int[] ports) {
    for (int port : ports) {
      try {
        jmxReporterServer = createJmxReport(host, port);
        LOG.info("Started JMX server on port " + port + ".");
        break;
      } catch (Exception e) {
        if (e.getCause() instanceof ExportException) {
          LOG.info("Skip for initializing jmx port " + port + " because of already in use");
        } else {
          LOG.info("Failed to initialize jmx port " + port + ". " + e.getMessage());
        }
      }
    }
  }

  @Override
  public void start() {
    if (isServerCreated()) {
      jmxReporterServer.start();
    } else {
      LOG.error("Cannot start as the jmxReporter is null.");
    }
  }

  @Override
  public void report() {
  }

  @Override
  public void stop() {
    Objects.requireNonNull(jmxReporterServer, "jmxReporterServer is not running.");
    try {
      jmxReporterServer.stop();
    } catch (Exception e) {
      throw new HoodieException("Stop jmxReporterServer fail", e);
    }
  }

  private JmxReporterServer createJmxReport(String host, int port) {
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    return JmxReporterServer.forRegistry(registry)
        .host(host)
        .port(port)
        .registerWith(mBeanServer)
        .build();
  }

  private int[] getPortRangeFromString(String portsConfig) {
    String range = portsConfig.trim();
    int dashIdx = range.indexOf('-');
    final int start;
    final int end;
    if (dashIdx == -1) {
      start = Integer.parseInt(range);
      end = Integer.parseInt(range);
    } else {
      start = Integer.parseInt(range.substring(0, dashIdx));
      end = Integer.parseInt(range.substring(dashIdx + 1));
    }
    return IntStream.rangeClosed(start, end)
        .filter(port -> (0 <= port && port <= 65535))
        .toArray();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy