001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import java.io.IOException;
021import java.util.Collection;
022import java.util.List;
023import java.util.OptionalDouble;
024import java.util.OptionalLong;
025import java.util.concurrent.ScheduledExecutorService;
026import java.util.concurrent.TimeUnit;
027
028import org.apache.commons.lang3.StringUtils;
029import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.HDFSBlocksDistribution;
032import org.apache.hadoop.hbase.HRegionInfo;
033import org.apache.hadoop.hbase.ServerName;
034import org.apache.hadoop.hbase.io.hfile.BlockCache;
035import org.apache.hadoop.hbase.io.hfile.CacheConfig;
036import org.apache.hadoop.hbase.io.hfile.CacheStats;
037import org.apache.hadoop.hbase.mob.MobCacheConfig;
038import org.apache.hadoop.hbase.mob.MobFileCache;
039import org.apache.hadoop.hbase.regionserver.wal.MetricsWALSource;
040import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
041import org.apache.hadoop.hbase.util.FSUtils;
042import org.apache.hadoop.hbase.wal.WALProvider;
043import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
044import org.apache.hadoop.hdfs.DFSHedgedReadMetrics;
045import org.apache.hadoop.metrics2.MetricsExecutor;
046import org.apache.yetus.audience.InterfaceAudience;
047import org.slf4j.Logger;
048import org.slf4j.LoggerFactory;
049
050/**
051 * Impl for exposing HRegionServer Information through Hadoop's metrics 2 system.
052 */
053@InterfaceAudience.Private
054class MetricsRegionServerWrapperImpl
055    implements MetricsRegionServerWrapper {
056
057  private static final Logger LOG = LoggerFactory.getLogger(MetricsRegionServerWrapperImpl.class);
058
059  private final HRegionServer regionServer;
060  private final MetricsWALSource metricsWALSource;
061
062  private BlockCache blockCache;
063  private MobFileCache mobFileCache;
064
065  private volatile long numStores = 0;
066  private volatile long numWALFiles = 0;
067  private volatile long walFileSize = 0;
068  private volatile long numStoreFiles = 0;
069  private volatile long memstoreSize = 0;
070  private volatile long storeFileSize = 0;
071  private volatile long maxStoreFileAge = 0;
072  private volatile long minStoreFileAge = 0;
073  private volatile long avgStoreFileAge = 0;
074  private volatile long numReferenceFiles = 0;
075  private volatile double requestsPerSecond = 0.0;
076  private volatile long readRequestsCount = 0;
077  private volatile long filteredReadRequestsCount = 0;
078  private volatile long writeRequestsCount = 0;
079  private volatile long checkAndMutateChecksFailed = 0;
080  private volatile long checkAndMutateChecksPassed = 0;
081  private volatile long storefileIndexSize = 0;
082  private volatile long totalStaticIndexSize = 0;
083  private volatile long totalStaticBloomSize = 0;
084  private volatile long numMutationsWithoutWAL = 0;
085  private volatile long dataInMemoryWithoutWAL = 0;
086  private volatile double percentFileLocal = 0;
087  private volatile double percentFileLocalSecondaryRegions = 0;
088  private volatile long flushedCellsCount = 0;
089  private volatile long compactedCellsCount = 0;
090  private volatile long majorCompactedCellsCount = 0;
091  private volatile long flushedCellsSize = 0;
092  private volatile long compactedCellsSize = 0;
093  private volatile long majorCompactedCellsSize = 0;
094  private volatile long cellsCountCompactedToMob = 0;
095  private volatile long cellsCountCompactedFromMob = 0;
096  private volatile long cellsSizeCompactedToMob = 0;
097  private volatile long cellsSizeCompactedFromMob = 0;
098  private volatile long mobFlushCount = 0;
099  private volatile long mobFlushedCellsCount = 0;
100  private volatile long mobFlushedCellsSize = 0;
101  private volatile long mobScanCellsCount = 0;
102  private volatile long mobScanCellsSize = 0;
103  private volatile long mobFileCacheAccessCount = 0;
104  private volatile long mobFileCacheMissCount = 0;
105  private volatile double mobFileCacheHitRatio = 0;
106  private volatile long mobFileCacheEvictedCount = 0;
107  private volatile long mobFileCacheCount = 0;
108  private volatile long blockedRequestsCount = 0L;
109  private volatile long averageRegionSize = 0L;
110
111  private CacheStats cacheStats;
112  private CacheStats l1Stats = null;
113  private CacheStats l2Stats = null;
114  private ScheduledExecutorService executor;
115  private Runnable runnable;
116  private long period;
117
118  /**
119   * Can be null if not on hdfs.
120   */
121  private DFSHedgedReadMetrics dfsHedgedReadMetrics;
122
123  public MetricsRegionServerWrapperImpl(final HRegionServer regionServer) {
124    this.regionServer = regionServer;
125    initBlockCache();
126    initMobFileCache();
127
128    this.period =
129        regionServer.conf.getLong(HConstants.REGIONSERVER_METRICS_PERIOD,
130          HConstants.DEFAULT_REGIONSERVER_METRICS_PERIOD);
131
132    this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
133    this.runnable = new RegionServerMetricsWrapperRunnable();
134    this.executor.scheduleWithFixedDelay(this.runnable, this.period, this.period,
135      TimeUnit.MILLISECONDS);
136    this.metricsWALSource = CompatibilitySingletonFactory.getInstance(MetricsWALSource.class);
137
138    try {
139      this.dfsHedgedReadMetrics = FSUtils.getDFSHedgedReadMetrics(regionServer.getConfiguration());
140    } catch (IOException e) {
141      LOG.warn("Failed to get hedged metrics", e);
142    }
143    if (LOG.isInfoEnabled()) {
144      LOG.info("Computing regionserver metrics every " + this.period + " milliseconds");
145    }
146  }
147
148  /**
149   * It's possible that due to threading the block cache could not be initialized
150   * yet (testing multiple region servers in one jvm).  So we need to try and initialize
151   * the blockCache and cacheStats reference multiple times until we succeed.
152   */
153  private synchronized  void initBlockCache() {
154    CacheConfig cacheConfig = this.regionServer.cacheConfig;
155    if (cacheConfig != null) {
156      l1Stats = cacheConfig.getOnHeapCacheStats();
157      l2Stats = cacheConfig.getL2CacheStats();
158      if (this.blockCache == null) {
159        this.blockCache = cacheConfig.getBlockCache();
160      }
161    }
162
163    if (this.blockCache != null && this.cacheStats == null) {
164      this.cacheStats = blockCache.getStats();
165    }
166  }
167
168  /**
169   * Initializes the mob file cache.
170   */
171  private synchronized void initMobFileCache() {
172    MobCacheConfig mobCacheConfig = this.regionServer.mobCacheConfig;
173    if (mobCacheConfig != null && this.mobFileCache == null) {
174      this.mobFileCache = mobCacheConfig.getMobFileCache();
175    }
176  }
177
178  @Override
179  public String getClusterId() {
180    return regionServer.getClusterId();
181  }
182
183  @Override
184  public long getStartCode() {
185    return regionServer.getStartcode();
186  }
187
188  @Override
189  public String getZookeeperQuorum() {
190    ZKWatcher zk = regionServer.getZooKeeper();
191    if (zk == null) {
192      return "";
193    }
194    return zk.getQuorum();
195  }
196
197  @Override
198  public String getCoprocessors() {
199    String[] coprocessors = regionServer.getRegionServerCoprocessors();
200    if (coprocessors == null || coprocessors.length == 0) {
201      return "";
202    }
203    return StringUtils.join(coprocessors, ", ");
204  }
205
206  @Override
207  public String getServerName() {
208    ServerName serverName = regionServer.getServerName();
209    if (serverName == null) {
210      return "";
211    }
212    return serverName.getServerName();
213  }
214
215  @Override
216  public long getNumOnlineRegions() {
217    Collection<HRegion> onlineRegionsLocalContext = regionServer.getOnlineRegionsLocalContext();
218    if (onlineRegionsLocalContext == null) {
219      return 0;
220    }
221    return onlineRegionsLocalContext.size();
222  }
223
224  @Override
225  public long getTotalRequestCount() {
226    return regionServer.rpcServices.requestCount.sum();
227  }
228
229  @Override
230  public long getTotalRowActionRequestCount() {
231    return readRequestsCount + writeRequestsCount;
232  }
233
234  @Override
235  public int getSplitQueueSize() {
236    if (this.regionServer.compactSplitThread == null) {
237      return 0;
238    }
239    return this.regionServer.compactSplitThread.getSplitQueueSize();
240  }
241
242  @Override
243  public int getCompactionQueueSize() {
244    //The thread could be zero.  if so assume there is no queue.
245    if (this.regionServer.compactSplitThread == null) {
246      return 0;
247    }
248    return this.regionServer.compactSplitThread.getCompactionQueueSize();
249  }
250
251  @Override
252  public int getSmallCompactionQueueSize() {
253    //The thread could be zero.  if so assume there is no queue.
254    if (this.regionServer.compactSplitThread == null) {
255      return 0;
256    }
257    return this.regionServer.compactSplitThread.getSmallCompactionQueueSize();
258  }
259
260  @Override
261  public int getLargeCompactionQueueSize() {
262    //The thread could be zero.  if so assume there is no queue.
263    if (this.regionServer.compactSplitThread == null) {
264      return 0;
265    }
266    return this.regionServer.compactSplitThread.getLargeCompactionQueueSize();
267  }
268
269  @Override
270  public int getFlushQueueSize() {
271    //If there is no flusher there should be no queue.
272    if (this.regionServer.cacheFlusher == null) {
273      return 0;
274    }
275    return this.regionServer.cacheFlusher.getFlushQueueSize();
276  }
277
278  @Override
279  public long getBlockCacheCount() {
280    if (this.blockCache == null) {
281      return 0;
282    }
283    return this.blockCache.getBlockCount();
284  }
285
286  @Override
287  public long getMemStoreLimit() {
288    return this.regionServer.getRegionServerAccounting().getGlobalMemStoreLimit();
289  }
290
291  @Override
292  public long getBlockCacheSize() {
293    if (this.blockCache == null) {
294      return 0;
295    }
296    return this.blockCache.getCurrentSize();
297  }
298
299  @Override
300  public long getBlockCacheFreeSize() {
301    if (this.blockCache == null) {
302      return 0;
303    }
304    return this.blockCache.getFreeSize();
305  }
306
307  @Override
308  public long getBlockCacheHitCount() {
309    if (this.cacheStats == null) {
310      return 0;
311    }
312    return this.cacheStats.getHitCount();
313  }
314
315  @Override
316  public long getBlockCachePrimaryHitCount() {
317    if (this.cacheStats == null) {
318      return 0;
319    }
320    return this.cacheStats.getPrimaryHitCount();
321  }
322
323  @Override
324  public long getBlockCacheMissCount() {
325    if (this.cacheStats == null) {
326      return 0;
327    }
328    return this.cacheStats.getMissCount();
329  }
330
331  @Override
332  public long getBlockCachePrimaryMissCount() {
333    if (this.cacheStats == null) {
334      return 0;
335    }
336    return this.cacheStats.getPrimaryMissCount();
337  }
338
339  @Override
340  public long getBlockCacheEvictedCount() {
341    if (this.cacheStats == null) {
342      return 0;
343    }
344    return this.cacheStats.getEvictedCount();
345  }
346
347  @Override
348  public long getBlockCachePrimaryEvictedCount() {
349    if (this.cacheStats == null) {
350      return 0;
351    }
352    return this.cacheStats.getPrimaryEvictedCount();
353  }
354
355  @Override
356  public double getBlockCacheHitPercent() {
357    if (this.cacheStats == null) {
358      return 0;
359    }
360    double ratio = this.cacheStats.getHitRatio();
361    if (Double.isNaN(ratio)) {
362      ratio = 0;
363    }
364    return (ratio * 100);
365  }
366
367  @Override
368  public double getBlockCacheHitCachingPercent() {
369    if (this.cacheStats == null) {
370      return 0;
371    }
372
373    double ratio = this.cacheStats.getHitCachingRatio();
374
375    if (Double.isNaN(ratio)) {
376      ratio = 0;
377    }
378    return (ratio * 100);
379  }
380
381  @Override
382  public long getBlockCacheFailedInsertions() {
383    if (this.cacheStats == null) {
384      return 0;
385    }
386    return this.cacheStats.getFailedInserts();
387  }
388
389  @Override
390  public long getL1CacheHitCount() {
391    if (this.l1Stats == null) {
392      return 0;
393    }
394    return this.l1Stats.getHitCount();
395  }
396
397  @Override
398  public long getL1CacheMissCount() {
399    if (this.l1Stats == null) {
400      return 0;
401    }
402    return this.l1Stats.getMissCount();
403  }
404
405  @Override
406  public double getL1CacheHitRatio() {
407    if (this.l1Stats == null) {
408      return 0;
409    }
410    return this.l1Stats.getHitRatio();
411  }
412
413  @Override
414  public double getL1CacheMissRatio() {
415    if (this.l1Stats == null) {
416      return 0;
417    }
418    return this.l1Stats.getMissRatio();
419  }
420
421  @Override
422  public long getL2CacheHitCount() {
423    if (this.l2Stats == null) {
424      return 0;
425    }
426    return this.l2Stats.getHitCount();
427  }
428
429  @Override
430  public long getL2CacheMissCount() {
431    if (this.l2Stats == null) {
432      return 0;
433    }
434    return this.l2Stats.getMissCount();
435  }
436
437  @Override
438  public double getL2CacheHitRatio() {
439    if (this.l2Stats == null) {
440      return 0;
441    }
442    return this.l2Stats.getHitRatio();
443  }
444
445  @Override
446  public double getL2CacheMissRatio() {
447    if (this.l2Stats == null) {
448      return 0;
449    }
450    return this.l2Stats.getMissRatio();
451  }
452
453  @Override public void forceRecompute() {
454    this.runnable.run();
455  }
456
457  @Override
458  public long getNumStores() {
459    return numStores;
460  }
461
462  @Override
463  public long getNumWALFiles() {
464    return numWALFiles;
465  }
466
467  @Override
468  public long getWALFileSize() {
469    return walFileSize;
470  }
471
472  @Override
473  public long getNumWALSlowAppend() {
474    return metricsWALSource.getSlowAppendCount();
475  }
476
477  @Override
478  public long getNumStoreFiles() {
479    return numStoreFiles;
480  }
481
482  @Override
483  public long getMaxStoreFileAge() {
484    return maxStoreFileAge;
485  }
486
487  @Override
488  public long getMinStoreFileAge() {
489    return minStoreFileAge;
490  }
491
492  @Override
493  public long getAvgStoreFileAge() {
494    return avgStoreFileAge;
495  }
496
497  @Override
498  public long getNumReferenceFiles() {
499    return numReferenceFiles;
500  }
501
502  @Override
503  public long getMemStoreSize() {
504    return memstoreSize;
505  }
506
507  @Override
508  public long getStoreFileSize() {
509    return storeFileSize;
510  }
511
512  @Override public double getRequestsPerSecond() {
513    return requestsPerSecond;
514  }
515
516  @Override
517  public long getReadRequestsCount() {
518    return readRequestsCount;
519  }
520
521  @Override
522  public long getFilteredReadRequestsCount() {
523    return filteredReadRequestsCount;
524  }
525
526  @Override
527  public long getWriteRequestsCount() {
528    return writeRequestsCount;
529  }
530
531  @Override
532  public long getRpcGetRequestsCount() {
533    return regionServer.rpcServices.rpcGetRequestCount.sum();
534  }
535
536  @Override
537  public long getRpcScanRequestsCount() {
538    return regionServer.rpcServices.rpcScanRequestCount.sum();
539  }
540
541  @Override
542  public long getRpcMultiRequestsCount() {
543    return regionServer.rpcServices.rpcMultiRequestCount.sum();
544  }
545
546  @Override
547  public long getRpcMutateRequestsCount() {
548    return regionServer.rpcServices.rpcMutateRequestCount.sum();
549  }
550
551  @Override
552  public long getCheckAndMutateChecksFailed() {
553    return checkAndMutateChecksFailed;
554  }
555
556  @Override
557  public long getCheckAndMutateChecksPassed() {
558    return checkAndMutateChecksPassed;
559  }
560
561  @Override
562  public long getStoreFileIndexSize() {
563    return storefileIndexSize;
564  }
565
566  @Override
567  public long getTotalStaticIndexSize() {
568    return totalStaticIndexSize;
569  }
570
571  @Override
572  public long getTotalStaticBloomSize() {
573    return totalStaticBloomSize;
574  }
575
576  @Override
577  public long getNumMutationsWithoutWAL() {
578    return numMutationsWithoutWAL;
579  }
580
581  @Override
582  public long getDataInMemoryWithoutWAL() {
583    return dataInMemoryWithoutWAL;
584  }
585
586  @Override
587  public double getPercentFileLocal() {
588    return percentFileLocal;
589  }
590
591  @Override
592  public double getPercentFileLocalSecondaryRegions() {
593    return percentFileLocalSecondaryRegions;
594  }
595
596  @Override
597  public long getUpdatesBlockedTime() {
598    if (this.regionServer.cacheFlusher == null) {
599      return 0;
600    }
601    return this.regionServer.cacheFlusher.getUpdatesBlockedMsHighWater().sum();
602  }
603
604  @Override
605  public long getFlushedCellsCount() {
606    return flushedCellsCount;
607  }
608
609  @Override
610  public long getCompactedCellsCount() {
611    return compactedCellsCount;
612  }
613
614  @Override
615  public long getMajorCompactedCellsCount() {
616    return majorCompactedCellsCount;
617  }
618
619  @Override
620  public long getFlushedCellsSize() {
621    return flushedCellsSize;
622  }
623
624  @Override
625  public long getCompactedCellsSize() {
626    return compactedCellsSize;
627  }
628
629  @Override
630  public long getMajorCompactedCellsSize() {
631    return majorCompactedCellsSize;
632  }
633
634  @Override
635  public long getCellsCountCompactedFromMob() {
636    return cellsCountCompactedFromMob;
637  }
638
639  @Override
640  public long getCellsCountCompactedToMob() {
641    return cellsCountCompactedToMob;
642  }
643
644  @Override
645  public long getCellsSizeCompactedFromMob() {
646    return cellsSizeCompactedFromMob;
647  }
648
649  @Override
650  public long getCellsSizeCompactedToMob() {
651    return cellsSizeCompactedToMob;
652  }
653
654  @Override
655  public long getMobFlushCount() {
656    return mobFlushCount;
657  }
658
659  @Override
660  public long getMobFlushedCellsCount() {
661    return mobFlushedCellsCount;
662  }
663
664  @Override
665  public long getMobFlushedCellsSize() {
666    return mobFlushedCellsSize;
667  }
668
669  @Override
670  public long getMobScanCellsCount() {
671    return mobScanCellsCount;
672  }
673
674  @Override
675  public long getMobScanCellsSize() {
676    return mobScanCellsSize;
677  }
678
679  @Override
680  public long getMobFileCacheAccessCount() {
681    return mobFileCacheAccessCount;
682  }
683
684  @Override
685  public long getMobFileCacheMissCount() {
686    return mobFileCacheMissCount;
687  }
688
689  @Override
690  public long getMobFileCacheCount() {
691    return mobFileCacheCount;
692  }
693
694  @Override
695  public long getMobFileCacheEvictedCount() {
696    return mobFileCacheEvictedCount;
697  }
698
699  @Override
700  public double getMobFileCacheHitPercent() {
701    return mobFileCacheHitRatio * 100;
702  }
703
704  /**
705   * This is the runnable that will be executed on the executor every PERIOD number of seconds
706   * It will take metrics/numbers from all of the regions and use them to compute point in
707   * time metrics.
708   */
709  public class RegionServerMetricsWrapperRunnable implements Runnable {
710
711    private long lastRan = 0;
712    private long lastRequestCount = 0;
713
714    @Override
715    synchronized public void run() {
716      try {
717        initBlockCache();
718        initMobFileCache();
719
720        HDFSBlocksDistribution hdfsBlocksDistribution =
721            new HDFSBlocksDistribution();
722        HDFSBlocksDistribution hdfsBlocksDistributionSecondaryRegions =
723            new HDFSBlocksDistribution();
724
725        long tempNumStores = 0, tempNumStoreFiles = 0, tempMemstoreSize = 0, tempStoreFileSize = 0;
726        long tempMaxStoreFileAge = 0, tempNumReferenceFiles = 0;
727        long avgAgeNumerator = 0, numHFiles = 0;
728        long tempMinStoreFileAge = Long.MAX_VALUE;
729        long tempReadRequestsCount = 0, tempFilteredReadRequestsCount = 0,
730          tempWriteRequestsCount = 0;
731        long tempCheckAndMutateChecksFailed = 0;
732        long tempCheckAndMutateChecksPassed = 0;
733        long tempStorefileIndexSize = 0;
734        long tempTotalStaticIndexSize = 0;
735        long tempTotalStaticBloomSize = 0;
736        long tempNumMutationsWithoutWAL = 0;
737        long tempDataInMemoryWithoutWAL = 0;
738        double tempPercentFileLocal = 0;
739        double tempPercentFileLocalSecondaryRegions = 0;
740        long tempFlushedCellsCount = 0;
741        long tempCompactedCellsCount = 0;
742        long tempMajorCompactedCellsCount = 0;
743        long tempFlushedCellsSize = 0;
744        long tempCompactedCellsSize = 0;
745        long tempMajorCompactedCellsSize = 0;
746        long tempCellsCountCompactedToMob = 0;
747        long tempCellsCountCompactedFromMob = 0;
748        long tempCellsSizeCompactedToMob = 0;
749        long tempCellsSizeCompactedFromMob = 0;
750        long tempMobFlushCount = 0;
751        long tempMobFlushedCellsCount = 0;
752        long tempMobFlushedCellsSize = 0;
753        long tempMobScanCellsCount = 0;
754        long tempMobScanCellsSize = 0;
755        long tempBlockedRequestsCount = 0;
756        int regionCount = 0;
757        for (HRegion r : regionServer.getOnlineRegionsLocalContext()) {
758          tempNumMutationsWithoutWAL += r.getNumMutationsWithoutWAL();
759          tempDataInMemoryWithoutWAL += r.getDataInMemoryWithoutWAL();
760          tempReadRequestsCount += r.getReadRequestsCount();
761          tempFilteredReadRequestsCount += r.getFilteredReadRequestsCount();
762          tempWriteRequestsCount += r.getWriteRequestsCount();
763          tempCheckAndMutateChecksFailed += r.getCheckAndMutateChecksFailed();
764          tempCheckAndMutateChecksPassed += r.getCheckAndMutateChecksPassed();
765          tempBlockedRequestsCount += r.getBlockedRequestsCount();
766          List<? extends Store> storeList = r.getStores();
767          tempNumStores += storeList.size();
768          for (Store store : storeList) {
769            tempNumStoreFiles += store.getStorefilesCount();
770            tempMemstoreSize += store.getMemStoreSize().getDataSize();
771            tempStoreFileSize += store.getStorefilesSize();
772
773            OptionalLong storeMaxStoreFileAge = store.getMaxStoreFileAge();
774            if (storeMaxStoreFileAge.isPresent() &&
775                storeMaxStoreFileAge.getAsLong() > tempMaxStoreFileAge) {
776              tempMaxStoreFileAge = storeMaxStoreFileAge.getAsLong();
777            }
778
779            OptionalLong storeMinStoreFileAge = store.getMinStoreFileAge();
780            if (storeMinStoreFileAge.isPresent() &&
781                storeMinStoreFileAge.getAsLong() < tempMinStoreFileAge) {
782              tempMinStoreFileAge = storeMinStoreFileAge.getAsLong();
783            }
784
785            long storeHFiles = store.getNumHFiles();
786            numHFiles += storeHFiles;
787            tempNumReferenceFiles += store.getNumReferenceFiles();
788
789            OptionalDouble storeAvgStoreFileAge = store.getAvgStoreFileAge();
790            if (storeAvgStoreFileAge.isPresent()) {
791              avgAgeNumerator =
792                  (long) (avgAgeNumerator + storeAvgStoreFileAge.getAsDouble() * storeHFiles);
793            }
794
795            tempStorefileIndexSize += store.getStorefilesRootLevelIndexSize();
796            tempTotalStaticBloomSize += store.getTotalStaticBloomSize();
797            tempTotalStaticIndexSize += store.getTotalStaticIndexSize();
798            tempFlushedCellsCount += store.getFlushedCellsCount();
799            tempCompactedCellsCount += store.getCompactedCellsCount();
800            tempMajorCompactedCellsCount += store.getMajorCompactedCellsCount();
801            tempFlushedCellsSize += store.getFlushedCellsSize();
802            tempCompactedCellsSize += store.getCompactedCellsSize();
803            tempMajorCompactedCellsSize += store.getMajorCompactedCellsSize();
804            if (store instanceof HMobStore) {
805              HMobStore mobStore = (HMobStore) store;
806              tempCellsCountCompactedToMob += mobStore.getCellsCountCompactedToMob();
807              tempCellsCountCompactedFromMob += mobStore.getCellsCountCompactedFromMob();
808              tempCellsSizeCompactedToMob += mobStore.getCellsSizeCompactedToMob();
809              tempCellsSizeCompactedFromMob += mobStore.getCellsSizeCompactedFromMob();
810              tempMobFlushCount += mobStore.getMobFlushCount();
811              tempMobFlushedCellsCount += mobStore.getMobFlushedCellsCount();
812              tempMobFlushedCellsSize += mobStore.getMobFlushedCellsSize();
813              tempMobScanCellsCount += mobStore.getMobScanCellsCount();
814              tempMobScanCellsSize += mobStore.getMobScanCellsSize();
815            }
816          }
817
818          HDFSBlocksDistribution distro = r.getHDFSBlocksDistribution();
819          hdfsBlocksDistribution.add(distro);
820          if (r.getRegionInfo().getReplicaId() != HRegionInfo.DEFAULT_REPLICA_ID) {
821            hdfsBlocksDistributionSecondaryRegions.add(distro);
822          }
823          regionCount++;
824        }
825        float localityIndex = hdfsBlocksDistribution.getBlockLocalityIndex(
826            regionServer.getServerName().getHostname());
827        tempPercentFileLocal = Double.isNaN(tempBlockedRequestsCount) ? 0 : (localityIndex * 100);
828
829        float localityIndexSecondaryRegions = hdfsBlocksDistributionSecondaryRegions
830            .getBlockLocalityIndex(regionServer.getServerName().getHostname());
831        tempPercentFileLocalSecondaryRegions = Double.
832            isNaN(localityIndexSecondaryRegions) ? 0 : (localityIndexSecondaryRegions * 100);
833
834        // Compute the number of requests per second
835        long currentTime = EnvironmentEdgeManager.currentTime();
836
837        // assume that it took PERIOD seconds to start the executor.
838        // this is a guess but it's a pretty good one.
839        if (lastRan == 0) {
840          lastRan = currentTime - period;
841        }
842        // If we've time traveled keep the last requests per second.
843        if ((currentTime - lastRan) > 0) {
844          long currentRequestCount = getTotalRequestCount();
845          requestsPerSecond = (currentRequestCount - lastRequestCount) /
846              ((currentTime - lastRan) / 1000.0);
847          lastRequestCount = currentRequestCount;
848        }
849        lastRan = currentTime;
850
851        WALProvider provider = regionServer.walFactory.getWALProvider();
852        WALProvider metaProvider = regionServer.walFactory.getMetaWALProvider();
853        numWALFiles = (provider == null ? 0 : provider.getNumLogFiles()) +
854            (metaProvider == null ? 0 : metaProvider.getNumLogFiles());
855        walFileSize = (provider == null ? 0 : provider.getLogFileSize()) +
856            (provider == null ? 0 : provider.getLogFileSize());
857        // Copy over computed values so that no thread sees half computed values.
858        numStores = tempNumStores;
859        numStoreFiles = tempNumStoreFiles;
860        memstoreSize = tempMemstoreSize;
861        storeFileSize = tempStoreFileSize;
862        maxStoreFileAge = tempMaxStoreFileAge;
863        if (regionCount > 0) {
864          averageRegionSize = (memstoreSize + storeFileSize) / regionCount;
865        }
866        if (tempMinStoreFileAge != Long.MAX_VALUE) {
867          minStoreFileAge = tempMinStoreFileAge;
868        }
869
870        if (numHFiles != 0) {
871          avgStoreFileAge = avgAgeNumerator / numHFiles;
872        }
873
874        numReferenceFiles= tempNumReferenceFiles;
875        readRequestsCount = tempReadRequestsCount;
876        filteredReadRequestsCount = tempFilteredReadRequestsCount;
877        writeRequestsCount = tempWriteRequestsCount;
878        checkAndMutateChecksFailed = tempCheckAndMutateChecksFailed;
879        checkAndMutateChecksPassed = tempCheckAndMutateChecksPassed;
880        storefileIndexSize = tempStorefileIndexSize;
881        totalStaticIndexSize = tempTotalStaticIndexSize;
882        totalStaticBloomSize = tempTotalStaticBloomSize;
883        numMutationsWithoutWAL = tempNumMutationsWithoutWAL;
884        dataInMemoryWithoutWAL = tempDataInMemoryWithoutWAL;
885        percentFileLocal = tempPercentFileLocal;
886        percentFileLocalSecondaryRegions = tempPercentFileLocalSecondaryRegions;
887        flushedCellsCount = tempFlushedCellsCount;
888        compactedCellsCount = tempCompactedCellsCount;
889        majorCompactedCellsCount = tempMajorCompactedCellsCount;
890        flushedCellsSize = tempFlushedCellsSize;
891        compactedCellsSize = tempCompactedCellsSize;
892        majorCompactedCellsSize = tempMajorCompactedCellsSize;
893        cellsCountCompactedToMob = tempCellsCountCompactedToMob;
894        cellsCountCompactedFromMob = tempCellsCountCompactedFromMob;
895        cellsSizeCompactedToMob = tempCellsSizeCompactedToMob;
896        cellsSizeCompactedFromMob = tempCellsSizeCompactedFromMob;
897        mobFlushCount = tempMobFlushCount;
898        mobFlushedCellsCount = tempMobFlushedCellsCount;
899        mobFlushedCellsSize = tempMobFlushedCellsSize;
900        mobScanCellsCount = tempMobScanCellsCount;
901        mobScanCellsSize = tempMobScanCellsSize;
902        mobFileCacheAccessCount = mobFileCache.getAccessCount();
903        mobFileCacheMissCount = mobFileCache.getMissCount();
904        mobFileCacheHitRatio = Double.
905            isNaN(mobFileCache.getHitRatio())?0:mobFileCache.getHitRatio();
906        mobFileCacheEvictedCount = mobFileCache.getEvictedFileCount();
907        mobFileCacheCount = mobFileCache.getCacheSize();
908        blockedRequestsCount = tempBlockedRequestsCount;
909      } catch (Throwable e) {
910        LOG.warn("Caught exception! Will suppress and retry.", e);
911      }
912    }
913  }
914
915  @Override
916  public long getHedgedReadOps() {
917    return this.dfsHedgedReadMetrics == null? 0: this.dfsHedgedReadMetrics.getHedgedReadOps();
918  }
919
920  @Override
921  public long getHedgedReadWins() {
922    return this.dfsHedgedReadMetrics == null? 0: this.dfsHedgedReadMetrics.getHedgedReadWins();
923  }
924
925  @Override
926  public long getBlockedRequestsCount() {
927    return blockedRequestsCount;
928  }
929
930  @Override
931  public long getAverageRegionSize() {
932    return averageRegionSize;
933  }
934
935  @Override
936  public long getDataMissCount() {
937    if (this.cacheStats == null) {
938      return 0;
939    }
940    return cacheStats.getDataMissCount();
941  }
942
943  @Override
944  public long getLeafIndexMissCount() {
945    if (this.cacheStats == null) {
946      return 0;
947    }
948    return cacheStats.getLeafIndexMissCount();
949  }
950
951  @Override
952  public long getBloomChunkMissCount() {
953    if (this.cacheStats == null) {
954      return 0;
955    }
956    return cacheStats.getBloomChunkMissCount();
957  }
958
959  @Override
960  public long getMetaMissCount() {
961    if (this.cacheStats == null) {
962      return 0;
963    }
964    return cacheStats.getMetaMissCount();
965  }
966
967  @Override
968  public long getRootIndexMissCount() {
969    if (this.cacheStats == null) {
970      return 0;
971    }
972    return cacheStats.getRootIndexMissCount();
973  }
974
975  @Override
976  public long getIntermediateIndexMissCount() {
977    if (this.cacheStats == null) {
978      return 0;
979    }
980    return cacheStats.getIntermediateIndexMissCount();
981  }
982
983  @Override
984  public long getFileInfoMissCount() {
985    if (this.cacheStats == null) {
986      return 0;
987    }
988    return cacheStats.getFileInfoMissCount();
989  }
990
991  @Override
992  public long getGeneralBloomMetaMissCount() {
993    if (this.cacheStats == null) {
994      return 0;
995    }
996    return cacheStats.getGeneralBloomMetaMissCount();
997  }
998
999  @Override
1000  public long getDeleteFamilyBloomMissCount() {
1001    if (this.cacheStats == null) {
1002      return 0;
1003    }
1004    return cacheStats.getDeleteFamilyBloomMissCount();
1005  }
1006
1007  @Override
1008  public long getTrailerMissCount() {
1009    if (this.cacheStats == null) {
1010      return 0;
1011    }
1012    return cacheStats.getTrailerMissCount();
1013  }
1014
1015  @Override
1016  public long getDataHitCount() {
1017    if (this.cacheStats == null) {
1018      return 0;
1019    }
1020    return cacheStats.getDataHitCount();
1021  }
1022
1023  @Override
1024  public long getLeafIndexHitCount() {
1025    if (this.cacheStats == null) {
1026      return 0;
1027    }
1028    return cacheStats.getLeafIndexHitCount();
1029  }
1030
1031  @Override
1032  public long getBloomChunkHitCount() {
1033    if (this.cacheStats == null) {
1034      return 0;
1035    }
1036    return cacheStats.getBloomChunkHitCount();
1037  }
1038
1039  @Override
1040  public long getMetaHitCount() {
1041    if (this.cacheStats == null) {
1042      return 0;
1043    }
1044    return cacheStats.getMetaHitCount();
1045  }
1046
1047  @Override
1048  public long getRootIndexHitCount() {
1049    if (this.cacheStats == null) {
1050      return 0;
1051    }
1052    return cacheStats.getRootIndexHitCount();
1053  }
1054
1055  @Override
1056  public long getIntermediateIndexHitCount() {
1057    if (this.cacheStats == null) {
1058      return 0;
1059    }
1060    return cacheStats.getIntermediateIndexHitCount();
1061  }
1062
1063  @Override
1064  public long getFileInfoHitCount() {
1065    if (this.cacheStats == null) {
1066      return 0;
1067    }
1068    return cacheStats.getFileInfoHitCount();
1069  }
1070
1071  @Override
1072  public long getGeneralBloomMetaHitCount() {
1073    if (this.cacheStats == null) {
1074      return 0;
1075    }
1076    return cacheStats.getGeneralBloomMetaHitCount();
1077  }
1078
1079  @Override
1080  public long getDeleteFamilyBloomHitCount() {
1081    if (this.cacheStats == null) {
1082      return 0;
1083    }
1084    return cacheStats.getDeleteFamilyBloomHitCount();
1085  }
1086
1087  @Override
1088  public long getTrailerHitCount() {
1089    if (this.cacheStats == null) {
1090      return 0;
1091    }
1092    return cacheStats.getTrailerHitCount();
1093  }
1094}