1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver;
20
21 import java.util.concurrent.atomic.AtomicBoolean;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.hbase.classification.InterfaceAudience;
26 import org.apache.hadoop.metrics2.MetricsRecordBuilder;
27 import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry;
28 import org.apache.hadoop.metrics2.lib.Interns;
29 import org.apache.hadoop.metrics2.lib.MutableFastCounter;
30
31 @InterfaceAudience.Private
32 public class MetricsRegionSourceImpl implements MetricsRegionSource {
33
34 private static final Log LOG = LogFactory.getLog(MetricsRegionSourceImpl.class);
35
36 private AtomicBoolean closed = new AtomicBoolean(false);
37
38
39
40
41
42 private MetricsRegionWrapper regionWrapper;
43
44 private final MetricsRegionAggregateSourceImpl agg;
45 private final DynamicMetricsRegistry registry;
46
47 private final String regionNamePrefix;
48 private final String regionPutKey;
49 private final String regionDeleteKey;
50 private final String regionGetKey;
51 private final String regionIncrementKey;
52 private final String regionAppendKey;
53 private final String regionScanKey;
54
55
56
57
58
59 private final MutableFastCounter regionPut;
60 private final MutableFastCounter regionDelete;
61 private final MutableFastCounter regionIncrement;
62 private final MutableFastCounter regionAppend;
63 private final MutableFastCounter regionGet;
64 private final MutableFastCounter regionScan;
65
66 private final int hashCode;
67
68 public MetricsRegionSourceImpl(MetricsRegionWrapper regionWrapper,
69 MetricsRegionAggregateSourceImpl aggregate) {
70 this.regionWrapper = regionWrapper;
71 agg = aggregate;
72 hashCode = regionWrapper.getRegionHashCode();
73 agg.register(this);
74
75 LOG.debug("Creating new MetricsRegionSourceImpl for table " +
76 regionWrapper.getTableName() + " " + regionWrapper.getRegionName());
77
78 registry = agg.getMetricsRegistry();
79
80 regionNamePrefix = "Namespace_" + regionWrapper.getNamespace() +
81 "_table_" + regionWrapper.getTableName() +
82 "_region_" + regionWrapper.getRegionName() +
83 "_metric_";
84
85 String suffix = "Count";
86
87 regionPutKey = regionNamePrefix + MetricsRegionServerSource.MUTATE_KEY + suffix;
88 regionPut = registry.getCounter(regionPutKey, 0L);
89
90 regionDeleteKey = regionNamePrefix + MetricsRegionServerSource.DELETE_KEY + suffix;
91 regionDelete = registry.getCounter(regionDeleteKey, 0L);
92
93 regionIncrementKey = regionNamePrefix + MetricsRegionServerSource.INCREMENT_KEY + suffix;
94 regionIncrement = registry.getCounter(regionIncrementKey, 0L);
95
96 regionAppendKey = regionNamePrefix + MetricsRegionServerSource.APPEND_KEY + suffix;
97 regionAppend = registry.getCounter(regionAppendKey, 0L);
98
99 regionGetKey = regionNamePrefix + MetricsRegionServerSource.GET_KEY + suffix;
100 regionGet = registry.getCounter(regionGetKey, 0L);
101
102 regionScanKey = regionNamePrefix + MetricsRegionServerSource.SCAN_KEY + suffix;
103 regionScan = registry.getCounter(regionScanKey, 0L);
104 }
105
106 @Override
107 public void close() {
108 boolean wasClosed = closed.getAndSet(true);
109
110
111 if (wasClosed) {
112 return;
113 }
114
115
116
117 agg.deregister(this);
118
119
120
121 synchronized (this) {
122 if (LOG.isTraceEnabled()) {
123 LOG.trace("Removing region Metrics: " + regionWrapper.getRegionName());
124 }
125
126 registry.removeMetric(regionPutKey);
127 registry.removeMetric(regionDeleteKey);
128 registry.removeMetric(regionIncrementKey);
129 registry.removeMetric(regionAppendKey);
130 registry.removeMetric(regionGetKey);
131 registry.removeMetric(regionScanKey);
132
133 regionWrapper = null;
134 }
135 }
136
137 @Override
138 public void updatePut() {
139 regionPut.incr();
140 }
141
142 @Override
143 public void updateDelete() {
144 regionDelete.incr();
145 }
146
147 @Override
148 public void updateGet(long mills) {
149 regionGet.incr();
150 }
151
152 @Override
153 public void updateScanTime(long mills) {
154 regionScan.incr();
155 }
156
157 @Override
158 public void updateIncrement() {
159 regionIncrement.incr();
160 }
161
162 @Override
163 public void updateAppend() {
164 regionAppend.incr();
165 }
166
167 @Override
168 public MetricsRegionAggregateSource getAggregateSource() {
169 return agg;
170 }
171
172 @Override
173 public int compareTo(MetricsRegionSource source) {
174 if (!(source instanceof MetricsRegionSourceImpl)) {
175 return -1;
176 }
177
178 MetricsRegionSourceImpl impl = (MetricsRegionSourceImpl) source;
179 if (impl == null) {
180 return -1;
181 }
182
183 return Long.compare(hashCode, impl.hashCode);
184 }
185
186 void snapshot(MetricsRecordBuilder mrb, boolean ignored) {
187
188
189
190
191
192 if (closed.get()) {
193 return;
194 }
195
196
197
198
199 synchronized (this) {
200
201
202
203 if (closed.get()) {
204 return;
205 }
206
207 mrb.addGauge(
208 Interns.info(
209 regionNamePrefix + MetricsRegionServerSource.STORE_COUNT,
210 MetricsRegionServerSource.STORE_COUNT_DESC),
211 this.regionWrapper.getNumStores());
212 mrb.addGauge(Interns.info(
213 regionNamePrefix + MetricsRegionServerSource.STOREFILE_COUNT,
214 MetricsRegionServerSource.STOREFILE_COUNT_DESC),
215 this.regionWrapper.getNumStoreFiles());
216 mrb.addGauge(Interns.info(
217 regionNamePrefix + MetricsRegionServerSource.MEMSTORE_SIZE,
218 MetricsRegionServerSource.MEMSTORE_SIZE_DESC),
219 this.regionWrapper.getMemstoreSize());
220 mrb.addGauge(Interns.info(
221 regionNamePrefix + MetricsRegionServerSource.MAX_STORE_FILE_AGE,
222 MetricsRegionServerSource.MAX_STORE_FILE_AGE_DESC),
223 this.regionWrapper.getMaxStoreFileAge());
224 mrb.addGauge(Interns.info(
225 regionNamePrefix + MetricsRegionServerSource.MIN_STORE_FILE_AGE,
226 MetricsRegionServerSource.MIN_STORE_FILE_AGE_DESC),
227 this.regionWrapper.getMinStoreFileAge());
228 mrb.addGauge(Interns.info(
229 regionNamePrefix + MetricsRegionServerSource.AVG_STORE_FILE_AGE,
230 MetricsRegionServerSource.AVG_STORE_FILE_AGE_DESC),
231 this.regionWrapper.getAvgStoreFileAge());
232 mrb.addGauge(Interns.info(
233 regionNamePrefix + MetricsRegionServerSource.NUM_REFERENCE_FILES,
234 MetricsRegionServerSource.NUM_REFERENCE_FILES_DESC),
235 this.regionWrapper.getNumReferenceFiles());
236 mrb.addGauge(Interns.info(
237 regionNamePrefix + MetricsRegionServerSource.STOREFILE_SIZE,
238 MetricsRegionServerSource.STOREFILE_SIZE_DESC),
239 this.regionWrapper.getStoreFileSize());
240 mrb.addCounter(Interns.info(
241 regionNamePrefix + MetricsRegionSource.COMPACTIONS_COMPLETED_COUNT,
242 MetricsRegionSource.COMPACTIONS_COMPLETED_DESC),
243 this.regionWrapper.getNumCompactionsCompleted());
244 mrb.addCounter(Interns.info(
245 regionNamePrefix + MetricsRegionSource.NUM_BYTES_COMPACTED_COUNT,
246 MetricsRegionSource.NUM_BYTES_COMPACTED_DESC),
247 this.regionWrapper.getNumBytesCompacted());
248 mrb.addCounter(Interns.info(
249 regionNamePrefix + MetricsRegionSource.NUM_FILES_COMPACTED_COUNT,
250 MetricsRegionSource.NUM_FILES_COMPACTED_DESC),
251 this.regionWrapper.getNumFilesCompacted());
252 mrb.addCounter(Interns.info(
253 regionNamePrefix + MetricsRegionServerSource.READ_REQUEST_COUNT,
254 MetricsRegionServerSource.READ_REQUEST_COUNT_DESC),
255 this.regionWrapper.getReadRequestCount());
256 mrb.addCounter(Interns.info(
257 regionNamePrefix + MetricsRegionServerSource.WRITE_REQUEST_COUNT,
258 MetricsRegionServerSource.WRITE_REQUEST_COUNT_DESC),
259 this.regionWrapper.getWriteRequestCount());
260 mrb.addCounter(Interns.info(
261 regionNamePrefix + MetricsRegionServerSource.TOTAL_REQUEST_COUNT,
262 MetricsRegionServerSource.TOTAL_REQUEST_COUNT_DESC),
263 this.regionWrapper.getTotalRequestCount());
264 mrb.addCounter(Interns.info(regionNamePrefix + MetricsRegionSource.REPLICA_ID,
265 MetricsRegionSource.REPLICA_ID_DESC),
266 this.regionWrapper.getReplicaId());
267 }
268 }
269
270 @Override
271 public int hashCode() {
272 return hashCode;
273 }
274
275 @Override
276 public boolean equals(Object obj) {
277 return obj == this ||
278 (obj instanceof MetricsRegionSourceImpl && compareTo((MetricsRegionSourceImpl) obj) == 0);
279 }
280 }