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.HBaseTestingUtil;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
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 HBaseTestingUtil UTIL = new HBaseTestingUtil();
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 RegionInfo HRI;
069
070  private static AsyncConnectionImpl CONN;
071
072  private static ClientProtos.ClientService.Interface STUB;
073
074  @BeforeClass
075  public static void setUp() throws Exception {
076    UTIL.startMiniCluster(1);
077    try (Table table = UTIL.createTable(TABLE_NAME, CF)) {
078      for (int i = 0; i < COUNT; i++) {
079        table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
080      }
081    }
082    HRI = UTIL.getAdmin().getRegions(TABLE_NAME).get(0);
083    CONN =
084      (AsyncConnectionImpl) ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get();
085    STUB = CONN.getRegionServerStub(UTIL.getHBaseCluster().getRegionServer(0).getServerName());
086  }
087
088  @AfterClass
089  public static void tearDown() throws Exception {
090    UTIL.shutdownMiniCluster();
091  }
092
093  private ScanResponse scan(HBaseRpcController hrc, ScanRequest req) throws IOException {
094    BlockingRpcCallback<ScanResponse> callback = new BlockingRpcCallback<>();
095    STUB.scan(hrc, req, callback);
096    return callback.get();
097  }
098
099  private void assertResult(int row, Result result) {
100    assertEquals(row, Bytes.toInt(result.getRow()));
101    assertEquals(row, Bytes.toInt(result.getValue(CF, CQ)));
102  }
103
104  @Test
105  public void test() throws ServiceException, IOException {
106    Scan scan = new Scan();
107    ScanRequest req = RequestConverter.buildScanRequest(HRI.getRegionName(), scan, 0, false);
108    HBaseRpcController hrc = new HBaseRpcControllerImpl();
109    ScanResponse resp = scan(hrc, req);
110    assertTrue(resp.getMoreResults());
111    assertTrue(resp.getMoreResultsInRegion());
112    assertEquals(0, ResponseConverter.getResults(hrc.cellScanner(), resp).length);
113    long scannerId = resp.getScannerId();
114    int nextCallSeq = 0;
115    // test normal next
116    for (int i = 0; i < COUNT / 2; i++) {
117      req = RequestConverter.buildScanRequest(scannerId, 1, false, nextCallSeq++, false, false, -1);
118      hrc.reset();
119      resp = scan(hrc, req);
120      assertTrue(resp.getMoreResults());
121      assertTrue(resp.getMoreResultsInRegion());
122      Result[] results = ResponseConverter.getResults(hrc.cellScanner(), resp);
123      assertEquals(1, results.length);
124      assertResult(i, results[0]);
125    }
126    // test zero next
127    req = RequestConverter.buildScanRequest(scannerId, 0, false, nextCallSeq++, false, false, -1);
128    hrc.reset();
129    resp = scan(hrc, req);
130    assertTrue(resp.getMoreResults());
131    assertTrue(resp.getMoreResultsInRegion());
132    assertEquals(0, ResponseConverter.getResults(hrc.cellScanner(), resp).length);
133    for (int i = COUNT / 2; i < COUNT; i++) {
134      req = RequestConverter.buildScanRequest(scannerId, 1, false, nextCallSeq++, false, false, -1);
135      hrc.reset();
136      resp = scan(hrc, req);
137      assertTrue(resp.getMoreResults());
138      assertEquals(i != COUNT - 1, resp.getMoreResultsInRegion());
139      Result[] results = ResponseConverter.getResults(hrc.cellScanner(), resp);
140      assertEquals(1, results.length);
141      assertResult(i, results[0]);
142    }
143    // close
144    req = RequestConverter.buildScanRequest(scannerId, 0, true, false);
145    hrc.reset();
146    resp = scan(hrc, req);
147  }
148}