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