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.HRegionInfo;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.apache.hadoop.hbase.testclassification.RegionServerTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.AfterClass;
032import org.junit.BeforeClass;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
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@Category({ RegionServerTests.class, MediumTests.class })
048public class TestAlwaysSetScannerId {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052      HBaseClassTestRule.forClass(TestAlwaysSetScannerId.class);
053
054  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
055
056  private static final TableName TABLE_NAME = TableName.valueOf("test");
057
058  private static final byte[] CF = Bytes.toBytes("cf");
059
060  private static final byte[] CQ = Bytes.toBytes("cq");
061
062  private static final int COUNT = 10;
063
064  private static HRegionInfo HRI;
065
066  private static ClientProtos.ClientService.BlockingInterface STUB;
067
068  @BeforeClass
069  public static void setUp() throws Exception {
070    UTIL.startMiniCluster(1);
071    try (Table table = UTIL.createTable(TABLE_NAME, CF)) {
072      for (int i = 0; i < COUNT; i++) {
073        table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
074      }
075    }
076    HRI = UTIL.getAdmin().getTableRegions(TABLE_NAME).get(0);
077    STUB = ((ConnectionImplementation) UTIL.getConnection())
078        .getClient(UTIL.getHBaseCluster().getRegionServer(0).getServerName());
079  }
080
081  @AfterClass
082  public static void tearDown() throws Exception {
083    UTIL.shutdownMiniCluster();
084  }
085
086  @Test
087  public void test() throws ServiceException, IOException {
088    Scan scan = new Scan();
089    ScanRequest req = RequestConverter.buildScanRequest(HRI.getRegionName(), scan, 1, false);
090    ScanResponse resp = STUB.scan(null, req);
091    assertTrue(resp.hasScannerId());
092    long scannerId = resp.getScannerId();
093    int nextCallSeq = 0;
094    // test next
095    for (int i = 0; i < COUNT / 2; i++) {
096      req = RequestConverter.buildScanRequest(scannerId, 1, false, nextCallSeq++, false, false, -1);
097      resp = STUB.scan(null, req);
098      assertTrue(resp.hasScannerId());
099      assertEquals(scannerId, resp.getScannerId());
100    }
101    // test renew
102    req = RequestConverter.buildScanRequest(scannerId, 0, false, nextCallSeq++, false, true, -1);
103    resp = STUB.scan(null, req);
104    assertTrue(resp.hasScannerId());
105    assertEquals(scannerId, resp.getScannerId());
106    // test close
107    req = RequestConverter.buildScanRequest(scannerId, 0, true, false);
108    resp = STUB.scan(null, req);
109    assertTrue(resp.hasScannerId());
110    assertEquals(scannerId, resp.getScannerId());
111  }
112}