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