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.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import java.io.IOException; 024import org.apache.hadoop.hbase.HBaseTestingUtil; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.ipc.BlockingRpcCallback; 027import org.apache.hadoop.hbase.ipc.HBaseRpcControllerImpl; 028import org.apache.hadoop.hbase.testclassification.MediumTests; 029import org.apache.hadoop.hbase.testclassification.RegionServerTests; 030import org.apache.hadoop.hbase.util.Bytes; 031import org.junit.jupiter.api.AfterAll; 032import org.junit.jupiter.api.BeforeAll; 033import org.junit.jupiter.api.Tag; 034import org.junit.jupiter.api.Test; 035 036import org.apache.hbase.thirdparty.com.google.common.io.Closeables; 037import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException; 038 039import org.apache.hadoop.hbase.shaded.protobuf.RequestConverter; 040import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos; 041import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest; 042import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanResponse; 043 044/** 045 * Testcase to make sure that we always set scanner id in ScanResponse. See HBASE-18000. 046 */ 047@Tag(RegionServerTests.TAG) 048@Tag(MediumTests.TAG) 049public class TestAlwaysSetScannerId { 050 051 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 052 053 private static final TableName TABLE_NAME = TableName.valueOf("test"); 054 055 private static final byte[] CF = Bytes.toBytes("cf"); 056 057 private static final byte[] CQ = Bytes.toBytes("cq"); 058 059 private static final int COUNT = 10; 060 061 private static RegionInfo HRI; 062 063 private static AsyncConnectionImpl CONN; 064 065 private static ClientProtos.ClientService.Interface STUB; 066 067 @BeforeAll 068 public static void setUp() throws Exception { 069 UTIL.startMiniCluster(1); 070 try (Table table = UTIL.createTable(TABLE_NAME, CF)) { 071 for (int i = 0; i < COUNT; i++) { 072 table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i))); 073 } 074 HRI = table.getRegionLocator().getAllRegionLocations().get(0).getRegion(); 075 } 076 CONN = 077 (AsyncConnectionImpl) ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get(); 078 STUB = CONN.getRegionServerStub(UTIL.getHBaseCluster().getRegionServer(0).getServerName()); 079 } 080 081 @AfterAll 082 public static void tearDown() throws Exception { 083 Closeables.close(CONN, true); 084 UTIL.shutdownMiniCluster(); 085 } 086 087 private ScanResponse scan(ScanRequest req) throws IOException { 088 BlockingRpcCallback<ScanResponse> callback = new BlockingRpcCallback<>(); 089 STUB.scan(new HBaseRpcControllerImpl(), req, callback); 090 return callback.get(); 091 } 092 093 @Test 094 public void test() throws ServiceException, IOException { 095 Scan scan = new Scan(); 096 ScanRequest req = RequestConverter.buildScanRequest(HRI.getRegionName(), scan, 1, false); 097 ScanResponse resp = scan(req); 098 assertTrue(resp.hasScannerId()); 099 long scannerId = resp.getScannerId(); 100 int nextCallSeq = 0; 101 // test next 102 for (int i = 0; i < COUNT / 2; i++) { 103 req = RequestConverter.buildScanRequest(scannerId, 1, false, nextCallSeq++, false, false, -1); 104 resp = scan(req); 105 assertTrue(resp.hasScannerId()); 106 assertEquals(scannerId, resp.getScannerId()); 107 } 108 // test renew 109 req = RequestConverter.buildScanRequest(scannerId, 0, false, nextCallSeq++, false, true, -1); 110 resp = scan(req); 111 assertTrue(resp.hasScannerId()); 112 assertEquals(scannerId, resp.getScannerId()); 113 // test close 114 req = RequestConverter.buildScanRequest(scannerId, 0, true, false); 115 resp = scan(req); 116 assertTrue(resp.hasScannerId()); 117 assertEquals(scannerId, resp.getScannerId()); 118 } 119}