1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver;
19
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.concurrent.ScheduledExecutorService;
23 import java.util.concurrent.TimeUnit;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.HDFSBlocksDistribution;
32 import org.apache.hadoop.hbase.HRegionInfo;
33 import org.apache.hadoop.hbase.ServerName;
34 import org.apache.hadoop.hbase.io.hfile.BlockCache;
35 import org.apache.hadoop.hbase.io.hfile.CacheConfig;
36 import org.apache.hadoop.hbase.io.hfile.CacheStats;
37 import org.apache.hadoop.hbase.wal.DefaultWALProvider;
38 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
39 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
40 import org.apache.hadoop.metrics2.MetricsExecutor;
41
42
43
44
45 @InterfaceAudience.Private
46 class MetricsRegionServerWrapperImpl
47 implements MetricsRegionServerWrapper {
48
49 public static final Log LOG = LogFactory.getLog(MetricsRegionServerWrapperImpl.class);
50
51 private final HRegionServer regionServer;
52
53 private BlockCache blockCache;
54
55 private volatile long numStores = 0;
56 private volatile long numWALFiles = 0;
57 private volatile long walFileSize = 0;
58 private volatile long numStoreFiles = 0;
59 private volatile long memstoreSize = 0;
60 private volatile long storeFileSize = 0;
61 private volatile double requestsPerSecond = 0.0;
62 private volatile long readRequestsCount = 0;
63 private volatile long writeRequestsCount = 0;
64 private volatile long checkAndMutateChecksFailed = 0;
65 private volatile long checkAndMutateChecksPassed = 0;
66 private volatile long storefileIndexSize = 0;
67 private volatile long totalStaticIndexSize = 0;
68 private volatile long totalStaticBloomSize = 0;
69 private volatile long numMutationsWithoutWAL = 0;
70 private volatile long dataInMemoryWithoutWAL = 0;
71 private volatile int percentFileLocal = 0;
72 private volatile int percentFileLocalSecondaryRegions = 0;
73 private volatile long flushedCellsCount = 0;
74 private volatile long compactedCellsCount = 0;
75 private volatile long majorCompactedCellsCount = 0;
76 private volatile long flushedCellsSize = 0;
77 private volatile long compactedCellsSize = 0;
78 private volatile long majorCompactedCellsSize = 0;
79 private volatile long blockedRequestsCount = 0L;
80
81 private CacheStats cacheStats;
82 private ScheduledExecutorService executor;
83 private Runnable runnable;
84 private long period;
85
86 public MetricsRegionServerWrapperImpl(final HRegionServer regionServer) {
87 this.regionServer = regionServer;
88 initBlockCache();
89
90 this.period =
91 regionServer.conf.getLong(HConstants.REGIONSERVER_METRICS_PERIOD,
92 HConstants.DEFAULT_REGIONSERVER_METRICS_PERIOD);
93
94 this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
95 this.runnable = new RegionServerMetricsWrapperRunnable();
96 this.executor.scheduleWithFixedDelay(this.runnable, this.period, this.period,
97 TimeUnit.MILLISECONDS);
98
99 if (LOG.isInfoEnabled()) {
100 LOG.info("Computing regionserver metrics every " + this.period + " milliseconds");
101 }
102 }
103
104
105
106
107
108
109 private synchronized void initBlockCache() {
110 CacheConfig cacheConfig = this.regionServer.cacheConfig;
111 if (cacheConfig != null && this.blockCache == null) {
112 this.blockCache = cacheConfig.getBlockCache();
113 }
114
115 if (this.blockCache != null && this.cacheStats == null) {
116 this.cacheStats = blockCache.getStats();
117 }
118 }
119
120 @Override
121 public String getClusterId() {
122 return regionServer.getClusterId();
123 }
124
125 @Override
126 public long getStartCode() {
127 return regionServer.getStartcode();
128 }
129
130 @Override
131 public String getZookeeperQuorum() {
132 ZooKeeperWatcher zk = regionServer.getZooKeeper();
133 if (zk == null) {
134 return "";
135 }
136 return zk.getQuorum();
137 }
138
139 @Override
140 public String getCoprocessors() {
141 String[] coprocessors = regionServer.getRegionServerCoprocessors();
142 if (coprocessors == null || coprocessors.length == 0) {
143 return "";
144 }
145 return StringUtils.join(coprocessors, ", ");
146 }
147
148 @Override
149 public String getServerName() {
150 ServerName serverName = regionServer.getServerName();
151 if (serverName == null) {
152 return "";
153 }
154 return serverName.getServerName();
155 }
156
157 @Override
158 public long getNumOnlineRegions() {
159 Collection<Region> onlineRegionsLocalContext = regionServer.getOnlineRegionsLocalContext();
160 if (onlineRegionsLocalContext == null) {
161 return 0;
162 }
163 return onlineRegionsLocalContext.size();
164 }
165
166 @Override
167 public long getTotalRequestCount() {
168 return regionServer.rpcServices.requestCount.get();
169 }
170
171 @Override
172 public int getSplitQueueSize() {
173 if (this.regionServer.compactSplitThread == null) {
174 return 0;
175 }
176 return this.regionServer.compactSplitThread.getSplitQueueSize();
177 }
178
179 @Override
180 public int getCompactionQueueSize() {
181
182 if (this.regionServer.compactSplitThread == null) {
183 return 0;
184 }
185 return this.regionServer.compactSplitThread.getCompactionQueueSize();
186 }
187
188 @Override
189 public int getSmallCompactionQueueSize() {
190
191 if (this.regionServer.compactSplitThread == null) {
192 return 0;
193 }
194 return this.regionServer.compactSplitThread.getSmallCompactionQueueSize();
195 }
196
197 @Override
198 public int getLargeCompactionQueueSize() {
199
200 if (this.regionServer.compactSplitThread == null) {
201 return 0;
202 }
203 return this.regionServer.compactSplitThread.getLargeCompactionQueueSize();
204 }
205
206 @Override
207 public int getFlushQueueSize() {
208
209 if (this.regionServer.cacheFlusher == null) {
210 return 0;
211 }
212 return this.regionServer.cacheFlusher.getFlushQueueSize();
213 }
214
215 @Override
216 public long getBlockCacheCount() {
217 if (this.blockCache == null) {
218 return 0;
219 }
220 return this.blockCache.getBlockCount();
221 }
222
223 @Override
224 public long getBlockCacheSize() {
225 if (this.blockCache == null) {
226 return 0;
227 }
228 return this.blockCache.getCurrentSize();
229 }
230
231 @Override
232 public long getBlockCacheFreeSize() {
233 if (this.blockCache == null) {
234 return 0;
235 }
236 return this.blockCache.getFreeSize();
237 }
238
239 @Override
240 public long getBlockCacheHitCount() {
241 if (this.cacheStats == null) {
242 return 0;
243 }
244 return this.cacheStats.getHitCount();
245 }
246
247 @Override
248 public long getBlockCacheMissCount() {
249 if (this.cacheStats == null) {
250 return 0;
251 }
252 return this.cacheStats.getMissCount();
253 }
254
255 @Override
256 public long getBlockCacheEvictedCount() {
257 if (this.cacheStats == null) {
258 return 0;
259 }
260 return this.cacheStats.getEvictedCount();
261 }
262
263 @Override
264 public double getBlockCacheHitPercent() {
265 if (this.cacheStats == null) {
266 return 0;
267 }
268 return (int) (this.cacheStats.getHitRatio() * 100);
269 }
270
271 @Override
272 public int getBlockCacheHitCachingPercent() {
273 if (this.cacheStats == null) {
274 return 0;
275 }
276 return (int) (this.cacheStats.getHitCachingRatio() * 100);
277 }
278
279 @Override public void forceRecompute() {
280 this.runnable.run();
281 }
282
283 @Override
284 public long getNumStores() {
285 return numStores;
286 }
287
288 @Override
289 public long getNumWALFiles() {
290 return numWALFiles;
291 }
292
293 @Override
294 public long getWALFileSize() {
295 return walFileSize;
296 }
297
298 @Override
299 public long getNumStoreFiles() {
300 return numStoreFiles;
301 }
302
303 @Override
304 public long getMemstoreSize() {
305 return memstoreSize;
306 }
307
308 @Override
309 public long getStoreFileSize() {
310 return storeFileSize;
311 }
312
313 @Override public double getRequestsPerSecond() {
314 return requestsPerSecond;
315 }
316
317 @Override
318 public long getReadRequestsCount() {
319 return readRequestsCount;
320 }
321
322 @Override
323 public long getWriteRequestsCount() {
324 return writeRequestsCount;
325 }
326
327 @Override
328 public long getCheckAndMutateChecksFailed() {
329 return checkAndMutateChecksFailed;
330 }
331
332 @Override
333 public long getCheckAndMutateChecksPassed() {
334 return checkAndMutateChecksPassed;
335 }
336
337 @Override
338 public long getStoreFileIndexSize() {
339 return storefileIndexSize;
340 }
341
342 @Override
343 public long getTotalStaticIndexSize() {
344 return totalStaticIndexSize;
345 }
346
347 @Override
348 public long getTotalStaticBloomSize() {
349 return totalStaticBloomSize;
350 }
351
352 @Override
353 public long getNumMutationsWithoutWAL() {
354 return numMutationsWithoutWAL;
355 }
356
357 @Override
358 public long getDataInMemoryWithoutWAL() {
359 return dataInMemoryWithoutWAL;
360 }
361
362 @Override
363 public int getPercentFileLocal() {
364 return percentFileLocal;
365 }
366
367 @Override
368 public int getPercentFileLocalSecondaryRegions() {
369 return percentFileLocalSecondaryRegions;
370 }
371
372 @Override
373 public long getUpdatesBlockedTime() {
374 if (this.regionServer.cacheFlusher == null) {
375 return 0;
376 }
377 return this.regionServer.cacheFlusher.getUpdatesBlockedMsHighWater().get();
378 }
379
380 @Override
381 public long getFlushedCellsCount() {
382 return flushedCellsCount;
383 }
384
385 @Override
386 public long getCompactedCellsCount() {
387 return compactedCellsCount;
388 }
389
390 @Override
391 public long getMajorCompactedCellsCount() {
392 return majorCompactedCellsCount;
393 }
394
395 @Override
396 public long getFlushedCellsSize() {
397 return flushedCellsSize;
398 }
399
400 @Override
401 public long getCompactedCellsSize() {
402 return compactedCellsSize;
403 }
404
405 @Override
406 public long getMajorCompactedCellsSize() {
407 return majorCompactedCellsSize;
408 }
409
410
411
412
413
414
415 public class RegionServerMetricsWrapperRunnable implements Runnable {
416
417 private long lastRan = 0;
418 private long lastRequestCount = 0;
419
420 @Override
421 synchronized public void run() {
422 initBlockCache();
423 cacheStats = blockCache.getStats();
424
425 HDFSBlocksDistribution hdfsBlocksDistribution =
426 new HDFSBlocksDistribution();
427 HDFSBlocksDistribution hdfsBlocksDistributionSecondaryRegions =
428 new HDFSBlocksDistribution();
429
430 long tempNumStores = 0;
431 long tempNumStoreFiles = 0;
432 long tempMemstoreSize = 0;
433 long tempStoreFileSize = 0;
434 long tempReadRequestsCount = 0;
435 long tempWriteRequestsCount = 0;
436 long tempCheckAndMutateChecksFailed = 0;
437 long tempCheckAndMutateChecksPassed = 0;
438 long tempStorefileIndexSize = 0;
439 long tempTotalStaticIndexSize = 0;
440 long tempTotalStaticBloomSize = 0;
441 long tempNumMutationsWithoutWAL = 0;
442 long tempDataInMemoryWithoutWAL = 0;
443 int tempPercentFileLocal = 0;
444 int tempPercentFileLocalSecondaryRegions = 0;
445 long tempFlushedCellsCount = 0;
446 long tempCompactedCellsCount = 0;
447 long tempMajorCompactedCellsCount = 0;
448 long tempFlushedCellsSize = 0;
449 long tempCompactedCellsSize = 0;
450 long tempMajorCompactedCellsSize = 0;
451 long tempBlockedRequestsCount = 0L;
452
453 for (Region r : regionServer.getOnlineRegionsLocalContext()) {
454 tempNumMutationsWithoutWAL += r.getNumMutationsWithoutWAL();
455 tempDataInMemoryWithoutWAL += r.getDataInMemoryWithoutWAL();
456 tempReadRequestsCount += r.getReadRequestsCount();
457 tempWriteRequestsCount += r.getWriteRequestsCount();
458 tempCheckAndMutateChecksFailed += r.getCheckAndMutateChecksFailed();
459 tempCheckAndMutateChecksPassed += r.getCheckAndMutateChecksPassed();
460 tempBlockedRequestsCount += r.getBlockedRequestsCount();
461 List<Store> storeList = r.getStores();
462 tempNumStores += storeList.size();
463 for (Store store : storeList) {
464 tempNumStoreFiles += store.getStorefilesCount();
465 tempMemstoreSize += store.getMemStoreSize();
466 tempStoreFileSize += store.getStorefilesSize();
467 tempStorefileIndexSize += store.getStorefilesIndexSize();
468 tempTotalStaticBloomSize += store.getTotalStaticBloomSize();
469 tempTotalStaticIndexSize += store.getTotalStaticIndexSize();
470 tempFlushedCellsCount += store.getFlushedCellsCount();
471 tempCompactedCellsCount += store.getCompactedCellsCount();
472 tempMajorCompactedCellsCount += store.getMajorCompactedCellsCount();
473 tempFlushedCellsSize += store.getFlushedCellsSize();
474 tempCompactedCellsSize += store.getCompactedCellsSize();
475 tempMajorCompactedCellsSize += store.getMajorCompactedCellsSize();
476 }
477
478 HDFSBlocksDistribution distro = r.getHDFSBlocksDistribution();
479 hdfsBlocksDistribution.add(distro);
480 if (r.getRegionInfo().getReplicaId() != HRegionInfo.DEFAULT_REPLICA_ID) {
481 hdfsBlocksDistributionSecondaryRegions.add(distro);
482 }
483 }
484
485 float localityIndex = hdfsBlocksDistribution.getBlockLocalityIndex(
486 regionServer.getServerName().getHostname());
487 tempPercentFileLocal = (int) (localityIndex * 100);
488
489 float localityIndexSecondaryRegions = hdfsBlocksDistributionSecondaryRegions
490 .getBlockLocalityIndex(regionServer.getServerName().getHostname());
491 tempPercentFileLocalSecondaryRegions = (int) (localityIndexSecondaryRegions * 100);
492
493
494 long currentTime = EnvironmentEdgeManager.currentTime();
495
496
497
498 if (lastRan == 0) {
499 lastRan = currentTime - period;
500 }
501
502
503
504 if ((currentTime - lastRan) > 0) {
505 long currentRequestCount = getTotalRequestCount();
506 requestsPerSecond = (currentRequestCount - lastRequestCount) / ((currentTime - lastRan) / 1000.0);
507 lastRequestCount = currentRequestCount;
508 }
509 lastRan = currentTime;
510
511 numWALFiles = DefaultWALProvider.getNumLogFiles(regionServer.walFactory);
512 walFileSize = DefaultWALProvider.getLogFileSize(regionServer.walFactory);
513
514
515 numStores = tempNumStores;
516 numStoreFiles = tempNumStoreFiles;
517 memstoreSize = tempMemstoreSize;
518 storeFileSize = tempStoreFileSize;
519 readRequestsCount = tempReadRequestsCount;
520 writeRequestsCount = tempWriteRequestsCount;
521 checkAndMutateChecksFailed = tempCheckAndMutateChecksFailed;
522 checkAndMutateChecksPassed = tempCheckAndMutateChecksPassed;
523 storefileIndexSize = tempStorefileIndexSize;
524 totalStaticIndexSize = tempTotalStaticIndexSize;
525 totalStaticBloomSize = tempTotalStaticBloomSize;
526 numMutationsWithoutWAL = tempNumMutationsWithoutWAL;
527 dataInMemoryWithoutWAL = tempDataInMemoryWithoutWAL;
528 percentFileLocal = tempPercentFileLocal;
529 percentFileLocalSecondaryRegions = tempPercentFileLocalSecondaryRegions;
530 flushedCellsCount = tempFlushedCellsCount;
531 compactedCellsCount = tempCompactedCellsCount;
532 majorCompactedCellsCount = tempMajorCompactedCellsCount;
533 flushedCellsSize = tempFlushedCellsSize;
534 compactedCellsSize = tempCompactedCellsSize;
535 majorCompactedCellsSize = tempMajorCompactedCellsSize;
536 blockedRequestsCount = tempBlockedRequestsCount;
537 }
538 }
539
540 @Override
541 public long getBlockedRequestsCount() {
542 return blockedRequestsCount;
543 }
544 }