001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.metrics.impl;
019
020import java.util.Collection;
021import java.util.HashMap;
022import java.util.Iterator;
023import java.util.Map.Entry;
024import java.util.Optional;
025import java.util.concurrent.TimeUnit;
026import java.util.concurrent.atomic.AtomicBoolean;
027import org.apache.hadoop.hbase.metrics.MetricRegistries;
028import org.apache.hadoop.hbase.metrics.MetricRegistry;
029import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
030import org.apache.hadoop.metrics2.MetricsCollector;
031import org.apache.hadoop.metrics2.MetricsExecutor;
032import org.apache.hadoop.metrics2.MetricsSource;
033import org.apache.hadoop.metrics2.impl.JmxCacheBuster;
034import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
035import org.apache.hadoop.metrics2.lib.DefaultMetricsSystemHelper;
036import org.apache.hadoop.metrics2.lib.MetricsExecutorImpl;
037import org.apache.yetus.audience.InterfaceAudience;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041/**
042 * This class acts as an adapter to export the MetricRegistry's in the global registry. Each
043 * MetricRegistry will be registered or unregistered from the metric2 system. The collection will
044 * be performed via the MetricsSourceAdapter and the MetricRegistry will collected like a
045 * BaseSource instance for a group of metrics  (like WAL, RPC, etc) with the MetricRegistryInfo's
046 * JMX context.
047 *
048 * <p>Developer note:
049 * Unlike the current metrics2 based approach, the new metrics approach
050 * (hbase-metrics-api and hbase-metrics modules) work by having different MetricRegistries that are
051 * initialized and used from the code that lives in their respective modules (hbase-server, etc).
052 * There is no need to define BaseSource classes and do a lot of indirection. The MetricRegistry'es
053 * will be in the global MetricRegistriesImpl, and this class will iterate over
054 * MetricRegistries.global() and register adapters to the metrics2 subsystem. These adapters then
055 * report the actual values by delegating to
056 * {@link HBaseMetrics2HadoopMetricsAdapter#snapshotAllMetrics(MetricRegistry, MetricsCollector)}.
057 *
058 * We do not initialize the Hadoop Metrics2 system assuming that other BaseSources already do so
059 * (see BaseSourceImpl). Once the last BaseSource is moved to the new system, the metric2
060 * initialization should be moved here.
061 * </p>
062 */
063@InterfaceAudience.Private
064public final class GlobalMetricRegistriesAdapter {
065
066  private static final Logger LOG = LoggerFactory.getLogger(GlobalMetricRegistriesAdapter.class);
067
068  private class MetricsSourceAdapter implements MetricsSource {
069    private final MetricRegistry registry;
070    MetricsSourceAdapter(MetricRegistry registry) {
071      this.registry = registry;
072    }
073
074    @Override
075    public void getMetrics(MetricsCollector collector, boolean all) {
076      metricsAdapter.snapshotAllMetrics(registry, collector);
077    }
078  }
079
080  private final MetricsExecutor executor;
081  private final AtomicBoolean stopped;
082  private final DefaultMetricsSystemHelper helper;
083  private final HBaseMetrics2HadoopMetricsAdapter metricsAdapter;
084  private final HashMap<MetricRegistryInfo, MetricsSourceAdapter> registeredSources;
085
086  private GlobalMetricRegistriesAdapter() {
087    this.executor = new MetricsExecutorImpl();
088    this.stopped = new AtomicBoolean(false);
089    this.metricsAdapter = new HBaseMetrics2HadoopMetricsAdapter();
090    this.registeredSources = new HashMap<>();
091    this.helper = new DefaultMetricsSystemHelper();
092    executor.getExecutor().scheduleAtFixedRate(() -> this.doRun(), 10, 10, TimeUnit.SECONDS);
093  }
094
095  /**
096   * Make sure that this global MetricSource for hbase-metrics module based metrics are initialized.
097   * This should be called only once.
098   */
099  public static GlobalMetricRegistriesAdapter init() {
100    return new GlobalMetricRegistriesAdapter();
101  }
102
103  public void stop() {
104    stopped.set(true);
105  }
106
107  private void doRun() {
108    if (stopped.get()) {
109      executor.stop();
110      return;
111    }
112    if (LOG.isTraceEnabled()) {
113      LOG.trace("doRun called: " + registeredSources);
114    }
115
116    Collection<MetricRegistry> registries = MetricRegistries.global().getMetricRegistries();
117    for (MetricRegistry registry : registries) {
118      MetricRegistryInfo info = registry.getMetricRegistryInfo();
119
120      if (info.isExistingSource()) {
121        // If there is an already existing BaseSource for this MetricRegistry, skip it here. These
122        // types of registries are there only due to existing BaseSource implementations in the
123        // source code (like MetricsRegionServer, etc). This is to make sure that we can transition
124        // iteratively to the new hbase-metrics system. These type of MetricRegistry metrics will be
125        // exported from the BaseSource.getMetrics() call directly because there is already a
126        // MetricRecordBuilder there (see MetricsRegionServerSourceImpl).
127        continue;
128      }
129
130      if (!registeredSources.containsKey(info)) {
131        if (LOG.isDebugEnabled()) {
132          LOG.debug("Registering adapter for the MetricRegistry: " + info.getMetricsJmxContext());
133        }
134        // register this as a MetricSource under different JMX Context'es.
135        MetricsSourceAdapter adapter = new MetricsSourceAdapter(registry);
136        LOG.info("Registering " + info.getMetricsJmxContext() + " " + info.getMetricsDescription());
137        DefaultMetricsSystem.instance().register(info.getMetricsJmxContext(),
138            info.getMetricsDescription(), adapter);
139        registeredSources.put(info, adapter);
140        // next collection will collect the newly registered MetricSource. Doing this here leads to
141        // ConcurrentModificationException.
142      }
143    }
144
145    boolean removed = false;
146    // Remove registered sources if it is removed from the global registry
147    for (Iterator<Entry<MetricRegistryInfo, MetricsSourceAdapter>> it =
148         registeredSources.entrySet().iterator(); it.hasNext();) {
149      Entry<MetricRegistryInfo, MetricsSourceAdapter> entry = it.next();
150      MetricRegistryInfo info = entry.getKey();
151      Optional<MetricRegistry> found = MetricRegistries.global().get(info);
152      if (!found.isPresent()) {
153        if (LOG.isDebugEnabled()) {
154          LOG.debug("Removing adapter for the MetricRegistry: " + info.getMetricsJmxContext());
155        }
156        synchronized(DefaultMetricsSystem.instance()) {
157          DefaultMetricsSystem.instance().unregisterSource(info.getMetricsJmxContext());
158          helper.removeSourceName(info.getMetricsJmxContext());
159          helper.removeObjectName(info.getMetricsJmxContext());
160          it.remove();
161          removed = true;
162        }
163      }
164    }
165    if (removed) {
166      JmxCacheBuster.clearJmxCache();
167    }
168  }
169}