001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client; 019 020import static org.junit.Assert.assertEquals; 021 022import org.apache.hadoop.conf.Configuration; 023import org.apache.hadoop.hbase.ArrayBackedTag; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseTestingUtility; 026import org.apache.hadoop.hbase.HConstants; 027import org.apache.hadoop.hbase.KeyValue; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.Tag; 030import org.apache.hadoop.hbase.io.hfile.HFile; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.apache.hadoop.hbase.util.Bytes; 033import org.junit.AfterClass; 034import org.junit.BeforeClass; 035import org.junit.ClassRule; 036import org.junit.Rule; 037import org.junit.Test; 038import org.junit.experimental.categories.Category; 039import org.junit.rules.TestName; 040 041@Category(MediumTests.class) 042public class TestResultSizeEstimation { 043 044 @ClassRule 045 public static final HBaseClassTestRule CLASS_RULE = 046 HBaseClassTestRule.forClass(TestResultSizeEstimation.class); 047 048 final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 049 050 final static int TAG_DATA_SIZE = 2048; 051 final static int SCANNER_DATA_LIMIT = TAG_DATA_SIZE + 256; 052 053 @Rule 054 public TestName name = new TestName(); 055 056 @BeforeClass 057 public static void setUpBeforeClass() throws Exception { 058 Configuration conf = TEST_UTIL.getConfiguration(); 059 // Need HFileV3 060 conf.setInt(HFile.FORMAT_VERSION_KEY, HFile.MIN_FORMAT_VERSION_WITH_TAGS); 061 // effectively limit max result size to one entry if it has tags 062 conf.setLong(HConstants.HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE_KEY, SCANNER_DATA_LIMIT); 063 conf.setBoolean(ScannerCallable.LOG_SCANNER_ACTIVITY, true); 064 TEST_UTIL.startMiniCluster(1); 065 } 066 067 @AfterClass 068 public static void tearDownAfterClass() throws Exception { 069 TEST_UTIL.shutdownMiniCluster(); 070 } 071 072 @Test 073 public void testResultSizeEstimation() throws Exception { 074 byte[] ROW1 = Bytes.toBytes("testRow1"); 075 byte[] ROW2 = Bytes.toBytes("testRow2"); 076 byte[] FAMILY = Bytes.toBytes("testFamily"); 077 byte[] QUALIFIER = Bytes.toBytes("testQualifier"); 078 byte[] VALUE = Bytes.toBytes("testValue"); 079 080 final TableName tableName = TableName.valueOf(name.getMethodName()); 081 byte[][] FAMILIES = new byte[][] { FAMILY }; 082 Table table = TEST_UTIL.createTable(tableName, FAMILIES); 083 Put p = new Put(ROW1); 084 p.add(new KeyValue(ROW1, FAMILY, QUALIFIER, Long.MAX_VALUE, VALUE)); 085 table.put(p); 086 p = new Put(ROW2); 087 p.add(new KeyValue(ROW2, FAMILY, QUALIFIER, Long.MAX_VALUE, VALUE)); 088 table.put(p); 089 090 Scan s = new Scan(); 091 s.setMaxResultSize(SCANNER_DATA_LIMIT); 092 ResultScanner rs = table.getScanner(s); 093 int count = 0; 094 while (rs.next() != null) { 095 count++; 096 } 097 assertEquals("Result size estimation did not work properly", 2, count); 098 rs.close(); 099 table.close(); 100 } 101 102 @Test 103 public void testResultSizeEstimationWithTags() throws Exception { 104 byte[] ROW1 = Bytes.toBytes("testRow1"); 105 byte[] ROW2 = Bytes.toBytes("testRow2"); 106 byte[] FAMILY = Bytes.toBytes("testFamily"); 107 byte[] QUALIFIER = Bytes.toBytes("testQualifier"); 108 byte[] VALUE = Bytes.toBytes("testValue"); 109 110 final TableName tableName = TableName.valueOf(name.getMethodName()); 111 byte[][] FAMILIES = new byte[][] { FAMILY }; 112 Table table = TEST_UTIL.createTable(tableName, FAMILIES); 113 Put p = new Put(ROW1); 114 p.add(new KeyValue(ROW1, FAMILY, QUALIFIER, Long.MAX_VALUE, VALUE, 115 new Tag[] { new ArrayBackedTag((byte) 1, new byte[TAG_DATA_SIZE]) })); 116 table.put(p); 117 p = new Put(ROW2); 118 p.add(new KeyValue(ROW2, FAMILY, QUALIFIER, Long.MAX_VALUE, VALUE, 119 new Tag[] { new ArrayBackedTag((byte) 1, new byte[TAG_DATA_SIZE]) })); 120 table.put(p); 121 122 Scan s = new Scan(); 123 s.setMaxResultSize(SCANNER_DATA_LIMIT); 124 ResultScanner rs = table.getScanner(s); 125 int count = 0; 126 while (rs.next() != null) { 127 count++; 128 } 129 assertEquals("Result size estimation did not work properly", 2, count); 130 rs.close(); 131 table.close(); 132 } 133}