1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.metrics;
20
21 import org.apache.hadoop.hbase.classification.InterfaceAudience;
22 import org.apache.hadoop.hbase.metrics.impl.GlobalMetricRegistriesAdapter;
23 import org.apache.hadoop.hbase.metrics.impl.HBaseMetrics2HadoopMetricsAdapter;
24 import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSourceImpl;
25 import org.apache.hadoop.metrics2.MetricsCollector;
26 import org.apache.hadoop.metrics2.MetricsSource;
27 import org.apache.hadoop.metrics2.impl.JmxCacheBuster;
28 import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
29 import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry;
30 import org.apache.hadoop.metrics2.lib.MutableFastCounter;
31 import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
32 import org.apache.hadoop.metrics2.lib.MutableHistogram;
33 import org.apache.hadoop.metrics2.source.JvmMetrics;
34
35
36
37
38
39
40
41 @InterfaceAudience.Private
42 public class BaseSourceImpl implements BaseSource, MetricsSource {
43
44 private static enum DefaultMetricsSystemInitializer {
45 INSTANCE;
46 private boolean inited = false;
47
48 synchronized void init(String name) {
49 if (inited) return;
50 inited = true;
51 DefaultMetricsSystem.initialize(HBASE_METRICS_SYSTEM_NAME);
52 JvmMetrics.initSingleton(name, "");
53
54
55
56
57 GlobalMetricRegistriesAdapter.init();
58 }
59 }
60
61
62
63
64
65
66
67 @Deprecated
68 protected final DynamicMetricsRegistry metricsRegistry;
69 protected final String metricsName;
70 protected final String metricsDescription;
71 protected final String metricsContext;
72 protected final String metricsJmxContext;
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 protected final MetricRegistry registry;
89
90
91
92
93
94
95
96
97
98 protected final HBaseMetrics2HadoopMetricsAdapter metricsAdapter;
99
100 public BaseSourceImpl(
101 String metricsName,
102 String metricsDescription,
103 String metricsContext,
104 String metricsJmxContext) {
105
106 this.metricsName = metricsName;
107 this.metricsDescription = metricsDescription;
108 this.metricsContext = metricsContext;
109 this.metricsJmxContext = metricsJmxContext;
110
111 metricsRegistry = new DynamicMetricsRegistry(metricsName).setContext(metricsContext);
112 DefaultMetricsSystemInitializer.INSTANCE.init(metricsName);
113
114
115 DefaultMetricsSystem.instance().register(metricsJmxContext, metricsDescription, this);
116
117
118 registry = MetricRegistries.global().create(this.getMetricRegistryInfo());
119 metricsAdapter = new HBaseMetrics2HadoopMetricsAdapter();
120
121 init();
122
123 }
124
125 public void init() {
126 this.metricsRegistry.clearMetrics();
127 }
128
129
130
131
132
133
134
135 public void setGauge(String gaugeName, long value) {
136 MutableGaugeLong gaugeInt = metricsRegistry.getGauge(gaugeName, value);
137 gaugeInt.set(value);
138 }
139
140
141
142
143
144
145
146 public void incGauge(String gaugeName, long delta) {
147 MutableGaugeLong gaugeInt = metricsRegistry.getGauge(gaugeName, 0l);
148 gaugeInt.incr(delta);
149 }
150
151
152
153
154
155
156
157 public void decGauge(String gaugeName, long delta) {
158 MutableGaugeLong gaugeInt = metricsRegistry.getGauge(gaugeName, 0l);
159 gaugeInt.decr(delta);
160 }
161
162
163
164
165
166
167
168 public void incCounters(String key, long delta) {
169 MutableFastCounter counter = metricsRegistry.getCounter(key, 0l);
170 counter.incr(delta);
171
172 }
173
174 @Override
175 public void updateHistogram(String name, long value) {
176 MutableHistogram histo = metricsRegistry.getHistogram(name);
177 histo.add(value);
178 }
179
180
181
182
183
184
185 public void removeMetric(String key) {
186 metricsRegistry.removeMetric(key);
187 JmxCacheBuster.clearJmxCache();
188 }
189
190 @Override
191 public void getMetrics(MetricsCollector metricsCollector, boolean all) {
192 metricsRegistry.snapshot(metricsCollector.addRecord(metricsRegistry.info()), all);
193 }
194
195 public DynamicMetricsRegistry getMetricsRegistry() {
196 return metricsRegistry;
197 }
198
199 public String getMetricsContext() {
200 return metricsContext;
201 }
202
203 public String getMetricsDescription() {
204 return metricsDescription;
205 }
206
207 public String getMetricsJmxContext() {
208 return metricsJmxContext;
209 }
210
211 public String getMetricsName() {
212 return metricsName;
213 }
214
215 @Override
216 public MetricRegistryInfo getMetricRegistryInfo() {
217 return new MetricRegistryInfo(getMetricsName(), getMetricsDescription(),
218 getMetricsContext(), getMetricsJmxContext(), true);
219 }
220
221 }