HBASE-24033 Add ut for loading the corrupt recovered hfiles (#1322)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / TestHMasterRPCException.java
blob71cc49ec927d3105b5cb75326070331da6958fbb
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.master;
20 import static org.junit.Assert.assertTrue;
22 import java.io.IOException;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.HBaseClassTestRule;
25 import org.apache.hadoop.hbase.HBaseTestingUtility;
26 import org.apache.hadoop.hbase.HConstants;
27 import org.apache.hadoop.hbase.ServerName;
28 import org.apache.hadoop.hbase.ipc.RpcClient;
29 import org.apache.hadoop.hbase.ipc.RpcClientFactory;
30 import org.apache.hadoop.hbase.security.User;
31 import org.apache.hadoop.hbase.testclassification.MasterTests;
32 import org.apache.hadoop.hbase.testclassification.MediumTests;
33 import org.apache.hadoop.hbase.util.Bytes;
34 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
35 import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
36 import org.apache.zookeeper.KeeperException;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.ClassRule;
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import org.apache.hbase.thirdparty.com.google.protobuf.BlockingRpcChannel;
46 import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
48 import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
49 import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos;
50 import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.IsMasterRunningRequest;
52 @Category({ MasterTests.class, MediumTests.class })
53 public class TestHMasterRPCException {
55 @ClassRule
56 public static final HBaseClassTestRule CLASS_RULE =
57 HBaseClassTestRule.forClass(TestHMasterRPCException.class);
59 private static final Logger LOG = LoggerFactory.getLogger(TestHMasterRPCException.class);
61 private final HBaseTestingUtility testUtil = new HBaseTestingUtility();
63 private HMaster master;
65 private RpcClient rpcClient;
67 @Before
68 public void setUp() throws Exception {
69 Configuration conf = testUtil.getConfiguration();
70 conf.set(HConstants.MASTER_PORT, "0");
71 conf.setInt(HConstants.ZK_SESSION_TIMEOUT, 2000);
72 testUtil.startMiniZKCluster();
74 ZKWatcher watcher = testUtil.getZooKeeperWatcher();
75 ZKUtil.createWithParents(watcher, watcher.getZNodePaths().masterAddressZNode,
76 Bytes.toBytes("fake:123"));
77 master = new HMaster(conf);
78 rpcClient = RpcClientFactory.createClient(conf, HConstants.CLUSTER_ID_DEFAULT);
81 @After
82 public void tearDown() throws IOException {
83 if (rpcClient != null) {
84 rpcClient.close();
86 if (master != null) {
87 master.stopMaster();
89 testUtil.shutdownMiniZKCluster();
92 @Test
93 public void testRPCException() throws IOException, InterruptedException, KeeperException {
94 ServerName sm = master.getServerName();
95 boolean fakeZNodeDelete = false;
96 for (int i = 0; i < 20; i++) {
97 try {
98 BlockingRpcChannel channel = rpcClient.createBlockingRpcChannel(sm, User.getCurrent(), 0);
99 MasterProtos.MasterService.BlockingInterface stub =
100 MasterProtos.MasterService.newBlockingStub(channel);
101 assertTrue(stub.isMasterRunning(null, IsMasterRunningRequest.getDefaultInstance())
102 .getIsMasterRunning());
103 return;
104 } catch (ServiceException ex) {
105 IOException ie = ProtobufUtil.handleRemoteException(ex);
106 // No SocketTimeoutException here. RpcServer is already started after the construction of
107 // HMaster.
108 assertTrue(ie.getMessage().startsWith(
109 "org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet"));
110 LOG.info("Expected exception: ", ie);
111 if (!fakeZNodeDelete) {
112 testUtil.getZooKeeperWatcher().getRecoverableZooKeeper()
113 .delete(testUtil.getZooKeeperWatcher().getZNodePaths().masterAddressZNode, -1);
114 fakeZNodeDelete = true;
117 Thread.sleep(1000);