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.io.IOException;
21  import java.util.Collection;
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.ServerName;
33  import org.apache.hadoop.hbase.io.hfile.BlockCache;
34  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
35  import org.apache.hadoop.hbase.io.hfile.CacheStats;
36  import org.apache.hadoop.hbase.wal.DefaultWALProvider;
37  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
38  import org.apache.hadoop.hbase.util.FSUtils;
39  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
40  import org.apache.hadoop.hdfs.DFSHedgedReadMetrics;
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 long flushedCellsCount = 0;
74    private volatile long compactedCellsCount = 0;
75    private volatile long majorCompactedCellsCount = 0;
76    private volatile long flushedCellsSize = 0;
77    private volatile long compactedCellsSize = 0;
78    private volatile long majorCompactedCellsSize = 0;
79    private volatile long blockedRequestsCount = 0L;
80  
81    private CacheStats cacheStats;
82    private ScheduledExecutorService executor;
83    private Runnable runnable;
84    private long period;
85  
86    /**
87     * Can be null if not on hdfs.
88     */
89    private DFSHedgedReadMetrics dfsHedgedReadMetrics;
90  
91    public MetricsRegionServerWrapperImpl(final HRegionServer regionServer) {
92      this.regionServer = regionServer;
93      initBlockCache();
94  
95      this.period =
96          regionServer.conf.getLong(HConstants.REGIONSERVER_METRICS_PERIOD,
97            HConstants.DEFAULT_REGIONSERVER_METRICS_PERIOD);
98  
99      this.executor = CompatibilitySingletonFactory.getInstance(MetricsExecutor.class).getExecutor();
100     this.runnable = new RegionServerMetricsWrapperRunnable();
101     this.executor.scheduleWithFixedDelay(this.runnable, this.period, this.period,
102       TimeUnit.MILLISECONDS);
103 
104     try {
105       this.dfsHedgedReadMetrics = FSUtils.getDFSHedgedReadMetrics(regionServer.getConfiguration());
106     } catch (IOException e) {
107       LOG.warn("Failed to get hedged metrics", e);
108     }
109     if (LOG.isInfoEnabled()) {
110       LOG.info("Computing regionserver metrics every " + this.period + " milliseconds");
111     }
112   }
113 
114   /**
115    * It's possible that due to threading the block cache could not be initialized
116    * yet (testing multiple region servers in one jvm).  So we need to try and initialize
117    * the blockCache and cacheStats reference multiple times until we succeed.
118    */
119   private synchronized  void initBlockCache() {
120     CacheConfig cacheConfig = this.regionServer.cacheConfig;
121     if (cacheConfig != null && this.blockCache == null) {
122       this.blockCache = cacheConfig.getBlockCache();
123     }
124 
125     if (this.blockCache != null && this.cacheStats == null) {
126       this.cacheStats = blockCache.getStats();
127     }
128   }
129 
130   @Override
131   public String getClusterId() {
132     return regionServer.getClusterId();
133   }
134 
135   @Override
136   public long getStartCode() {
137     return regionServer.getStartcode();
138   }
139 
140   @Override
141   public String getZookeeperQuorum() {
142     ZooKeeperWatcher zk = regionServer.getZooKeeper();
143     if (zk == null) {
144       return "";
145     }
146     return zk.getQuorum();
147   }
148 
149   @Override
150   public String getCoprocessors() {
151     String[] coprocessors = regionServer.getRegionServerCoprocessors();
152     if (coprocessors == null || coprocessors.length == 0) {
153       return "";
154     }
155     return StringUtils.join(coprocessors, ", ");
156   }
157 
158   @Override
159   public String getServerName() {
160     ServerName serverName = regionServer.getServerName();
161     if (serverName == null) {
162       return "";
163     }
164     return serverName.getServerName();
165   }
166 
167   @Override
168   public long getNumOnlineRegions() {
169     Collection<HRegion> onlineRegionsLocalContext = regionServer.getOnlineRegionsLocalContext();
170     if (onlineRegionsLocalContext == null) {
171       return 0;
172     }
173     return onlineRegionsLocalContext.size();
174   }
175 
176   @Override
177   public long getTotalRequestCount() {
178     return regionServer.rpcServices.requestCount.get();
179   }
180 
181   @Override
182   public int getCompactionQueueSize() {
183     //The thread could be zero.  if so assume there is no queue.
184     if (this.regionServer.compactSplitThread == null) {
185       return 0;
186     }
187     return this.regionServer.compactSplitThread.getCompactionQueueSize();
188   }
189 
190   @Override
191   public int getSmallCompactionQueueSize() {
192     //The thread could be zero.  if so assume there is no queue.
193     if (this.regionServer.compactSplitThread == null) {
194       return 0;
195     }
196     return this.regionServer.compactSplitThread.getSmallCompactionQueueSize();
197   }
198 
199   @Override
200   public int getLargeCompactionQueueSize() {
201     //The thread could be zero.  if so assume there is no queue.
202     if (this.regionServer.compactSplitThread == null) {
203       return 0;
204     }
205     return this.regionServer.compactSplitThread.getLargeCompactionQueueSize();
206   }
207 
208   @Override
209   public int getFlushQueueSize() {
210     //If there is no flusher there should be no queue.
211     if (this.regionServer.cacheFlusher == null) {
212       return 0;
213     }
214     return this.regionServer.cacheFlusher.getFlushQueueSize();
215   }
216 
217   @Override
218   public long getBlockCacheCount() {
219     if (this.blockCache == null) {
220       return 0;
221     }
222     return this.blockCache.getBlockCount();
223   }
224 
225   @Override
226   public long getBlockCacheSize() {
227     if (this.blockCache == null) {
228       return 0;
229     }
230     return this.blockCache.getCurrentSize();
231   }
232 
233   @Override
234   public long getBlockCacheFreeSize() {
235     if (this.blockCache == null) {
236       return 0;
237     }
238     return this.blockCache.getFreeSize();
239   }
240 
241   @Override
242   public long getBlockCacheHitCount() {
243     if (this.cacheStats == null) {
244       return 0;
245     }
246     return this.cacheStats.getHitCount();
247   }
248 
249   @Override
250   public long getBlockCacheMissCount() {
251     if (this.cacheStats == null) {
252       return 0;
253     }
254     return this.cacheStats.getMissCount();
255   }
256 
257   @Override
258   public long getBlockCacheEvictedCount() {
259     if (this.cacheStats == null) {
260       return 0;
261     }
262     return this.cacheStats.getEvictedCount();
263   }
264 
265   @Override
266   public double getBlockCacheHitPercent() {
267     if (this.cacheStats == null) {
268       return 0;
269     }
270     return (int) (this.cacheStats.getHitRatio() * 100);
271   }
272 
273   @Override
274   public int getBlockCacheHitCachingPercent() {
275     if (this.cacheStats == null) {
276       return 0;
277     }
278     return (int) (this.cacheStats.getHitCachingRatio() * 100);
279   }
280 
281   @Override public void forceRecompute() {
282     this.runnable.run();
283   }
284 
285   @Override
286   public long getNumStores() {
287     return numStores;
288   }
289   
290   @Override
291   public long getNumWALFiles() {
292     return numWALFiles;
293   }
294 
295   @Override
296   public long getWALFileSize() {
297     return walFileSize;
298   }
299   
300   @Override
301   public long getNumStoreFiles() {
302     return numStoreFiles;
303   }
304 
305   @Override
306   public long getMemstoreSize() {
307     return memstoreSize;
308   }
309 
310   @Override
311   public long getStoreFileSize() {
312     return storeFileSize;
313   }
314 
315   @Override public double getRequestsPerSecond() {
316     return requestsPerSecond;
317   }
318 
319   @Override
320   public long getReadRequestsCount() {
321     return readRequestsCount;
322   }
323 
324   @Override
325   public long getWriteRequestsCount() {
326     return writeRequestsCount;
327   }
328 
329   @Override
330   public long getCheckAndMutateChecksFailed() {
331     return checkAndMutateChecksFailed;
332   }
333 
334   @Override
335   public long getCheckAndMutateChecksPassed() {
336     return checkAndMutateChecksPassed;
337   }
338 
339   @Override
340   public long getStoreFileIndexSize() {
341     return storefileIndexSize;
342   }
343 
344   @Override
345   public long getTotalStaticIndexSize() {
346     return totalStaticIndexSize;
347   }
348 
349   @Override
350   public long getTotalStaticBloomSize() {
351     return totalStaticBloomSize;
352   }
353 
354   @Override
355   public long getNumMutationsWithoutWAL() {
356     return numMutationsWithoutWAL;
357   }
358 
359   @Override
360   public long getDataInMemoryWithoutWAL() {
361     return dataInMemoryWithoutWAL;
362   }
363 
364   @Override
365   public int getPercentFileLocal() {
366     return percentFileLocal;
367   }
368 
369   @Override
370   public long getUpdatesBlockedTime() {
371     if (this.regionServer.cacheFlusher == null) {
372       return 0;
373     }
374     return this.regionServer.cacheFlusher.getUpdatesBlockedMsHighWater().get();
375   }
376 
377   @Override
378   public long getFlushedCellsCount() {
379     return flushedCellsCount;
380   }
381 
382   @Override
383   public long getCompactedCellsCount() {
384     return compactedCellsCount;
385   }
386 
387   @Override
388   public long getMajorCompactedCellsCount() {
389     return majorCompactedCellsCount;
390   }
391 
392   @Override
393   public long getFlushedCellsSize() {
394     return flushedCellsSize;
395   }
396 
397   @Override
398   public long getCompactedCellsSize() {
399     return compactedCellsSize;
400   }
401 
402   @Override
403   public long getMajorCompactedCellsSize() {
404     return majorCompactedCellsSize;
405   }
406 
407   /**
408    * This is the runnable that will be executed on the executor every PERIOD number of seconds
409    * It will take metrics/numbers from all of the regions and use them to compute point in
410    * time metrics.
411    */
412   public class RegionServerMetricsWrapperRunnable implements Runnable {
413 
414     private long lastRan = 0;
415     private long lastRequestCount = 0;
416 
417     @Override
418     synchronized public void run() {
419       initBlockCache();
420       cacheStats = blockCache.getStats();
421 
422       HDFSBlocksDistribution hdfsBlocksDistribution =
423           new HDFSBlocksDistribution();
424 
425       long tempNumStores = 0;
426       long tempNumStoreFiles = 0;
427       long tempMemstoreSize = 0;
428       long tempStoreFileSize = 0;
429       long tempReadRequestsCount = 0;
430       long tempWriteRequestsCount = 0;
431       long tempCheckAndMutateChecksFailed = 0;
432       long tempCheckAndMutateChecksPassed = 0;
433       long tempStorefileIndexSize = 0;
434       long tempTotalStaticIndexSize = 0;
435       long tempTotalStaticBloomSize = 0;
436       long tempNumMutationsWithoutWAL = 0;
437       long tempDataInMemoryWithoutWAL = 0;
438       int tempPercentFileLocal = 0;
439       long tempFlushedCellsCount = 0;
440       long tempCompactedCellsCount = 0;
441       long tempMajorCompactedCellsCount = 0;
442       long tempFlushedCellsSize = 0;
443       long tempCompactedCellsSize = 0;
444       long tempMajorCompactedCellsSize = 0;
445       long tempBlockedRequestsCount = 0L;
446 
447       for (HRegion r : regionServer.getOnlineRegionsLocalContext()) {
448         tempNumMutationsWithoutWAL += r.numMutationsWithoutWAL.get();
449         tempDataInMemoryWithoutWAL += r.dataInMemoryWithoutWAL.get();
450         tempReadRequestsCount += r.readRequestsCount.get();
451         tempWriteRequestsCount += r.writeRequestsCount.get();
452         tempCheckAndMutateChecksFailed += r.checkAndMutateChecksFailed.get();
453         tempCheckAndMutateChecksPassed += r.checkAndMutateChecksPassed.get();
454         tempBlockedRequestsCount += r.getBlockedRequestsCount();
455         tempNumStores += r.stores.size();
456         for (Store store : r.stores.values()) {
457           tempNumStoreFiles += store.getStorefilesCount();
458           tempMemstoreSize += store.getMemStoreSize();
459           tempStoreFileSize += store.getStorefilesSize();
460           tempStorefileIndexSize += store.getStorefilesIndexSize();
461           tempTotalStaticBloomSize += store.getTotalStaticBloomSize();
462           tempTotalStaticIndexSize += store.getTotalStaticIndexSize();
463           tempFlushedCellsCount += store.getFlushedCellsCount();
464           tempCompactedCellsCount += store.getCompactedCellsCount();
465           tempMajorCompactedCellsCount += store.getMajorCompactedCellsCount();
466           tempFlushedCellsSize += store.getFlushedCellsSize();
467           tempCompactedCellsSize += store.getCompactedCellsSize();
468           tempMajorCompactedCellsSize += store.getMajorCompactedCellsSize();
469         }
470 
471         hdfsBlocksDistribution.add(r.getHDFSBlocksDistribution());
472       }
473 
474       float localityIndex = hdfsBlocksDistribution.getBlockLocalityIndex(
475           regionServer.getServerName().getHostname());
476       tempPercentFileLocal = (int) (localityIndex * 100);
477 
478 
479       //Compute the number of requests per second
480       long currentTime = EnvironmentEdgeManager.currentTime();
481 
482       // assume that it took PERIOD seconds to start the executor.
483       // this is a guess but it's a pretty good one.
484       if (lastRan == 0) {
485         lastRan = currentTime - period;
486       }
487 
488 
489       //If we've time traveled keep the last requests per second.
490       if ((currentTime - lastRan) > 0) {
491         long currentRequestCount = getTotalRequestCount();
492         requestsPerSecond = (currentRequestCount - lastRequestCount) / ((currentTime - lastRan) / 1000.0);
493         lastRequestCount = currentRequestCount;
494       }
495       lastRan = currentTime;
496 
497       numWALFiles = DefaultWALProvider.getNumLogFiles(regionServer.walFactory);
498       walFileSize = DefaultWALProvider.getLogFileSize(regionServer.walFactory);
499 
500       //Copy over computed values so that no thread sees half computed values.
501       numStores = tempNumStores;
502       numStoreFiles = tempNumStoreFiles;
503       memstoreSize = tempMemstoreSize;
504       storeFileSize = tempStoreFileSize;
505       readRequestsCount = tempReadRequestsCount;
506       writeRequestsCount = tempWriteRequestsCount;
507       checkAndMutateChecksFailed = tempCheckAndMutateChecksFailed;
508       checkAndMutateChecksPassed = tempCheckAndMutateChecksPassed;
509       storefileIndexSize = tempStorefileIndexSize;
510       totalStaticIndexSize = tempTotalStaticIndexSize;
511       totalStaticBloomSize = tempTotalStaticBloomSize;
512       numMutationsWithoutWAL = tempNumMutationsWithoutWAL;
513       dataInMemoryWithoutWAL = tempDataInMemoryWithoutWAL;
514       percentFileLocal = tempPercentFileLocal;
515       flushedCellsCount = tempFlushedCellsCount;
516       compactedCellsCount = tempCompactedCellsCount;
517       majorCompactedCellsCount = tempMajorCompactedCellsCount;
518       flushedCellsSize = tempFlushedCellsSize;
519       compactedCellsSize = tempCompactedCellsSize;
520       majorCompactedCellsSize = tempMajorCompactedCellsSize;
521       blockedRequestsCount = tempBlockedRequestsCount;
522     }
523   }
524 
525   @Override
526   public long getHedgedReadOps() {
527     return this.dfsHedgedReadMetrics == null? 0: this.dfsHedgedReadMetrics.getHedgedReadOps();
528   }
529 
530   @Override
531   public long getHedgedReadWins() {
532     return this.dfsHedgedReadMetrics == null? 0: this.dfsHedgedReadMetrics.getHedgedReadWins();
533   }
534 
535   @Override
536   public long getBlockedRequestsCount() {
537     return blockedRequestsCount;
538   }
539 }