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