View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.client;
21  
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.net.SocketTimeoutException;
26  import java.util.Random;
27  import java.util.concurrent.atomic.AtomicInteger;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.HBaseConfiguration;
33  import org.apache.hadoop.hbase.HBaseTestingUtility;
34  import org.apache.hadoop.hbase.HConstants;
35  import org.apache.hadoop.hbase.MasterNotRunningException;
36  import org.apache.hadoop.hbase.MediumTests;
37  import org.apache.hadoop.hbase.ServerName;
38  import org.apache.hadoop.hbase.ipc.RpcClient;
39  import org.apache.hadoop.hbase.security.User;
40  import org.junit.AfterClass;
41  import org.junit.BeforeClass;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  
45  import com.google.protobuf.BlockingRpcChannel;
46  import com.google.protobuf.Descriptors.MethodDescriptor;
47  import com.google.protobuf.Message;
48  import com.google.protobuf.RpcController;
49  import com.google.protobuf.ServiceException;
50  
51  @Category(MediumTests.class)
52  public class TestClientTimeouts {
53    final Log LOG = LogFactory.getLog(getClass());
54    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
55    protected static int SLAVES = 1;
56  
57   /**
58     * @throws java.lang.Exception
59     */
60    @BeforeClass
61    public static void setUpBeforeClass() throws Exception {
62      TEST_UTIL.startMiniCluster(SLAVES);
63    }
64  
65    /**
66     * @throws java.lang.Exception
67     */
68    @AfterClass
69    public static void tearDownAfterClass() throws Exception {
70      TEST_UTIL.shutdownMiniCluster();
71    }
72  
73    /**
74     * Test that a client that fails an RPC to the master retries properly and
75     * doesn't throw any unexpected exceptions.
76     * @throws Exception
77     */
78    @Test
79    public void testAdminTimeout() throws Exception {
80      long lastLimit = HConstants.DEFAULT_HBASE_CLIENT_PREFETCH_LIMIT;
81      HConnection lastConnection = null;
82      boolean lastFailed = false;
83      int initialInvocations = RandomTimeoutBlockingRpcChannel.invokations.get();
84      RpcClient rpcClient = new RpcClient(TEST_UTIL.getConfiguration(), TEST_UTIL.getClusterKey()) {
85        // Return my own instance, one that does random timeouts
86        @Override
87        public BlockingRpcChannel createBlockingRpcChannel(ServerName sn,
88            User ticket, int rpcTimeout) {
89          return new RandomTimeoutBlockingRpcChannel(this, sn, ticket, rpcTimeout);
90        }
91      };
92      try {
93        for (int i = 0; i < 5 || (lastFailed && i < 100); ++i) {
94          lastFailed = false;
95          // Ensure the HBaseAdmin uses a new connection by changing Configuration.
96          Configuration conf = HBaseConfiguration.create(TEST_UTIL.getConfiguration());
97          conf.setLong(HConstants.HBASE_CLIENT_PREFETCH_LIMIT, ++lastLimit);
98          HBaseAdmin admin = null;
99          try {
100           admin = new HBaseAdmin(conf);
101           HConnection connection = admin.getConnection();
102           assertFalse(connection == lastConnection);
103           lastConnection = connection;
104           // Override the connection's rpc client for timeout testing
105           ((HConnectionManager.HConnectionImplementation)connection).setRpcClient(rpcClient);
106           // run some admin commands
107           HBaseAdmin.checkHBaseAvailable(conf);
108           admin.setBalancerRunning(false, false);
109         } catch (MasterNotRunningException ex) {
110           // Since we are randomly throwing SocketTimeoutExceptions, it is possible to get
111           // a MasterNotRunningException.  It's a bug if we get other exceptions.
112           lastFailed = true;
113         } finally {
114           admin.close();
115         }
116       }
117       // Ensure the RandomTimeoutRpcEngine is actually being used.
118       assertFalse(lastFailed);
119       assertTrue(RandomTimeoutBlockingRpcChannel.invokations.get() > initialInvocations);
120     } finally {
121       rpcClient.stop();
122     }
123   }
124 
125   /**
126    * Blocking rpc channel that goes via hbase rpc.
127    */
128   static class RandomTimeoutBlockingRpcChannel extends RpcClient.BlockingRpcChannelImplementation {
129     private static final Random RANDOM = new Random(System.currentTimeMillis());
130     public static final double CHANCE_OF_TIMEOUT = 0.3;
131     private static AtomicInteger invokations = new AtomicInteger();
132 
133     RandomTimeoutBlockingRpcChannel(final RpcClient rpcClient, final ServerName sn,
134         final User ticket, final int rpcTimeout) {
135       super(rpcClient, sn, ticket, rpcTimeout);
136     }
137 
138     @Override
139     public Message callBlockingMethod(MethodDescriptor md,
140         RpcController controller, Message param, Message returnType)
141         throws ServiceException {
142       invokations.getAndIncrement();
143       if (RANDOM.nextFloat() < CHANCE_OF_TIMEOUT) {
144         // throw a ServiceException, becuase that is the only exception type that
145         // {@link ProtobufRpcEngine} throws.  If this RpcEngine is used with a different
146         // "actual" type, this may not properly mimic the underlying RpcEngine.
147         throw new ServiceException(new SocketTimeoutException("fake timeout"));
148       }
149       return super.callBlockingMethod(md, controller, param, returnType);
150     }
151   }
152 }