HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / client / TestAlwaysSetScannerId.java
blobac1bdb6038927da4e24f19bcaedf4c00884ec5d1
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.HBaseTestingUtil;
26 import org.apache.hadoop.hbase.TableName;
27 import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
28 import org.apache.hadoop.hbase.ipc.HBaseRpcControllerImpl;
29 import org.apache.hadoop.hbase.testclassification.MediumTests;
30 import org.apache.hadoop.hbase.testclassification.RegionServerTests;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.ClassRule;
35 import org.junit.Test;
36 import org.junit.experimental.categories.Category;
38 import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
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.generated.ClientProtos;
43 import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest;
44 import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanResponse;
46 /**
47 * Testcase to make sure that we always set scanner id in ScanResponse. See HBASE-18000.
49 @Category({ RegionServerTests.class, MediumTests.class })
50 public class TestAlwaysSetScannerId {
52 @ClassRule
53 public static final HBaseClassTestRule CLASS_RULE =
54 HBaseClassTestRule.forClass(TestAlwaysSetScannerId.class);
56 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
58 private static final TableName TABLE_NAME = TableName.valueOf("test");
60 private static final byte[] CF = Bytes.toBytes("cf");
62 private static final byte[] CQ = Bytes.toBytes("cq");
64 private static final int COUNT = 10;
66 private static RegionInfo HRI;
68 private static AsyncConnectionImpl CONN;
70 private static ClientProtos.ClientService.Interface STUB;
72 @BeforeClass
73 public static void setUp() throws Exception {
74 UTIL.startMiniCluster(1);
75 try (Table table = UTIL.createTable(TABLE_NAME, CF)) {
76 for (int i = 0; i < COUNT; i++) {
77 table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
79 HRI = table.getRegionLocator().getAllRegionLocations().get(0).getRegion();
81 CONN =
82 (AsyncConnectionImpl) ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get();
83 STUB = CONN.getRegionServerStub(UTIL.getHBaseCluster().getRegionServer(0).getServerName());
86 @AfterClass
87 public static void tearDown() throws Exception {
88 Closeables.close(CONN, true);
89 UTIL.shutdownMiniCluster();
92 private ScanResponse scan(ScanRequest req) throws IOException {
93 BlockingRpcCallback<ScanResponse> callback = new BlockingRpcCallback<>();
94 STUB.scan(new HBaseRpcControllerImpl(), req, callback);
95 return callback.get();
98 @Test
99 public void test() throws ServiceException, IOException {
100 Scan scan = new Scan();
101 ScanRequest req = RequestConverter.buildScanRequest(HRI.getRegionName(), scan, 1, false);
102 ScanResponse resp = scan(req);
103 assertTrue(resp.hasScannerId());
104 long scannerId = resp.getScannerId();
105 int nextCallSeq = 0;
106 // test next
107 for (int i = 0; i < COUNT / 2; i++) {
108 req = RequestConverter.buildScanRequest(scannerId, 1, false, nextCallSeq++, false, false, -1);
109 resp = scan(req);
110 assertTrue(resp.hasScannerId());
111 assertEquals(scannerId, resp.getScannerId());
113 // test renew
114 req = RequestConverter.buildScanRequest(scannerId, 0, false, nextCallSeq++, false, true, -1);
115 resp = scan(req);
116 assertTrue(resp.hasScannerId());
117 assertEquals(scannerId, resp.getScannerId());
118 // test close
119 req = RequestConverter.buildScanRequest(scannerId, 0, true, false);
120 resp = scan(req);
121 assertTrue(resp.hasScannerId());
122 assertEquals(scannerId, resp.getScannerId());