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.TreeSet;
22  import java.util.concurrent.locks.ReentrantReadWriteLock;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
26  import org.apache.hadoop.metrics2.MetricsCollector;
27  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
28  
29  @InterfaceAudience.Private
30  public class MetricsRegionAggregateSourceImpl extends BaseSourceImpl
31      implements MetricsRegionAggregateSource {
32  
33    // lock to guard against concurrent access to regionSources
34    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
35  
36    private final TreeSet<MetricsRegionSourceImpl> regionSources =
37        new TreeSet<MetricsRegionSourceImpl>();
38  
39    public MetricsRegionAggregateSourceImpl() {
40      this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT);
41    }
42  
43  
44    public MetricsRegionAggregateSourceImpl(String metricsName,
45                                            String metricsDescription,
46                                            String metricsContext,
47                                            String metricsJmxContext) {
48      super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
49    }
50  
51    @Override
52    public void register(MetricsRegionSource source) {
53      lock.writeLock().lock();
54      try {
55        regionSources.add((MetricsRegionSourceImpl) source);
56      } finally {
57        lock.writeLock().unlock();
58      }
59    }
60  
61    @Override
62    public void deregister(MetricsRegionSource source) {
63      lock.writeLock().lock();
64      try {
65        regionSources.remove(source);
66      } finally {
67        lock.writeLock().unlock();
68      }
69    }
70  
71    /**
72     * Yes this is a get function that doesn't return anything.  Thanks Hadoop for breaking all
73     * expectations of java programmers.  Instead of returning anything Hadoop metrics expects
74     * getMetrics to push the metrics into the collector.
75     *
76     * @param collector the collector
77     * @param all       get all the metrics regardless of when they last changed.
78     */
79    @Override
80    public void getMetrics(MetricsCollector collector, boolean all) {
81  
82  
83      MetricsRecordBuilder mrb = collector.addRecord(metricsName)
84          .setContext(metricsContext);
85  
86      if (regionSources != null) {
87        lock.readLock().lock();
88        try {
89          for (MetricsRegionSourceImpl regionMetricSource : regionSources) {
90            regionMetricSource.snapshot(mrb, all);
91          }
92        } finally {
93          lock.readLock().unlock();
94        }
95      }
96  
97      metricsRegistry.snapshot(mrb, all);
98    }
99  }