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