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