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.HBaseTestingUtil;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
028import org.apache.hadoop.hbase.ipc.HBaseRpcControllerImpl;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.apache.hadoop.hbase.testclassification.RegionServerTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.junit.AfterClass;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
039import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
040
041import org.apache.hadoop.hbase.shaded.protobuf.RequestConverter;
042import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;
043import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest;
044import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanResponse;
045
046/**
047 * Testcase to make sure that we always set scanner id in ScanResponse. See HBASE-18000.
048 */
049@Category({ RegionServerTests.class, MediumTests.class })
050public class TestAlwaysSetScannerId {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TestAlwaysSetScannerId.class);
055
056  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
057
058  private static final TableName TABLE_NAME = TableName.valueOf("test");
059
060  private static final byte[] CF = Bytes.toBytes("cf");
061
062  private static final byte[] CQ = Bytes.toBytes("cq");
063
064  private static final int COUNT = 10;
065
066  private static RegionInfo HRI;
067
068  private static AsyncConnectionImpl CONN;
069
070  private static ClientProtos.ClientService.Interface STUB;
071
072  @BeforeClass
073  public static void setUp() throws Exception {
074    UTIL.startMiniCluster(1);
075    try (Table table = UTIL.createTable(TABLE_NAME, CF)) {
076      for (int i = 0; i < COUNT; i++) {
077        table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
078      }
079      HRI = table.getRegionLocator().getAllRegionLocations().get(0).getRegion();
080    }
081    CONN =
082      (AsyncConnectionImpl) ConnectionFactory.createAsyncConnection(UTIL.getConfiguration()).get();
083    STUB = CONN.getRegionServerStub(UTIL.getHBaseCluster().getRegionServer(0).getServerName());
084  }
085
086  @AfterClass
087  public static void tearDown() throws Exception {
088    Closeables.close(CONN, true);
089    UTIL.shutdownMiniCluster();
090  }
091
092  private ScanResponse scan(ScanRequest req) throws IOException {
093    BlockingRpcCallback<ScanResponse> callback = new BlockingRpcCallback<>();
094    STUB.scan(new HBaseRpcControllerImpl(), req, callback);
095    return callback.get();
096  }
097
098  @Test
099  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());
112    }
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());
123  }
124}