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.HBaseTestingUtility; 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 HBaseTestingUtility testUtil = HBaseTestingUtility.createLocalHTU(); 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.znodePaths.masterAddressZNode, Bytes.toBytes("fake:123")); 076 master = new HMaster(conf); 077 rpcClient = RpcClientFactory.createClient(conf, HConstants.CLUSTER_ID_DEFAULT); 078 } 079 080 @After 081 public void tearDown() throws IOException { 082 if (rpcClient != null) { 083 rpcClient.close(); 084 } 085 if (master != null) { 086 master.stopMaster(); 087 } 088 testUtil.shutdownMiniZKCluster(); 089 } 090 091 @Test 092 public void testRPCException() throws IOException, InterruptedException, KeeperException { 093 ServerName sm = master.getServerName(); 094 boolean fakeZNodeDelete = false; 095 for (int i = 0; i < 20; i++) { 096 try { 097 BlockingRpcChannel channel = rpcClient.createBlockingRpcChannel(sm, User.getCurrent(), 0); 098 MasterProtos.MasterService.BlockingInterface stub = 099 MasterProtos.MasterService.newBlockingStub(channel); 100 assertTrue(stub.isMasterRunning(null, IsMasterRunningRequest.getDefaultInstance()) 101 .getIsMasterRunning()); 102 return; 103 } catch (ServiceException ex) { 104 IOException ie = ProtobufUtil.handleRemoteException(ex); 105 // No SocketTimeoutException here. RpcServer is already started after the construction of 106 // HMaster. 107 assertTrue(ie.getMessage().startsWith( 108 "org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet")); 109 LOG.info("Expected exception: ", ie); 110 if (!fakeZNodeDelete) { 111 testUtil.getZooKeeperWatcher().getRecoverableZooKeeper() 112 .delete(testUtil.getZooKeeperWatcher().znodePaths.masterAddressZNode, -1); 113 fakeZNodeDelete = true; 114 } 115 } 116 Thread.sleep(1000); 117 } 118 } 119}