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.client;
19  
20  import org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.hbase.HBaseTestingUtility;
22  import org.apache.hadoop.hbase.HConstants;
23  import org.apache.hadoop.hbase.KeyValue;
24  import org.apache.hadoop.hbase.TableName;
25  import org.apache.hadoop.hbase.Tag;
26  import org.apache.hadoop.hbase.client.HTable;
27  import org.apache.hadoop.hbase.client.Put;
28  import org.apache.hadoop.hbase.client.ResultScanner;
29  import org.apache.hadoop.hbase.client.Scan;
30  import org.apache.hadoop.hbase.io.hfile.HFile;
31  import org.apache.hadoop.hbase.testclassification.LargeTests;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.junit.AfterClass;
34  import org.junit.BeforeClass;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  import static org.junit.Assert.assertEquals;
39  
40  @Category(LargeTests.class)
41  public class TestResultSizeEstimation {
42  
43    final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
44  
45    final static int TAG_DATA_SIZE = 2048;
46    final static int SCANNER_DATA_LIMIT = TAG_DATA_SIZE + 256;
47  
48    @BeforeClass
49    public static void setUpBeforeClass() throws Exception {
50      Configuration conf = TEST_UTIL.getConfiguration();
51      // Need HFileV3
52      conf.setInt(HFile.FORMAT_VERSION_KEY, HFile.MIN_FORMAT_VERSION_WITH_TAGS);
53      // effectively limit max result size to one entry if it has tags
54      conf.setLong(HConstants.HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE_KEY, SCANNER_DATA_LIMIT);
55      conf.setBoolean(ScannerCallable.LOG_SCANNER_ACTIVITY, true);
56      TEST_UTIL.startMiniCluster(1);
57    }
58  
59    @AfterClass
60    public static void tearDownAfterClass() throws Exception {
61      TEST_UTIL.shutdownMiniCluster();
62    }
63  
64    @Test
65    public void testResultSizeEstimation() throws Exception {
66      byte [] ROW1 = Bytes.toBytes("testRow1");
67      byte [] ROW2 = Bytes.toBytes("testRow2");
68      byte [] FAMILY = Bytes.toBytes("testFamily");
69      byte [] QUALIFIER = Bytes.toBytes("testQualifier");
70      byte [] VALUE = Bytes.toBytes("testValue");
71  
72      TableName TABLE = TableName.valueOf("testResultSizeEstimation");
73      byte[][] FAMILIES = new byte[][] { FAMILY };
74      Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
75      HTable table = TEST_UTIL.createTable(TABLE, FAMILIES, conf);
76      Put p = new Put(ROW1);
77      p.add(new KeyValue(ROW1, FAMILY, QUALIFIER, Long.MAX_VALUE, VALUE));
78      table.put(p);
79      p = new Put(ROW2);
80      p.add(new KeyValue(ROW2, FAMILY, QUALIFIER, Long.MAX_VALUE, VALUE));
81      table.put(p);
82  
83      Scan s = new Scan();
84      s.setMaxResultSize(SCANNER_DATA_LIMIT);
85      ResultScanner rs = table.getScanner(s);
86      int count = 0;
87      while(rs.next() != null) {
88        count++;
89      }
90      assertEquals("Result size estimation did not work properly", 2, count);
91      rs.close();
92      table.close();
93    }
94  
95    @Test
96    public void testResultSizeEstimationWithTags() throws Exception {
97      byte [] ROW1 = Bytes.toBytes("testRow1");
98      byte [] ROW2 = Bytes.toBytes("testRow2");
99      byte [] FAMILY = Bytes.toBytes("testFamily");
100     byte [] QUALIFIER = Bytes.toBytes("testQualifier");
101     byte [] VALUE = Bytes.toBytes("testValue");
102 
103     TableName TABLE = TableName.valueOf("testResultSizeEstimationWithTags");
104     byte[][] FAMILIES = new byte[][] { FAMILY };
105     Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
106     HTable table = TEST_UTIL.createTable(TABLE, FAMILIES, conf);
107     Put p = new Put(ROW1);
108     p.add(new KeyValue(ROW1, FAMILY, QUALIFIER, Long.MAX_VALUE, VALUE,
109       new Tag[] { new Tag((byte)1, new byte[TAG_DATA_SIZE]) } ));
110     table.put(p);
111     p = new Put(ROW2);
112     p.add(new KeyValue(ROW2, FAMILY, QUALIFIER, Long.MAX_VALUE, VALUE,
113       new Tag[] { new Tag((byte)1, new byte[TAG_DATA_SIZE]) } ));
114     table.put(p);
115 
116     Scan s = new Scan();
117     s.setMaxResultSize(SCANNER_DATA_LIMIT);
118     ResultScanner rs = table.getScanner(s);
119     int count = 0;
120     while(rs.next() != null) {
121       count++;
122     }
123     assertEquals("Result size estimation did not work properly", 2, count);
124     rs.close();
125     table.close();
126   }
127 }