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.hbase.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  
46      synchronized void init(String name) {
47        if (inited) return;
48        inited = true;
49        DefaultMetricsSystem.initialize(HBASE_METRICS_SYSTEM_NAME);
50        JvmMetrics.initSingleton(name, "");
51      }
52    }
53  
54    protected final DynamicMetricsRegistry metricsRegistry;
55    protected final String metricsName;
56    protected final String metricsDescription;
57    protected final String metricsContext;
58    protected final String metricsJmxContext;
59  
60    public BaseSourceImpl(
61        String metricsName,
62        String metricsDescription,
63        String metricsContext,
64        String metricsJmxContext) {
65  
66      this.metricsName = metricsName;
67      this.metricsDescription = metricsDescription;
68      this.metricsContext = metricsContext;
69      this.metricsJmxContext = metricsJmxContext;
70  
71      metricsRegistry = new DynamicMetricsRegistry(metricsName).setContext(metricsContext);
72      DefaultMetricsSystemInitializer.INSTANCE.init(metricsName);
73  
74      //Register this instance.
75      DefaultMetricsSystem.instance().register(metricsJmxContext, metricsDescription, this);
76      init();
77  
78    }
79  
80    public void init() {
81      this.metricsRegistry.clearMetrics();
82    }
83  
84    /**
85     * Set a single gauge to a value.
86     *
87     * @param gaugeName gauge name
88     * @param value     the new value of the gauge.
89     */
90    public void setGauge(String gaugeName, long value) {
91      MutableGaugeLong gaugeInt = metricsRegistry.getLongGauge(gaugeName, value);
92      gaugeInt.set(value);
93    }
94  
95    /**
96     * Add some amount to a gauge.
97     *
98     * @param gaugeName The name of the gauge to increment.
99     * @param delta     The amount to increment the gauge by.
100    */
101   public void incGauge(String gaugeName, long delta) {
102     MutableGaugeLong gaugeInt = metricsRegistry.getLongGauge(gaugeName, 0l);
103     gaugeInt.incr(delta);
104   }
105 
106   /**
107    * Decrease the value of a named gauge.
108    *
109    * @param gaugeName The name of the gauge.
110    * @param delta     the ammount to subtract from a gauge value.
111    */
112   public void decGauge(String gaugeName, long delta) {
113     MutableGaugeLong gaugeInt = metricsRegistry.getLongGauge(gaugeName, 0l);
114     gaugeInt.decr(delta);
115   }
116 
117   /**
118    * Increment a named counter by some value.
119    *
120    * @param key   the name of the counter
121    * @param delta the ammount to increment
122    */
123   public void incCounters(String key, long delta) {
124     MutableCounterLong counter = metricsRegistry.getLongCounter(key, 0l);
125     counter.incr(delta);
126 
127   }
128 
129   @Override
130   public void updateHistogram(String name, long value) {
131     MutableHistogram histo = metricsRegistry.getHistogram(name);
132     histo.add(value);
133   }
134 
135   @Override
136   public void updateQuantile(String name, long value) {
137     MetricMutableQuantiles histo = metricsRegistry.getQuantile(name);
138     histo.add(value);
139   }
140 
141   /**
142    * Remove a named gauge.
143    *
144    * @param key
145    */
146   public void removeMetric(String key) {
147     metricsRegistry.removeMetric(key);
148     JmxCacheBuster.clearJmxCache();
149   }
150 
151   @Override
152   public void getMetrics(MetricsCollector metricsCollector, boolean all) {
153     metricsRegistry.snapshot(metricsCollector.addRecord(metricsRegistry.info()), all);
154   }
155 
156   public DynamicMetricsRegistry getMetricsRegistry() {
157     return metricsRegistry;
158   }
159 
160   public String getMetricsContext() {
161     return metricsContext;
162   }
163 
164   public String getMetricsDescription() {
165     return metricsDescription;
166   }
167 
168   public String getMetricsJmxContext() {
169     return metricsJmxContext;
170   }
171 
172   public String getMetricsName() {
173     return metricsName;
174   }
175 
176 }