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