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.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.Ignore;
49 import org.junit.Test;
50 import org.junit.experimental.categories.Category;
51 import org.mockito.Matchers;
52 import org.mockito.Mockito;
53 import org.mockito.invocation.InvocationOnMock;
54 import org.mockito.stubbing.Answer;
55 import org.mortbay.log.Log;
56
57 import com.google.protobuf.RpcController;
58 import com.google.protobuf.ServiceException;
59
60 @Category(SmallTests.class)
61 public class TestHBaseAdminNoCluster {
62
63
64
65
66
67
68
69
70
71
72
73 @Ignore
74 @Test
75 public void testMasterMonitorCallableRetries()
76 throws MasterNotRunningException, ZooKeeperConnectionException, IOException, ServiceException {
77 Configuration configuration = HBaseConfiguration.create();
78
79 configuration.setLong(HConstants.HBASE_CLIENT_PAUSE, 1);
80 final int count = 10;
81 configuration.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, count);
82
83
84 ClusterConnection connection = HConnectionTestingUtility.getMockedConnection(configuration);
85
86
87 MasterKeepAliveConnection masterAdmin = Mockito.mock(MasterKeepAliveConnection.class);
88 Mockito.when(masterAdmin.createTable((RpcController)Mockito.any(),
89 (CreateTableRequest)Mockito.any())).
90 thenThrow(new ServiceException("Test fail").initCause(new PleaseHoldException("test")));
91 Mockito.when(connection.getKeepAliveMasterService()).thenReturn(masterAdmin);
92 Admin admin = new HBaseAdmin(connection);
93 try {
94 HTableDescriptor htd =
95 new HTableDescriptor(TableName.valueOf("testMasterMonitorCollableRetries"));
96
97 try {
98 admin.createTable(htd, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
99 fail();
100 } catch (RetriesExhaustedException e) {
101 Log.info("Expected fail", e);
102 }
103
104 Mockito.verify(masterAdmin, Mockito.atLeast(count)).createTable((RpcController)Mockito.any(),
105 (CreateTableRequest)Mockito.any());
106 } finally {
107 admin.close();
108 if (connection != null) connection.close();
109 }
110 }
111
112 @Test
113 public void testMasterOperationsRetries() throws Exception {
114
115
116 testMasterOperationIsRetried(new MethodCaller() {
117 @Override
118 public void call(Admin admin) throws Exception {
119 admin.listTables();
120 }
121 @Override
122 public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
123 Mockito.verify(masterAdmin, Mockito.atLeast(count))
124 .getTableDescriptors((RpcController)Mockito.any(),
125 (GetTableDescriptorsRequest)Mockito.any());
126 }
127 });
128
129
130 testMasterOperationIsRetried(new MethodCaller() {
131 @Override
132 public void call(Admin admin) throws Exception {
133 admin.listTableNames();
134 }
135 @Override
136 public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
137 Mockito.verify(masterAdmin, Mockito.atLeast(count))
138 .getTableNames((RpcController)Mockito.any(),
139 (GetTableNamesRequest)Mockito.any());
140 }
141 });
142
143
144 testMasterOperationIsRetried(new MethodCaller() {
145 @Override
146 public void call(Admin admin) throws Exception {
147 admin.getTableDescriptor(TableName.valueOf("getTableDescriptor"));
148 }
149 @Override
150 public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
151 Mockito.verify(masterAdmin, Mockito.atLeast(count))
152 .getTableDescriptors((RpcController)Mockito.any(),
153 (GetTableDescriptorsRequest)Mockito.any());
154 }
155 });
156
157
158 testMasterOperationIsRetried(new MethodCaller() {
159 @Override
160 public void call(Admin admin) throws Exception {
161 admin.getTableDescriptorsByTableName(new ArrayList<TableName>());
162 }
163 @Override
164 public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
165 Mockito.verify(masterAdmin, Mockito.atLeast(count))
166 .getTableDescriptors((RpcController)Mockito.any(),
167 (GetTableDescriptorsRequest)Mockito.any());
168 }
169 });
170
171
172 testMasterOperationIsRetried(new MethodCaller() {
173 @Override
174 public void call(Admin admin) throws Exception {
175 admin.move(new byte[0], null);
176 }
177 @Override
178 public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
179 Mockito.verify(masterAdmin, Mockito.atLeast(count))
180 .moveRegion((RpcController)Mockito.any(),
181 (MoveRegionRequest)Mockito.any());
182 }
183 });
184
185
186 testMasterOperationIsRetried(new MethodCaller() {
187 @Override
188 public void call(Admin admin) throws Exception {
189 admin.offline(new byte[0]);
190 }
191 @Override
192 public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
193 Mockito.verify(masterAdmin, Mockito.atLeast(count))
194 .offlineRegion((RpcController)Mockito.any(),
195 (OfflineRegionRequest)Mockito.any());
196 }
197 });
198
199
200 testMasterOperationIsRetried(new MethodCaller() {
201 @Override
202 public void call(Admin admin) throws Exception {
203 admin.setBalancerRunning(true, true);
204 }
205 @Override
206 public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
207 Mockito.verify(masterAdmin, Mockito.atLeast(count))
208 .setBalancerRunning((RpcController)Mockito.any(),
209 (SetBalancerRunningRequest)Mockito.any());
210 }
211 });
212
213
214 testMasterOperationIsRetried(new MethodCaller() {
215 @Override
216 public void call(Admin admin) throws Exception {
217 admin.balancer();
218 }
219 @Override
220 public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
221 Mockito.verify(masterAdmin, Mockito.atLeast(count))
222 .balance((RpcController)Mockito.any(),
223 (BalanceRequest)Mockito.any());
224 }
225 });
226
227
228 testMasterOperationIsRetried(new MethodCaller() {
229 @Override
230 public void call(Admin admin) throws Exception {
231 admin.enableCatalogJanitor(true);
232 }
233 @Override
234 public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
235 Mockito.verify(masterAdmin, Mockito.atLeast(count))
236 .enableCatalogJanitor((RpcController)Mockito.any(),
237 (EnableCatalogJanitorRequest)Mockito.any());
238 }
239 });
240
241
242 testMasterOperationIsRetried(new MethodCaller() {
243 @Override
244 public void call(Admin admin) throws Exception {
245 admin.runCatalogScan();
246 }
247 @Override
248 public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
249 Mockito.verify(masterAdmin, Mockito.atLeast(count))
250 .runCatalogScan((RpcController)Mockito.any(),
251 (RunCatalogScanRequest)Mockito.any());
252 }
253 });
254
255
256 testMasterOperationIsRetried(new MethodCaller() {
257 @Override
258 public void call(Admin admin) throws Exception {
259 admin.isCatalogJanitorEnabled();
260 }
261 @Override
262 public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
263 Mockito.verify(masterAdmin, Mockito.atLeast(count))
264 .isCatalogJanitorEnabled((RpcController)Mockito.any(),
265 (IsCatalogJanitorEnabledRequest)Mockito.any());
266 }
267 });
268
269 testMasterOperationIsRetried(new MethodCaller() {
270 @Override
271 public void call(Admin admin) throws Exception {
272 admin.mergeRegions(new byte[0], new byte[0], true);
273 }
274 @Override
275 public void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception {
276 Mockito.verify(masterAdmin, Mockito.atLeast(count))
277 .dispatchMergingRegions((RpcController)Mockito.any(),
278 (DispatchMergingRegionsRequest)Mockito.any());
279 }
280 });
281 }
282
283 private static interface MethodCaller {
284 void call(Admin admin) throws Exception;
285 void verify(MasterKeepAliveConnection masterAdmin, int count) throws Exception;
286 }
287
288 private void testMasterOperationIsRetried(MethodCaller caller) throws Exception {
289 Configuration configuration = HBaseConfiguration.create();
290
291 configuration.setLong(HConstants.HBASE_CLIENT_PAUSE, 1);
292 final int count = 10;
293 configuration.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, count);
294
295 ClusterConnection connection = mock(ClusterConnection.class);
296 when(connection.getConfiguration()).thenReturn(configuration);
297 MasterKeepAliveConnection masterAdmin =
298 Mockito.mock(MasterKeepAliveConnection.class, new Answer() {
299 @Override
300 public Object answer(InvocationOnMock invocation) throws Throwable {
301 if (invocation.getMethod().getName().equals("close")) {
302 return null;
303 }
304 throw new MasterNotRunningException();
305 }
306 });
307 Mockito.when(connection.getKeepAliveMasterService()).thenReturn(masterAdmin);
308
309 Admin admin = null;
310 try {
311 admin = Mockito.spy(new HBaseAdmin(connection));
312
313
314 Mockito.doReturn(null).when(((HBaseAdmin)admin)).getRegion(Matchers.<byte[]>any());
315 try {
316 caller.call(admin);
317 fail();
318 } catch (RetriesExhaustedException e) {
319 Log.info("Expected fail", e);
320 }
321
322 caller.verify(masterAdmin, count);
323 } finally {
324 if (admin != null) {admin.close();}
325 }
326 }
327 }