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 java.util.List;
023import java.util.concurrent.ExecutionException;
024import java.util.stream.Collectors;
025import java.util.stream.IntStream;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.testclassification.ClientTests;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.junit.AfterClass;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038@Category({ MediumTests.class, ClientTests.class })
039public class TestRawAsyncTablePartialScan {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestRawAsyncTablePartialScan.class);
044
045  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
046
047  private static TableName TABLE_NAME = TableName.valueOf("async");
048
049  private static byte[] FAMILY = Bytes.toBytes("cf");
050
051  private static byte[][] CQS =
052    new byte[][] { Bytes.toBytes("cq1"), Bytes.toBytes("cq2"), Bytes.toBytes("cq3") };
053
054  private static int COUNT = 100;
055
056  private static AsyncConnection CONN;
057
058  private static AsyncTable<?> TABLE;
059
060  @BeforeClass
061  public static void setUp() throws Exception {
062    TEST_UTIL.startMiniCluster(1);
063    TEST_UTIL.createTable(TABLE_NAME, FAMILY);
064    TEST_UTIL.waitTableAvailable(TABLE_NAME);
065    CONN = ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get();
066    TABLE = CONN.getTable(TABLE_NAME);
067    TABLE.putAll(IntStream.range(0, COUNT)
068      .mapToObj(i -> new Put(Bytes.toBytes(String.format("%02d", i)))
069        .addColumn(FAMILY, CQS[0], Bytes.toBytes(i)).addColumn(FAMILY, CQS[1], Bytes.toBytes(2 * i))
070        .addColumn(FAMILY, CQS[2], Bytes.toBytes(3 * i)))
071      .collect(Collectors.toList())).get();
072  }
073
074  @AfterClass
075  public static void tearDown() throws Exception {
076    CONN.close();
077    TEST_UTIL.shutdownMiniCluster();
078  }
079
080  @Test
081  public void testBatchDoNotAllowPartial() throws InterruptedException, ExecutionException {
082    // we set batch to 2 and max result size to 1, then server will only returns one result per call
083    // but we should get 2 + 1 for every row.
084    List<Result> results = TABLE.scanAll(new Scan().setBatch(2).setMaxResultSize(1)).get();
085    assertEquals(2 * COUNT, results.size());
086    for (int i = 0; i < COUNT; i++) {
087      Result firstTwo = results.get(2 * i);
088      assertEquals(String.format("%02d", i), Bytes.toString(firstTwo.getRow()));
089      assertEquals(2, firstTwo.size());
090      assertEquals(i, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[0])));
091      assertEquals(2 * i, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[1])));
092
093      Result secondOne = results.get(2 * i + 1);
094      assertEquals(String.format("%02d", i), Bytes.toString(secondOne.getRow()));
095      assertEquals(1, secondOne.size());
096      assertEquals(3 * i, Bytes.toInt(secondOne.getValue(FAMILY, CQS[2])));
097    }
098  }
099
100  @Test
101  public void testReversedBatchDoNotAllowPartial() throws InterruptedException, ExecutionException {
102    // we set batch to 2 and max result size to 1, then server will only returns one result per call
103    // but we should get 2 + 1 for every row.
104    List<Result> results =
105      TABLE.scanAll(new Scan().setBatch(2).setMaxResultSize(1).setReversed(true)).get();
106    assertEquals(2 * COUNT, results.size());
107    for (int i = 0; i < COUNT; i++) {
108      int row = COUNT - i - 1;
109      Result firstTwo = results.get(2 * i);
110      assertEquals(String.format("%02d", row), Bytes.toString(firstTwo.getRow()));
111      assertEquals(2, firstTwo.size());
112      assertEquals(row, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[0])));
113      assertEquals(2 * row, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[1])));
114
115      Result secondOne = results.get(2 * i + 1);
116      assertEquals(String.format("%02d", row), Bytes.toString(secondOne.getRow()));
117      assertEquals(1, secondOne.size());
118      assertEquals(3 * row, Bytes.toInt(secondOne.getValue(FAMILY, CQS[2])));
119    }
120  }
121}