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 068 .putAll(IntStream.range(0, COUNT) 069 .mapToObj(i -> new Put(Bytes.toBytes(String.format("%02d", i))) 070 .addColumn(FAMILY, CQS[0], Bytes.toBytes(i)) 071 .addColumn(FAMILY, CQS[1], Bytes.toBytes(2 * i)) 072 .addColumn(FAMILY, CQS[2], Bytes.toBytes(3 * i))) 073 .collect(Collectors.toList())) 074 .get(); 075 } 076 077 @AfterClass 078 public static void tearDown() throws Exception { 079 CONN.close(); 080 TEST_UTIL.shutdownMiniCluster(); 081 } 082 083 @Test 084 public void testBatchDoNotAllowPartial() throws InterruptedException, ExecutionException { 085 // we set batch to 2 and max result size to 1, then server will only returns one result per call 086 // but we should get 2 + 1 for every row. 087 List<Result> results = TABLE.scanAll(new Scan().setBatch(2).setMaxResultSize(1)).get(); 088 assertEquals(2 * COUNT, results.size()); 089 for (int i = 0; i < COUNT; i++) { 090 Result firstTwo = results.get(2 * i); 091 assertEquals(String.format("%02d", i), Bytes.toString(firstTwo.getRow())); 092 assertEquals(2, firstTwo.size()); 093 assertEquals(i, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[0]))); 094 assertEquals(2 * i, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[1]))); 095 096 Result secondOne = results.get(2 * i + 1); 097 assertEquals(String.format("%02d", i), Bytes.toString(secondOne.getRow())); 098 assertEquals(1, secondOne.size()); 099 assertEquals(3 * i, Bytes.toInt(secondOne.getValue(FAMILY, CQS[2]))); 100 } 101 } 102 103 @Test 104 public void testReversedBatchDoNotAllowPartial() throws InterruptedException, ExecutionException { 105 // we set batch to 2 and max result size to 1, then server will only returns one result per call 106 // but we should get 2 + 1 for every row. 107 List<Result> results = 108 TABLE.scanAll(new Scan().setBatch(2).setMaxResultSize(1).setReversed(true)).get(); 109 assertEquals(2 * COUNT, results.size()); 110 for (int i = 0; i < COUNT; i++) { 111 int row = COUNT - i - 1; 112 Result firstTwo = results.get(2 * i); 113 assertEquals(String.format("%02d", row), Bytes.toString(firstTwo.getRow())); 114 assertEquals(2, firstTwo.size()); 115 assertEquals(row, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[0]))); 116 assertEquals(2 * row, Bytes.toInt(firstTwo.getValue(FAMILY, CQS[1]))); 117 118 Result secondOne = results.get(2 * i + 1); 119 assertEquals(String.format("%02d", row), Bytes.toString(secondOne.getRow())); 120 assertEquals(1, secondOne.size()); 121 assertEquals(3 * row, Bytes.toInt(secondOne.getValue(FAMILY, CQS[2]))); 122 } 123 } 124}