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.TableName; 027import org.apache.hadoop.hbase.testclassification.MediumTests; 028import org.apache.hadoop.hbase.testclassification.RegionServerTests; 029import org.apache.hadoop.hbase.util.Bytes; 030import org.junit.AfterClass; 031import org.junit.BeforeClass; 032import org.junit.ClassRule; 033import org.junit.Test; 034import org.junit.experimental.categories.Category; 035 036import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException; 037 038import org.apache.hadoop.hbase.shaded.protobuf.RequestConverter; 039import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos; 040import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest; 041import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanResponse; 042 043/** 044 * Testcase to make sure that we always set scanner id in ScanResponse. See HBASE-18000. 045 */ 046@Category({ RegionServerTests.class, MediumTests.class }) 047public class TestAlwaysSetScannerId { 048 049 @ClassRule 050 public static final HBaseClassTestRule CLASS_RULE = 051 HBaseClassTestRule.forClass(TestAlwaysSetScannerId.class); 052 053 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 054 055 private static final TableName TABLE_NAME = TableName.valueOf("test"); 056 057 private static final byte[] CF = Bytes.toBytes("cf"); 058 059 private static final byte[] CQ = Bytes.toBytes("cq"); 060 061 private static final int COUNT = 10; 062 063 private static RegionInfo HRI; 064 065 private static ClientProtos.ClientService.BlockingInterface STUB; 066 067 @BeforeClass 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 STUB = ((ConnectionImplementation) UTIL.getConnection()) 077 .getClient(UTIL.getHBaseCluster().getRegionServer(0).getServerName()); 078 } 079 080 @AfterClass 081 public static void tearDown() throws Exception { 082 UTIL.shutdownMiniCluster(); 083 } 084 085 @Test 086 public void test() throws ServiceException, IOException { 087 Scan scan = new Scan(); 088 ScanRequest req = RequestConverter.buildScanRequest(HRI.getRegionName(), scan, 1, false); 089 ScanResponse resp = STUB.scan(null, req); 090 assertTrue(resp.hasScannerId()); 091 long scannerId = resp.getScannerId(); 092 int nextCallSeq = 0; 093 // test next 094 for (int i = 0; i < COUNT / 2; i++) { 095 req = RequestConverter.buildScanRequest(scannerId, 1, false, nextCallSeq++, false, false, -1); 096 resp = STUB.scan(null, req); 097 assertTrue(resp.hasScannerId()); 098 assertEquals(scannerId, resp.getScannerId()); 099 } 100 // test renew 101 req = RequestConverter.buildScanRequest(scannerId, 0, false, nextCallSeq++, false, true, -1); 102 resp = STUB.scan(null, req); 103 assertTrue(resp.hasScannerId()); 104 assertEquals(scannerId, resp.getScannerId()); 105 // test close 106 req = RequestConverter.buildScanRequest(scannerId, 0, true, false); 107 resp = STUB.scan(null, req); 108 assertTrue(resp.hasScannerId()); 109 assertEquals(scannerId, resp.getScannerId()); 110 } 111}