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.*;
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   * Test client behavior w/o setting up a cluster.
50   * Mock up cluster emissions.
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      // Run my HConnection overrides.  Use my little HConnectionImplementation below which
60      // allows me insert mocks and also use my Registry below rather than the default zk based
61      // one so tests run faster and don't have zk dependency.
62      this.conf.set("hbase.client.registry.impl", SimpleRegistry.class.getName());
63    }
64  
65    /**
66     * Simple cluster registry inserted in place of our usual zookeeper based one.
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     * Remove the @Ignore to try out timeout and retry asettings
99     * @throws IOException
100    */
101   @Ignore 
102   @Test
103   public void testTimeoutAndRetries() throws IOException {
104     Configuration localConfig = HBaseConfiguration.create(this.conf);
105     // This override mocks up our exists/get call to throw a RegionServerStoppedException.
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       // An exists call turns into a get w/ a flag.
112       table.exists(new Get(Bytes.toBytes("abc")));
113     } catch (SocketTimeoutException e) {
114       // I expect this exception.
115       LOG.info("Got expected exception", e);
116       t = e;
117     } catch (RetriesExhaustedException e) {
118       // This is the old, unwanted behavior.  If we get here FAIL!!!
119       fail();
120     } finally {
121       table.close();
122     }
123     LOG.info("Stop");
124     assertTrue(t != null);
125   }
126 
127   /**
128    * Test that operation timeout prevails over rpc default timeout and retries, etc.
129    * @throws IOException
130    */
131   @Test
132   public void testRocTimeout() throws IOException {
133     Configuration localConfig = HBaseConfiguration.create(this.conf);
134     // This override mocks up our exists/get call to throw a RegionServerStoppedException.
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     // Set the operation timeout to be < the pause.  Expectation is that after first pause, we will
140     // fail out of the rpc because the rpc timeout will have been set to the operation tiemout
141     // and it has expired.  Otherwise, if this functionality is broke, all retries will be run --
142     // all ten of them -- and we'll get the RetriesExhaustedException exception.
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       // An exists call turns into a get w/ a flag.
148       table.exists(new Get(Bytes.toBytes("abc")));
149     } catch (SocketTimeoutException e) {
150       // I expect this exception.
151       LOG.info("Got expected exception", e);
152       t = e;
153     } catch (RetriesExhaustedException e) {
154       // This is the old, unwanted behavior.  If we get here FAIL!!!
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     // Go against meta else we will try to find first region for the table on construction which
174     // means we'll have to do a bunch more mocking.  Tests that go against meta only should be
175     // good for a bit of testing.
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     // Go against meta else we will try to find first region for the table on construction which
194     // means we'll have to do a bunch more mocking.  Tests that go against meta only should be
195     // good for a bit of testing.
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    * Override to shutdown going to zookeeper for cluster id and meta location.
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       // Mock up my stub so open scanner returns a scanner id and then on next, we throw
220       // exceptions for three times and then after that, we return no more to scan.
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    * Override to shutdown going to zookeeper for cluster id and meta location.
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       // Mock up my stub so open scanner returns a scanner id and then on next, we throw
252       // exceptions for three times and then after that, we return no more to scan.
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    * Override to check we are setting rpc timeout right.
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       // Mock up my stub so an exists call -- which turns into a get -- throws an exception
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 }