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 be 044 * performed via the MetricsSourceAdapter and the MetricRegistry will collected like a BaseSource 045 * instance for a group of metrics (like WAL, RPC, etc) with the MetricRegistryInfo's JMX context. 046 * <p> 047 * Developer note: Unlike the current metrics2 based approach, the new metrics approach 048 * (hbase-metrics-api and hbase-metrics modules) work by having different MetricRegistries that are 049 * initialized and used from the code that lives in their respective modules (hbase-server, etc). 050 * There is no need to define BaseSource classes and do a lot of indirection. The MetricRegistry'es 051 * will be in the global MetricRegistriesImpl, and this class will iterate over 052 * MetricRegistries.global() and register adapters to the metrics2 subsystem. These adapters then 053 * report the actual values by delegating to 054 * {@link HBaseMetrics2HadoopMetricsAdapter#snapshotAllMetrics(MetricRegistry, MetricsCollector)}. 055 * We do not initialize the Hadoop Metrics2 system assuming that other BaseSources already do so 056 * (see BaseSourceImpl). Once the last BaseSource is moved to the new system, the metric2 057 * initialization should be moved here. 058 * </p> 059 */ 060@InterfaceAudience.Private 061public final class GlobalMetricRegistriesAdapter { 062 063 private static final Logger LOG = LoggerFactory.getLogger(GlobalMetricRegistriesAdapter.class); 064 065 private class MetricsSourceAdapter implements MetricsSource { 066 private final MetricRegistry registry; 067 068 MetricsSourceAdapter(MetricRegistry registry) { 069 this.registry = registry; 070 } 071 072 @Override 073 public void getMetrics(MetricsCollector collector, boolean all) { 074 metricsAdapter.snapshotAllMetrics(registry, collector); 075 } 076 } 077 078 private final MetricsExecutor executor; 079 private final AtomicBoolean stopped; 080 private final DefaultMetricsSystemHelper helper; 081 private final HBaseMetrics2HadoopMetricsAdapter metricsAdapter; 082 private final HashMap<MetricRegistryInfo, MetricsSourceAdapter> registeredSources; 083 084 private GlobalMetricRegistriesAdapter() { 085 this.executor = new MetricsExecutorImpl(); 086 this.stopped = new AtomicBoolean(false); 087 this.metricsAdapter = new HBaseMetrics2HadoopMetricsAdapter(); 088 this.registeredSources = new HashMap<>(); 089 this.helper = new DefaultMetricsSystemHelper(); 090 executor.getExecutor().scheduleAtFixedRate(() -> this.doRun(), 10, 10, TimeUnit.SECONDS); 091 } 092 093 /** 094 * Make sure that this global MetricSource for hbase-metrics module based metrics are initialized. 095 * This should be called only once. 096 */ 097 public static GlobalMetricRegistriesAdapter init() { 098 return new GlobalMetricRegistriesAdapter(); 099 } 100 101 public void stop() { 102 stopped.set(true); 103 } 104 105 private void doRun() { 106 if (stopped.get()) { 107 executor.stop(); 108 return; 109 } 110 if (LOG.isTraceEnabled()) { 111 LOG.trace("doRun called: " + registeredSources); 112 } 113 114 Collection<MetricRegistry> registries = MetricRegistries.global().getMetricRegistries(); 115 for (MetricRegistry registry : registries) { 116 MetricRegistryInfo info = registry.getMetricRegistryInfo(); 117 118 LOG.trace("MetricRegistryInfo : " + info.getMetricsName()); 119 if (info.isExistingSource()) { 120 // If there is an already existing BaseSource for this MetricRegistry, skip it here. These 121 // types of registries are there only due to existing BaseSource implementations in the 122 // source code (like MetricsRegionServer, etc). This is to make sure that we can transition 123 // iteratively to the new hbase-metrics system. These type of MetricRegistry metrics will be 124 // exported from the BaseSource.getMetrics() call directly because there is already a 125 // MetricRecordBuilder there (see MetricsRegionServerSourceImpl). 126 continue; 127 } 128 129 if (!registeredSources.containsKey(info)) { 130 if (LOG.isDebugEnabled()) { 131 LOG.debug("Registering adapter for the MetricRegistry: " + info.getMetricsJmxContext()); 132 } 133 // register this as a MetricSource under different JMX Context'es. 134 MetricsSourceAdapter adapter = new MetricsSourceAdapter(registry); 135 LOG.info("Registering " + info.getMetricsJmxContext() + " " + info.getMetricsDescription()); 136 DefaultMetricsSystem.instance().register(info.getMetricsJmxContext(), 137 info.getMetricsDescription(), adapter); 138 registeredSources.put(info, adapter); 139 // next collection will collect the newly registered MetricSource. Doing this here leads to 140 // ConcurrentModificationException. 141 } 142 } 143 144 boolean removed = false; 145 // Remove registered sources if it is removed from the global registry 146 for (Iterator<Entry<MetricRegistryInfo, MetricsSourceAdapter>> it = 147 registeredSources.entrySet().iterator(); it.hasNext();) { 148 Entry<MetricRegistryInfo, MetricsSourceAdapter> entry = it.next(); 149 MetricRegistryInfo info = entry.getKey(); 150 Optional<MetricRegistry> found = MetricRegistries.global().get(info); 151 if (!found.isPresent()) { 152 if (LOG.isDebugEnabled()) { 153 LOG.debug("Removing adapter for the MetricRegistry: " + info.getMetricsJmxContext()); 154 } 155 synchronized (DefaultMetricsSystem.instance()) { 156 DefaultMetricsSystem.instance().unregisterSource(info.getMetricsJmxContext()); 157 helper.removeSourceName(info.getMetricsJmxContext()); 158 helper.removeObjectName(info.getMetricsJmxContext()); 159 it.remove(); 160 removed = true; 161 } 162 } 163 } 164 if (removed) { 165 JmxCacheBuster.clearJmxCache(); 166 } 167 } 168}