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.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         // No SocketTimeoutException here. RpcServer is already started after the construction of
105         // HMaster.
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 }