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.HRegionInfo; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.ipc.HBaseRpcController; 029import org.apache.hadoop.hbase.ipc.HBaseRpcControllerImpl; 030import org.apache.hadoop.hbase.testclassification.MediumTests; 031import org.apache.hadoop.hbase.testclassification.RegionServerTests; 032import org.apache.hadoop.hbase.util.Bytes; 033import org.junit.AfterClass; 034import org.junit.BeforeClass; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038 039import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException; 040 041import org.apache.hadoop.hbase.shaded.protobuf.RequestConverter; 042import org.apache.hadoop.hbase.shaded.protobuf.ResponseConverter; 043import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos; 044import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest; 045import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanResponse; 046 047/** 048 * Testcase to make sure that we do not close scanners if ScanRequest.numberOfRows is zero. See 049 * HBASE-18042 for more details. 050 */ 051@Category({ RegionServerTests.class, MediumTests.class }) 052public class TestScanWithoutFetchingData { 053 054 @ClassRule 055 public static final HBaseClassTestRule CLASS_RULE = 056 HBaseClassTestRule.forClass(TestScanWithoutFetchingData.class); 057 058 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 059 060 private static final TableName TABLE_NAME = TableName.valueOf("test"); 061 062 private static final byte[] CF = Bytes.toBytes("cf"); 063 064 private static final byte[] CQ = Bytes.toBytes("cq"); 065 066 private static final int COUNT = 10; 067 068 private static HRegionInfo HRI; 069 070 private static ClientProtos.ClientService.BlockingInterface STUB; 071 072 @BeforeClass 073 public static void setUp() throws Exception { 074 UTIL.startMiniCluster(1); 075 try (Table table = UTIL.createTable(TABLE_NAME, CF)) { 076 for (int i = 0; i < COUNT; i++) { 077 table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i))); 078 } 079 } 080 HRI = UTIL.getAdmin().getTableRegions(TABLE_NAME).get(0); 081 STUB = ((ConnectionImplementation) UTIL.getConnection()) 082 .getClient(UTIL.getHBaseCluster().getRegionServer(0).getServerName()); 083 } 084 085 @AfterClass 086 public static void tearDown() throws Exception { 087 UTIL.shutdownMiniCluster(); 088 } 089 090 private void assertResult(int row, Result result) { 091 assertEquals(row, Bytes.toInt(result.getRow())); 092 assertEquals(row, Bytes.toInt(result.getValue(CF, CQ))); 093 } 094 095 @Test 096 public void test() throws ServiceException, IOException { 097 Scan scan = new Scan(); 098 ScanRequest req = RequestConverter.buildScanRequest(HRI.getRegionName(), scan, 0, false); 099 HBaseRpcController hrc = new HBaseRpcControllerImpl(); 100 ScanResponse resp = STUB.scan(hrc, req); 101 assertTrue(resp.getMoreResults()); 102 assertTrue(resp.getMoreResultsInRegion()); 103 assertEquals(0, ResponseConverter.getResults(hrc.cellScanner(), resp).length); 104 long scannerId = resp.getScannerId(); 105 int nextCallSeq = 0; 106 // test normal next 107 for (int i = 0; i < COUNT / 2; i++) { 108 req = RequestConverter.buildScanRequest(scannerId, 1, false, nextCallSeq++, false, false, -1); 109 hrc.reset(); 110 resp = STUB.scan(hrc, req); 111 assertTrue(resp.getMoreResults()); 112 assertTrue(resp.getMoreResultsInRegion()); 113 Result[] results = ResponseConverter.getResults(hrc.cellScanner(), resp); 114 assertEquals(1, results.length); 115 assertResult(i, results[0]); 116 } 117 // test zero next 118 req = RequestConverter.buildScanRequest(scannerId, 0, false, nextCallSeq++, false, false, -1); 119 hrc.reset(); 120 resp = STUB.scan(hrc, req); 121 assertTrue(resp.getMoreResults()); 122 assertTrue(resp.getMoreResultsInRegion()); 123 assertEquals(0, ResponseConverter.getResults(hrc.cellScanner(), resp).length); 124 for (int i = COUNT / 2; i < COUNT; i++) { 125 req = RequestConverter.buildScanRequest(scannerId, 1, false, nextCallSeq++, false, false, -1); 126 hrc.reset(); 127 resp = STUB.scan(hrc, req); 128 assertTrue(resp.getMoreResults()); 129 assertEquals(i != COUNT - 1, resp.getMoreResultsInRegion()); 130 Result[] results = ResponseConverter.getResults(hrc.cellScanner(), resp); 131 assertEquals(1, results.length); 132 assertResult(i, results[0]); 133 } 134 // close 135 req = RequestConverter.buildScanRequest(scannerId, 0, true, false); 136 resp = STUB.scan(null, req); 137 } 138}