View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.util.concurrent.ConcurrentHashMap;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
27  import org.apache.hadoop.hbase.metrics.Interns;
28  import org.apache.hadoop.metrics2.MetricsCollector;
29  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
30  
31  @InterfaceAudience.Private
32  public class MetricsTableAggregateSourceImpl extends BaseSourceImpl
33  implements MetricsTableAggregateSource {
34  
35    private static final Log LOG = LogFactory.getLog(MetricsTableAggregateSourceImpl.class);
36    private ConcurrentHashMap<String, MetricsTableSource> tableSources = new ConcurrentHashMap<>();
37  
38    public MetricsTableAggregateSourceImpl() {
39      this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT);
40    }
41  
42    public MetricsTableAggregateSourceImpl(String metricsName,
43        String metricsDescription,
44        String metricsContext,
45        String metricsJmxContext) {
46      super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
47    }
48  
49    @Override
50    public void register(String table, MetricsTableSource source) {
51      tableSources.put(table, source);
52    }
53  
54    @Override
55    public void deregister(String table) {
56      try {
57        tableSources.remove(table);
58      } catch (Exception e) {
59        // Ignored. If this errors out it means that someone is double
60        // closing the region source and the region is already nulled out.
61        LOG.info(
62          "Error trying to remove " + table + " from " + this.getClass().getSimpleName(),
63          e);
64      }
65    }
66  
67    /**
68     * Yes this is a get function that doesn't return anything.  Thanks Hadoop for breaking all
69     * expectations of java programmers.  Instead of returning anything Hadoop metrics expects
70     * getMetrics to push the metrics into the collector.
71     *
72     * @param collector the collector
73     * @param all       get all the metrics regardless of when they last changed.
74     */
75    @Override
76    public void getMetrics(MetricsCollector collector, boolean all) {
77      MetricsRecordBuilder mrb = collector.addRecord(metricsName);
78  
79      if (tableSources != null) {
80        for (MetricsTableSource tableMetricSource : tableSources.values()) {
81          if (tableMetricSource instanceof MetricsTableSourceImpl) {
82            ((MetricsTableSourceImpl) tableMetricSource).snapshot(mrb, all);
83          }
84        }
85        mrb.addGauge(Interns.info(NUM_TABLES, NUMBER_OF_TABLES_DESC), tableSources.size());
86        metricsRegistry.snapshot(mrb, all);
87      }
88    }
89  }