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.filter;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.client.Durability;
32  import org.apache.hadoop.hbase.client.Put;
33  import org.apache.hadoop.hbase.client.Result;
34  import org.apache.hadoop.hbase.client.ResultScanner;
35  import org.apache.hadoop.hbase.client.Scan;
36  import org.apache.hadoop.hbase.client.Table;
37  import org.apache.hadoop.hbase.filter.MultiRowRangeFilter.RowRange;
38  import org.apache.hadoop.hbase.io.hfile.HFile;
39  import org.apache.hadoop.hbase.testclassification.MediumTests;
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   * This test is for the optimization added in HBASE-15243.
48   * FilterList with two MultiRowRangeFilter's is constructed using Operator.MUST_PASS_ONE.
49   */
50  @Category(MediumTests.class)
51  public class TestFilterListOrOperatorWithBlkCnt {
52  
53    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
54    private static final Log LOG = LogFactory.getLog(TestFilterListOrOperatorWithBlkCnt.class);
55    private byte[] family = Bytes.toBytes("family");
56    private byte[] qf = Bytes.toBytes("qf");
57    private byte[] value = Bytes.toBytes("val");
58    private TableName tableName;
59    private int numRows = 10000;
60  
61    /**
62     * @throws Exception
63     */
64    @BeforeClass
65    public static void setUpBeforeClass() throws Exception {
66      long blkSize = 4096;
67      /*
68       * dfs block size is adjusted so that the specified number of rows would result in
69       * multiple blocks (8 for this test).
70       * Later in the test, assertion is made on the number of blocks read.
71       */
72      TEST_UTIL.getConfiguration().setLong("dfs.blocksize", blkSize);
73      TEST_UTIL.getConfiguration().setLong("dfs.bytes-per-checksum", blkSize);
74      TEST_UTIL.startMiniCluster();
75    }
76  
77    /**
78     * @throws Exception
79     */
80    @AfterClass
81    public static void tearDownAfterClass() throws Exception {
82      TEST_UTIL.shutdownMiniCluster();
83    }
84  
85    private static long getBlkAccessCount() {
86      return HFile.DATABLOCK_READ_COUNT.get();
87    }
88  
89    @Test
90    public void testMultiRowRangeWithFilterListOrOperatorWithBlkCnt() throws IOException {
91      tableName = TableName.valueOf("TestMultiRowRangeFilterWithFilterListOrOperatorWithBlkCnt");
92      Table ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
93      generateRows(numRows, ht, family, qf, value);
94  
95      Scan scan = new Scan();
96      scan.setMaxVersions();
97      long blocksStart = getBlkAccessCount();
98  
99      List<RowRange> ranges1 = new ArrayList<RowRange>();
100     ranges1.add(new RowRange(Bytes.toBytes(10), true, Bytes.toBytes(15), false));
101     ranges1.add(new RowRange(Bytes.toBytes(9980), true, Bytes.toBytes(9985), false));
102 
103     MultiRowRangeFilter filter1 = new MultiRowRangeFilter(ranges1);
104 
105     List<RowRange> ranges2 = new ArrayList<RowRange>();
106     ranges2.add(new RowRange(Bytes.toBytes(15), true, Bytes.toBytes(20), false));
107     ranges2.add(new RowRange(Bytes.toBytes(9985), true, Bytes.toBytes(9990), false));
108 
109     MultiRowRangeFilter filter2 = new MultiRowRangeFilter(ranges2);
110 
111     FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
112     filterList.addFilter(filter1);
113     filterList.addFilter(filter2);
114     scan.setFilter(filterList);
115     int resultsSize = getResultsSize(ht, scan);
116     LOG.info("found " + resultsSize + " results");
117     List<Cell> results1 = getScanResult(Bytes.toBytes(10), Bytes.toBytes(20), ht);
118     List<Cell> results2 = getScanResult(Bytes.toBytes(9980), Bytes.toBytes(9990), ht);
119 
120     assertEquals(results1.size() + results2.size(), resultsSize);
121     long blocksEnd = getBlkAccessCount();
122     long diff = blocksEnd - blocksStart;
123     LOG.info("Diff in number of blocks " + diff);
124     /*
125      * Verify that we don't read all the blocks (8 in total).
126      */
127     assertEquals(4, diff);
128 
129     ht.close();
130   }
131 
132   private void generateRows(int numberOfRows, Table ht, byte[] family, byte[] qf, byte[] value)
133       throws IOException {
134     for (int i = 0; i < numberOfRows; i++) {
135       byte[] row = Bytes.toBytes(i);
136       Put p = new Put(row);
137       p.addColumn(family, qf, value);
138       p.setDurability(Durability.SKIP_WAL);
139       ht.put(p);
140     }
141     TEST_UTIL.flush();
142   }
143 
144   private List<Cell> getScanResult(byte[] startRow, byte[] stopRow, Table ht) throws IOException {
145     Scan scan = new Scan();
146     scan.setMaxVersions();
147     if(!Bytes.toString(startRow).isEmpty()) {
148       scan.setStartRow(startRow);
149     }
150     if(!Bytes.toString(stopRow).isEmpty()) {
151       scan.setStopRow(stopRow);
152     }
153     ResultScanner scanner = ht.getScanner(scan);
154     List<Cell> kvList = new ArrayList<Cell>();
155     Result r;
156     while ((r = scanner.next()) != null) {
157       for (Cell kv : r.listCells()) {
158         kvList.add(kv);
159       }
160     }
161     return kvList;
162   }
163 
164   private int getResultsSize(Table ht, Scan scan) throws IOException {
165     ResultScanner scanner = ht.getScanner(scan);
166     List<Cell> results = new ArrayList<Cell>();
167     Result r;
168     while ((r = scanner.next()) != null) {
169       for (Cell kv : r.listCells()) {
170         results.add(kv);
171       }
172     }
173     return results.size();
174   }
175 }