001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.ipc.RpcClient;
029import org.apache.hadoop.hbase.ipc.RpcClientFactory;
030import org.apache.hadoop.hbase.security.User;
031import org.apache.hadoop.hbase.testclassification.MasterTests;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.hadoop.hbase.zookeeper.ZKUtil;
035import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
036import org.apache.zookeeper.KeeperException;
037import org.junit.After;
038import org.junit.Before;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045import org.apache.hbase.thirdparty.com.google.protobuf.BlockingRpcChannel;
046import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
047
048import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
049import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos;
050import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.IsMasterRunningRequest;
051
052@Category({ MasterTests.class, MediumTests.class })
053public class TestHMasterRPCException {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057    HBaseClassTestRule.forClass(TestHMasterRPCException.class);
058
059  private static final Logger LOG = LoggerFactory.getLogger(TestHMasterRPCException.class);
060
061  private final HBaseTestingUtil testUtil = new HBaseTestingUtil();
062
063  private HMaster master;
064
065  private RpcClient rpcClient;
066
067  @Before
068  public void setUp() throws Exception {
069    Configuration conf = testUtil.getConfiguration();
070    conf.set(HConstants.MASTER_PORT, "0");
071    conf.setInt(HConstants.ZK_SESSION_TIMEOUT, 2000);
072    testUtil.startMiniZKCluster();
073
074    ZKWatcher watcher = testUtil.getZooKeeperWatcher();
075    ZKUtil.createWithParents(watcher, watcher.getZNodePaths().masterAddressZNode,
076      Bytes.toBytes("fake:123"));
077    master = new HMaster(conf);
078    rpcClient = RpcClientFactory.createClient(conf, HConstants.CLUSTER_ID_DEFAULT);
079  }
080
081  @After
082  public void tearDown() throws IOException {
083    if (rpcClient != null) {
084      rpcClient.close();
085    }
086    if (master != null) {
087      master.stopMaster();
088    }
089    testUtil.shutdownMiniZKCluster();
090  }
091
092  @Test
093  public void testRPCException() throws IOException, InterruptedException, KeeperException {
094    ServerName sm = master.getServerName();
095    boolean fakeZNodeDelete = false;
096    for (int i = 0; i < 20; i++) {
097      try {
098        BlockingRpcChannel channel = rpcClient.createBlockingRpcChannel(sm, User.getCurrent(), 0);
099        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;
115        }
116      }
117      Thread.sleep(1000);
118    }
119  }
120}