View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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.MediumTests;
41  import org.apache.hadoop.hbase.util.Bytes;
42  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
43  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
44  import org.apache.zookeeper.KeeperException;
45  import org.junit.After;
46  import org.junit.Before;
47  import org.junit.Test;
48  import org.junit.experimental.categories.Category;
49  
50  import com.google.protobuf.BlockingRpcChannel;
51  import com.google.protobuf.ServiceException;
52  
53  @Category(MediumTests.class)
54  public class TestHMasterRPCException {
55  
56    private static final Log LOG = LogFactory.getLog(TestHMasterRPCException.class);
57  
58    private final HBaseTestingUtility testUtil = HBaseTestingUtility.createLocalHTU();
59  
60    private HMaster master;
61  
62    private RpcClient rpcClient;
63  
64    @Before
65    public void setUp() throws Exception {
66      Configuration conf = testUtil.getConfiguration();
67      conf.set(HConstants.MASTER_PORT, "0");
68      conf.setInt(HConstants.ZK_SESSION_TIMEOUT, 2000);
69      testUtil.startMiniZKCluster();
70  
71      CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(conf);
72      ZooKeeperWatcher watcher = testUtil.getZooKeeperWatcher();
73      ZKUtil.createWithParents(watcher, watcher.getMasterAddressZNode(), Bytes.toBytes("fake:123"));
74      master = new HMaster(conf, cp);
75      rpcClient = RpcClientFactory.createClient(conf, HConstants.CLUSTER_ID_DEFAULT);
76    }
77  
78    @After
79    public void tearDown() throws IOException {
80      if (rpcClient != null) {
81        rpcClient.close();
82      }
83      if (master != null) {
84        master.stopMaster();
85      }
86      testUtil.shutdownMiniZKCluster();
87    }
88  
89    @Test
90    public void testRPCException() throws IOException, InterruptedException, KeeperException {
91      ServerName sm = master.getServerName();
92      boolean fakeZNodeDelete = false;
93      for (int i = 0; i < 20; i++) {
94        try {
95          BlockingRpcChannel channel = rpcClient.createBlockingRpcChannel(sm, User.getCurrent(), 0);
96          MasterProtos.MasterService.BlockingInterface stub =
97              MasterProtos.MasterService.newBlockingStub(channel);
98          assertTrue(stub.isMasterRunning(null, IsMasterRunningRequest.getDefaultInstance())
99              .getIsMasterRunning());
100         return;
101       } catch (ServiceException ex) {
102         IOException ie = ProtobufUtil.getRemoteException(ex);
103         // No SocketTimeoutException here. RpcServer is already started after the construction of
104         // HMaster.
105         assertTrue(ie.getMessage().startsWith(
106           "org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet"));
107         LOG.info("Expected exception: ", ie);
108         if (!fakeZNodeDelete) {
109           testUtil.getZooKeeperWatcher().getRecoverableZooKeeper()
110               .delete(testUtil.getZooKeeperWatcher().getMasterAddressZNode(), -1);
111           fakeZNodeDelete = true;
112         }
113       }
114       Thread.sleep(1000);
115     }
116   }
117 }