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.jupiter.api.Assertions.assertTrue;
021
022import java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.ipc.RpcClient;
028import org.apache.hadoop.hbase.ipc.RpcClientFactory;
029import org.apache.hadoop.hbase.security.User;
030import org.apache.hadoop.hbase.testclassification.MasterTests;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.apache.hadoop.hbase.zookeeper.ZKUtil;
034import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
035import org.apache.zookeeper.KeeperException;
036import org.junit.jupiter.api.AfterEach;
037import org.junit.jupiter.api.BeforeEach;
038import org.junit.jupiter.api.Tag;
039import org.junit.jupiter.api.Test;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043import org.apache.hbase.thirdparty.com.google.protobuf.BlockingRpcChannel;
044import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
045
046import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
047import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos;
048import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.IsMasterRunningRequest;
049
050@Tag(MasterTests.TAG)
051@Tag(MediumTests.TAG)
052public class TestHMasterRPCException {
053
054  private static final Logger LOG = LoggerFactory.getLogger(TestHMasterRPCException.class);
055
056  private final HBaseTestingUtil testUtil = new HBaseTestingUtil();
057
058  private HMaster master;
059
060  private RpcClient rpcClient;
061
062  @BeforeEach
063  public void setUp() throws Exception {
064    Configuration conf = testUtil.getConfiguration();
065    conf.set(HConstants.MASTER_PORT, "0");
066    conf.setInt(HConstants.ZK_SESSION_TIMEOUT, 2000);
067    testUtil.startMiniZKCluster();
068
069    ZKWatcher watcher = testUtil.getZooKeeperWatcher();
070    ZKUtil.createWithParents(watcher, watcher.getZNodePaths().masterAddressZNode,
071      Bytes.toBytes("fake:123"));
072    master = new HMaster(conf);
073    rpcClient = RpcClientFactory.createClient(conf, HConstants.CLUSTER_ID_DEFAULT);
074  }
075
076  @AfterEach
077  public void tearDown() throws IOException {
078    if (rpcClient != null) {
079      rpcClient.close();
080    }
081    if (master != null) {
082      master.stopMaster();
083    }
084    testUtil.shutdownMiniZKCluster();
085  }
086
087  @Test
088  public void testRPCException() throws IOException, InterruptedException, KeeperException {
089    ServerName sm = master.getServerName();
090    boolean fakeZNodeDelete = false;
091    for (int i = 0; i < 20; i++) {
092      try {
093        BlockingRpcChannel channel = rpcClient.createBlockingRpcChannel(sm, User.getCurrent(), 0);
094        MasterProtos.MasterService.BlockingInterface stub =
095          MasterProtos.MasterService.newBlockingStub(channel);
096        assertTrue(stub.isMasterRunning(null, IsMasterRunningRequest.getDefaultInstance())
097          .getIsMasterRunning());
098        return;
099      } catch (ServiceException ex) {
100        IOException ie = ProtobufUtil.handleRemoteException(ex);
101        // No SocketTimeoutException here. RpcServer is already started after the construction of
102        // HMaster.
103        assertTrue(ie.getMessage().startsWith(
104          "org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet"));
105        LOG.info("Expected exception: ", ie);
106        if (!fakeZNodeDelete) {
107          testUtil.getZooKeeperWatcher().getRecoverableZooKeeper()
108            .delete(testUtil.getZooKeeperWatcher().getZNodePaths().masterAddressZNode, -1);
109          fakeZNodeDelete = true;
110        }
111      }
112      Thread.sleep(1000);
113    }
114  }
115}