1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
37
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 }