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.*;
21
22 import java.io.IOException;
23 import java.net.SocketTimeoutException;
24 import java.util.concurrent.ExecutorService;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.hbase.TableName;
30 import org.apache.hadoop.hbase.HBaseConfiguration;
31 import org.apache.hadoop.hbase.HConstants;
32 import org.apache.hadoop.hbase.HRegionInfo;
33 import org.apache.hadoop.hbase.HRegionLocation;
34 import org.apache.hadoop.hbase.ServerName;
35 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
36 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService;
37 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService.BlockingInterface;
38 import org.apache.hadoop.hbase.regionserver.RegionServerStoppedException;
39 import org.apache.hadoop.hbase.util.Bytes;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.Ignore;
43 import org.mockito.Mockito;
44
45 import com.google.protobuf.RpcController;
46 import com.google.protobuf.ServiceException;
47
48
49
50
51
52 public class TestClientNoCluster {
53 private static final Log LOG = LogFactory.getLog(TestClientNoCluster.class);
54 private Configuration conf;
55
56 @Before
57 public void setUp() throws Exception {
58 this.conf = HBaseConfiguration.create();
59
60
61
62 this.conf.set("hbase.client.registry.impl", SimpleRegistry.class.getName());
63 }
64
65
66
67
68 static class SimpleRegistry implements Registry {
69 final ServerName META_HOST = new ServerName("10.10.10.10", 60010, 12345);
70
71 @Override
72 public void init(HConnection connection) {
73 }
74
75 @Override
76 public HRegionLocation getMetaRegionLocation() throws IOException {
77 return new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, META_HOST);
78 }
79
80 @Override
81 public String getClusterId() {
82 return HConstants.CLUSTER_ID_DEFAULT;
83 }
84
85 @Override
86 public boolean isTableOnlineState(TableName tableName, boolean enabled)
87 throws IOException {
88 return enabled;
89 }
90
91 @Override
92 public int getCurrentNrHRS() throws IOException {
93 return 1;
94 }
95 }
96
97
98
99
100
101 @Ignore
102 @Test
103 public void testTimeoutAndRetries() throws IOException {
104 Configuration localConfig = HBaseConfiguration.create(this.conf);
105
106 localConfig.set("hbase.client.connection.impl", RpcTimeoutConnection.class.getName());
107 HTable table = new HTable(localConfig, TableName.META_TABLE_NAME);
108 Throwable t = null;
109 LOG.info("Start");
110 try {
111
112 table.exists(new Get(Bytes.toBytes("abc")));
113 } catch (SocketTimeoutException e) {
114
115 LOG.info("Got expected exception", e);
116 t = e;
117 } catch (RetriesExhaustedException e) {
118
119 fail();
120 } finally {
121 table.close();
122 }
123 LOG.info("Stop");
124 assertTrue(t != null);
125 }
126
127
128
129
130
131 @Test
132 public void testRocTimeout() throws IOException {
133 Configuration localConfig = HBaseConfiguration.create(this.conf);
134
135 localConfig.set("hbase.client.connection.impl", RpcTimeoutConnection.class.getName());
136 int pause = 10;
137 localConfig.setInt("hbase.client.pause", pause);
138 localConfig.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 10);
139
140
141
142
143 localConfig.setInt(HConstants.HBASE_CLIENT_META_OPERATION_TIMEOUT, pause - 1);
144 HTable table = new HTable(localConfig, TableName.META_TABLE_NAME);
145 Throwable t = null;
146 try {
147
148 table.exists(new Get(Bytes.toBytes("abc")));
149 } catch (SocketTimeoutException e) {
150
151 LOG.info("Got expected exception", e);
152 t = e;
153 } catch (RetriesExhaustedException e) {
154
155 fail();
156 } finally {
157 table.close();
158 }
159 assertTrue(t != null);
160 }
161
162 @Test
163 public void testDoNotRetryMetaScanner() throws IOException {
164 this.conf.set("hbase.client.connection.impl",
165 RegionServerStoppedOnScannerOpenConnection.class.getName());
166 MetaScanner.metaScan(this.conf, null);
167 }
168
169 @Test
170 public void testDoNotRetryOnScanNext() throws IOException {
171 this.conf.set("hbase.client.connection.impl",
172 RegionServerStoppedOnScannerOpenConnection.class.getName());
173
174
175
176 HTable table = new HTable(this.conf, TableName.META_TABLE_NAME);
177 ResultScanner scanner = table.getScanner(HConstants.CATALOG_FAMILY);
178 try {
179 Result result = null;
180 while ((result = scanner.next()) != null) {
181 LOG.info(result);
182 }
183 } finally {
184 scanner.close();
185 table.close();
186 }
187 }
188
189 @Test
190 public void testRegionServerStoppedOnScannerOpen() throws IOException {
191 this.conf.set("hbase.client.connection.impl",
192 RegionServerStoppedOnScannerOpenConnection.class.getName());
193
194
195
196 HTable table = new HTable(this.conf, TableName.META_TABLE_NAME);
197 ResultScanner scanner = table.getScanner(HConstants.CATALOG_FAMILY);
198 try {
199 Result result = null;
200 while ((result = scanner.next()) != null) {
201 LOG.info(result);
202 }
203 } finally {
204 scanner.close();
205 table.close();
206 }
207 }
208
209
210
211
212 static class ScanOpenNextThenExceptionThenRecoverConnection
213 extends HConnectionManager.HConnectionImplementation {
214 final ClientService.BlockingInterface stub;
215
216 ScanOpenNextThenExceptionThenRecoverConnection(Configuration conf,
217 boolean managed, ExecutorService pool) throws IOException {
218 super(conf, managed);
219
220
221 this.stub = Mockito.mock(ClientService.BlockingInterface.class);
222 long sid = 12345L;
223 try {
224 Mockito.when(stub.scan((RpcController)Mockito.any(),
225 (ClientProtos.ScanRequest)Mockito.any())).
226 thenReturn(ClientProtos.ScanResponse.newBuilder().setScannerId(sid).build()).
227 thenThrow(new ServiceException(new RegionServerStoppedException("From Mockito"))).
228 thenReturn(ClientProtos.ScanResponse.newBuilder().setScannerId(sid).
229 setMoreResults(false).build());
230 } catch (ServiceException e) {
231 throw new IOException(e);
232 }
233 }
234
235 @Override
236 public BlockingInterface getClient(ServerName sn) throws IOException {
237 return this.stub;
238 }
239 }
240
241
242
243
244 static class RegionServerStoppedOnScannerOpenConnection
245 extends HConnectionManager.HConnectionImplementation {
246 final ClientService.BlockingInterface stub;
247
248 RegionServerStoppedOnScannerOpenConnection(Configuration conf, boolean managed,
249 ExecutorService pool) throws IOException {
250 super(conf, managed);
251
252
253 this.stub = Mockito.mock(ClientService.BlockingInterface.class);
254 long sid = 12345L;
255 try {
256 Mockito.when(stub.scan((RpcController)Mockito.any(),
257 (ClientProtos.ScanRequest)Mockito.any())).
258 thenReturn(ClientProtos.ScanResponse.newBuilder().setScannerId(sid).build()).
259 thenThrow(new ServiceException(new RegionServerStoppedException("From Mockito"))).
260 thenReturn(ClientProtos.ScanResponse.newBuilder().setScannerId(sid).
261 setMoreResults(false).build());
262 } catch (ServiceException e) {
263 throw new IOException(e);
264 }
265 }
266
267 @Override
268 public BlockingInterface getClient(ServerName sn) throws IOException {
269 return this.stub;
270 }
271 }
272
273
274
275
276 static class RpcTimeoutConnection
277 extends HConnectionManager.HConnectionImplementation {
278 final ClientService.BlockingInterface stub;
279
280 RpcTimeoutConnection(Configuration conf, boolean managed, ExecutorService pool)
281 throws IOException {
282 super(conf, managed);
283
284 this.stub = Mockito.mock(ClientService.BlockingInterface.class);
285 try {
286 Mockito.when(stub.get((RpcController)Mockito.any(),
287 (ClientProtos.GetRequest)Mockito.any())).
288 thenThrow(new ServiceException(new RegionServerStoppedException("From Mockito")));
289 } catch (ServiceException e) {
290 throw new IOException(e);
291 }
292 }
293
294 @Override
295 public BlockingInterface getClient(ServerName sn) throws IOException {
296 return this.stub;
297 }
298 }
299 }