View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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.BoundedRegionGroupingProvider;
38  import org.apache.hadoop.hbase.wal.DefaultWALProvider;
39  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
40  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
41  import org.apache.hadoop.metrics2.MetricsExecutor;
42  
43  /**
44   * Impl for exposing HRegionServer Information through Hadoop's metrics 2 system.
45   */
46  @InterfaceAudience.Private
47  class MetricsRegionServerWrapperImpl
48      implements MetricsRegionServerWrapper {
49  
50    public static final Log LOG = LogFactory.getLog(MetricsRegionServerWrapperImpl.class);
51  
52    private final HRegionServer regionServer;
53  
54    private BlockCache blockCache;
55  
56    private volatile long numStores = 0;
57    private volatile long numWALFiles = 0;
58    private volatile long walFileSize = 0;
59    private volatile long numStoreFiles = 0;
60    private volatile long memstoreSize = 0;
61    private volatile long storeFileSize = 0;
62    private volatile double requestsPerSecond = 0.0;
63    private volatile long readRequestsCount = 0;
64    private volatile long writeRequestsCount = 0;
65    private volatile long checkAndMutateChecksFailed = 0;
66    private volatile long checkAndMutateChecksPassed = 0;
67    private volatile long storefileIndexSize = 0;
68    private volatile long totalStaticIndexSize = 0;
69    private volatile long totalStaticBloomSize = 0;
70    private volatile long numMutationsWithoutWAL = 0;
71    private volatile long dataInMemoryWithoutWAL = 0;
72    private volatile int percentFileLocal = 0;
73    private volatile int percentFileLocalSecondaryRegions = 0;
74    private volatile long flushedCellsCount = 0;
75    private volatile long compactedCellsCount = 0;
76    private volatile long majorCompactedCellsCount = 0;
77    private volatile long flushedCellsSize = 0;
78    private volatile long compactedCellsSize = 0;
79    private volatile long majorCompactedCellsSize = 0;
80    private volatile long blockedRequestsCount = 0L;
81  
82    private CacheStats cacheStats;
83    private ScheduledExecutorService executor;
84    private Runnable runnable;
85    private long period;
86  
87    public MetricsRegionServerWrapperImpl(final HRegionServer regionServer) {
88      this.regionServer = regionServer;
89      initBlockCache();
90  
91      this.period =
92          regionServer.conf.getLong(HConstants.REGIONSERVER_METRICS_PERIOD,
93            HConstants.DEFAULT_REGIONSERVER_METRICS_PERIOD);
94  
95      this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
96      this.runnable = new RegionServerMetricsWrapperRunnable();
97      this.executor.scheduleWithFixedDelay(this.runnable, this.period, this.period,
98        TimeUnit.MILLISECONDS);
99  
100     if (LOG.isInfoEnabled()) {
101       LOG.info("Computing regionserver metrics every " + this.period + " milliseconds");
102     }
103   }
104 
105   /**
106    * It's possible that due to threading the block cache could not be initialized
107    * yet (testing multiple region servers in one jvm).  So we need to try and initialize
108    * the blockCache and cacheStats reference multiple times until we succeed.
109    */
110   private synchronized  void initBlockCache() {
111     CacheConfig cacheConfig = this.regionServer.cacheConfig;
112     if (cacheConfig != null && this.blockCache == null) {
113       this.blockCache = cacheConfig.getBlockCache();
114     }
115 
116     if (this.blockCache != null && this.cacheStats == null) {
117       this.cacheStats = blockCache.getStats();
118     }
119   }
120 
121   @Override
122   public String getClusterId() {
123     return regionServer.getClusterId();
124   }
125 
126   @Override
127   public long getStartCode() {
128     return regionServer.getStartcode();
129   }
130 
131   @Override
132   public String getZookeeperQuorum() {
133     ZooKeeperWatcher zk = regionServer.getZooKeeper();
134     if (zk == null) {
135       return "";
136     }
137     return zk.getQuorum();
138   }
139 
140   @Override
141   public String getCoprocessors() {
142     String[] coprocessors = regionServer.getRegionServerCoprocessors();
143     if (coprocessors == null || coprocessors.length == 0) {
144       return "";
145     }
146     return StringUtils.join(coprocessors, ", ");
147   }
148 
149   @Override
150   public String getServerName() {
151     ServerName serverName = regionServer.getServerName();
152     if (serverName == null) {
153       return "";
154     }
155     return serverName.getServerName();
156   }
157 
158   @Override
159   public long getNumOnlineRegions() {
160     Collection<Region> onlineRegionsLocalContext = regionServer.getOnlineRegionsLocalContext();
161     if (onlineRegionsLocalContext == null) {
162       return 0;
163     }
164     return onlineRegionsLocalContext.size();
165   }
166 
167   @Override
168   public long getTotalRequestCount() {
169     return regionServer.rpcServices.requestCount.get();
170   }
171 
172   @Override
173   public int getSplitQueueSize() {
174     if (this.regionServer.compactSplitThread == null) {
175       return 0;
176     }
177     return this.regionServer.compactSplitThread.getSplitQueueSize();
178   }
179 
180   @Override
181   public int getCompactionQueueSize() {
182     //The thread could be zero.  if so assume there is no queue.
183     if (this.regionServer.compactSplitThread == null) {
184       return 0;
185     }
186     return this.regionServer.compactSplitThread.getCompactionQueueSize();
187   }
188 
189   @Override
190   public int getSmallCompactionQueueSize() {
191     //The thread could be zero.  if so assume there is no queue.
192     if (this.regionServer.compactSplitThread == null) {
193       return 0;
194     }
195     return this.regionServer.compactSplitThread.getSmallCompactionQueueSize();
196   }
197 
198   @Override
199   public int getLargeCompactionQueueSize() {
200     //The thread could be zero.  if so assume there is no queue.
201     if (this.regionServer.compactSplitThread == null) {
202       return 0;
203     }
204     return this.regionServer.compactSplitThread.getLargeCompactionQueueSize();
205   }
206 
207   @Override
208   public int getFlushQueueSize() {
209     //If there is no flusher there should be no queue.
210     if (this.regionServer.cacheFlusher == null) {
211       return 0;
212     }
213     return this.regionServer.cacheFlusher.getFlushQueueSize();
214   }
215 
216   @Override
217   public long getBlockCacheCount() {
218     if (this.blockCache == null) {
219       return 0;
220     }
221     return this.blockCache.getBlockCount();
222   }
223 
224   @Override
225   public long getBlockCacheSize() {
226     if (this.blockCache == null) {
227       return 0;
228     }
229     return this.blockCache.getCurrentSize();
230   }
231 
232   @Override
233   public long getBlockCacheFreeSize() {
234     if (this.blockCache == null) {
235       return 0;
236     }
237     return this.blockCache.getFreeSize();
238   }
239 
240   @Override
241   public long getBlockCacheHitCount() {
242     if (this.cacheStats == null) {
243       return 0;
244     }
245     return this.cacheStats.getHitCount();
246   }
247 
248   @Override
249   public long getBlockCacheMissCount() {
250     if (this.cacheStats == null) {
251       return 0;
252     }
253     return this.cacheStats.getMissCount();
254   }
255 
256   @Override
257   public long getBlockCacheEvictedCount() {
258     if (this.cacheStats == null) {
259       return 0;
260     }
261     return this.cacheStats.getEvictedCount();
262   }
263 
264   @Override
265   public double getBlockCacheHitPercent() {
266     if (this.cacheStats == null) {
267       return 0;
268     }
269     return (int) (this.cacheStats.getHitRatio() * 100);
270   }
271 
272   @Override
273   public int getBlockCacheHitCachingPercent() {
274     if (this.cacheStats == null) {
275       return 0;
276     }
277     return (int) (this.cacheStats.getHitCachingRatio() * 100);
278   }
279 
280   @Override public void forceRecompute() {
281     this.runnable.run();
282   }
283 
284   @Override
285   public long getNumStores() {
286     return numStores;
287   }
288   
289   @Override
290   public long getNumWALFiles() {
291     return numWALFiles;
292   }
293 
294   @Override
295   public long getWALFileSize() {
296     return walFileSize;
297   }
298   
299   @Override
300   public long getNumStoreFiles() {
301     return numStoreFiles;
302   }
303 
304   @Override
305   public long getMemstoreSize() {
306     return memstoreSize;
307   }
308 
309   @Override
310   public long getStoreFileSize() {
311     return storeFileSize;
312   }
313 
314   @Override public double getRequestsPerSecond() {
315     return requestsPerSecond;
316   }
317 
318   @Override
319   public long getReadRequestsCount() {
320     return readRequestsCount;
321   }
322 
323   @Override
324   public long getWriteRequestsCount() {
325     return writeRequestsCount;
326   }
327 
328   @Override
329   public long getCheckAndMutateChecksFailed() {
330     return checkAndMutateChecksFailed;
331   }
332 
333   @Override
334   public long getCheckAndMutateChecksPassed() {
335     return checkAndMutateChecksPassed;
336   }
337 
338   @Override
339   public long getStoreFileIndexSize() {
340     return storefileIndexSize;
341   }
342 
343   @Override
344   public long getTotalStaticIndexSize() {
345     return totalStaticIndexSize;
346   }
347 
348   @Override
349   public long getTotalStaticBloomSize() {
350     return totalStaticBloomSize;
351   }
352 
353   @Override
354   public long getNumMutationsWithoutWAL() {
355     return numMutationsWithoutWAL;
356   }
357 
358   @Override
359   public long getDataInMemoryWithoutWAL() {
360     return dataInMemoryWithoutWAL;
361   }
362 
363   @Override
364   public int getPercentFileLocal() {
365     return percentFileLocal;
366   }
367 
368   @Override
369   public int getPercentFileLocalSecondaryRegions() {
370     return percentFileLocalSecondaryRegions;
371   }
372 
373   @Override
374   public long getUpdatesBlockedTime() {
375     if (this.regionServer.cacheFlusher == null) {
376       return 0;
377     }
378     return this.regionServer.cacheFlusher.getUpdatesBlockedMsHighWater().get();
379   }
380 
381   @Override
382   public long getFlushedCellsCount() {
383     return flushedCellsCount;
384   }
385 
386   @Override
387   public long getCompactedCellsCount() {
388     return compactedCellsCount;
389   }
390 
391   @Override
392   public long getMajorCompactedCellsCount() {
393     return majorCompactedCellsCount;
394   }
395 
396   @Override
397   public long getFlushedCellsSize() {
398     return flushedCellsSize;
399   }
400 
401   @Override
402   public long getCompactedCellsSize() {
403     return compactedCellsSize;
404   }
405 
406   @Override
407   public long getMajorCompactedCellsSize() {
408     return majorCompactedCellsSize;
409   }
410 
411   /**
412    * This is the runnable that will be executed on the executor every PERIOD number of seconds
413    * It will take metrics/numbers from all of the regions and use them to compute point in
414    * time metrics.
415    */
416   public class RegionServerMetricsWrapperRunnable implements Runnable {
417 
418     private long lastRan = 0;
419     private long lastRequestCount = 0;
420 
421     @Override
422     synchronized public void run() {
423       initBlockCache();
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       //Compute the number of requests per second
494       long currentTime = EnvironmentEdgeManager.currentTime();
495 
496       // assume that it took PERIOD seconds to start the executor.
497       // this is a guess but it's a pretty good one.
498       if (lastRan == 0) {
499         lastRan = currentTime - period;
500       }
501 
502 
503       //If we've time traveled keep the last requests per second.
504       if ((currentTime - lastRan) > 0) {
505         long currentRequestCount = getTotalRequestCount();
506         requestsPerSecond = (currentRequestCount - lastRequestCount) /
507             ((currentTime - lastRan) / 1000.0);
508         lastRequestCount = currentRequestCount;
509       }
510       lastRan = currentTime;
511 
512       numWALFiles = DefaultWALProvider.getNumLogFiles(regionServer.walFactory) +
513           BoundedRegionGroupingProvider.getNumLogFiles(regionServer.walFactory);
514       walFileSize = DefaultWALProvider.getLogFileSize(regionServer.walFactory) +
515           BoundedRegionGroupingProvider.getLogFileSize(regionServer.walFactory);
516       //Copy over computed values so that no thread sees half computed values.
517       numStores = tempNumStores;
518       numStoreFiles = tempNumStoreFiles;
519       memstoreSize = tempMemstoreSize;
520       storeFileSize = tempStoreFileSize;
521       readRequestsCount = tempReadRequestsCount;
522       writeRequestsCount = tempWriteRequestsCount;
523       checkAndMutateChecksFailed = tempCheckAndMutateChecksFailed;
524       checkAndMutateChecksPassed = tempCheckAndMutateChecksPassed;
525       storefileIndexSize = tempStorefileIndexSize;
526       totalStaticIndexSize = tempTotalStaticIndexSize;
527       totalStaticBloomSize = tempTotalStaticBloomSize;
528       numMutationsWithoutWAL = tempNumMutationsWithoutWAL;
529       dataInMemoryWithoutWAL = tempDataInMemoryWithoutWAL;
530       percentFileLocal = tempPercentFileLocal;
531       percentFileLocalSecondaryRegions = tempPercentFileLocalSecondaryRegions;
532       flushedCellsCount = tempFlushedCellsCount;
533       compactedCellsCount = tempCompactedCellsCount;
534       majorCompactedCellsCount = tempMajorCompactedCellsCount;
535       flushedCellsSize = tempFlushedCellsSize;
536       compactedCellsSize = tempCompactedCellsSize;
537       majorCompactedCellsSize = tempMajorCompactedCellsSize;
538       blockedRequestsCount = tempBlockedRequestsCount;
539     }
540   }
541 
542   @Override
543   public long getBlockedRequestsCount() {
544     return blockedRequestsCount;
545   }
546 }