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;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.testclassification.ClientTests;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.AfterClass;
031import org.junit.BeforeClass;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036/**
037 * Testcase for HBASE-21032, where use the wrong readType from a Scan instance which is actually a
038 * get scan and cause returning only 1 cell per rpc call.
039 */
040@Category({ ClientTests.class, MediumTests.class })
041public class TestGetScanPartialResult {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestGetScanPartialResult.class);
046
047  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
048  private static final TableName TABLE = TableName.valueOf("table");
049  private static final byte[] CF = { 'c', 'f' };
050  private static final byte[] ROW = { 'r', 'o', 'w' };
051  private static final int VALUE_SIZE = 10000;
052  private static final int NUM_COLUMNS = 300;
053
054  @BeforeClass
055  public static void setUp() throws Exception {
056    TEST_UTIL.startMiniCluster();
057    TEST_UTIL.createTable(TABLE, CF);
058  }
059
060  @AfterClass
061  public static void tearDown() throws Exception {
062    TEST_UTIL.shutdownMiniCluster();
063  }
064
065  private static byte[] makeLargeValue(int size) {
066    byte[] v = new byte[size];
067    for (int i = 0; i < size; i++) {
068      v[i] = 0;
069    }
070    return v;
071  }
072
073  @Test
074  public void test() throws IOException {
075    try (Table t = TEST_UTIL.getConnection().getTable(TABLE)) {
076      // populate a row with bunch of columns and large values
077      // to cause scan to return partials
078      byte[] val = makeLargeValue(VALUE_SIZE);
079      Put p = new Put(ROW);
080      for (int i = 0; i < NUM_COLUMNS; i++) {
081        p.addColumn(CF, Bytes.toBytes(Integer.toString(i)), val);
082      }
083      t.put(p);
084
085      Scan scan = new Scan();
086      scan.withStartRow(ROW);
087      scan.withStopRow(ROW, true);
088      scan.setAllowPartialResults(true);
089      scan.setMaxResultSize(2L * 1024 * 1024);
090      scan.readVersions(1);
091      ResultScanner scanner = t.getScanner(scan);
092
093      int nResults = 0;
094      int nCells = 0;
095      for (Result result = scanner.next(); (result != null); result = scanner.next()) {
096        nResults++;
097        nCells += result.listCells().size();
098      }
099      assertEquals(NUM_COLUMNS, nCells);
100      assertTrue(nResults < 5);
101    }
102  }
103}