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 org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
24  import org.apache.hadoop.metrics2.MetricsCollector;
25  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
26  
27  import java.util.TreeSet;
28  
29  public class MetricsRegionAggregateSourceImpl extends BaseSourceImpl
30      implements MetricsRegionAggregateSource {
31  
32    private final Log LOG = LogFactory.getLog(this.getClass());
33  
34    private final TreeSet<MetricsRegionSourceImpl> regionSources =
35        new TreeSet<MetricsRegionSourceImpl>();
36  
37    public MetricsRegionAggregateSourceImpl() {
38      this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT);
39    }
40  
41  
42    public MetricsRegionAggregateSourceImpl(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(MetricsRegionSource source) {
51      regionSources.add((MetricsRegionSourceImpl) source);
52    }
53  
54    @Override
55    public void deregister(MetricsRegionSource source) {
56      regionSources.remove(source);
57    }
58  
59    /**
60     * Yes this is a get function that doesn't return anything.  Thanks Hadoop for breaking all
61     * expectations of java programmers.  Instead of returning anything Hadoop metrics expects
62     * getMetrics to push the metrics into the collector.
63     *
64     * @param collector the collector
65     * @param all       get all the metrics regardless of when they last changed.
66     */
67    @Override
68    public void getMetrics(MetricsCollector collector, boolean all) {
69  
70  
71      MetricsRecordBuilder mrb = collector.addRecord(metricsName)
72          .setContext(metricsContext);
73  
74      if (regionSources != null) {
75        for (MetricsRegionSourceImpl regionMetricSource : regionSources) {
76          regionMetricSource.snapshot(mrb, all);
77        }
78      }
79  
80      metricsRegistry.snapshot(mrb, all);
81    }
82  }