View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.MediumTests;
31  import org.apache.hadoop.hbase.client.HTable;
32  import org.apache.hadoop.hbase.client.HTableUtil;
33  import org.apache.hadoop.hbase.client.Put;
34  import org.apache.hadoop.hbase.client.Result;
35  import org.apache.hadoop.hbase.client.Row;
36  import org.apache.hadoop.hbase.client.Scan;
37  import org.apache.hadoop.hbase.io.hfile.BlockCache;
38  import org.apache.hadoop.hbase.io.hfile.BlockCacheColumnFamilySummary;
39  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.junit.AfterClass;
42  import org.junit.BeforeClass;
43  import org.junit.Test;
44  import org.junit.experimental.categories.Category;
45  
46  /**
47   * Tests the block cache summary functionality in StoreFile, 
48   * which contains the BlockCache
49   *
50   */
51  @Category(MediumTests.class)
52  public class TestStoreFileBlockCacheSummary {
53    final Log LOG = LogFactory.getLog(getClass());
54    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();  
55    private static final String TEST_TABLE = "testTable";
56    private static final String TEST_TABLE2 = "testTable2";
57    private static final String TEST_CF = "testFamily";
58    private static byte [] FAMILY = Bytes.toBytes(TEST_CF);
59    private static byte [] QUALIFIER = Bytes.toBytes("testQualifier");
60    private static byte [] VALUE = Bytes.toBytes("testValue");
61  
62    private final int TOTAL_ROWS = 4;
63    
64    /**
65     * @throws java.lang.Exception exception
66     */
67    @BeforeClass
68    public static void setUpBeforeClass() throws Exception {
69      TEST_UTIL.startMiniCluster();
70    }
71  
72    /**
73     * @throws java.lang.Exception exception
74     */
75    @AfterClass
76    public static void tearDownAfterClass() throws Exception {
77      TEST_UTIL.shutdownMiniCluster();
78    }
79    
80  
81    private Put createPut(byte[] family, String row) {
82      Put put = new Put( Bytes.toBytes(row));
83      put.add(family, QUALIFIER, VALUE);
84      return put;
85    }
86    
87    /**
88    * This test inserts data into multiple tables and then reads both tables to ensure
89    * they are in the block cache.
90    *
91    * @throws Exception exception
92    */
93   @Test
94   public void testBlockCacheSummary() throws Exception {
95     HTable ht = TEST_UTIL.createTable(Bytes.toBytes(TEST_TABLE), FAMILY);
96     addRows(ht, FAMILY);
97  
98     HTable ht2 = TEST_UTIL.createTable(Bytes.toBytes(TEST_TABLE2), FAMILY);
99     addRows(ht2, FAMILY);
100 
101    TEST_UTIL.flush();
102    
103    scan(ht, FAMILY);
104    scan(ht2, FAMILY);
105       
106    BlockCache bc =
107      new CacheConfig(TEST_UTIL.getConfiguration()).getBlockCache();
108    List<BlockCacheColumnFamilySummary> bcs = 
109      bc.getBlockCacheColumnFamilySummaries(TEST_UTIL.getConfiguration());
110    LOG.info("blockCacheSummary: " + bcs);
111 
112    assertEquals("blockCache summary has entries", 2, bcs.size());
113 
114    BlockCacheColumnFamilySummary e = bcs.get(0);
115    assertEquals("table", TEST_TABLE, e.getTable());
116    assertEquals("cf", TEST_CF, e.getColumnFamily());
117 
118    e = bcs.get(1);
119    assertEquals("table", TEST_TABLE2, e.getTable());
120    assertEquals("cf", TEST_CF, e.getColumnFamily());
121 
122    ht.close();
123    ht2.close();
124  }
125 
126  private void addRows(HTable ht, byte[] family) throws IOException {
127  
128    List<Row> rows = new ArrayList<Row>();
129    for (int i = 0; i < TOTAL_ROWS;i++) {
130      rows.add(createPut(family, "row" + i));
131    }
132    
133    HTableUtil.bucketRsBatch( ht, rows);
134  }
135 
136  private void scan(HTable ht, byte[] family) throws IOException {
137    Scan scan = new Scan();
138    scan.addColumn(family, QUALIFIER);
139    
140    int count = 0;
141    for(@SuppressWarnings("unused") Result result : ht.getScanner(scan)) {
142      count++;
143    }
144    if (TOTAL_ROWS != count) {
145      throw new IOException("Incorrect number of rows!");
146    }
147  }
148 
149 }
150