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.regionserver;
019
020import java.util.Collections;
021import java.util.Set;
022import java.util.concurrent.ConcurrentHashMap;
023import java.util.concurrent.TimeUnit;
024import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
025import org.apache.hadoop.metrics2.MetricsCollector;
026import org.apache.hadoop.metrics2.MetricsRecordBuilder;
027import org.apache.hadoop.metrics2.impl.JmxCacheBuster;
028import org.apache.hadoop.metrics2.lib.MetricsExecutorImpl;
029import org.apache.yetus.audience.InterfaceAudience;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033@InterfaceAudience.Private
034public class MetricsRegionAggregateSourceImpl extends BaseSourceImpl
035  implements MetricsRegionAggregateSource {
036
037  private static final Logger LOG = LoggerFactory.getLogger(MetricsRegionAggregateSourceImpl.class);
038
039  private final MetricsExecutorImpl executor = new MetricsExecutorImpl();
040
041  private final Set<MetricsRegionSource> regionSources =
042    Collections.newSetFromMap(new ConcurrentHashMap<MetricsRegionSource, Boolean>());
043
044  public MetricsRegionAggregateSourceImpl() {
045    this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT);
046  }
047
048  public MetricsRegionAggregateSourceImpl(String metricsName, String metricsDescription,
049    String metricsContext, String metricsJmxContext) {
050    super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
051
052    // Every few mins clean the JMX cache.
053    executor.getExecutor().scheduleWithFixedDelay(new Runnable() {
054      @Override
055      public void run() {
056        JmxCacheBuster.clearJmxCache();
057      }
058    }, 5, 5, TimeUnit.MINUTES);
059  }
060
061  @Override
062  public void register(MetricsRegionSource source) {
063    regionSources.add(source);
064    clearCache();
065  }
066
067  @Override
068  public void deregister(MetricsRegionSource toRemove) {
069    try {
070      regionSources.remove(toRemove);
071    } catch (Exception e) {
072      // Ignored. If this errors out it means that someone is double
073      // closing the region source and the region is already nulled out.
074      LOG.info("Error trying to remove " + toRemove + " from " + this.getClass().getSimpleName(),
075        e);
076    }
077    clearCache();
078  }
079
080  private synchronized void clearCache() {
081    JmxCacheBuster.clearJmxCache();
082  }
083
084  /**
085   * Yes this is a get function that doesn't return anything. Thanks Hadoop for breaking all
086   * expectations of java programmers. Instead of returning anything Hadoop metrics expects
087   * getMetrics to push the metrics into the collector.
088   * @param collector the collector
089   * @param all       get all the metrics regardless of when they last changed.
090   */
091  @Override
092  public void getMetrics(MetricsCollector collector, boolean all) {
093    MetricsRecordBuilder mrb = collector.addRecord(metricsName);
094
095    if (regionSources != null) {
096      for (MetricsRegionSource regionMetricSource : regionSources) {
097        if (regionMetricSource instanceof MetricsRegionSourceImpl) {
098          ((MetricsRegionSourceImpl) regionMetricSource).snapshot(mrb, all);
099        }
100      }
101      metricsRegistry.snapshot(mrb, all);
102    }
103  }
104}