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