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 org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.hbase.*;
22  import org.apache.hadoop.hbase.client.*;
23  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
24  import org.apache.hadoop.hbase.testclassification.MediumTests;
25  import org.apache.hadoop.hbase.util.Bytes;
26  import org.apache.hadoop.hbase.util.Threads;
27  import org.apache.log4j.Level;
28  import org.apache.log4j.Logger;
29  import org.junit.AfterClass;
30  import org.junit.BeforeClass;
31  import org.junit.Ignore;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  import static org.junit.Assert.*;
36  
37  import java.io.IOException;
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  
42  @Category(MediumTests.class)
43  public class TestRegionServerMetrics {
44    private static MetricsAssertHelper metricsHelper;
45  
46    static {
47      Logger.getLogger("org.apache.hadoop.hbase").setLevel(Level.DEBUG);
48    }
49  
50    private static MiniHBaseCluster cluster;
51    private static HRegionServer rs;
52    private static Configuration conf;
53    private static HBaseTestingUtility TEST_UTIL;
54    private static MetricsRegionServer metricsRegionServer;
55    private static MetricsRegionServerSource serverSource;
56    private static final int NUM_SCAN_NEXT = 30;
57    private static int numScanNext = 0;
58  
59    @BeforeClass
60    public static void startCluster() throws Exception {
61      metricsHelper = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
62      TEST_UTIL = new HBaseTestingUtility();
63      conf = TEST_UTIL.getConfiguration();
64      conf.getLong("hbase.splitlog.max.resubmit", 0);
65      // Make the failure test faster
66      conf.setInt("zookeeper.recovery.retry", 0);
67      conf.setInt(HConstants.REGIONSERVER_INFO_PORT, -1);
68  
69      TEST_UTIL.startMiniCluster(1, 1);
70      cluster = TEST_UTIL.getHBaseCluster();
71  
72      cluster.waitForActiveAndReadyMaster();
73  
74      while (cluster.getLiveRegionServerThreads().size() < 1) {
75        Threads.sleep(100);
76      }
77  
78      rs = cluster.getRegionServer(0);
79      metricsRegionServer = rs.getRegionServerMetrics();
80      serverSource = metricsRegionServer.getMetricsSource();
81    }
82  
83    @AfterClass
84    public static void after() throws Exception {
85      if (TEST_UTIL != null) {
86        TEST_UTIL.shutdownMiniCluster();
87      }
88    }
89  
90    @Test(timeout = 300000)
91    public void testRegionCount() throws Exception {
92      String regionMetricsKey = "regionCount";
93      long regions = metricsHelper.getGaugeLong(regionMetricsKey, serverSource);
94      // Creating a table should add one region
95      TEST_UTIL.createTable(TableName.valueOf("table"), Bytes.toBytes("cf"));
96      metricsHelper.assertGaugeGt(regionMetricsKey, regions, serverSource);
97    }
98  
99    @Test
100   public void testLocalFiles() throws Exception {
101     metricsHelper.assertGauge("percentFilesLocal", 0, serverSource);
102     metricsHelper.assertGauge("percentFilesLocalSecondaryRegions", 0, serverSource);
103   }
104 
105   @Test
106   public void testRequestCount() throws Exception {
107     String tableNameString = "testRequestCount";
108     TableName tName = TableName.valueOf(tableNameString);
109     byte[] cfName = Bytes.toBytes("d");
110     byte[] row = Bytes.toBytes("rk");
111     byte[] qualifier = Bytes.toBytes("qual");
112     byte[] initValue = Bytes.toBytes("Value");
113 
114     TEST_UTIL.createTable(tName, cfName);
115 
116     Connection connection = TEST_UTIL.getConnection();
117     connection.getTable(tName).close(); //wait for the table to come up.
118 
119     // Do a first put to be sure that the connection is established, meta is there and so on.
120     Table table = connection.getTable(tName);
121     Put p = new Put(row);
122     p.add(cfName, qualifier, initValue);
123     table.put(p);
124 
125     metricsRegionServer.getRegionServerWrapper().forceRecompute();
126     long requests = metricsHelper.getCounter("totalRequestCount", serverSource);
127     long readRequests = metricsHelper.getCounter("readRequestCount", serverSource);
128     long writeRequests = metricsHelper.getCounter("writeRequestCount", serverSource);
129 
130     for (int i=0; i< 30; i++) {
131       table.put(p);
132     }
133 
134     metricsRegionServer.getRegionServerWrapper().forceRecompute();
135     metricsHelper.assertCounter("totalRequestCount", requests + 30, serverSource);
136     metricsHelper.assertCounter("readRequestCount", readRequests, serverSource);
137     metricsHelper.assertCounter("writeRequestCount", writeRequests + 30, serverSource);
138 
139     Get g = new Get(row);
140     for (int i=0; i< 10; i++) {
141       table.get(g);
142     }
143 
144     metricsRegionServer.getRegionServerWrapper().forceRecompute();
145     metricsHelper.assertCounter("totalRequestCount", requests + 40, serverSource);
146     metricsHelper.assertCounter("readRequestCount", readRequests + 10, serverSource);
147     metricsHelper.assertCounter("writeRequestCount", writeRequests + 30, serverSource);
148 
149     try (RegionLocator locator = connection.getRegionLocator(tName)) {
150       for ( HRegionLocation location: locator.getAllRegionLocations()) {
151         HRegionInfo i = location.getRegionInfo();
152         MetricsRegionAggregateSource agg = rs.getRegion(i.getRegionName())
153             .getMetrics()
154             .getSource()
155             .getAggregateSource();
156         String prefix = "namespace_"+NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR+
157             "_table_"+tableNameString +
158             "_region_" + i.getEncodedName()+
159             "_metric";
160         metricsHelper.assertCounter(prefix + "_getCount", 10, agg);
161         metricsHelper.assertCounter(prefix + "_mutateCount", 31, agg);
162       }
163     }
164     List<Get> gets = new ArrayList<Get>();
165     for (int i=0; i< 10; i++) {
166       gets.add(new Get(row));
167     }
168     table.get(gets);
169 
170     // By default, master doesn't host meta now.
171     // Adding some meta related requests
172     requests += 3;
173     readRequests ++;
174 
175     metricsRegionServer.getRegionServerWrapper().forceRecompute();
176     metricsHelper.assertCounter("totalRequestCount", requests + 50, serverSource);
177     metricsHelper.assertCounter("readRequestCount", readRequests + 20, serverSource);
178     metricsHelper.assertCounter("writeRequestCount", writeRequests + 30, serverSource);
179 
180     List<Put> puts = new ArrayList<>();
181     for (int i=0; i< 30; i++) {
182       puts.add(p);
183     }
184     table.put(puts);
185 
186     metricsRegionServer.getRegionServerWrapper().forceRecompute();
187     metricsHelper.assertCounter("totalRequestCount", requests + 80, serverSource);
188     metricsHelper.assertCounter("readRequestCount", readRequests + 20, serverSource);
189     metricsHelper.assertCounter("writeRequestCount", writeRequests + 60, serverSource);
190 
191     table.close();
192   }
193 
194   @Test
195   public void testGet() throws Exception {
196     String tableNameString = "testGet";
197     TableName tName = TableName.valueOf(tableNameString);
198     byte[] cfName = Bytes.toBytes("d");
199     byte[] row = Bytes.toBytes("rk");
200     byte[] qualifier = Bytes.toBytes("qual");
201     byte[] initValue = Bytes.toBytes("Value");
202 
203     TEST_UTIL.createTable(tName, cfName);
204 
205     Connection connection = TEST_UTIL.getConnection();
206     connection.getTable(tName).close(); //wait for the table to come up.
207 
208     // Do a first put to be sure that the connection is established, meta is there and so on.
209     Table table = connection.getTable(tName);
210     Put p = new Put(row);
211     p.addColumn(cfName, qualifier, initValue);
212     table.put(p);
213 
214     Get g = new Get(row);
215     for (int i=0; i< 10; i++) {
216       table.get(g);
217     }
218 
219     metricsRegionServer.getRegionServerWrapper().forceRecompute();
220 
221     try (RegionLocator locator = connection.getRegionLocator(tName)) {
222       for ( HRegionLocation location: locator.getAllRegionLocations()) {
223         HRegionInfo i = location.getRegionInfo();
224         MetricsRegionAggregateSource agg = rs.getRegion(i.getRegionName())
225           .getMetrics()
226           .getSource()
227           .getAggregateSource();
228         String prefix = "namespace_"+NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR+
229           "_table_"+tableNameString +
230           "_region_" + i.getEncodedName()+
231           "_metric";
232         metricsHelper.assertCounter(prefix + "_getCount", 10, agg);
233       }
234       metricsHelper.assertCounterGt("Get_num_ops", 10, serverSource);
235     }
236     table.close();
237   }
238 
239   @Test
240   public void testMutationsWithoutWal() throws Exception {
241     TableName tableName = TableName.valueOf("testMutationsWithoutWal");
242     byte[] cf = Bytes.toBytes("d");
243     byte[] row = Bytes.toBytes("rk");
244     byte[] qualifier = Bytes.toBytes("qual");
245     byte[] val = Bytes.toBytes("Value");
246 
247     metricsRegionServer.getRegionServerWrapper().forceRecompute();
248 
249     TEST_UTIL.createTable(tableName, cf);
250 
251     Table t = new HTable(conf, tableName);
252 
253     Put p = new Put(row);
254     p.add(cf, qualifier, val);
255     p.setDurability(Durability.SKIP_WAL);
256 
257     t.put(p);
258 
259     metricsRegionServer.getRegionServerWrapper().forceRecompute();
260     metricsHelper.assertGauge("mutationsWithoutWALCount", 1, serverSource);
261     long minLength = row.length + cf.length + qualifier.length + val.length;
262     metricsHelper.assertGaugeGt("mutationsWithoutWALSize", minLength, serverSource);
263 
264     t.close();
265   }
266 
267   @Test
268   public void testStoreCount() throws Exception {
269     TableName tableName = TableName.valueOf("testStoreCount");
270     byte[] cf = Bytes.toBytes("d");
271     byte[] row = Bytes.toBytes("rk");
272     byte[] qualifier = Bytes.toBytes("qual");
273     byte[] val = Bytes.toBytes("Value");
274 
275     metricsRegionServer.getRegionServerWrapper().forceRecompute();
276     long stores = metricsHelper.getGaugeLong("storeCount", serverSource);
277     long storeFiles = metricsHelper.getGaugeLong("storeFileCount", serverSource);
278 
279     TEST_UTIL.createTable(tableName, cf);
280 
281     //Force a hfile.
282     Table t = new HTable(conf, tableName);
283     Put p = new Put(row);
284     p.add(cf, qualifier, val);
285     t.put(p);
286     TEST_UTIL.getHBaseAdmin().flush(tableName);
287 
288     metricsRegionServer.getRegionServerWrapper().forceRecompute();
289     metricsHelper.assertGauge("storeCount", stores +1, serverSource);
290     metricsHelper.assertGauge("storeFileCount", storeFiles + 1, serverSource);
291 
292     t.close();
293   }
294 
295   @Test
296   public void testStoreFileAge() throws Exception {
297     TableName tableName = TableName.valueOf("testStoreFileAge");
298     byte[] cf = Bytes.toBytes("d");
299     byte[] row = Bytes.toBytes("rk");
300     byte[] qualifier = Bytes.toBytes("qual");
301     byte[] val = Bytes.toBytes("Value");
302 
303     //Force a hfile.
304     Table t = TEST_UTIL.createTable(tableName, cf);
305     Put p = new Put(row);
306     p.addColumn(cf, qualifier, val);
307     t.put(p);
308     TEST_UTIL.getHBaseAdmin().flush(tableName);
309 
310     metricsRegionServer.getRegionServerWrapper().forceRecompute();
311     assertTrue(metricsHelper.getGaugeLong("maxStoreFileAge", serverSource) > 0);
312     assertTrue(metricsHelper.getGaugeLong("minStoreFileAge", serverSource) >= 0);
313     assertTrue(metricsHelper.getGaugeLong("avgStoreFileAge", serverSource) > 0);
314 
315     t.close();
316   }
317 
318   @Test
319   public void testCheckAndPutCount() throws Exception {
320     String tableNameString = "testCheckAndPutCount";
321     TableName tableName = TableName.valueOf(tableNameString);
322     byte[] cf = Bytes.toBytes("d");
323     byte[] row = Bytes.toBytes("rk");
324     byte[] qualifier = Bytes.toBytes("qual");
325     byte[] valOne = Bytes.toBytes("Value");
326     byte[] valTwo = Bytes.toBytes("ValueTwo");
327     byte[] valThree = Bytes.toBytes("ValueThree");
328 
329     TEST_UTIL.createTable(tableName, cf);
330     Table t = new HTable(conf, tableName);
331     Put p = new Put(row);
332     p.add(cf, qualifier, valOne);
333     t.put(p);
334 
335     Put pTwo = new Put(row);
336     pTwo.add(cf, qualifier, valTwo);
337     t.checkAndPut(row, cf, qualifier, valOne, pTwo);
338 
339     Put pThree = new Put(row);
340     pThree.add(cf, qualifier, valThree);
341     t.checkAndPut(row, cf, qualifier, valOne, pThree);
342 
343     metricsRegionServer.getRegionServerWrapper().forceRecompute();
344     metricsHelper.assertCounter("checkMutateFailedCount", 1, serverSource);
345     metricsHelper.assertCounter("checkMutatePassedCount", 1, serverSource);
346 
347     t.close();
348   }
349 
350   @Test
351   public void testIncrement() throws Exception {
352     String tableNameString = "testIncrement";
353     TableName tableName = TableName.valueOf(tableNameString);
354     byte[] cf = Bytes.toBytes("d");
355     byte[] row = Bytes.toBytes("rk");
356     byte[] qualifier = Bytes.toBytes("qual");
357     byte[] val = Bytes.toBytes(0l);
358 
359 
360     TEST_UTIL.createTable(tableName, cf);
361     Table t = new HTable(conf, tableName);
362 
363     Put p = new Put(row);
364     p.add(cf, qualifier, val);
365     t.put(p);
366 
367     for(int count = 0; count< 13; count++) {
368       Increment inc = new Increment(row);
369       inc.addColumn(cf, qualifier, 100);
370       t.increment(inc);
371     }
372 
373     metricsRegionServer.getRegionServerWrapper().forceRecompute();
374     metricsHelper.assertCounter("incrementNumOps", 13, serverSource);
375 
376     t.close();
377   }
378 
379   @Test
380   public void testAppend() throws Exception {
381     String tableNameString = "testAppend";
382     TableName tableName = TableName.valueOf(tableNameString);
383     byte[] cf = Bytes.toBytes("d");
384     byte[] row = Bytes.toBytes("rk");
385     byte[] qualifier = Bytes.toBytes("qual");
386     byte[] val = Bytes.toBytes("One");
387 
388 
389     TEST_UTIL.createTable(tableName, cf);
390     Table t = new HTable(conf, tableName);
391 
392     Put p = new Put(row);
393     p.add(cf, qualifier, val);
394     t.put(p);
395 
396     for(int count = 0; count< 73; count++) {
397       Append append = new Append(row);
398       append.add(cf, qualifier, Bytes.toBytes(",Test"));
399       t.append(append);
400     }
401 
402     metricsRegionServer.getRegionServerWrapper().forceRecompute();
403     metricsHelper.assertCounter("appendNumOps", 73, serverSource);
404 
405     t.close();
406   }
407 
408   @Test
409   public void testScanSize() throws IOException {
410     String tableNameString = "testScanSize";
411     TableName tableName = TableName.valueOf(tableNameString);
412     byte[] cf = Bytes.toBytes("d");
413     byte[] qualifier = Bytes.toBytes("qual");
414     byte[] val = Bytes.toBytes("One");
415 
416     List<Put> puts = new ArrayList<>();
417     for (int insertCount =0; insertCount < 100; insertCount++) {
418       Put p = new Put(Bytes.toBytes("" + insertCount + "row"));
419       p.add(cf, qualifier, val);
420       puts.add(p);
421     }
422     try (HTable t = TEST_UTIL.createTable(tableName, cf)) {
423       t.put(puts);
424 
425       Scan s = new Scan();
426       s.setBatch(1);
427       s.setCaching(1);
428       ResultScanner resultScanners = t.getScanner(s);
429 
430       for (int nextCount = 0; nextCount < NUM_SCAN_NEXT; nextCount++) {
431         Result result = resultScanners.next();
432         assertNotNull(result);
433         assertEquals(1, result.size());
434       }
435     }
436     numScanNext += NUM_SCAN_NEXT;
437     try (RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
438       for ( HRegionLocation location: locator.getAllRegionLocations()) {
439         HRegionInfo i = location.getRegionInfo();
440         MetricsRegionAggregateSource agg = rs.getRegion(i.getRegionName())
441             .getMetrics()
442             .getSource()
443             .getAggregateSource();
444         String prefix = "namespace_"+NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR+
445             "_table_"+tableNameString +
446             "_region_" + i.getEncodedName()+
447             "_metric";
448         metricsHelper.assertCounter(prefix + "_scanCount", NUM_SCAN_NEXT, agg);
449       }
450       metricsHelper.assertCounterGt("ScanSize_num_ops", numScanNext, serverSource);
451     }
452     try (Admin admin = TEST_UTIL.getHBaseAdmin()) {
453       admin.disableTable(tableName);
454       admin.deleteTable(tableName);
455     }
456   }
457 
458   @Test
459   public void testScanTime() throws IOException {
460     String tableNameString = "testScanTime";
461     TableName tableName = TableName.valueOf(tableNameString);
462     byte[] cf = Bytes.toBytes("d");
463     byte[] qualifier = Bytes.toBytes("qual");
464     byte[] val = Bytes.toBytes("One");
465 
466     List<Put> puts = new ArrayList<>();
467     for (int insertCount =0; insertCount < 100; insertCount++) {
468       Put p = new Put(Bytes.toBytes("" + insertCount + "row"));
469       p.addColumn(cf, qualifier, val);
470       puts.add(p);
471     }
472     try (Table t = TEST_UTIL.createTable(tableName, cf)) {
473       t.put(puts);
474 
475       Scan s = new Scan();
476       s.setBatch(1);
477       s.setCaching(1);
478       ResultScanner resultScanners = t.getScanner(s);
479 
480       for (int nextCount = 0; nextCount < NUM_SCAN_NEXT; nextCount++) {
481         Result result = resultScanners.next();
482         assertNotNull(result);
483         assertEquals(1, result.size());
484       }
485     }
486     numScanNext += NUM_SCAN_NEXT;
487     try (RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
488       for ( HRegionLocation location: locator.getAllRegionLocations()) {
489         HRegionInfo i = location.getRegionInfo();
490         MetricsRegionAggregateSource agg = rs.getRegion(i.getRegionName())
491           .getMetrics()
492           .getSource()
493           .getAggregateSource();
494         String prefix = "namespace_"+NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR+
495           "_table_"+tableNameString +
496           "_region_" + i.getEncodedName()+
497           "_metric";
498         metricsHelper.assertCounter(prefix + "_scanCount", NUM_SCAN_NEXT, agg);
499       }
500       metricsHelper.assertCounterGt("ScanTime_num_ops", numScanNext, serverSource);
501     }
502     try (Admin admin = TEST_UTIL.getHBaseAdmin()) {
503       admin.disableTable(tableName);
504       admin.deleteTable(tableName);
505     }
506   }
507 
508   @Test
509   public void testScanSizeForSmallScan() throws IOException {
510     String tableNameString = "testScanSizeSmall";
511     TableName tableName = TableName.valueOf(tableNameString);
512     byte[] cf = Bytes.toBytes("d");
513     byte[] qualifier = Bytes.toBytes("qual");
514     byte[] val = Bytes.toBytes("One");
515 
516     List<Put> puts = new ArrayList<>();
517     for (int insertCount =0; insertCount < 100; insertCount++) {
518       Put p = new Put(Bytes.toBytes("" + insertCount + "row"));
519       p.add(cf, qualifier, val);
520       puts.add(p);
521     }
522     try (HTable t = TEST_UTIL.createTable(tableName, cf)) {
523       t.put(puts);
524 
525       Scan s = new Scan();
526       s.setSmall(true);
527       s.setCaching(1);
528       ResultScanner resultScanners = t.getScanner(s);
529 
530       for (int nextCount = 0; nextCount < NUM_SCAN_NEXT; nextCount++) {
531         Result result = resultScanners.next();
532         assertNotNull(result);
533         assertEquals(1, result.size());
534       }
535     }
536     numScanNext += NUM_SCAN_NEXT;
537     try (RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
538       int cnt = 0;
539       for (HRegionLocation location: locator.getAllRegionLocations()) {
540         cnt++;
541         HRegionInfo i = location.getRegionInfo();
542         MetricsRegionAggregateSource agg = rs.getRegion(i.getRegionName())
543             .getMetrics()
544             .getSource()
545             .getAggregateSource();
546         String prefix = "namespace_"+NamespaceDescriptor.DEFAULT_NAMESPACE_NAME_STR+
547             "_table_"+tableNameString +
548             "_region_" + i.getEncodedName()+
549             "_metric";
550         metricsHelper.assertCounter(prefix + "_scanCount", NUM_SCAN_NEXT, agg);
551       }
552       metricsHelper.assertCounterGt("ScanSize_num_ops", numScanNext, serverSource);
553     }
554     try (Admin admin = TEST_UTIL.getHBaseAdmin()) {
555       admin.disableTable(tableName);
556       admin.deleteTable(tableName);
557     }
558   }
559 
560   @Test
561   @Ignore
562   public void testRangeCountMetrics() throws Exception {
563     String tableNameString = "testRangeCountMetrics";
564     final long[] timeranges =
565         { 1, 3, 10, 30, 100, 300, 1000, 3000, 10000, 30000, 60000, 120000, 300000, 600000 };
566     final String timeRangeType = "TimeRangeCount";
567     final String timeRangeMetricName = "Mutate";
568     boolean timeRangeCountUpdated = false;
569 
570     TableName tName = TableName.valueOf(tableNameString);
571     byte[] cfName = Bytes.toBytes("d");
572     byte[] row = Bytes.toBytes("rk");
573     byte[] qualifier = Bytes.toBytes("qual");
574     byte[] initValue = Bytes.toBytes("Value");
575 
576     TEST_UTIL.createTable(tName, cfName);
577 
578     Connection connection = TEST_UTIL.getConnection();
579     connection.getTable(tName).close(); // wait for the table to come up.
580 
581     // Do a first put to be sure that the connection is established, meta is there and so on.
582     Table table = connection.getTable(tName);
583     Put p = new Put(row);
584     p.addColumn(cfName, qualifier, initValue);
585     table.put(p);
586 
587     // do some puts and gets
588     for (int i = 0; i < 10; i++) {
589       table.put(p);
590     }
591 
592     Get g = new Get(row);
593     for (int i = 0; i < 10; i++) {
594       table.get(g);
595     }
596 
597     metricsRegionServer.getRegionServerWrapper().forceRecompute();
598 
599     // Check some time range counters were updated
600     long prior = 0;
601 
602     String dynamicMetricName;
603     for (int i = 0; i < timeranges.length; i++) {
604       dynamicMetricName =
605           timeRangeMetricName + "_" + timeRangeType + "_" + prior + "-" + timeranges[i];
606       if (metricsHelper.checkCounterExists(dynamicMetricName, serverSource)) {
607         long count = metricsHelper.getGaugeLong(dynamicMetricName, serverSource);
608         if (count > 0) {
609           timeRangeCountUpdated = true;
610           break;
611         }
612       }
613       prior = timeranges[i];
614     }
615     dynamicMetricName =
616         timeRangeMetricName + "_" + timeRangeType + "_" + timeranges[timeranges.length - 1] + "-inf";
617     if (metricsHelper.checkCounterExists(dynamicMetricName, serverSource)) {
618       long count = metricsHelper.getCounter(dynamicMetricName, serverSource);
619       if (count > 0) {
620         timeRangeCountUpdated = true;
621       }
622     }
623     assertEquals(true, timeRangeCountUpdated);
624 
625     table.close();
626   }
627 
628   @Test
629   public void testAverageRegionSize() throws Exception {
630     TableName tableName = TableName.valueOf("testAverageRegionSize");
631     byte[] cf = Bytes.toBytes("d");
632     byte[] row = Bytes.toBytes("rk");
633     byte[] qualifier = Bytes.toBytes("qual");
634     byte[] val = Bytes.toBytes("Value");
635 
636     //Force a hfile.
637     Table t = TEST_UTIL.createTable(tableName, cf);
638     Put p = new Put(row);
639     p.addColumn(cf, qualifier, val);
640     t.put(p);
641     TEST_UTIL.getHBaseAdmin().flush(tableName);
642 
643     metricsRegionServer.getRegionServerWrapper().forceRecompute();
644     assertTrue(metricsHelper.getGaugeDouble("averageRegionSize", serverSource) > 0.0);
645 
646     t.close();
647   }
648 
649   @Test
650   public void testMobMetrics() throws IOException, InterruptedException {
651     String tableNameString = "testMobMetrics";
652     TableName tableName = TableName.valueOf(tableNameString);
653     byte[] cf = Bytes.toBytes("d");
654     byte[] qualifier = Bytes.toBytes("qual");
655     byte[] val = Bytes.toBytes("mobdata");
656     int numHfiles = conf.getInt("hbase.hstore.compactionThreshold", 3) - 1;
657     HTableDescriptor htd = new HTableDescriptor(tableName);
658     HColumnDescriptor hcd = new HColumnDescriptor(cf);
659     hcd.setMobEnabled(true);
660     hcd.setMobThreshold(0);
661     htd.addFamily(hcd);
662     HBaseAdmin admin = new HBaseAdmin(conf);
663     HTable t = TEST_UTIL.createTable(htd, new byte[0][0], conf);
664     Region region = rs.getOnlineRegions(tableName).get(0);
665     t.setAutoFlush(true, true);
666     for (int insertCount = 0; insertCount < numHfiles; insertCount++) {
667       Put p = new Put(Bytes.toBytes(insertCount));
668       p.add(cf, qualifier, val);
669       t.put(p);
670       admin.flush(tableName);
671     }
672     metricsRegionServer.getRegionServerWrapper().forceRecompute();
673     metricsHelper.assertCounter("mobFlushCount", numHfiles, serverSource);
674     Scan scan = new Scan(Bytes.toBytes(0), Bytes.toBytes(2));
675     ResultScanner scanner = t.getScanner(scan);
676     scanner.next(100);
677     numScanNext++;  // this is an ugly construct
678     scanner.close();
679     metricsRegionServer.getRegionServerWrapper().forceRecompute();
680     metricsHelper.assertCounter("mobScanCellsCount", 2, serverSource);
681     region.getTableDesc().getFamily(cf).setMobThreshold(100);
682     ((HRegion)region).initialize();
683     region.compact(true);
684     metricsRegionServer.getRegionServerWrapper().forceRecompute();
685     metricsHelper.assertCounter("cellsCountCompactedFromMob", numHfiles,
686         serverSource);
687     metricsHelper.assertCounter("cellsCountCompactedToMob", 0, serverSource);
688     scanner = t.getScanner(scan);
689     scanner.next(100);
690     numScanNext++;  // this is an ugly construct
691     metricsRegionServer.getRegionServerWrapper().forceRecompute();
692     // metrics are reset by the region initialization
693     metricsHelper.assertCounter("mobScanCellsCount", 0, serverSource);
694     for (int insertCount = numHfiles;
695         insertCount < 2 * numHfiles - 1; insertCount++) {
696       Put p = new Put(Bytes.toBytes(insertCount));
697       p.add(cf, qualifier, val);
698       t.put(p);
699       admin.flush(tableName);
700     }
701     region.getTableDesc().getFamily(cf).setMobThreshold(0);
702     ((HRegion)region).initialize();
703     region.compact(true);
704     metricsRegionServer.getRegionServerWrapper().forceRecompute();
705     // metrics are reset by the region initialization
706     metricsHelper.assertCounter("cellsCountCompactedFromMob", 0, serverSource);
707     metricsHelper.assertCounter("cellsCountCompactedToMob", 2 * numHfiles - 1,
708         serverSource);
709     t.close();
710     admin.close();
711   }
712 }