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.fail;
23  
24  import java.io.IOException;
25  import java.net.SocketTimeoutException;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.*;
29  import org.apache.hadoop.hbase.CoordinatedStateManager;
30  import org.apache.hadoop.hbase.CoordinatedStateManagerFactory;
31  import org.apache.hadoop.hbase.ipc.RpcClient;
32  import org.apache.hadoop.hbase.ipc.RpcClientFactory;
33  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
34  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos;
35  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsMasterRunningRequest;
36  import org.apache.hadoop.hbase.security.User;
37  import org.apache.hadoop.hbase.testclassification.MediumTests;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  import com.google.protobuf.BlockingRpcChannel;
42  import com.google.protobuf.ServiceException;
43  
44  @Category(MediumTests.class)
45  public class TestHMasterRPCException {
46  
47    @Test
48    public void testRPCException() throws Exception {
49      HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
50      TEST_UTIL.startMiniZKCluster();
51      Configuration conf = TEST_UTIL.getConfiguration();
52      conf.set(HConstants.MASTER_PORT, "0");
53      CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(conf);
54      HMaster hm = new HMaster(conf, cp);
55      ServerName sm = hm.getServerName();
56      RpcClient rpcClient = RpcClientFactory.createClient(conf, HConstants.CLUSTER_ID_DEFAULT);
57      try {
58        int i = 0;
59        //retry the RPC a few times; we have seen SocketTimeoutExceptions if we
60        //try to connect too soon. Retry on SocketTimeoutException.
61        while (i < 20) {
62          try {
63            BlockingRpcChannel channel =
64              rpcClient.createBlockingRpcChannel(sm, User.getCurrent(), 0);
65            MasterProtos.MasterService.BlockingInterface stub =
66              MasterProtos.MasterService.newBlockingStub(channel);
67            stub.isMasterRunning(null, IsMasterRunningRequest.getDefaultInstance());
68            fail();
69          } catch (ServiceException ex) {
70            IOException ie = ProtobufUtil.getRemoteException(ex);
71            if (!(ie instanceof SocketTimeoutException)) {
72              if (ie.getMessage().startsWith("org.apache.hadoop.hbase.ipc." +
73                  "ServerNotRunningYetException: Server is not running yet")) {
74                // Done.  Got the exception we wanted.
75                System.out.println("Expected exception: " + ie.getMessage());
76                return;
77              } else {
78                throw ex;
79              }
80            } else {
81              System.err.println("Got SocketTimeoutException. Will retry. ");
82            }
83          } catch (Throwable t) {
84            fail("Unexpected throwable: " + t);
85          }
86          Thread.sleep(100);
87          i++;
88        }
89        fail();
90      } finally {
91        rpcClient.close();
92      }
93    }
94  }