HBASE-24033 Add ut for loading the corrupt recovered hfiles (#1322)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / TestScanWithoutFetchingData.java
blob858fbb753bb94cd3d899d4cec0aa88ced5630817
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.client;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
23 import java.io.IOException;
24 import org.apache.hadoop.hbase.HBaseClassTestRule;
25 import org.apache.hadoop.hbase.HBaseTestingUtility;
26 import org.apache.hadoop.hbase.TableName;
27 import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
28 import org.apache.hadoop.hbase.ipc.HBaseRpcController;
29 import org.apache.hadoop.hbase.ipc.HBaseRpcControllerImpl;
30 import org.apache.hadoop.hbase.testclassification.MediumTests;
31 import org.apache.hadoop.hbase.testclassification.RegionServerTests;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.ClassRule;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
39 import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
41 import org.apache.hadoop.hbase.shaded.protobuf.RequestConverter;
42 import org.apache.hadoop.hbase.shaded.protobuf.ResponseConverter;
43 import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
44 import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest;
45 import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanResponse;
47 /**
48 * Testcase to make sure that we do not close scanners if ScanRequest.numberOfRows is zero. See
49 * HBASE-18042 for more details.
51 @Category({ RegionServerTests.class, MediumTests.class })
52 public class TestScanWithoutFetchingData {
54 @ClassRule
55 public static final HBaseClassTestRule CLASS_RULE =
56 HBaseClassTestRule.forClass(TestScanWithoutFetchingData.class);
58 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
60 private static final TableName TABLE_NAME = TableName.valueOf("test");
62 private static final byte[] CF = Bytes.toBytes("cf");
64 private static final byte[] CQ = Bytes.toBytes("cq");
66 private static final int COUNT = 10;
68 private static RegionInfo HRI;
70 private static AsyncConnectionImpl CONN;
72 private static ClientProtos.ClientService.Interface STUB;
74 @BeforeClass
75 public static void setUp() throws Exception {
76 UTIL.startMiniCluster(1);
77 try (Table table = UTIL.createTable(TABLE_NAME, CF)) {
78 for (int i = 0; i < COUNT; i++) {
79 table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
82 HRI = UTIL.getAdmin().getRegions(TABLE_NAME).get(0);
83 CONN =
84 (AsyncConnectionImpl) ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get();
85 STUB = CONN.getRegionServerStub(UTIL.getHBaseCluster().getRegionServer(0).getServerName());
88 @AfterClass
89 public static void tearDown() throws Exception {
90 UTIL.shutdownMiniCluster();
93 private ScanResponse scan(HBaseRpcController hrc, ScanRequest req) throws IOException {
94 BlockingRpcCallback<ScanResponse> callback = new BlockingRpcCallback<>();
95 STUB.scan(hrc, req, callback);
96 return callback.get();
99 private void assertResult(int row, Result result) {
100 assertEquals(row, Bytes.toInt(result.getRow()));
101 assertEquals(row, Bytes.toInt(result.getValue(CF, CQ)));
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]);
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]);
143 // close
144 req = RequestConverter.buildScanRequest(scannerId, 0, true, false);
145 hrc.reset();
146 resp = scan(hrc, req);