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.jupiter.api.Assertions.assertEquals;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.ArrayBackedTag;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.KeyValue;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.Tag;
029import org.apache.hadoop.hbase.io.hfile.HFile;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.junit.jupiter.api.AfterAll;
033import org.junit.jupiter.api.BeforeAll;
034import org.junit.jupiter.api.BeforeEach;
035import org.junit.jupiter.api.Test;
036import org.junit.jupiter.api.TestInfo;
037
038@org.junit.jupiter.api.Tag(MediumTests.TAG)
039public class TestResultSizeEstimation {
040
041  final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
042
043  final static int TAG_DATA_SIZE = 2048;
044  final static int SCANNER_DATA_LIMIT = TAG_DATA_SIZE + 256;
045
046  private String methodName;
047
048  @BeforeEach
049  public void setUp(TestInfo testInfo) {
050    this.methodName = testInfo.getTestMethod().get().getName();
051  }
052
053  @BeforeAll
054  public static void setUpBeforeClass() throws Exception {
055    Configuration conf = TEST_UTIL.getConfiguration();
056    // Need HFileV3
057    conf.setInt(HFile.FORMAT_VERSION_KEY, HFile.MIN_FORMAT_VERSION_WITH_TAGS);
058    // effectively limit max result size to one entry if it has tags
059    conf.setLong(HConstants.HBASE_CLIENT_SCANNER_MAX_RESULT_SIZE_KEY, SCANNER_DATA_LIMIT);
060    conf.setBoolean(ConnectionConfiguration.LOG_SCANNER_ACTIVITY, true);
061    TEST_UTIL.startMiniCluster(1);
062  }
063
064  @AfterAll
065  public static void tearDownAfterClass() throws Exception {
066    TEST_UTIL.shutdownMiniCluster();
067  }
068
069  @Test
070  public void testResultSizeEstimation() throws Exception {
071    byte[] ROW1 = Bytes.toBytes("testRow1");
072    byte[] ROW2 = Bytes.toBytes("testRow2");
073    byte[] FAMILY = Bytes.toBytes("testFamily");
074    byte[] QUALIFIER = Bytes.toBytes("testQualifier");
075    byte[] VALUE = Bytes.toBytes("testValue");
076
077    final TableName tableName = TableName.valueOf(methodName);
078    byte[][] FAMILIES = new byte[][] { FAMILY };
079    Table table = TEST_UTIL.createTable(tableName, FAMILIES);
080    Put p = new Put(ROW1);
081    p.add(new KeyValue(ROW1, FAMILY, QUALIFIER, Long.MAX_VALUE, VALUE));
082    table.put(p);
083    p = new Put(ROW2);
084    p.add(new KeyValue(ROW2, FAMILY, QUALIFIER, Long.MAX_VALUE, VALUE));
085    table.put(p);
086
087    Scan s = new Scan();
088    s.setMaxResultSize(SCANNER_DATA_LIMIT);
089    ResultScanner rs = table.getScanner(s);
090    int count = 0;
091    while (rs.next() != null) {
092      count++;
093    }
094    assertEquals(2, count, "Result size estimation did not work properly");
095    rs.close();
096    table.close();
097  }
098
099  @Test
100  public void testResultSizeEstimationWithTags() throws Exception {
101    byte[] ROW1 = Bytes.toBytes("testRow1");
102    byte[] ROW2 = Bytes.toBytes("testRow2");
103    byte[] FAMILY = Bytes.toBytes("testFamily");
104    byte[] QUALIFIER = Bytes.toBytes("testQualifier");
105    byte[] VALUE = Bytes.toBytes("testValue");
106
107    final TableName tableName = TableName.valueOf(methodName);
108    byte[][] FAMILIES = new byte[][] { FAMILY };
109    Table table = TEST_UTIL.createTable(tableName, FAMILIES);
110    Put p = new Put(ROW1);
111    p.add(new KeyValue(ROW1, FAMILY, QUALIFIER, Long.MAX_VALUE, VALUE,
112      new Tag[] { new ArrayBackedTag((byte) 1, new byte[TAG_DATA_SIZE]) }));
113    table.put(p);
114    p = new Put(ROW2);
115    p.add(new KeyValue(ROW2, FAMILY, QUALIFIER, Long.MAX_VALUE, VALUE,
116      new Tag[] { new ArrayBackedTag((byte) 1, new byte[TAG_DATA_SIZE]) }));
117    table.put(p);
118
119    Scan s = new Scan();
120    s.setMaxResultSize(SCANNER_DATA_LIMIT);
121    ResultScanner rs = table.getScanner(s);
122    int count = 0;
123    while (rs.next() != null) {
124      count++;
125    }
126    assertEquals(2, count, "Result size estimation did not work properly");
127    rs.close();
128    table.close();
129  }
130}