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