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