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  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.hadoop.hbase.HBaseTestCase;
24  import org.apache.hadoop.hbase.HBaseTestingUtility;
25  import org.apache.hadoop.hbase.HColumnDescriptor;
26  import org.apache.hadoop.hbase.HTableDescriptor;
27  import org.apache.hadoop.hbase.KeyValue;
28  import org.apache.hadoop.hbase.SmallTests;
29  import org.apache.hadoop.hbase.client.Scan;
30  import org.apache.hadoop.hbase.io.hfile.Compression;
31  import org.apache.hadoop.hbase.io.hfile.BlockType.BlockCategory;
32  import org.apache.hadoop.hbase.regionserver.metrics.SchemaMetrics;
33  import org.apache.hadoop.hbase.regionserver.metrics.SchemaMetrics.BlockMetricType;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.junit.Assert;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  @SuppressWarnings("deprecation")
40  @Category(SmallTests.class)
41  public class TestBlocksScanned extends HBaseTestCase {
42    private static byte [] TABLE = Bytes.toBytes("TestBlocksScanned");
43    private static byte [] FAMILY = Bytes.toBytes("family");
44    private static byte [] COL = Bytes.toBytes("col");
45    private static byte [] START_KEY = Bytes.toBytes("aaa");
46    private static byte [] END_KEY = Bytes.toBytes("zzz");
47    private static int BLOCK_SIZE = 70;
48  
49    private static HBaseTestingUtility TEST_UTIL = null;
50    private static HTableDescriptor TESTTABLEDESC = null;
51  
52     @Override
53     public void setUp() throws Exception {
54       super.setUp();
55       SchemaMetrics.setUseTableNameInTest(true);
56       TEST_UTIL = new HBaseTestingUtility();
57       TESTTABLEDESC = new HTableDescriptor(TABLE);
58  
59       TESTTABLEDESC.addFamily(
60           new HColumnDescriptor(FAMILY)
61           .setMaxVersions(10)
62           .setBlockCacheEnabled(true)
63           .setBlocksize(BLOCK_SIZE)
64           .setCompressionType(Compression.Algorithm.NONE)
65       );
66     }
67  
68     @Test
69    public void testBlocksScanned() throws Exception {
70      HRegion r = createNewHRegion(TESTTABLEDESC, START_KEY, END_KEY,
71          TEST_UTIL.getConfiguration());
72      addContent(r, FAMILY, COL);
73      r.flushcache();
74  
75      // Get the per-cf metrics
76      SchemaMetrics schemaMetrics =
77        SchemaMetrics.getInstance(Bytes.toString(TABLE), Bytes.toString(FAMILY));
78      Map<String, Long> schemaMetricSnapshot = SchemaMetrics.getMetricsSnapshot();
79  
80      // Do simple test of getting one row only first.
81      Scan scan = new Scan(Bytes.toBytes("aaa"), Bytes.toBytes("aaz"));
82      scan.addColumn(FAMILY, COL);
83      scan.setMaxVersions(1);
84  
85      InternalScanner s = r.getScanner(scan);
86      List<KeyValue> results = new ArrayList<KeyValue>();
87      while (s.next(results));
88      s.close();
89  
90      int expectResultSize = 'z' - 'a';
91      Assert.assertEquals(expectResultSize, results.size());
92  
93      int kvPerBlock = (int) Math.ceil(BLOCK_SIZE / (double) results.get(0).getLength());
94      Assert.assertEquals(2, kvPerBlock);
95  
96      long expectDataBlockRead = (long) Math.ceil(expectResultSize / (double) kvPerBlock);
97      long expectIndexBlockRead = expectDataBlockRead;
98  
99      verifyDataAndIndexBlockRead(schemaMetricSnapshot, schemaMetrics,
100         expectDataBlockRead, expectIndexBlockRead);
101   }
102 
103   private void verifyDataAndIndexBlockRead(Map<String, Long> previousMetricSnapshot,
104       SchemaMetrics schemaMetrics, long expectDataBlockRead, long expectedIndexBlockRead){
105     Map<String, Long> currentMetricsSnapshot = SchemaMetrics.getMetricsSnapshot();
106     Map<String, Long> diffs =
107       SchemaMetrics.diffMetrics(previousMetricSnapshot, currentMetricsSnapshot);
108 
109     long dataBlockRead = SchemaMetrics.getLong(diffs,
110         schemaMetrics.getBlockMetricName(BlockCategory.DATA, false, BlockMetricType.READ_COUNT));
111     long indexBlockRead = SchemaMetrics.getLong(diffs,
112         schemaMetrics.getBlockMetricName(BlockCategory.INDEX, false, BlockMetricType.READ_COUNT));
113 
114     Assert.assertEquals(expectDataBlockRead, dataBlockRead);
115     Assert.assertEquals(expectedIndexBlockRead, indexBlockRead);
116   }
117 }