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.fail;
21  import static org.mockito.Mockito.mock;
22  import static org.mockito.Mockito.when;
23  
24  import java.io.IOException;
25  import java.util.ArrayList;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HBaseConfiguration;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.HConstants;
31  import org.apache.hadoop.hbase.HTableDescriptor;
32  import org.apache.hadoop.hbase.testclassification.SmallTests;
33  import org.apache.hadoop.hbase.MasterNotRunningException;
34  import org.apache.hadoop.hbase.PleaseHoldException;
35  import org.apache.hadoop.hbase.TableName;
36  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
37  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.BalanceRequest;
38  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.CreateTableRequest;
39  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.DispatchMergingRegionsRequest;
40  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.EnableCatalogJanitorRequest;
41  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetTableDescriptorsRequest;
42  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetTableNamesRequest;
43  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsCatalogJanitorEnabledRequest;
44  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.MoveRegionRequest;
45  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.OfflineRegionRequest;
46  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.RunCatalogScanRequest;
47  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.SetBalancerRunningRequest;
48  import org.junit.Test;
49  import org.junit.experimental.categories.Category;
50  import org.mockito.Matchers;
51  import org.mockito.Mockito;
52  import org.mockito.invocation.InvocationOnMock;
53  import org.mockito.stubbing.Answer;
54  import org.mortbay.log.Log;
55  
56  import com.google.protobuf.RpcController;
57  import com.google.protobuf.ServiceException;
58  
59  @Category(SmallTests.class)
60  public class TestHBaseAdminNoCluster {
61    /**
62     * Verify that PleaseHoldException gets retried.
63     * HBASE-8764
64     * @throws IOException
65     * @throws ZooKeeperConnectionException
66     * @throws MasterNotRunningException
67     * @throws ServiceException
68     */
69    @Test
70    public void testMasterMonitorCallableRetries()
71    throws MasterNotRunningException, ZooKeeperConnectionException, IOException, ServiceException {
72      Configuration configuration = HBaseConfiguration.create();
73      // Set the pause and retry count way down.
74      configuration.setLong(HConstants.HBASE_CLIENT_PAUSE, 1);
75      final int count = 10;
76      configuration.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, count);
77      // Get mocked connection.   Getting the connection will register it so when HBaseAdmin is
78      // constructed with same configuration, it will find this mocked connection.
79      ClusterConnection connection = HConnectionTestingUtility.getMockedConnection(configuration);
80      // Mock so we get back the master interface.  Make it so when createTable is called, we throw
81      // the PleaseHoldException.
82      MasterKeepAliveConnection masterAdmin = Mockito.mock(MasterKeepAliveConnection.class);
83      Mockito.when(masterAdmin.createTable((RpcController)Mockito.any(),
84        (CreateTableRequest)Mockito.any())).
85          thenThrow(new ServiceException("Test fail").initCause(new PleaseHoldException("test")));
86      Mockito.when(connection.getKeepAliveMasterService()).thenReturn(masterAdmin);
87      Admin admin = new HBaseAdmin(connection);
88      try {
89        HTableDescriptor htd =
90          new HTableDescriptor(TableName.valueOf("testMasterMonitorCollableRetries"));
91        // Pass any old htable descriptor; not important
92        try {
93          admin.createTable(htd, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
94          fail();
95        } catch (RetriesExhaustedException e) {
96          Log.info("Expected fail", e);
97        }
98        // Assert we were called 'count' times.
99        Mockito.verify(masterAdmin, Mockito.atLeast(count)).createTable((RpcController)Mockito.any(),
100         (CreateTableRequest)Mockito.any());
101     } finally {
102       admin.close();
103       if (connection != null) connection.close();
104     }
105   }
106 
107   @Test
108   public void testMasterOperationsRetries() throws Exception {
109 
110     // Admin.listTables()
111     testMasterOperationIsRetried(new MethodCaller() {
112       @Override
113       public void call(Admin admin) throws Exception {
114         admin.listTables();
115       }
116       @Override
117       public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
118         Mockito.verify(masterAdmin, Mockito.atLeast(count))
119           .getTableDescriptors((RpcController)Mockito.any(),
120             (GetTableDescriptorsRequest)Mockito.any());
121       }
122     });
123 
124     // Admin.listTableNames()
125     testMasterOperationIsRetried(new MethodCaller() {
126       @Override
127       public void call(Admin admin) throws Exception {
128         admin.listTableNames();
129       }
130       @Override
131       public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
132         Mockito.verify(masterAdmin, Mockito.atLeast(count))
133           .getTableNames((RpcController)Mockito.any(),
134             (GetTableNamesRequest)Mockito.any());
135       }
136     });
137 
138     // Admin.getTableDescriptor()
139     testMasterOperationIsRetried(new MethodCaller() {
140       @Override
141       public void call(Admin admin) throws Exception {
142         admin.getTableDescriptor(TableName.valueOf("getTableDescriptor"));
143       }
144       @Override
145       public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
146         Mockito.verify(masterAdmin, Mockito.atLeast(count))
147           .getTableDescriptors((RpcController)Mockito.any(),
148             (GetTableDescriptorsRequest)Mockito.any());
149       }
150     });
151 
152     // Admin.getTableDescriptorsByTableName()
153     testMasterOperationIsRetried(new MethodCaller() {
154       @Override
155       public void call(Admin admin) throws Exception {
156         admin.getTableDescriptorsByTableName(new ArrayList<TableName>());
157       }
158       @Override
159       public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
160         Mockito.verify(masterAdmin, Mockito.atLeast(count))
161           .getTableDescriptors((RpcController)Mockito.any(),
162             (GetTableDescriptorsRequest)Mockito.any());
163       }
164     });
165 
166     // Admin.move()
167     testMasterOperationIsRetried(new MethodCaller() {
168       @Override
169       public void call(Admin admin) throws Exception {
170         admin.move(new byte[0], null);
171       }
172       @Override
173       public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
174         Mockito.verify(masterAdmin, Mockito.atLeast(count))
175           .moveRegion((RpcController)Mockito.any(),
176             (MoveRegionRequest)Mockito.any());
177       }
178     });
179 
180     // Admin.offline()
181     testMasterOperationIsRetried(new MethodCaller() {
182       @Override
183       public void call(Admin admin) throws Exception {
184         admin.offline(new byte[0]);
185       }
186       @Override
187       public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
188         Mockito.verify(masterAdmin, Mockito.atLeast(count))
189           .offlineRegion((RpcController)Mockito.any(),
190             (OfflineRegionRequest)Mockito.any());
191       }
192     });
193 
194     // Admin.setBalancerRunning()
195     testMasterOperationIsRetried(new MethodCaller() {
196       @Override
197       public void call(Admin admin) throws Exception {
198         admin.setBalancerRunning(true, true);
199       }
200       @Override
201       public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
202         Mockito.verify(masterAdmin, Mockito.atLeast(count))
203           .setBalancerRunning((RpcController)Mockito.any(),
204             (SetBalancerRunningRequest)Mockito.any());
205       }
206     });
207 
208     // Admin.balancer()
209     testMasterOperationIsRetried(new MethodCaller() {
210       @Override
211       public void call(Admin admin) throws Exception {
212         admin.balancer();
213       }
214       @Override
215       public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
216         Mockito.verify(masterAdmin, Mockito.atLeast(count))
217           .balance((RpcController)Mockito.any(),
218             (BalanceRequest)Mockito.any());
219       }
220     });
221 
222     // Admin.enabledCatalogJanitor()
223     testMasterOperationIsRetried(new MethodCaller() {
224       @Override
225       public void call(Admin admin) throws Exception {
226         admin.enableCatalogJanitor(true);
227       }
228       @Override
229       public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
230         Mockito.verify(masterAdmin, Mockito.atLeast(count))
231           .enableCatalogJanitor((RpcController)Mockito.any(),
232             (EnableCatalogJanitorRequest)Mockito.any());
233       }
234     });
235 
236     // Admin.runCatalogScan()
237     testMasterOperationIsRetried(new MethodCaller() {
238       @Override
239       public void call(Admin admin) throws Exception {
240         admin.runCatalogScan();
241       }
242       @Override
243       public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
244         Mockito.verify(masterAdmin, Mockito.atLeast(count))
245           .runCatalogScan((RpcController)Mockito.any(),
246             (RunCatalogScanRequest)Mockito.any());
247       }
248     });
249 
250     // Admin.isCatalogJanitorEnabled()
251     testMasterOperationIsRetried(new MethodCaller() {
252       @Override
253       public void call(Admin admin) throws Exception {
254         admin.isCatalogJanitorEnabled();
255       }
256       @Override
257       public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
258         Mockito.verify(masterAdmin, Mockito.atLeast(count))
259           .isCatalogJanitorEnabled((RpcController)Mockito.any(),
260             (IsCatalogJanitorEnabledRequest)Mockito.any());
261       }
262     });
263     // Admin.mergeRegions()
264     testMasterOperationIsRetried(new MethodCaller() {
265       @Override
266       public void call(Admin admin) throws Exception {
267         admin.mergeRegions(new byte[0], new byte[0], true);
268       }
269       @Override
270       public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
271         Mockito.verify(masterAdmin, Mockito.atLeast(count))
272           .dispatchMergingRegions((RpcController)Mockito.any(),
273             (DispatchMergingRegionsRequest)Mockito.any());
274       }
275     });
276   }
277 
278   private static interface MethodCaller {
279     void call(Admin admin) throws Exception;
280     void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception;
281   }
282 
283   private void testMasterOperationIsRetried(MethodCaller caller) throws Exception {
284     Configuration configuration = HBaseConfiguration.create();
285     // Set the pause and retry count way down.
286     configuration.setLong(HConstants.HBASE_CLIENT_PAUSE, 1);
287     final int count = 10;
288     configuration.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, count);
289 
290     ClusterConnection connection = mock(ClusterConnection.class);
291     when(connection.getConfiguration()).thenReturn(configuration);
292     MasterKeepAliveConnection masterAdmin =
293         Mockito.mock(MasterKeepAliveConnection.class, new Answer() {
294           @Override
295           public Object answer(InvocationOnMock invocation) throws Throwable {
296             if (invocation.getMethod().getName().equals("close")) {
297               return null;
298             }
299             throw new MasterNotRunningException(); // all methods will throw an exception
300           }
301         });
302     Mockito.when(connection.getKeepAliveMasterService()).thenReturn(masterAdmin);
303 
304     Admin admin = null;
305     try {
306       admin = Mockito.spy(new HBaseAdmin(connection));
307       // mock the call to getRegion since in the absence of a cluster (which means the meta
308       // is not assigned), getRegion can't function
309       Mockito.doReturn(null).when(((HBaseAdmin)admin)).getRegion(Matchers.<byte[]>any());
310       try {
311         caller.call(admin); // invoke the HBaseAdmin method
312         fail();
313       } catch (RetriesExhaustedException e) {
314         Log.info("Expected fail", e);
315       }
316       // Assert we were called 'count' times.
317       caller.verify(masterAdmin, count);
318     } finally {
319       if (admin != null) {admin.close();}
320     }
321   }
322 }