1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.master;
21
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
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.CoordinatedStateManager;
30 import org.apache.hadoop.hbase.CoordinatedStateManagerFactory;
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.HConstants;
33 import org.apache.hadoop.hbase.ServerName;
34 import org.apache.hadoop.hbase.ipc.RpcClient;
35 import org.apache.hadoop.hbase.ipc.RpcClientFactory;
36 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
37 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos;
38 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsMasterRunningRequest;
39 import org.apache.hadoop.hbase.security.User;
40 import org.apache.hadoop.hbase.testclassification.MasterTests;
41 import org.apache.hadoop.hbase.testclassification.MediumTests;
42 import org.apache.hadoop.hbase.util.Bytes;
43 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
44 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
45 import org.apache.zookeeper.KeeperException;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.junit.experimental.categories.Category;
50
51 import com.google.protobuf.BlockingRpcChannel;
52 import com.google.protobuf.ServiceException;
53
54 @Category({ MasterTests.class, MediumTests.class })
55 public class TestHMasterRPCException {
56
57 private static final Log LOG = LogFactory.getLog(TestHMasterRPCException.class);
58
59 private final HBaseTestingUtility testUtil = HBaseTestingUtility.createLocalHTU();
60
61 private HMaster master;
62
63 private RpcClient rpcClient;
64
65 @Before
66 public void setUp() throws Exception {
67 Configuration conf = testUtil.getConfiguration();
68 conf.set(HConstants.MASTER_PORT, "0");
69 conf.setInt(HConstants.ZK_SESSION_TIMEOUT, 2000);
70 testUtil.startMiniZKCluster();
71
72 CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(conf);
73 ZooKeeperWatcher watcher = testUtil.getZooKeeperWatcher();
74 ZKUtil.createWithParents(watcher, watcher.getMasterAddressZNode(), Bytes.toBytes("fake:123"));
75 master = new HMaster(conf, cp);
76 rpcClient = RpcClientFactory.createClient(conf, HConstants.CLUSTER_ID_DEFAULT);
77 }
78
79 @After
80 public void tearDown() throws IOException {
81 if (rpcClient != null) {
82 rpcClient.close();
83 }
84 if (master != null) {
85 master.stopMaster();
86 }
87 testUtil.shutdownMiniZKCluster();
88 }
89
90 @Test
91 public void testRPCException() throws IOException, InterruptedException, KeeperException {
92 ServerName sm = master.getServerName();
93 boolean fakeZNodeDelete = false;
94 for (int i = 0; i < 20; i++) {
95 try {
96 BlockingRpcChannel channel = rpcClient.createBlockingRpcChannel(sm, User.getCurrent(), 0);
97 MasterProtos.MasterService.BlockingInterface stub =
98 MasterProtos.MasterService.newBlockingStub(channel);
99 assertTrue(stub.isMasterRunning(null, IsMasterRunningRequest.getDefaultInstance())
100 .getIsMasterRunning());
101 return;
102 } catch (ServiceException ex) {
103 IOException ie = ProtobufUtil.getRemoteException(ex);
104
105
106 assertTrue(ie.getMessage().startsWith(
107 "org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet"));
108 LOG.info("Expected exception: ", ie);
109 if (!fakeZNodeDelete) {
110 testUtil.getZooKeeperWatcher().getRecoverableZooKeeper()
111 .delete(testUtil.getZooKeeperWatcher().getMasterAddressZNode(), -1);
112 fakeZNodeDelete = true;
113 }
114 }
115 Thread.sleep(1000);
116 }
117 }
118 }