View Javadoc

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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.IOException;
24  
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.client.ConnectionManager.HConnectionImplementation;
29  import org.apache.hadoop.hbase.protobuf.RequestConverter;
30  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
31  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanRequest;
32  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanResponse;
33  import org.apache.hadoop.hbase.testclassification.MediumTests;
34  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.junit.AfterClass;
37  import org.junit.BeforeClass;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  import com.google.protobuf.ServiceException;
42  
43  /**
44   * Testcase to make sure that we always set scanner id in ScanResponse. See HBASE-18000.
45   */
46  @Category({ RegionServerTests.class, MediumTests.class })
47  public class TestAlwaysSetScannerId {
48  
49    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
50  
51    private static final TableName TABLE_NAME = TableName.valueOf("test");
52  
53    private static final byte[] CF = Bytes.toBytes("cf");
54  
55    private static final byte[] CQ = Bytes.toBytes("cq");
56  
57    private static final int COUNT = 10;
58  
59    private static HRegionInfo HRI;
60  
61    private static ClientProtos.ClientService.BlockingInterface STUB;
62  
63    @BeforeClass
64    public static void setUp() throws Exception {
65      UTIL.startMiniCluster(1);
66      try (Table table = UTIL.createTable(TABLE_NAME, CF)) {
67        for (int i = 0; i < COUNT; i++) {
68          table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
69        }
70      }
71      HRI = UTIL.getHBaseAdmin().getTableRegions(TABLE_NAME).get(0);
72      STUB = ((HConnectionImplementation)UTIL.getConnection())
73          .getClient(UTIL.getHBaseCluster().getRegionServer(0).getServerName());
74    }
75  
76    @AfterClass
77    public static void tearDown() throws Exception {
78      UTIL.shutdownMiniCluster();
79    }
80  
81    @Test
82    public void test() throws ServiceException, IOException {
83      Scan scan = new Scan();
84      ScanRequest req = RequestConverter.buildScanRequest(HRI.getRegionName(), scan, 1, false);
85      ScanResponse resp = STUB.scan(null, req);
86      assertTrue(resp.hasScannerId());
87      long scannerId = resp.getScannerId();
88      int nextCallSeq = 0;
89      // test next
90      for (int i = 0; i < COUNT / 2; i++) {
91        req = RequestConverter.buildScanRequest(scannerId, 1, false, nextCallSeq++, false, false, -1);
92        resp = STUB.scan(null, req);
93        assertTrue(resp.hasScannerId());
94        assertEquals(scannerId, resp.getScannerId());
95      }
96      // test renew
97      req = RequestConverter.buildScanRequest(scannerId, 0, false, nextCallSeq++, false, true, -1);
98      resp = STUB.scan(null, req);
99      assertTrue(resp.hasScannerId());
100     assertEquals(scannerId, resp.getScannerId());
101     // test close
102     req = RequestConverter.buildScanRequest(scannerId, 0, true, false);
103     resp = STUB.scan(null, req);
104     assertTrue(resp.hasScannerId());
105     assertEquals(scannerId, resp.getScannerId());
106   }
107 }