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.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
63
64
65
66
67
68
69 @Test
70 public void testMasterMonitorCallableRetries()
71 throws MasterNotRunningException, ZooKeeperConnectionException, IOException, ServiceException {
72 Configuration configuration = HBaseConfiguration.create();
73
74 configuration.setLong(HConstants.HBASE_CLIENT_PAUSE, 1);
75 final int count = 10;
76 configuration.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, count);
77
78
79 ClusterConnection connection = HConnectionTestingUtility.getMockedConnection(configuration);
80
81
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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();
300 }
301 });
302 Mockito.when(connection.getKeepAliveMasterService()).thenReturn(masterAdmin);
303
304 Admin admin = null;
305 try {
306 admin = Mockito.spy(new HBaseAdmin(connection));
307
308
309 Mockito.doReturn(null).when(((HBaseAdmin)admin)).getRegion(Matchers.<byte[]>any());
310 try {
311 caller.call(admin);
312 fail();
313 } catch (RetriesExhaustedException e) {
314 Log.info("Expected fail", e);
315 }
316
317 caller.verify(masterAdmin, count);
318 } finally {
319 if (admin != null) {admin.close();}
320 }
321 }
322 }