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.assertNotNull;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.MediumTests;
28  import org.apache.hadoop.hbase.MiniHBaseCluster.MiniHBaseClusterRegionServer;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.junit.AfterClass;
31  import org.junit.BeforeClass;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  /**
36   * Test the scenario where a next() call, while scanning, timeout at client side and getting retried.
37   * This scenario should not result in some data being skipped at RS side.
38   */
39  @Category(MediumTests.class)
40  public class TestClientScannerRPCTimeout {
41    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
42    private static final byte[] FAMILY = Bytes.toBytes("testFamily");
43    private static final byte[] QUALIFIER = Bytes.toBytes("testQualifier");
44    private static final byte[] VALUE = Bytes.toBytes("testValue");
45    private static final int SLAVES = 1;
46    private static final int rpcTimeout = 5 * 1000;
47  
48    @BeforeClass
49    public static void setUpBeforeClass() throws Exception {
50      Configuration conf = TEST_UTIL.getConfiguration();
51      conf.setInt(HConstants.HBASE_RPC_TIMEOUT_KEY, rpcTimeout);
52      conf.setStrings(HConstants.REGION_SERVER_IMPL, RegionServerWithScanTimeout.class.getName());
53      TEST_UTIL.startMiniCluster(SLAVES);
54    }
55  
56    @AfterClass
57    public static void tearDownAfterClass() throws Exception {
58      TEST_UTIL.shutdownMiniCluster();
59    }
60  
61    @Test
62    public void testScannerNextRPCTimesout() throws Exception {
63      byte[] TABLE = Bytes.toBytes("testScannerNextRPCTimesout");
64      HTable ht = TEST_UTIL.createTable(TABLE, FAMILY);
65      putToTable(ht, "row-1");
66      putToTable(ht, "row-2");
67      RegionServerWithScanTimeout.seqNoToSleepOn = 1;
68      Scan scan = new Scan();
69      scan.setCaching(1);
70      ResultScanner scanner = ht.getScanner(scan);
71      Result result = scanner.next();
72      assertNotNull("Expected not null result", result);
73      result = scanner.next();
74      assertNotNull("Expected not null result", result);
75      scanner.close();
76    }
77  
78    private void putToTable(HTable ht, String rowkey) throws IOException {
79      Put put = new Put(rowkey.getBytes());
80      put.add(FAMILY, QUALIFIER, VALUE);
81      ht.put(put);
82    }
83  
84    private static class RegionServerWithScanTimeout extends MiniHBaseClusterRegionServer {
85      private long tableScannerId;
86      private boolean slept;
87      private static long seqNoToSleepOn = -1;
88  
89      public RegionServerWithScanTimeout(Configuration conf) throws IOException,
90          InterruptedException {
91        super(conf);
92      }
93  
94      @Override
95      public long openScanner(byte[] regionName, Scan scan) throws IOException {
96        long scannerId = super.openScanner(regionName, scan);
97        if (!getRegionInfo(regionName).isMetaTable()) {
98          tableScannerId = scannerId;
99        }
100       return scannerId;
101     }
102 
103     @Override
104     public Result[] next(long scannerId, int nbRows, long callSeq) throws IOException {
105       if (!slept && this.tableScannerId == scannerId && seqNoToSleepOn == callSeq) {
106         try {
107           Thread.sleep(rpcTimeout + 500);
108         } catch (InterruptedException e) {
109         }
110         slept = true;
111       }
112       return super.next(scannerId, nbRows, callSeq);
113     }
114   }
115 }