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.metrics;
20  
21  import org.apache.hadoop.classification.InterfaceAudience;
22  import org.apache.hadoop.metrics2.MetricsCollector;
23  import org.apache.hadoop.metrics2.MetricsSource;
24  import org.apache.hadoop.metrics2.impl.JmxCacheBuster;
25  import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
26  import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry;
27  import org.apache.hadoop.metrics2.lib.MetricMutableQuantiles;
28  import org.apache.hadoop.metrics2.lib.MutableCounterLong;
29  import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
30  import org.apache.hadoop.metrics2.lib.MutableHistogram;
31  import org.apache.hadoop.metrics2.source.JvmMetrics;
32  
33  /**
34   * Hadoop 2 implementation of BaseSource (using metrics2 framework).  It handles registration to
35   * DefaultMetricsSystem and creation of the metrics registry.
36   *
37   * All MetricsSource's in hbase-hadoop2-compat should derive from this class.
38   */
39  @InterfaceAudience.Private
40  public class BaseSourceImpl implements BaseSource, MetricsSource {
41  
42    private static enum DefaultMetricsSystemInitializer {
43      INSTANCE;
44      private boolean inited = false;
45      private JvmMetrics jvmMetricsSource;
46  
47      synchronized void init(String name) {
48        if (inited) return;
49        inited = true;
50        DefaultMetricsSystem.initialize(HBASE_METRICS_SYSTEM_NAME);
51        jvmMetricsSource = JvmMetrics.initSingleton(name, "");
52  
53      }
54    }
55  
56    protected final DynamicMetricsRegistry metricsRegistry;
57    protected final String metricsName;
58    protected final String metricsDescription;
59    protected final String metricsContext;
60    protected final String metricsJmxContext;
61  
62    public BaseSourceImpl(
63        String metricsName,
64        String metricsDescription,
65        String metricsContext,
66        String metricsJmxContext) {
67  
68      this.metricsName = metricsName;
69      this.metricsDescription = metricsDescription;
70      this.metricsContext = metricsContext;
71      this.metricsJmxContext = metricsJmxContext;
72  
73      metricsRegistry = new DynamicMetricsRegistry(metricsName).setContext(metricsContext);
74      DefaultMetricsSystemInitializer.INSTANCE.init(metricsName);
75  
76      //Register this instance.
77      DefaultMetricsSystem.instance().register(metricsJmxContext, metricsDescription, this);
78      init();
79  
80    }
81  
82    public void init() {
83      this.metricsRegistry.clearMetrics();
84    }
85  
86    /**
87     * Set a single gauge to a value.
88     *
89     * @param gaugeName gauge name
90     * @param value     the new value of the gauge.
91     */
92    public void setGauge(String gaugeName, long value) {
93      MutableGaugeLong gaugeInt = metricsRegistry.getLongGauge(gaugeName, value);
94      gaugeInt.set(value);
95    }
96  
97    /**
98     * Add some amount to a gauge.
99     *
100    * @param gaugeName The name of the gauge to increment.
101    * @param delta     The amount to increment the gauge by.
102    */
103   public void incGauge(String gaugeName, long delta) {
104     MutableGaugeLong gaugeInt = metricsRegistry.getLongGauge(gaugeName, 0l);
105     gaugeInt.incr(delta);
106   }
107 
108   /**
109    * Decrease the value of a named gauge.
110    *
111    * @param gaugeName The name of the gauge.
112    * @param delta     the ammount to subtract from a gauge value.
113    */
114   public void decGauge(String gaugeName, long delta) {
115     MutableGaugeLong gaugeInt = metricsRegistry.getLongGauge(gaugeName, 0l);
116     gaugeInt.decr(delta);
117   }
118 
119   /**
120    * Increment a named counter by some value.
121    *
122    * @param key   the name of the counter
123    * @param delta the ammount to increment
124    */
125   public void incCounters(String key, long delta) {
126     MutableCounterLong counter = metricsRegistry.getLongCounter(key, 0l);
127     counter.incr(delta);
128 
129   }
130 
131   @Override
132   public void updateHistogram(String name, long value) {
133     MutableHistogram histo = metricsRegistry.getHistogram(name);
134     histo.add(value);
135   }
136 
137   @Override
138   public void updateQuantile(String name, long value) {
139     MetricMutableQuantiles histo = metricsRegistry.getQuantile(name);
140     histo.add(value);
141   }
142 
143   /**
144    * Remove a named gauge.
145    *
146    * @param key
147    */
148   public void removeMetric(String key) {
149     metricsRegistry.removeMetric(key);
150     JmxCacheBuster.clearJmxCache();
151   }
152 
153   @Override
154   public void getMetrics(MetricsCollector metricsCollector, boolean all) {
155     metricsRegistry.snapshot(metricsCollector.addRecord(metricsRegistry.info()), all);
156   }
157 
158   public DynamicMetricsRegistry getMetricsRegistry() {
159     return metricsRegistry;
160   }
161 
162   public String getMetricsContext() {
163     return metricsContext;
164   }
165 
166   public String getMetricsDescription() {
167     return metricsDescription;
168   }
169 
170   public String getMetricsJmxContext() {
171     return metricsJmxContext;
172   }
173 
174   public String getMetricsName() {
175     return metricsName;
176   }
177 
178 }